summaryrefslogtreecommitdiff
path: root/src/snbt/print.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2023-12-17 11:10:04 -0500
committerFreya Murphy <freya@freyacat.org>2023-12-17 11:10:04 -0500
commite0eacfa9773c83850ed5169d1e889ff845180581 (patch)
tree9f8bb433404ce7e4dda1b86ca4dd5d5a2fba10e9 /src/snbt/print.c
parentsnbt (diff)
downloadnbtvis-e0eacfa9773c83850ed5169d1e889ff845180581.tar.gz
nbtvis-e0eacfa9773c83850ed5169d1e889ff845180581.tar.bz2
nbtvis-e0eacfa9773c83850ed5169d1e889ff845180581.zip
refactorHEADmain
Diffstat (limited to 'src/snbt/print.c')
-rw-r--r--src/snbt/print.c210
1 files changed, 0 insertions, 210 deletions
diff --git a/src/snbt/print.c b/src/snbt/print.c
deleted file mode 100644
index 9dec75c..0000000
--- a/src/snbt/print.c
+++ /dev/null
@@ -1,210 +0,0 @@
-#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;
-}