From ddf4a45b339fa62000a8de13373d25ce015f764e Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Thu, 15 Mar 2018 15:19:52 -0700 Subject: [PATCH] Use JSON writer functions for tile size stats --- decode.cpp | 51 +++++++++++++++++++++++++++++++++++++++++--------- write_json.cpp | 22 ---------------------- write_json.hpp | 44 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 31 deletions(-) diff --git a/decode.cpp b/decode.cpp index 8585afc..623f025 100644 --- a/decode.cpp +++ b/decode.cpp @@ -28,16 +28,32 @@ int maxzoom = 32; bool force = false; void do_stats(mvt_tile &tile, size_t size, bool compressed, int z, unsigned x, unsigned y) { - printf("{ \"zoom\": %d, \"x\": %u, \"y\": %u, \"bytes\": %zu, \"compressed\": %s", z, x, y, size, compressed ? "true" : "false"); + json_write_state state; + + json_write_hash(stdout, state); + + json_write_string(stdout, "zoom", state); + json_write_signed(stdout, z, state); + + json_write_string(stdout, "x", state); + json_write_unsigned(stdout, x, state); + + json_write_string(stdout, "y", state); + json_write_unsigned(stdout, y, state); + + json_write_string(stdout, "bytes", state); + json_write_unsigned(stdout, size, state); + + json_write_string(stdout, "compressed", state); + json_write_bool(stdout, compressed, state); + + json_write_string(stdout, "layers", state); + json_write_hash(stdout, state); - printf(", \"layers\": { "); for (size_t i = 0; i < tile.layers.size(); i++) { - if (i != 0) { - printf(", "); - } - fprintq(stdout, tile.layers[i].name.c_str()); + json_write_string(stdout, tile.layers[i].name, state); - int points = 0, lines = 0, polygons = 0; + size_t points = 0, lines = 0, polygons = 0; for (size_t j = 0; j < tile.layers[i].features.size(); j++) { if (tile.layers[i].features[j].type == mvt_point) { points++; @@ -48,10 +64,27 @@ void do_stats(mvt_tile &tile, size_t size, bool compressed, int z, unsigned x, u } } - printf(": { \"points\": %d, \"lines\": %d, \"polygons\": %d, \"extent\": %lld }", points, lines, polygons, tile.layers[i].extent); + json_write_hash(stdout, state); + + json_write_string(stdout, "points", state); + json_write_unsigned(stdout, points, state); + + json_write_string(stdout, "lines", state); + json_write_unsigned(stdout, lines, state); + + json_write_string(stdout, "polygons", state); + json_write_unsigned(stdout, polygons, state); + + json_write_string(stdout, "extent", state); + json_write_signed(stdout, tile.layers[i].extent, state); + + json_end_hash(stdout, state); } - printf(" } }\n"); + json_end_hash(stdout, state); + json_end_hash(stdout, state); + + printf("\n"); } void handle(std::string message, int z, unsigned x, unsigned y, std::set const &to_decode, bool pipeline, bool stats) { diff --git a/write_json.cpp b/write_json.cpp index b8f6ddb..fcd4167 100644 --- a/write_json.cpp +++ b/write_json.cpp @@ -9,28 +9,6 @@ #include "write_json.hpp" #include "milo/dtoa_milo.h" -enum json_write_tok { - JSON_WRITE_HASH, - JSON_WRITE_HASH_KEY, - JSON_WRITE_HASH_VALUE, - JSON_WRITE_ARRAY, - JSON_WRITE_ARRAY_ELEMENT, - JSON_WRITE_TOP, -}; - -struct json_write_state { - std::vector state; - - ~json_write_state() { - if (state.size() > 0) { - if (state.size() != 1 || state[0] != JSON_WRITE_TOP) { - fprintf(stderr, "JSON not closed at end\n"); - exit(EXIT_FAILURE); - } - } - } -}; - static void json_adjust(FILE *f, json_write_state &state) { if (state.state.size() == 0) { state.state.push_back(JSON_WRITE_TOP); diff --git a/write_json.hpp b/write_json.hpp index 4f0526a..8c13854 100644 --- a/write_json.hpp +++ b/write_json.hpp @@ -1,2 +1,46 @@ +#ifndef WRITE_JSON_HPP +#define WRITE_JSON_HPP + +#include +#include +#include + +enum json_write_tok { + JSON_WRITE_HASH, + JSON_WRITE_HASH_KEY, + JSON_WRITE_HASH_VALUE, + JSON_WRITE_ARRAY, + JSON_WRITE_ARRAY_ELEMENT, + JSON_WRITE_TOP, +}; + +struct json_write_state { + std::vector state; + + ~json_write_state() { + if (state.size() > 0) { + if (state.size() != 1 || state[0] != JSON_WRITE_TOP) { + fprintf(stderr, "JSON not closed at end\n"); + exit(EXIT_FAILURE); + } + } + } +}; + void layer_to_geojson(FILE *fp, mvt_layer const &layer, unsigned z, unsigned x, unsigned y, bool comma, bool name, bool zoom, bool dropped, unsigned long long index, long long sequence, long long extent, bool complain); void fprintq(FILE *f, const char *s); + +void json_write_array(FILE *f, json_write_state &state); +void json_end_array(FILE *f, json_write_state &state); +void json_write_hash(FILE *f, json_write_state &state); +void json_end_hash(FILE *f, json_write_state &state); +void json_write_string(FILE *f, std::string const &s, json_write_state &state); +void json_write_number(FILE *f, double d, json_write_state &state); +void json_write_float(FILE *f, double d, json_write_state &state); +void json_write_unsigned(FILE *f, unsigned long long v, json_write_state &state); +void json_write_signed(FILE *f, long long v, json_write_state &state); +void json_write_stringified(FILE *f, std::string const &s, json_write_state &state); +void json_write_bool(FILE *f, bool b, json_write_state &state); +void json_write_null(FILE *f, json_write_state &state); + +#endif