Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | 837240f | 2007-03-13 08:26:25 +0000 | [diff] [blame] | 2 | * $Id: json_util.c,v 1.4 2006/01/30 23:07:57 mclark Exp $ |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 3 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 5 | * Michael Clark <michael@metaparadigm.com> |
| 6 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 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. |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include "config.h" |
| 13 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
Michael Clark | c8f4a6e | 2007-12-07 02:44:24 +0000 | [diff] [blame] | 16 | #include <stddef.h> |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 17 | #include <limits.h> |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 18 | #include <string.h> |
| 19 | #include <errno.h> |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 20 | |
| 21 | #if HAVE_SYS_TYPES_H |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 22 | #include <sys/types.h> |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 23 | #endif /* HAVE_SYS_TYPES_H */ |
| 24 | |
| 25 | #if HAVE_SYS_STAT_H |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 26 | #include <sys/stat.h> |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 27 | #endif /* HAVE_SYS_STAT_H */ |
| 28 | |
| 29 | #if HAVE_FCNTL_H |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 31 | #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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 42 | |
Michael Clark | 837240f | 2007-03-13 08:26:25 +0000 | [diff] [blame] | 43 | #if !HAVE_OPEN && defined(WIN32) |
| 44 | # define open _open |
| 45 | #endif |
| 46 | |
| 47 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 48 | #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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 55 | struct 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 Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 63 | MC_ERROR("json_object_from_file: error reading file %s: %s\n", |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 64 | filename, strerror(errno)); |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame^] | 65 | return (struct json_object*)error_ptr(-1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 66 | } |
| 67 | if(!(pb = printbuf_new())) { |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 68 | MC_ERROR("json_object_from_file: printbuf_new failed\n"); |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame^] | 69 | return (struct json_object*)error_ptr(-1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 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 Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 76 | MC_ABORT("json_object_from_file: error reading file %s: %s\n", |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 77 | filename, strerror(errno)); |
| 78 | printbuf_free(pb); |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame^] | 79 | return (struct json_object*)error_ptr(-1); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 80 | } |
| 81 | obj = json_tokener_parse(pb->buf); |
| 82 | printbuf_free(pb); |
| 83 | return obj; |
| 84 | } |
| 85 | |
| 86 | int json_object_to_file(char *filename, struct json_object *obj) |
| 87 | { |
Michael Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 88 | const char *json_str; |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 89 | int fd, ret; |
| 90 | unsigned int wpos, wsize; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 91 | |
| 92 | if(!obj) { |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 93 | MC_ERROR("json_object_to_file: object is null\n"); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) { |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 98 | MC_ERROR("json_object_to_file: error opening file %s: %s\n", |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 99 | filename, strerror(errno)); |
| 100 | return -1; |
| 101 | } |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 102 | |
| 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 107 | wpos = 0; |
| 108 | while(wpos < wsize) { |
| 109 | if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) { |
| 110 | close(fd); |
Michael Clark | dfaf670 | 2007-10-25 02:26:00 +0000 | [diff] [blame] | 111 | MC_ERROR("json_object_to_file: error writing file %s: %s\n", |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 112 | filename, strerror(errno)); |
| 113 | return -1; |
| 114 | } |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 115 | |
| 116 | /* because of the above check for ret < 0, we can safely cast and add */ |
| 117 | wpos += (unsigned int)ret; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | close(fd); |
| 121 | return 0; |
| 122 | } |