-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_values.c
484 lines (359 loc) · 13.3 KB
/
content_values.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include "content_values.h"
#include <json_object.h>
#include <linkhash.h>
#include <printbuf.h>
#include <tg_utility.h>
#include "db_util.h"
#include <stdio.h>
#define CONTENT_VALUSES_DEF_HASH_ENTRIES 16
extern char *tg_strdup( const char *src );
struct _ContentValues
{
struct lh_table* hash;
};
typedef struct
{
void *data;
UINT32 len;
}ContentValuesBlobEntry;
struct _ContentValuesEntry
{
ContentValuesEntry_TYPE type;
union data
{
BOOL c_bool;
DOUBLE c_double;
INT64 c_int;
CHAR *c_string;
ContentValuesBlobEntry c_blob;
WCHAR *c_wstring;
} o;
};
typedef struct _ContentValuesEntry ContentValuesEntry;
static void _content_values_lh_entry_free(struct lh_entry *ent)
{
TG_FREE(ent->k);
TG_FREE((void*)ent->v);
}
ContentValues* content_values_create()
{
ContentValues* thiz = NULL;
struct lh_table* hash = lh_kchar_table_new(CONTENT_VALUSES_DEF_HASH_ENTRIES, NULL, &_content_values_lh_entry_free);
return_val_if_fail(hash, NULL);
thiz = TG_CALLOC_V2(sizeof(ContentValues));
thiz->hash = hash;
return thiz;
}
void content_values_destroy(ContentValues* thiz)
{
return_if_fail(thiz);
lh_table_free(thiz->hash);
TG_FREE(thiz);
}
ContentValues* content_values_duplicate(ContentValues* values)
{
ContentValues* dup_values = NULL;
ContentValuesItor itor;
ContentValuesEntry_TYPE type;
UINT32 len;
INT32 value_num;
INT32 ret=0;
INT32 i;
ret=content_values_get_count(values,&value_num);
return_val_if_fail((value_num>0 && ret==ContentValues_SUCC), NULL);
dup_values = content_values_create();
return_val_if_fail(dup_values, NULL);
for (i=0,itor=content_values_first(values);i<value_num && itor;i++,itor=content_values_next(values,itor))
{
const void* value = content_values_get_value(itor,&type,&len);
const CHAR* key = content_values_get_key(itor);
if (type==ContentValuesEntry_TYPE_INT)
ret=content_values_put_int(dup_values,key,*((INT64*)value));
else if (type==ContentValuesEntry_TYPE_DOUBLE)
ret=content_values_put_double(dup_values,key,*((double*)value));
else if (type==ContentValuesEntry_TYPE_BOOL)
{
ret=ret=content_values_put_bool(dup_values,key,*((BOOL*)value));
}
else if (type==ContentValuesEntry_TYPE_STRING)
{
ret=content_values_put_string(dup_values,key,(const CHAR*)value);
}
else if (type==ContentValuesEntry_TYPE_STRING_16)
{
ret=content_values_put_string_16(dup_values,key,(const WCHAR*)value);
}
else if (type==ContentValuesEntry_TYPE_BLOB)
{
ret=content_values_put_blob(dup_values,key,(const void*)value,len);
}
}
return dup_values;
}
INT32 content_values_get_count(ContentValues* thiz,INT32* count)
{
return_val_if_fail((thiz && thiz->hash),ContentValues_ERROR);
*count = thiz->hash->count;
return ContentValues_SUCC;
}
BOOL content_values_contains_key(ContentValues* thiz,const CHAR* key)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((thiz && thiz->hash),FALSE);
entry = (ContentValuesEntry*)lh_table_lookup(thiz->hash,key);
return (entry?TRUE:FALSE);
}
INT32 content_values_get_blob(ContentValues* thiz,const CHAR* key,void** ret,UINT32* len)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
entry = (ContentValuesEntry*)lh_table_lookup(thiz->hash,key);
return_val_if_fail((entry!=NULL && entry->type ==ContentValuesEntry_TYPE_BLOB),ContentValues_KEY_NOT_EXIST);
if ( entry->o.c_blob.data && entry->o.c_blob.len > 0)
{
*ret=TG_MALLOC(entry->o.c_blob.len );
*len = entry->o.c_blob.len;
memcpy(*ret,entry->o.c_blob.data,entry->o.c_blob.len);
}
else
{
*ret = NULL;
*len =0;
}
return ContentValues_SUCC;
}
void* content_values_get_blob_v2(ContentValues* thiz,const CHAR* key,UINT32* len)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),NULL);
*len = 0;
entry = (ContentValuesEntry*)lh_table_lookup(thiz->hash,key);
return_val_if_fail((entry!=NULL && entry->type ==ContentValuesEntry_TYPE_BLOB),NULL);
*len = entry->o.c_blob.len;
return entry->o.c_blob.data;
}
INT32 content_values_get_string16(ContentValues* thiz,const CHAR* key,WCHAR** ret)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
entry = (ContentValuesEntry*)lh_table_lookup(thiz->hash,key);
return_val_if_fail((entry!=NULL && entry->type ==ContentValuesEntry_TYPE_STRING_16),ContentValues_KEY_NOT_EXIST);
*ret=wstrdup(entry->o.c_wstring);
return ContentValues_SUCC;
}
const WCHAR* content_values_get_string16_v2(ContentValues* thiz,const CHAR* key)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),NULL);
entry = (ContentValuesEntry*)lh_table_lookup(thiz->hash,key);
return_val_if_fail((entry!=NULL && entry->type ==ContentValuesEntry_TYPE_STRING_16),NULL);
return entry->o.c_wstring;
}
INT32 content_values_get_string(ContentValues* thiz,const CHAR* key,CHAR** ret)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
entry = (ContentValuesEntry*)lh_table_lookup(thiz->hash,key);
return_val_if_fail((entry!=NULL && entry->type ==ContentValuesEntry_TYPE_STRING),ContentValues_KEY_NOT_EXIST);
if ( entry->o.c_string)
*ret=tg_strdup(entry->o.c_string);
else
*ret = NULL;
return ContentValues_SUCC;
}
const CHAR* content_values_get_string_v2(ContentValues* thiz,const CHAR* key)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),NULL);
entry = (ContentValuesEntry*)lh_table_lookup(thiz->hash,key);
return_val_if_fail((entry!=NULL && entry->type ==ContentValuesEntry_TYPE_STRING),NULL);
return entry->o.c_string;
}
INT32 content_values_get_int(ContentValues* thiz,const CHAR* key,INT64* ret)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
entry = (ContentValuesEntry*)lh_table_lookup(thiz->hash,key);
return_val_if_fail((entry!=NULL && entry->type ==ContentValuesEntry_TYPE_INT),ContentValues_KEY_NOT_EXIST);
*ret = entry->o.c_int;
return ContentValues_SUCC;
}
INT32 content_values_get_double(ContentValues* thiz,const CHAR* key,double* ret)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
entry = (ContentValuesEntry*)lh_table_lookup(thiz->hash,key);
return_val_if_fail((entry!=NULL && entry->type ==ContentValuesEntry_TYPE_DOUBLE),ContentValues_KEY_NOT_EXIST);
*ret = entry->o.c_double;
return ContentValues_SUCC;
}
INT32 content_values_get_bool(ContentValues* thiz,const CHAR* key,BOOL* ret)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
entry = (ContentValuesEntry*)lh_table_lookup(thiz->hash,key);
return_val_if_fail((entry!=NULL && entry->type ==ContentValuesEntry_TYPE_BOOL),ContentValues_KEY_NOT_EXIST);
*ret = entry->o.c_bool;
return ContentValues_SUCC;
}
INT32 content_values_delete_key(ContentValues* thiz,const CHAR* key)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
entry = (ContentValuesEntry*)lh_table_lookup(thiz->hash,key);
return_val_if_fail((entry!=NULL),ContentValues_KEY_NOT_EXIST);
lh_table_delete(thiz->hash,key);
return ContentValues_SUCC;
}
INT32 content_values_put_blob(ContentValues* thiz,const CHAR* key,const void* value,UINT32 len)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
entry = (ContentValuesEntry*)TG_CALLOC_V2(sizeof(ContentValuesEntry) + len);
return_val_if_fail((entry),ContentValues_NO_MEMORY);
entry->type=ContentValuesEntry_TYPE_BLOB;
if (value)
{
entry->o.c_blob.data=(UINT8*)entry+ sizeof(ContentValuesEntry);
entry->o.c_blob.len = len;
memcpy(entry->o.c_blob.data,value,len);
}
lh_table_delete(thiz->hash,key);
lh_table_insert(thiz->hash, tg_strdup(key), entry);
return ContentValues_SUCC;
}
INT32 content_values_put_string_16(ContentValues* thiz,const CHAR* key,const WCHAR* value)
{
ContentValuesEntry* entry = NULL;
INT32 len = 0;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
if (value)
len = 2*wstrlen(value)+2;
entry = (ContentValuesEntry*)TG_CALLOC_V2(sizeof(ContentValuesEntry)+len);
return_val_if_fail((entry),ContentValues_NO_MEMORY);
entry->type=ContentValuesEntry_TYPE_STRING_16;
if (value)
{
entry->o.c_wstring=(WCHAR*)((CHAR*)entry+ sizeof(ContentValuesEntry));
wstrcpy(entry->o.c_wstring,value);
}
lh_table_delete(thiz->hash,key);
lh_table_insert(thiz->hash, tg_strdup(key), entry);
return ContentValues_SUCC;
}
INT32 content_values_put_string(ContentValues* thiz,const CHAR* key,const CHAR* value)
{
ContentValuesEntry* entry = NULL;
INT32 len = 0;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
if (value)
len = strlen(value)+1;
entry = (ContentValuesEntry*)TG_CALLOC_V2(sizeof(ContentValuesEntry)+len);
return_val_if_fail((entry),ContentValues_NO_MEMORY);
entry->type=ContentValuesEntry_TYPE_STRING;
if (value)
{
entry->o.c_string=(CHAR*)entry+ sizeof(ContentValuesEntry);
strcpy(entry->o.c_string,value);
}
lh_table_delete(thiz->hash,key);
lh_table_insert(thiz->hash, tg_strdup(key), entry);
return ContentValues_SUCC;
}
INT32 content_values_put_int(ContentValues* thiz,const CHAR* key,INT64 value)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
entry = (ContentValuesEntry*)TG_CALLOC_V2(sizeof(ContentValuesEntry));
return_val_if_fail((entry),ContentValues_NO_MEMORY);
entry->type=ContentValuesEntry_TYPE_INT;
entry->o.c_int=value;
lh_table_delete(thiz->hash,key);
lh_table_insert(thiz->hash, tg_strdup(key), entry);
return ContentValues_SUCC;
}
INT32 content_values_put_double(ContentValues* thiz,const CHAR* key,double value)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
entry = (ContentValuesEntry*)TG_CALLOC_V2(sizeof(ContentValuesEntry));
return_val_if_fail((entry),ContentValues_NO_MEMORY);
entry->type=ContentValuesEntry_TYPE_DOUBLE;
entry->o.c_double=value;
lh_table_delete(thiz->hash,key);
lh_table_insert(thiz->hash, tg_strdup(key), entry);
return ContentValues_SUCC;
}
INT32 content_values_put_bool(ContentValues* thiz,const CHAR* key,BOOL value)
{
ContentValuesEntry* entry = NULL;
return_val_if_fail((key&&thiz&&thiz->hash),ContentValues_ERROR);
entry = (ContentValuesEntry*)TG_CALLOC_V2(sizeof(ContentValuesEntry));
return_val_if_fail((entry),ContentValues_NO_MEMORY);
entry->type=ContentValuesEntry_TYPE_BOOL;
entry->o.c_bool=value;
lh_table_delete(thiz->hash,key);
lh_table_insert(thiz->hash, tg_strdup(key), entry);
return ContentValues_SUCC;
}
ContentValuesItor content_values_first(ContentValues* thiz)
{
return_val_if_fail((thiz&&thiz->hash),ContentValuesInvalidItor);
return (ContentValuesItor)thiz->hash->head;
}
ContentValuesItor content_values_next(ContentValues* thiz,ContentValuesItor* itor)
{
struct lh_entry * entry =( struct lh_entry *)itor;
return_val_if_fail((thiz&&thiz->hash),ContentValuesInvalidItor);
return (ContentValuesItor)entry->next;
}
const CHAR* content_values_get_key(ContentValuesItor* itor)
{
struct lh_entry * entry =( struct lh_entry *)itor;
return_val_if_fail((itor),NULL);
return (const CHAR*)entry->k;
}
const void* content_values_get_value(ContentValuesItor* itor,ContentValuesEntry_TYPE* type,UINT32* len)
{
void* data = NULL;
ContentValuesEntry* content_entry = NULL;
struct lh_entry * entry =( struct lh_entry *)itor;
*len = 0;
return_val_if_fail((itor),NULL);
content_entry = (ContentValuesEntry*)entry->v;
*type = content_entry->type;
if (content_entry->type == ContentValuesEntry_TYPE_INT)
{
data =(void*)&content_entry->o.c_int;
*len =sizeof(INT32);
}
else if (content_entry->type == ContentValuesEntry_TYPE_DOUBLE)
{
data =(void*)&content_entry->o.c_double;
*len =sizeof(double);
}
else if (content_entry->type == ContentValuesEntry_TYPE_BOOL)
{
data =(void*)&content_entry->o.c_bool;
*len =sizeof(BOOL);
}
else if (content_entry->type == ContentValuesEntry_TYPE_STRING)
{
data =(void*)content_entry->o.c_string;
if (data)
*len =strlen(content_entry->o.c_string)+1;
}
else if (content_entry->type == ContentValuesEntry_TYPE_STRING_16)
{
data =(void*)content_entry->o.c_wstring;
if (data)
*len =2*wstrlen(content_entry->o.c_wstring)+2;
}
else if (content_entry->type == ContentValuesEntry_TYPE_BLOB)
{
data =(void*)content_entry->o.c_blob.data;
if (data)
*len =content_entry->o.c_blob.len;
}
return data;
}