IndexField.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. IndexField Class
  8. --------------------------------------------------------------------------- */
  9. #include "nde.h"
  10. //---------------------------------------------------------------------------
  11. IndexField::IndexField(unsigned char id, int Pos, int type, CFStringRef FieldName)
  12. {
  13. InitField();
  14. Type = FIELD_INDEX;
  15. Name = (CFStringRef)CFRetain(FieldName);
  16. ID = id;
  17. Position = Pos;
  18. DataType = type;
  19. }
  20. //---------------------------------------------------------------------------
  21. void IndexField::InitField(void)
  22. {
  23. index = 0;
  24. Type = FIELD_INDEX;
  25. Name = NULL;
  26. ID = 0;
  27. Position = -1;
  28. DataType = FIELD_UNKNOWN;
  29. }
  30. //---------------------------------------------------------------------------
  31. IndexField::IndexField()
  32. {
  33. InitField();
  34. }
  35. //---------------------------------------------------------------------------
  36. IndexField::~IndexField()
  37. {
  38. if (Name)
  39. CFRelease(Name);
  40. delete index;
  41. }
  42. //---------------------------------------------------------------------------
  43. void IndexField::ReadTypedData(const uint8_t *data, size_t len)
  44. {
  45. unsigned char c;
  46. int pos=0;
  47. CHECK_INT(len);
  48. Position = GET_INT(); pos += 4;
  49. CHECK_INT(len);
  50. DataType = GET_INT(); pos += 4;
  51. CHECK_CHAR(len);
  52. c = GET_CHAR(); pos++;
  53. if (c)
  54. {
  55. CHECK_BIN(len, c);
  56. if (Name)
  57. CFRelease(Name);
  58. Name = CFStringCreateWithBytes(kCFAllocatorDefault, data+pos, c, kCFStringEncodingUTF8, false);
  59. }
  60. }
  61. //---------------------------------------------------------------------------
  62. void IndexField::WriteTypedData(uint8_t *data, size_t len)
  63. {
  64. int pos=0;
  65. CHECK_INT(len);
  66. PUT_INT(Position); pos += 4;
  67. CHECK_INT(len);
  68. PUT_INT(DataType); pos += 4;
  69. CHECK_CHAR(len);
  70. if (Name)
  71. {
  72. CFIndex lengthRequired=0;
  73. CFStringGetBytes(Name, CFRangeMake(0, CFStringGetLength(Name)), kCFStringEncodingUTF8, 0, false, NULL, 0, &lengthRequired);
  74. CHECK_BIN(len, lengthRequired+1);
  75. PUT_CHAR(lengthRequired); pos++;
  76. CFStringGetBytes(Name, CFRangeMake(0, CFStringGetLength(Name)), kCFStringEncodingUTF8, 0, false, data+pos, lengthRequired, 0);
  77. }
  78. }
  79. //---------------------------------------------------------------------------
  80. CFStringRef IndexField::GetIndexName(void)
  81. {
  82. return Name;
  83. }
  84. //---------------------------------------------------------------------------
  85. size_t IndexField::GetDataSize(void)
  86. {
  87. if (Name)
  88. {
  89. CFIndex lengthRequired=0;
  90. CFStringGetBytes(Name, CFRangeMake(0, CFStringGetLength(Name)), kCFStringEncodingUTF8, 0, false, NULL, 0, &lengthRequired);
  91. return lengthRequired+9;
  92. }
  93. else
  94. return 9;
  95. }
  96. //---------------------------------------------------------------------------
  97. int IndexField::Compare(Field * /*Entry*/)
  98. {
  99. return 0;
  100. }
  101. //---------------------------------------------------------------------------
  102. int IndexField::TranslateToIndex(int Id, IndexField *toindex)
  103. {
  104. if (index && toindex && toindex->index)
  105. return index->TranslateIndex(Id, toindex->index);
  106. return -1;
  107. }