Files
Cataclysm-DDA/tools/format_emscripten/format_emscripten.cpp
T
Alexey c011e9de12 Improve output for JSON errors (#66623)
* Colorize JSON errors

* Make formatter feed source filename to TextJsonIn

* Throw exception instead of dumping output to std::cerr

* Adjust math parser's caret appearance

* Set color in chkjson

* Adjust tests and formatters

* Add json error cursor check for unicode string

* Squelch global state lint for entry points

* Adjust stream offset so the buffer for utf8_width is pointing at a codepoint
2023-07-17 19:51:50 -05:00

36 lines
1.0 KiB
C++

enum class error_log_format_t { human_readable };
extern constexpr error_log_format_t error_log_format = error_log_format_t::human_readable;
#include "json.h"
#include "format.h"
extern "C" {
const char *json_format( const char *input )
{
// we can do the malloc/free game with emscripten
// or just leave the buffer in place and realloc
// to make sure the strings fit
static char *ret = nullptr;
std::stringstream ss_out;
std::stringstream ss_in( input );
std::string ret_tmp;
try {
TextJsonIn jsin( ss_in );
JsonOut jsout( ss_out, true );
formatter::format( jsin, jsout );
ret_tmp = ss_out.str();
} catch( const std::exception &ex ) {
ret_tmp = std::string( ex.what() );
}
const char *ret_ctmp = ret_tmp.c_str();
const int len = strlen( ret_ctmp );
ret = static_cast<char *>( realloc( ret, len + 1 ) );
ret[0] = 0;
strncat( ret, ret_ctmp, len );
return ret;
}
}