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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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|UCL_CHARACTER_DENIED)) {
  96. if (len > 0) {
  97. func->ucl_emitter_append_len (c, len, func->ud);
  98. }
  99. switch (*p) {
  100. case '\n':
  101. func->ucl_emitter_append_len ("\\n", 2, func->ud);
  102. break;
  103. case '\r':
  104. func->ucl_emitter_append_len ("\\r", 2, func->ud);
  105. break;
  106. case '\b':
  107. func->ucl_emitter_append_len ("\\b", 2, func->ud);
  108. break;
  109. case '\t':
  110. func->ucl_emitter_append_len ("\\t", 2, func->ud);
  111. break;
  112. case '\f':
  113. func->ucl_emitter_append_len ("\\f", 2, func->ud);
  114. break;
  115. case '\\':
  116. func->ucl_emitter_append_len ("\\\\", 2, func->ud);
  117. break;
  118. case '"':
  119. func->ucl_emitter_append_len ("\\\"", 2, func->ud);
  120. break;
  121. default:
  122. /* Emit unicode unknown character */
  123. func->ucl_emitter_append_len ("\\uFFFD", 5, func->ud);
  124. break;
  125. }
  126. len = 0;
  127. c = ++p;
  128. }
  129. else {
  130. p ++;
  131. len ++;
  132. }
  133. size --;
  134. }
  135. if (len > 0) {
  136. func->ucl_emitter_append_len (c, len, func->ud);
  137. }
  138. func->ucl_emitter_append_character ('"', 1, func->ud);
  139. }
  140. void
  141. ucl_elt_string_write_squoted (const char *str, size_t size,
  142. struct ucl_emitter_context *ctx)
  143. {
  144. const char *p = str, *c = str;
  145. size_t len = 0;
  146. const struct ucl_emitter_functions *func = ctx->func;
  147. func->ucl_emitter_append_character ('\'', 1, func->ud);
  148. while (size) {
  149. if (*p == '\'') {
  150. if (len > 0) {
  151. func->ucl_emitter_append_len (c, len, func->ud);
  152. }
  153. len = 0;
  154. c = ++p;
  155. func->ucl_emitter_append_len ("\\\'", 2, func->ud);
  156. }
  157. else {
  158. p ++;
  159. len ++;
  160. }
  161. size --;
  162. }
  163. if (len > 0) {
  164. func->ucl_emitter_append_len (c, len, func->ud);
  165. }
  166. func->ucl_emitter_append_character ('\'', 1, func->ud);
  167. }
  168. void
  169. ucl_elt_string_write_multiline (const char *str, size_t size,
  170. struct ucl_emitter_context *ctx)
  171. {
  172. const struct ucl_emitter_functions *func = ctx->func;
  173. func->ucl_emitter_append_len ("<<EOD\n", sizeof ("<<EOD\n") - 1, func->ud);
  174. func->ucl_emitter_append_len (str, size, func->ud);
  175. func->ucl_emitter_append_len ("\nEOD", sizeof ("\nEOD") - 1, func->ud);
  176. }
  177. /*
  178. * Generic utstring output
  179. */
  180. static int
  181. ucl_utstring_append_character (unsigned char c, size_t len, void *ud)
  182. {
  183. UT_string *buf = ud;
  184. if (len == 1) {
  185. utstring_append_c (buf, c);
  186. }
  187. else {
  188. utstring_reserve (buf, len + 1);
  189. memset (&buf->d[buf->i], c, len);
  190. buf->i += len;
  191. buf->d[buf->i] = '\0';
  192. }
  193. return 0;
  194. }
  195. static int
  196. ucl_utstring_append_len (const unsigned char *str, size_t len, void *ud)
  197. {
  198. UT_string *buf = ud;
  199. utstring_append_len (buf, str, len);
  200. return 0;
  201. }
  202. static int
  203. ucl_utstring_append_int (int64_t val, void *ud)
  204. {
  205. UT_string *buf = ud;
  206. utstring_printf (buf, "%jd", (intmax_t)val);
  207. return 0;
  208. }
  209. static int
  210. ucl_utstring_append_double (double val, void *ud)
  211. {
  212. UT_string *buf = ud;
  213. const double delta = 0.0000001;
  214. if (val == (double)(int)val) {
  215. utstring_printf (buf, "%.1lf", val);
  216. }
  217. else if (fabs (val - (double)(int)val) < delta) {
  218. /* Write at maximum precision */
  219. utstring_printf (buf, "%.*lg", DBL_DIG, val);
  220. }
  221. else {
  222. utstring_printf (buf, "%lf", val);
  223. }
  224. return 0;
  225. }
  226. /*
  227. * Generic file output
  228. */
  229. static int
  230. ucl_file_append_character (unsigned char c, size_t len, void *ud)
  231. {
  232. FILE *fp = ud;
  233. while (len --) {
  234. fputc (c, fp);
  235. }
  236. return 0;
  237. }
  238. static int
  239. ucl_file_append_len (const unsigned char *str, size_t len, void *ud)
  240. {
  241. FILE *fp = ud;
  242. fwrite (str, len, 1, fp);
  243. return 0;
  244. }
  245. static int
  246. ucl_file_append_int (int64_t val, void *ud)
  247. {
  248. FILE *fp = ud;
  249. fprintf (fp, "%jd", (intmax_t)val);
  250. return 0;
  251. }
  252. static int
  253. ucl_file_append_double (double val, void *ud)
  254. {
  255. FILE *fp = ud;
  256. const double delta = 0.0000001;
  257. if (val == (double)(int)val) {
  258. fprintf (fp, "%.1lf", val);
  259. }
  260. else if (fabs (val - (double)(int)val) < delta) {
  261. /* Write at maximum precision */
  262. fprintf (fp, "%.*lg", DBL_DIG, val);
  263. }
  264. else {
  265. fprintf (fp, "%lf", val);
  266. }
  267. return 0;
  268. }
  269. /*
  270. * Generic file descriptor writing functions
  271. */
  272. static int
  273. ucl_fd_append_character (unsigned char c, size_t len, void *ud)
  274. {
  275. int fd = *(int *)ud;
  276. unsigned char *buf;
  277. if (len == 1) {
  278. return write (fd, &c, 1);
  279. }
  280. else {
  281. buf = malloc (len);
  282. if (buf == NULL) {
  283. /* Fallback */
  284. while (len --) {
  285. if (write (fd, &c, 1) == -1) {
  286. return -1;
  287. }
  288. }
  289. }
  290. else {
  291. memset (buf, c, len);
  292. if (write (fd, buf, len) == -1) {
  293. free(buf);
  294. return -1;
  295. }
  296. free (buf);
  297. }
  298. }
  299. return 0;
  300. }
  301. static int
  302. ucl_fd_append_len (const unsigned char *str, size_t len, void *ud)
  303. {
  304. int fd = *(int *)ud;
  305. return write (fd, str, len);
  306. }
  307. static int
  308. ucl_fd_append_int (int64_t val, void *ud)
  309. {
  310. int fd = *(int *)ud;
  311. char intbuf[64];
  312. snprintf (intbuf, sizeof (intbuf), "%jd", (intmax_t)val);
  313. return write (fd, intbuf, strlen (intbuf));
  314. }
  315. static int
  316. ucl_fd_append_double (double val, void *ud)
  317. {
  318. int fd = *(int *)ud;
  319. const double delta = 0.0000001;
  320. char nbuf[64];
  321. if (val == (double)(int)val) {
  322. snprintf (nbuf, sizeof (nbuf), "%.1lf", val);
  323. }
  324. else if (fabs (val - (double)(int)val) < delta) {
  325. /* Write at maximum precision */
  326. snprintf (nbuf, sizeof (nbuf), "%.*lg", DBL_DIG, val);
  327. }
  328. else {
  329. snprintf (nbuf, sizeof (nbuf), "%lf", val);
  330. }
  331. return write (fd, nbuf, strlen (nbuf));
  332. }
  333. struct ucl_emitter_functions*
  334. ucl_object_emit_memory_funcs (void **pmem)
  335. {
  336. struct ucl_emitter_functions *f;
  337. UT_string *s;
  338. f = calloc (1, sizeof (*f));
  339. if (f != NULL) {
  340. f->ucl_emitter_append_character = ucl_utstring_append_character;
  341. f->ucl_emitter_append_double = ucl_utstring_append_double;
  342. f->ucl_emitter_append_int = ucl_utstring_append_int;
  343. f->ucl_emitter_append_len = ucl_utstring_append_len;
  344. f->ucl_emitter_free_func = free;
  345. utstring_new (s);
  346. f->ud = s;
  347. *pmem = s->d;
  348. s->pd = pmem;
  349. }
  350. return f;
  351. }
  352. struct ucl_emitter_functions*
  353. ucl_object_emit_file_funcs (FILE *fp)
  354. {
  355. struct ucl_emitter_functions *f;
  356. f = calloc (1, sizeof (*f));
  357. if (f != NULL) {
  358. f->ucl_emitter_append_character = ucl_file_append_character;
  359. f->ucl_emitter_append_double = ucl_file_append_double;
  360. f->ucl_emitter_append_int = ucl_file_append_int;
  361. f->ucl_emitter_append_len = ucl_file_append_len;
  362. f->ucl_emitter_free_func = NULL;
  363. f->ud = fp;
  364. }
  365. return f;
  366. }
  367. struct ucl_emitter_functions*
  368. ucl_object_emit_fd_funcs (int fd)
  369. {
  370. struct ucl_emitter_functions *f;
  371. int *ip;
  372. f = calloc (1, sizeof (*f));
  373. if (f != NULL) {
  374. ip = malloc (sizeof (fd));
  375. if (ip == NULL) {
  376. free (f);
  377. return NULL;
  378. }
  379. memcpy (ip, &fd, sizeof (fd));
  380. f->ucl_emitter_append_character = ucl_fd_append_character;
  381. f->ucl_emitter_append_double = ucl_fd_append_double;
  382. f->ucl_emitter_append_int = ucl_fd_append_int;
  383. f->ucl_emitter_append_len = ucl_fd_append_len;
  384. f->ucl_emitter_free_func = free;
  385. f->ud = ip;
  386. }
  387. return f;
  388. }
  389. void
  390. ucl_object_emit_funcs_free (struct ucl_emitter_functions *f)
  391. {
  392. if (f != NULL) {
  393. if (f->ucl_emitter_free_func != NULL) {
  394. f->ucl_emitter_free_func (f->ud);
  395. }
  396. free (f);
  397. }
  398. }
  399. unsigned char *
  400. ucl_object_emit_single_json (const ucl_object_t *obj)
  401. {
  402. UT_string *buf = NULL;
  403. unsigned char *res = NULL;
  404. if (obj == NULL) {
  405. return NULL;
  406. }
  407. utstring_new (buf);
  408. if (buf != NULL) {
  409. switch (obj->type) {
  410. case UCL_OBJECT:
  411. ucl_utstring_append_len ("object", 6, buf);
  412. break;
  413. case UCL_ARRAY:
  414. ucl_utstring_append_len ("array", 5, buf);
  415. break;
  416. case UCL_INT:
  417. ucl_utstring_append_int (obj->value.iv, buf);
  418. break;
  419. case UCL_FLOAT:
  420. case UCL_TIME:
  421. ucl_utstring_append_double (obj->value.dv, buf);
  422. break;
  423. case UCL_NULL:
  424. ucl_utstring_append_len ("null", 4, buf);
  425. break;
  426. case UCL_BOOLEAN:
  427. if (obj->value.iv) {
  428. ucl_utstring_append_len ("true", 4, buf);
  429. }
  430. else {
  431. ucl_utstring_append_len ("false", 5, buf);
  432. }
  433. break;
  434. case UCL_STRING:
  435. ucl_utstring_append_len (obj->value.sv, obj->len, buf);
  436. break;
  437. case UCL_USERDATA:
  438. ucl_utstring_append_len ("userdata", 8, buf);
  439. break;
  440. }
  441. res = utstring_body (buf);
  442. free (buf);
  443. }
  444. return res;
  445. }
  446. #define LONG_STRING_LIMIT 80
  447. bool
  448. ucl_maybe_long_string (const ucl_object_t *obj)
  449. {
  450. if (obj->len > LONG_STRING_LIMIT || (obj->flags & UCL_OBJECT_MULTILINE)) {
  451. /* String is long enough, so search for newline characters in it */
  452. if (memchr (obj->value.sv, '\n', obj->len) != NULL) {
  453. return true;
  454. }
  455. }
  456. return false;
  457. }