srjson.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*
  2. Copyright (c) 2009 Dave Gamble
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. /**
  20. * srjson - JSON parser in C - MIT License
  21. * - addapted from cJSON to fit better within Kamailio/SER environment
  22. */
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <math.h>
  26. #include <stdlib.h>
  27. #include <float.h>
  28. #include <limits.h>
  29. #include <ctype.h>
  30. #include "srjson.h"
  31. static const char *ep;
  32. const char *srjson_GetErrorPtr()
  33. {
  34. return ep;
  35. }
  36. static int srjson_strcasecmp(const char *s1, const char *s2)
  37. {
  38. if (!s1)
  39. return (s1 == s2) ? 0 : 1;
  40. if (!s2)
  41. return 1;
  42. for (; tolower(*s1) == tolower(*s2); ++s1, ++s2)
  43. if (*s1 == 0)
  44. return 0;
  45. return tolower(*(const unsigned char *) s1) - tolower(*(const unsigned char *) s2);
  46. }
  47. srjson_doc_t *srjson_NewDoc(srjson_Hooks *hooks)
  48. {
  49. srjson_doc_t *d;
  50. if(hooks && hooks->malloc_fn)
  51. {
  52. d = (srjson_doc_t*)hooks->malloc_fn(sizeof(srjson_doc_t));
  53. } else {
  54. d = (srjson_doc_t*)malloc(sizeof(srjson_doc_t));
  55. }
  56. if(d)
  57. {
  58. memset(d, 0, sizeof(srjson_doc_t));
  59. d->malloc_fn = (hooks && hooks->malloc_fn) ? hooks->malloc_fn : malloc;
  60. d->free_fn = (hooks && hooks->free_fn) ? hooks->free_fn : free;
  61. }
  62. return d;
  63. }
  64. int srjson_InitDoc(srjson_doc_t *doc, srjson_Hooks *hooks)
  65. {
  66. if(!doc)
  67. return -1;
  68. memset(doc, 0, sizeof(srjson_doc_t));
  69. doc->malloc_fn = (hooks && hooks->malloc_fn) ? hooks->malloc_fn : malloc;
  70. doc->free_fn = (hooks && hooks->free_fn) ? hooks->free_fn : free;
  71. return 0;
  72. }
  73. void srjson_DeleteDoc(srjson_doc_t *doc)
  74. {
  75. void (*f) (void *);
  76. if(!doc)
  77. return;
  78. srjson_Delete(doc, doc->root);
  79. f = doc->free_fn;
  80. f(doc);
  81. }
  82. void srjson_DestroyDoc(srjson_doc_t *doc)
  83. {
  84. if(!doc)
  85. return;
  86. srjson_Delete(doc, doc->root);
  87. memset(doc, 0, sizeof(srjson_doc_t));
  88. }
  89. static char *srjson_strdup(srjson_doc_t *doc, const char *str)
  90. {
  91. size_t len;
  92. char *copy;
  93. len = strlen(str) + 1;
  94. if (!(copy = (char *) doc->malloc_fn(len)))
  95. return 0;
  96. memcpy(copy, str, len);
  97. return copy;
  98. }
  99. static char *srjson_strndupz(srjson_doc_t *doc, const char *str, int len)
  100. {
  101. char *copy;
  102. if (!(copy = (char *) doc->malloc_fn(len+1)))
  103. return 0;
  104. memcpy(copy, str, len);
  105. copy[len] = '\0';
  106. return copy;
  107. }
  108. /* Internal constructor. */
  109. static srjson_t *srjson_New_Item(srjson_doc_t *doc)
  110. {
  111. srjson_t *node = (srjson_t*)doc->malloc_fn(sizeof(srjson_t));
  112. if (node)
  113. memset(node, 0, sizeof(srjson_t));
  114. return node;
  115. }
  116. /* Delete a srjson structure. */
  117. void srjson_Delete(srjson_doc_t *doc, srjson_t *c)
  118. {
  119. srjson_t *next;
  120. while (c) {
  121. next = c->next;
  122. if (!(c->type & srjson_IsReference) && c->child)
  123. srjson_Delete(doc, c->child);
  124. if (!(c->type & srjson_IsReference) && c->valuestring)
  125. doc->free_fn(c->valuestring);
  126. if (c->string)
  127. doc->free_fn(c->string);
  128. doc->free_fn(c);
  129. c = next;
  130. }
  131. }
  132. /*
  133. * Parse the input text to generate a number, and populate the result into
  134. * item.
  135. */
  136. static const char *parse_number(srjson_doc_t *doc, srjson_t *item, const char *num)
  137. {
  138. double n = 0, sign = 1, scale = 0;
  139. int subscale = 0, signsubscale = 1;
  140. /* Could use sscanf for this? */
  141. if (*num == '-')
  142. sign = -1, num++; /* Has sign? */
  143. if (*num == '0')
  144. num++; /* is zero */
  145. if (*num >= '1' && *num <= '9')
  146. do
  147. n = (n * 10.0) + (*num++ - '0');
  148. while (*num >= '0' && *num <= '9'); /* Number? */
  149. if (*num == '.' && num[1] >= '0' && num[1] <= '9') {
  150. num++;
  151. do
  152. n = (n * 10.0) + (*num++ - '0'), scale--;
  153. while (*num >= '0' && *num <= '9');
  154. } /* Fractional part? */
  155. if (*num == 'e' || *num == 'E') { /* Exponent? */
  156. num++;
  157. if (*num == '+')
  158. num++;
  159. else if (*num == '-')
  160. signsubscale = -1, num++; /* With sign? */
  161. while (*num >= '0' && *num <= '9')
  162. subscale = (subscale * 10) + (*num++ - '0'); /* Number? */
  163. }
  164. n = sign * n * pow(10.0, (scale + subscale * signsubscale)); /* number = +/-
  165. * number.fraction *
  166. * 10^+/- exponent */
  167. item->valuedouble = n;
  168. item->valueint = (int) n;
  169. item->type = srjson_Number;
  170. return num;
  171. }
  172. /* Render the number nicely from the given item into a string. */
  173. static char *print_number(srjson_doc_t *doc, srjson_t *item)
  174. {
  175. char *str;
  176. double d = item->valuedouble;
  177. if (fabs(((double) item->valueint) - d) <= DBL_EPSILON && d <= INT_MAX && d >= INT_MIN) {
  178. str = (char *) doc->malloc_fn(21); /* 2^64+1 can be
  179. * represented in 21
  180. * chars. */
  181. if (str)
  182. sprintf(str, "%d", item->valueint);
  183. } else {
  184. str = (char *) doc->malloc_fn(64); /* This is a nice
  185. * tradeoff. */
  186. if (str) {
  187. if (fabs(floor(d) - d) <= DBL_EPSILON)
  188. sprintf(str, "%.0f", d);
  189. else if (fabs(d) < 1.0e-6 || fabs(d) > 1.0e9)
  190. sprintf(str, "%e", d);
  191. else
  192. sprintf(str, "%f", d);
  193. }
  194. }
  195. return str;
  196. }
  197. /* Parse the input text into an unescaped cstring, and populate item. */
  198. static const unsigned char firstByteMark[7] = {0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};
  199. static const char *parse_string(srjson_doc_t *doc, srjson_t *item, const char *str)
  200. {
  201. const char *ptr = str + 1;
  202. char *ptr2;
  203. char *out;
  204. int len = 0;
  205. unsigned uc, uc2;
  206. if (*str != '\"') {
  207. ep = str;
  208. return 0;
  209. } /* not a string! */
  210. while (*ptr != '\"' && *ptr && ++len)
  211. if (*ptr++ == '\\')
  212. ptr++; /* Skip escaped quotes. */
  213. out = (char *) doc->malloc_fn(len + 1); /* This is how long we need
  214. * for the string, roughly. */
  215. if (!out)
  216. return 0;
  217. ptr = str + 1;
  218. ptr2 = out;
  219. while (*ptr != '\"' && *ptr) {
  220. if (*ptr != '\\')
  221. *ptr2++ = *ptr++;
  222. else {
  223. ptr++;
  224. switch (*ptr) {
  225. case 'b':
  226. *ptr2++ = '\b';
  227. break;
  228. case 'f':
  229. *ptr2++ = '\f';
  230. break;
  231. case 'n':
  232. *ptr2++ = '\n';
  233. break;
  234. case 'r':
  235. *ptr2++ = '\r';
  236. break;
  237. case 't':
  238. *ptr2++ = '\t';
  239. break;
  240. case 'u': /* transcode utf16 to utf8. */
  241. sscanf(ptr + 1, "%4x", &uc);
  242. ptr += 4; /* get the unicode char. */
  243. if ((uc >= 0xDC00 && uc <= 0xDFFF) || uc == 0)
  244. break;
  245. //check for invalid
  246. if (uc >= 0xD800 && uc <= 0xDBFF)
  247. //UTF16 surrogate pairs.
  248. {
  249. if (ptr[1] != '\\' || ptr[2] != 'u')
  250. break;
  251. //missing second - half of surrogate.
  252. sscanf(ptr + 3, "%4x", &uc2);
  253. ptr += 6;
  254. if (uc2 < 0xDC00 || uc2 > 0xDFFF)
  255. break;
  256. //invalid second - half of surrogate.
  257. uc = 0x10000 | ((uc & 0x3FF) << 10) | (uc2 & 0x3FF);
  258. }
  259. len = 4;
  260. if (uc < 0x80)
  261. len = 1;
  262. else if (uc < 0x800)
  263. len = 2;
  264. else if (uc < 0x10000)
  265. len = 3;
  266. ptr2 += len;
  267. switch (len) {
  268. case 4:
  269. *--ptr2 = ((uc | 0x80) & 0xBF);
  270. uc >>= 6;
  271. case 3:
  272. *--ptr2 = ((uc | 0x80) & 0xBF);
  273. uc >>= 6;
  274. case 2:
  275. *--ptr2 = ((uc | 0x80) & 0xBF);
  276. uc >>= 6;
  277. case 1:
  278. *--ptr2 = (uc | firstByteMark[len]);
  279. }
  280. ptr2 += len;
  281. break;
  282. default:
  283. *ptr2++ = *ptr;
  284. break;
  285. }
  286. ptr++;
  287. }
  288. }
  289. *ptr2 = 0;
  290. if (*ptr == '\"')
  291. ptr++;
  292. item->valuestring = out;
  293. item->type = srjson_String;
  294. return ptr;
  295. }
  296. /* Render the cstring provided to an escaped version that can be printed. */
  297. static char *print_string_ptr(srjson_doc_t *doc, const char *str)
  298. {
  299. const char *ptr;
  300. char *ptr2, *out;
  301. int len = 0;
  302. unsigned char token;
  303. if (!str)
  304. return srjson_strdup(doc, "");
  305. ptr = str;
  306. while ((token = *ptr) && ++len) {
  307. if (strchr("\"\\\b\f\n\r\t", token))
  308. len++;
  309. else if (token < 32)
  310. len += 5;
  311. ptr++;
  312. }
  313. out = (char *) doc->malloc_fn(len + 3);
  314. if (!out)
  315. return 0;
  316. ptr2 = out;
  317. ptr = str;
  318. *ptr2++ = '\"';
  319. while (*ptr) {
  320. if ((unsigned char) *ptr > 31 && *ptr != '\"' && *ptr != '\\')
  321. *ptr2++ = *ptr++;
  322. else {
  323. *ptr2++ = '\\';
  324. switch (token = *ptr++) {
  325. case '\\':
  326. *ptr2++ = '\\';
  327. break;
  328. case '\"':
  329. *ptr2++ = '\"';
  330. break;
  331. case '\b':
  332. *ptr2++ = 'b';
  333. break;
  334. case '\f':
  335. *ptr2++ = 'f';
  336. break;
  337. case '\n':
  338. *ptr2++ = 'n';
  339. break;
  340. case '\r':
  341. *ptr2++ = 'r';
  342. break;
  343. case '\t':
  344. *ptr2++ = 't';
  345. break;
  346. default:
  347. sprintf(ptr2, "u%04x", token);
  348. ptr2 += 5;
  349. break; /* escape and print */
  350. }
  351. }
  352. }
  353. *ptr2++ = '\"';
  354. *ptr2++ = 0;
  355. return out;
  356. }
  357. /* Invote print_string_ptr (which is useful) on an item. */
  358. static char *print_string(srjson_doc_t *doc, srjson_t *item) {
  359. return print_string_ptr(doc, item->valuestring);
  360. }
  361. /* Predeclare these prototypes. */
  362. static const char *parse_value(srjson_doc_t *doc, srjson_t *item, const char *value);
  363. static char *print_value(srjson_doc_t *doc, srjson_t *item, int depth, int fmt);
  364. static const char *parse_array(srjson_doc_t *doc, srjson_t *item, const char *value);
  365. static char *print_array(srjson_doc_t *doc, srjson_t *item, int depth, int fmt);
  366. static const char *parse_object(srjson_doc_t *doc, srjson_t *item, const char *value);
  367. static char *print_object(srjson_doc_t *doc, srjson_t *item, int depth, int fmt);
  368. /* Utility to jump whitespace and cr/lf */
  369. static const char *skip(const char *in)
  370. {
  371. while (in && *in && (unsigned char) *in <= 32)
  372. in++;
  373. return in;
  374. }
  375. /* Parse an object - create a new root, and populate. */
  376. srjson_t *srjson_Parse(srjson_doc_t *doc, const char *value)
  377. {
  378. srjson_t *c = srjson_New_Item(doc);
  379. ep = 0;
  380. if (!c)
  381. return 0; /* memory fail */
  382. if (!parse_value(doc, c, skip(value))) {
  383. srjson_Delete(doc, c);
  384. return 0;
  385. }
  386. return c;
  387. }
  388. /* Render a srjson item/entity/structure to text. */
  389. char *srjson_Print(srjson_doc_t *doc, srjson_t *item) {
  390. return print_value(doc, item, 0, 1);
  391. }
  392. char *srjson_PrintUnformatted(srjson_doc_t *doc, srjson_t *item) {
  393. return print_value(doc, item, 0, 0);
  394. }
  395. /* Parser core - when encountering text, process appropriately. */
  396. static const char *parse_value(srjson_doc_t *doc, srjson_t *item, const char *value)
  397. {
  398. if (!value)
  399. return 0; /* Fail on null. */
  400. if (!strncmp(value, "null", 4)) {
  401. item->type = srjson_NULL;
  402. return value + 4;
  403. }
  404. if (!strncmp(value, "false", 5)) {
  405. item->type = srjson_False;
  406. return value + 5;
  407. }
  408. if (!strncmp(value, "true", 4)) {
  409. item->type = srjson_True;
  410. item->valueint = 1;
  411. return value + 4;
  412. }
  413. if (*value == '\"') {
  414. return parse_string(doc, item, value);
  415. }
  416. if (*value == '-' || (*value >= '0' && *value <= '9')) {
  417. return parse_number(doc, item, value);
  418. }
  419. if (*value == '[') {
  420. return parse_array(doc, item, value);
  421. }
  422. if (*value == '{') {
  423. return parse_object(doc, item, value);
  424. }
  425. ep = value;
  426. return 0; /* failure. */
  427. }
  428. /* Render a value to text. */
  429. static char *print_value(srjson_doc_t *doc, srjson_t *item, int depth, int fmt)
  430. {
  431. char *out = 0;
  432. if (!item)
  433. return 0;
  434. switch ((item->type) & 255) {
  435. case srjson_NULL:
  436. out = srjson_strdup(doc, "null");
  437. break;
  438. case srjson_False:
  439. out = srjson_strdup(doc, "false");
  440. break;
  441. case srjson_True:
  442. out = srjson_strdup(doc, "true");
  443. break;
  444. case srjson_Number:
  445. out = print_number(doc, item);
  446. break;
  447. case srjson_String:
  448. out = print_string(doc, item);
  449. break;
  450. case srjson_Array:
  451. out = print_array(doc, item, depth, fmt);
  452. break;
  453. case srjson_Object:
  454. out = print_object(doc, item, depth, fmt);
  455. break;
  456. }
  457. return out;
  458. }
  459. /* Build an array from input text. */
  460. static const char *parse_array(srjson_doc_t *doc, srjson_t *item, const char *value)
  461. {
  462. srjson_t *child;
  463. if (*value != '[') {
  464. ep = value;
  465. return 0;
  466. } /* not an array! */
  467. item->type = srjson_Array;
  468. value = skip(value + 1);
  469. if (*value == ']')
  470. return value + 1; /* empty array. */
  471. item->child = child = srjson_New_Item(doc);
  472. if (!item->child)
  473. return 0; /* memory fail */
  474. value = skip(parse_value(doc, child, skip(value))); /* skip any spacing, get
  475. * the value. */
  476. if (!value)
  477. return 0;
  478. while (*value == ',') {
  479. srjson_t *new_item;
  480. if (!(new_item = srjson_New_Item(doc)))
  481. return 0; /* memory fail */
  482. child->next = new_item;
  483. new_item->prev = child;
  484. child = new_item;
  485. value = skip(parse_value(doc, child, skip(value + 1)));
  486. if (!value)
  487. return 0; /* memory fail */
  488. }
  489. if (*value == ']')
  490. return value + 1; /* end of array */
  491. ep = value;
  492. return 0; /* malformed. */
  493. }
  494. /* Render an array to text */
  495. static char *print_array(srjson_doc_t *doc, srjson_t *item, int depth, int fmt)
  496. {
  497. char **entries;
  498. char *out = 0, *ptr, *ret;
  499. int len = 5;
  500. srjson_t *child = item->child;
  501. int numentries = 0, i = 0, fail = 0;
  502. /* How many entries in the array? */
  503. while (child)
  504. numentries++, child = child->next;
  505. /* Allocate an array to hold the values for each */
  506. entries = (char **) doc->malloc_fn(numentries * sizeof(char *));
  507. if (!entries)
  508. return 0;
  509. memset(entries, 0, numentries * sizeof(char *));
  510. /* Retrieve all the results: */
  511. child = item->child;
  512. while (child && !fail) {
  513. ret = print_value(doc, child, depth + 1, fmt);
  514. entries[i++] = ret;
  515. if (ret)
  516. len += strlen(ret) + 2 + (fmt ? 1 : 0);
  517. else
  518. fail = 1;
  519. child = child->next;
  520. }
  521. /* If we didn't fail, try to malloc the output string */
  522. if (!fail)
  523. out = (char *) doc->malloc_fn(len);
  524. /* If that fails, we fail. */
  525. if (!out)
  526. fail = 1;
  527. /* Handle failure. */
  528. if (fail) {
  529. for (i = 0; i < numentries; i++)
  530. if (entries[i])
  531. doc->free_fn(entries[i]);
  532. doc->free_fn(entries);
  533. return 0;
  534. }
  535. /* Compose the output array. */
  536. *out = '[';
  537. ptr = out + 1;
  538. *ptr = 0;
  539. for (i = 0; i < numentries; i++) {
  540. strcpy(ptr, entries[i]);
  541. ptr += strlen(entries[i]);
  542. if (i != numentries - 1) {
  543. *ptr++ = ',';
  544. if (fmt)
  545. *ptr++ = ' ';
  546. *ptr = 0;
  547. }
  548. doc->free_fn(entries[i]);
  549. }
  550. doc->free_fn(entries);
  551. *ptr++ = ']';
  552. *ptr++ = 0;
  553. return out;
  554. }
  555. /* Build an object from the text. */
  556. static const char *parse_object(srjson_doc_t *doc, srjson_t *item, const char *value)
  557. {
  558. srjson_t *child;
  559. if (*value != '{') {
  560. ep = value;
  561. return 0;
  562. } /* not an object! */
  563. item->type = srjson_Object;
  564. value = skip(value + 1);
  565. if (*value == '}')
  566. return value + 1; /* empty array. */
  567. item->child = child = srjson_New_Item(doc);
  568. if (!item->child)
  569. return 0;
  570. value = skip(parse_string(doc, child, skip(value)));
  571. if (!value)
  572. return 0;
  573. child->string = child->valuestring;
  574. child->valuestring = 0;
  575. if (*value != ':') {
  576. ep = value;
  577. return 0;
  578. } /* fail! */
  579. value = skip(parse_value(doc, child, skip(value + 1))); /* skip any spacing, get
  580. * the value. */
  581. if (!value)
  582. return 0;
  583. while (*value == ',') {
  584. srjson_t *new_item;
  585. if (!(new_item = srjson_New_Item(doc)))
  586. return 0; /* memory fail */
  587. child->next = new_item;
  588. new_item->prev = child;
  589. child = new_item;
  590. value = skip(parse_string(doc, child, skip(value + 1)));
  591. if (!value)
  592. return 0;
  593. child->string = child->valuestring;
  594. child->valuestring = 0;
  595. if (*value != ':') {
  596. ep = value;
  597. return 0;
  598. } /* fail! */
  599. value = skip(parse_value(doc, child, skip(value + 1))); /* skip any spacing, get
  600. * the value. */
  601. if (!value)
  602. return 0;
  603. }
  604. if (*value == '}')
  605. return value + 1; /* end of array */
  606. ep = value;
  607. return 0; /* malformed. */
  608. }
  609. /* Render an object to text. */
  610. static char *print_object(srjson_doc_t *doc, srjson_t *item, int depth, int fmt)
  611. {
  612. char **entries = 0, **names = 0;
  613. char *out = 0, *ptr, *ret, *str;
  614. int len = 7, i = 0, j;
  615. srjson_t *child = item->child;
  616. int numentries = 0, fail = 0;
  617. /* Count the number of entries. */
  618. while (child)
  619. numentries++, child = child->next;
  620. /* Allocate space for the names and the objects */
  621. entries = (char **) doc->malloc_fn(numentries * sizeof(char *));
  622. if (!entries)
  623. return 0;
  624. names = (char **)doc->malloc_fn(numentries * sizeof(char *));
  625. if (!names) {
  626. doc->free_fn(entries);
  627. return 0;
  628. }
  629. memset(entries, 0, sizeof(char *) * numentries);
  630. memset(names, 0, sizeof(char *) * numentries);
  631. /* Collect all the results into our arrays: */
  632. child = item->child;
  633. depth++;
  634. if (fmt)
  635. len += depth;
  636. while (child) {
  637. names[i] = str = print_string_ptr(doc, child->string);
  638. entries[i++] = ret = print_value(doc, child, depth, fmt);
  639. if (str && ret)
  640. len += strlen(ret) + strlen(str) + 2 + (fmt ? 2 + depth : 0);
  641. else
  642. fail = 1;
  643. child = child->next;
  644. }
  645. /* Try to allocate the output string */
  646. if (!fail)
  647. out = (char *) doc->malloc_fn(len);
  648. if (!out)
  649. fail = 1;
  650. /* Handle failure */
  651. if (fail) {
  652. for (i = 0; i < numentries; i++) {
  653. if (names[i])
  654. doc->free_fn(names[i]);
  655. if (entries[i])
  656. doc->free_fn(entries[i]);
  657. }
  658. doc->free_fn(names);
  659. doc->free_fn(entries);
  660. return 0;
  661. }
  662. /* Compose the output: */
  663. *out = '{';
  664. ptr = out + 1;
  665. if (fmt)
  666. *ptr++ = '\n';
  667. *ptr = 0;
  668. for (i = 0; i < numentries; i++) {
  669. if (fmt)
  670. for (j = 0; j < depth; j++)
  671. *ptr++ = '\t';
  672. strcpy(ptr, names[i]);
  673. ptr += strlen(names[i]);
  674. *ptr++ = ':';
  675. if (fmt)
  676. *ptr++ = '\t';
  677. strcpy(ptr, entries[i]);
  678. ptr += strlen(entries[i]);
  679. if (i != numentries - 1)
  680. *ptr++ = ',';
  681. if (fmt)
  682. *ptr++ = '\n';
  683. *ptr = 0;
  684. doc->free_fn(names[i]);
  685. doc->free_fn(entries[i]);
  686. }
  687. doc->free_fn(names);
  688. doc->free_fn(entries);
  689. if (fmt)
  690. for (i = 0; i < depth - 1; i++)
  691. *ptr++ = '\t';
  692. *ptr++ = '}';
  693. *ptr++ = 0;
  694. return out;
  695. }
  696. /* Get Array size/item / object item. */
  697. int srjson_GetArraySize(srjson_doc_t *doc, srjson_t *array)
  698. {
  699. srjson_t *c = array->child;
  700. int i = 0;
  701. while (c)
  702. i++, c = c->next;
  703. return i;
  704. }
  705. srjson_t *srjson_GetArrayItem(srjson_doc_t *doc, srjson_t *array, int item)
  706. {
  707. srjson_t *c = array->child;
  708. while (c && item > 0)
  709. item--, c = c->next;
  710. return c;
  711. }
  712. srjson_t *srjson_GetObjectItem(srjson_doc_t *doc, srjson_t *object, const char *string)
  713. {
  714. srjson_t *c = object->child;
  715. while (c && srjson_strcasecmp(c->string, string))
  716. c = c->next;
  717. return c;
  718. }
  719. /* Utility for array list handling. */
  720. static void suffix_object(srjson_t *prev, srjson_t *item)
  721. {
  722. prev->next = item;
  723. item->prev = prev;
  724. }
  725. /* Utility for handling references. */
  726. static srjson_t *create_reference(srjson_doc_t *doc, srjson_t *item) {
  727. srjson_t *ref = srjson_New_Item(doc);
  728. if (!ref)
  729. return 0;
  730. memcpy(ref, item, sizeof(srjson_t));
  731. ref->string = 0;
  732. ref->type |= srjson_IsReference;
  733. ref->next = ref->prev = 0;
  734. return ref;
  735. }
  736. /* Add item to array/object. */
  737. void srjson_AddItemToArray(srjson_doc_t *doc, srjson_t *array, srjson_t *item) {
  738. srjson_t *c = array->child;
  739. if (!item)
  740. return;
  741. if (!c) {
  742. array->child = item;
  743. } else {
  744. while (c && c->next)
  745. c = c->next;
  746. suffix_object(c, item);
  747. }
  748. }
  749. void srjson_AddItemToObject(srjson_doc_t *doc, srjson_t *object, const char *string, srjson_t *item) {
  750. if (!item)
  751. return;
  752. if (item->string)
  753. doc->free_fn(item->string);
  754. item->string = srjson_strdup(doc, string);
  755. srjson_AddItemToArray(doc, object, item);
  756. }
  757. void srjson_AddStrItemToObject(srjson_doc_t *doc, srjson_t *object, const char *string, int len, srjson_t *item) {
  758. if (!item)
  759. return;
  760. if (item->string)
  761. doc->free_fn(item->string);
  762. item->string = srjson_strndupz(doc, string, len);
  763. srjson_AddItemToArray(doc, object, item);
  764. }
  765. void srjson_AddItemReferenceToArray(srjson_doc_t *doc, srjson_t *array, srjson_t *item) {
  766. srjson_AddItemToArray(doc, array, create_reference(doc, item));
  767. }
  768. void srjson_AddItemReferenceToObject(srjson_doc_t *doc, srjson_t *object, const char *string, srjson_t *item) {
  769. srjson_AddItemToObject(doc, object, string, create_reference(doc, item));
  770. }
  771. srjson_t *srjson_UnlinkItemFromObj(srjson_doc_t *doc, srjson_t *obj, srjson_t *c)
  772. {
  773. if (!c)
  774. return 0;
  775. if (c->prev)
  776. c->prev->next = c->next;
  777. if (c->next)
  778. c->next->prev = c->prev;
  779. if (c == obj->child)
  780. obj->child = c->next;
  781. c->prev = c->next = 0;
  782. return c;
  783. }
  784. srjson_t *srjson_DetachItemFromArray(srjson_doc_t *doc, srjson_t *array, int which)
  785. {
  786. srjson_t *c = array->child;
  787. while (c && which > 0)
  788. c = c->next, which--;
  789. if (!c)
  790. return 0;
  791. if (c->prev)
  792. c->prev->next = c->next;
  793. if (c->next)
  794. c->next->prev = c->prev;
  795. if (c == array->child)
  796. array->child = c->next;
  797. c->prev = c->next = 0;
  798. return c;
  799. }
  800. void srjson_DeleteItemFromArray(srjson_doc_t *doc, srjson_t *array, int which) {
  801. srjson_Delete(doc, srjson_DetachItemFromArray(doc, array, which));
  802. }
  803. srjson_t *srjson_DetachItemFromObject(srjson_doc_t *doc, srjson_t *object, const char *string) {
  804. int i = 0;
  805. srjson_t *c = object->child;
  806. while (c && srjson_strcasecmp(c->string, string))
  807. i++, c = c->next;
  808. if (c)
  809. return srjson_DetachItemFromArray(doc, object, i);
  810. return 0;
  811. }
  812. void srjson_DeleteItemFromObject(srjson_doc_t *doc, srjson_t *object, const char *string) {
  813. srjson_Delete(doc, srjson_DetachItemFromObject(doc, object, string));
  814. }
  815. /* Replace array/object items with new ones. */
  816. void srjson_ReplaceItemInArray(srjson_doc_t *doc, srjson_t *array, int which, srjson_t *newitem)
  817. {
  818. srjson_t *c = array->child;
  819. while (c && which > 0)
  820. c = c->next, which--;
  821. if (!c)
  822. return;
  823. newitem->next = c->next;
  824. newitem->prev = c->prev;
  825. if (newitem->next)
  826. newitem->next->prev = newitem;
  827. if (c == array->child)
  828. array->child = newitem;
  829. else
  830. newitem->prev->next = newitem;
  831. c->next = c->prev = 0;
  832. srjson_Delete(doc, c);
  833. }
  834. void srjson_ReplaceItemInObject(srjson_doc_t *doc, srjson_t * object, const char *string, srjson_t *newitem) {
  835. int i = 0;
  836. srjson_t *c = object->child;
  837. while (c && srjson_strcasecmp(c->string, string))
  838. i++, c = c->next;
  839. if (c) {
  840. newitem->string = srjson_strdup(doc, string);
  841. srjson_ReplaceItemInArray(doc, object, i, newitem);
  842. }
  843. }
  844. /* Create basic types: */
  845. srjson_t *srjson_CreateNull(srjson_doc_t *doc) {
  846. srjson_t *item = srjson_New_Item(doc);
  847. if (item)
  848. item->type = srjson_NULL;
  849. return item;
  850. }
  851. srjson_t *srjson_CreateTrue(srjson_doc_t *doc) {
  852. srjson_t *item = srjson_New_Item(doc);
  853. if (item)
  854. item->type = srjson_True;
  855. return item;
  856. }
  857. srjson_t *srjson_CreateFalse(srjson_doc_t *doc) {
  858. srjson_t *item = srjson_New_Item(doc);
  859. if (item)
  860. item->type = srjson_False;
  861. return item;
  862. }
  863. srjson_t *srjson_CreateBool(srjson_doc_t *doc, int b) {
  864. srjson_t *item = srjson_New_Item(doc);
  865. if (item)
  866. item->type = b ? srjson_True : srjson_False;
  867. return item;
  868. }
  869. srjson_t *srjson_CreateNumber(srjson_doc_t *doc, double num) {
  870. srjson_t *item = srjson_New_Item(doc);
  871. if (item) {
  872. item->type = srjson_Number;
  873. item->valuedouble = num;
  874. item->valueint = (int) num;
  875. } return item;
  876. }
  877. srjson_t *srjson_CreateString(srjson_doc_t *doc, const char *string) {
  878. srjson_t *item = srjson_New_Item(doc);
  879. if (item) {
  880. item->type = srjson_String;
  881. item->valuestring = srjson_strdup(doc, string);
  882. } return item;
  883. }
  884. srjson_t *srjson_CreateStr(srjson_doc_t *doc, const char *string, int len) {
  885. srjson_t *item = srjson_New_Item(doc);
  886. if (item) {
  887. item->type = srjson_String;
  888. item->valuestring = srjson_strndupz(doc, string, len);
  889. } return item;
  890. }
  891. srjson_t *srjson_CreateArray(srjson_doc_t *doc) {
  892. srjson_t *item = srjson_New_Item(doc);
  893. if (item)
  894. item->type = srjson_Array;
  895. return item;
  896. }
  897. srjson_t *srjson_CreateObject(srjson_doc_t *doc) {
  898. srjson_t *item = srjson_New_Item(doc);
  899. if (item)
  900. item->type = srjson_Object;
  901. return item;
  902. }
  903. /* Create Arrays: */
  904. srjson_t *srjson_CreateIntArray(srjson_doc_t *doc, int *numbers, int count) {
  905. int i;
  906. srjson_t *n = 0, *p = 0, *a = srjson_CreateArray(doc);
  907. for (i = 0; a && i < count; i++) {
  908. n = srjson_CreateNumber(doc, numbers[i]);
  909. if (!i)
  910. a->child = n;
  911. else
  912. suffix_object(p, n);
  913. p = n;
  914. } return a;
  915. }
  916. srjson_t *srjson_CreateFloatArray(srjson_doc_t *doc, float *numbers, int count) {
  917. int i;
  918. srjson_t *n = 0, *p = 0, *a = srjson_CreateArray(doc);
  919. for (i = 0; a && i < count; i++) {
  920. n = srjson_CreateNumber(doc, numbers[i]);
  921. if (!i)
  922. a->child = n;
  923. else
  924. suffix_object(p, n);
  925. p = n;
  926. } return a;
  927. }
  928. srjson_t *srjson_CreateDoubleArray(srjson_doc_t *doc, double *numbers, int count) {
  929. int i;
  930. srjson_t *n = 0, *p = 0, *a = srjson_CreateArray(doc);
  931. for (i = 0; a && i < count; i++) {
  932. n = srjson_CreateNumber(doc, numbers[i]);
  933. if (!i)
  934. a->child = n;
  935. else
  936. suffix_object(p, n);
  937. p = n;
  938. } return a;
  939. }
  940. srjson_t *srjson_CreateStringArray(srjson_doc_t *doc, const char **strings, int count) {
  941. int i;
  942. srjson_t *n = 0, *p = 0, *a = srjson_CreateArray(doc);
  943. for (i = 0; a && i < count; i++) {
  944. n = srjson_CreateString(doc, strings[i]);
  945. if (!i)
  946. a->child = n;
  947. else
  948. suffix_object(p, n);
  949. p = n;
  950. } return a;
  951. }