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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <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. #if !HAVE_OPEN && defined(WIN32)
  35. # define open _open
  36. #endif
  37. #include "bits.h"
  38. #include "debug.h"
  39. #include "printbuf.h"
  40. #include "json_object.h"
  41. #include "json_tokener.h"
  42. #include "json_util.h"
  43. struct json_object* json_object_from_file(char *filename)
  44. {
  45. struct printbuf *pb;
  46. struct json_object *obj;
  47. char buf[JSON_FILE_BUF_SIZE];
  48. int fd, ret;
  49. if((fd = open(filename, O_RDONLY)) < 0) {
  50. mc_error("json_object_from_file: error reading file %s: %s\n",
  51. filename, strerror(errno));
  52. return error_ptr(-1);
  53. }
  54. if(!(pb = printbuf_new())) {
  55. mc_error("json_object_from_file: printbuf_new failed\n");
  56. return error_ptr(-1);
  57. }
  58. while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
  59. printbuf_memappend(pb, buf, ret);
  60. }
  61. close(fd);
  62. if(ret < 0) {
  63. mc_abort("json_object_from_file: error reading file %s: %s\n",
  64. filename, strerror(errno));
  65. printbuf_free(pb);
  66. return error_ptr(-1);
  67. }
  68. obj = json_tokener_parse(pb->buf);
  69. printbuf_free(pb);
  70. return obj;
  71. }
  72. int json_object_to_file(char *filename, struct json_object *obj)
  73. {
  74. char *json_str;
  75. int fd, ret;
  76. unsigned int wpos, wsize;
  77. if(!obj) {
  78. mc_error("json_object_to_file: object is null\n");
  79. return -1;
  80. }
  81. if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0) {
  82. mc_error("json_object_to_file: error opening file %s: %s\n",
  83. filename, strerror(errno));
  84. return -1;
  85. }
  86. if(!(json_str = json_object_to_json_string(obj))) { return -1; }
  87. wsize = (unsigned int)(strlen(json_str) & UINT_MAX); /* CAW: probably unnecessary, but the most 64bit safe */
  88. wpos = 0;
  89. while(wpos < wsize) {
  90. if((ret = write(fd, json_str + wpos, wsize-wpos)) < 0) {
  91. close(fd);
  92. mc_error("json_object_to_file: error writing file %s: %s\n",
  93. filename, strerror(errno));
  94. return -1;
  95. }
  96. /* because of the above check for ret < 0, we can safely cast and add */
  97. wpos += (unsigned int)ret;
  98. }
  99. close(fd);
  100. return 0;
  101. }

No Description

Contributors (1)