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.8 kB

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

No Description

Contributors (1)