示例#1
0
文件: RJSON.c 项目: cran/RJSONIO
void
R_json_parse_connection(SEXP r_input, SEXP numLines, struct JSON_parser_struct *parser)
{
    const char *input;
    unsigned int count = 0, len, totalCount = 0, lineCount = 0;
    SEXP call, ans;
    int n, i, maxNumLines;

    PROTECT(call = allocVector(LANGSXP, 3));
    SETCAR(call, Rf_install("readLines"));
    SETCAR(CDR(call), r_input);
    SETCAR(CDR(CDR(call)), ScalarInteger(1));

    maxNumLines = INTEGER(numLines)[0];

    while(1) {

      PROTECT(ans =  Rf_eval(call, R_GlobalEnv));
      n = Rf_length(ans);
      lineCount += n;

      if(n == 0) {
	  UNPROTECT(1);
	  break;
      }

      for(i = 0 ; i < n ; i++) {
	input = CHAR(STRING_ELT(ans, i));
	len = strlen(input);
	for (count = 0; count < len ; ++count, ++totalCount) {
	    int next_char = input[count];
	    if (next_char <= 0) {
		break;
	    }
	    if (!JSON_parser_char(parser, next_char)) {
		delete_JSON_parser(parser);
		PROBLEM "JSON parser error: syntax error, byte %d (%c)", totalCount, input[count]
		    ERROR;
	    }
	}
      }
      UNPROTECT(1);

      if(maxNumLines > 0 && lineCount == maxNumLines)
	  break;
    }

    UNPROTECT(1);
    if (!JSON_parser_done(parser)) {
	delete_JSON_parser(parser);
	PROBLEM "JSON parser error: syntax error, incomplete content" 
	    ERROR;
    }
}
示例#2
0
文件: json.c 项目: duncantl/RJSONIO
int main(int argc, char* argv[]) {
    int count = 0;
    FILE* input;
        
    JSON_config config;

    struct JSON_parser_struct* jc = NULL;
    
    init_JSON_config(&config);
    
    config.depth                  = 20;
    config.callback               = &print;
    config.allow_comments         = 1;
    config.handle_floats_manually = 0;
    
    /* Important! Set locale before parser is created.*/
    if (argc >= 2) {
        if (!setlocale(LC_ALL, argv[1])) {
            fprintf(stderr, "Failed to set locale to '%s'\n", argv[1]);
        }
    } else {
        fprintf(stderr, "No locale provided, C locale is used\n");
    }
    
    jc = new_JSON_parser(&config);
    
    input = stdin;
    for (; input ; ++count) {
        int next_char = fgetc(input);
	if(count == 1601)
	    fprintf(stderr, "1061\n");
        if (next_char <= 0) {
            break;
        }
fprintf(stderr, "%d) %c %u\n", count, next_char, (unsigned int) next_char);fflush(stderr);
        if (!JSON_parser_char(jc, next_char)) {
            delete_JSON_parser(jc);
            fprintf(stderr, "JSON_parser_char: syntax error, byte %d\n", count);
            return 1;
        }
    }
    if (!JSON_parser_done(jc)) {
        delete_JSON_parser(jc);
        fprintf(stderr, "JSON_parser_end: syntax error\n");
        return 1;
    }
    
    return 0;
}
示例#3
0
void douban_radio_parse(struct douban_radio* douban, const char* buffer, rt_size_t length)
{
	JSON_config config;
	struct JSON_parser_struct* jc = NULL;
	const char* ptr;

	init_JSON_config(&config);
    config.depth                  = 19;
    config.callback               = &_parse_callback;
	config.callback_ctx           = douban;
    config.allow_comments         = 1;
    config.handle_floats_manually = 0;

	jc = new_JSON_parser(&config);
	ptr = buffer;
	while (ptr < buffer + length)
	{
		if (!JSON_parser_char(jc, *ptr++))
		{
			// rt_kprintf("JSON_parser_error: parse failed\n");
			break;
		}
	}

	if (!JSON_parser_done(jc))
	{
		// rt_kprintf("JSON_parser_end: syntax error\n");
	}

	delete_JSON_parser(jc);
}
示例#4
0
void createPTreeFromJsonString(const char *json, bool caseInsensitive, StringBuffer &xml, const char *tail)
{
    int count = 0, result = 0;
        
    JSonToXmlStringContext jcontext(xml, tail);
    
    struct JSON_parser_struct* jc = NULL;

    JSON_config config;
    init_JSON_config(&config);
    
    config.depth                  = 19;
    config.callback               = &ptree_builder;
    config.allow_comments         = 1;
    config.handle_floats_manually = 0;
    config.callback_ctx = &jcontext;
    
    /* Important! Set locale before parser is created.*/
    setlocale(LC_ALL, "english");
    
    jc = new_JSON_parser(&config);
    
    const char *finger = json;
    for (; *finger ; finger++) {
        if (!JSON_parser_char(jc, *finger)) {
            throw MakeStringException(-1, "JSON_parser_char: syntax error, byte %d\n", (int) (finger - json));
        }
    }
    if (!JSON_parser_done(jc)) {
        throw MakeStringException(-1, "JSON_parser_end: syntax error\n");
    }

    //TBD: worry about exception cleanup later
    delete_JSON_parser(jc);
}
示例#5
0
文件: RJSON.c 项目: cran/RJSONIO
void
R_json_parse_character(SEXP r_input, SEXP maxChar, struct JSON_parser_struct *parser)
{
    const char *input;
    int *ivals = NULL;
    unsigned int count = 0, len;

    int next_char;


    count = INTEGER(maxChar)[0];
    len = INTEGER(maxChar)[1];

    if(TYPEOF(r_input) == RAWSXP)
	input = RAW(r_input);
    else if(TYPEOF(r_input) == INTSXP) {
	ivals = INTEGER(r_input);
    } else
	input = CHAR(STRING_ELT(r_input, 0));

    for (; count < len ; ++count) {

	if(ivals)
	    next_char = ivals[count];
	else
	    next_char = input[count];

        if (next_char <= 0) {
            break;
        }
	/* fprintf(stderr, "%d) %c %u\n", count, next_char, (unsigned int) next_char);fflush(stderr); */
        if (!JSON_parser_char(parser, next_char)) {
            delete_JSON_parser(parser);
            PROBLEM "JSON parser error: syntax error, byte %d (%c)\n", count, input[count]
	    ERROR;
        }
    }
    if (!JSON_parser_done(parser)) {
        delete_JSON_parser(parser);
	PROBLEM "JSON parser error: syntax error, byte %d\n", count
	ERROR;
    }
}
示例#6
0
ham_status_t
config_parse_string(const char *string, config_table_t **params)
{
    unsigned count=0;
    JSON_config config;
    struct JSON_parser_struct *jc=0;
    config_table_t *p=(config_table_t *)calloc(sizeof(config_table_t), 1);
    if (!p)
        return (HAM_OUT_OF_MEMORY);

    *params=0;

    init_JSON_config(&config);
    config.depth=20;
    config.callback=__parser_cb;
    config.callback_ctx=(void *)p;
    config.allow_comments=1;
    config.handle_floats_manually=1;

    jc=new_JSON_parser(&config);
    while (*string) {
        if (!JSON_parser_char(jc, *string)) {
            delete_JSON_parser(jc);
            hlog(3, "JSON syntax error in byte %u\n", count);
            config_clear_table(p);
            return (HAM_INV_PARAMETER);
        }
        count++;
        string++;
    }

    if (!JSON_parser_done(jc)) {
        delete_JSON_parser(jc);
        config_clear_table(p);
        return (HAM_INV_PARAMETER);
    }

    *params=p;
    delete_JSON_parser(jc);

    return (0);
}
/**
 * Process a JSON object that we got from the burrow server.
 * Presently intended to be called on a complete JSON thingie. In future
 * perhaps this should be extended so it can be called on an incomplete
 * JSON response, allowing us to start parsing before we get the entire
 * message.
 *
 * @param backend the http backend
 * @param jsontext the actual JSON text we got from the burrow server
 * @param jsonsize the size of the JSON text we got from the burrow server
 * @return 0 if successful, otherwise errno value if it fails.
 */
