don't initialize gfx if we're in terminal mode

This commit is contained in:
silverweed 2024-07-25 17:26:39 +02:00
parent 5d9acf702c
commit 5660b4ca05
2 changed files with 128 additions and 124 deletions

106
src/argparse.cpp Normal file
View file

@ -0,0 +1,106 @@
internal
void print_help(const char *argv0)
{
fprintf(stderr,
"rntviewer v" V_MAJOR "." V_MINOR " by silverweed"
"\n"
"\nUsage: %s [-t] [-s START] [-l LEN] [-w WIDTH] <ntuple_name> <ntuple_file.root>"
"\n\t-t: no graphics, output to terminal"
"\n\t-s: set first displayed byte to START"
"\n\t-l: display LEN bytes (only in terminal mode)"
"\n\t-w: display WIDTH bytes per column"
"\n"
, argv0);
}
struct Cmdline_Args {
b8 print_to_terminal;
b8 show_help_and_exit;
u64 start_addr;
u64 nbytes_displayed;
u16 n_cols;
String8 ntpl_name;
String8 file_name;
};
struct Conv_Res {
u64 num;
b8 error;
};
internal
Conv_Res str_to_u64(String8 s)
{
Conv_Res res {};
for (u64 i = 0; i < s.size; ++i) {
u8 c = s.str[i];
if (c >= '0' && c <= '9') {
res.num *= 10;
res.num += c - '0';
} else {
res.error = true;
break;
}
}
return res;
}
internal
void parse_int_arg(i32 &cur_arg_idx, i32 argc, char **argv, u64 &out)
{
const char *arg = argv[cur_arg_idx];
if (cur_arg_idx < argc - 1) {
String8 nxt_arg = str8_from_c(argv[++cur_arg_idx]);
Conv_Res res = str_to_u64(nxt_arg);
if (res.error)
fprintf(stderr, "Invalid integer after %s flag.\n", arg);
else
out = res.num;
} else {
fprintf(stderr, "Argument required after %s flag.\n", arg);
}
}
internal
Cmdline_Args parse_args(i32 argc, char **argv)
{
Cmdline_Args args {};
if (argc < 3) {
args.show_help_and_exit = true;
return args;
}
// TODO: refactor this probably
for (i32 i = 1; i < argc; ++i) {
String8 arg = str8_from_c(argv[i]);
if (arg.str[0] == '-') {
if (arg.size == 2) {
if (arg.str[1] == 't')
args.print_to_terminal = true;
else if (arg.str[1] == 's')
parse_int_arg(i, argc, argv, args.start_addr);
else if (arg.str[1] == 'l')
parse_int_arg(i, argc, argv, args.nbytes_displayed);
else if (arg.str[1] == 'w') {
u64 n_cols = 0;
parse_int_arg(i, argc, argv, n_cols);
args.n_cols = (u16)n_cols;
}
else
args.show_help_and_exit = true;
} else {
args.show_help_and_exit = true;
}
} else if (args.ntpl_name.size) {
if (args.file_name.size)
args.show_help_and_exit = true;
else
args.file_name = arg;
} else {
args.ntpl_name = arg;
}
}
return args;
}

View file

