summaryrefslogtreecommitdiffstats
path: root/src/plist.c
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2008-12-13 16:16:58 +0100
committerGravatar Jonathan Beck2008-12-13 16:16:58 +0100
commitc39685d3a87858e7ad8ada0da2798aaf670969b4 (patch)
tree743a653c4e29209b851d9602912aa5a74e9ef687 /src/plist.c
parente220e2cf08809a6a8853a8c9c7b06cef4e90cb57 (diff)
downloadlibplist-c39685d3a87858e7ad8ada0da2798aaf670969b4.tar.gz
libplist-c39685d3a87858e7ad8ada0da2798aaf670969b4.tar.bz2
Refine API and fix some warnings.
Diffstat (limited to 'src/plist.c')
-rw-r--r--src/plist.c166
1 files changed, 60 insertions, 106 deletions
diff --git a/src/plist.c b/src/plist.c
index 172eceb..ca80c74 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -30,14 +30,14 @@
30 30
31plist_t plist_new_node(plist_data_t data) 31plist_t plist_new_node(plist_data_t data)
32{ 32{
33 return (plist_t)g_node_new(data); 33 return (plist_t) g_node_new(data);
34} 34}
35 35
36plist_data_t plist_get_data(plist_t node) 36plist_data_t plist_get_data(const plist_t node)
37{ 37{
38 if (!node) 38 if (!node)
39 return NULL; 39 return NULL;
40 return ((GNode*)node)->data; 40 return ((GNode *) node)->data;
41} 41}
42 42
43plist_data_t plist_new_plist_data() 43plist_data_t plist_new_plist_data()
@@ -48,90 +48,71 @@ plist_data_t plist_new_plist_data()
48 48
49void plist_free_plist_data(plist_data_t data) 49void plist_free_plist_data(plist_data_t data)
50{ 50{
51 free(data); 51 if (data) {
52} 52 switch (data->type) {
53
54void plist_new_dict(plist_t * plist)
55{
56 if (*plist != NULL)
57 return;
58 plist_data_t data = plist_new_plist_data();
59 data->type = PLIST_DICT;
60 *plist = plist_new_node(data);
61}
62 53
63void plist_new_array(plist_t * plist) 54 default:
64{ 55 break;
65 if (*plist != NULL) 56 }
66 return; 57 free(data);
67 plist_data_t data = plist_new_plist_data(); 58 }
68 data->type = PLIST_ARRAY;
69 *plist = plist_new_node(data);
70} 59}
71 60
72void plist_new_dict_in_plist(plist_t plist, plist_t * dict) 61plist_t plist_new_dict()
73{ 62{
74 if (!plist || *dict)
75 return;
76
77 plist_data_t data = plist_new_plist_data(); 63 plist_data_t data = plist_new_plist_data();
78 data->type = PLIST_DICT; 64 data->type = PLIST_DICT;
79 *dict = plist_new_node(data); 65 return plist_new_node(data);
80 g_node_append(plist, *dict);
81} 66}
82 67
83 68plist_t plist_new_array()
84/** Adds a new key pair to a dict.
85 *
86 * @param dict The dict node in the plist.
87 * @param key the key name of the key pair.
88 * @param type The the type of the value in the key pair.
89 * @param value a pointer to the actual buffer containing the value. WARNING : the buffer is supposed to match the type of the value
90 *
91 */
92void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value, uint64_t length)
93{ 69{
94 if (!dict || !key || !value)
95 return;
96
97 plist_data_t data = plist_new_plist_data(); 70 plist_data_t data = plist_new_plist_data();
98 data->type = PLIST_KEY; 71 data->type = PLIST_ARRAY;
99 data->strval = strdup(key); 72 return plist_new_node(data);
100 plist_t keynode = plist_new_node(data); 73}
101 g_node_append(dict, keynode); 74
102 75plist_t plist_add_sub_element(plist_t node, plist_type type, void *value, uint64_t length)
103 //now handle value 76{
104 plist_data_t val = plist_new_plist_data(); 77 //only structured types are allowed to have nulll value
105 val->type = type; 78 if (!value && (type == PLIST_DICT || type == PLIST_ARRAY)) {
106 val->length = length; 79 //now handle value
80 plist_data_t data = plist_new_plist_data();
81 data->type = type;
82 data->length = length;
83
84 switch (type) {
85 case PLIST_BOOLEAN:
86 data->boolval = *((char *) value);
87 break;
88 case PLIST_UINT:
89 data->intval = *((uint64_t *) value);
90 break;
91 case PLIST_REAL:
92 data->realval = *((double *) value);
93 break;
94 case PLIST_STRING:
95 data->strval = strdup((char *) value);
96 break;
97 case PLIST_UNICODE:
98 data->unicodeval = wcsdup((wchar_t *) value);
99 break;
100 case PLIST_DATA:
101 memcpy(data->buff, value, length);
102 break;
103 case PLIST_ARRAY:
104 case PLIST_DICT:
105 case PLIST_DATE:
106 default:
107 break;
108 }
107 109
108 switch (type) { 110 plist_t subnode = plist_new_node(data);
109 case PLIST_BOOLEAN: 111 if (node)
110 val->boolval = *((char *) value); 112 g_node_append(node, subnode);
111 break; 113 return subnode;
112 case PLIST_UINT: 114 } else
113 val->intval = *((uint64_t *) value); 115 return NULL;
114 break;
115 case PLIST_REAL:
116 val->realval = *((double *) value);
117 break;
118 case PLIST_STRING:
119 val->strval = strdup((char *) value);
120 break;
121 case PLIST_UNICODE:
122 val->unicodeval = wcsdup((wchar_t *) value);
123 break;
124 case PLIST_DATA:
125 memcpy(val->buff, value, length);
126 break;
127 case PLIST_ARRAY:
128 case PLIST_DICT:
129 case PLIST_DATE:
130 default:
131 break;
132 }
133 plist_t valnode = plist_new_node(val);
134 g_node_append(dict, valnode);
135} 116}
136 117
137void plist_free(plist_t plist) 118void plist_free(plist_t plist)
@@ -141,44 +122,17 @@ void plist_free(plist_t plist)
141 122
142plist_t plist_get_first_child(plist_t node) 123plist_t plist_get_first_child(plist_t node)
143{ 124{
144 return (plist_t)g_node_first_child( (GNode*)node ); 125 return (plist_t) g_node_first_child((GNode *) node);
145} 126}
146 127
147plist_t plist_get_next_sibling(plist_t node) 128plist_t plist_get_next_sibling(plist_t node)
148{ 129{
149 return (plist_t)g_node_next_sibling( (GNode*)node ); 130 return (plist_t) g_node_next_sibling((GNode *) node);
150} 131}
151 132
152plist_t plist_get_prev_sibling(plist_t node) 133plist_t plist_get_prev_sibling(plist_t node)
153{ 134{
154 return (plist_t)g_node_prev_sibling( (GNode*)node ); 135 return (plist_t) g_node_prev_sibling((GNode *) node);
155}
156
157plist_t plist_find_query_node(plist_t plist, char *key, char *request)
158{
159 if (!plist)
160 return NULL;
161
162 plist_t current = NULL;
163 plist_t next = NULL;
164 for (current = plist_get_first_child(plist); current; current = next) {
165
166 next = plist_get_next_sibling(current);
167 plist_data_t data = plist_get_data(current);
168
169 if (data->type == PLIST_KEY && !strcmp(data->strval, key) && next) {
170
171 data = plist_get_data(next);
172 if (data->type == PLIST_STRING && !strcmp(data->strval, request))
173 return next;
174 }
175 if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) {
176 plist_t sub = plist_find_query_node(current, key, request);
177 if (sub)
178 return sub;
179 }
180 }
181 return NULL;
182} 136}
183 137
184char compare_node_value(plist_type type, plist_data_t data, void *value) 138char compare_node_value(plist_type type, plist_data_t data, void *value)