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 : /* Excluded from GDAL for Windows CE port. */
13 : #ifndef _WIN32_WCE
14 :
15 : #include "config.h"
16 :
17 : #include <stdio.h>
18 : #include <stdlib.h>
19 : #include <limits.h>
20 : #include <string.h>
21 : #include <errno.h>
22 :
23 : #if HAVE_SYS_TYPES_H
24 : #include <sys/types.h>
25 : #endif /* HAVE_SYS_TYPES_H */
26 :
27 : #if HAVE_SYS_STAT_H
28 : #include <sys/stat.h>
29 : #endif /* HAVE_SYS_STAT_H */
30 :
31 : #if HAVE_FCNTL_H
32 : #include <fcntl.h>
33 : #endif /* HAVE_FCNTL_H */
34 :
35 : #if HAVE_UNISTD_H
36 : # include <unistd.h>
37 : #endif /* HAVE_UNISTD_H */
38 :
39 : #ifdef _WIN32
40 : # define WIN32_LEAN_AND_MEAN
41 : # include <windows.h>
42 : # include <io.h>
43 : #endif /* defined(WIN32) */
44 :
45 : #if !HAVE_OPEN && defined(WIN32)
46 : # define open _open
47 : #endif
48 :
49 :
50 : #include "bits.h"
51 : #include "debug.h"
52 : #include "printbuf.h"
53 : #include "json_object.h"
54 : #include "json_tokener.h"
55 : #include "json_util.h"
56 :
57 0 : struct json_object* json_object_from_file(char *filename)
58 : {
59 : struct printbuf *pb;
60 : struct json_object *obj;
61 : char buf[JSON_FILE_BUF_SIZE];
62 : int fd, ret;
63 :
64 0 : if((fd = open(filename, O_RDONLY)) < 0) {
65 0 : mc_error("json_object_from_file: error reading file %s: %s\n",
66 : filename, strerror(errno));
67 0 : return error_ptr(-1);
68 : }
69 0 : if((pb = printbuf_new()) == NULL) {
70 0 : mc_error("json_object_from_file: printbuf_new failed\n");
71 0 : return error_ptr(-1);
72 : }
73 0 : while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
74 0 : printbuf_memappend(pb, buf, ret);
75 : }
76 0 : close(fd);
77 0 : if(ret < 0) {
78 0 : mc_abort("json_object_from_file: error reading file %s: %s\n",
79 : filename, strerror(errno));
80 0 : printbuf_free(pb);
81 0 : return error_ptr(-1);
82 : }
83 0 : obj = json_tokener_parse(pb->buf);
84 0 : printbuf_free(pb);
85 0 : return obj;
86 : }
87 :
88 0 : int json_object_to_file(char *filename, struct json_object *obj)
89 : {
90 : char *json_str;
91 : int fd, ret;
92 : unsigned int wpos, wsize;
93 :
94 0 : if(!obj) {
95 0 : mc_error("json_object_to_file: object is null\n");
96 0 : return -1;
97 : }
98 :
99 0 : if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
100 0 : mc_error("json_object_to_file: error opening file %s: %s\n",
101 : filename, strerror(errno));
102 0 : return -1;
103 : }
104 :
105 0 : if((json_str = json_object_to_json_string(obj)) == NULL) {
106 0 : return -1;
107 : }
108 :
109 :
110 0 : wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
111 0 : wpos = 0;
112 0 : while(wpos < wsize) {
113 0 : if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
114 0 : close(fd);
115 0 : mc_error("json_object_to_file: error writing file %s: %s\n",
116 : filename, strerror(errno));
117 0 : return -1;
118 : }
119 :
120 : /* because of the above check for ret < 0, we can safely cast and add */
121 0 : wpos += (unsigned int)ret;
122 : }
123 :
124 0 : close(fd);
125 0 : return 0;
126 : }
127 :
128 : #endif
129 :
|