@ -54,6 +54,7 @@ namespace chr = std::chrono;
#include "rntuple.cpp"
#include "render.cpp"
#include "mainloop.cpp"
#include "argparse.cpp"
internal
b8 init_imgui(GLFWwindow* window) {
@ -78,125 +79,13 @@ void app_cleanup(App_State &app)
if (app.inspected_file.stream) fclose(app.inspected_file.stream);
}
internal
void print_help(const char *argv0)
{
fprintf(stderr,
"rntviewer v" V_MAJOR "." V_MINOR " by silverweed"
"\n"
"\nUsage: %s [-t] [-s START] [-l LEN] [-w WIDTH] <ntuple_name> <ntuple_file.root>"
"\n\t-t: no graphics, output to terminal"
"\n\t-s: set first displayed byte to START"
"\n\t-l: display LEN bytes (only in terminal mode)"
"\n\t-w: display WIDTH bytes per column"
"\n"
, argv0);
}
struct Cmdline_Args {
b8 print_to_terminal;
b8 show_help_and_exit;
u64 start_addr;
u64 nbytes_displayed;
u16 n_cols;
String8 ntpl_name;
String8 file_name;
};
struct Conv_Res {
u64 num;
b8 error;
};
internal
Conv_Res str_to_u64(String8 s)
{
Conv_Res res {};
for (u64 i = 0; i < s.size; ++i) {
u8 c = s.str[i];
if (c >= '0' && c <= '9') {
res.num *= 10;
res.num += c - '0';
} else {
res.error = true;
break;
}
}
return res;
}
internal
void parse_int_arg(i32 &cur_arg_idx, i32 argc, char **argv, u64 &out)
{
const char *arg = argv[cur_arg_idx];
if (cur_arg_idx < argc - 1) {
String8 nxt_arg = str8_from_c(argv[++cur_arg_idx]);
Conv_Res res = str_to_u64(nxt_arg);
if (res.error)
fprintf(stderr, "Invalid integer after %s flag.\n", arg);
else
out = res.num;
} else {
fprintf(stderr, "Argument required after %s flag.\n", arg);
}
}
internal
Cmdline_Args parse_args(i32 argc, char **argv)
{
Cmdline_Args args {};
if (argc < 3) {
args.show_help_and_exit = true;
return args;
}
// TODO: refactor this probably
for (i32 i = 1; i < argc; ++i) {
String8 arg = str8_from_c(argv[i]);
if (arg.str[0] == '-') {
if (arg.size == 2) {
if (arg.str[1] == 't')
args.print_to_terminal = true;
else if (arg.str[1] == 's')
parse_int_arg(i, argc, argv, args.start_addr);
else if (arg.str[1] == 'l')
parse_int_arg(i, argc, argv, args.nbytes_displayed);
else if (arg.str[1] == 'w') {
u64 n_cols = 0;
parse_int_arg(i, argc, argv, n_cols);
args.n_cols = (u16)n_cols;
}
else
args.show_help_and_exit = true;
} else {
args.show_help_and_exit = true;
}
} else if (args.ntpl_name.size) {
if (args.file_name.size)
args.show_help_and_exit = true;
else
args.file_name = arg;
} else {
args.ntpl_name = arg;
}
}
return args;
}
int main(int argc, char **argv)
{
// Prepare thread context
Thread_Ctx tctx;
tctx_init(tctx);
defer { tctx_release(); };
// Parse cmdline
Cmdline_Args args = parse_args(argc, argv);
if (args.show_help_and_exit || !args.ntpl_name.size || !args.file_name.size) {
print_help(argv[0]);
return 1;
}
// Allocate program memory
Arena *arena = arena_alloc();
if (!arena) {
@ -205,19 +94,14 @@ int main(int argc, char **argv)
}
defer { arena_release(arena); };
// Init imgui and GLFW
GLFWwindow *window = init_glfw(800, 600);
if (!window) {
fprintf(stderr, "Failed to init GLFW\n");
return 1;
}
defer { glfwTerminate(); };
if (!init_imgui(window)) {
fprintf(stderr, "Failed to init Imgui\n");
// Parse cmdline
Cmdline_Args args = parse_args(argc, argv);
if (args.show_help_and_exit || !args.ntpl_name.size || !args.file_name.size) {
print_help(argv[0]);
return 1;
}
// Create app state
App_State app {};
defer { app_cleanup(app); };
@ -228,6 +112,7 @@ int main(int argc, char **argv)
// Watch file for changes (to adapt the displayed file size - otherwise
// we may try to access invalid memory when the file gets shrunk)
if (!args.print_to_terminal)
os_start_file_watch(args.file_name, app);
}
@ -246,6 +131,19 @@ int main(int argc, char **argv)
return 0;
}
// Init imgui and GLFW
GLFWwindow *window = init_glfw(800, 600);
if (!window) {
fprintf(stderr, "Failed to init GLFW\n");
return 1;
}
defer { glfwTerminate(); };
if (!init_imgui(window)) {
fprintf(stderr, "Failed to init Imgui\n");
return 1;
}
// Start main loop
run_main_loop(window, arena, app);