int
burrow_backend_http_parse_json(burrow_backend_t *backend,
			       char *jsontext,
			       size_t jsonsize)
{
  JSON_config config;
  struct JSON_parser_struct* jc = NULL;
  init_JSON_config(&config);
  
  json_processing_t *json_processing = burrow_easy_json_st_create(backend);

  config.depth                  = 19;
  config.callback               = &burrow_backend_http_json_callback;
  config.callback_ctx		= json_processing;
  config.allow_comments         = 1;
  config.handle_floats_manually = 0;
  jc = new_JSON_parser(&config);

  int i;
  for (i = 0; i < (int)jsonsize; ++i) {
    int retval;
    int nextchar = jsontext[i];
    if ((retval = JSON_parser_char(jc, nextchar)) <= 0) {
      burrow_error(burrow_backend_http_get_burrow(backend),
		   EINVAL,
		   "WARNING! JSON_parser_char (%d) at byte %d (%d = '%c')\n",
		   retval, i, (int)nextchar, nextchar);
      return EINVAL;
    }
  }    
  if (!JSON_parser_done(jc)) {
    burrow_error(burrow_backend_http_get_burrow(backend),
		 EINVAL,
		 "WARNING! JSON_parser_end indicates JSON syntax error\n");
    delete_JSON_parser(jc);
    burrow_easy_json_st_destroy(json_processing);
    return EINVAL;
  }
  delete_JSON_parser(jc);
  burrow_easy_json_st_destroy(json_processing);
  return 0;
}
示例#8
0
int
tr_jsonParse( const void     * vbuf,
              size_t           len,
              tr_benc        * setme_benc,
              const uint8_t ** setme_end )
{
    int                         line = 1;
    int                         column = 1;
    int                         err = 0;
    const unsigned char       * buf = vbuf;
    const void                * bufend = buf + len;
    struct JSON_config_struct   config;
    struct JSON_parser_struct * checker;
    struct json_benc_data       data;

    init_JSON_config( &config );
    config.callback = callback;
    config.callback_ctx = &data;
    config.depth = -1;

    data.hasContent = FALSE;
    data.key = NULL;
    data.top = setme_benc;
    data.stack = TR_PTR_ARRAY_INIT;

    checker = new_JSON_parser( &config );
    while( ( buf != bufend ) && JSON_parser_char( checker, *buf ) ) {
        if( *buf != '\n' )
            ++column;
        else {
            ++line;
            column = 1;
        }
        ++buf;
    }

    if( buf != bufend ) {
        tr_err( "JSON parser failed at line %d, column %d: \"%.16s\"", line, column, buf );
        err = EILSEQ;
    }

    if( !data.hasContent )
        err = EINVAL;

    if( setme_end )
        *setme_end = (const uint8_t*) buf;

    delete_JSON_parser( checker );
    tr_ptrArrayDestruct( &data.stack, NULL );
    return err;
}
示例#9
0
文件: json.c 项目: 3l13/APE_Server
json_item *init_json_parser(const char *json_string)
{
	const char *pRaw;
	JSON_config config;

	struct JSON_parser_struct* jc = NULL;
	
	json_context jcx = {0, 0, NULL, NULL};

	init_JSON_config(&config);
	
	config.depth		= 15;
	config.callback		= &json_callback;
	config.callback_ctx	= &jcx;
	
	config.allow_comments	= 0;
	config.handle_floats_manually = 0;

	jc = new_JSON_parser(&config);

	for (pRaw = json_string; *pRaw; pRaw++) {
		if (!JSON_parser_char(jc, *pRaw)) {
			free_json_item(jcx.head);
		    delete_JSON_parser(jc);
		    return NULL;
		}
	}
	
	if (!JSON_parser_done(jc)) {
		free_json_item(jcx.head);
		delete_JSON_parser(jc);
		return NULL;
	}

	delete_JSON_parser(jc);
	
	return jcx.head;	
}
示例#10
0
void
config_destroy(void)
{
	if (pandora->config.log_file) {
		free(pandora->config.log_file);
		pandora->config.log_file = NULL;
	}
	if (pandora->config.state) {
		free(pandora->config.state);
		pandora->config.state = NULL;
	}
	if (pandora->config.parser) {
		delete_JSON_parser(pandora->config.parser);
		pandora->config.parser = NULL;
	}
}
示例#11
0
文件: json.c 项目: fangang190/canary
int
tr_jsonParse( const void *     vbuf,
              size_t           len,
              tr_benc *        setme_benc,
              const uint8_t ** setme_end )
{
    int                         err = 0;
    const unsigned char *       buf = vbuf;
    const void *                bufend = buf + len;
    struct JSON_config_struct   config;
    struct JSON_parser_struct * checker;
    struct json_benc_data       data;

    init_JSON_config( &config );
    config.callback = callback;
    config.callback_ctx = &data;
    config.depth = -1;

    data.key = NULL;
    data.top = setme_benc;
    data.stack = tr_ptrArrayNew( );

    checker = new_JSON_parser( &config );
    while( ( buf != bufend ) && JSON_parser_char( checker, *buf ) )
        ++buf;

    if( buf != bufend )
        err = EILSEQ;

    if( setme_end )
        *setme_end = (const uint8_t*) buf;

    delete_JSON_parser( checker );
    tr_ptrArrayFree( data.stack, NULL );
    return err;
}
示例#12
0
/**
 * @brief initialize 
 * 初始化libhttp.so插件
 * 在该函数内执行初始化插件级别参数结构体
 * 比如:
 * 读取用户指定的配置文件
 * 分析该配置文件(可以用json格式)
 * 生成http_conft_st结构体
 * 并将该结构体指针赋值给bench_t的param元素
 * 以后init(),act(),end()都可以通过指针得到该元素
 * 从而完成了插件级别业务参数的传递
 *
 * @param p
 *
 * @return 
 */
