Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/support/json/json-ident.c

raw
1 #include "json-ident.h"
2 #include "support/otfcc-alloc.h"
3
4 #ifdef _MSC_VER
5 #include "winfns.h"
6 #endif
7
8 static bool compare_json_arrays(const json_value *a, const json_value *b) {
9 for (uint16_t j = 0; j < a->u.array.length; j++) {
10 if (!json_ident(a->u.array.values[j], b->u.array.values[j])) { return false; }
11 }
12 return true;
13 }
14
15 typedef struct {
16 char *key;
17 json_value *val;
18 bool check;
19 UT_hash_handle hh;
20 } json_obj_entry;
21 static bool compare_json_objects(const json_value *a, const json_value *b) {
22 json_obj_entry *h = NULL;
23 for (uint32_t j = 0; j < a->u.object.length; j++) {
24 char *k = a->u.object.values[j].name;
25 json_obj_entry *e = NULL;
26 HASH_FIND_STR(h, k, e);
27 if (!e) {
28 NEW(e);
29 e->key = xstrdup(k);
30 e->val = a->u.object.values[j].value;
31 e->check = false;
32 HASH_ADD_STR(h, key, e);
33 }
34 }
35 bool allcheck = true;
36 for (uint32_t j = 0; j < b->u.object.length; j++) {
37 char *k = b->u.object.values[j].name;
38 json_obj_entry *e = NULL;
39 HASH_FIND_STR(h, k, e);
40 if (!e) {
41 allcheck = false;
42 break;
43 } else {
44 bool check = json_ident(e->val, b->u.object.values[j].value);
45 if (!check) {
46 allcheck = false;
47 break;
48 } else {
49 e->check = true;
50 }
51 }
52 }
53 json_obj_entry *e, *tmp;
54 HASH_ITER(hh, h, e, tmp) {
55 allcheck = allcheck && e->check;
56 HASH_DEL(h, e);
57 FREE(e->key);
58 FREE(e);
59 }
60 return allcheck;
61 }
62
63 bool json_ident(const json_value *a, const json_value *b) {
64 if (!a && !b) return true;
65 if (!a || !b) return false;
66 if (a->type != b->type) return false;
67 switch (a->type) {
68 case json_none:
69 case json_null:
70 return true;
71 case json_integer:
72 return a->u.integer == b->u.integer;
73 case json_double:
74 return a->u.dbl == b->u.dbl;
75 case json_boolean:
76 return a->u.boolean == b->u.boolean;
77 case json_string:
78 return a->u.string.length == b->u.string.length && strcmp(a->u.string.ptr, b->u.string.ptr) == 0;
79 case json_array:
80 return a->u.array.length == b->u.array.length && compare_json_arrays(a, b);
81 case json_object:
82 return a->u.object.length == b->u.object.length && compare_json_objects(a, b);
83 default:
84 return false;
85 }
86 }