diff options
author | Freya Murphy <freya@freyacat.org> | 2023-12-16 20:09:24 -0500 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2023-12-16 20:09:24 -0500 |
commit | 081bdb70628163768a7361be10772c11980b5536 (patch) | |
tree | ea979fb19da3285ca369acaaba4bf985d19971a9 /src/snbt/print.c | |
parent | use hashmap for compound tag (diff) | |
download | nbtvis-081bdb70628163768a7361be10772c11980b5536.tar.gz nbtvis-081bdb70628163768a7361be10772c11980b5536.tar.bz2 nbtvis-081bdb70628163768a7361be10772c11980b5536.zip |
snbt
Diffstat (limited to 'src/snbt/print.c')
-rw-r--r-- | src/snbt/print.c | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/src/snbt/print.c b/src/snbt/print.c new file mode 100644 index 0000000..9dec75c --- /dev/null +++ b/src/snbt/print.c @@ -0,0 +1,210 @@ +#include "snbt.h" +#include <stdarg.h> +#include <stdio.h> + +static char buf[1024]; + +__attribute__((format(printf, 3, 4))) +static bool printi(const stream_t *stream, int depth, const char *format, ...) { + for (int i = 0; i < depth; i++) + if (stream_write(stream, "\t", 1) == false) + return false; + va_list list; + va_start(list, format); + int len; + if ((len = vsnprintf(buf, 1024, format, list)) < 0) + return false; + if (stream_write(stream, buf, len) == false) + return false; + return true; +} + +static bool snbt_print_impl(const tag_t *tag, const stream_t *stream, int depth); + +static bool snbt_print_byte_array(const tagdata_t *data, const stream_t *stream) { + if (printi(stream, 0, "[B;") == false) + return false; + for (int32_t i = 0; i < data->b_arr.size; i++) { + if (i != 0) + if (printi(stream, 0, ",") == false) + return false; + if (printi(stream, 0, "%hhdb", data->b_arr.data[i]) == false) + return false; + } + if (printi(stream, 0, "]") == false) + return false; + return true; +} + +static bool snbt_print_int_array(const tagdata_t *data, const stream_t *stream) { + if (printi(stream, 0, "[I;") == false) + return false; + for (int32_t i = 0; i < data->i_arr.size; i++) { + if (i != 0) + if (printi(stream, 0, ",") == false) + return false; + if (printi(stream, 0, "%d", data->i_arr.data[i]) == false) + return false; + } + if (printi(stream, 0, "]") == false) + return false; + return true; +} + +static bool snbt_print_long_array(const tagdata_t *data, const stream_t *stream) { + if (printi(stream, 0, "[L;") == false) + return false; + for (int32_t i = 0; i < data->l_arr.size; i++) { + if (i != 0) + if (printi(stream, 0, ",") == false) + return false; + if (printi(stream, 0, "%ldL", data->l_arr.data[i]) == false) + return false; + } + if (printi(stream, 0, "]") == false) + return false; + return true; +} + +static bool snbt_string_unquoted(const char *text, uint16_t len) { + for (uint16_t i = 0; i < len; i++) { + char c = text[i]; + if (snbt_allowed_ident(c)) + continue; + return false; + } + return true; +} + +static bool snbt_print_string_impl(const stream_t *stream, int depth, char *text, uint16_t len) { + if (len > 0 && snbt_string_unquoted(text, len)) { + if (printi(stream, depth, "%.*s", len, text) == false) + return false; + } else if (len > 0) { + if (printi(stream, depth, "\"") == false) + return false; + for (uint16_t i = 0; i < len; i++) { + char c = text[i]; + if (c == '\\' || c == '"') { + if (printi(stream, 0, "\\%c", c) == false) + return false; + } else { + if (printi(stream, 0, "%c", c) == false) + return false; + } + } + if (printi(stream, 0, "\"") == false) + return false; + } else { + if (printi(stream, depth, "''") == false) + return false; + } + return true; +} + +static bool snbt_print_string(const tagdata_t *data, const stream_t *stream) { + char *text = data->string.data; + uint16_t len = data->string.size; + return snbt_print_string_impl(stream, 0, text, len); +} + +static bool snbt_print_compound(const tagdata_t *data, const stream_t *stream, int depth) { + if (printi(stream, 0, "{\n") == false) + return false; + bool first = true; + for (uint32_t i = 0; i < data->compound.capacity; i++) { + if (data->compound.entries[i].name == NULL) + continue; + if (!first && printi(stream, 0, ",\n") == false) + return false; + first = false; + if (snbt_print_impl(&data->compound.entries[i], stream, depth + 1) == false) + return false; + } + if (printi(stream, 0, "\n") == false || printi(stream, depth, "}") == false) + return false; + return true; +} + +static bool snbt_print_list(const tagdata_t *data, const stream_t *stream, int depth) { + if (printi(stream, 0, "[\n") == false) + return false; + for (int32_t i = 0; i < data->list.size; i++) { + if (i != 0 && printi(stream, 0, ",\n") == false) + return false; + if (snbt_print_impl(&data->list.tags[i], stream, depth + 1) == false) + return false; + } + if (printi(stream, 0, "\n") == false || printi(stream, depth, "]") == false) + return false; + return true; +} + +static bool snbt_print_data(const tag_t *tag, const stream_t *stream, int depth) { + + bool ok = true; + + switch (tag->type) { + case TAG_BYTE: + ok = printi(stream, 0, "%hhdb", tag->data.b); + break; + case TAG_SHORT: + ok = printi(stream, 0, "%hds", tag->data.s); + break; + case TAG_INT: + ok = printi(stream, 0, "%d", tag->data.i); + break; + case TAG_LONG: + ok = printi(stream, 0, "%ldL", tag->data.l); + break; + case TAG_FLOAT: + ok = printi(stream, 0, "%.9gf", tag->data.f); + break; + case TAG_DOUBLE: + ok = printi(stream, 0, "%.17g", tag->data.d); + break; + case TAG_BYTE_ARRAY: + ok = snbt_print_byte_array(&tag->data, stream); + break; + case TAG_STRING: + ok = snbt_print_string(&tag->data, stream); + break; + case TAG_LIST: + ok = snbt_print_list(&tag->data, stream, depth); + break; + case TAG_COMPOUND: + ok = snbt_print_compound(&tag->data, stream, depth); + break; + case TAG_INT_ARRAY: + ok = snbt_print_int_array(&tag->data, stream); + break; + case TAG_LONG_ARRAY: + ok = snbt_print_long_array(&tag->data, stream); + break; + case TAG_END: + break; + } + + return ok; +} + +static bool snbt_print_impl(const tag_t *tag, const stream_t *stream, int depth) { + if (tag->name_len > 0) { + if (snbt_print_string_impl(stream, depth, tag->name, tag->name_len) == false) + return false; + if (printi(stream, 0, ": ") == false) + return false; + } else { + for (int i = 0; i < depth; i++) + printi(stream, 0, "\t"); + } + return snbt_print_data(tag, stream, depth); +} + +bool snbt_print(const tag_t *tag, const stream_t *stream) { + if (snbt_print_impl(tag, stream, 0) == false) + return false; + if (stream_write(stream, "\n", 1) == false) + return false; + return true; +} |