clean up I/O database

This commit is contained in:
biglyderv 2025-04-12 09:44:14 -04:00
parent d0553e6575
commit 881d3602e7
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
7 changed files with 87 additions and 50 deletions

3
.gitignore vendored
View file

@ -4,4 +4,5 @@
/inv.txt
/inv_users.txt
/polls.txt
/elem.tar.gz
/elem.tar.gz
/combos.txt

View file

@ -4,7 +4,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "loader.h"
#include "main.h"
// huge cleanup operation soon
@ -159,7 +159,7 @@ int handle_pages_int(char *command, char *invs) {
}
}
int suggest_command(char *command, char *command_re, hashmap *polls, char *name,
int suggest_command(char *command, char *command_re, hashmap *polls, hashmap *combos, char *name,
int was_combination) {
char *page = handle_pages(command, "/suggest ");
@ -204,12 +204,9 @@ int suggest_command(char *command, char *command_re, hashmap *polls, char *name,
printf("Poll was added into the game!\n");
FILE *fptr;
fptr = fopen("../elem_data/" combo_file, "a");
if (fptr == NULL)
return 1;
fwrite(val, sizeof(char), strlen(val), fptr);
fwrite("\n", sizeof(char), 1, fptr);
fclose(fptr);
hashmap_set(combos, command_re, strlen(command_re), (uintptr_t)page, 0);
write_elements(combos, "../elem_data/" COMBO_FILE, 0);
return 1;
}
@ -219,14 +216,7 @@ int suggest_command(char *command, char *command_re, hashmap *polls, char *name,
FILE *fptr;
fptr = fopen("../elem_data/" poll_file, "a");
if (fptr == NULL)
return 1;
fwrite(key, sizeof(char), strlen(key), fptr);
fwrite(";", sizeof(char), 1, fptr);
fwrite(val, sizeof(char), strlen(val), fptr);
fwrite("\n", sizeof(char), 1, fptr);
fclose(fptr);
write_elements(polls, "../elem_data/" POLL_FILE, 3);
return 1;
}

View file

@ -2,7 +2,7 @@
int get_command(char *command, char *command_re, char **sort_tmp);
int slash_command(char *command, hashmap *inv);
int suggest_command(char *command, char *command_re, hashmap *polls,
char *name, int was_combination);
hashmap *combos, char *name, int was_combination);
int help_command(char *command);
int polls_command(char *command, hashmap *polls, hashmap *cmd);
int path_command(char *command, hashmap *elements_rev, hashmap *already_done,

View file

@ -6,7 +6,56 @@
#include "main.h"
int load_elements(hashmap *m, char *table, int use_inv) {
struct write_struct {
FILE *fptr;
int mode;
};
int entry_handler(const void *key, size_t size, uintptr_t val, void *usr) {
struct write_struct *usr2 = (struct write_struct *)usr;
char *val2 = (char *)val;
char *key2 = (char *)key;
if (usr2->mode == 0) {
fwrite(val2, sizeof(char), strlen(val2), usr2->fptr);
fwrite(";", sizeof(char), 1, usr2->fptr);
fwrite(key2, sizeof(char), strlen(key2), usr2->fptr);
if (key2[strlen(key2) - 1] != '\n') {
fwrite("\n", sizeof(char), 1, usr2->fptr);
}
} else if (usr2->mode == 1) {
fwrite(key2, sizeof(char), strlen(key2), usr2->fptr);
fwrite("\n", sizeof(char), 1, usr2->fptr);
} else {
fwrite(key2, sizeof(char), strlen(key2), usr2->fptr);
fwrite(";", sizeof(char), 1, usr2->fptr);
fwrite(val2, sizeof(char), strlen(val2), usr2->fptr);
if (val2[strlen(val2) - 1] != '\n') {
fwrite("\n", sizeof(char), 1, usr2->fptr);
}
}
return 1;
}
void write_elements(hashmap *m, char *table, int mode) {
FILE *fptr;
fptr = fopen(table, "w");
if (fptr == NULL)
return;
struct write_struct writer = {.fptr = fptr, .mode = mode};
hashmap_iterate(m, entry_handler, (void *)&writer);
//fwrite("\n", sizeof(char), 1, fptr);
fclose(fptr);
}
int load_elements(hashmap *m, char *table, int mode) {
FILE *fptr;
fptr = fopen(table, "r");
@ -19,6 +68,8 @@ int load_elements(hashmap *m, char *table, int use_inv) {
int did_something = 0;
int lines_get = 0;
while (1) {
str2 = calloc(MAX_FILE_SIZE, sizeof(char));
if (!fgets(str2, MAX_FILE_SIZE, fptr)) {
@ -26,13 +77,15 @@ int load_elements(hashmap *m, char *table, int use_inv) {
break;
}
lines_get++;
str = calloc(strlen(str2) + 1, sizeof(char));
strcpy(str, str2);
free(str2);
did_something = 1;
if (use_inv == 1) {
if (mode == 1) {
hashmap_set(m, str, strlen(str) - 1, (uintptr_t)1, 0);
continue;
}
@ -44,7 +97,7 @@ int load_elements(hashmap *m, char *table, int use_inv) {
char *combo = calloc(strlen(combo_o) + 1, sizeof(char));
strcpy(combo, combo_o);
if (use_inv == 3) {
if (mode == 3) {
uintptr_t result;
hashmap_get(m, str, strlen(str) - 1, &result);
@ -54,7 +107,7 @@ int load_elements(hashmap *m, char *table, int use_inv) {
continue;
}
if (use_inv == 2 || use_inv == 3) {
if (mode == 2 || mode == 3) {
hashmap_set(m, str, strlen(str) - 1, (uintptr_t)combo, 1);
continue;
}

View file

@ -1,2 +1,3 @@
#include "map.h"
int load_elements(hashmap *m, char *table, int use_inv);
int load_elements(hashmap *m, char *table, int mode);
void write_elements(hashmap *m, char *table, int mode);

View file

@ -12,24 +12,24 @@
void init_tables(hashmap *elements, hashmap *inv, hashmap *polls,
hashmap *elements_rev, int do_inv) {
load_elements(elements, "../elem_data/" combo_file, 0);
load_elements(elements, combo_file, 0) ||
load_elements(elements, "bin/" combo_file, 0);
load_elements(elements, "../elem_data/" COMBO_FILE, 0);
load_elements(elements, COMBO_FILE, 0) ||
load_elements(elements, "bin/" COMBO_FILE, 0);
load_elements(elements_rev, "../elem_data/" combo_file, 3);
load_elements(elements_rev, combo_file, 3) ||
load_elements(elements_rev, "bin/" combo_file, 3);
load_elements(elements_rev, "../elem_data/" COMBO_FILE, 3);
load_elements(elements_rev, COMBO_FILE, 3) ||
load_elements(elements_rev, "bin/" COMBO_FILE, 3);
load_elements(polls, "../elem_data/" poll_file, 3);
load_elements(polls, "../elem_data/" POLL_FILE, 3);
if (!do_inv)
return;
load_elements(inv, "../elem_data/" inv_base_file, 1) ||
load_elements(inv, inv_base_file, 1) ||
load_elements(inv, "bin/" inv_base_file, 1);
load_elements(inv, "../elem_data/" INV_BASE_FILE, 1) ||
load_elements(inv, INV_BASE_FILE, 1) ||
load_elements(inv, "bin/" INV_BASE_FILE, 1);
load_elements(inv, inv_file, 1);
load_elements(inv, INV_FILE, 1);
}
int main(int argc, char *argv[]) {
@ -81,7 +81,7 @@ int main(int argc, char *argv[]) {
}
if (was_combination &&
suggest_command(command, command_re, polls, name, was_combination)) {
suggest_command(command, command_re, polls, elements, name, was_combination)) {
continue;
}
was_combination = 0;
@ -148,15 +148,7 @@ int main(int argc, char *argv[]) {
hashmap_set(inv, res_str, strlen(res_str), (uintptr_t)1, 0);
printf("You made %s!\n", res_str);
FILE *fptr;
fptr = fopen(inv_file, "a");
if (fptr == NULL)
continue;
fwrite(res_str, sizeof(char), strlen(res_str), fptr);
fwrite("\n", sizeof(char), 1, fptr);
fclose(fptr);
write_elements(inv, INV_FILE, 1);
}
// free(command);

View file

@ -1,9 +1,9 @@
#define inv_file "inv.txt"
#define inv_base_file "inv_base.txt"
#define combo_file "combos.txt"
#define poll_file "polls.txt"
#define polls_lock_file "polls_lock.txt"
#define lb_file "lb.txt"
#define INV_FILE "inv.txt"
#define INV_BASE_FILE "inv_base.txt"
#define COMBO_FILE "combos.txt"
#define POLL_FILE "polls.txt"
#define POLLS_LOCK_FILE "polls_lock.txt"
#define LB_FILE "lb.txt"
#define MAX_BUF_LENGTH 1024
#define MAX_COMBO_LENGTH 1024