qdict.c (12c06d6f967a63515399b9e1f6a40f5ce871a8b7) qdict.c (7dc847ebba953db90853d15f140c20eef74d4fb2)
1/*
2 * QDict Module
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *

--- 192 unchanged lines hidden (view full) ---

201 * qdict_get_double(): Get an number mapped by 'key'
202 *
203 * This function assumes that 'key' exists and it stores a QNum.
204 *
205 * Return number mapped by 'key'.
206 */
207double qdict_get_double(const QDict *qdict, const char *key)
208{
1/*
2 * QDict Module
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *

--- 192 unchanged lines hidden (view full) ---

201 * qdict_get_double(): Get an number mapped by 'key'
202 *
203 * This function assumes that 'key' exists and it stores a QNum.
204 *
205 * Return number mapped by 'key'.
206 */
207double qdict_get_double(const QDict *qdict, const char *key)
208{
209 return qnum_get_double(qobject_to_qnum(qdict_get(qdict, key)));
209 return qnum_get_double(qobject_to(QNum, qdict_get(qdict, key)));
210}
211
212/**
213 * qdict_get_int(): Get an integer mapped by 'key'
214 *
215 * This function assumes that 'key' exists and it stores a
216 * QNum representable as int.
217 *
218 * Return integer mapped by 'key'.
219 */
220int64_t qdict_get_int(const QDict *qdict, const char *key)
221{
210}
211
212/**
213 * qdict_get_int(): Get an integer mapped by 'key'
214 *
215 * This function assumes that 'key' exists and it stores a
216 * QNum representable as int.
217 *
218 * Return integer mapped by 'key'.
219 */
220int64_t qdict_get_int(const QDict *qdict, const char *key)
221{
222 return qnum_get_int(qobject_to_qnum(qdict_get(qdict, key)));
222 return qnum_get_int(qobject_to(QNum, qdict_get(qdict, key)));
223}
224
225/**
226 * qdict_get_bool(): Get a bool mapped by 'key'
227 *
228 * This function assumes that 'key' exists and it stores a
229 * QBool object.
230 *
231 * Return bool mapped by 'key'.
232 */
233bool qdict_get_bool(const QDict *qdict, const char *key)
234{
223}
224
225/**
226 * qdict_get_bool(): Get a bool mapped by 'key'
227 *
228 * This function assumes that 'key' exists and it stores a
229 * QBool object.
230 *
231 * Return bool mapped by 'key'.
232 */
233bool qdict_get_bool(const QDict *qdict, const char *key)
234{
235 return qbool_get_bool(qobject_to_qbool(qdict_get(qdict, key)));
235 return qbool_get_bool(qobject_to(QBool, qdict_get(qdict, key)));
236}
237
238/**
239 * qdict_get_qlist(): If @qdict maps @key to a QList, return it, else NULL.
240 */
241QList *qdict_get_qlist(const QDict *qdict, const char *key)
242{
236}
237
238/**
239 * qdict_get_qlist(): If @qdict maps @key to a QList, return it, else NULL.
240 */
241QList *qdict_get_qlist(const QDict *qdict, const char *key)
242{
243 return qobject_to_qlist(qdict_get(qdict, key));
243 return qobject_to(QList, qdict_get(qdict, key));
244}
245
246/**
247 * qdict_get_qdict(): If @qdict maps @key to a QDict, return it, else NULL.
248 */
249QDict *qdict_get_qdict(const QDict *qdict, const char *key)
250{
244}
245
246/**
247 * qdict_get_qdict(): If @qdict maps @key to a QDict, return it, else NULL.
248 */
249QDict *qdict_get_qdict(const QDict *qdict, const char *key)
250{
251 return qobject_to_qdict(qdict_get(qdict, key));
251 return qobject_to(QDict, qdict_get(qdict, key));
252}
253
254/**
255 * qdict_get_str(): Get a pointer to the stored string mapped
256 * by 'key'
257 *
258 * This function assumes that 'key' exists and it stores a
259 * QString object.
260 *
261 * Return pointer to the string mapped by 'key'.
262 */
263const char *qdict_get_str(const QDict *qdict, const char *key)
264{
252}
253
254/**
255 * qdict_get_str(): Get a pointer to the stored string mapped
256 * by 'key'
257 *
258 * This function assumes that 'key' exists and it stores a
259 * QString object.
260 *
261 * Return pointer to the string mapped by 'key'.
262 */
263const char *qdict_get_str(const QDict *qdict, const char *key)
264{
265 return qstring_get_str(qobject_to_qstring(qdict_get(qdict, key)));
265 return qstring_get_str(qobject_to(QString, qdict_get(qdict, key)));
266}
267
268/**
269 * qdict_get_try_int(): Try to get integer mapped by 'key'
270 *
271 * Return integer mapped by 'key', if it is not present in the
272 * dictionary or if the stored object is not a QNum representing an
273 * integer, 'def_value' will be returned.
274 */
275int64_t qdict_get_try_int(const QDict *qdict, const char *key,
276 int64_t def_value)
277{
266}
267
268/**
269 * qdict_get_try_int(): Try to get integer mapped by 'key'
270 *
271 * Return integer mapped by 'key', if it is not present in the
272 * dictionary or if the stored object is not a QNum representing an
273 * integer, 'def_value' will be returned.
274 */
275int64_t qdict_get_try_int(const QDict *qdict, const char *key,
276 int64_t def_value)
277{
278 QNum *qnum = qobject_to_qnum(qdict_get(qdict, key));
278 QNum *qnum = qobject_to(QNum, qdict_get(qdict, key));
279 int64_t val;
280
281 if (!qnum || !qnum_get_try_int(qnum, &val)) {
282 return def_value;
283 }
284
285 return val;
286}
287
288/**
289 * qdict_get_try_bool(): Try to get a bool mapped by 'key'
290 *
291 * Return bool mapped by 'key', if it is not present in the
292 * dictionary or if the stored object is not of QBool type
293 * 'def_value' will be returned.
294 */
295bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value)
296{
279 int64_t val;
280
281 if (!qnum || !qnum_get_try_int(qnum, &val)) {
282 return def_value;
283 }
284
285 return val;
286}
287
288/**
289 * qdict_get_try_bool(): Try to get a bool mapped by 'key'
290 *
291 * Return bool mapped by 'key', if it is not present in the
292 * dictionary or if the stored object is not of QBool type
293 * 'def_value' will be returned.
294 */
295bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value)
296{
297 QBool *qbool = qobject_to_qbool(qdict_get(qdict, key));
297 QBool *qbool = qobject_to(QBool, qdict_get(qdict, key));
298
299 return qbool ? qbool_get_bool(qbool) : def_value;
300}
301
302/**
303 * qdict_get_try_str(): Try to get a pointer to the stored string
304 * mapped by 'key'
305 *
306 * Return a pointer to the string mapped by 'key', if it is not present
307 * in the dictionary or if the stored object is not of QString type
308 * NULL will be returned.
309 */
310const char *qdict_get_try_str(const QDict *qdict, const char *key)
311{
298
299 return qbool ? qbool_get_bool(qbool) : def_value;
300}
301
302/**
303 * qdict_get_try_str(): Try to get a pointer to the stored string
304 * mapped by 'key'
305 *
306 * Return a pointer to the string mapped by 'key', if it is not present
307 * in the dictionary or if the stored object is not of QString type
308 * NULL will be returned.
309 */
310const char *qdict_get_try_str(const QDict *qdict, const char *key)
311{
312 QString *qstr = qobject_to_qstring(qdict_get(qdict, key));
312 QString *qstr = qobject_to(QString, qdict_get(qdict, key));
313
314 return qstr ? qstring_get_str(qstr) : NULL;
315}
316
317/**
318 * qdict_iter(): Iterate over all the dictionary's stored values.
319 *
320 * This function allows the user to provide an iterator, which will be

--- 106 unchanged lines hidden (view full) ---

427 * qdict_is_equal(): Test whether the two QDicts are equal
428 *
429 * Here, equality means whether they contain the same keys and whether
430 * the respective values are in turn equal (i.e. invoking
431 * qobject_is_equal() on them yields true).
432 */
433bool qdict_is_equal(const QObject *x, const QObject *y)
434{
313
314 return qstr ? qstring_get_str(qstr) : NULL;
315}
316
317/**
318 * qdict_iter(): Iterate over all the dictionary's stored values.
319 *
320 * This function allows the user to provide an iterator, which will be

--- 106 unchanged lines hidden (view full) ---

427 * qdict_is_equal(): Test whether the two QDicts are equal
428 *
429 * Here, equality means whether they contain the same keys and whether
430 * the respective values are in turn equal (i.e. invoking
431 * qobject_is_equal() on them yields true).
432 */
433bool qdict_is_equal(const QObject *x, const QObject *y)
434{
435 const QDict *dict_x = qobject_to_qdict(x);
436 const QDict *dict_y = qobject_to_qdict(y);
435 const QDict *dict_x = qobject_to(QDict, x);
436 const QDict *dict_y = qobject_to(QDict, y);
437 const QDictEntry *e;
438
439 if (qdict_size(dict_x) != qdict_size(dict_y)) {
440 return false;
441 }
442
443 for (e = qdict_first(dict_x); e; e = qdict_next(dict_x, e)) {
444 const QObject *obj_x = qdict_entry_value(e);

--- 11 unchanged lines hidden (view full) ---

456 * qdict_destroy_obj(): Free all the memory allocated by a QDict
457 */
458void qdict_destroy_obj(QObject *obj)
459{
460 int i;
461 QDict *qdict;
462
463 assert(obj != NULL);
437 const QDictEntry *e;
438
439 if (qdict_size(dict_x) != qdict_size(dict_y)) {
440 return false;
441 }
442
443 for (e = qdict_first(dict_x); e; e = qdict_next(dict_x, e)) {
444 const QObject *obj_x = qdict_entry_value(e);

--- 11 unchanged lines hidden (view full) ---

456 * qdict_destroy_obj(): Free all the memory allocated by a QDict
457 */
458void qdict_destroy_obj(QObject *obj)
459{
460 int i;
461 QDict *qdict;
462
463 assert(obj != NULL);
464 qdict = qobject_to_qdict(obj);
464 qdict = qobject_to(QDict, obj);
465
466 for (i = 0; i < QDICT_BUCKET_MAX; i++) {
467 QDictEntry *entry = QLIST_FIRST(&qdict->table[i]);
468 while (entry) {
469 QDictEntry *tmp = QLIST_NEXT(entry, next);
470 QLIST_REMOVE(entry, next);
471 qentry_destroy(entry);
472 entry = tmp;

--- 54 unchanged lines hidden (view full) ---

527
528 entry = qlist_first(qlist);
529
530 for (i = 0; entry; entry = qlist_next(entry), i++) {
531 value = qlist_entry_obj(entry);
532 new_key = g_strdup_printf("%s.%i", prefix, i);
533
534 if (qobject_type(value) == QTYPE_QDICT) {
465
466 for (i = 0; i < QDICT_BUCKET_MAX; i++) {
467 QDictEntry *entry = QLIST_FIRST(&qdict->table[i]);
468 while (entry) {
469 QDictEntry *tmp = QLIST_NEXT(entry, next);
470 QLIST_REMOVE(entry, next);
471 qentry_destroy(entry);
472 entry = tmp;

--- 54 unchanged lines hidden (view full) ---

527
528 entry = qlist_first(qlist);
529
530 for (i = 0; entry; entry = qlist_next(entry), i++) {
531 value = qlist_entry_obj(entry);
532 new_key = g_strdup_printf("%s.%i", prefix, i);
533
534 if (qobject_type(value) == QTYPE_QDICT) {
535 qdict_flatten_qdict(qobject_to_qdict(value), target, new_key);
535 qdict_flatten_qdict(qobject_to(QDict, value), target, new_key);
536 } else if (qobject_type(value) == QTYPE_QLIST) {
536 } else if (qobject_type(value) == QTYPE_QLIST) {
537 qdict_flatten_qlist(qobject_to_qlist(value), target, new_key);
537 qdict_flatten_qlist(qobject_to(QList, value), target, new_key);
538 } else {
539 /* All other types are moved to the target unchanged. */
540 qobject_incref(value);
541 qdict_put_obj(target, new_key, value);
542 }
543
544 g_free(new_key);
545 }

--- 17 unchanged lines hidden (view full) ---

563
564 if (prefix) {
565 new_key = g_strdup_printf("%s.%s", prefix, entry->key);
566 }
567
568 if (qobject_type(value) == QTYPE_QDICT) {
569 /* Entries of QDicts are processed recursively, the QDict object
570 * itself disappears. */
538 } else {
539 /* All other types are moved to the target unchanged. */
540 qobject_incref(value);
541 qdict_put_obj(target, new_key, value);
542 }
543
544 g_free(new_key);
545 }

--- 17 unchanged lines hidden (view full) ---

563
564 if (prefix) {
565 new_key = g_strdup_printf("%s.%s", prefix, entry->key);
566 }
567
568 if (qobject_type(value) == QTYPE_QDICT) {
569 /* Entries of QDicts are processed recursively, the QDict object
570 * itself disappears. */
571 qdict_flatten_qdict(qobject_to_qdict(value), target,
571 qdict_flatten_qdict(qobject_to(QDict, value), target,
572 new_key ? new_key : entry->key);
573 delete = true;
574 } else if (qobject_type(value) == QTYPE_QLIST) {
572 new_key ? new_key : entry->key);
573 delete = true;
574 } else if (qobject_type(value) == QTYPE_QLIST) {
575 qdict_flatten_qlist(qobject_to_qlist(value), target,
575 qdict_flatten_qlist(qobject_to(QList, value), target,
576 new_key ? new_key : entry->key);
577 delete = true;
578 } else if (prefix) {
579 /* All other objects are moved to the target unchanged. */
580 qobject_incref(value);
581 qdict_put_obj(target, new_key, value);
582 delete = true;
583 }

--- 315 unchanged lines hidden (view full) ---

899 prefix);
900 goto error;
901 }
902 } else {
903 child = QOBJECT(qdict_new());
904 qdict_put_obj(two_level, prefix, child);
905 }
906 qobject_incref(ent->value);
576 new_key ? new_key : entry->key);
577 delete = true;
578 } else if (prefix) {
579 /* All other objects are moved to the target unchanged. */
580 qobject_incref(value);
581 qdict_put_obj(target, new_key, value);
582 delete = true;
583 }

--- 315 unchanged lines hidden (view full) ---

899 prefix);
900 goto error;
901 }
902 } else {
903 child = QOBJECT(qdict_new());
904 qdict_put_obj(two_level, prefix, child);
905 }
906 qobject_incref(ent->value);
907 qdict_put_obj(qobject_to_qdict(child), suffix, ent->value);
907 qdict_put_obj(qobject_to(QDict, child), suffix, ent->value);
908 } else {
909 if (child) {
910 error_setg(errp, "Key %s prefix is already set as a dict",
911 prefix);
912 goto error;
913 }
914 qobject_incref(ent->value);
915 qdict_put_obj(two_level, prefix, ent->value);

--- 5 unchanged lines hidden (view full) ---

921
922 /* Step 2: optionally process the two level dict recursively
923 * into a multi-level dict */
924 multi_level = qdict_new();
925 for (ent = qdict_first(two_level); ent != NULL;
926 ent = qdict_next(two_level, ent)) {
927
928 if (qobject_type(ent->value) == QTYPE_QDICT) {
908 } else {
909 if (child) {
910 error_setg(errp, "Key %s prefix is already set as a dict",
911 prefix);
912 goto error;
913 }
914 qobject_incref(ent->value);
915 qdict_put_obj(two_level, prefix, ent->value);

--- 5 unchanged lines hidden (view full) ---

921
922 /* Step 2: optionally process the two level dict recursively
923 * into a multi-level dict */
924 multi_level = qdict_new();
925 for (ent = qdict_first(two_level); ent != NULL;
926 ent = qdict_next(two_level, ent)) {
927
928 if (qobject_type(ent->value) == QTYPE_QDICT) {
929 child = qdict_crumple(qobject_to_qdict(ent->value), errp);
929 child = qdict_crumple(qobject_to(QDict, ent->value), errp);
930 if (!child) {
931 goto error;
932 }
933
934 qdict_put_obj(multi_level, ent->key, child);
935 } else {
936 qobject_incref(ent->value);
937 qdict_put_obj(multi_level, ent->key, ent->value);

--- 18 unchanged lines hidden (view full) ---

956 g_free(key);
957
958 if (!child) {
959 error_setg(errp, "Missing list index %zu", i);
960 goto error;
961 }
962
963 qobject_incref(child);
930 if (!child) {
931 goto error;
932 }
933
934 qdict_put_obj(multi_level, ent->key, child);
935 } else {
936 qobject_incref(ent->value);
937 qdict_put_obj(multi_level, ent->key, ent->value);

--- 18 unchanged lines hidden (view full) ---

956 g_free(key);
957
958 if (!child) {
959 error_setg(errp, "Missing list index %zu", i);
960 goto error;
961 }
962
963 qobject_incref(child);
964 qlist_append_obj(qobject_to_qlist(dst), child);
964 qlist_append_obj(qobject_to(QList, dst), child);
965 }
966 QDECREF(multi_level);
967 multi_level = NULL;
968 } else {
969 dst = QOBJECT(multi_level);
970 }
971
972 return dst;

--- 136 unchanged lines hidden ---
965 }
966 QDECREF(multi_level);
967 multi_level = NULL;
968 } else {
969 dst = QOBJECT(multi_level);
970 }
971
972 return dst;

--- 136 unchanged lines hidden ---