elemental-on-terminal/src/loader.c
2025-04-02 20:37:18 -04:00

74 lines
No EOL
1.3 KiB
C

#include "map.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
int load_elements(hashmap *m, char *table, int use_inv) {
FILE *fptr;
fptr = fopen(table, "r");
if (fptr == NULL)
return 0;
char *str;
char *str2;
int did_something = 0;
while (1) {
str2 = calloc(MAX_FILE_SIZE, sizeof(char));
if (!fgets(str2, MAX_FILE_SIZE, fptr)) {
free(str2);
break;
}
str = calloc(strlen(str2) + 1, sizeof(char));
strcpy(str, str2);
free(str2);
did_something = 1;
if (use_inv == 1) {
hashmap_set(m, str, strlen(str) - 1, (uintptr_t)1, 0);
continue;
}
char *combo_o = strstr(str, ";");
combo_o[0] = '\0';
combo_o++;
char *combo = calloc(strlen(combo_o) + 1, sizeof(char));
strcpy(combo, combo_o);
if (use_inv == 3) {
uintptr_t result;
hashmap_get(m, str, strlen(str) - 1, &result);
char *res = (char *)result;
if (res != 0)
continue;
}
if (use_inv == 2 || use_inv == 3) {
hashmap_set(m, str, strlen(str) - 1, (uintptr_t)combo, 1);
continue;
}
hashmap_set(m, combo, strlen(combo) - 1, (uintptr_t)str, 1);
}
if (!did_something) {
free(str);
}
fclose(fptr);
return did_something;
// todo: properly free this
}