Exemplo n.º 1
0
struct json_object* json_object_from_file(const char *filename)
{
    struct printbuf *pb;
    struct json_object *obj;
    char buf[JSON_FILE_BUF_SIZE];
    int fd, ret;

    if((fd = open(filename, O_RDONLY)) < 0) {
        MC_ERROR("json_object_from_file: error reading file %s: %s\n",
                 filename, strerror(errno));
        return (struct json_object*)error_ptr(-1);
    }
    if(!(pb = printbuf_new())) {
        MC_ERROR("json_object_from_file: printbuf_new failed\n");
        return (struct json_object*)error_ptr(-1);
    }
    while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
        printbuf_memappend(pb, buf, ret);
    }
    close(fd);
    if(ret < 0) {
        MC_ABORT("json_object_from_file: error reading file %s: %s\n",
                 filename, strerror(errno));
        printbuf_free(pb);
        return (struct json_object*)error_ptr(-1);
    }
    obj = json_tokener_parse(pb->buf);
    printbuf_free(pb);
    return obj;
}
Exemplo n.º 2
0
struct mtev_json_object *mtev_json_object_from_fd(int fd)
{
  struct jl_printbuf *pb;
  struct mtev_json_object *obj;
  char buf[JSON_FILE_BUF_SIZE];
  int ret;

  if(!(pb = jl_printbuf_new())) {
    MC_ERROR("mtev_json_object_from_fd: jl_printbuf_new failed%s\n", "");
    return (struct mtev_json_object*)error_ptr(-1);
  }
  while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
    jl_printbuf_memappend(pb, buf, ret);
  }
  if(ret < 0) {
    MC_ABORT("mtev_json_object_from_fd: error reading fd %d: %s\n",
	     fd, strerror(errno));
    jl_printbuf_free(pb);
    return (struct mtev_json_object*)error_ptr(-1);
  }
  obj = mtev_json_tokener_parse(pb->buf);
  jl_printbuf_free(pb);
  return obj;
}