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 */
40 # define WIN32_LEAN_AND_MEAN
43 #endif /* defined(WIN32) */
45 #if !defined(HAVE_OPEN) && defined(WIN32)
49 #if !defined(HAVE_SNPRINTF) && defined(_MSC_VER)
50 /* MSC has the version as _snprintf */
51 # define snprintf _snprintf
52 #elif !defined(HAVE_SNPRINTF)
53 # error You do not have snprintf on your system.
54 #endif /* HAVE_SNPRINTF */
59 #include "json_inttypes.h"
60 #include "json_object.h"
61 #include "json_tokener.h"
62 #include "json_util.h"
64 struct json_object* json_object_from_file(const char *filename)
67 struct json_object *obj;
68 char buf[JSON_FILE_BUF_SIZE];
71 if((fd = open(filename, O_RDONLY)) < 0) {
72 MC_ERROR("json_object_from_file: error reading file %s: %s\n",
73 filename, strerror(errno));
74 return NULL; // XAX this is an API change!
76 if(!(pb = printbuf_new())) {
78 MC_ERROR("json_object_from_file: printbuf_new failed\n");
81 while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
82 printbuf_memappend(pb, buf, ret);
86 MC_ABORT("json_object_from_file: error reading file %s: %s\n",
87 filename, strerror(errno));
91 obj = json_tokener_parse(pb->buf);
96 /* extended "format and write to file" function */
98 int json_object_to_file_ext(char *filename, struct json_object *obj, int flags)
100 const char *json_str;
102 unsigned int wpos, wsize;
105 MC_ERROR("json_object_to_file: object is null\n");
109 if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
110 MC_ERROR("json_object_to_file: error opening file %s: %s\n",
111 filename, strerror(errno));
115 if(!(json_str = json_object_to_json_string_ext(obj,flags))) {
120 wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
122 while(wpos < wsize) {
123 if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
125 MC_ERROR("json_object_to_file: error writing file %s: %s\n",
126 filename, strerror(errno));
130 /* because of the above check for ret < 0, we can safely cast and add */
131 wpos += (unsigned int)ret;
138 // backwards compatible "format and write to file" function
140 int json_object_to_file(char *filename, struct json_object *obj)
142 return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
145 int json_parse_int64(const char *buf, int64_t *retval)
148 const char *buf_skip_space;
150 if (sscanf(buf, "%" SCNd64, &num64) != 1)
152 MC_DEBUG("Failed to parse, sscanf != 1\n");
155 buf_skip_space = buf;
157 // Skip leading spaces
158 while (isspace((int)*buf_skip_space) && *buf_skip_space)
160 if (*buf_skip_space == '-')
165 // Skip leading zeros, but keep at least one digit
166 while (buf_skip_space[0] == '0' && buf_skip_space[1] != '\0')
168 if (buf_skip_space[0] == '0' && buf_skip_space[1] == '\0')
169 orig_has_neg = 0; // "-0" is the same as just plain "0"
174 char *buf_cmp_start = buf_cmp;
175 int recheck_has_neg = 0;
177 snprintf(buf_cmp_start, sizeof(buf_cmp), "%" PRId64, num64);
178 if (*buf_cmp_start == '-')
183 // No need to skip leading spaces or zeros here.
185 buf_cmp_len = strlen(buf_cmp_start);
187 * If the sign is different, or
188 * some of the digits are different, or
189 * there is another digit present in the original string
190 * then we NOT successfully parsed the value.
192 if (orig_has_neg != recheck_has_neg ||
193 strncmp(buf_skip_space, buf_cmp_start, strlen(buf_cmp_start)) != 0 ||
194 (strlen(buf_skip_space) != buf_cmp_len &&
195 isdigit((int)buf_skip_space[buf_cmp_len])
214 void* rpl_realloc(void* p, size_t n)
220 return realloc(p, n);
224 #define NELEM(a) (sizeof(a) / sizeof(a[0]))
225 static const char* json_type_name[] = {
226 /* If you change this, be sure to update the enum json_type definition too */
236 const char *json_type_to_name(enum json_type o_type)
238 if (o_type < 0 || o_type >= NELEM(json_type_name))
240 MC_ERROR("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
243 return json_type_name[o_type];