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 static int sscanf_is_broken
= 0;
65 static int sscanf_is_broken_testdone
= 0;
66 static void sscanf_is_broken_test(void);
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 opening 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_ERROR("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(const 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(const 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
)
151 return (sscanf(buf
, "%lf", retval
)==1 ? 0 : 1);
155 * Not all implementations of sscanf actually work properly.
156 * Check whether the one we're currently using does, and if
157 * it's broken, enable the workaround code.
159 static void sscanf_is_broken_test()
162 int ret_errno
, is_int64_min
, ret_errno2
, is_int64_max
;
164 (void)sscanf(" -01234567890123456789012345", "%" SCNd64
, &num64
);
166 is_int64_min
= (num64
== INT64_MIN
);
168 (void)sscanf(" 01234567890123456789012345", "%" SCNd64
, &num64
);
170 is_int64_max
= (num64
== INT64_MAX
);
172 if (ret_errno
!= ERANGE
|| !is_int64_min
||
173 ret_errno2
!= ERANGE
|| !is_int64_max
)
175 MC_DEBUG("sscanf_is_broken_test failed, enabling workaround code\n");
176 sscanf_is_broken
= 1;
180 int json_parse_int64(const char *buf
, int64_t *retval
)
183 const char *buf_sig_digits
;
187 if (!sscanf_is_broken_testdone
)
189 sscanf_is_broken_test();
190 sscanf_is_broken_testdone
= 1;
193 // Skip leading spaces
194 while (isspace((int)*buf
) && *buf
)
197 errno
= 0; // sscanf won't always set errno, so initialize
198 if (sscanf(buf
, "%" SCNd64
, &num64
) != 1)
200 MC_DEBUG("Failed to parse, sscanf != 1\n");
205 buf_sig_digits
= buf
;
207 if (*buf_sig_digits
== '-')
213 // Not all sscanf implementations actually work
214 if (sscanf_is_broken
&& saved_errno
!= ERANGE
)
217 char *buf_cmp_start
= buf_cmp
;
218 int recheck_has_neg
= 0;
221 // Skip leading zeros, but keep at least one digit
222 while (buf_sig_digits
[0] == '0' && buf_sig_digits
[1] != '\0')
224 if (num64
== 0) // assume all sscanf impl's will parse -0 to 0
225 orig_has_neg
= 0; // "-0" is the same as just plain "0"
227 snprintf(buf_cmp_start
, sizeof(buf_cmp
), "%" PRId64
, num64
);
228 if (*buf_cmp_start
== '-')
233 // No need to skip leading spaces or zeros here.
235 buf_cmp_len
= strlen(buf_cmp_start
);
237 * If the sign is different, or
238 * some of the digits are different, or
239 * there is another digit present in the original string
240 * then we have NOT successfully parsed the value.
242 if (orig_has_neg
!= recheck_has_neg
||
243 strncmp(buf_sig_digits
, buf_cmp_start
, strlen(buf_cmp_start
)) != 0 ||
244 ((int)strlen(buf_sig_digits
) != buf_cmp_len
&&
245 isdigit((int)buf_sig_digits
[buf_cmp_len
])
249 saved_errno
= ERANGE
;
253 // Not all sscanf impl's set the value properly when out of range.
254 // Always do this, even for properly functioning implementations,
255 // since it shouldn't slow things down much.
256 if (saved_errno
== ERANGE
)
268 void* rpl_realloc(void* p
, size_t n
)
274 return realloc(p
, n
);
278 #define NELEM(a) (sizeof(a) / sizeof(a[0]))
279 static const char* json_type_name
[] = {
280 /* If you change this, be sure to update the enum json_type definition too */
290 const char *json_type_to_name(enum json_type o_type
)
292 int o_type_int
= (int)o_type
;
293 if (o_type_int
< 0 || o_type_int
>= (int)NELEM(json_type_name
))
295 MC_ERROR("json_type_to_name: type %d is out of range [0,%d]\n", o_type
, NELEM(json_type_name
));
298 return json_type_name
[o_type
];