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 10KB

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