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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* presets.c
* Copyright (C) John Stebbins 2008-2015 <stebbins@stebbins>
*
* presets.c is free software.
*
* You may redistribute it and/or modify it under the terms of the
* GNU General Public License, as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
*/
#include <glib.h>
#include <glib-object.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "values.h"
GhbType
ghb_value_type(const GhbValue *val)
{
if (val == NULL)
return GHB_NULL;
GhbType type = json_typeof(val);
if (type == JSON_TRUE || type == JSON_FALSE)
return GHB_BOOL;
return type;
}
GhbValue*
ghb_value_new(GhbType type)
{
GhbValue *val = NULL;
switch (type)
{
case GHB_DICT:
val = json_object();
break;
case GHB_ARRAY:
val = json_array();
break;
case GHB_STRING:
val = json_string("");
break;
case GHB_INT:
val = json_integer(0);
break;
case GHB_DOUBLE:
val = json_real(0.0);
break;
case GHB_BOOL:
val = json_false();
break;
default:
g_warning("Unrecognized GHB value type %d", type);
break;
}
return val;
}
void
ghb_value_free(GhbValue *gval)
{
if (gval == NULL) return;
json_decref(gval);
}
GhbValue*
ghb_value_dup(const GhbValue *gval)
{
if (gval == NULL) return NULL;
return json_deep_copy(gval);
}
void
debug_show_type(GhbType tp)
{
const gchar *str = "unknown";
if (tp == GHB_STRING)
{
str ="string";
}
else if (tp == GHB_INT)
{
str ="int";
}
else if (tp == GHB_DOUBLE)
{
str ="double";
}
else if (tp == GHB_BOOL)
{
str ="bool";
}
else if (tp == GHB_ARRAY)
{
str ="array";
}
else if (tp == GHB_DICT)
{
str ="dict";
}
g_debug("Type %s", str);
}
void
debug_show_value(GhbValue *gval)
{
GhbType tp;
tp = ghb_value_type(gval);
if (tp == GHB_STRING)
{
g_message("Type %s value %s", "string", json_string_value(gval));
}
else if (tp == GHB_INT)
{
g_message("Type %s value %"JSON_INTEGER_FORMAT, "int",
json_integer_value(gval));
}
else if (tp == GHB_DOUBLE)
{
g_message("Type %s value %f", "double", json_real_value(gval));
}
else if (tp == GHB_BOOL)
{
g_message("Type %s value %d", "boolean", json_is_true(gval));
}
else if (tp == GHB_ARRAY)
{
g_message("Type %s", "array");
}
else if (tp == GHB_DICT)
{
g_message("Type %s", "dict");
}
}
static GhbValue* xform_int(json_int_t i, GhbType type)
{
switch (type)
{
default:
case GHB_NULL:
return json_null();
case GHB_BOOL:
return json_boolean(i);
case GHB_INT:
return json_integer(i);
case GHB_DOUBLE:
return json_real(i);
case GHB_STRING:
{
char *s = g_strdup_printf("%"JSON_INTEGER_FORMAT, i);
GhbValue *v = json_string(s);
g_free(s);
return v;
}
}
}
static GhbValue* xform_double(double d, GhbType type)
{
switch (type)
{
default:
case GHB_NULL:
return json_null();
case GHB_BOOL:
return json_boolean((int)d != 0);
case GHB_INT:
return json_integer(d);
case GHB_DOUBLE:
return json_real(d);
case GHB_STRING:
{
char *s = g_strdup_printf("%g", d);
GhbValue *v = json_string(s);
g_free(s);
return v;
}
}
}
static GhbValue* xform_string(const char * s, GhbType type)
{
switch (type)
{
default:
case GHB_NULL:
{
return json_null();
}
case GHB_BOOL:
{
if (!strcasecmp(s, "true") ||
!strcasecmp(s, "yes") ||
!strcasecmp(s, "1"))
{
return json_true();
}
return json_false();
}
case GHB_INT:
{
double d = g_strtod(s, NULL);
return json_integer(d);
}
case GHB_DOUBLE:
{
double d = g_strtod(s, NULL);
return json_real(d);
}
case GHB_STRING:
{
return json_string(s);
}
}
}
static GhbValue* xform_null(GhbType type)
{
switch (type)
{
default:
case GHB_NULL:
return json_null();
case GHB_BOOL:
return json_false();
case GHB_INT:
return json_integer(0);
case GHB_DOUBLE:
return json_real(0.0);
case GHB_STRING:
return json_string("");
}
}
GhbValue* ghb_value_xform(const GhbValue *val, GhbType type)
{
GhbType src_type = ghb_value_type(val);
if (src_type == type && val != NULL)
{
json_incref((GhbValue*)val);
return (GhbValue*)val;
}
switch (src_type)
{
default:
case GHB_NULL:
{
return xform_null(type);
}
case GHB_BOOL:
{
json_int_t b = json_is_true(val);
return xform_int(b, type);
}
case GHB_INT:
{
json_int_t i = json_integer_value(val);
return xform_int(i, type);
}
case GHB_DOUBLE:
{
double d = json_real_value(val);
return xform_double(d, type);
}
case GHB_STRING:
{
const char *s = json_string_value(val);
return xform_string(s, type);
}
}
}
gint
ghb_value_int(const GhbValue *val)
{
gint result;
GhbValue *v = ghb_value_xform(val, GHB_INT);
result = json_integer_value(v);
json_decref(v);
return result;
}
gint64
ghb_value_int64(const GhbValue *val)
{
gint64 result;
GhbValue *v = ghb_value_xform(val, GHB_INT);
result = json_integer_value(v);
json_decref(v);
return result;
}
gdouble
ghb_value_double(const GhbValue *val)
{
gdouble result;
GhbValue *v = ghb_value_xform(val, GHB_DOUBLE);
result = json_real_value(v);
json_decref(v);
return result;
}
gchar*
ghb_value_string(const GhbValue *val)
{
gchar *result;
GhbValue *v = ghb_value_xform(val, GHB_STRING);
result = g_strdup(json_string_value(v));
json_decref(v);
return result;
}
const gchar*
ghb_value_const_string(const GhbValue *val)
{
if (ghb_value_type(val) != GHB_STRING)
return NULL;
return json_string_value(val);
}
gboolean
ghb_value_boolean(const GhbValue *val)
{
gboolean result;
GhbValue *v = ghb_value_xform(val, GHB_BOOL);
result = json_is_true(v);
json_decref(v);
return result;
}
gint
ghb_value_cmp(const GhbValue *vala, const GhbValue *valb)
{
return !json_equal((GhbValue*)vala, (GhbValue*)valb);
}
GhbValue*
ghb_string_value(const gchar *str)
{
static GhbValue *gval = NULL;
if (gval == NULL)
gval = json_string(str);
else
json_string_set(gval, str);
return gval;
}
GhbValue*
ghb_int64_value(gint64 ival)
{
static GhbValue *gval = NULL;
if (gval == NULL)
gval = json_integer(ival);
else
json_integer_set(gval, ival);
return gval;
}
GhbValue*
ghb_int_value(gint ival)
{
return ghb_int64_value(ival);
}
GhbValue*
ghb_double_value(gdouble dval)
{
static GhbValue *gval = NULL;
if (gval == NULL)
gval = json_real(dval);
else
json_real_set(gval, dval);
return gval;
}
GhbValue*
ghb_boolean_value(gboolean bval)
{
// Jansson boolean is singleton, no need for local static
GhbValue *gval = json_boolean(bval);
json_decref(gval);
return gval;
}
GhbValue*
ghb_string_value_new(const gchar *str)
{
return json_string(str);
}
void
ghb_string_value_set(GhbValue *gval, const gchar *str)
{
json_string_set(gval, str);
}
GhbValue*
ghb_int64_value_new(gint64 ival)
{
return json_integer(ival);
}
GhbValue*
ghb_int_value_new(gint ival)
{
return json_integer(ival);
}
GhbValue*
ghb_double_value_new(gdouble dval)
{
return json_real(dval);
}
GhbValue*
ghb_boolean_value_new(gboolean bval)
{
return json_boolean(bval);
}
GhbValue*
ghb_dict_value_new()
{
return json_object();
}
GhbValue*
ghb_array_value_new()
{
return json_array();
}
void
ghb_array_value_reset(GhbValue *array)
{
json_array_clear(array);
}
void
ghb_dict_insert(GhbValue *dict, const gchar *key, GhbValue *val)
{
json_object_set_new(dict, key, val);
}
void
ghb_dict_iter_init(GhbValue *dict, GhbDictIter *iter)
{
*iter = json_object_iter(dict);
}
int
ghb_dict_iter_next(GhbValue *dict, GhbDictIter *iter,
const char **key, GhbValue **val)
{
if (*iter == NULL)
return 0;
if (key != NULL)
*key = json_object_iter_key(*iter);
if (val != NULL)
*val = json_object_iter_value(*iter);
*iter = json_object_iter_next(dict, *iter);
return 1;
}
GhbValue*
ghb_dict_lookup(const GhbValue *dict, const gchar *key)
{
return json_object_get(dict, key);
}
gboolean
ghb_dict_remove(GhbValue *dict, const gchar *key)
{
return json_object_del(dict, key) == 0;
}
GhbValue*
ghb_array_get_nth(const GhbValue *array, gint ii)
{
return json_array_get(array, ii);
}
void
ghb_array_insert(GhbValue *array, guint ii, GhbValue *val)
{
json_array_insert_new(array, ii, val);
}
void
ghb_array_append(GhbValue *array, GhbValue *val)
{
json_array_append_new(array, val);
}
void
ghb_array_remove(GhbValue *array, guint ii)
{
json_array_remove(array, ii);
}
void
ghb_array_replace(GhbValue *array, guint ii, GhbValue *val)
{
if (ii < 0 || ii >= json_array_size(array))
{
g_warning("ghb_array_replace: invalid index");
return;
}
json_array_set_new(array, ii, val);
}
void
ghb_array_copy(GhbValue *arr1, GhbValue *arr2, gint count)
{
gint len, ii;
// empty the first array if it is not already empty
json_array_clear(arr1);
len = ghb_array_len(arr2);
count = MIN(count, len);
for (ii = 0; ii < count; ii++)
ghb_array_append(arr1, ghb_value_dup(ghb_array_get_nth(arr2, ii)));
}
gint
ghb_array_len(const GhbValue *array)
{
return json_array_size(array);
}
|