コード例 #1
0
ファイル: printbuf.c プロジェクト: robertqingsong/jsonal
int sprintbuf(struct printbuf *p, const char *msg, ...)
{
  va_list ap;
  char *t;
  int size;
  char buf[128];

  /* user stack buffer first */
  va_start(ap, msg);
  size = vsnprintf(buf, 128, msg, ap);
  va_end(ap);
  /* if string is greater than stack buffer, then use dynamic string
     with vasprintf.  Note: some implementation of vsnprintf return -1
     if output is truncated whereas some return the number of bytes that
     would have been written - this code handles both cases. */
  if(size == -1 || size > 127) {
    va_start(ap, msg);
    if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
    va_end(ap);
    printbuf_memappend(p, t, size);
    free(t);
    return size;
  } else {
    printbuf_memappend(p, buf, size);
    return size;
  }
}
コード例 #2
0
static char* json_escape_str(char** presult, char *str)
{
	int result_pos = 0;
	int pos = 0, start_offset = 0;
	unsigned char c;

	(*presult) = palloc(strlen(str) * 6);
	(*presult)[0] = 0;
	do {
		c = str[pos];
		switch(c) {
		case '\0':
			break;
		case '\b':
		case '\n':
		case '\r':
		case '\t':
		case '"':
		case '\\':
//		case '/':
			if(pos - start_offset > 0)
			{
				printbuf_memappend(*presult, &result_pos, str + start_offset, pos - start_offset);
			}
			if(c == '\b')
			{
				printbuf_memappend(*presult, &result_pos, "\\b", 2);
			}
			else if(c == '\n')
			{
				printbuf_memappend(*presult, &result_pos, "\\n", 2);
			}
			else if(c == '\r')
			{
				printbuf_memappend(*presult, &result_pos, "\\r", 2);
			}
			else if(c == '\t') printbuf_memappend(*presult, &result_pos, "\\t", 2);
			else if(c == '"') printbuf_memappend(*presult, &result_pos, "\\\"", 2);
			else if(c == '\\') printbuf_memappend(*presult, &result_pos, "\\\\", 2);
//			else if(c == '/') printbuf_memappend(*presult, &result_pos, "\\/", 2);
			start_offset = ++pos;
			break;
		default:
			if(c < ' ') {
				if(pos - start_offset > 0)
					printbuf_memappend(*presult, &result_pos, str + start_offset, pos - start_offset);
				sprintf((*presult)+result_pos, "\\u00%c%c",
					json_hex_chars[c >> 4],
					json_hex_chars[c & 0xf]);
				result_pos += 6;
				(*presult)[result_pos] = '\0';
				start_offset = ++pos;
			} else pos++;
		}
	} while(c);
	if(pos - start_offset > 0)
		printbuf_memappend(*presult, &result_pos, str + start_offset, pos - start_offset);
	return *presult;
}
コード例 #3
0
ファイル: json_util.c プロジェクト: tjyang/abmon
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;
}
コード例 #4
0
ファイル: ogrgmejson.cpp プロジェクト: AbdelghaniDr/mirror
static int json_gme_double_to_string(json_object *pjo,
                                     printbuf *pb,
                                     CPL_UNUSED int level,
                                     CPL_UNUSED int flags)
{
  char buf[128], *p, *q;
  int size;

  size = CPLsnprintf(buf, 128, "%.8f", json_object_get_double(pjo));
  p = strchr(buf, ',');
  if (p) {
    *p = '.';
  } else {
    p = strchr(buf, '.');
  }
  if (p) {
    /* last useful digit, always keep 1 zero */
    p++;
    for (q=p ; *q ; q++) {
      if (*q!='0') p=q;
    }
    /* drop trailing zeroes */
    *(++p) = 0;
    size = p-buf;
  }
  printbuf_memappend(pb, buf, size);
  return size;
}
コード例 #5
0
static int OGR_json_double_with_precision_to_string(struct json_object *jso,
                                                    struct printbuf *pb,
                                                    CPL_UNUSED int level,
                                                    CPL_UNUSED int flags)
{
    char szBuffer[75];
    int nPrecision = (int) (size_t) jso->_userdata;
    OGRFormatDouble( szBuffer, sizeof(szBuffer), jso->o.c_double, '.',
                     (nPrecision < 0) ? 15 : nPrecision );
    if( szBuffer[0] == 't' /*oobig */ )
    {
        CPLsnprintf(szBuffer, sizeof(szBuffer), "%.18g", jso->o.c_double);
    }
    return printbuf_memappend(pb, szBuffer, strlen(szBuffer)); 
}
コード例 #6
0
ファイル: net_json.c プロジェクト: bluecube/bonsai-raytracer
/**
 * Read a line from a socket to a print buffer buf.
 * Calls error() in case of any problems.
 */
