ColumnField.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. ColumnField Class
  8. Windows 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, const wchar_t *FieldName, unsigned char FieldType, Table *parentTable)
  18. {
  19. InitField();
  20. Type = FIELD_COLUMN;
  21. MyType = FieldType;
  22. Name = ndestring_wcsdup(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. ndestring_release(Name);
  43. }
  44. void ColumnField::SetSearchable(bool val)
  45. {
  46. searchable=val;
  47. }
  48. bool ColumnField::IsSearchableField() const
  49. {
  50. return searchable;
  51. }
  52. //---------------------------------------------------------------------------
  53. void ColumnField::ReadTypedData(const uint8_t *data, size_t len)
  54. {
  55. unsigned char c;
  56. int pos=0;
  57. // [1 byte] Field Type
  58. CHECK_CHAR(len);
  59. MyType = GET_CHAR();
  60. pos++;
  61. // [1 byte] unused
  62. CHECK_CHAR(len);
  63. //cut: indexUnique = (BOOL)GET_CHAR();
  64. pos++;
  65. // [1 byte] string length
  66. CHECK_CHAR(len);
  67. c = GET_CHAR();
  68. pos++;
  69. if (c)
  70. {
  71. CHECK_BIN(len, c);
  72. int string_length = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(data+pos), c, 0, 0);
  73. Name = ndestring_malloc((string_length+1)*sizeof(wchar_t));
  74. MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)(data+pos), c, Name, string_length);
  75. Name[string_length]=0;
  76. }
  77. }
  78. //---------------------------------------------------------------------------
  79. void ColumnField::WriteTypedData(uint8_t *data, size_t len)
  80. {
  81. int pos = 0;
  82. // [1 byte] Field Type
  83. CHECK_CHAR(len);
  84. PUT_CHAR(MyType);
  85. pos++;
  86. // [1 byte] unused
  87. CHECK_CHAR(len);
  88. PUT_CHAR(0/*(char)indexUnique*/);
  89. pos++;
  90. CHECK_CHAR(len);
  91. if (Name)
  92. {
  93. size_t string_length = WideCharToMultiByte(CP_UTF8, 0, Name, -1, 0, 0, 0, 0);
  94. if (string_length)
  95. {
  96. PUT_CHAR((uint8_t)string_length-1);
  97. pos++;
  98. CHECK_BIN(len, string_length-1);
  99. WideCharToMultiByte(CP_UTF8, 0, Name, -1, (LPSTR)(data+pos), (int)string_length-1, 0, 0);
  100. }
  101. else
  102. {
  103. PUT_CHAR(0);
  104. }
  105. }
  106. else
  107. {
  108. PUT_CHAR(0);
  109. }
  110. }
  111. //---------------------------------------------------------------------------
  112. unsigned char ColumnField::GetDataType(void)
  113. {
  114. return MyType;
  115. }
  116. //---------------------------------------------------------------------------
  117. void ColumnField::SetDataType(unsigned char type)
  118. {
  119. if ((MyType == FIELD_INTEGER || MyType == FIELD_BOOLEAN || MyType == FIELD_DATETIME || MyType == FIELD_LENGTH || MyType == FIELD_INT64) &&
  120. (type == FIELD_INTEGER || type == FIELD_BOOLEAN || type == FIELD_DATETIME || type == FIELD_LENGTH || type == FIELD_INT64)) {
  121. MyType = type;
  122. }
  123. // going from string to filename or filename to string is OK
  124. if ((MyType == FIELD_FILENAME && type == FIELD_STRING)
  125. || (MyType == FIELD_STRING && type == FIELD_FILENAME))
  126. {
  127. MyType = type;
  128. }
  129. }
  130. //---------------------------------------------------------------------------
  131. wchar_t *ColumnField::GetFieldName(void)
  132. {
  133. return Name;
  134. }
  135. //---------------------------------------------------------------------------
  136. void ColumnField::SetFieldName(wchar_t *name)
  137. {
  138. if (Name) ndestring_release(Name);
  139. Name = ndestring_wcsdup(name);
  140. }
  141. //---------------------------------------------------------------------------
  142. size_t ColumnField::GetDataSize(void)
  143. {
  144. size_t s=3;
  145. if (Name)
  146. {
  147. int string_length=WideCharToMultiByte(CP_UTF8, 0, Name, -1, 0, 0, 0, 0);
  148. if (string_length)
  149. s+=string_length-1;
  150. }
  151. return s;
  152. }
  153. //---------------------------------------------------------------------------
  154. int ColumnField::Compare(Field * /*Entry*/)
  155. {
  156. return 0;
  157. }