Hash getter to extract all Features

This commit is contained in:
Eric Fischer 2014-02-05 18:23:49 -08:00
parent 8aec0ed9d2
commit 45e37eb66a

25
json.c
View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>
typedef enum json_type {
@ -59,6 +60,23 @@ static json_object *add_object(json_type type, json_object *parent) {
return o;
}
json_object *json_hash_get(json_object *o, char *s) {
if (o == NULL || o->type != JSON_HASH) {
return NULL;
}
json_hash *h;
for (h = o->hash; h != NULL; h = h->next) {
if (h->key != NULL && h->key->type == JSON_STRING) {
if (strcmp(h->key->string, s) == 0) {
return h->value;
}
}
}
return NULL;
}
static void json_error(char *s, ...) {
va_list ap;
va_start(ap, s);
@ -295,6 +313,7 @@ json_object *json_parse(FILE *f, json_object *current) {
}
json_error("unrecognized character %c\n", c);
return NULL;
}
static void indent(int depth) {
@ -360,6 +379,12 @@ int main() {
json_object *j = NULL;
while ((j = json_parse(stdin, j)) != NULL) {
json_object *g = json_hash_get(j, "type");
if (g != NULL && g->type == JSON_STRING && strcmp(g->string, "Feature") == 0) {
json_print(j, 0);
printf("\n");
}
if (j->parent == NULL) {
json_print(j, 0);
printf("\n");