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

json_util.c 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * $Id: json_util.c,v 1.4 2006/01/30 23:07:57 mclark Exp $
  3. *
  4. * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the MIT license. See COPYING for details.
  9. *
  10. */
  11. #include "config.h"
  12. #undef realloc
  13. #include "strerror_override.h"
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stddef.h>
  18. #include <limits.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #ifdef HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif /* HAVE_SYS_TYPES_H */
  24. #ifdef HAVE_SYS_STAT_H
  25. #include <sys/stat.h>
  26. #endif /* HAVE_SYS_STAT_H */
  27. #ifdef HAVE_FCNTL_H
  28. #include <fcntl.h>
  29. #endif /* HAVE_FCNTL_H */
  30. #ifdef HAVE_UNISTD_H
  31. # include <unistd.h>
  32. #endif /* HAVE_UNISTD_H */
  33. #ifdef WIN32
  34. # define strtoll _strtoi64
  35. # define WIN32_LEAN_AND_MEAN
  36. # include <windows.h>
  37. # include <io.h>
  38. #endif /* defined(WIN32) */
  39. #if !defined(HAVE_OPEN) && defined(WIN32)
  40. # define open _open
  41. #endif
  42. #include "snprintf_compat.h"
  43. #include "debug.h"
  44. #include "printbuf.h"
  45. #include "json_inttypes.h"
  46. #include "json_object.h"
  47. #include "json_tokener.h"
  48. #include "json_util.h"
  49. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename);
  50. static char _last_err[256] = "";
  51. const char *json_util_get_last_err()
  52. {
  53. if (_last_err[0] == '\0')
  54. return NULL;
  55. return _last_err;
  56. }
  57. void _set_last_err(const char *err_fmt, ...)
  58. {
  59. va_list ap;
  60. va_start(ap, err_fmt);
  61. // Ignore (attempted) overruns from snprintf
  62. (void)vsnprintf(_last_err, sizeof(_last_err), err_fmt, ap);
  63. va_end(ap);
  64. }
  65. struct json_object* json_object_from_fd(int fd)
  66. {
  67. struct printbuf *pb;
  68. struct json_object *obj;
  69. char buf[JSON_FILE_BUF_SIZE];
  70. int ret;
  71. if(!(pb = printbuf_new())) {
  72. _set_last_err("json_object_from_file: printbuf_new failed\n");
  73. return NULL;
  74. }
  75. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  76. printbuf_memappend(pb, buf, ret);
  77. }
  78. if(ret < 0) {
  79. _set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd, strerror(errno));
  80. printbuf_free(pb);
  81. return NULL;
  82. }
  83. obj = json_tokener_parse(pb->buf);
  84. printbuf_free(pb);
  85. return obj;
  86. }
  87. struct json_object* json_object_from_file(const char *filename)
  88. {
  89. struct json_object *obj;
  90. int fd;
  91. if((fd = open(filename, O_RDONLY)) < 0) {
  92. _set_last_err("json_object_from_file: error opening file %s: %s\n",
  93. filename, strerror(errno));
  94. return NULL;
  95. }
  96. obj = json_object_from_fd(fd);
  97. close(fd);
  98. return obj;
  99. }
  100. /* extended "format and write to file" function */
  101. int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
  102. {
  103. int fd, ret;
  104. int saved_errno;
  105. if (!obj) {
  106. _set_last_err("json_object_to_file: object is null\n");
  107. return -1;
  108. }
  109. if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  110. _set_last_err("json_object_to_file: error opening file %s: %s\n",
  111. filename, strerror(errno));
  112. return -1;
  113. }
  114. ret = _json_object_to_fd(fd, obj, flags, filename);
  115. saved_errno = errno;
  116. close(fd);
  117. errno = saved_errno;
  118. return ret;
  119. }
  120. int json_object_to_fd(int fd, struct json_object *obj, int flags)
  121. {
  122. if (!obj) {
  123. _set_last_err("json_object_to_fd: object is null\n");
  124. return -1;
  125. }
  126. return _json_object_to_fd(fd, obj, flags, NULL);
  127. }
  128. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
  129. {
  130. int ret;
  131. const char *json_str;
  132. unsigned int wpos, wsize;
  133. filename = filename ? filename : "(fd)";
  134. if (!(json_str = json_object_to_json_string_ext(obj,flags))) {
  135. return -1;
  136. }
  137. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  138. wpos = 0;
  139. while(wpos < wsize) {
  140. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  141. _set_last_err("json_object_to_file: error writing file %s: %s\n",
  142. filename, strerror(errno));
  143. return -1;
  144. }
  145. /* because of the above check for ret < 0, we can safely cast and add */
  146. wpos += (unsigned int)ret;
  147. }
  148. return 0;
  149. }
  150. // backwards compatible "format and write to file" function
  151. int json_object_to_file(const char *filename, struct json_object *obj)
  152. {
  153. return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  154. }
  155. int json_parse_double(const char *buf, double *retval)
  156. {
  157. char *end;
  158. *retval = strtod(buf, &end);
  159. return end == buf ? 1 : 0;
  160. }
  161. int json_parse_int64(const char *buf, int64_t *retval)
  162. {
  163. char *end = NULL;
  164. int64_t val;
  165. errno = 0;
  166. val = strtoll(buf, &end, 10);
  167. if (end != buf)
  168. *retval = val;
  169. return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
  170. }
  171. #ifndef HAVE_REALLOC
  172. void* rpl_realloc(void* p, size_t n)
  173. {
  174. if (n == 0)
  175. n = 1;
  176. if (p == 0)
  177. return malloc(n);
  178. return realloc(p, n);
  179. }
  180. #endif
  181. #define NELEM(a) (sizeof(a) / sizeof(a[0]))
  182. static const char* json_type_name[] = {
  183. /* If you change this, be sure to update the enum json_type definition too */
  184. "null",
  185. "boolean",
  186. "double",
  187. "int",
  188. "object",
  189. "array",
  190. "string",
  191. };
  192. const char *json_type_to_name(enum json_type o_type)
  193. {
  194. int o_type_int = (int)o_type;
  195. if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
  196. {
  197. _set_last_err("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
  198. return NULL;
  199. }
  200. return json_type_name[o_type];
  201. }