Flatten out error-handling so it isn't nested so deeply

This commit is contained in:
Eric Fischer
2014-09-15 15:47:06 -07:00
parent efa3995d94
commit 3d43c5afbd
+93 -84
View File
@@ -49,7 +49,6 @@ void read_json(FILE *f) {
while (1) { while (1) {
json_object *j = json_read(jp); json_object *j = json_read(jp);
if (j == NULL) { if (j == NULL) {
if (jp->error != NULL) { if (jp->error != NULL) {
fprintf(stderr, "%d: %s\n", jp->line, jp->error); fprintf(stderr, "%d: %s\n", jp->line, jp->error);
@@ -60,104 +59,114 @@ void read_json(FILE *f) {
} }
json_object *type = json_hash_get(j, "type"); json_object *type = json_hash_get(j, "type");
if (type != NULL && type->type == JSON_STRING && strcmp(type->string, "Feature") == 0) { if (type == NULL || type->type != JSON_STRING || strcmp(type->string, "Feature") != 0) {
json_object *geometry = json_hash_get(j, "geometry"); continue;
}
if (geometry != NULL) { json_object *geometry = json_hash_get(j, "geometry");
json_object *geometry_type = json_hash_get(geometry, "type"); if (geometry == NULL) {
if (geometry_type != NULL && geometry_type->type == JSON_STRING) { fprintf(stderr, "%d: feature with no geometry\n", jp->line);
int t; goto next_feature;
}
for (t = 0; t < GEOM_TYPES; t++) { json_object *geometry_type = json_hash_get(geometry, "type");
if (strcmp(geometry_type->string, geometry_names[t]) == 0) { if (geometry_type == NULL || geometry_type->type != JSON_STRING) {
break; fprintf(stderr, "%d: geometry without type string\n", jp->line);
} goto next_feature;
} }
if (t < GEOM_TYPES) { json_object *properties = json_hash_get(j, "properties");
json_object *properties = json_hash_get(j, "properties"); if (properties == NULL || properties->type != JSON_HASH) {
json_object *coordinates = json_hash_get(geometry, "coordinates"); fprintf(stderr, "%d: feature without properties hash\n", jp->line);
goto next_feature;
}
if (properties != NULL && properties->type == JSON_HASH) { json_object *coordinates = json_hash_get(geometry, "coordinates");
char *metakey[properties->length]; if (coordinates == NULL || coordinates->type != JSON_ARRAY) {
char *metaval[properties->length]; fprintf(stderr, "%d: feature without coordinates array\n", jp->line);
int metatype[properties->length]; goto next_feature;
int m = 0; }
int i; int t;
for (i = 0; i < properties->length; i++) { for (t = 0; t < GEOM_TYPES; t++) {
if (properties->keys[i]->type == JSON_STRING) { if (strcmp(geometry_type->string, geometry_names[t]) == 0) {
metakey[m] = properties->keys[i]->string; break;
}
}
if (t >= GEOM_TYPES) {
fprintf(stderr, "%d: Can't handle geometry type %s\n", jp->line, geometry_type->string);
goto next_feature;
}
if (properties->values[i] != NULL && properties->values[i]->type == JSON_STRING) { /* scope for variable-length arrays */
metatype[m] = META_STRING; {
metaval[m] = properties->values[i]->string; char *metakey[properties->length];
m++; char *metaval[properties->length];
} else if (properties->values[i] != NULL && properties->values[i]->type == JSON_NUMBER) { int metatype[properties->length];
metatype[m] = META_INTEGER; int m = 0;
metaval[m] = properties->values[i]->string;
m++;
} else {
fprintf(stderr, "%d: Unsupported meta type\n", jp->line);
}
}
}
if (coordinates != NULL && coordinates->type == JSON_ARRAY) { int i;
int n; for (i = 0; i < properties->length; i++) {
if (properties->keys[i]->type == JSON_STRING) {
metakey[m] = properties->keys[i]->string;
if (t == GEOM_POINT) { if (properties->values[i] != NULL && properties->values[i]->type == JSON_STRING) {
n = 1; metatype[m] = META_STRING;
} else { metaval[m] = properties->values[i]->string;
n = coordinates->length; m++;
} } else if (properties->values[i] != NULL && properties->values[i]->type == JSON_NUMBER) {
metatype[m] = META_INTEGER;
double lon[n], lat[n]; metaval[m] = properties->values[i]->string;
int ok = 1; m++;
if (t == GEOM_POINT) {
if (coordinates->length >= 2) {
lon[0] = coordinates->array[0]->number;
lat[0] = coordinates->array[1]->number;
} else {
fprintf(stderr, "%d: not enough coordinates for a point\n", jp->line);
ok = 0;
}
} else {
for (i = 0; i < n; i++) {
if (coordinates->array[i]->type == JSON_ARRAY &&
coordinates->array[i]->length >= 2) {
lon[i] = coordinates->array[i]->array[0]->number;
lat[i] = coordinates->array[i]->array[1]->number;
} else {
fprintf(stderr, "%d: not enough coordinates within the geometry\n", jp->line);
ok = 0;
}
}
}
if (ok) {
// XXX encode(destdir, files, maxn, extra, xoff, pool, n, lat, lon, m, metakey, metaval, metatype);
}
} else {
fprintf(stderr, "%d: feature geometry with no coordinates\n", jp->line);
}
} else {
fprintf(stderr, "%d: feature with no properties\n", jp->line);
}
} else { } else {
fprintf(stderr, "%d: Can't handle geometry type %s\n", jp->line, geometry_type->string); fprintf(stderr, "%d: Unsupported meta type\n", jp->line);
} }
} else {
fprintf(stderr, "%d: geometry has no type\n", jp->line);
} }
} else {
fprintf(stderr, "%d: feature with no geometry\n", jp->line);
} }
json_free(j); int n;
if (t == GEOM_POINT) {
n = 1;
} else {
n = coordinates->length;
}
double lon[n], lat[n];
int ok = 1;
if (t == GEOM_POINT) {
if (coordinates->length >= 2) {
lon[0] = coordinates->array[0]->number;
lat[0] = coordinates->array[1]->number;
} else {
fprintf(stderr, "%d: not enough coordinates for a point\n", jp->line);
ok = 0;
}
} else {
for (i = 0; i < n; i++) {
if (coordinates->array[i]->type == JSON_ARRAY &&
coordinates->array[i]->length >= 2) {
lon[i] = coordinates->array[i]->array[0]->number;
lat[i] = coordinates->array[i]->array[1]->number;
} else {
fprintf(stderr, "%d: not enough coordinates within the geometry\n", jp->line);
ok = 0;
}
}
}
if (ok) {
for (i = 0; i < n; i++) {
printf("%f,%f ", lat[i], lon[i]);
}
printf("\n");
// XXX encode(destdir, files, maxn, extra, xoff, pool, n, lat, lon, m, metakey, metaval, metatype);
}
} }
next_feature:
json_free(j);
/* XXX check for any non-features in the outer object */ /* XXX check for any non-features in the outer object */
} }