ColumnField.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. ColumnField Class
  8. Mac OS X implementation
  9. Field data layout:
  10. [1 byte] Field Type
  11. [1 byte] Unused (maybe convert to 'searchable')
  12. [1 byte] Name Length
  13. [Name Length bytes] Name (UTF-8)
  14. --------------------------------------------------------------------------- */
  15. #include "../nde.h"
  16. //---------------------------------------------------------------------------
  17. ColumnField::ColumnField(unsigned char FieldID, CFStringRef FieldName, unsigned char FieldType, Table *parentTable)
  18. {
  19. InitField();
  20. Type = FIELD_COLUMN;
  21. MyType = FieldType;
  22. Name = (CFStringRef)CFRetain(FieldName);
  23. ID = FieldID;
  24. }
  25. //---------------------------------------------------------------------------
  26. void ColumnField::InitField(void)
  27. {
  28. searchable = false;
  29. MyType = FIELD_UNKNOWN;
  30. Type = FIELD_COLUMN;
  31. Name = NULL;
  32. ID = 0;
  33. }
  34. //---------------------------------------------------------------------------
  35. ColumnField::ColumnField()
  36. {
  37. InitField();
  38. }
  39. //---------------------------------------------------------------------------
  40. ColumnField::~ColumnField()
  41. {
  42. if (Name)
  43. CFRelease(Name);
  44. }
  45. void ColumnField::SetSearchable(bool val)
  46. {
  47. searchable=val;
  48. }
  49. bool ColumnField::IsSearchableField() const
  50. {
  51. return searchable;
  52. }
  53. //---------------------------------------------------------------------------
  54. void ColumnField::ReadTypedData(const uint8_t *data, size_t len)
  55. {
  56. unsigned char c;
  57. int pos=0;
  58. CHECK_CHAR(len);
  59. MyType = GET_CHAR(); pos++;
  60. CHECK_CHAR(len);
  61. pos++;
  62. CHECK_CHAR(len);
  63. c = GET_CHAR(); pos++;
  64. if (c)
  65. {
  66. CHECK_BIN(len, c);
  67. if (Name)
  68. CFRelease(Name);
  69. Name = CFStringCreateWithBytes(kCFAllocatorDefault, data+pos, c, kCFStringEncodingUTF8, false);
  70. }
  71. }
  72. //---------------------------------------------------------------------------
  73. void ColumnField::WriteTypedData(uint8_t *data, size_t len)
  74. {
  75. int pos = 0;
  76. CHECK_CHAR(len);
  77. PUT_CHAR(MyType); pos++;
  78. CHECK_CHAR(len);
  79. PUT_CHAR(0/*(char)indexUnique*/);
  80. pos++;
  81. if (Name)
  82. {
  83. CFIndex lengthRequired=0;
  84. CFStringGetBytes(Name, CFRangeMake(0, CFStringGetLength(Name)), kCFStringEncodingUTF8, 0, false, NULL, 0, &lengthRequired);
  85. CHECK_BIN(len, lengthRequired+1);
  86. PUT_CHAR(lengthRequired); pos++;
  87. CFStringGetBytes(Name, CFRangeMake(0, CFStringGetLength(Name)), kCFStringEncodingUTF8, 0, false, data+pos, lengthRequired, 0);
  88. }
  89. else
  90. {
  91. PUT_CHAR(0);
  92. }
  93. }
  94. //---------------------------------------------------------------------------
  95. unsigned char ColumnField::GetDataType(void)
  96. {
  97. return MyType;
  98. }
  99. //---------------------------------------------------------------------------
  100. void ColumnField::SetDataType(unsigned char type)
  101. {
  102. if ((MyType == FIELD_INTEGER || MyType == FIELD_BOOLEAN || MyType == FIELD_DATETIME || MyType == FIELD_LENGTH) &&
  103. (type == FIELD_INTEGER || type == FIELD_BOOLEAN || type == FIELD_DATETIME || type == FIELD_LENGTH)) {
  104. MyType = type;
  105. }
  106. // going from string to filename or filename to string is OK
  107. if ((MyType == FIELD_FILENAME && type == FIELD_STRING)
  108. || (MyType == FIELD_STRING && type == FIELD_FILENAME))
  109. {
  110. MyType = type;
  111. }
  112. }
  113. //---------------------------------------------------------------------------
  114. CFStringRef ColumnField::GetFieldName(void)
  115. {
  116. return Name;
  117. }
  118. //---------------------------------------------------------------------------
  119. size_t ColumnField::GetDataSize(void)
  120. {
  121. if (Name)
  122. {
  123. CFIndex lengthRequired=0;
  124. CFStringGetBytes(Name, CFRangeMake(0, CFStringGetLength(Name)), kCFStringEncodingUTF8, 0, false, NULL, 0, &lengthRequired);
  125. return lengthRequired+3;
  126. }
  127. else
  128. return 3;
  129. }
  130. //---------------------------------------------------------------------------
  131. int ColumnField::Compare(Field * /*Entry*/)
  132. {
  133. return 0;
  134. }