json_util.c
Go to the documentation of this file.
1 /*
2  * $Id: json_util.c,v 1.4 2006/01/30 23:07:57 mclark Exp $
3  *
4  * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5  * Michael Clark <michael@metaparadigm.com>
6  *
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.
9  *
10  */
11 
12 #include "config.h"
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <limits.h>
17 #include <string.h>
18 #include <errno.h>
19 
20 #if HAVE_SYS_TYPES_H
21 #include <sys/types.h>
22 #endif /* HAVE_SYS_TYPES_H */
23 
24 #if HAVE_SYS_STAT_H
25 #include <sys/stat.h>
26 #endif /* HAVE_SYS_STAT_H */
27 
28 #if HAVE_FCNTL_H
29 #include <fcntl.h>
30 #endif /* HAVE_FCNTL_H */
31 
32 #if HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif /* HAVE_UNISTD_H */
35 
36 #ifdef WIN32
37 # define WIN32_LEAN_AND_MEAN
38 # include <windows.h>
39 # include <io.h>
40 #endif /* defined(WIN32) */
41 
42 #if !HAVE_OPEN && defined(WIN32)
43 # define open _open
44 #endif
45 
46 
47 #include "bits.h"
48 #include "debug.h"
49 #include "printbuf.h"
50 #include "json_object.h"
51 #include "json_tokener.h"
52 #include "json_util.h"
53 
54 struct json_object* json_object_from_file(char *filename)
55 {
56  struct printbuf *pb;
57  struct json_object *obj;
58  char buf[JSON_FILE_BUF_SIZE];
59  int fd, ret;
60 
61  if((fd = open(filename, O_RDONLY)) < 0) {
62  mc_error("json_object_from_file: error reading file %s: %s\n",
63  filename, strerror(errno));
64  return error_ptr(-1);
65  }
66  if(!(pb = printbuf_new())) {
67  mc_error("json_object_from_file: printbuf_new failed\n");
68  return error_ptr(-1);
69  }
70  while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
71  printbuf_memappend(pb, buf, ret);
72  }
73  close(fd);
74  if(ret < 0) {
75  mc_abort("json_object_from_file: error reading file %s: %s\n",
76  filename, strerror(errno));
77  printbuf_free(pb);
78  return error_ptr(-1);
79  }
80  obj = json_tokener_parse(pb->buf);
81  printbuf_free(pb);
82  return obj;
83 }
84 
85 int json_object_to_file(char *filename, struct json_object *obj)
86 {
87  char *json_str;
88  int fd, ret;
89  unsigned int wpos, wsize;
90 
91  if(!obj) {
92  mc_error("json_object_to_file: object is null\n");
93  return -1;
94  }
95 
96  if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
97  mc_error("json_object_to_file: error opening file %s: %s\n",
98  filename, strerror(errno));
99  return -1;
100  }
101 
102  if(!(json_str = json_object_to_json_string(obj))) { return -1; }
103 
104 
105  wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
106  wpos = 0;
107  while(wpos < wsize) {
108  if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
109  close(fd);
110  mc_error("json_object_to_file: error writing file %s: %s\n",
111  filename, strerror(errno));
112  return -1;
113  }
114 
115  /* because of the above check for ret < 0, we can safely cast and add */
116  wpos += (unsigned int)ret;
117  }
118 
119  close(fd);
120  return 0;
121 }
char * buf
Definition: printbuf.h:18
struct printbuf * printbuf_new()
Definition: printbuf.c:28
#define error_ptr(error)
Definition: bits.h:24
void mc_error(const char *msg,...)
Definition: debug.c:72
#define JSON_FILE_BUF_SIZE
Definition: json_util.h:17
struct json_object * json_object_from_file(char *filename)
Definition: json_util.c:54
const char * json_object_to_json_string(struct json_object *this)
Definition: json_object.c:199
void mc_abort(const char *msg,...)
Definition: debug.c:44
void printbuf_free(struct printbuf *p)
Definition: printbuf.c:138
int json_object_to_file(char *filename, struct json_object *obj)
Definition: json_util.c:85
int printbuf_memappend(struct printbuf *p, const char *buf, int size)
Definition: printbuf.c:43
struct json_object * json_tokener_parse(const char *str)
Definition: json_tokener.c:92
char buf[100]
Definition: ld_recover.c:87


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23