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