From 42d1c82a0bfa615b832f5ecc2652edc290bf6e9c Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 15 Dec 2023 23:02:45 -0500 Subject: add json support and other things --- src/main.c | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index fec6d06..f4419eb 100644 --- a/src/main.c +++ b/src/main.c @@ -1,24 +1,54 @@ -#include "tag.h" #include "lib.h" +#include "tag.h" +#include "flags.h" + #include +#include + +__attribute__((__noreturn__)) +void version() { + fprintf(stderr, "nbtvis v0.0.1\n"); + fprintf(stderr, "Copyright (C) 2023 Freya Murphy\n"); + exit(0); +} + +__attribute__((__noreturn__)) +void help() { + fprintf(stderr, "Usage: nbtvis [OPTION]... [INFILE] [OUTFILE]\n\n"); + fprintf(stderr, "\t-j\tinput data is JSON\n"); + fprintf(stderr, "\t-s\tinput data is SNBT\n"); + fprintf(stderr, "\t-n\tinput data is NBT\n"); + fprintf(stderr, "\t-J\toutput data is JSON\n"); + fprintf(stderr, "\t-S\toutput data is SNBT\n"); + fprintf(stderr, "\t-N\toutput data is NBT\n\n"); + fprintf(stderr, "\t-h --help\tprint the help message\n"); + fprintf(stderr, "\t-v --version\tprint the version\n"); + fprintf(stderr, "\t--in=\tset input file name\n"); + fprintf(stderr, "\t--out=\tset output file name\n"); + exit(0); +} + +int main(int argc, char **argv) { -int main (int argc, char** argv) { + flags_t flags; + parse_flags(&flags, argc, argv); - if (argc != 2) { - printf("usage: nbtvis file.nbt\n"); - return 0; - } + if (flags.help) + help(); - stream_t stream = stream_open(argv[1], "rb"); + if (flags.version) + version(); tag_t tag; - if (tag_read(&tag, &stream, true) == false) - error_and_die("failed to read tag\n"); + if (tag_read(&tag, &flags.in, flags.fin) == false) + error_and_die("error: failed to read tag\n"); if (tag.type != TAG_COMPOUND) - error_and_die("root tag is not of type compound\n"); + error_and_die("error: nbt tag not a valid compound tag\n"); - tag_print(&tag); + if (tag_print(&tag, &flags.out, flags.fout) == false) + error_and_die("error: failed to write tag\n"); + tag_free(&tag); - stream_close(&stream); + return 0; } -- cgit v1.2.3-freya