From 2dc9d118efb64de6ea54a5a9eb4474f8e5ef3145 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Thu, 29 Feb 2024 01:50:44 +0100 Subject: Initial commit --- tsalis/src/tsalis.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 tsalis/src/tsalis.c (limited to 'tsalis/src/tsalis.c') diff --git a/tsalis/src/tsalis.c b/tsalis/src/tsalis.c new file mode 100644 index 0000000..ab24c61 --- /dev/null +++ b/tsalis/src/tsalis.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +#include "printer.h" +#include "handler.h" +#include "tsalis.h" + +#define DEFAULT_ORDER 16 + +sbool g_exit; +sbool g_running; +sword g_autoSaveInterval; +char g_simName[NAME_MAX_SIZE + 1] = "def.sim"; + +static void +onDefault(void) +{ + FILE *testFile = fopen(g_simName, "r"); + + if (testFile) { + fclose(testFile); + s_load(g_simName); + } else { + s_init(DEFAULT_ORDER); + } +} + +static void +onLoad(const char *fileName) +{ + FILE *testFile; + + if (strlen(fileName) > NAME_MAX_SIZE) { + fputs("ERROR: File name too long", stderr); + exit(1); + } + + strncpy(g_simName, fileName, NAME_MAX_SIZE); + testFile = fopen(g_simName, "r"); + + if (testFile) { + fclose(testFile); + s_load(g_simName); + } else { + fputs("ERROR: File does not exist", stderr); + exit(1); + } +} + +static void +init(int argc, char **argv) +{ + if (argc == 1) { + onDefault(); + } else if (argc == 2) { + char cmd = argv[1][0]; + char *val = &argv[1][1]; + + if (cmd == 'n') { + s_init(atoi(val)); + } else if (cmd == 'l') { + onLoad(val); + } else { + fputs("ERROR: Incorrect arguments", stderr); + exit(1); + } + } else { + fputs("ERROR: Incorrect argument count", stderr); + exit(1); + } + + tsp_init(); +} + +static void +exec(void) +{ + while (!g_exit) { + if (g_running) { + clock_t beg = clock(); + clock_t end; + float delay; + + do { + s_cycle(); + + if (g_autoSaveInterval && !(s_getCycle() % g_autoSaveInterval)) { + s_save(g_simName); + } + + end = clock(); + delay = (float)(end - beg) / CLOCKS_PER_SEC; + } while (delay < (1.0 / 60.0)); + } + + tsp_printData(); + tsh_handleEvent(getch()); + } +} + +static void +quit(void) +{ + tsp_quit(); + s_save(g_simName); + s_quit(); +} + +int +main(int argc, char **argv) +{ + init(argc, argv); + exec(); + quit(); + return 0; +} -- cgit v1.2.1