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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 WIN32_LEAN_AND_MEAN
  35. # include <windows.h>
  36. # include <io.h>
  37. #endif /* defined(WIN32) */
  38. #if !defined(HAVE_OPEN) && defined(WIN32)
  39. # define open _open
  40. #endif
  41. #include "snprintf_compat.h"
  42. #include "debug.h"
  43. #include "printbuf.h"
  44. #include "json_inttypes.h"
  45. #include "json_object.h"
  46. #include "json_tokener.h"
  47. #include "json_util.h"
  48. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename);
  49. static char _last_err[256] = "";
  50. const char *json_util_get_last_err()
  51. {
  52. if (_last_err[0] == '\0')
  53. return NULL;
  54. return _last_err;
  55. }
  56. void _set_last_err(const char *err_fmt, ...)
  57. {
  58. va_list ap;
  59. va_start(ap, err_fmt);
  60. // Ignore (attempted) overruns from snprintf
  61. (void)vsnprintf(_last_err, sizeof(_last_err), err_fmt, ap);
  62. va_end(ap);
  63. }
  64. struct json_object* json_object_from_fd(int fd)
  65. {
  66. struct printbuf *pb;
  67. struct json_object *obj;
  68. char buf[JSON_FILE_BUF_SIZE];
  69. int ret;
  70. if(!(pb = printbuf_new())) {
  71. _set_last_err("json_object_from_file: printbuf_new failed\n");
  72. return NULL;
  73. }
  74. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  75. printbuf_memappend(pb, buf, ret);
  76. }
  77. if(ret < 0) {
  78. _set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd, strerror(errno));
  79. printbuf_free(pb);
  80. return NULL;
  81. }
  82. obj = json_tokener_parse(pb->buf);
  83. printbuf_free(pb);
  84. return obj;
  85. }
  86. struct json_object* json_object_from_file(const char *filename)
  87. {
  88. struct json_object *obj;
  89. int fd;
  90. if((fd = open(filename, O_RDONLY)) < 0) {
  91. _set_last_err("json_object_from_file: error opening file %s: %s\n",
  92. filename, strerror(errno));
  93. return NULL;
  94. }
  95. obj = json_object_from_fd(fd);
  96. close(fd);
  97. return obj;
  98. }
  99. /* extended "format and write to file" function */
  100. int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
  101. {
  102. int fd, ret;
  103. int saved_errno;
  104. if (!obj) {
  105. _set_last_err("json_object_to_file: object is null\n");
  106. return -1;
  107. }
  108. if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  109. _set_last_err("json_object_to_file: error opening file %s: %s\n",
  110. filename, strerror(errno));
  111. return -1;
  112. }
  113. ret = _json_object_to_fd(fd, obj, flags, filename);
  114. saved_errno = errno;
  115. close(fd);
  116. errno = saved_errno;
  117. return ret;
  118. }
  119. int json_object_to_fd(int fd, struct json_object *obj, int flags)
  120. {
  121. if (!obj) {
  122. _set_last_err("json_object_to_fd: object is null\n");
  123. return -1;
  124. }
  125. return _json_object_to_fd(fd, obj, flags, NULL);
  126. }
  127. static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename)
  128. {
  129. int ret;
  130. const char *json_str;
  131. unsigned int wpos, wsize;
  132. filename = filename ? filename : "(fd)";
  133. if (!(json_str = json_object_to_json_string_ext(obj,flags))) {
  134. return -1;
  135. }
  136. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  137. wpos = 0;
  138. while(wpos < wsize) {
  139. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  140. _set_last_err("json_object_to_file: error writing file %s: %s\n",
  141. filename, strerror(errno));
  142. return -1;
  143. }
  144. /* because of the above check for ret < 0, we can safely cast and add */
  145. wpos += (unsigned int)ret;
  146. }
  147. return 0;
  148. }
  149. // backwards compatible "format and write to file" function
  150. int json_object_to_file(const char *filename, struct json_object *obj)
  151. {
  152. return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
  153. }
  154. int json_parse_double(const char *buf, double *retval)
  155. {
  156. char *end;
  157. *retval = strtod(buf, &end);
  158. return end == buf ? 1 : 0;
  159. }
  160. int json_parse_int64(const char *buf, int64_t *retval)
  161. {
  162. char *end = NULL;
  163. int64_t val;
  164. errno = 0;
  165. val = strtoll(buf, &end, 10);
  166. if (end != buf)
  167. *retval = val;
  168. return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
  169. }
  170. #ifndef HAVE_REALLOC
  171. void* rpl_realloc(void* p, size_t n)
  172. {
  173. if (n == 0)
  174. n = 1;
  175. if (p == 0)
  176. return malloc(n);
  177. return realloc(p, n);
  178. }
  179. #endif
  180. #define NELEM(a) (sizeof(a) / sizeof(a[0]))
  181. static const char* json_type_name[] = {
  182. /* If you change this, be sure to update the enum json_type definition too */
  183. "null",
  184. "boolean",
  185. "double",
  186. "int",
  187. "object",
  188. "array",
  189. "string",
  190. };
  191. const char *json_type_to_name(enum json_type o_type)
  192. {
  193. int o_type_int = (int)o_type;
  194. if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
  195. {
  196. _set_last_err("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
  197. return NULL;
  198. }
  199. return json_type_name[o_type];
  200. }