IndexField.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. IndexField Class
  8. Windows implementation
  9. Field data layout
  10. [4 bytes] Position
  11. [4 bytes] Data Type
  12. [1 byte] Name Length
  13. [Name Length bytes] Name (UTF-8)
  14. --------------------------------------------------------------------------- */
  15. #include "../nde.h"
  16. #include "../ndestring.h"
  17. //---------------------------------------------------------------------------
  18. IndexField::IndexField(unsigned char id, int Pos, int type, const wchar_t *FieldName)
  19. {
  20. InitField();
  21. Type = FIELD_INDEX;
  22. Name = ndestring_wcsdup(FieldName);
  23. ID = id;
  24. Position = Pos;
  25. DataType = type;
  26. }
  27. //---------------------------------------------------------------------------
  28. void IndexField::InitField(void)
  29. {
  30. index = 0;
  31. Type = FIELD_INDEX;
  32. Name = NULL;
  33. ID = 0;
  34. Position = -1;
  35. DataType = FIELD_UNKNOWN;
  36. }
  37. //---------------------------------------------------------------------------
  38. IndexField::IndexField()
  39. {
  40. InitField();
  41. }
  42. //---------------------------------------------------------------------------
  43. IndexField::~IndexField()
  44. {
  45. ndestring_release(Name);
  46. delete index;
  47. }
  48. //---------------------------------------------------------------------------
  49. void IndexField::ReadTypedData(const uint8_t *data, size_t len)
  50. {
  51. unsigned char c;
  52. int pos=0;
  53. CHECK_INT(len);
  54. Position = GET_INT(); pos += 4;
  55. CHECK_INT(len);
  56. DataType = GET_INT(); pos += 4;
  57. CHECK_CHAR(len);
  58. c = GET_CHAR(); pos++;
  59. if (c)
  60. {
  61. CHECK_BIN(len, c);
  62. int string_length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(data+pos), c, 0, 0);
  63. Name = ndestring_malloc((string_length+1)*sizeof(wchar_t));
  64. MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(data+pos), c, Name, string_length);
  65. Name[string_length]=0;
  66. }
  67. }
  68. //---------------------------------------------------------------------------
  69. void IndexField::WriteTypedData(uint8_t *data, size_t len)
  70. {
  71. int pos=0;
  72. CHECK_INT(len);
  73. PUT_INT(Position); pos += 4;
  74. CHECK_INT(len);
  75. PUT_INT(DataType); pos += 4;
  76. CHECK_CHAR(len);
  77. CHECK_CHAR(len);
  78. if (Name)
  79. {
  80. size_t string_length = WideCharToMultiByte(CP_UTF8, 0, Name, -1, 0, 0, 0, 0);
  81. if (string_length)
  82. {
  83. PUT_CHAR((uint8_t)string_length-1);
  84. pos++;
  85. CHECK_BIN(len, string_length-1);
  86. WideCharToMultiByte(CP_UTF8, 0, Name, -1, (LPSTR)(data+pos), (int)string_length-1, 0, 0);
  87. }
  88. else
  89. {
  90. PUT_CHAR(0);
  91. }
  92. }
  93. else
  94. {
  95. PUT_CHAR(0);
  96. }
  97. }
  98. //---------------------------------------------------------------------------
  99. wchar_t *IndexField::GetIndexName(void)
  100. {
  101. return Name;
  102. }
  103. //---------------------------------------------------------------------------
  104. size_t IndexField::GetDataSize(void)
  105. {
  106. size_t s=9;
  107. if (Name)
  108. {
  109. int string_length=WideCharToMultiByte(CP_UTF8, 0, Name, -1, 0, 0, 0, 0);
  110. if (string_length)
  111. s+=string_length-1;
  112. }
  113. s++;
  114. return s;
  115. }
  116. //---------------------------------------------------------------------------
  117. int IndexField::Compare(Field * /*Entry*/)
  118. {
  119. return 0;
  120. }
  121. //---------------------------------------------------------------------------
  122. int IndexField::TranslateToIndex(int Id, IndexField *toindex)
  123. {
  124. if (index && toindex && toindex->index)
  125. return index->TranslateIndex(Id, toindex->index);
  126. return -1;
  127. }