elemental-on-terminal/src/loader.c
2025-03-27 15:56:47 -04:00

59 lines
No EOL
1 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;
int did_something = 0;
while (1) {
str = calloc(MAX_FILE_SIZE, sizeof(char));
if (!fgets(str, MAX_FILE_SIZE, fptr)) {
free(str);
break;
}
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 == 2) {
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
}