blob: 1a65596c846135bea45f24152e20faf775c8c291 [file] [log] [blame]
Michael Clark4504df72007-03-13 08:26:20 +00001/*
Michael Clark837240f2007-03-13 08:26:25 +00002 * $Id: json_util.c,v 1.4 2006/01/30 23:07:57 mclark Exp $
Michael Clark4504df72007-03-13 08:26:20 +00003 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00004 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
Michael Clark4504df72007-03-13 08:26:20 +00005 * Michael Clark <michael@metaparadigm.com>
6 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00007 * 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.
Michael Clark4504df72007-03-13 08:26:20 +00009 *
10 */
11
12#include "config.h"
13
Michael Clarkf0d08882007-03-13 08:26:18 +000014#include <stdio.h>
15#include <stdlib.h>
Michael Clarkc8f4a6e2007-12-07 02:44:24 +000016#include <stddef.h>
Michael Clark4504df72007-03-13 08:26:20 +000017#include <limits.h>
Michael Clarkf0d08882007-03-13 08:26:18 +000018#include <string.h>
19#include <errno.h>
Michael Clark4504df72007-03-13 08:26:20 +000020
21#if HAVE_SYS_TYPES_H
Michael Clarkf0d08882007-03-13 08:26:18 +000022#include <sys/types.h>
Michael Clark4504df72007-03-13 08:26:20 +000023#endif /* HAVE_SYS_TYPES_H */
24
25#if HAVE_SYS_STAT_H
Michael Clarkf0d08882007-03-13 08:26:18 +000026#include <sys/stat.h>
Michael Clark4504df72007-03-13 08:26:20 +000027#endif /* HAVE_SYS_STAT_H */
28
29#if HAVE_FCNTL_H
Michael Clarkf0d08882007-03-13 08:26:18 +000030#include <fcntl.h>
Michael Clark4504df72007-03-13 08:26:20 +000031#endif /* HAVE_FCNTL_H */
32
33#if HAVE_UNISTD_H
34# include <unistd.h>
35#endif /* HAVE_UNISTD_H */
36
37#ifdef WIN32
38# define WIN32_LEAN_AND_MEAN
39# include <windows.h>
40# include <io.h>
41#endif /* defined(WIN32) */
Michael Clarkf0d08882007-03-13 08:26:18 +000042
Michael Clark837240f2007-03-13 08:26:25 +000043#if !HAVE_OPEN && defined(WIN32)
44# define open _open
45#endif
46
47
Michael Clarkf0d08882007-03-13 08:26:18 +000048#include "bits.h"
49#include "debug.h"
50#include "printbuf.h"
51#include "json_object.h"
52#include "json_tokener.h"
53#include "json_util.h"
54
Michael Clarkf0d08882007-03-13 08:26:18 +000055struct json_object* json_object_from_file(char *filename)
56{
57 struct printbuf *pb;
58 struct json_object *obj;
59 char buf[JSON_FILE_BUF_SIZE];
60 int fd, ret;
61
62 if((fd = open(filename, O_RDONLY)) < 0) {
Michael Clarkdfaf6702007-10-25 02:26:00 +000063 MC_ERROR("json_object_from_file: error reading file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +000064 filename, strerror(errno));
65 return error_ptr(-1);
66 }
67 if(!(pb = printbuf_new())) {
Michael Clarkdfaf6702007-10-25 02:26:00 +000068 MC_ERROR("json_object_from_file: printbuf_new failed\n");
Michael Clarkf0d08882007-03-13 08:26:18 +000069 return error_ptr(-1);
70 }
71 while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
72 printbuf_memappend(pb, buf, ret);
73 }
74 close(fd);
75 if(ret < 0) {
Michael Clarkdfaf6702007-10-25 02:26:00 +000076 MC_ABORT("json_object_from_file: error reading file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +000077 filename, strerror(errno));
78 printbuf_free(pb);
79 return error_ptr(-1);
80 }
81 obj = json_tokener_parse(pb->buf);
82 printbuf_free(pb);
83 return obj;
84}
85
86int json_object_to_file(char *filename, struct json_object *obj)
87{
88 char *json_str;
Michael Clark4504df72007-03-13 08:26:20 +000089 int fd, ret;
90 unsigned int wpos, wsize;
Michael Clarkf0d08882007-03-13 08:26:18 +000091
92 if(!obj) {
Michael Clarkdfaf6702007-10-25 02:26:00 +000093 MC_ERROR("json_object_to_file: object is null\n");
Michael Clarkf0d08882007-03-13 08:26:18 +000094 return -1;
95 }
96
97 if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
Michael Clarkdfaf6702007-10-25 02:26:00 +000098 MC_ERROR("json_object_to_file: error opening file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +000099 filename, strerror(errno));
100 return -1;
101 }
Michael Clark4504df72007-03-13 08:26:20 +0000102
103 if(!(json_str = json_object_to_json_string(obj))) { return -1; }
104
105
106 wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
Michael Clarkf0d08882007-03-13 08:26:18 +0000107 wpos = 0;
108 while(wpos < wsize) {
109 if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
110 close(fd);
Michael Clarkdfaf6702007-10-25 02:26:00 +0000111 MC_ERROR("json_object_to_file: error writing file %s: %s\n",
Michael Clarkf0d08882007-03-13 08:26:18 +0000112 filename, strerror(errno));
113 return -1;
114 }
Michael Clark4504df72007-03-13 08:26:20 +0000115
116 /* because of the above check for ret < 0, we can safely cast and add */
117 wpos += (unsigned int)ret;
Michael Clarkf0d08882007-03-13 08:26:18 +0000118 }
119
120 close(fd);
121 return 0;
122}