add inv saving, fix bugs

This commit is contained in:
biglyderv 2025-03-25 19:48:55 -04:00
parent 4bef1a220c
commit d5e55b54ee
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
5 changed files with 26 additions and 10 deletions

3
.gitignore vendored
View file

@ -1,2 +1,3 @@
/bin/elem
/bin/elem.exe
/bin/elem.exe
/inv.txt

View file

@ -26,7 +26,7 @@ int load_elements(hashmap *m, char *table, int use_inv) {
did_something = 1;
if (use_inv) {
hashmap_set(m, str, strlen(str) - 2, (uintptr_t)1);
hashmap_set(m, str, strlen(str) - 1, (uintptr_t)1);
continue;
}
@ -37,13 +37,12 @@ int load_elements(hashmap *m, char *table, int use_inv) {
hashmap_set(m, combo, strlen(combo) - 1, (uintptr_t)str);
}
printf("%i\n",did_something);
if (!did_something) {
free(str);
}
fclose(fptr);
return did_something;
// todo: properly free this

View file

@ -5,6 +5,10 @@
#include <stdlib.h>
#include <string.h>
#define inv_file "inv.txt"
#define inv_base_file "inv_base.txt"
#define combo_file "combos.txt"
#define MAX_BUF_LENGTH 1024
#define MAX_COMBO_LENGTH 1024
@ -16,9 +20,12 @@ int main(int argc, char *argv[]) {
char *command_re = calloc(MAX_BUF_LENGTH, sizeof(char));
char **sort_tmp = calloc(MAX_COMBO_LENGTH, sizeof(char **));
load_elements(elements, "combos.txt", 0) ||
load_elements(elements, "bin/combos.txt", 0);
load_elements(inv, "inv.txt", 1) || load_elements(inv, "bin/inv.txt", 1);
load_elements(elements, combo_file, 0) ||
load_elements(elements, "bin/" combo_file, 0);
load_elements(inv, inv_base_file, 1) ||
load_elements(inv, "bin/" inv_base_file, 1);
load_elements(inv, inv_file, 1);
printf("Welcome to Elemental on Command Line!\n");
while (1) {
@ -35,7 +42,7 @@ int main(int argc, char *argv[]) {
int failed = 0;
for (int i = 0; i < combos; i++) {
uintptr_t result;
hashmap_get(inv, sort_tmp[i], strlen(sort_tmp[i]) - 1, &result);
hashmap_get(inv, sort_tmp[i], strlen(sort_tmp[i]), &result);
if (result != 1) {
printf("You don't have %s.\n", sort_tmp[i]);
failed = 1;
@ -56,8 +63,17 @@ int main(int argc, char *argv[]) {
char *res_str = (char *)result;
hashmap_set(inv, res_str, strlen(res_str) - 1, (uintptr_t)1);
hashmap_set(inv, res_str, strlen(res_str), (uintptr_t)1);
printf("You made %s!\n", res_str);
FILE *fptr;
fptr = fopen(inv_file, "a");
if (fptr == NULL)
continue;
fwrite("\n",sizeof(char),1,fptr);
fwrite(res_str, sizeof(char), strlen(res_str), fptr);
fclose(fptr);
}
// free(command);

0
src/writer.c Normal file
View file