source: lib/json_util.c @ 552da22

Last change on this file since 552da22 was 552da22, checked in by GitHub <noreply@…>, at 2023-04-01T20:29:42Z

Use g_memdup2() with glib versions >= 2.68.0 (#168)

  • Use g_memdup2() with glib versions >= 2.68.0

g_memdup() was deprecated in glib 2.68.0. The new function is
g_memdup2(). Still support building with older versions of glib via
macros.

Signed-off-by: David Cantrell <dcantrell@…>

  • Fall back to g_memdup when g_memdup2 is not available, rather than defining custom macro

Signed-off-by: David Cantrell <dcantrell@…>
Co-authored-by: Jelmer Vernooij <jelmer@…>

  • Property mode set to 100644
File size: 2.7 KB
Line 
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#ifdef GLIB_VERSION_2_68
32#define g_memdup2 g_memdup2
33#else
34#define g_memdup2 g_memdup
35#endif
36
37json_value *json_o_get(const json_value *obj, const json_char *name)
38{
39        int i;
40
41        if (!obj || obj->type != json_object) {
42                return NULL;
43        }
44
45        for (i = 0; i < obj->u.object.length; ++i) {
46                if (strcmp(obj->u.object.values[i].name, name) == 0) {
47                        return obj->u.object.values[i].value;
48                }
49        }
50
51        return NULL;
52}
53
54const 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) {
59                return ret->u.string.ptr;
60        } else {
61                return NULL;
62        }
63}
64
65char *json_o_strdup(const json_value *obj, const json_char *name)
66{
67        json_value *ret = json_o_get(obj, name);
68
69        if (ret && ret->type == json_string && ret->u.string.ptr) {
70                return g_memdup2(ret->u.string.ptr, ret->u.string.length + 1);
71        } else {
72                return NULL;
73        }
74}
Note: See TracBrowser for help on using the repository browser.