json_util.c
Go to the documentation of this file.
00001 /*
00002  * $Id: json_util.c,v 1.4 2006/01/30 23:07:57 mclark Exp $
00003  *
00004  * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
00005  * Michael Clark <michael@metaparadigm.com>
00006  *
00007  * This library is free software; you can redistribute it and/or modify
00008  * it under the terms of the MIT license. See COPYING for details.
00009  *
00010  */
00011 
00012 #include "config.h"
00013 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <limits.h>
00017 #include <string.h>
00018 #include <errno.h>
00019 
00020 #if HAVE_SYS_TYPES_H
00021 #include <sys/types.h>
00022 #endif /* HAVE_SYS_TYPES_H */
00023 
00024 #if HAVE_SYS_STAT_H
00025 #include <sys/stat.h>
00026 #endif /* HAVE_SYS_STAT_H */
00027 
00028 #if HAVE_FCNTL_H
00029 #include <fcntl.h>
00030 #endif /* HAVE_FCNTL_H */
00031 
00032 #if HAVE_UNISTD_H
00033 # include <unistd.h>
00034 #endif /* HAVE_UNISTD_H */
00035 
00036 #ifdef WIN32
00037 # define WIN32_LEAN_AND_MEAN
00038 # include <windows.h>
00039 # include <io.h>
00040 #endif /* defined(WIN32) */
00041 
00042 #if !HAVE_OPEN && defined(WIN32)
00043 # define open _open
00044 #endif
00045 
00046 
00047 #include "bits.h"
00048 #include "debug.h"
00049 #include "printbuf.h"
00050 #include "json_object.h"
00051 #include "json_tokener.h"
00052 #include "json_util.h"
00053 
00054 struct json_object* json_object_from_file(char *filename)
00055 {
00056   struct printbuf *pb;
00057   struct json_object *obj;
00058   char buf[JSON_FILE_BUF_SIZE];
00059   int fd, ret;
00060 
00061   if((fd = open(filename, O_RDONLY)) < 0) {
00062     mc_error("json_object_from_file: error reading file %s: %s\n",
00063              filename, strerror(errno));
00064     return error_ptr(-1);
00065   }
00066   if(!(pb = printbuf_new())) {
00067     mc_error("json_object_from_file: printbuf_new failed\n");
00068     return error_ptr(-1);
00069   }
00070   while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
00071     printbuf_memappend(pb, buf, ret);
00072   }
00073   close(fd);
00074   if(ret < 0) {
00075     mc_abort("json_object_from_file: error reading file %s: %s\n",
00076              filename, strerror(errno));
00077     printbuf_free(pb);
00078     return error_ptr(-1);
00079   }
00080   obj = json_tokener_parse(pb->buf);
00081   printbuf_free(pb);
00082   return obj;
00083 }
00084 
00085 int json_object_to_file(char *filename, struct json_object *obj)
00086 {
00087   char *json_str;
00088   int fd, ret;
00089   unsigned int wpos, wsize;
00090 
00091   if(!obj) {
00092     mc_error("json_object_to_file: object is null\n");
00093     return -1;
00094   }
00095 
00096   if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
00097     mc_error("json_object_to_file: error opening file %s: %s\n",
00098              filename, strerror(errno));
00099     return -1;
00100   }
00101 
00102   if(!(json_str = json_object_to_json_string(obj))) { return -1; }
00103 
00104 
00105   wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
00106   wpos = 0;
00107   while(wpos < wsize) {
00108     if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
00109       close(fd);
00110       mc_error("json_object_to_file: error writing file %s: %s\n",
00111              filename, strerror(errno));
00112       return -1;
00113     }
00114 
00115         /* because of the above check for ret < 0, we can safely cast and add */
00116     wpos += (unsigned int)ret;
00117   }
00118 
00119   close(fd);
00120   return 0;
00121 }


csm_ros
Author(s): Gaƫl Ecorchard , Ivan Dryanovski, William Morris, Andrea Censi
autogenerated on Sat Jun 8 2019 19:52:42