static void read_line(int fd, struct printbuf *buf){
	printbuf_reset(buf);

	char c;
	do{
		ssize_t status = read(fd, &c, 1);
		
		if(status == -1){
			error(errno, NULL);
		}else if(status < 1){
			error(0, "Remote side closed the connection up.");
		}

		if(printbuf_memappend(buf, &c, 1) == -1){
			error(errno, NULL);
		}
	}while(c != '\n');
}
コード例 #7
0
/*
 * Create a JSON object from already opened file descriptor.
 *
 * This function can be helpful, when you opened the file already,
 * e.g. when you have a temp file.
 * Note, that the fd must be readable at the actual position, i.e.
 * use lseek(fd, 0, SEEK_SET) before.
 */
struct json_object* json_object_from_fd(int fd)
{
  struct printbuf *pb;
  struct json_object *obj;
  char buf[JSON_FILE_BUF_SIZE];
  int ret;

  if(!(pb = printbuf_new())) {
    MC_ERROR("json_object_from_file: printbuf_new failed\n");
    return NULL;
  }
  while((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
    printbuf_memappend(pb, buf, ret);
  }
  if(ret < 0) {
    MC_ERROR("json_object_from_fd: error reading fd %d: %s\n", fd, strerror(errno));
    printbuf_free(pb);
    return NULL;
  }
  obj = json_tokener_parse(pb->buf);
  printbuf_free(pb);
  return obj;
}
コード例 #8
0
SharedPreferences* tg_shared_preferences_open(const CHAR* path,SharedPreferences_Open_Mode mode)
{

    struct json_object *obj;

    UINT32 ret;
    TG_FILE * fp = NULL;
    SharedPreferences* sp = NULL;
    BOOL new_file = FALSE;
    return_val_if_fail(path,NULL);
    if (mode == SharedPreferences_ReadWrite_Mode)  //if already lock, can not open it with write mode
    {
        if (tg_shared_preferences_is_locked(path))
            return NULL;
        if (!tg_shared_preferences_is_ready(path))
        {
            fp =tg_fopen(path, "w+");
            return_val_if_fail(fp,NULL);
            tg_fclose(fp);
            new_file = TRUE;
        }
    }
    if (!new_file)
    {
        struct printbuf *pb;
        CHAR* buf=NULL;
        fp =tg_fopen(path, "r");
        return_val_if_fail((fp),NULL);

        if (!(pb = printbuf_new()))
        {
            tg_fclose(fp);
            return NULL;
        }
        buf =TG_MALLOC(SHARED_PREFERENCE_FILE_BUF_SIZE);
        while ((ret = tg_fread(buf, SHARED_PREFERENCE_FILE_BUF_SIZE,1,fp)) > 0)
        {
            printbuf_memappend(pb, buf, ret);
        }
        TG_FREE(buf);
        tg_fclose(fp);

        obj = json_tokener_parse(pb->buf);
        printbuf_free(pb);
    }
    else
    {
        obj = json_object_new_object();
    }
    sp = TG_CALLOC_V2(sizeof(SharedPreferences));
    return_val_if_fail((sp),NULL);
    sp->obj=obj;
    sp->mode = mode;
    if (mode == SharedPreferences_ReadWrite_Mode)
    {
        sp->path = TG_CALLOC_V2(strlen(path)+1);
        strcpy(sp->path ,path);
        tg_shared_preferences_lock(path,TRUE);

    }
    return sp;
}