mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-04 17:35:42 +02:00
Add an input form that parses JSON from a string
This commit is contained in:
+32
-2
@@ -9,11 +9,11 @@ typedef enum json_expect {
|
|||||||
JSON_ITEM, JSON_COMMA, JSON_COLON, JSON_KEY, JSON_VALUE,
|
JSON_ITEM, JSON_COMMA, JSON_COLON, JSON_KEY, JSON_VALUE,
|
||||||
} json_expect;
|
} json_expect;
|
||||||
|
|
||||||
int read_file(json_pull *p) {
|
static int read_file(json_pull *p) {
|
||||||
return fgetc(p->source);
|
return fgetc(p->source);
|
||||||
}
|
}
|
||||||
|
|
||||||
int peek_file(json_pull *p) {
|
static int peek_file(json_pull *p) {
|
||||||
int c = getc(p->source);
|
int c = getc(p->source);
|
||||||
ungetc(c, p->source);
|
ungetc(c, p->source);
|
||||||
return c;
|
return c;
|
||||||
@@ -30,6 +30,36 @@ json_pull *json_begin_file(FILE *f) {
|
|||||||
return j;
|
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)
|
#define SIZE_FOR(i) (((i) + 31) & ~31)
|
||||||
|
|
||||||
static json_object *add_object(json_pull *j, json_type type) {
|
static json_object *add_object(json_pull *j, json_type type) {
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ struct json_pull {
|
|||||||
typedef struct json_pull json_pull;
|
typedef struct json_pull json_pull;
|
||||||
|
|
||||||
json_pull *json_begin_file(FILE *f);
|
json_pull *json_begin_file(FILE *f);
|
||||||
|
json_pull *json_begin_string(char *s);
|
||||||
json_object *json_parse(json_pull *j);
|
json_object *json_parse(json_pull *j);
|
||||||
|
|
||||||
json_object *json_hash_get(json_object *o, char *s);
|
json_object *json_hash_get(json_object *o, char *s);
|
||||||
|
|||||||
Reference in New Issue
Block a user