Browse code

Update jansson_path.c

in case of build on old debian (lenny) - statically linked libjansson has to be used, as a result build throw errors, cos libjansson has functions with the same names

Vasiliy Ganchev authored on 31/01/2017 06:29:08 • GitHub committed on 31/01/2017 06:29:08
Showing 1 changed files
... ...
@@ -18,9 +18,9 @@
18 18
 #include "../../core/mod_fix.h"
19 19
 
20 20
 /* jansson private helper functions */
21
-void *jsonp_malloc(size_t size);
22
-void jsonp_free(void *ptr);
23
-char *jsonp_strdup(const char *str);
21
+static void *jsonp_malloc(size_t size);
22
+static void jsonp_free(void *ptr);
23
+static char *jsonp_strdup(const char *str);
24 24
 
25 25
 static json_malloc_t do_malloc = malloc;
26 26
 static json_free_t do_free = free;
... ...
@@ -252,21 +252,21 @@ fail:
252 252
 }
253 253
 
254 254
 /* jansson private helper functions */
255
-void *jsonp_malloc(size_t size) {
255
+static void *jsonp_malloc(size_t size) {
256 256
 	if(!size)
257 257
 		return NULL;
258 258
 
259 259
 	return (*do_malloc)(size);
260 260
 }
261 261
 
262
-void jsonp_free(void *ptr) {
262
+static void jsonp_free(void *ptr) {
263 263
 	if(!ptr)
264 264
 		return;
265 265
 
266 266
 	(*do_free)(ptr);
267 267
 }
268 268
 
269
-char *jsonp_strdup(const char *str) {
269
+static char *jsonp_strdup(const char *str) {
270 270
 	char *new_str;
271 271
 
272 272
 	new_str = jsonp_malloc(strlen(str) + 1); 
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -15,7 +15,7 @@
15 15
 
16 16
 #include <jansson.h>
17 17
 
18
-#include "../../mod_fix.h"
18
+#include "../../core/mod_fix.h"
19 19
 
20 20
 /* jansson private helper functions */
21 21
 void *jsonp_malloc(size_t size);
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,279 @@
1
+/*
2
+ * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
3
+ * Copyright (c) 2011-2012 Basile Starynkevitch <basile@starynkevitch.net>
4
+ * Copyright (c) 2012 Rogerz Zhang <rogerz.zhang@gmail.com>
5
+ * Copyright (c) 2013 Flowroute LLC (flowroute.com)
6
+ *
7
+ * Jansson is free software; you can redistribute it and/or modify
8
+ * it under the terms of the MIT license.
9
+ *
10
+ * Pulled from https://github.com/rogerz/jansson/blob/json_path/src/path.c
11
+ */
12
+
13
+#include <string.h>
14
+#include <assert.h>
15
+
16
+#include <jansson.h>
17
+
18
+#include "../../mod_fix.h"
19
+
20
+/* jansson private helper functions */
21
+void *jsonp_malloc(size_t size);
22
+void jsonp_free(void *ptr);
23
+char *jsonp_strdup(const char *str);
24
+
25
+static json_malloc_t do_malloc = malloc;
26
+static json_free_t do_free = free;
27
+
28
+json_t *json_path_get(const json_t *json, const char *path)
29
+{
30
+	static const char array_open = '[';
31
+	static const char *path_delims = ".[", *array_close = "]";
32
+	const json_t *cursor;
33
+	char *token, *buf, *peek, *endptr, delim = '\0';
34
+	const char *expect;
35
+
36
+	if (!json || !path)
37
+		return NULL;
38
+	else
39
+		buf = jsonp_strdup(path);
40
+
41
+	peek = buf;
42
+	token = buf;
43
+	cursor = json;
44
+	expect = path_delims;
45
+
46
+	if (*token == array_open) {
47
+		expect = array_close;
48
+		token++;
49
+	}
50
+
51
+	while (peek && *peek && cursor)
52
+	{
53
+		char *last_peek = peek;
54
+		peek = strpbrk(peek, expect);
55
+		if (peek) {
56
+			if (!token && peek != last_peek)
57
+				goto fail;
58
+			delim = *peek;
59
+			*peek++ = '\0';
60
+		} else if (expect != path_delims || !token) {
61
+			goto fail;
62
+		}
63
+
64
+		if (expect == path_delims) {
65
+			if (token) {
66
+				cursor = json_object_get(cursor, token);
67
+			}
68
+			expect = (delim == array_open ? array_close : path_delims);
69
+			token = peek;
70
+		} else if (expect == array_close) {
71
+			size_t index = strtol(token, &endptr, 0);
72
+			if (*endptr)
73
+				goto fail;
74
+			cursor = json_array_get(cursor, index);
75
+			token = NULL;
76
+			expect = path_delims;
77
+		} else {
78
+			goto fail;
79
+		}
80
+	}
81
+
82
+	jsonp_free(buf);
83
+	return (json_t *)cursor;
84
+fail:
85
+	jsonp_free(buf);
86
+	return NULL;
87
+}
88
+
89
+int json_path_set(json_t *json, const char *path, json_t *value,
90
+		unsigned int append)
91
+{
92
+	static const char array_open = '[';
93
+	static const char object_delim = '.';
94
+	static const char *path_delims = ".[";
95
+	static const char *array_close = "]";
96
+
97
+	json_t *cursor, *parent = NULL;
98
+	char *token, *buf = NULL, *peek, delim = '\0';
99
+	const char *expect;
100
+	int index_saved = -1;
101
+
102
+	if (!json || !path || !value) {
103
+		ERR("invalid arguments\n");
104
+		goto fail;
105
+	} else {
106
+		buf = jsonp_strdup(path);
107
+	}
108
+
109
+	peek = buf;
110
+	token = buf;
111
+	cursor = json;
112
+	expect = path_delims;
113
+
114
+	if (*token == array_open) {
115
+		expect = array_close;
116
+		token++;
117
+	}
118
+
119
+	while (peek && *peek && cursor)
120
+	{
121
+		char *last_peek = peek;
122
+		peek = strpbrk(last_peek, expect);
123
+
124
+		if (peek) {
125
+			if (!token && peek != last_peek) {
126
+				ERR("unexpected trailing chars in JSON path at pos %zu\n",
127
+						last_peek - buf);
128
+				goto fail;
129
+			}
130
+			delim = *peek;
131
+			*peek++ = '\0';
132
+		} else { // end of path
133
+			if (expect == path_delims) {
134
+				break;
135
+			} else {
136
+				ERR("missing ']' at pos %zu\n", peek - buf);
137
+				goto fail;
138
+			}
139
+		}
140
+
141
+		if (expect == path_delims) {
142
+			if (token) {
143
+				if (token[0] == '\0') {
144
+					ERR("empty token at pos %zu\n", peek - buf);
145
+					goto fail;
146
+				}
147
+
148
+				parent = cursor;
149
+				cursor = json_object_get(parent, token);
150
+
151
+				if (!cursor) {
152
+					if (!json_is_object(parent)) {
153
+						ERR("object expected at pos %zu\n", peek - buf);
154
+						goto fail;
155
+					}
156
+					if (delim == object_delim) {
157
+						cursor = json_object();
158
+						json_object_set_new(parent, token, cursor);
159
+					} else {
160
+						ERR("new array is not allowed at pos %zu\n", peek - buf);
161
+						goto fail;
162
+					}
163
+				}
164
+			}
165
+			expect = (delim == array_open ? array_close : path_delims);
166
+			token = peek;
167
+		} else if (expect == array_close) {
168
+			char *endptr;
169
+			size_t index;
170
+
171
+			parent = cursor;
172
+			if (!json_is_array(parent)) {
173
+				ERR("array expected at pos %zu\n", peek - buf);
174
+				goto fail;
175
+			}
176
+
177
+			index = strtol(token, &endptr, 0);
178
+			if (*endptr) {
179
+				ERR("invalid array index at pos %zu\n", peek - buf);
180
+				goto fail;
181
+			}
182
+
183
+			cursor = json_array_get(parent, index);
184
+			if (!cursor) {
185
+				ERR("array index out of bound at pos %zu\n", peek - buf);
186
+				goto fail;
187
+			}
188
+
189
+			index_saved = index;
190
+			token = NULL;
191
+			expect = path_delims;
192
+
193
+		} else {
194
+			ERR("fatal JSON error at pos %zu\n", peek - buf);
195
+			goto fail;
196
+		}
197
+	}
198
+
199
+	if (token && append) {
200
+
201
+		if(strlen(token) > 0) {
202
+			json_t* tmp  = json_object_get(cursor, token);
203
+			if(json_is_array(tmp)) {
204
+				json_array_append(tmp, value);
205
+				json_object_set(cursor, token, tmp);
206
+			} else if(json_is_object(tmp) && json_is_object(value) ) {
207
+				json_object_update(tmp, value);
208
+				json_object_set(cursor, token, tmp);
209
+			} else {
210
+				ERR("JSON array or object expected at pos %zu\n", peek - buf);
211
+				goto fail;
212
+			}
213
+		} else if(json_is_array(cursor)) {
214
+			json_array_append(cursor, value);
215
+
216
+		} else if(json_is_object(cursor) && json_is_object(value)) {
217
+			json_object_update(cursor, value);
218
+
219
+		} else {
220
+			ERR("JSON array or object expected at pos %zu\n", peek - buf);
221
+			goto fail;
222
+		}
223
+
224
+	} else if (token && strlen(token) != 0 ) {
225
+
226
+		if (json_is_object(cursor)) {
227
+			json_object_set(cursor, token, value);
228
+
229
+		} else {
230
+			ERR("JSON object expected at pos %zu\n", peek - buf);
231
+			goto fail;
232
+		}
233
+
234
+		cursor = json_object_get(cursor, token);
235
+	} else if (index_saved != -1 && json_is_array(parent)) {
236
+		json_array_set(parent, index_saved, value);
237
+		cursor = json_array_get(parent, index_saved);
238
+
239
+	} else {
240
+		ERR("invalid JSON path at pos %zu\n", peek - buf);
241
+		goto fail;
242
+	}
243
+
244
+	json_decref(value);
245
+	jsonp_free(buf);
246
+	return 0;
247
+
248
+fail:
249
+	json_decref(value);
250
+	jsonp_free(buf);
251
+	return -1;
252
+}
253
+
254
+/* jansson private helper functions */
255
+void *jsonp_malloc(size_t size) {
256
+	if(!size)
257
+		return NULL;
258
+
259
+	return (*do_malloc)(size);
260
+}
261
+
262
+void jsonp_free(void *ptr) {
263
+	if(!ptr)
264
+		return;
265
+
266
+	(*do_free)(ptr);
267
+}
268
+
269
+char *jsonp_strdup(const char *str) {
270
+	char *new_str;
271
+
272
+	new_str = jsonp_malloc(strlen(str) + 1); 
273
+	if(!new_str)
274
+		return NULL;
275
+
276
+	strcpy(new_str, str);
277
+	return new_str;
278
+}
279
+/* end jansson private helpers */