You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_util.c 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * $Id: json_util.c,v 1.2 2005/06/14 22:41:51 mclark Exp $
  3. *
  4. * Copyright Metaparadigm Pte. Ltd. 2004.
  5. * Michael Clark <michael@metaparadigm.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public (LGPL)
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details: http://www.gnu.org/
  16. *
  17. */
  18. #include "config.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <limits.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #if HAVE_SYS_TYPES_H
  25. #include <sys/types.h>
  26. #endif /* HAVE_SYS_TYPES_H */
  27. #if HAVE_SYS_STAT_H
  28. #include <sys/stat.h>
  29. #endif /* HAVE_SYS_STAT_H */
  30. #if HAVE_FCNTL_H
  31. #include <fcntl.h>
  32. #endif /* HAVE_FCNTL_H */
  33. #if HAVE_UNISTD_H
  34. # include <unistd.h>
  35. #endif /* HAVE_UNISTD_H */
  36. #ifdef WIN32
  37. # define WIN32_LEAN_AND_MEAN
  38. # include <windows.h>
  39. # include <io.h>
  40. #endif /* defined(WIN32) */
  41. #include "bits.h"
  42. #include "debug.h"
  43. #include "printbuf.h"
  44. #include "json_object.h"
  45. #include "json_tokener.h"
  46. #include "json_util.h"
  47. struct json_object* json_object_from_file(char *filename)
  48. {
  49. struct printbuf *pb;
  50. struct json_object *obj;
  51. char buf[JSON_FILE_BUF_SIZE];
  52. int fd, ret;
  53. if((fd = open(filename, O_RDONLY)) < 0) {
  54. mc_error("json_object_from_file: error reading file %s: %s\n",
  55. filename, strerror(errno));
  56. return error_ptr(-1);
  57. }
  58. if(!(pb = printbuf_new())) {
  59. mc_error("json_object_from_file: printbuf_new failed\n");
  60. return error_ptr(-1);
  61. }
  62. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  63. printbuf_memappend(pb, buf, ret);
  64. }
  65. close(fd);
  66. if(ret < 0) {
  67. mc_abort("json_object_from_file: error reading file %s: %s\n",
  68. filename, strerror(errno));
  69. printbuf_free(pb);
  70. return error_ptr(-1);
  71. }
  72. obj = json_tokener_parse(pb->buf);
  73. printbuf_free(pb);
  74. return obj;
  75. }
  76. int json_object_to_file(char *filename, struct json_object *obj)
  77. {
  78. char *json_str;
  79. int fd, ret;
  80. unsigned int wpos, wsize;
  81. if(!obj) {
  82. mc_error("json_object_to_file: object is null\n");
  83. return -1;
  84. }
  85. if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  86. mc_error("json_object_to_file: error opening file %s: %s\n",
  87. filename, strerror(errno));
  88. return -1;
  89. }
  90. if(!(json_str = json_object_to_json_string(obj))) { return -1; }
  91. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  92. wpos = 0;
  93. while(wpos < wsize) {
  94. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  95. close(fd);
  96. mc_error("json_object_to_file: error writing file %s: %s\n",
  97. filename, strerror(errno));
  98. return -1;
  99. }
  100. /* because of the above check for ret < 0, we can safely cast and add */
  101. wpos += (unsigned int)ret;
  102. }
  103. close(fd);
  104. return 0;
  105. }

No Description

Contributors (1)