Commits

Anonymous committed c3427ad

changed API: parse now is more flexible, but init jsut resets the parser; added new test macro to compate strings, fixed Makefile

Comments (0)

Files changed (4)

 
 all: libjsmn.a 
 
-#demo: libjsmn.a demo.o
-#	$(CC) $(LDFLAGS) demo.o -L. -ljsmn -o $@
-
 libjsmn.a: jsmn.o
 	$(AR) rc $@ $^
 
 	$(CC) -L. -ljsmn $< -o $@
 
 clean:
-	rm -f jsmn.o demo.o
+	rm -f jsmn.o jsmn_test.o
+	rm -f jsmn_test
 	rm -f libjsmn.a
-	rm -f demo
 
-.PHONY: all clean test demo
+.PHONY: all clean test
 
 }
 
 /**
- * Creates a new parser based over a given  buffer with an array of tokens 
- * available.
- */
-void jsmn_init_parser(jsmn_parser *parser, const char *js, 
-                      jsmntok_t *tokens, unsigned int num_tokens) {
-	unsigned int i;
-
-	parser->js = js;
-	parser->pos = 0;
-	parser->tokens = tokens;
-	parser->num_tokens = num_tokens;
-	parser->curtoken = 0;
-	parser->cursize = NULL;
-
-	for (i = 0; i < parser->num_tokens; i++) {
-		jsmn_fill_token(&parser->tokens[i], JSMN_PRIMITIVE, -1, -1);
-	}
-}
-
-/**
  * Fills next available token with JSON primitive.
  */
 static int jsmn_parse_primitive(jsmn_parser *parser) {
 /**
  * Parse JSON string and fill tokens.
  */
-jsmnerr_t jsmn_parse(jsmn_parser *parser) {
+jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, jsmntok_t *tokens, 
+		unsigned int num_tokens) {
 	int r;
 	int i;
-	const char *js;
 	jsmntype_t type;
 	jsmntok_t *token;
 
-	js = parser->js;
+	/* initialize the rest of tokens (they could be reallocated) */
+	parser->num_tokens = num_tokens;
+	parser->tokens = tokens;
+	parser->js = js;
+	for (i = parser->curtoken; i < parser->num_tokens; i++) {
+		jsmn_fill_token(&parser->tokens[i], JSMN_PRIMITIVE, -1, -1);
+	}
 
 	for (; js[parser->pos] != '\0'; parser->pos++) {
 		char c;
 	return JSMN_SUCCESS;
 }
 
+/**
+ * Creates a new parser based over a given  buffer with an array of tokens 
+ * available.
+ */
+void jsmn_init(jsmn_parser *parser) {
+	parser->pos = 0;
+	parser->curtoken = 0;
+	parser->cursize = NULL;
+}
+
 } jsmntype_t;
 
 typedef enum {
+	/* Not enough tokens were provided */
 	JSMN_ERROR_NOMEM = -1,
+	/* Invalid character inside JSON string */
 	JSMN_ERROR_INVAL = -2,
+	/* The string is not a full JSON packet, more bytes expected */
 	JSMN_ERROR_PART = -3,
+	/* Everything was fine */
 	JSMN_SUCCESS = 0
 } jsmnerr_t;
 
 /**
  * Create JSON parser over an array of tokens
  */
-void jsmn_init_parser(jsmn_parser *parser, const char *js, 
-                      jsmntok_t *tokens, unsigned int num_tokens);
+void jsmn_init(jsmn_parser *parser);
 
 /**
  * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
  * a single JSON object.
  */
-jsmnerr_t jsmn_parse(jsmn_parser *parser);
+jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, 
+		jsmntok_t *tokens, unsigned int num_tokens);
 
 #endif /* __JSMN_H_ */
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
 #include "jsmn.c"
 
 	 && (t).end == tok_end  \
 	 && (t).type == (tok_type))
 
+#define TOKEN_STIRNG(js, t, s) \
+	(strncmp(js+(t).start, s, (t).end - (t).start) == 0 \
+	 && strlen(s) == (t).end - (t).start)
+
 #define TOKEN_PRINT(t) \
 	printf("start: %d, end: %d, type: %d\n", (t).start, (t).end, (t).type)
 
 
 
-int test_primitive() {
+int test_simple() {
+	const char *js;
 	int r;
 	jsmn_parser p;
 	jsmntok_t tokens[10];
 
-	jsmn_init_parser(&p, "{\"a\": 0}", tokens, 10);
+	js = "{\"a\": 0}";
 
-	r = jsmn_parse(&p);
+	jsmn_init(&p);
+	r = jsmn_parse(&p, js, tokens, 10);
 	check(r == JSMN_SUCCESS);
 	check(TOKEN_EQ(tokens[0], 0, 8, JSMN_OBJECT));
 	check(TOKEN_EQ(tokens[1], 2, 3, JSMN_STRING));
 	check(TOKEN_EQ(tokens[2], 6, 7, JSMN_PRIMITIVE));
 
+	check(TOKEN_STIRNG(js, tokens[0], js));
+	check(TOKEN_STIRNG(js, tokens[1], "a"));
+	check(TOKEN_STIRNG(js, tokens[2], "0"));
+
 	return 0;
 }
 
+int test_primitive() {
+	jsmn_parser p;
+	jsmntok_t tokens[10];
+	const char *js;
+}
+
 int main() {
-	test(test_primitive, "test primitive values");
+	test(test_simple, "general test for a simple JSON string");
 	return 0;
 }
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.