diff --git a/jsonpull.c b/jsonpull.c index d2ac1d9..dcd6e92 100644 --- a/jsonpull.c +++ b/jsonpull.c @@ -9,11 +9,11 @@ typedef enum json_expect { JSON_ITEM, JSON_COMMA, JSON_COLON, JSON_KEY, JSON_VALUE, } json_expect; -int read_file(json_pull *p) { +static int read_file(json_pull *p) { return fgetc(p->source); } -int peek_file(json_pull *p) { +static int peek_file(json_pull *p) { int c = getc(p->source); ungetc(c, p->source); return c; @@ -30,6 +30,36 @@ json_pull *json_begin_file(FILE *f) { return j; } +static int read_string(json_pull *p) { + char *cp = p->source; + if (*cp == '\0') { + return EOF; + } + int c = (unsigned char) *cp; + cp++; + p->source = cp; + return c; +} + +static int peek_string(json_pull *p) { + char *cp = p->source; + if (*cp == '\0') { + return EOF; + } + return (unsigned char) *cp; +} + +json_pull *json_begin_string(char *s) { + json_pull *j = malloc(sizeof(json_pull)); + j->container = NULL; + + j->read = read_string; + j->peek = peek_string; + j->source = s; + + return j; +} + #define SIZE_FOR(i) (((i) + 31) & ~31) static json_object *add_object(json_pull *j, json_type type) { diff --git a/jsonpull.h b/jsonpull.h index 92786ba..428916b 100644 --- a/jsonpull.h +++ b/jsonpull.h @@ -28,6 +28,7 @@ struct json_pull { typedef struct json_pull json_pull; json_pull *json_begin_file(FILE *f); +json_pull *json_begin_string(char *s); json_object *json_parse(json_pull *j); json_object *json_hash_get(json_object *o, char *s);