diff --git a/src/cs_error.hh b/src/cs_error.hh new file mode 100644 index 0000000..0a1aaf8 --- /dev/null +++ b/src/cs_error.hh @@ -0,0 +1,32 @@ +#ifndef LIBCUBESCRIPT_ERROR_HH +#define LIBCUBESCRIPT_ERROR_HH + +#include + +#include +#include + +namespace cubescript { + +struct error_p { + template + static error make(state &cs, std::string_view msg, A const &...args) { + std::size_t sz = msg.size() + 64; + char *buf, *sp; + for (;;) { + buf = state_p{cs}.ts().request_errbuf(sz, sp); + int written = std::snprintf(buf, sz, msg.data(), args...); + if (written <= 0) { + throw error{cs, "malformed format string"}; + } else if (std::size_t(written) <= sz) { + break; + } + sz = std::size_t(written); + } + return error{cs, sp, buf + sz}; + } +}; + +} /* namespace cubescript */ + +#endif