summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 010a2624eb304c7fdfe9f5ee0a3f6c84a09f3289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "lib.h"
#include "stream.h"
#include "flags.h"

#include <stdio.h>
#include <stdlib.h>

__attribute__((__noreturn__))
void version(void) {
	fprintf(stderr, "nbtvis v0.0.1\n");
	fprintf(stderr, "Copyright (C) 2023 Freya Murphy\n");
	exit(0);
}

__attribute__((__noreturn__))
void help(void) {
	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=<file>\tset input file name\n");
	fprintf(stderr, "\t--out=<file>\tset output file name\n");
	exit(0);
}

int main(int argc, char **argv) {

	flags_t flags;
	parse_flags(&flags, argc, argv);

	if (flags.help)
		help();

	if (flags.version)
		version();

	tag_t tag;
	if (tag_read(&tag, &flags.in, flags.fin) == false)
		error_and_die("error: failed to read tag\n");
	stream_close(&flags.in);

	if (tag.type != TAG_COMPOUND)
		error_and_die("error: nbt tag not a valid compound tag\n");

	if (tag_print(&tag, &flags.out, flags.fout) == false)
		error_and_die("error: failed to write tag\n");
	stream_close(&flags.out);
	
	tag_free(&tag);

	return 0;
}