diff options
| author | Mosder <xmosder12@gmail.com> | 2026-07-21 22:48:20 +0200 |
|---|---|---|
| committer | Mosder <xmosder12@gmail.com> | 2026-07-21 22:48:20 +0200 |
| commit | 1f664d5b6396fb24c8ce4990ba8882118abb8dcd (patch) | |
| tree | 721108d0e5dd3a22019b865923c7b1a16488f82c | |
| parent | e20946d04232cdf9d21aac3a2e7352a548ba50ae (diff) | |
refactor: improved csv table generations
| -rw-r--r-- | Makefile | 5 | ||||
| -rw-r--r-- | analyzer/parser/fight_results/fight_results.c | 24 | ||||
| -rw-r--r-- | analyzer/parser/fight_results/fight_results.h | 2 | ||||
| -rw-r--r-- | analyzer/render.h | 2 | ||||
| -rw-r--r-- | csv_to_headers.c | 104 |
5 files changed, 82 insertions, 55 deletions
@@ -15,9 +15,10 @@ compile: csv structures utils parser capture analyzer render rm *.o csv: - $(CC) csv_to_headers.c -o csv_to_headers + $(CC) $(CFLAGS) csv_to_headers.c -o csv_to_headers ./csv_to_headers rm csv_to_headers + $(CC) $(CFLAGS) -c analyzer/csv/*.c structures: $(CC) $(CFLAGS) -c analyzer/structures/hashmap.c @@ -32,7 +33,7 @@ utils: parser: $(CC) $(CFLAGS) -c analyzer/parser/parser.c - $(CC) $(CFLAGS) -c analyzer/parser/fight_results.c + $(CC) $(CFLAGS) -c analyzer/parser/fight_results/fight_results.c capture: $(CC) $(CFLAGS) -c analyzer/capture.c diff --git a/analyzer/parser/fight_results/fight_results.c b/analyzer/parser/fight_results/fight_results.c index a994674..4f0f3a1 100644 --- a/analyzer/parser/fight_results/fight_results.c +++ b/analyzer/parser/fight_results/fight_results.c @@ -1,4 +1,4 @@ -#include "parser/fight_results.h" +#include "parser/fight_results/fight_results.h" #include <stdio.h> #include <stdlib.h> @@ -146,14 +146,6 @@ Orb get_orb(char *data, size_t length) { return orb; } -char *get_artifact_size(int artifact_size_id) { - return artifact_size_id < id_to_artifact_size_len ? id_to_artifact_size[artifact_size_id] : "null"; -} - -char *get_orb_name(int orb_name_id) { - return orb_name_id < id_to_orb_name_len ? id_to_orb_name[orb_name_id] : "null"; -} - void get_stars(char *buffer, int incr_above_b1) { char star_types[] = {'B', 'S', 'G'}; sprintf(buffer, " (%c%d)", star_types[incr_above_b1 / 3], incr_above_b1 % 3 + '1'); @@ -221,7 +213,7 @@ void add_gear(FriendlyResults *results, size_t *items_capacity, char *gears_str) Item item; char buffer[SINGLE_ITEM_BUFFER_LEN]; Orb orb = get_orb(gear.orb, strlen(gear.orb)); - sprintf(buffer, "%sorb %s", get_artifact_size(orb.size), get_orb_name(orb.id)); + sprintf(buffer, "%sorb %s", get_artifact_size_from_id(orb.size), get_orb_name_from_id(orb.id)); item.data = strdup(buffer); destroy_orb(orb); item.type = ORB; @@ -275,10 +267,6 @@ void add_drifs(FriendlyResults *results, size_t *items_capacity, char *drifs_str } while (drifs_separator); } -char *get_item_name(int item_id) { - return item_id < id_to_item_name_len ? id_to_item_name[item_id] : "null"; -} - void add_items(FriendlyResults *results, size_t *items_capacity, char *items_str) { if (strlen(items_str) == 0) return; @@ -290,7 +278,7 @@ void add_items(FriendlyResults *results, size_t *items_capacity, char *items_str int item_id, item_count; sscanf(items_str, "%d,%d", &item_id, &item_count); - sprintf(buffer, "%dx %s", item_count, get_item_name(item_id)); + sprintf(buffer, "%dx %s", item_count, get_item_name_from_id(item_id)); // if there are still items - modify items_str pointer if (items_separator) @@ -307,10 +295,6 @@ void add_items(FriendlyResults *results, size_t *items_capacity, char *items_str } while (items_separator); } -char *get_saturation_type(int saturation_id) { - return saturation_id < id_to_saturation_type_len ? id_to_saturation_type[saturation_id] : "null"; -} - // parse EntityResults to FriendlyResults FriendlyResults parse_to_friendly(EntityResults entity_results) { FriendlyResults results; @@ -334,7 +318,7 @@ FriendlyResults parse_to_friendly(EntityResults entity_results) { if (strlen(entity_results.saturation) > 0) { int saturation_id; sscanf(entity_results.saturation, "%d,%d", &saturation_id, &results.saturation); - results.saturation_type = get_saturation_type(saturation_id); + results.saturation_type = get_saturation_type_from_id(saturation_id); } return results; diff --git a/analyzer/parser/fight_results/fight_results.h b/analyzer/parser/fight_results/fight_results.h index 59d1e45..0618bf2 100644 --- a/analyzer/parser/fight_results/fight_results.h +++ b/analyzer/parser/fight_results/fight_results.h @@ -426,7 +426,7 @@ typedef struct { int level; int psycho; int splinters; - char *saturation_type; + const char *saturation_type; int saturation; } FriendlyResults; diff --git a/analyzer/render.h b/analyzer/render.h index 843cea5..1015eb9 100644 --- a/analyzer/render.h +++ b/analyzer/render.h @@ -1,7 +1,7 @@ // render.h // functions for "rendering" data onto the terminal -#include "parser/fight_results.h" +#include "parser/fight_results/fight_results.h" // ANSI escape sequence colors #define COLOR_GOLD 33 diff --git a/csv_to_headers.c b/csv_to_headers.c index 5595713..51a1c58 100644 --- a/csv_to_headers.c +++ b/csv_to_headers.c @@ -6,74 +6,116 @@ #define EXT ".csv" #define EXT_LEN strlen(".csv") #define CSV_PATH "csv" -#define HEADER_PATH "analyzer/" CSV_PATH +#define OUT_PATH "analyzer/" CSV_PATH #define BUFFER_SIZE 256 #define MAX_ARR_SIZE 4096 -void create_header(char *file_name) { - // open input file - char input_path[BUFFER_SIZE]; - sprintf(input_path, "%s/%s", CSV_PATH, file_name); - FILE *input_fp = fopen(input_path, "r"); +// read values from input csv and save them to buffers +// returns max_index of the array +int read_csv(char *path, char *from, char *to, char from_to_array[][BUFFER_SIZE]) { + FILE *fp = fopen(path, "r"); // read field names - char from[BUFFER_SIZE], to[BUFFER_SIZE]; - fscanf(input_fp, "%[^,],%[^\n]", from, to); + fscanf(fp, "%[^,],%[^\n]", from, to); - // read values from CSV - char from_to_array[MAX_ARR_SIZE][BUFFER_SIZE] = {""}; + // read values size_t max_index = 0; size_t val1; char val2[BUFFER_SIZE]; - while (fscanf(input_fp, "%zu,%[^\n]", &val1, val2) == 2) { + while (fscanf(fp, "%zu,%[^\n]", &val1, val2) == 2) { strcpy(from_to_array[val1], val2); if (val1 > max_index) max_index = val1; } - // close the input file - fclose(input_fp); + fclose(fp); + + return max_index; +} + +// create output .h file +void create_h(char *path, char *from, char *to) { + FILE *fp = fopen(path, "w"); + + // write comments and function prototype + fprintf(fp, "// %s\n", strstr(path, CSV_PATH)); + fprintf(fp, "// translate %s to %s - generated from csv\n\n", from, to); + fprintf(fp, "// get %s from %s\n", to, from); + fprintf(fp, "// params:\n"); + fprintf(fp, "// - %s - %s to translate from\n", from, from); + fprintf(fp, "// returns:\n"); + fprintf(fp, "// %s corresponding to %s\n", to, from); + fprintf(fp, "extern const char *get_%s_from_%s(int %s);", to, from, from); + + fclose(fp); +} + +// create output .c file +void create_c(char *path, char *h_path, char *from, char *to, char from_to_array[][BUFFER_SIZE], size_t max_index) { + FILE *fp = fopen(path, "w"); - // create output file - char output_path[BUFFER_SIZE]; - sprintf(output_path, "%s/%s_to_%s.h", HEADER_PATH, from, to); - FILE *output_fp = fopen(output_path, "w"); + char arr_name[BUFFER_SIZE], arr_len_name[BUFFER_SIZE]; + sprintf(arr_name, "%s_to_%s", from, to); + sprintf(arr_len_name, "%s_to_%s_len", from, to); - // write comments and beginning of output array - fprintf(output_fp, "// %s\n", input_path); - fprintf(output_fp, "// array translating %s to %s - generated from csv file\n\n", from, to); - fprintf(output_fp, "char *%s_to_%s[] = {\n", from, to); + // write include and beginning of array + fprintf(fp, "#include \"%s\"\n\n", strstr(h_path, CSV_PATH)); + fprintf(fp, "static const char *%s[] = {\n", arr_name); // write values for (size_t i = 0; i <= max_index; i++) { if (strlen(from_to_array[i]) > 0) - fprintf(output_fp, " \"%s\",\n", from_to_array[i]); + fprintf(fp, " \"%s\",\n", from_to_array[i]); else - fprintf(output_fp, " \"%zu\",\n", i); + fprintf(fp, " \"%zu\",\n", i); } // write the end of array and len - fprintf(output_fp, "};\n"); - fprintf(output_fp, "int %s_to_%s_len = %zu;", from, to, max_index + 1); + fprintf(fp, "};\n"); + fprintf(fp, "int %s = %zu;\n\n", arr_len_name, max_index + 1); - // close the output file - fclose(output_fp); + // write the getter + fprintf(fp, "const char *get_%s_from_%s(int %s) {\n", to, from, from); + fprintf(fp, " return %s >= 0 && %s < %s ? %s[%s] : \"null\";\n", from, from, arr_len_name, arr_name, from); + fprintf(fp, "}"); + + fclose(fp); +} + +void create_table(char *file_name) { + // read from input file + char input_path[BUFFER_SIZE]; + sprintf(input_path, "%s/%s", CSV_PATH, file_name); + char from[BUFFER_SIZE], to[BUFFER_SIZE]; + char from_to_array[MAX_ARR_SIZE][BUFFER_SIZE] = {0}; + size_t max_index = read_csv(input_path, from, to, from_to_array); + + // create output .h file + char h_path[BUFFER_SIZE]; + sprintf(h_path, "%s/%s_to_%s.h", OUT_PATH, from, to); + create_h(h_path, from, to); + + // create output .c file + char c_path[BUFFER_SIZE]; + sprintf(c_path, "%s/%s_to_%s.c", OUT_PATH, from, to); + create_c(c_path, h_path, from, to, from_to_array, max_index); } int main() { - // make directory for csv header files - mkdir(HEADER_PATH, 0755); + // make directory for output files + mkdir(OUT_PATH, 0755); DIR *csv_dir = opendir(CSV_PATH); struct dirent *entry; while ((entry = readdir(csv_dir))) { char *file_name = entry->d_name; size_t file_name_len = strlen(file_name); - // create header for each *.csv file + // create lookup tables for each *.csv file if (file_name_len > EXT_LEN && strcmp(file_name + file_name_len - EXT_LEN, EXT) == 0) - create_header(entry->d_name); + create_table(entry->d_name); } closedir(csv_dir); + return 0; } |