void* initialize(void *p)
{
	int scenarioId = 0;
    pmw = (middleware_t*)p;
    bench_t *pb = pmw->bp;
	char conf[1024] = {0};
	char conf_content[10240] = {0};
	FILE *conf_file;

	/*
	 * 性能测试的时候
	 * 类似http这种协议
	 * 会建立非常多的句柄fd
	 * 因此需要设置ulimit
	 *
	 * 每个用户都有最大的ulimit open files限制
	 * 必须使用root帐户更改最大数量
	 * 命令如下:
	 * [root:] ulimit -n 1000000
	 */

	/*
	 *	pb指向的bench_t结构体是处于mmap空间的
	 *	所以需要通过cbutil中的strpcpy方法来复制字符串
	 *	而不能使用strcpy等方法
	 * */
	pmalloc(sizeof(http_conf_st), &conf_st);
	pb->param = conf_st;

	/*
	 * 初始化json-parser
	 */
	JSON_config config;

    struct JSON_parser_struct* jc = NULL;
    
    init_JSON_config(&config);
    
    config.depth                  = 19;
    config.callback               = &cfg_cb;
    config.allow_comments         = 1;
    config.handle_floats_manually = 0;

	jc = new_JSON_parser(&config);

	/* 
	 *	初始化curl
	 *	该函数可能线程不安全
	 *	在curl官网有描述
	 *	但在官网的这个例子里面可以在下面这个多线程代码中使用:
	 *	http://curl.haxx.se/libcurl/c/multithread.html
	 * */
	/*
	 * curl太慢,不如自己实现get和post
	 */
	//curl_global_init(CURL_GLOBAL_ALL);

	printf("Config file[default \"../../conf/http.conf\"]:\n");
	conf[0] = (char)fgetc(stdin);
	/* 
	 * 	如够只输入回车
	 *	则读取默认conf文件位置
	 * */
	if(conf[0] == '\n' )
		strcpy(conf, "../../conf/http.conf");
	else{
		scanf("%s",conf+1);
		printf("\n");
	}

	/* 
	 *	读入配置文件内容
	 *	解析配置
	 * */
	if((conf_file = fopen(conf,"r")) == 0)
		fprintf(stderr,"Can't read %s",conf);
	else{
		int char_buf;
		while((char_buf = fgetc(conf_file)) > 0){
				if (!JSON_parser_char(jc, char_buf)) {
						fprintf(stderr, "JSON_parser_char: syntax error\n");
						exit(-1);
				}
		}
		if (!JSON_parser_done(jc)) {
				fprintf(stderr, "JSON_parser_end: syntax error\n");
				exit(-1);
		}
		fclose(conf_file);
	}
	delete_JSON_parser(jc);
}