2 * $Id: json_util.c,v 1.4 2006/01/30 23:07:57 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
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.
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif /* HAVE_SYS_TYPES_H */
27 #ifdef HAVE_SYS_STAT_H
29 #endif /* HAVE_SYS_STAT_H */
33 #endif /* HAVE_FCNTL_H */
37 #endif /* HAVE_UNISTD_H */
41 #endif /* HAVE_LOCALE_H */
44 # define WIN32_LEAN_AND_MEAN
47 #endif /* defined(WIN32) */
49 #if !defined(HAVE_OPEN) && defined(WIN32)
53 #if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
54 /* MSC has the version as _snprintf */
55 # define snprintf _snprintf
56 #elif !defined(HAVE_SNPRINTF)
57 # error You do not have snprintf on your system.
58 #endif /* HAVE_SNPRINTF */
63 #include "json_inttypes.h"
64 #include "json_object.h"
65 #include "json_tokener.h"
66 #include "json_util.h"
68 struct json_object* json_object_from_file(const char *filename)
71 struct json_object *obj;
72 char buf[JSON_FILE_BUF_SIZE];
75 if((fd = open(filename, O_RDONLY)) < 0) {
76 MC_ERROR("json_object_from_file: error reading file %s: %s\n",
77 filename, strerror(errno));
80 if(!(pb = printbuf_new())) {
82 MC_ERROR("json_object_from_file: printbuf_new failed\n");
85 while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
86 printbuf_memappend(pb, buf, ret);
90 MC_ABORT("json_object_from_file: error reading file %s: %s\n",
91 filename, strerror(errno));
95 obj = json_tokener_parse(pb->buf);
100 /* extended "format and write to file" function */
102 int json_object_to_file_ext(char *filename, struct json_object *obj, int flags)
104 const char *json_str;
106 unsigned int wpos, wsize;
109 MC_ERROR("json_object_to_file: object is null\n");
113 if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
114 MC_ERROR("json_object_to_file: error opening file %s: %s\n",
115 filename, strerror(errno));
119 if(!(json_str = json_object_to_json_string_ext(obj,flags))) {
124 wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
126 while(wpos < wsize) {
127 if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
129 MC_ERROR("json_object_to_file: error writing file %s: %s\n",
130 filename, strerror(errno));
134 /* because of the above check for ret < 0, we can safely cast and add */
135 wpos += (unsigned int)ret;
142 // backwards compatible "format and write to file" function
144 int json_object_to_file(char *filename, struct json_object *obj)
146 return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
149 int json_parse_double(const char *buf, double *retval)
152 #ifdef HAVE_SETLOCALE
153 char *old=NULL, *tmp;
155 tmp = setlocale(LC_NUMERIC, NULL);
156 if (tmp) old = strdup(tmp);
157 setlocale(LC_NUMERIC, "C");
160 ret = sscanf(buf, "%lf", retval);
162 #ifdef HAVE_SETLOCALE
163 setlocale(LC_NUMERIC, old);
167 return (ret==1 ? 0 : 1);
170 int json_parse_int64(const char *buf, int64_t *retval)
173 const char *buf_skip_space;
176 errno = 0; // sscanf won't always set errno, so initialize
177 if (sscanf(buf, "%" SCNd64, &num64) != 1)
179 MC_DEBUG("Failed to parse, sscanf != 1\n");
183 buf_skip_space = buf;
185 // Skip leading spaces
186 while (isspace((int)*buf_skip_space) && *buf_skip_space)
188 if (*buf_skip_space == '-')
193 // Skip leading zeros, but keep at least one digit
194 while (buf_skip_space[0] == '0' && buf_skip_space[1] != '\0')
196 if (buf_skip_space[0] == '0' && buf_skip_space[1] == '\0')
197 orig_has_neg = 0; // "-0" is the same as just plain "0"
199 if (_errno != ERANGE)
202 char *buf_cmp_start = buf_cmp;
203 int recheck_has_neg = 0;
205 snprintf(buf_cmp_start, sizeof(buf_cmp), "%" PRId64, num64);
206 if (*buf_cmp_start == '-')
211 // No need to skip leading spaces or zeros here.
213 buf_cmp_len = strlen(buf_cmp_start);
215 * If the sign is different, or
216 * some of the digits are different, or
217 * there is another digit present in the original string
218 * then we NOT successfully parsed the value.
220 if (orig_has_neg != recheck_has_neg ||
221 strncmp(buf_skip_space, buf_cmp_start, strlen(buf_cmp_start)) != 0 ||
222 (strlen(buf_skip_space) != buf_cmp_len &&
223 isdigit((int)buf_skip_space[buf_cmp_len])
230 if (_errno == ERANGE)
242 void* rpl_realloc(void* p, size_t n)
248 return realloc(p, n);
252 #define NELEM(a) (sizeof(a) / sizeof(a[0]))
253 static const char* json_type_name[] = {
254 /* If you change this, be sure to update the enum json_type definition too */
264 const char *json_type_to_name(enum json_type o_type)
266 if (o_type < 0 || o_type >= NELEM(json_type_name))
268 MC_ERROR("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
271 return json_type_name[o_type];