1
0

ColumnField.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. ColumnField Class
  8. Android (linux) 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 "ColumnField.h"
  16. #include "../NDEString.h"
  17. #include "../nde.h"
  18. //---------------------------------------------------------------------------
  19. ColumnField::ColumnField(unsigned char FieldID, const char *FieldName, unsigned char FieldType, Table *parentTable)
  20. {
  21. InitField();
  22. Type = FIELD_COLUMN;
  23. MyType = FieldType;
  24. Name = ndestring_wcsdup(FieldName);
  25. ID = FieldID;
  26. }
  27. //---------------------------------------------------------------------------
  28. void ColumnField::InitField(void)
  29. {
  30. searchable = false;
  31. MyType = FIELD_UNKNOWN;
  32. Type = FIELD_COLUMN;
  33. Name = NULL;
  34. ID = 0;
  35. }
  36. //---------------------------------------------------------------------------
  37. ColumnField::ColumnField()
  38. {
  39. InitField();
  40. }
  41. //---------------------------------------------------------------------------
  42. ColumnField::~ColumnField()
  43. {
  44. if (Name) free(Name);
  45. }
  46. void ColumnField::SetSearchable(bool val)
  47. {
  48. searchable=val;
  49. }
  50. bool ColumnField::IsSearchableField() const
  51. {
  52. return searchable;
  53. }
  54. //---------------------------------------------------------------------------
  55. void ColumnField::ReadTypedData(const uint8_t *data, size_t len)
  56. {
  57. unsigned char c;
  58. int pos=0;
  59. // [1 byte] Field Type
  60. CHECK_CHAR(len);
  61. MyType = GET_CHAR();
  62. pos++;
  63. // [1 byte] unused
  64. CHECK_CHAR(len);
  65. //cut: indexUnique = (BOOL)GET_CHAR();
  66. pos++;
  67. // [1 byte] string length
  68. CHECK_CHAR(len);
  69. c = GET_CHAR();
  70. pos++;
  71. if (c)
  72. {
  73. CHECK_BIN(len, c);
  74. Name = ndestring_wcsndup((const char *)(data+pos), c);
  75. }
  76. }
  77. //---------------------------------------------------------------------------
  78. void ColumnField::WriteTypedData(uint8_t *data, size_t len)
  79. {
  80. int pos = 0;
  81. // [1 byte] Field Type
  82. CHECK_CHAR(len);
  83. PUT_CHAR(MyType);
  84. pos++;
  85. // [1 byte] unused
  86. CHECK_CHAR(len);
  87. PUT_CHAR(0/*(char)indexUnique*/);
  88. pos++;
  89. CHECK_CHAR(len);
  90. if (Name)
  91. {
  92. int string_length = strlen(Name);
  93. if (string_length)
  94. {
  95. PUT_CHAR(string_length);
  96. pos++;
  97. CHECK_BIN(len, string_length);
  98. PUT_BINARY(data, (const uint8_t *)Name, string_length, pos);
  99. }
  100. else
  101. {
  102. PUT_CHAR(0);
  103. }
  104. }
  105. else
  106. {
  107. PUT_CHAR(0);
  108. }
  109. }
  110. //---------------------------------------------------------------------------
  111. unsigned char ColumnField::GetDataType(void)
  112. {
  113. return MyType;
  114. }
  115. //---------------------------------------------------------------------------
  116. void ColumnField::SetDataType(unsigned char type)
  117. {
  118. if ((MyType == FIELD_INTEGER || MyType == FIELD_BOOLEAN || MyType == FIELD_DATETIME || MyType == FIELD_LENGTH) &&
  119. (type == FIELD_INTEGER || type == FIELD_BOOLEAN || type == FIELD_DATETIME || type == FIELD_LENGTH)) {
  120. MyType = type;
  121. }
  122. // going from string to filename or filename to string is OK
  123. if ((MyType == FIELD_FILENAME && type == FIELD_STRING)
  124. || (MyType == FIELD_STRING && type == FIELD_FILENAME))
  125. {
  126. MyType = type;
  127. }
  128. }
  129. //---------------------------------------------------------------------------
  130. char *ColumnField::GetFieldName(void)
  131. {
  132. return Name;
  133. }
  134. //---------------------------------------------------------------------------
  135. size_t ColumnField::GetDataSize(void)
  136. {
  137. size_t s=3;
  138. if (Name)
  139. {
  140. s+=strlen(Name);
  141. }
  142. return s;
  143. }
  144. //---------------------------------------------------------------------------
  145. int ColumnField::Compare(Field * /*Entry*/)
  146. {
  147. return 0;
  148. }