summaryrefslogtreecommitdiff
path: root/json_util.c
diff options
context:
space:
mode:
authordota17 <chenguopingdota@163.com>2020-03-28 10:25:00 +0800
committerdota17 <chenguopingdota@163.com>2020-04-03 11:39:30 +0800
commit8b162c4b896e5b40feb587aedb7c4f687d14dc67 (patch)
tree1404f3ba6994caf33f5147c793307c2569ca3226 /json_util.c
parentc117d8a8a83d5bcdd433b05e1846ccb73eb3413d (diff)
downloadjson-c-8b162c4b896e5b40feb587aedb7c4f687d14dc67.tar.gz
clang-format the files
Diffstat (limited to 'json_util.c')
-rw-r--r--json_util.c158
1 files changed, 87 insertions, 71 deletions
diff --git a/json_util.c b/json_util.c
index 00e6fd1..abbabd3 100644
--- a/json_util.c
+++ b/json_util.c
@@ -14,13 +14,13 @@
#include "strerror_override.h"
+#include <ctype.h>
+#include <limits.h>
#include <stdarg.h>
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
-#include <stddef.h>
-#include <limits.h>
#include <string.h>
-#include <ctype.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
@@ -35,32 +35,32 @@
#endif /* HAVE_FCNTL_H */
#ifdef HAVE_UNISTD_H
-# include <unistd.h>
+#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef WIN32
-# if MSC_VER < 1800
+#if MSC_VER < 1800
/* strtoll/strtoull is available only since Visual Studio 2013 */
-# define strtoll _strtoi64
-# define strtoull _strtoui64
-# endif
-# define WIN32_LEAN_AND_MEAN
-# include <windows.h>
-# include <io.h>
+#define strtoll _strtoi64
+#define strtoull _strtoui64
+#endif
+#define WIN32_LEAN_AND_MEAN
+#include <io.h>
+#include <windows.h>
#endif /* defined(WIN32) */
#if !defined(HAVE_OPEN) && defined(WIN32)
-# define open _open
+#define open _open
#endif
#include "snprintf_compat.h"
#include "debug.h"
-#include "printbuf.h"
#include "json_inttypes.h"
#include "json_object.h"
#include "json_tokener.h"
#include "json_util.h"
+#include "printbuf.h"
static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const char *filename);
@@ -82,66 +82,74 @@ void _json_c_set_last_err(const char *err_fmt, ...)
va_end(ap);
}
-struct json_object* json_object_from_fd(int fd)
+struct json_object *json_object_from_fd(int fd)
{
return json_object_from_fd_ex(fd, -1);
}
-struct json_object* json_object_from_fd_ex(int fd, int in_depth)
+struct json_object *json_object_from_fd_ex(int fd, int in_depth)
{
- struct printbuf *pb;
- struct json_object *obj;
- char buf[JSON_FILE_BUF_SIZE];
- int ret;
+ struct printbuf *pb;
+ struct json_object *obj;
+ char buf[JSON_FILE_BUF_SIZE];
+ int ret;
int depth = JSON_TOKENER_DEFAULT_DEPTH;
json_tokener *tok;
- if(!(pb = printbuf_new())) {
- _json_c_set_last_err("json_object_from_file: printbuf_new failed\n");
- return NULL;
- }
+ if (!(pb = printbuf_new()))
+ {
+ _json_c_set_last_err("json_object_from_file: printbuf_new failed\n");
+ return NULL;
+ }
if (in_depth != -1)
depth = in_depth;
tok = json_tokener_new_ex(depth);
if (!tok)
{
- _json_c_set_last_err("json_object_from_fd: unable to allocate json_tokener(depth=%d): %s\n", depth, strerror(errno));
+ _json_c_set_last_err(
+ "json_object_from_fd: unable to allocate json_tokener(depth=%d): %s\n", depth,
+ strerror(errno));
printbuf_free(pb);
return NULL;
}
- while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
- printbuf_memappend(pb, buf, ret);
- }
- if(ret < 0) {
- _json_c_set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd, strerror(errno));
- json_tokener_free(tok);
- printbuf_free(pb);
- return NULL;
- }
+ while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0)
+ {
+ printbuf_memappend(pb, buf, ret);
+ }
+ if (ret < 0)
+ {
+ _json_c_set_last_err("json_object_from_fd: error reading fd %d: %s\n", fd,
+ strerror(errno));
+ json_tokener_free(tok);
+ printbuf_free(pb);
+ return NULL;
+ }
- obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb));
+ obj = json_tokener_parse_ex(tok, pb->buf, printbuf_length(pb));
if (obj == NULL)
- _json_c_set_last_err("json_tokener_parse_ex failed: %s\n", json_tokener_error_desc(json_tokener_get_error(tok)));
+ _json_c_set_last_err("json_tokener_parse_ex failed: %s\n",
+ json_tokener_error_desc(json_tokener_get_error(tok)));
json_tokener_free(tok);
printbuf_free(pb);
return obj;
}
-struct json_object* json_object_from_file(const char *filename)
+struct json_object *json_object_from_file(const char *filename)
{
- struct json_object *obj;
- int fd;
-
- if((fd = open(filename, O_RDONLY)) < 0) {
- _json_c_set_last_err("json_object_from_file: error opening file %s: %s\n",
- filename, strerror(errno));
- return NULL;
- }
- obj = json_object_from_fd(fd);
- close(fd);
- return obj;
+ struct json_object *obj;
+ int fd;
+
+ if ((fd = open(filename, O_RDONLY)) < 0)
+ {
+ _json_c_set_last_err("json_object_from_file: error opening file %s: %s\n", filename,
+ strerror(errno));
+ return NULL;
+ }
+ obj = json_object_from_fd(fd);
+ close(fd);
+ return obj;
}
/* extended "format and write to file" function */
@@ -151,14 +159,16 @@ int json_object_to_file_ext(const char *filename, struct json_object *obj, int f
int fd, ret;
int saved_errno;
- if (!obj) {
+ if (!obj)
+ {
_json_c_set_last_err("json_object_to_file: object is null\n");
return -1;
}
- if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
- _json_c_set_last_err("json_object_to_file: error opening file %s: %s\n",
- filename, strerror(errno));
+ if ((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0)
+ {
+ _json_c_set_last_err("json_object_to_file: error opening file %s: %s\n", filename,
+ strerror(errno));
return -1;
}
ret = _json_object_to_fd(fd, obj, flags, filename);
@@ -170,7 +180,8 @@ int json_object_to_file_ext(const char *filename, struct json_object *obj, int f
int json_object_to_fd(int fd, struct json_object *obj, int flags)
{
- if (!obj) {
+ if (!obj)
+ {
_json_c_set_last_err("json_object_to_fd: object is null\n");
return -1;
}
@@ -185,18 +196,21 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const
filename = filename ? filename : "(fd)";
- if (!(json_str = json_object_to_json_string_ext(obj,flags))) {
+ if (!(json_str = json_object_to_json_string_ext(obj, flags)))
+ {
return -1;
}
/* CAW: probably unnecessary, but the most 64bit safe */
wsize = (unsigned int)(strlen(json_str) & UINT_MAX);
wpos = 0;
- while(wpos < wsize) {
- if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
- _json_c_set_last_err("json_object_to_file: error writing file %s: %s\n",
- filename, strerror(errno));
- return -1;
+ while (wpos < wsize)
+ {
+ if ((ret = write(fd, json_str + wpos, wsize - wpos)) < 0)
+ {
+ _json_c_set_last_err("json_object_to_file: error writing file %s: %s\n",
+ filename, strerror(errno));
+ return -1;
}
/* because of the above check for ret < 0, we can safely cast and add */
@@ -210,14 +224,14 @@ static int _json_object_to_fd(int fd, struct json_object *obj, int flags, const
int json_object_to_file(const char *filename, struct json_object *obj)
{
- return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
+ return json_object_to_file_ext(filename, obj, JSON_C_TO_STRING_PLAIN);
}
int json_parse_double(const char *buf, double *retval)
{
- char *end;
- *retval = strtod(buf, &end);
- return end == buf ? 1 : 0;
+ char *end;
+ *retval = strtod(buf, &end);
+ return end == buf ? 1 : 0;
}
int json_parse_int64(const char *buf, int64_t *retval)
@@ -238,10 +252,12 @@ int json_parse_uint64(const char *buf, uint64_t *retval)
uint64_t val;
errno = 1;
- while (*buf == ' ') {
+ while (*buf == ' ')
+ {
buf++;
}
- if (*buf == '-') errno = 0;
+ if (*buf == '-')
+ errno = 0;
val = strtoull(buf, &end, 10);
if (end != buf)
@@ -250,7 +266,7 @@ int json_parse_uint64(const char *buf, uint64_t *retval)
}
#ifndef HAVE_REALLOC
-void* rpl_realloc(void* p, size_t n)
+void *rpl_realloc(void *p, size_t n)
{
if (n == 0)
n = 1;
@@ -260,7 +276,7 @@ void* rpl_realloc(void* p, size_t n)
}
#endif
-#define NELEM(a) (sizeof(a) / sizeof(a[0]))
+#define NELEM(a) (sizeof(a) / sizeof(a[0]))
/* clang-format off */
static const char *json_type_name[] = {
/* If you change this, be sure to update the enum json_type definition too */
@@ -279,7 +295,8 @@ const char *json_type_to_name(enum json_type o_type)
int o_type_int = (int)o_type;
if (o_type_int < 0 || o_type_int >= (int)NELEM(json_type_name))
{
- _json_c_set_last_err("json_type_to_name: type %d is out of range [0,%d]\n", o_type, NELEM(json_type_name));
+ _json_c_set_last_err("json_type_to_name: type %d is out of range [0,%d]\n", o_type,
+ NELEM(json_type_name));
return NULL;
}
return json_type_name[o_type];
@@ -287,8 +304,7 @@ const char *json_type_to_name(enum json_type o_type)
void json_abort(const char *message)
{
- if (message != NULL)
- fprintf (stderr, "json-c aborts with error: %s\n", message);
- abort();
+ if (message != NULL)
+ fprintf(stderr, "json-c aborts with error: %s\n", message);
+ abort();
}
-