1. Serge Zaitsev
  2. jsmn

Commits

Serge A. Zaitsev  committed c8ff928

All objects renamed using jsmn prefix to keep consistency and not be mixed with other json libraries. Demo is now called just demo.

  • Participants
  • Parent commits 555ce4f
  • Branches default

Comments (0)

Files changed (5)

File Makefile

View file
 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

File demo.c

View file
 
 #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;
 
 		({
 			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;
 		}));
 
 	jsmn_parser parser;
 	char *js = NULL;
-	jsontok_t *tokens;
+	jsmntok_t *tokens;
 	int block_size = 1024;
 	int num_tokens = 100;
 
 		}
 	}
 
-	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);

File jsmn.c

View file
 
 #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; i<parser->num_tokens; i++) {
 		if (tokens[i].start == -1 && tokens[i].end == -1) {
 			parser->curtoken = i;
 	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;
 	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;
 				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;
 		}
 
 static int jsmn_parse_string(jsmn_parser *parser) {
 	const char *js;
-	jsontok_t *token;
+	jsmntok_t *token;
 
 	int start = parser->pos;
 
 			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;
 		}
 
 	int r;
 	unsigned int i;
 	const char *js;
-	jsontype_t type;
-	jsontok_t *token;
+	jsmntype_t type;
+	jsmntok_t *token;
 
 	js = parser->js;
 
 				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) {

File jsmn.h

View file
  * 	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,
  * @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
 	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

File test.sh

View file
 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