From 404bf698474fe401431922f76b0a0e2d9374f3ae Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Mon, 11 Jan 2016 10:46:25 -0800 Subject: [PATCH 1/2] Give an error if -p or -a is used with an undefined option letter --- geojson.c | 24 ++++++++++++++++++++++-- tile.cc | 18 +++++++++--------- tile.h | 26 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/geojson.c b/geojson.c index 319841f..727eae9 100644 --- a/geojson.c +++ b/geojson.c @@ -1826,6 +1826,16 @@ int read_json(int argc, char **argv, char *fname, const char *layername, int max return ret; } +int int_in(int v, int *a, int len) { + for (int i = 0; i < len; i++) { + if (a[i] == v) { + return 1; + } + } + + return 0; +} + int main(int argc, char **argv) { #ifdef MTRACE mtrace(); @@ -1961,14 +1971,24 @@ int main(int argc, char **argv) { case 'p': { char *cp; for (cp = optarg; *cp != '\0'; cp++) { - prevent[*cp & 0xFF] = 1; + if (int_in(*cp, prevent_options, sizeof(prevent_options) / sizeof(prevent_options[0]))) { + prevent[*cp & 0xFF] = 1; + } else { + fprintf(stderr, "%s: Unknown option -p%c\n", argv[0], *cp); + exit(EXIT_FAILURE); + } } } break; case 'a': { char *cp; for (cp = optarg; *cp != '\0'; cp++) { - additional[*cp & 0xFF] = 1; + if (int_in(*cp, additional_options, sizeof(additional_options) / sizeof(additional_options[0]))) { + additional[*cp & 0xFF] = 1; + } else { + fprintf(stderr, "%s: Unknown option -a%c\n", argv[0], *cp); + exit(EXIT_FAILURE); + } } } break; diff --git a/tile.cc b/tile.cc index 7cc613d..06f44db 100644 --- a/tile.cc +++ b/tile.cc @@ -502,7 +502,7 @@ void *partial_feature_worker(void *v) { char *additional = (*partials)[i].additional; int maxzoom = (*partials)[i].maxzoom; - if ((t == VT_LINE || t == VT_POLYGON) && !(prevent['s' & 0xFF] || (z == maxzoom && prevent['S' & 0xFF]))) { + if ((t == VT_LINE || t == VT_POLYGON) && !(prevent[P_SIMPLIFY] || (z == maxzoom && prevent[P_SIMPLIFY_LOW]))) { if (1 /* !reduced */) { // XXX why did this not simplify if reduced? if (t == VT_LINE) { geom = remove_noop(geom, t, 32 - z - line_detail); @@ -518,7 +518,7 @@ void *partial_feature_worker(void *v) { } #endif - if (t == VT_LINE && additional['r' & 0xFF]) { + if (t == VT_LINE && additional[A_REVERSE]) { geom = reorder_lines(geom); } @@ -746,7 +746,7 @@ long long write_tile(char **geoms, char *metabase, char *stringpool, int z, unsi continue; } - if (gamma >= 0 && (t == VT_POINT || (additional['l' & 0xFF] && t == VT_LINE))) { + if (gamma >= 0 && (t == VT_POINT || (additional[A_LINE_DROP] && t == VT_LINE))) { seq++; if (seq >= 0) { seq -= interval; @@ -880,7 +880,7 @@ long long write_tile(char **geoms, char *metabase, char *stringpool, int z, unsi } for (j = 0; j < nlayers; j++) { - if (additional['o' & 0xFF]) { + if (additional[A_REORDER]) { std::sort(features[j].begin(), features[j].end()); } @@ -895,7 +895,7 @@ long long write_tile(char **geoms, char *metabase, char *stringpool, int z, unsi } #endif - if (additional['c' & 0xFF] && out.size() > 0 && out[y].geom.size() + features[j][x].geom.size() < 20000 && coalcmp(&features[j][x], &out[y]) == 0 && features[j][x].type != VT_POINT) { + if (additional[A_COALESCE] && out.size() > 0 && out[y].geom.size() + features[j][x].geom.size() < 20000 && coalcmp(&features[j][x], &out[y]) == 0 && features[j][x].type != VT_POINT) { unsigned z; for (z = 0; z < features[j][x].geom.size(); z++) { out[y].geom.push_back(features[j][x].geom[z]); @@ -920,7 +920,7 @@ long long write_tile(char **geoms, char *metabase, char *stringpool, int z, unsi } features[j] = out; - if (prevent['i' & 0xFF]) { + if (prevent[P_INPUT_ORDER]) { std::sort(features[j].begin(), features[j].end(), preservecmp); } } @@ -936,7 +936,7 @@ long long write_tile(char **geoms, char *metabase, char *stringpool, int z, unsi } if (totalsize > 0) { - if (totalsize > 200000 && !prevent['f' & 0xFF]) { + if (totalsize > 200000 && !prevent[P_FEATURE_LIMIT]) { fprintf(stderr, "tile %d/%u/%u has %lld features, >200000 \n", z, tx, ty, totalsize); fprintf(stderr, "Try using -z to set a higher base zoom level.\n"); return -1; @@ -956,12 +956,12 @@ long long write_tile(char **geoms, char *metabase, char *stringpool, int z, unsi tile.SerializeToString(&s); compress(s, compressed); - if (compressed.size() > 500000 && !prevent['k' & 0xFF]) { + if (compressed.size() > 500000 && !prevent[P_KILOBYTE_LIMIT]) { if (!quiet) { fprintf(stderr, "tile %d/%u/%u size is %lld with detail %d, >500000 \n", z, tx, ty, (long long) compressed.size(), line_detail); } - if (prevent['d' & 0xFF]) { + if (prevent[P_DYNAMIC_DROP]) { // The 95% is a guess to avoid too many retries // and probably actually varies based on how much duplicated metadata there is diff --git a/tile.h b/tile.h index bca0d4f..652aa82 100644 --- a/tile.h +++ b/tile.h @@ -35,3 +35,29 @@ extern int quiet; extern int CPUS; extern int TEMP_FILES; + +static int additional_options[] = { +#define A_COALESCE ((int) 'c') + A_COALESCE, +#define A_REVERSE ((int) 'r') + A_REVERSE, +#define A_REORDER ((int) 'o') + A_REORDER, +#define A_LINE_DROP ((int) 'l') + A_LINE_DROP, +}; + +static int prevent_options[] = { +#define P_SIMPLIFY ((int) 's') + P_SIMPLIFY, +#define P_SIMPLIFY_LOW ((int) 'S') + P_SIMPLIFY_LOW, +#define P_FEATURE_LIMIT ((int) 'f') + P_FEATURE_LIMIT, +#define P_KILOBYTE_LIMIT ((int) 'k') + P_KILOBYTE_LIMIT, +#define P_DYNAMIC_DROP ((int) 'd') + P_DYNAMIC_DROP, +#define P_INPUT_ORDER ((int) 'i') + P_INPUT_ORDER, +}; From a597733a019b56ee06b756a4dda867435a681721 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Mon, 11 Jan 2016 11:00:23 -0800 Subject: [PATCH 2/2] Fix the build --- geojson.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/geojson.c b/geojson.c index 727eae9..41a532b 100644 --- a/geojson.c +++ b/geojson.c @@ -1827,7 +1827,9 @@ int read_json(int argc, char **argv, char *fname, const char *layername, int max } int int_in(int v, int *a, int len) { - for (int i = 0; i < len; i++) { + int i; + + for (i = 0; i < len; i++) { if (a[i] == v) { return 1; }