/*
* $Id: json_tokener.c,v 1.20 2006/07/25 03:24:50 mclark Exp $
*
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
* Michael Clark <michael@metaparadigm.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See COPYING for details.
*
*
* Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
* The copyrights to the contents of this file are licensed under the MIT License
* (http://www.opensource.org/licenses/mit-license.php)
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include "bits.h"
#include "debug.h"
#include "printbuf.h"
#include "arraylist.h"
#include "json_object.h"
#include "json_tokener.h"
#if !HAVE_STRNCASECMP && defined(_MSC_VER)
/* MSC has the version as _strnicmp */
# define strncasecmp _strnicmp
#elif !HAVE_STRNCASECMP
# error You do not have strncasecmp on your system.
#endif /* HAVE_STRNCASECMP */
static const char* json_null_str = "null";
static const char* json_true_str = "true";
static const char* json_false_str = "false";
const char* json_tokener_errors[] = {
"success",
"continue",
"nesting to deep",
"unexpected end of data",
"unexpected character",
"null expected",
"boolean expected",
"number expected",
"array value separator ',' expected",
"quoted object property name expected",
"object property name separator ':' expected",
"object value separator ',' expected",
"invalid string sequence",
"expected comment",
};
struct json_tokener* json_tokener_new(void)
{
struct json_tokener *tok;
tok = (struct json_tokener*)calloc(1, sizeof(struct json_tokener));
if (!tok) return NULL;
tok->pb = printbuf_new();
json_tokener_reset(tok);
return tok;
}
void json_tokener_free(struct json_tokener *tok)
{
json_tokener_reset(tok);
if(tok) printbuf_free(tok->pb);
free(tok);
}
static void json_tokener_reset_level(struct json_tokener *tok, int depth)
{
tok->stack[depth].state = json_tokener_state_eatws;
tok->stack[depth].saved_state = json_tokener_state_start;
json_object_put(tok->stack[depth].current);
tok->stack[depth].current = NULL;
free(tok->stack[depth].obj_field_name);
tok->stack[depth].obj_field_name = NULL;
}
void json_tokener_reset(struct json_tokener *tok)
{
int i;
if (!tok)
return;
for(i = tok->depth; i >= 0; i--)
json_tokener_reset_level(tok, i);
tok->depth = 0;
tok->err = json_tokener_success;
}
struct json_object* json_tokener_parse(const char *str)
{
struct json_tokener* tok;
struct json_object* obj;
tok = json_tokener_new();
obj = json_tokener_parse_ex(tok, str, -1);
if(tok->err != json_tokener_success)
obj = (struct json_object*)error_ptr(-tok->err);
json_tokener_free(tok);
return obj;
}
#if !HAVE_STRNDUP
/* CAW: compliant version of strndup() */
char* strndup(const char* str, size_t n)
{
if(str) {
size_t len = strlen(str);
size_t nn = json_min(len,n);
char* s = (char*)malloc(sizeof(char) * (nn + 1));
if(s) {
memcpy(s, str, nn);
s[nn] = '\0';
}
return s;
}
return NULL;
}
#endif
#define state tok->stack[tok->depth].state
#define saved_state tok->stack[tok->depth].saved_state
#define current tok->stack[tok->depth].current
#define obj_field_name tok->stack[tok->depth].obj_field_name
/* Optimization:
* json_tokener_parse_ex() consumed a lot of CPU in its main loop,
* iterating character-by character. A large performance boost is
* achieved by using tighter loops to locally handle units such as
* comments and strings. Loops that handle an entire token within
* their scope also gather entire strings and pass them to
* printbuf_memappend() in a single call, rather than calling
* printbuf_memappend() one char at a time.
*
* POP_CHAR() and ADVANCE_CHAR() macros are used for code that is
* common to both the main loop and the tighter loops.
*/
/* POP_CHAR(dest, tok) macro:
* Not really a pop()...peeks at the current char and stores it in dest.
* Returns 1 on success, sets tok->err and returns 0 if no more chars.
* Implicit inputs: str, len vars
*/
#define POP_CHAR(dest, tok) \
(((tok)->char_offset == len) ? \
(((tok)->depth == 0 && state == json_tokener_state_eatws && saved_state == json_tokener_state_finish) ? \
(((tok)->err = json_tokener_success), 0) \
: \
(((tok)->err = json_tokener_continue), 0) \
) : \
(((dest) = *str), 1) \
)
/* ADVANCE_CHAR() macro:
* Incrementes str & tok->char_offset.
* For convenience of existing conditionals, returns the old value of c (0 on eof)
* Implicit inputs: c var
*/
#define ADVANCE_CHAR(str, tok) \
( ++(str), ((tok)->char_offset)++, c)
/* End optimization macro defs */
struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
const char *str, int len)
{
struct json_object *obj = NULL;
char c = '\1';
tok->char_offset = 0;
tok->err = json_tokener_success;
while (POP_CHAR(c, tok)) {
redo_char:
switch(state) {
case json_tokener_state_eatws:
/* Advance until we change state */
while (isspace(c)) {
if ((!ADVANCE_CHAR(str, tok)) || (!POP_CHAR(c, tok)))
goto out;
}
if(c == '/') {
printbuf_reset(tok->pb);
printbuf_memappend_fast(tok->pb, &c, 1);
state = json_tokener_state_comment_start;
} else {
state = saved_state;
goto redo_char;
}
break;
case json_tokener_state_start:
switch(c) {
case '{':
state = json_tokener_state_eatws;
saved_state = json_tokener_state_object_field_start;
current = json_object_new_object();
break;
case '[':
state = json_tokener_state_eatws;
saved_state = json_tokener_state_array;
current = json_object_new_array();
break;
case 'N':
case 'n':
state = json_tokener_state_null;
printbuf_reset(tok->pb);
tok->st_pos = 0;
goto redo_char;
case '"':
case '\'':
state = json_tokener_state_string;
printbuf_reset(tok->pb);
tok->quote_char = c;
break;
case 'T':
case 't':
case 'F':
case 'f':
state = json_tokener_state_boolean;
printbuf_reset(tok->pb);
tok->st_pos = 0;
goto redo_char;
#if defined(__GNUC__)
case '0' ... '9':
#else
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
#endif
case '-':
state = json_tokener_state_number;
printbuf_reset(tok->pb);
tok->is_double = 0;
goto redo_char;
default:
tok->err = json_tokener_error_parse_unexpected;
goto out;
}
break;
case json_tokener_state_finish:
if(tok->depth == 0) goto out;
obj = json_object_get(current);
json_tokener_reset_level(tok, tok->depth);
tok->depth--;
goto redo_char;
case json_tokener_state_null:
printbuf_memappend_fast(tok->pb, &c, 1);
if(strncasecmp(json_null_str, tok->pb->buf,
json_min(tok->st_pos+1, strlen(json_null_str))) == 0) {
if(tok->st_pos == strlen(json_null_str)) {
current = NULL;
saved_state = json_tokener_state_finish;
state = json_tokener_state_eatws;
goto redo_char;
}
} else {
tok->err = json_tokener_error_parse_null;
goto out;
}
tok->st_pos++;
break;
case json_tokener_state_comment_start:
if(c == '*') {
state = json_tokener_state_comment;
} else if(c == '/') {
state = json_tokener_state_comment_eol;
} else {
tok->err = json_tokener_error_parse_comment;
goto out;
}
printbuf_memappend_fast(tok->pb, &c, 1);
break;
case json_tokener_state_comment:
{
没有合适的资源?快使用搜索试试~ 我知道了~
json-c-0.9.tar.gz
5星 · 超过95%的资源 需积分: 44 444 下载量 198 浏览量
2012-03-21
09:35:36
上传
评论 5
收藏 333KB GZ 举报
温馨提示
共81个文件
html:35个
h:10个
c:10个
网上这个包不是很好找,特找到供大家下载,json的安装包。方便大家也方便自己以后容易找。
资源推荐
资源详情
资源评论
收起资源包目录
json-c-0.9.tar.gz (81个子文件)
json-c-0.9
Makefile.am 847B
debug.c 2KB
Makefile.in 24KB
configure 414KB
json_tokener.h 2KB
json_tokener.c 17KB
README-WIN32.html 3KB
arraylist.h 993B
README.html 2KB
json_object.c 12KB
config.h.in 3KB
linkhash.c 5KB
json_object.h 10KB
linkhash.h 6KB
bits.h 661B
test2.c 769B
ltmain.sh 238KB
printbuf.c 4KB
configure.in 708B
doc
html
json_8h.html 1KB
arraylist_8h.html 10KB
printbuf_8h.html 9KB
classes.html 3KB
structjson__object__iter.html 4KB
bits_8h.html 6KB
globals_eval.html 9KB
doxygen.css 6KB
structarray__list.html 4KB
doxygen.png 1KB
tab_r.gif 3KB
functions_vars.html 9KB
globals_func.html 10KB
globals.html 26KB
json__util_8h.html 4KB
structjson__object.html 7KB
structjson__tokener.html 9KB
structjson__tokener__srec.html 5KB
tab_b.gif 35B
globals_enum.html 2KB
linkhash_8h.html 33KB
debug_8h.html 14KB
structlh__entry.html 5KB
files.html 2KB
globals_type.html 3KB
index.html 1012B
annotated.html 2KB
structprintbuf.html 3KB
config_8h.html 23KB
globals_vars.html 2KB
structlh__table.html 12KB
json__object_8h.html 61KB
json__tokener_8h.html 27KB
json__object__private_8h.html 4KB
unionjson__object_1_1data.html 6KB
globals_defs.html 9KB
tabs.css 2KB
tab_l.gif 706B
functions.html 9KB
ChangeLog 5KB
test1.c 6KB
COPYING 1KB
depcomp 16KB
install-sh 13KB
missing 11KB
config.h.win32 3KB
json.h 573B
json_object_private.h 1KB
INSTALL 9KB
arraylist.c 2KB
test3.c 597B
json_util.h 654B
config.guess 44KB
README 363B
aclocal.m4 308KB
AUTHORS 84B
NEWS 5B
json_util.c 3KB
json.pc.in 217B
printbuf.h 2KB
config.sub 33KB
debug.h 1KB
共 81 条
- 1
小菜菜__
- 粉丝: 16
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页