[c08d201] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Helper functions for json.c * |
---|
| 5 | * * |
---|
[0e788f5] | 6 | * Copyright 2012-2012 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
[c08d201] | 7 | * * |
---|
[4f7255d] | 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. * |
---|
[c08d201] | 12 | * * |
---|
[4f7255d] | 13 | * This program is distributed in the hope that it will be useful, * |
---|
[c08d201] | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
[4f7255d] | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
| 16 | * GNU General Public License for more details. * |
---|
[c08d201] | 17 | * * |
---|
[4f7255d] | 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. * |
---|
[c08d201] | 21 | * * |
---|
| 22 | ****************************************************************************/ |
---|
| 23 | |
---|
| 24 | #include <stdlib.h> |
---|
| 25 | #include <string.h> |
---|
[398b2533] | 26 | #include <glib.h> |
---|
[c08d201] | 27 | |
---|
| 28 | #include "json_util.h" |
---|
| 29 | |
---|
[552da22] | 30 | /* g_memdup() deprecated as of glib 2.68.0 */ |
---|
| 31 | #ifdef GLIB_VERSION_2_68 |
---|
| 32 | #define g_memdup2 g_memdup2 |
---|
| 33 | #else |
---|
| 34 | #define g_memdup2 g_memdup |
---|
| 35 | #endif |
---|
| 36 | |
---|
[5ebff60] | 37 | json_value *json_o_get(const json_value *obj, const json_char *name) |
---|
| 38 | { |
---|
[c08d201] | 39 | int i; |
---|
[5ebff60] | 40 | |
---|
| 41 | if (!obj || obj->type != json_object) { |
---|
[c08d201] | 42 | return NULL; |
---|
[5ebff60] | 43 | } |
---|
[c08d201] | 44 | |
---|
[5ebff60] | 45 | for (i = 0; i < obj->u.object.length; ++i) { |
---|
| 46 | if (strcmp(obj->u.object.values[i].name, name) == 0) { |
---|
[c08d201] | 47 | return obj->u.object.values[i].value; |
---|
[5ebff60] | 48 | } |
---|
| 49 | } |
---|
[c08d201] | 50 | |
---|
| 51 | return NULL; |
---|
| 52 | } |
---|
| 53 | |
---|
[5ebff60] | 54 | const char *json_o_str(const json_value *obj, const json_char *name) |
---|
| 55 | { |
---|
| 56 | json_value *ret = json_o_get(obj, name); |
---|
| 57 | |
---|
| 58 | if (ret && ret->type == json_string) { |
---|
[c08d201] | 59 | return ret->u.string.ptr; |
---|
[5ebff60] | 60 | } else { |
---|
[c08d201] | 61 | return NULL; |
---|
[5ebff60] | 62 | } |
---|
[c08d201] | 63 | } |
---|
[8e3b7ac] | 64 | |
---|
[5ebff60] | 65 | char *json_o_strdup(const json_value *obj, const json_char *name) |
---|
[8e3b7ac] | 66 | { |
---|
[5ebff60] | 67 | json_value *ret = json_o_get(obj, name); |
---|
| 68 | |
---|
| 69 | if (ret && ret->type == json_string && ret->u.string.ptr) { |
---|
[552da22] | 70 | return g_memdup2(ret->u.string.ptr, ret->u.string.length + 1); |
---|
[5ebff60] | 71 | } else { |
---|
[8e3b7ac] | 72 | return NULL; |
---|
[5ebff60] | 73 | } |
---|
[8e3b7ac] | 74 | } |
---|