1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Helper functions for json.c * |
---|
5 | * * |
---|
6 | * Copyright 2012-2012 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
7 | * * |
---|
8 | * This program is free software; you can redistribute it and/or modify * |
---|
9 | * it under the terms of the GNU General Public License as published by * |
---|
10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
11 | * (at your option) any later version. * |
---|
12 | * * |
---|
13 | * This program is distributed in the hope that it will be useful, * |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
16 | * GNU General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU General Public License along * |
---|
19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
21 | * * |
---|
22 | ****************************************************************************/ |
---|
23 | |
---|
24 | #include <stdlib.h> |
---|
25 | #include <string.h> |
---|
26 | #include <glib.h> |
---|
27 | |
---|
28 | #include "json_util.h" |
---|
29 | |
---|
30 | /* g_memdup() deprecated as of glib 2.68.0 */ |
---|
31 | #ifndef GLIB_VERSION_2_68 |
---|
32 | #define g_memdup2 g_memdup |
---|
33 | #endif |
---|
34 | |
---|
35 | json_value *json_o_get(const json_value *obj, const json_char *name) |
---|
36 | { |
---|
37 | int i; |
---|
38 | |
---|
39 | if (!obj || obj->type != json_object) { |
---|
40 | return NULL; |
---|
41 | } |
---|
42 | |
---|
43 | for (i = 0; i < obj->u.object.length; ++i) { |
---|
44 | if (strcmp(obj->u.object.values[i].name, name) == 0) { |
---|
45 | return obj->u.object.values[i].value; |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | return NULL; |
---|
50 | } |
---|
51 | |
---|
52 | const char *json_o_str(const json_value *obj, const json_char *name) |
---|
53 | { |
---|
54 | json_value *ret = json_o_get(obj, name); |
---|
55 | |
---|
56 | if (ret && ret->type == json_string) { |
---|
57 | return ret->u.string.ptr; |
---|
58 | } else { |
---|
59 | return NULL; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | char *json_o_strdup(const json_value *obj, const json_char *name) |
---|
64 | { |
---|
65 | json_value *ret = json_o_get(obj, name); |
---|
66 | |
---|
67 | if (ret && ret->type == json_string && ret->u.string.ptr) { |
---|
68 | return g_memdup2(ret->u.string.ptr, ret->u.string.length + 1); |
---|
69 | } else { |
---|
70 | return NULL; |
---|
71 | } |
---|
72 | } |
---|