From 7e6c14a2f6e9e9cbac3431f3085c42bbe474d13d Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 29 Aug 2024 15:51:33 +0200 Subject: Refactor tools to remove duplicate code --- tools/tool.h | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 tools/tool.h (limited to 'tools/tool.h') diff --git a/tools/tool.h b/tools/tool.h new file mode 100644 index 0000000..d75342d --- /dev/null +++ b/tools/tool.h @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "../src/nissy.h" + +static void +log_stderr(const char *str, ...) +{ + va_list args; + + va_start(args, str); + vfprintf(stderr, str, args); + va_end(args); +} + +static double +timerun(void (*run)(void), char *name) +{ + struct timespec start, end; + double tdiff, tdsec, tdnano; + + printf("\n"); + fflush(stdout); + + if (run == NULL) { + printf("> %s: nothing to run!\n", name); + fflush(stdout); + return -1.0; + } + + printf("Running tool: %s\n", name); + printf("==========\n"); + fflush(stdout); + + clock_gettime(CLOCK_MONOTONIC, &start); + run(); + clock_gettime(CLOCK_MONOTONIC, &end); + + tdsec = end.tv_sec - start.tv_sec; + tdnano = end.tv_nsec - start.tv_nsec; + tdiff = tdsec + 1e-9 * tdnano; + + printf("==========\n"); + printf("\nTotal time: %.4fs\n", tdiff); + fflush(stdout); + + return tdiff; +} + +static int +getdata( + const char *solver, + const char *options, + char **buf, + const char *filename +) { + int64_t s, size, sizeread; + FILE *f; + + if ((size = nissy_datasize(solver, options)) == -1) { + printf("Error in datasize\n"); + goto getdata_error_nofree; + } + + *buf = malloc(size); + + if ((f = fopen(filename, "rb")) == NULL) { + fprintf(stderr, "Table file not found, generating them." + " This can take a while.\n"); + s = nissy_gendata(solver, options, *buf); + if (s != size) { + fprintf(stderr, "Error generating table"); + if (s != -1) + fprintf(stderr, " (got %" PRId64 " bytes)", s); + fprintf(stderr, "\n"); + goto getdata_error; + } + if ((f = fopen(filename, "wb")) == NULL) { + fprintf(stderr, "Could not write tables to file %s" + ", will be regenerated next time.\n", filename); + } else { + fwrite(*buf, size, 1, f); + fclose(f); + } + } else { + fprintf(stderr, "Reading tables from file %s\n", filename); + sizeread = fread(*buf, size, 1, f); + fclose(f); + if (sizeread != 1) { + fprintf(stderr, "Error reading table, stopping\n"); + goto getdata_error; + } + } + + return 0; + +getdata_error: + free(*buf); +getdata_error_nofree: + return 1; +} -- cgit v1.3