mirror of
https://github.com/json-c/json-c.git
synced 2026-03-21 14:09:06 +08:00
Introduce json_object_from_fd
Also refactor json_object_from_file to use json_object_from_fd internally. Signed-off-by: Michael Heimpold <mhei@heimpold.de>
This commit is contained in:
37
json_util.c
37
json_util.c
@@ -64,30 +64,30 @@ static int sscanf_is_broken = 0;
|
||||
static int sscanf_is_broken_testdone = 0;
|
||||
static void sscanf_is_broken_test(void);
|
||||
|
||||
struct json_object* json_object_from_file(const char *filename)
|
||||
/*
|
||||
* Create a JSON object from already opened file descriptor.
|
||||
*
|
||||
* This function can be helpful, when you opened the file already,
|
||||
* e.g. when you have a temp file.
|
||||
* Note, that the fd must be readable at the actual position, i.e.
|
||||
* use lseek(fd, 0, SEEK_SET) before.
|
||||
*/
|
||||
struct json_object* json_object_from_fd(int fd)
|
||||
{
|
||||
struct printbuf *pb;
|
||||
struct json_object *obj;
|
||||
char buf[JSON_FILE_BUF_SIZE];
|
||||
int fd, ret;
|
||||
int ret;
|
||||
|
||||
if((fd = open(filename, O_RDONLY)) < 0) {
|
||||
MC_ERROR("json_object_from_file: error opening file %s: %s\n",
|
||||
filename, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
if(!(pb = printbuf_new())) {
|
||||
close(fd);
|
||||
MC_ERROR("json_object_from_file: printbuf_new failed\n");
|
||||
return NULL;
|
||||
}
|
||||
while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
|
||||
printbuf_memappend(pb, buf, ret);
|
||||
}
|
||||
close(fd);
|
||||
if(ret < 0) {
|
||||
MC_ERROR("json_object_from_file: error reading file %s: %s\n",
|
||||
filename, strerror(errno));
|
||||
MC_ERROR("json_object_from_fd: error reading fd %d: %s\n", fd, strerror(errno));
|
||||
printbuf_free(pb);
|
||||
return NULL;
|
||||
}
|
||||
@@ -96,6 +96,21 @@ struct json_object* json_object_from_file(const char *filename)
|
||||
return obj;
|
||||
}
|
||||
|
||||
struct json_object* json_object_from_file(const char *filename)
|
||||
{
|
||||
struct json_object *obj;
|
||||
int fd;
|
||||
|
||||
if((fd = open(filename, O_RDONLY)) < 0) {
|
||||
MC_ERROR("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 */
|
||||
|
||||
int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags)
|
||||
|
||||
@@ -31,6 +31,7 @@ extern "C" {
|
||||
|
||||
/* utility functions */
|
||||
extern struct json_object* json_object_from_file(const char *filename);
|
||||
extern struct json_object* json_object_from_fd(int fd);
|
||||
extern int json_object_to_file(const char *filename, struct json_object *obj);
|
||||
extern int json_object_to_file_ext(const char *filename, struct json_object *obj, int flags);
|
||||
extern int json_parse_int64(const char *buf, int64_t *retval);
|
||||
|
||||
Reference in New Issue
Block a user