Add an input form that parses JSON from a string

This commit is contained in:
Eric Fischer 2014-02-06 19:27:21 -08:00
parent ed079e37a9
commit e22b154ecc
2 changed files with 33 additions and 2 deletions

View File

@ -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) {

View File

@ -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);