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.
24 #include <sys/types.h>
25 #endif /* HAVE_SYS_TYPES_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 !HAVE_OPEN && defined(WIN32)
53 #include "json_inttypes.h"
54 #include "json_object.h"
55 #include "json_tokener.h"
56 #include "json_util.h"
58 struct json_object* json_object_from_file(const char *filename)
61 struct json_object *obj;
62 char buf[JSON_FILE_BUF_SIZE];
65 if((fd = open(filename, O_RDONLY)) < 0) {
66 MC_ERROR("json_object_from_file: error reading file %s: %s\n",
67 filename, strerror(errno));
68 return NULL; // XAX this is an API change!
70 if(!(pb = printbuf_new())) {
72 MC_ERROR("json_object_from_file: printbuf_new failed\n");
75 while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
76 printbuf_memappend(pb, buf, ret);
80 MC_ABORT("json_object_from_file: error reading file %s: %s\n",
81 filename, strerror(errno));
85 obj = json_tokener_parse(pb->buf);
90 /* extended "format and write to file" function */
92 int json_object_to_file_ext(char *filename, struct json_object *obj, int flags)
96 unsigned int wpos, wsize;
99 MC_ERROR("json_object_to_file: object is null\n");
103 if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
104 MC_ERROR("json_object_to_file: error opening file %s: %s\n",
105 filename, strerror(errno));
109 if(!(json_str = json_object_to_json_string_ext(obj,flags))) {
114 wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
116 while(wpos < wsize) {
117 if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
119 MC_ERROR("json_object_to_file: error writing file %s: %s\n",
120 filename, strerror(errno));
124 /* because of the above check for ret < 0, we can safely cast and add */
125 wpos += (unsigned int)ret;
132 // backwards compatible "format and write to file" function
134 int json_object_to_file(char *filename, struct json_object *obj)
136 return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
139 int json_parse_int64(const char *buf, int64_t *retval)
142 const char *buf_skip_space;
144 if (sscanf(buf, "%" SCNd64, &num64) != 1)
146 MC_DEBUG("Failed to parse, sscanf != 1\n");
149 buf_skip_space = buf;
151 // Skip leading spaces
152 while (isspace((int)*buf_skip_space) && *buf_skip_space)
154 if (*buf_skip_space == '-')
159 // Skip leading zeros, but keep at least one digit
160 while (buf_skip_space[0] == '0' && buf_skip_space[1] != '\0')
162 if (buf_skip_space[0] == '0' && buf_skip_space[1] == '\0')
163 orig_has_neg = 0; // "-0" is the same as just plain "0"
168 char *buf_cmp_start = buf_cmp;
169 int recheck_has_neg = 0;
171 snprintf(buf_cmp_start, sizeof(buf_cmp), "%" PRId64, num64);
172 if (*buf_cmp_start == '-')
177 // No need to skip leading spaces or zeros here.
179 buf_cmp_len = strlen(buf_cmp_start);
181 * If the sign is different, or
182 * some of the digits are different, or
183 * there is another digit present in the original string
184 * then we NOT successfully parsed the value.
186 if (orig_has_neg != recheck_has_neg ||
187 strncmp(buf_skip_space, buf_cmp_start, strlen(buf_cmp_start)) != 0 ||
188 (strlen(buf_skip_space) != buf_cmp_len &&
189 isdigit((int)buf_skip_space[buf_cmp_len])
207 #if HAVE_REALLOC == 0
208 void* rpl_realloc(void* p, size_t n)
214 return realloc(p, n);
218 #define NELEM(a) (sizeof(a) / sizeof(a[0]))
219 static const char* json_type_name[] = {
220 /* If you change this, be sure to update the enum json_type definition too */
230 const char *json_type_to_name(enum json_type o_type)
232 if (o_type < 0 || o_type >= NELEM(json_type_name))
234 MC_ERROR("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
237 return json_type_name[o_type];