summaryrefslogtreecommitdiff
path: root/jsmn.c
diff options
context:
space:
mode:
authorSerge A. Zaitsev <devnull@localhost>2012-02-01 13:56:06 +0200
committerSerge A. Zaitsev <devnull@localhost>2012-02-01 13:56:06 +0200
commitbed0a7a3e647abb7204d73c69f9f44b37bdfc84f (patch)
tree0c4fd814c90270eac4370d2e4def493dc85d1c1b /jsmn.c
parent4b5c5ed66ac35f50b6c172a712883acb5f0cc62f (diff)
changed API: parse now is more flexible, but init jsut resets the parser; added new test macro to compate strings, fixed Makefile
Diffstat (limited to 'jsmn.c')
-rw-r--r--jsmn.c42
1 files changed, 19 insertions, 23 deletions
diff --git a/jsmn.c b/jsmn.c
index 04404a7a6..5554c8f8f 100644
--- a/jsmn.c
+++ b/jsmn.c
@@ -29,26 +29,6 @@ static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
}
/**
- * 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) {
@@ -132,14 +112,20 @@ static int jsmn_parse_string(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;
@@ -199,3 +185,13 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser) {
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;
+}
+