diff options
Diffstat (limited to 'src/json.c')
-rw-r--r-- | src/json.c | 18 |
1 files changed, 11 insertions, 7 deletions
@@ -10,9 +10,9 @@ struct dc_json { /* does not care about syntax, only purpose is to detect object char backup; /* internal we store byte we overwrote with \0 when we were ready */ int ready; /* internal we indicate to the next call that we were ready previous time */ }; /* note that no memory is transfered. in is copied and return mustn't be freed. */ -char * dc_json (struct dc_json * j, char * in) { /* detects start/end of a cat objects JSON stream */ - size_t i; /* input a null terminated string - a chunk of the json stream */ - if (!j->buf) +char * dc_json (struct dc_json * j, const char * in, int ln) { /* detects start/end in JSON stream */ + size_t i; /* input a null terminated string and ln==-1 - a chunk of the json stream */ + if (!j->buf) /* of if you know the length or string is not null terminated, set ln. */ (j->buf = malloc((j->bufcap = 1024) * sizeof(char)))[0] = '\0'; if (j->ready) { if (j->ready > 0) @@ -20,12 +20,13 @@ char * dc_json (struct dc_json * j, char * in) { /* detects start/end of a cat o j->buf[0] = j->backup; } size_t bufstrlen = strlen(j->buf); /* could optimize and cache it into the struct */ - size_t instrlen = strlen(in); + if (ln == -1) + ln = strlen(in); i = bufstrlen; - if (bufstrlen + instrlen > j->bufcap) - j->buf = realloc(j->buf, (j->bufcap=(bufstrlen+instrlen)*DC_REALLOC_K)*sizeof(char)); + if (bufstrlen + ln > j->bufcap) + j->buf = realloc(j->buf, (j->bufcap=(bufstrlen+ln)*DC_REALLOC_K)*sizeof(char)); strcpy(j->buf+bufstrlen, in); - bufstrlen += instrlen; + bufstrlen += ln; while (i < bufstrlen) { if (j->instr) { if (j->buf[i] == '"') { @@ -74,6 +75,9 @@ next: } return NULL; } /* returns pointer to null terminated string when there's an object to be parsed or NULL. */ +struct dc_json * dc_json_init () { + return calloc(1, sizeof(struct dc_json)); +} void dc_json_free (struct dc_json * s) { free(s->buf); free(s); |