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 2.7 kB

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

No Description

Contributors (1)