You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ucl_emitter_utils.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /* Copyright (c) 2014, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "ucl.h"
  27. #include "ucl_internal.h"
  28. #include "ucl_chartable.h"
  29. #ifdef HAVE_FLOAT_H
  30. #include <float.h>
  31. #endif
  32. #ifdef HAVE_MATH_H
  33. #include <math.h>
  34. #endif
  35. extern const struct ucl_emitter_operations ucl_standartd_emitter_ops[];
  36. static const struct ucl_emitter_context ucl_standard_emitters[] = {
  37. [UCL_EMIT_JSON] = {
  38. .name = "json",
  39. .id = UCL_EMIT_JSON,
  40. .func = NULL,
  41. .ops = &ucl_standartd_emitter_ops[UCL_EMIT_JSON]
  42. },
  43. [UCL_EMIT_JSON_COMPACT] = {
  44. .name = "json_compact",
  45. .id = UCL_EMIT_JSON_COMPACT,
  46. .func = NULL,
  47. .ops = &ucl_standartd_emitter_ops[UCL_EMIT_JSON_COMPACT]
  48. },
  49. [UCL_EMIT_CONFIG] = {
  50. .name = "config",
  51. .id = UCL_EMIT_CONFIG,
  52. .func = NULL,
  53. .ops = &ucl_standartd_emitter_ops[UCL_EMIT_CONFIG]
  54. },
  55. [UCL_EMIT_YAML] = {
  56. .name = "yaml",
  57. .id = UCL_EMIT_YAML,
  58. .func = NULL,
  59. .ops = &ucl_standartd_emitter_ops[UCL_EMIT_YAML]
  60. },
  61. [UCL_EMIT_MSGPACK] = {
  62. .name = "msgpack",
  63. .id = UCL_EMIT_MSGPACK,
  64. .func = NULL,
  65. .ops = &ucl_standartd_emitter_ops[UCL_EMIT_MSGPACK]
  66. }
  67. };
  68. /**
  69. * Get standard emitter context for a specified emit_type
  70. * @param emit_type type of emitter
  71. * @return context or NULL if input is invalid
  72. */
  73. const struct ucl_emitter_context *
  74. ucl_emit_get_standard_context (enum ucl_emitter emit_type)
  75. {
  76. if (emit_type >= UCL_EMIT_JSON && emit_type < UCL_EMIT_MAX) {
  77. return &ucl_standard_emitters[emit_type];
  78. }
  79. return NULL;
  80. }
  81. /**
  82. * Serialise string
  83. * @param str string to emit
  84. * @param buf target buffer
  85. */
  86. void
  87. ucl_elt_string_write_json (const char *str, size_t size,
  88. struct ucl_emitter_context *ctx)
  89. {
  90. const char *p = str, *c = str;
  91. size_t len = 0;
  92. const struct ucl_emitter_functions *func = ctx->func;
  93. func->ucl_emitter_append_character ('"', 1, func->ud);
  94. while (size) {
  95. if (ucl_test_character (*p, (UCL_CHARACTER_JSON_UNSAFE|
  96. UCL_CHARACTER_DENIED|
  97. UCL_CHARACTER_WHITESPACE_UNSAFE))) {
  98. if (len > 0) {
  99. func->ucl_emitter_append_len (c, len, func->ud);
  100. }
  101. switch (*p) {
  102. case '\n':
  103. func->ucl_emitter_append_len ("\\n", 2, func->ud);
  104. break;
  105. case '\r':
  106. func->ucl_emitter_append_len ("\\r", 2, func->ud);
  107. break;
  108. case '\b':
  109. func->ucl_emitter_append_len ("\\b", 2, func->ud);
  110. break;
  111. case '\t':
  112. func->ucl_emitter_append_len ("\\t", 2, func->ud);
  113. break;
  114. case '\f':
  115. func->ucl_emitter_append_len ("\\f", 2, func->ud);
  116. break;
  117. case '\v':
  118. func->ucl_emitter_append_len ("\\u000B", 6, func->ud);
  119. break;
  120. case '\\':
  121. func->ucl_emitter_append_len ("\\\\", 2, func->ud);
  122. break;
  123. case ' ':
  124. func->ucl_emitter_append_character (' ', 1, func->ud);
  125. break;
  126. case '"':
  127. func->ucl_emitter_append_len ("\\\"", 2, func->ud);
  128. break;
  129. default:
  130. /* Emit unicode unknown character */
  131. func->ucl_emitter_append_len ("\\uFFFD", 6, func->ud);
  132. break;
  133. }
  134. len = 0;
  135. c = ++p;
  136. }
  137. else {
  138. p ++;
  139. len ++;
  140. }
  141. size --;
  142. }
  143. if (len > 0) {
  144. func->ucl_emitter_append_len (c, len, func->ud);
  145. }
  146. func->ucl_emitter_append_character ('"', 1, func->ud);
  147. }
  148. void
  149. ucl_elt_string_write_squoted (const char *str, size_t size,
  150. struct ucl_emitter_context *ctx)
  151. {
  152. const char *p = str, *c = str;
  153. size_t len = 0;
  154. const struct ucl_emitter_functions *func = ctx->func;
  155. func->ucl_emitter_append_character ('\'', 1, func->ud);
  156. while (size) {
  157. if (*p == '\'') {
  158. if (len > 0) {
  159. func->ucl_emitter_append_len (c, len, func->ud);
  160. }
  161. len = 0;
  162. c = ++p;
  163. func->ucl_emitter_append_len ("\\\'", 2, func->ud);
  164. }
  165. else {
  166. p ++;
  167. len ++;
  168. }
  169. size --;
  170. }
  171. if (len > 0) {
  172. func->ucl_emitter_append_len (c, len, func->ud);
  173. }
  174. func->ucl_emitter_append_character ('\'', 1, func->ud);
  175. }
  176. void
  177. ucl_elt_string_write_multiline (const char *str, size_t size,
  178. struct ucl_emitter_context *ctx)
  179. {
  180. const struct ucl_emitter_functions *func = ctx->func;
  181. func->ucl_emitter_append_len ("<<EOD\n", sizeof ("<<EOD\n") - 1, func->ud);
  182. func->ucl_emitter_append_len (str, size, func->ud);
  183. func->ucl_emitter_append_len ("\nEOD", sizeof ("\nEOD") - 1, func->ud);
  184. }
  185. /*
  186. * Generic utstring output
  187. */
  188. static int
  189. ucl_utstring_append_character (unsigned char c, size_t len, void *ud)
  190. {
  191. UT_string *buf = ud;
  192. if (len == 1) {
  193. utstring_append_c (buf, c);
  194. }
  195. else {
  196. utstring_reserve (buf, len + 1);
  197. memset (&buf->d[buf->i], c, len);
  198. buf->i += len;
  199. buf->d[buf->i] = '\0';
  200. }
  201. return 0;
  202. }
  203. static int
  204. ucl_utstring_append_len (const unsigned char *str, size_t len, void *ud)
  205. {
  206. UT_string *buf = ud;
  207. utstring_append_len (buf, str, len);
  208. return 0;
  209. }
  210. static int
  211. ucl_utstring_append_int (int64_t val, void *ud)
  212. {
  213. UT_string *buf = ud;
  214. utstring_printf (buf, "%jd", (intmax_t)val);
  215. return 0;
  216. }
  217. static int
  218. ucl_utstring_append_double (double val, void *ud)
  219. {
  220. UT_string *buf = ud;
  221. const double delta = 0.0000001;
  222. if (val == (double)(int)val) {
  223. utstring_printf (buf, "%.1lf", val);
  224. }
  225. else if (fabs (val - (double)(int)val) < delta) {
  226. /* Write at maximum precision */
  227. utstring_printf (buf, "%.*lg", DBL_DIG, val);
  228. }
  229. else {
  230. utstring_printf (buf, "%lf", val);
  231. }
  232. return 0;
  233. }
  234. /*
  235. * Generic file output
  236. */
  237. static int
  238. ucl_file_append_character (unsigned char c, size_t len, void *ud)
  239. {
  240. FILE *fp = ud;
  241. while (len --) {
  242. fputc (c, fp);
  243. }
  244. return 0;
  245. }
  246. static int
  247. ucl_file_append_len (const unsigned char *str, size_t len, void *ud)
  248. {
  249. FILE *fp = ud;
  250. fwrite (str, len, 1, fp);
  251. return 0;
  252. }
  253. static int
  254. ucl_file_append_int (int64_t val, void *ud)
  255. {
  256. FILE *fp = ud;
  257. fprintf (fp, "%jd", (intmax_t)val);
  258. return 0;
  259. }
  260. static int
  261. ucl_file_append_double (double val, void *ud)
  262. {
  263. FILE *fp = ud;
  264. const double delta = 0.0000001;
  265. if (val == (double)(int)val) {
  266. fprintf (fp, "%.1lf", val);
  267. }
  268. else if (fabs (val - (double)(int)val) < delta) {
  269. /* Write at maximum precision */
  270. fprintf (fp, "%.*lg", DBL_DIG, val);
  271. }
  272. else {
  273. fprintf (fp, "%lf", val);
  274. }
  275. return 0;
  276. }
  277. /*
  278. * Generic file descriptor writing functions
  279. */
  280. static int
  281. ucl_fd_append_character (unsigned char c, size_t len, void *ud)
  282. {
  283. int fd = *(int *)ud;
  284. unsigned char *buf;
  285. if (len == 1) {
  286. return write (fd, &c, 1);
  287. }
  288. else {
  289. buf = malloc (len);
  290. if (buf == NULL) {
  291. /* Fallback */
  292. while (len --) {
  293. if (write (fd, &c, 1) == -1) {
  294. return -1;
  295. }
  296. }
  297. }
  298. else {
  299. memset (buf, c, len);
  300. if (write (fd, buf, len) == -1) {
  301. free(buf);
  302. return -1;
  303. }
  304. free (buf);
  305. }
  306. }
  307. return 0;
  308. }
  309. static int
  310. ucl_fd_append_len (const unsigned char *str, size_t len, void *ud)
  311. {
  312. int fd = *(int *)ud;
  313. return write (fd, str, len);
  314. }
  315. static int
  316. ucl_fd_append_int (int64_t val, void *ud)
  317. {
  318. int fd = *(int *)ud;
  319. char intbuf[64];
  320. snprintf (intbuf, sizeof (intbuf), "%jd", (intmax_t)val);
  321. return write (fd, intbuf, strlen (intbuf));
  322. }
  323. static int
  324. ucl_fd_append_double (double val, void *ud)
  325. {
  326. int fd = *(int *)ud;
  327. const double delta = 0.0000001;
  328. char nbuf[64];
  329. if (val == (double)(int)val) {
  330. snprintf (nbuf, sizeof (nbuf), "%.1lf", val);
  331. }
  332. else if (fabs (val - (double)(int)val) < delta) {
  333. /* Write at maximum precision */
  334. snprintf (nbuf, sizeof (nbuf), "%.*lg", DBL_DIG, val);
  335. }
  336. else {
  337. snprintf (nbuf, sizeof (nbuf), "%lf", val);
  338. }
  339. return write (fd, nbuf, strlen (nbuf));
  340. }
  341. struct ucl_emitter_functions*
  342. ucl_object_emit_memory_funcs (void **pmem)
  343. {
  344. struct ucl_emitter_functions *f;
  345. UT_string *s;
  346. f = calloc (1, sizeof (*f));
  347. if (f != NULL) {
  348. f->ucl_emitter_append_character = ucl_utstring_append_character;
  349. f->ucl_emitter_append_double = ucl_utstring_append_double;
  350. f->ucl_emitter_append_int = ucl_utstring_append_int;
  351. f->ucl_emitter_append_len = ucl_utstring_append_len;
  352. f->ucl_emitter_free_func = free;
  353. utstring_new (s);
  354. f->ud = s;
  355. *pmem = s->d;
  356. s->pd = pmem;
  357. }
  358. return f;
  359. }
  360. struct ucl_emitter_functions*
  361. ucl_object_emit_file_funcs (FILE *fp)
  362. {
  363. struct ucl_emitter_functions *f;
  364. f = calloc (1, sizeof (*f));
  365. if (f != NULL) {
  366. f->ucl_emitter_append_character = ucl_file_append_character;
  367. f->ucl_emitter_append_double = ucl_file_append_double;
  368. f->ucl_emitter_append_int = ucl_file_append_int;
  369. f->ucl_emitter_append_len = ucl_file_append_len;
  370. f->ucl_emitter_free_func = NULL;
  371. f->ud = fp;
  372. }
  373. return f;
  374. }
  375. struct ucl_emitter_functions*
  376. ucl_object_emit_fd_funcs (int fd)
  377. {
  378. struct ucl_emitter_functions *f;
  379. int *ip;
  380. f = calloc (1, sizeof (*f));
  381. if (f != NULL) {
  382. ip = malloc (sizeof (fd));
  383. if (ip == NULL) {
  384. free (f);
  385. return NULL;
  386. }
  387. memcpy (ip, &fd, sizeof (fd));
  388. f->ucl_emitter_append_character = ucl_fd_append_character;
  389. f->ucl_emitter_append_double = ucl_fd_append_double;
  390. f->ucl_emitter_append_int = ucl_fd_append_int;
  391. f->ucl_emitter_append_len = ucl_fd_append_len;
  392. f->ucl_emitter_free_func = free;
  393. f->ud = ip;
  394. }
  395. return f;
  396. }
  397. void
  398. ucl_object_emit_funcs_free (struct ucl_emitter_functions *f)
  399. {
  400. if (f != NULL) {
  401. if (f->ucl_emitter_free_func != NULL) {
  402. f->ucl_emitter_free_func (f->ud);
  403. }
  404. free (f);
  405. }
  406. }
  407. unsigned char *
  408. ucl_object_emit_single_json (const ucl_object_t *obj)
  409. {
  410. UT_string *buf = NULL;
  411. unsigned char *res = NULL;
  412. if (obj == NULL) {
  413. return NULL;
  414. }
  415. utstring_new (buf);
  416. if (buf != NULL) {
  417. switch (obj->type) {
  418. case UCL_OBJECT:
  419. ucl_utstring_append_len ("object", 6, buf);
  420. break;
  421. case UCL_ARRAY:
  422. ucl_utstring_append_len ("array", 5, buf);
  423. break;
  424. case UCL_INT:
  425. ucl_utstring_append_int (obj->value.iv, buf);
  426. break;
  427. case UCL_FLOAT:
  428. case UCL_TIME:
  429. ucl_utstring_append_double (obj->value.dv, buf);
  430. break;
  431. case UCL_NULL:
  432. ucl_utstring_append_len ("null", 4, buf);
  433. break;
  434. case UCL_BOOLEAN:
  435. if (obj->value.iv) {
  436. ucl_utstring_append_len ("true", 4, buf);
  437. }
  438. else {
  439. ucl_utstring_append_len ("false", 5, buf);
  440. }
  441. break;
  442. case UCL_STRING:
  443. ucl_utstring_append_len (obj->value.sv, obj->len, buf);
  444. break;
  445. case UCL_USERDATA:
  446. ucl_utstring_append_len ("userdata", 8, buf);
  447. break;
  448. }
  449. res = utstring_body (buf);
  450. free (buf);
  451. }
  452. return res;
  453. }
  454. #define LONG_STRING_LIMIT 80
  455. bool
  456. ucl_maybe_long_string (const ucl_object_t *obj)
  457. {
  458. if (obj->len > LONG_STRING_LIMIT || (obj->flags & UCL_OBJECT_MULTILINE)) {
  459. /* String is long enough, so search for newline characters in it */
  460. if (memchr (obj->value.sv, '\n', obj->len) != NULL) {
  461. return true;
  462. }
  463. }
  464. return false;
  465. }