diff --git a/mbtiles.cpp b/mbtiles.cpp index b8c7684..1c63752 100644 --- a/mbtiles.cpp +++ b/mbtiles.cpp @@ -88,38 +88,6 @@ void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data, } } -static void quote(std::string &buf, std::string const &s) { - for (size_t i = 0; i < s.size(); i++) { - unsigned char ch = s[i]; - - if (ch == '\\' || ch == '\"') { - buf.push_back('\\'); - buf.push_back(ch); - } else if (ch < ' ') { - char tmp[7]; - sprintf(tmp, "\\u%04x", ch); - buf.append(std::string(tmp)); - } else { - buf.push_back(ch); - } - } -} - -void aprintf(std::string *buf, const char *format, ...) { - va_list ap; - char *tmp; - - va_start(ap, format); - if (vasprintf(&tmp, format, ap) < 0) { - fprintf(stderr, "memory allocation failure\n"); - exit(EXIT_FAILURE); - } - va_end(ap); - - buf->append(tmp, strlen(tmp)); - free(tmp); -} - bool type_and_string::operator<(const type_and_string &o) const { if (string < o.string) { return true; @@ -505,7 +473,10 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *outdir, const char *fnam exit(EXIT_FAILURE); } - fprintf(fp, "{\n"); + json_writer state(fp); + + state.json_write_hash(); + state.json_write_newline(); sqlite3_stmt *stmt; bool first = true; @@ -520,19 +491,17 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *outdir, const char *fnam exit(EXIT_FAILURE); } - quote(key, k); - quote(value, v); - - if (!first) { - fprintf(fp, ",\n"); - } - fprintf(fp, " \"%s\": \"%s\"", key.c_str(), value.c_str()); + state.json_comma_newline(); + state.json_write_string(k); + state.json_write_string(v); first = false; } sqlite3_finalize(stmt); } - fprintf(fp, "\n}\n"); + state.json_write_newline(); + state.json_end_hash(); + state.json_write_newline(); fclose(fp); } } diff --git a/mbtiles.hpp b/mbtiles.hpp index 1f085a8..fd39f7e 100644 --- a/mbtiles.hpp +++ b/mbtiles.hpp @@ -44,8 +44,6 @@ void mbtiles_write_metadata(sqlite3 *outdb, const char *outdir, const char *fnam void mbtiles_close(sqlite3 *outdb, const char *pgm); -void aprintf(std::string *buf, const char *format, ...); - std::map merge_layermaps(std::vector > const &maps); std::map merge_layermaps(std::vector > const &maps, bool trunc); diff --git a/tests/raw-tiles/compare/metadata.json b/tests/raw-tiles/compare/metadata.json index 14a62ae..01c91e7 100644 --- a/tests/raw-tiles/compare/metadata.json +++ b/tests/raw-tiles/compare/metadata.json @@ -1,12 +1,12 @@ { - "name": "tests/raw-tiles/raw-tiles", - "description": "tests/raw-tiles/raw-tiles", - "version": "2", - "minzoom": "0", - "maxzoom": "14", - "center": "-122.662354,45.514045,14", - "bounds": "-122.682427,45.512331,-122.654961,45.569975", - "type": "overlay", - "format": "pbf", - "json": "{\"vector_layers\": [ { \"id\": \"hackspots\", \"description\": \"\", \"minzoom\": 0, \"maxzoom\": 14, \"fields\": {\"Address\": \"String\", \"Name\": \"String\", \"Notes\": \"String\"} } ],\"tilestats\": {\"layerCount\": 1,\"layers\": [{\"layer\": \"hackspots\",\"count\": 4,\"geometry\": \"Point\",\"attributeCount\": 3,\"attributes\": [{\"attribute\": \"Address\",\"count\": 4,\"type\": \"string\",\"values\": [\"1507 N Rosa Parks Way Portland, OR 97217\",\"201 SE 12th Ave, Portland, OR 97214\",\"4637 N Albina Ave Portland, OR 97217\",\"915 SE Hawthorne Blvd. Portland, OR 97214\"]},{\"attribute\": \"Name\",\"count\": 4,\"type\": \"string\",\"values\": [\"Albina Press\",\"Arbor Lodge\",\"Lucky Labrador Brew Pub\",\"Three Friends Coffeehouse\"]},{\"attribute\": \"Notes\",\"count\": 3,\"type\": \"string\",\"values\": [\"\",\"Dog friendly\",\"usually busy, outlets on side wall only\"]}]}]}}" +"name": "tests/raw-tiles/raw-tiles", +"description": "tests/raw-tiles/raw-tiles", +"version": "2", +"minzoom": "0", +"maxzoom": "14", +"center": "-122.662354,45.514045,14", +"bounds": "-122.682427,45.512331,-122.654961,45.569975", +"type": "overlay", +"format": "pbf", +"json": "{\"vector_layers\": [ { \"id\": \"hackspots\", \"description\": \"\", \"minzoom\": 0, \"maxzoom\": 14, \"fields\": {\"Address\": \"String\", \"Name\": \"String\", \"Notes\": \"String\"} } ],\"tilestats\": {\"layerCount\": 1,\"layers\": [{\"layer\": \"hackspots\",\"count\": 4,\"geometry\": \"Point\",\"attributeCount\": 3,\"attributes\": [{\"attribute\": \"Address\",\"count\": 4,\"type\": \"string\",\"values\": [\"1507 N Rosa Parks Way Portland, OR 97217\",\"201 SE 12th Ave, Portland, OR 97214\",\"4637 N Albina Ave Portland, OR 97217\",\"915 SE Hawthorne Blvd. Portland, OR 97214\"]},{\"attribute\": \"Name\",\"count\": 4,\"type\": \"string\",\"values\": [\"Albina Press\",\"Arbor Lodge\",\"Lucky Labrador Brew Pub\",\"Three Friends Coffeehouse\"]},{\"attribute\": \"Notes\",\"count\": 3,\"type\": \"string\",\"values\": [\"\",\"Dog friendly\",\"usually busy, outlets on side wall only\"]}]}]}}" } diff --git a/tile-join.cpp b/tile-join.cpp index b38db8c..abe340e 100644 --- a/tile-join.cpp +++ b/tile-join.cpp @@ -47,6 +47,21 @@ struct stats { double minlat, minlon, maxlat, maxlon; }; +void aprintf(std::string *buf, const char *format, ...) { + va_list ap; + char *tmp; + + va_start(ap, format); + if (vasprintf(&tmp, format, ap) < 0) { + fprintf(stderr, "memory allocation failure\n"); + exit(EXIT_FAILURE); + } + va_end(ap); + + buf->append(tmp, strlen(tmp)); + free(tmp); +} + void handle(std::string message, int z, unsigned x, unsigned y, std::map &layermap, std::vector &header, std::map> &mapping, std::set &exclude, std::set &keep_layers, std::set &remove_layers, int ifmatched, mvt_tile &outtile, json_object *filter) { mvt_tile tile; int features_added = 0; diff --git a/write_json.cpp b/write_json.cpp index 9ddcf60..90a5184 100644 --- a/write_json.cpp +++ b/write_json.cpp @@ -195,18 +195,18 @@ void json_writer::json_comma_newline() { } void json_writer::aprintf(const char *format, ...) { - va_list ap; - char *tmp; + va_list ap; + char *tmp; - va_start(ap, format); - if (vasprintf(&tmp, format, ap) < 0) { - fprintf(stderr, "memory allocation failure\n"); - exit(EXIT_FAILURE); - } - va_end(ap); + va_start(ap, format); + if (vasprintf(&tmp, format, ap) < 0) { + fprintf(stderr, "memory allocation failure\n"); + exit(EXIT_FAILURE); + } + va_end(ap); - adds(std::string(tmp, strlen(tmp))); - free(tmp); + adds(std::string(tmp, strlen(tmp))); + free(tmp); } void json_writer::addc(char c) { diff --git a/write_json.hpp b/write_json.hpp index 6ddd4d4..d0b9d18 100644 --- a/write_json.hpp +++ b/write_json.hpp @@ -53,7 +53,7 @@ struct json_writer { void json_write_newline(); void json_comma_newline(); -private: + private: void json_adjust(); void aprintf(const char *format, ...); void addc(char c);