f27c2f084417eef23b0aa290f3ed0d5dff618931
[freeradius.git] / src / modules / rlm_couchbase / jsonc_missing.c
1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16
17 /*
18  * $Id$
19  *
20  * @brief Workarounds for missing functions in older json-c libraries.
21  * @file json_missing.c
22  *
23  * @copyright 2013-2014 Aaron Hurt <ahurt@anbcs.com>
24  */
25
26 RCSID("$Id$");
27
28 #include <string.h>
29
30 #include "jsonc_missing.h"
31
32 #ifndef HAVE_JSON_OBJECT_GET_STRING_LEN
33 int json_object_get_string_len(json_object *obj) {
34         if (json_object_get_type(obj) != json_type_string)
35                 return 0;
36         return (int)strlen(json_object_to_json_string(obj));
37 }
38 #endif
39
40 #ifndef HAVE_JSON_OBJECT_OBJECT_GET_EX
41 int json_object_object_get_ex(struct json_object *jso, const char *key, struct json_object **value) {
42         struct json_object *jobj;
43
44         if ((jso == NULL) || (key == NULL)) return 0;
45         if (value != NULL) *value = NULL;
46
47         switch (json_object_get_type(jso)) {
48         case json_type_object:
49                 jobj = json_object_object_get(jso, key);
50                 if (jobj == NULL) return 0;
51
52                 if (value != NULL) *value = jobj;
53                 return 1;
54
55         default:
56                 if (value != NULL) *value = NULL;
57                 return 0;
58         }
59 }
60 #endif
61
62 #ifndef HAVE_JSON_TOKENER_PARSE_VERBOSE
63 struct json_object* json_tokener_parse_verbose(const char *str, enum json_tokener_error *error) {
64         struct json_tokener* tok;
65         struct json_object* obj;
66
67         tok = json_tokener_new();
68         if (!tok)
69                 return NULL;
70         obj = json_tokener_parse_ex(tok, str, -1);
71         *error = tok->err;
72         if(tok->err != json_tokener_success) {
73                 if (obj != NULL)
74                         json_object_put(obj);
75                 obj = NULL;
76         }
77
78         json_tokener_free(tok);
79         return obj;
80 }
81 #endif
82
83 #ifndef HAVE_JSON_TOKENER_GET_ERROR
84 enum json_tokener_error json_tokener_get_error(json_tokener *tok) {
85         return tok->err;
86 }
87 #endif
88
89 #ifndef HAVE_JSON_TOKENER_ERROR_DESC
90 const char *json_tokener_error_desc(enum json_tokener_error jerr) {
91         int jerr_int = (int)jerr;
92         if (json_tokener_errors[jerr_int] == NULL)
93                 return "Unknown error, invalid json_tokener_error value passed to json_tokener_error_desc()";
94         return json_tokener_errors[jerr_int];
95 }
96 #endif