StringField.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. StringField Class
  8. Android (linux) specific version
  9. Field data layout:
  10. [2 bytes] string length (bytes)
  11. [length bytes] String data. UTF-16 data will start with a BOM
  12. --------------------------------------------------------------------------- */
  13. #include "../nde.h"
  14. #include "StringField.h"
  15. //---------------------------------------------------------------------------
  16. StringField::StringField(const char *Str, int strkind)
  17. {
  18. InitField();
  19. Type = FIELD_STRING;
  20. if (Str)
  21. {
  22. if (strkind == STRING_IS_WCHAR)
  23. String = ndestring_wcsdup(Str);
  24. else
  25. {
  26. String = const_cast<char *>(Str);
  27. ndestring_retain(String);
  28. }
  29. }
  30. }
  31. //---------------------------------------------------------------------------
  32. void StringField::InitField(void)
  33. {
  34. Type = FIELD_STRING;
  35. // String = NULL;
  36. String = NULL;
  37. }
  38. //---------------------------------------------------------------------------
  39. StringField::StringField()
  40. {
  41. InitField();
  42. }
  43. //---------------------------------------------------------------------------
  44. StringField::~StringField()
  45. {
  46. ndestring_release(String);
  47. String=0;
  48. }
  49. //---------------------------------------------------------------------------
  50. void StringField::ReadTypedData(const uint8_t *data, size_t len)
  51. {
  52. size_t pos=0;
  53. unsigned short c;
  54. CHECK_SHORT(len);
  55. c = GET_SHORT();
  56. pos+=2;
  57. if (c)
  58. {
  59. bool unicode=false;
  60. bool utf16BE=false;
  61. if (c >= 2 // enough room for BOM
  62. && (c & 1) == 0) // can't be unicode if it's not an even multiple of 2
  63. {
  64. uint16_t BOM=GET_SHORT();
  65. if (BOM == 0xFEFF)
  66. {
  67. pos+=2;
  68. c-=2;
  69. unicode=true;
  70. }
  71. else if (BOM == 0xFFFE)
  72. {
  73. pos+=2;
  74. c-=2;
  75. unicode=true;
  76. utf16BE=true;
  77. }
  78. }
  79. CHECK_BIN(len, c);
  80. if (unicode)
  81. {
  82. ndestring_release(String);
  83. if (utf16BE)
  84. {
  85. size_t bytes = utf16BE_to_utf8((uint16_t *)(data+pos), c>>1, 0, 0);
  86. String = ndestring_malloc(bytes+1);
  87. utf16BE_to_utf8((uint16_t *)(data+pos), c>>1, String, bytes);
  88. String[bytes]=0;
  89. }
  90. else
  91. {
  92. size_t bytes = utf16LE_to_utf8((uint16_t *)(data+pos), c>>1, 0, 0);
  93. String = ndestring_malloc(bytes+1);
  94. utf16LE_to_utf8((uint16_t *)(data+pos), c>>1, String, bytes);
  95. String[bytes]=0;
  96. }
  97. }
  98. else
  99. {
  100. // TODO: check for utf-8 byte marker
  101. String = ndestring_malloc(c+1);
  102. GET_BINARY((uint8_t *)String, data, c, pos);
  103. String[c]=0;
  104. }
  105. }
  106. }
  107. //---------------------------------------------------------------------------
  108. void StringField::WriteTypedData(uint8_t *data, size_t len)
  109. {
  110. int pos=0;
  111. if (String)
  112. {
  113. unsigned short c = (unsigned short)strlen(String);
  114. // write size
  115. CHECK_SHORT(len);
  116. PUT_SHORT(c); pos+=2;
  117. // write string
  118. CHECK_BIN(len, c);
  119. PUT_BINARY(data, (uint8_t *)String, c, pos);
  120. }
  121. else
  122. {
  123. CHECK_SHORT(len);
  124. PUT_SHORT(0); pos+=2;
  125. }
  126. }
  127. //---------------------------------------------------------------------------
  128. char *StringField::GetString(void)
  129. {
  130. return String;
  131. }
  132. //---------------------------------------------------------------------------
  133. void StringField::SetString(const char *Str)
  134. {
  135. if (!Str) return;
  136. ndestring_release(String);
  137. String = NULL;
  138. String = ndestring_wcsdup(Str);
  139. }
  140. //---------------------------------------------------------------------------
  141. void StringField::SetNDEString(char *Str)
  142. {
  143. if (!Str) return;
  144. // copy and then release, just in case we're copying into ourselves
  145. char *oldStr = String;
  146. String = Str;
  147. ndestring_retain(String);
  148. ndestring_release(oldStr);
  149. }
  150. //---------------------------------------------------------------------------
  151. size_t StringField::GetDataSize(void)
  152. {
  153. if (String)
  154. {
  155. return strlen(String) + 2;
  156. }
  157. else
  158. {
  159. return 2;
  160. }
  161. }
  162. //---------------------------------------------------------------------------
  163. int StringField::Compare(Field *Entry)
  164. {
  165. if (!Entry) return -1;
  166. if (Entry->GetType() != GetType()) return 0;
  167. return mystricmp(GetString(), ((StringField*)Entry)->GetString());
  168. }
  169. //---------------------------------------------------------------------------
  170. int StringField::Starts(Field *Entry)
  171. {
  172. if (!Entry) return -1;
  173. if (Entry->GetType() != GetType()) return 0;
  174. return (mystristr(GetString(), ((StringField*)Entry)->GetString()) == GetString());
  175. }
  176. //---------------------------------------------------------------------------
  177. int StringField::Contains(Field *Entry)
  178. {
  179. if (!Entry) return -1;
  180. if (Entry->GetType() != GetType()) return 0;
  181. return (mystristr(GetString(), ((StringField*)Entry)->GetString()) != NULL);
  182. }
  183. Field *StringField::Clone(Table *pTable)
  184. {
  185. StringField *clone = new StringField(String, STRING_IS_NDESTRING);
  186. clone->Pos = FIELD_CLONE;
  187. clone->ID = ID;
  188. clone->MaxSizeOnDisk = GetDataSize();
  189. return clone;
  190. }
  191. bool StringField::ApplyFilter(Field *Data, int op)
  192. {
  193. // TODO: maybe do this?
  194. if (op == FILTER_ISEMPTY || op == FILTER_ISNOTEMPTY)
  195. {
  196. bool r = (op == FILTER_ISEMPTY);
  197. if (!String)
  198. return r;
  199. if (String && String[0] == 0)
  200. return r;
  201. return !r;
  202. }
  203. //
  204. bool r;
  205. StringField *compField = (StringField *)Data;
  206. const char *p = compField->GetString();
  207. const char *d = GetString();
  208. if (!p)
  209. p = "";
  210. if (!d)
  211. d = "";
  212. switch (op)
  213. {
  214. case FILTER_EQUALS:
  215. r = !nde_stricmp(d, p);
  216. break;
  217. case FILTER_NOTEQUALS:
  218. r = !!nde_stricmp(d, p);
  219. break;
  220. case FILTER_CONTAINS:
  221. r = (NULL != stristr_ignore(d, p));
  222. break;
  223. case FILTER_NOTCONTAINS:
  224. r = (NULL == stristr_ignore(d, p));
  225. break;
  226. case FILTER_ABOVE:
  227. r = (bool)(nde_stricmp(d, p) > 0);
  228. break;
  229. case FILTER_ABOVEOREQUAL:
  230. r = (bool)(nde_stricmp(d, p) >= 0);
  231. break;
  232. case FILTER_BELOW:
  233. r = (bool)(nde_stricmp(d, p) < 0);
  234. break;
  235. case FILTER_BELOWOREQUAL:
  236. r = (bool)(nde_stricmp(d, p) <= 0);
  237. break;
  238. case FILTER_BEGINS:
  239. r = (bool)(nde_strnicmp(d, p, strlen(p)) == 0);
  240. break;
  241. case FILTER_ENDS:
  242. {
  243. size_t lenp = strlen(p), lend = strlen(d);
  244. if (lend < lenp) return 0; // too short
  245. r = (bool)(nde_stricmp((d + lend) - lenp, p) == 0);
  246. }
  247. break;
  248. case FILTER_LIKE:
  249. r = (bool)(nde_stricmp(d, p) == 0);
  250. break;
  251. case FILTER_BEGINSLIKE:
  252. r = (bool)(nde_strnicmp_ignore(d, p, strlen(p)) == 0);
  253. break;
  254. default:
  255. r = true;
  256. break;
  257. }
  258. return r;
  259. }