IndexField.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. IndexField Class
  8. Android (linux) 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 char *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. Name = ndestring_wcsndup((const char *)(data+pos), c);
  63. }
  64. }
  65. //---------------------------------------------------------------------------
  66. void IndexField::WriteTypedData(uint8_t *data, size_t len)
  67. {
  68. int pos=0;
  69. CHECK_INT(len);
  70. PUT_INT(Position); pos += 4;
  71. CHECK_INT(len);
  72. PUT_INT(DataType); pos += 4;
  73. CHECK_CHAR(len);
  74. CHECK_CHAR(len);
  75. if (Name)
  76. {
  77. int string_length = strlen(Name);
  78. if (string_length)
  79. {
  80. PUT_CHAR(string_length);
  81. pos++;
  82. CHECK_BIN(len, string_length);
  83. PUT_BINARY(data, (const uint8_t *)Name, string_length, pos);
  84. }
  85. else
  86. {
  87. PUT_CHAR(0);
  88. }
  89. }
  90. else
  91. {
  92. PUT_CHAR(0);
  93. }
  94. }
  95. //---------------------------------------------------------------------------
  96. char *IndexField::GetIndexName(void)
  97. {
  98. return Name;
  99. }
  100. //---------------------------------------------------------------------------
  101. size_t IndexField::GetDataSize(void)
  102. {
  103. size_t s=9;
  104. if (Name)
  105. {
  106. s+=strlen(Name);
  107. }
  108. s++;
  109. return s;
  110. }
  111. //---------------------------------------------------------------------------
  112. int IndexField::Compare(Field * /*Entry*/)
  113. {
  114. return 0;
  115. }
  116. //---------------------------------------------------------------------------
  117. int IndexField::TranslateToIndex(int Id, IndexField *toindex)
  118. {
  119. if (index && toindex && toindex->index)
  120. return index->TranslateIndex(Id, toindex->index);
  121. return -1;
  122. }