Debian Codegraph @

libs/pplib/pplib-src/src/ppdict.c

raw
1
2 #include "pplib.h"
3
4 ppdict * ppdict_create (const ppobj *stackpos, size_t size, ppheap *heap)
5 {
6 ppdict *dict;
7 ppobj *data;
8 ppname **pkey;
9 size_t i;
10
11 size >>= 1; // num of key-value pairs
12 dict = (ppdict *)ppstruct_take(heap, sizeof(ppdict));
13 dict->data = data = (ppobj *)ppstruct_take(heap, size * sizeof(ppobj));
14 dict->keys = pkey = (ppname **)ppstruct_take(heap, (size + 1) * sizeof(ppname **));
15 dict->size = 0;
16
17 for (i = 0; i < size; ++i, stackpos += 2)
18 {
19 if (stackpos->type != PPNAME) // we need this check at lest for trailer hack
20 continue;
21 *pkey = stackpos->name;
22 *data = *(stackpos + 1);
23 ++pkey, ++data, ++dict->size;
24 }
25 *pkey = NULL; // sentinel for convinient iteration
26 return dict;
27 }
28
29 ppobj * ppdict_get_obj (ppdict *dict, const char *name)
30 {
31 ppname **pkey;
32 ppobj *obj;
33
34 for (ppdict_first(dict, pkey, obj); *pkey != NULL; ppdict_next(pkey, obj))
35 if (strcmp((*pkey)->data, name) == 0) // not ppname_eq() or ppname_is()!!
36 return obj;
37 return NULL;
38 }
39
40 ppobj * ppdict_rget_obj (ppdict *dict, const char *name)
41 {
42 ppobj *obj;
43 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_rget_obj(obj) : NULL;
44 }
45
46 int ppdict_get_bool (ppdict *dict, const char *name, int *v)
47 {
48 ppobj *obj;
49 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_get_bool(obj, *v) : 0;
50 }
51
52 int ppdict_rget_bool (ppdict *dict, const char *name, int *v)
53 {
54 ppobj *obj;
55 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_rget_bool(obj, *v) : 0;
56 }
57
58 int ppdict_get_int (ppdict *dict, const char *name, ppint *v)
59 {
60 ppobj *obj;
61 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_get_int(obj, *v) : 0;
62 }
63
64 int ppdict_rget_int (ppdict *dict, const char *name, ppint *v)
65 {
66 ppobj *obj;
67 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_rget_int(obj, *v) : 0;
68 }
69
70 int ppdict_get_uint (ppdict *dict, const char *name, ppuint *v)
71 {
72 ppobj *obj;
73 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_get_uint(obj, *v) : 0;
74 }
75
76 int ppdict_rget_uint (ppdict *dict, const char *name, ppuint *v)
77 {
78 ppobj *obj;
79 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_rget_uint(obj, *v) : 0;
80 }
81
82 int ppdict_get_num (ppdict *dict, const char *name, ppnum *v)
83 {
84 ppobj *obj;
85 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_get_num(obj, *v) : 0;
86 }
87
88 int ppdict_rget_num (ppdict *dict, const char *name, ppnum *v)
89 {
90 ppobj *obj;
91 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_rget_num(obj, *v) : 0;
92 }
93
94 ppname * ppdict_get_name (ppdict *dict, const char *name)
95 {
96 ppobj *obj;
97 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_get_name(obj) : NULL;
98 }
99
100 ppname * ppdict_rget_name (ppdict *dict, const char *name)
101 {
102 ppobj *obj;
103 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_rget_name(obj) : NULL;
104 }
105
106 ppstring * ppdict_get_string (ppdict *dict, const char *name)
107 {
108 ppobj *obj;
109 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_get_string(obj) : NULL;
110 }
111
112 ppstring * ppdict_rget_string (ppdict *dict, const char *name)
113 {
114 ppobj *obj;
115 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_rget_string(obj) : NULL;
116 }
117
118 pparray * ppdict_get_array (ppdict *dict, const char *name)
119 {
120 ppobj *obj;
121 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_get_array(obj) : NULL;
122 }
123
124 pparray * ppdict_rget_array (ppdict *dict, const char *name)
125 {
126 ppobj *obj;
127 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_rget_array(obj) : NULL;
128 }
129
130 ppdict * ppdict_get_dict (ppdict *dict, const char *name)
131 {
132 ppobj *obj;
133 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_get_dict(obj) : NULL;
134 }
135
136 ppdict * ppdict_rget_dict (ppdict *dict, const char *name)
137 {
138 ppobj *obj;
139 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_rget_dict(obj) : NULL;
140 }
141
142 /*
143 ppstream * ppdict_get_stream (ppdict *dict, const char *name)
144 {
145 ppobj *obj;
146 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_get_stream(obj) : NULL;
147 }
148 */
149
150 ppstream * ppdict_rget_stream (ppdict *dict, const char *name)
151 {
152 ppobj *obj;
153 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_rget_stream(obj) : NULL;
154 }
155
156 ppref * ppdict_get_ref (ppdict *dict, const char *name)
157 {
158 ppobj *obj;
159 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_get_ref(obj) : NULL;
160 }
161
162 ppref * ppdict_rget_ref (ppdict *dict, const char *name)
163 {
164 ppobj *obj;
165 return (obj = ppdict_get_obj(dict, name)) != NULL ? ppobj_rget_ref(obj) : NULL;
166 }