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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/* ---------------------------------------------------------------------------
Nullsoft Database Engine
--------------------
codename: Near Death Experience
--------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------
StringField Class
Windows 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"
#include "../../nu/AutoChar.h"
#include "../../nu/AutoWide.h"
static wchar_t CharSwap(wchar_t value)
{
return (value >> 8) | (value << 8);
}
//---------------------------------------------------------------------------
StringField::StringField(const wchar_t *Str, int strkind)
{
InitField();
Type = FIELD_STRING;
if (Str)
{
if (strkind == STRING_IS_WCHAR)
StringW = ndestring_wcsdup(Str);
else
{
StringW = const_cast<wchar_t *>(Str);
ndestring_retain(StringW);
}
}
}
//---------------------------------------------------------------------------
void StringField::InitField(void)
{
Type = FIELD_STRING;
StringW = NULL;
optimized_the = 0;
}
//---------------------------------------------------------------------------
StringField::StringField()
{
InitField();
}
//---------------------------------------------------------------------------
StringField::~StringField()
{
ndestring_release(StringW);
StringW=0;
}
//---------------------------------------------------------------------------
void StringField::ReadTypedData(const uint8_t *data, size_t len)
{
unsigned short c;
CHECK_SHORT(len);
c = (unsigned short)(data[0]|(data[1]<<8));
data+=2;
if (c)
{
bool unicode=false;
bool reverseEndian=false;
if (c >= 2 // enough room for BOM
&& (c % 2) == 0) // can't be unicode if it's not an even multiple of 2
{
wchar_t BOM=0;
memcpy(&BOM, data, 2);
if (BOM == 0xFEFF)
{
data+=2;
c-=2;
unicode=true;
}
else if (BOM == 0xFFFE)
{
data+=2;
c-=2;
unicode=true;
reverseEndian=true;
}
}
CHECK_BIN(len, c);
if (unicode)
{
ndestring_release(StringW);
StringW = ndestring_malloc(c+sizeof(wchar_t));
memcpy(StringW, data, c);
StringW[c/2]=0;
if (reverseEndian)
{
for (unsigned short i=0;i<c;i++)
StringW[i]=CharSwap(StringW[i]);
}
}
else
{
int len = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)data, c, 0, 0);
StringW = ndestring_malloc((len+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)data, c, StringW, len);
StringW[len]=0;
}
}
}
//---------------------------------------------------------------------------
void StringField::WriteTypedData(uint8_t *data, size_t len)
{
int pos=0;
if (StringW)
{
unsigned short c = (unsigned short)wcslen(StringW) * sizeof(wchar_t) + 2 /* for BOM */;
// write size
CHECK_SHORT(len);
PUT_SHORT(c); pos+=2;
// write byte order mark
CHECK_BIN(len, 2);
wchar_t BOM = 0xFEFF;
PUT_BINARY(data, (uint8_t *)&BOM, 2, pos);
pos+=2;
c-=2;
// write string
CHECK_BIN(len, c);
PUT_BINARY(data, (uint8_t *)StringW, c, pos);
}
else
{
CHECK_SHORT(len);
PUT_SHORT(0); /*pos+=2;*/
}
}
//---------------------------------------------------------------------------
wchar_t *StringField::GetStringW(void)
{
return StringW;
}
//---------------------------------------------------------------------------
void StringField::SetStringW(const wchar_t *Str)
{
if (!Str) return;
ndestring_release(StringW);
StringW = NULL;
StringW = ndestring_wcsdup(Str);
optimized_the=0;
}
//---------------------------------------------------------------------------
void StringField::SetNDEString(wchar_t *Str)
{
if (!Str) return;
// copy and then release, just in case we're copying into ourselves
wchar_t *oldStr = StringW;
StringW = Str;
ndestring_retain(StringW);
ndestring_release(oldStr);
optimized_the=0;
}
//---------------------------------------------------------------------------
size_t StringField::GetDataSize(void)
{
if (StringW)
{
return wcslen(StringW)*2 +2 /*for BOM*/ + 2 /*for byte length*/;
}
else
{
return 2;
}
}
//---------------------------------------------------------------------------
int StringField::Compare(Field *Entry)
{
if (!Entry) return -1;
if (Entry->GetType() != GetType()) return 0;
return mywcsicmp(GetStringW(), ((StringField*)Entry)->GetStringW());
}
//---------------------------------------------------------------------------
int StringField::Starts(Field *Entry)
{
if (!Entry) return -1;
if (Entry->GetType() != GetType()) return 0;
const wchar_t *p = ((StringField*)Entry)->GetStringW();
const wchar_t *d = GetStringW();
if (!d || !p) return 0;
return nde_wcsbegins(d, p);
}
//---------------------------------------------------------------------------
int StringField::Contains(Field *Entry)
{
if (!Entry) return -1;
if (Entry->GetType() != GetType()) return 0;
const wchar_t *p = ((StringField*)Entry)->GetStringW();
const wchar_t *d = GetStringW();
if (!d || !p) return 0;
return nde_wcscontains(GetStringW(), ((StringField*)Entry)->GetStringW());
}
Field *StringField::Clone(Table *pTable)
{
StringField *clone = new StringField(StringW, STRING_IS_NDESTRING);
clone->Pos = FIELD_CLONE;
clone->ID = ID;
clone->MaxSizeOnDisk = (uint32_t)GetDataSize();
return clone;
}
// todo: make configurable words to skip, as well as trailing whitespace removal
#define IsCharSpaceW(c) (c == L' ' || c == L'\t')
inline bool IsTheW(const wchar_t *str) { if (str && (str[0] == L't' || str[0] == L'T') && (str[1] == L'h' || str[1] == L'H') && (str[2] == L'e' || str[2] == L'E') && (str[3] == L' ')) return true; else return false; }
#define SKIP_THE_AND_WHITESPACEW(x) { wchar_t *save##x=(wchar_t*)x; while (IsCharSpaceW(*x) && *x) x++; if (IsTheW(x)) x+=4; while (IsCharSpaceW(*x)) x++; if (!*x) x=save##x; }
bool StringField::ApplyFilter(Field *Data, int op)
{
// TODO: maybe do this?
if (op == FILTER_ISEMPTY || op == FILTER_ISNOTEMPTY)
{
bool r = (op == FILTER_ISEMPTY);
if (!StringW)
return r;
if (StringW && StringW[0] == 0)
return r;
return !r;
}
//
bool r;
StringField *compField = (StringField *)Data;
const wchar_t *p = compField->GetStringW();
const wchar_t *d = GetStringW();
if (!p)
p = L"";
if (!d)
d = L"";
switch (op)
{
case FILTER_EQUALS:
r = !nde_wcsicmp(d, p);
break;
case FILTER_NOTEQUALS:
r = !!nde_wcsicmp(d, p);
break;
case FILTER_CONTAINS:
r = nde_wcscontains(d, p);
break;
case FILTER_NOTCONTAINS:
r = !nde_wcscontains(d, p);
break;
case FILTER_ABOVE:
r = (bool)(nde_wcsicmp(d, p) > 0);
break;
case FILTER_ABOVEOREQUAL:
r = (bool)(nde_wcsicmp(d, p) >= 0);
break;
case FILTER_BELOW:
r = (bool)(nde_wcsicmp(d, p) < 0);
break;
case FILTER_BELOWOREQUAL:
r = (bool)(nde_wcsicmp(d, p) <= 0);
break;
case FILTER_BEGINS:
r = nde_wcsbegins(d, p);
break;
case FILTER_ENDS:
r = nde_wcsends(d, p);
break;
case FILTER_LIKE:
if (compField->optimized_the)
p = compField->optimized_the;
else
{
SKIP_THE_AND_WHITESPACEW(p);
compField->optimized_the = p;
}
if (optimized_the)
d = optimized_the;
else
{
SKIP_THE_AND_WHITESPACEW(d);
optimized_the=d;
}
r = (bool)(nde_wcsicmp(d, p) == 0);
break;
case FILTER_BEGINSLIKE:
if (compField->optimized_the)
p = compField->optimized_the;
else
{
SKIP_THE_AND_WHITESPACEW(p);
compField->optimized_the = p;
}
if (optimized_the)
d = optimized_the;
else
{
SKIP_THE_AND_WHITESPACEW(d);
optimized_the=d;
}
r = nde_wcsbegins(d, p);
break;
default:
r = true;
break;
}
return r;
}
|