diff options
| author | Mosder <xmosder12@gmail.com> | 2026-07-14 17:25:04 +0200 |
|---|---|---|
| committer | Mosder <xmosder12@gmail.com> | 2026-07-14 17:25:04 +0200 |
| commit | 189fd56b24859a89d71899e070f885e93ffabd97 (patch) | |
| tree | 41c1ed6470731a959d0168951b3c156071800ffa | |
| parent | c7277e7654c0d8d0e7a4665373c0762c6e8a038d (diff) | |
feat: created converter from CSV files to a C header files containing static arrays. Used for creating ID to STR conversion for saturations and items.
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 8 | ||||
| -rw-r--r-- | analyzer/parser/fight_results.c | 14 | ||||
| -rw-r--r-- | csv/items.csv | 4 | ||||
| -rw-r--r-- | csv/saturations.csv | 8 | ||||
| -rw-r--r-- | csv_to_headers.c | 79 |
6 files changed, 111 insertions, 3 deletions
@@ -1,3 +1,4 @@ compile_commands.json sample_data/ analyzer/headers/server.h +analyzer/csv/ @@ -7,10 +7,15 @@ global: compile local: compile mv branal ${HOME}/.local/bin -compile: structures utils parser capture analyzer +compile: csv structures utils parser capture analyzer $(CC) $(CFLAGS) -lpcap *.o main.c -o branal rm *.o +csv: + gcc csv_to_headers.c -o csv_to_headers + ./csv_to_headers + rm csv_to_headers + structures: $(CC) $(CFLAGS) -c analyzer/structures/hashmap.c $(CC) $(CFLAGS) -c analyzer/structures/heap.c @@ -32,3 +37,4 @@ analyzer: $(CC) $(CFLAGS) -c analyzer/analyzer.c .PHONY: analyzer +.PHONY: csv diff --git a/analyzer/parser/fight_results.c b/analyzer/parser/fight_results.c index 65e72ea..b3227e2 100644 --- a/analyzer/parser/fight_results.c +++ b/analyzer/parser/fight_results.c @@ -4,6 +4,9 @@ #include <stdlib.h> #include <string.h> +#include "csv/id_to_item_name.h" +#include "csv/id_to_saturation_type.h" + // function to cleanup fight results to be used in ParsedData struct void cleanup_fight_results(void *data) { FightResults *fight_results = data; @@ -79,6 +82,14 @@ EntityResults get_entity_results(byte_t *data, size_t length) { return results; } +char *get_item_name(int item_id) { + return item_id < id_to_item_name_len ? id_to_item_name[item_id] : "item_id out of bounds"; +} + +char *get_saturation_type(int saturation_id) { + return saturation_id < id_to_saturation_type_len ? id_to_saturation_type[saturation_id] : "saturation_id out of bounds"; +} + // parse EntityResults to FriendlyResults FriendlyResults parse_to_friendly(EntityResults entity_results) { FriendlyResults results; @@ -100,8 +111,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); - // TODO: determine type - results.saturation_type = "typ"; + results.saturation_type = get_saturation_type(saturation_id); } return results; diff --git a/csv/items.csv b/csv/items.csv new file mode 100644 index 0000000..904454d --- /dev/null +++ b/csv/items.csv @@ -0,0 +1,4 @@ +id,item_name +1222,Daimonit +1219,Trop trwogi +986,KrysztaĆ czasu diff --git a/csv/saturations.csv b/csv/saturations.csv new file mode 100644 index 0000000..7179ab8 --- /dev/null +++ b/csv/saturations.csv @@ -0,0 +1,8 @@ +id,saturation_type +1,type1 +2,type2 +3,type3 +4,type4 +5,type5 +6,type6 +7,type7 diff --git a/csv_to_headers.c b/csv_to_headers.c new file mode 100644 index 0000000..5845daf --- /dev/null +++ b/csv_to_headers.c @@ -0,0 +1,79 @@ +#include <dirent.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> + +#define EXT ".csv" +#define EXT_LEN strlen(".csv") +#define CSV_PATH "csv" +#define HEADER_PATH "analyzer/csv" + +#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 field names + char from[BUFFER_SIZE], to[BUFFER_SIZE]; + fscanf(input_fp, "%[^,],%[^\n]", from, to); + + // read values from CSV + char from_to_array[MAX_ARR_SIZE][BUFFER_SIZE] = {""}; + size_t max_index = 0; + size_t val1; + char val2[BUFFER_SIZE]; + while (fscanf(input_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); + + // 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"); + + // 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 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]); + else + fprintf(output_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); + + // close the output file + fclose(output_fp); +} + +int main() { + // make directory for csv header files + mkdir(HEADER_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 + if (file_name_len > EXT_LEN && strcmp(file_name + file_name_len - EXT_LEN, EXT) == 0) + create_header(entry->d_name); + } + closedir(csv_dir); + return 0; +} |
