Files
tippecanoe/write_json.hpp
T
Claude 85547150c2 Throw instead of exiting from the tile clipping and overzooming code
The code reachable from overzooming a tile no longer calls exit(). It
reports errors by throwing tippecanoe_error, and owns its FILE * and
json_pull * handles through RAII wrappers so that nothing leaks when an
error unwinds the stack.

New files:

* errors.hpp/errors.cpp: the tippecanoe_error exception class, plus
  throw_tippecanoe_error() and throw_perror() helpers.
* raii.hpp: unique_file and unique_json_pull, both std::unique_ptr
  aliases, for FILE * and json_pull *.

exit() becomes throw in clip.cpp, mvt.cpp, mvt.hpp, read_json.cpp,
evaluator.cpp, text.cpp, geometry.cpp, attribute.cpp, and
write_json.hpp. Each tool's main() is now a try/catch around an
inner_main(), so the command-line tools exit with the status they
always did.

Along the way this fixes a z_stream leaked when compress() failed, an
exit() that could run from json_writer's destructor during unwinding,
and an exit() in mvt_value's hash function.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DuCSVGssWNUCpVEE8Yp5HT
2026-08-07 18:46:09 +00:00

70 lines
1.7 KiB
C++

#ifndef WRITE_JSON_HPP
#define WRITE_JSON_HPP
#include <string>
#include <vector>
#include <stdio.h>
#include <set>
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_writer {
std::vector<json_write_tok> state;
bool nospace = false;
bool wantnl = false;
FILE *f = NULL;
std::string *s = NULL;
~json_writer() {
if (state.size() > 0) {
if (state.size() != 1 || state[0] != JSON_WRITE_TOP) {
// Log but don't throw — throwing from a destructor
// during stack unwinding calls std::terminate().
fprintf(stderr, "JSON not closed at end\n");
}
}
}
json_writer(FILE *fp) {
f = fp;
}
json_writer(std::string *out) {
s = out;
}
void json_write_array();
void json_end_array();
void json_write_hash();
void json_end_hash();
void json_write_string(std::string const &s);
void json_write_json(std::string const &s);
void json_write_number(double d);
void json_write_float(double d);
void json_write_unsigned(unsigned long long v);
void json_write_signed(long long v);
void json_write_stringified(std::string const &s);
void json_write_bool(bool b);
void json_write_null();
void json_write_newline();
void json_comma_newline();
private:
void json_adjust();
void aprintf(const char *format, ...);
void addc(char c);
void adds(std::string const &s);
};
void layer_to_geojson(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, json_writer &state, double scale, std::set<std::string> const &include_attr);
void fprintq(FILE *f, const char *s);
#endif