From c955364a952c5ad5158637b77c75edcf010d49af Mon Sep 17 00:00:00 2001 From: "Serge A. Zaitsev" Date: Mon, 27 Dec 2010 17:00:24 +0200 Subject: [PATCH] All objects renamed using jsmn prefix to keep consistency and not be mixed with other json libraries. Demo is now called just demo. --- Makefile | 19 ++++++++++++------- demo.c | 14 +++++++------- jsmn.c | 26 +++++++++++++------------- jsmn.h | 18 +++++++++--------- test.sh | 2 +- 5 files changed, 42 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index 53a7403..d1f9993 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,22 @@ CFLAGS=-Wall -std=c89 -g -O2 -OBJS=jsmn.o demo.o +all: libjsmn.a -all: jsmn_demo +demo: libjsmn.a demo.o + $(CC) $(LDFLAGS) demo.o -L. -ljsmn -o $@ -jsmn_demo: $(OBJS) - $(CC) $(LDFLAGS) $(OBJS) -o $@ +libjsmn.a: jsmn.o + ar rc $@ $^ %.o: %.c jsmn.h $(CC) -c $(CFLAGS) $< -o $@ +test: all demo + sh test.sh + clean: - rm -f $(OBJS) - rm -f jsmn_demo + rm -f jsmn.o demo.o + rm -f libjsmn.a + rm -f demo -.PHONY: clean all +.PHONY: all clean test demo diff --git a/demo.c b/demo.c index 8880fb7..0de80fa 100644 --- a/demo.c +++ b/demo.c @@ -11,7 +11,7 @@ #include "jsmn.h" -static void jsmn_dump_obj(jsontok_t *obj, const char *js) { +static void jsmn_dump_obj(jsmntok_t *obj, const char *js) { size_t len; char *s; @@ -25,10 +25,10 @@ static void jsmn_dump_obj(jsontok_t *obj, const char *js) { ({ char c; switch (obj->type) { - case JSON_PRIMITIVE: c = '.'; break; - case JSON_STRING: c = 's'; break; - case JSON_ARRAY: c = 'A'; break; - case JSON_OBJECT: c = 'O'; break; + case JSMN_PRIMITIVE: c = '.'; break; + case JSMN_STRING: c = 's'; break; + case JSMN_ARRAY: c = 'A'; break; + case JSMN_OBJECT: c = 'O'; break; default: c = '?'; }; c; })); @@ -57,7 +57,7 @@ int main(int argc, char *argv[]) { jsmn_parser parser; char *js = NULL; - jsontok_t *tokens; + jsmntok_t *tokens; int block_size = 1024; int num_tokens = 100; @@ -96,7 +96,7 @@ int main(int argc, char *argv[]) { } } - tokens = malloc(num_tokens * sizeof(jsontok_t)); + tokens = malloc(num_tokens * sizeof(jsmntok_t)); if (tokens == NULL) { fprintf(stderr, "Cannot allocate anough memory\n"); exit(EXIT_FAILURE); diff --git a/jsmn.c b/jsmn.c index d983be1..d02c2bb 100644 --- a/jsmn.c +++ b/jsmn.c @@ -2,9 +2,9 @@ #include "jsmn.h" -static jsontok_t *jsmn_get_token(jsmn_parser *parser) { +static jsmntok_t *jsmn_get_token(jsmn_parser *parser) { unsigned int i; - jsontok_t *tokens = parser->tokens; + jsmntok_t *tokens = parser->tokens; for (i = parser->curtoken; inum_tokens; i++) { if (tokens[i].start == -1 && tokens[i].end == -1) { parser->curtoken = i; @@ -14,14 +14,14 @@ static jsontok_t *jsmn_get_token(jsmn_parser *parser) { return NULL; } -static void jsmn_fill_token(jsontok_t *token, jsontype_t type, int start, int end) { +static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type, int start, int end) { token->type = type; token->start = start; token->end = end; } void jsmn_init_parser(jsmn_parser *parser, const char *js, - jsontok_t *tokens, unsigned int num_tokens) { + jsmntok_t *tokens, unsigned int num_tokens) { unsigned int i; parser->js = js; @@ -31,13 +31,13 @@ void jsmn_init_parser(jsmn_parser *parser, const char *js, parser->curtoken = 0; for (i = 0; i < parser->num_tokens; i++) { - jsmn_fill_token(&parser->tokens[i], JSON_PRIMITIVE, -1, -1); + jsmn_fill_token(&parser->tokens[i], JSMN_PRIMITIVE, -1, -1); } } static int jsmn_parse_primitive(jsmn_parser *parser) { const char *js; - jsontok_t *token; + jsmntok_t *token; int start; start = parser->pos; @@ -51,7 +51,7 @@ static int jsmn_parse_primitive(jsmn_parser *parser) { token = jsmn_get_token(parser); if (token == NULL) return JSMN_ERROR_NOMEM; - jsmn_fill_token(token, JSON_PRIMITIVE, start, parser->pos); + jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos); parser->pos--; return JSMN_SUCCESS; } @@ -66,7 +66,7 @@ static int jsmn_parse_primitive(jsmn_parser *parser) { static int jsmn_parse_string(jsmn_parser *parser) { const char *js; - jsontok_t *token; + jsmntok_t *token; int start = parser->pos; @@ -83,7 +83,7 @@ static int jsmn_parse_string(jsmn_parser *parser) { token = jsmn_get_token(parser); if (token == NULL) return JSMN_ERROR_NOMEM; - jsmn_fill_token(token, JSON_PRIMITIVE, start+1, parser->pos); + jsmn_fill_token(token, JSMN_PRIMITIVE, start+1, parser->pos); return JSMN_SUCCESS; } @@ -114,8 +114,8 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser) { int r; unsigned int i; const char *js; - jsontype_t type; - jsontok_t *token; + jsmntype_t type; + jsmntok_t *token; js = parser->js; @@ -127,11 +127,11 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser) { token = jsmn_get_token(parser); if (token == NULL) return JSMN_ERROR_NOMEM; - token->type = (c == '{' ? JSON_OBJECT : JSON_ARRAY); + token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY); token->start = parser->pos; break; case '}': case ']': - type = (c == '}' ? JSON_OBJECT : JSON_ARRAY); + type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY); for (i = parser->curtoken; i >= 0; i--) { token = &parser->tokens[i]; if (token->start != -1 && token->end == -1) { diff --git a/jsmn.h b/jsmn.h index a325237..a455696 100644 --- a/jsmn.h +++ b/jsmn.h @@ -9,11 +9,11 @@ * o Other primitive: number, boolean (true/false) or null */ typedef enum { - JSON_PRIMITIVE = 0, - JSON_OBJECT = 1, - JSON_ARRAY = 2, - JSON_STRING = 3 -} jsontype_t; + JSMN_PRIMITIVE = 0, + JSMN_OBJECT = 1, + JSMN_ARRAY = 2, + JSMN_STRING = 3 +} jsmntype_t; typedef enum { JSMN_ERROR_NOMEM = -1, @@ -29,10 +29,10 @@ typedef enum { * @param end end position in JSON data string */ typedef struct { - jsontype_t type; + jsmntype_t type; int start; int end; -} jsontok_t; +} jsmntok_t; /** * JSON parser. Contains an array of token blocks available. Also stores @@ -43,14 +43,14 @@ typedef struct { unsigned int pos; unsigned int num_tokens; int curtoken; - jsontok_t *tokens; + jsmntok_t *tokens; } jsmn_parser; /** * Create JSON parser over an array of tokens */ void jsmn_init_parser(jsmn_parser *parser, const char *js, - jsontok_t *tokens, unsigned int num_tokens); + jsmntok_t *tokens, unsigned int num_tokens); /** * Run JSON parser. It parses a JSON data string into and array of tokens, each describing diff --git a/test.sh b/test.sh index 4db4365..05d52b8 100644 --- a/test.sh +++ b/test.sh @@ -21,7 +21,7 @@ PASSED=0 FAILED=0 function expect() { - ret=$(./jsmn_demo -t 10 -b 256 - | grep -A 1 "$1" | tail -n 1 | cut -c 15-) + ret=$(./demo -t 10 -b 256 - | grep -A 1 "$1" | tail -n 1 | cut -c 15-) if [ "x$ret" = "x$2" ]; then PASSED=$(($PASSED+1)) else