This commit is contained in:
biglyderv 2025-03-24 20:12:37 -04:00
parent af961dd144
commit 8740f23c0a
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
6 changed files with 104 additions and 80 deletions

View file

@ -2,4 +2,4 @@ CC?=gcc
BIN?=elem
make: src/main.c
$(CC) -o $(BIN) src/map.c src/main.c
$(CC) -o $(BIN) src/map.c src/loader.c src/command.c src/main.c

68
src/command.c Normal file
View file

@ -0,0 +1,68 @@
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BUF_LENGTH 1024
#define MAX_COMBO_LENGTH 1024
int fix_delim(char *command, char *needle) {
char *delim = strstr(command, needle);
if (!delim)
return 0;
delim[0] = ';';
return 1;
}
int sort_comp(const void *a, const void *b) {
return strcmp(*(char **)a, *(char **)b);
}
char *get_command(char *command, char *command_re, char **sort_tmp) {
fgets(command, MAX_BUF_LENGTH - 1, stdin);
while (1) {
int get_out = 0;
get_out |= fix_delim(command, "+");
get_out |= fix_delim(command, ",");
if (!get_out)
break;
}
int cl = strlen(command);
command[cl - 1] = '\0';
for (int i = 0; i < cl - 1; i++) {
command[i] = tolower(command[i]);
}
sort_tmp[0] = command;
int combos = 0;
for (int i = 1; i < MAX_COMBO_LENGTH; i++) {
combos = i;
sort_tmp[i] = strstr(command, ";");
if (sort_tmp[i] == 0)
break;
sort_tmp[i][0] = '\0';
sort_tmp[i]++;
}
qsort(sort_tmp, combos, sizeof(char *), sort_comp);
char *command_re_tmp = command_re;
for (int i = 0; i < combos; i++) {
if (i != 0) {
command_re_tmp[0] = ';';
command_re_tmp++;
}
char *last = sort_tmp[i];
strcpy(command_re_tmp, last);
command_re_tmp += strlen(last);
}
return command_re;
}

1
src/command.h Normal file
View file

@ -0,0 +1 @@
char *get_command(char *command, char *command_re, char **sort_tmp);

29
src/loader.c Normal file
View file

@ -0,0 +1,29 @@
#include "map.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILE_SIZE 1024 * 16
void load_elements(hashmap *m, char *table) {
FILE *fptr;
fptr = fopen(table, "r");
char *str;
while (1) {
str = calloc(MAX_FILE_SIZE, sizeof(char));
if (!fgets(str, MAX_FILE_SIZE, fptr))
break;
char *combo = strstr(str, ";");
combo[0] = '\0';
combo++;
hashmap_set(m, combo, strlen(combo) - 1, (uintptr_t)str);
}
// todo: properly free this
}

2
src/loader.h Normal file
View file

@ -0,0 +1,2 @@
#include "map.h"
void load_elements(hashmap *m, char *table);

View file

@ -1,51 +1,13 @@
#include "map.h"
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "command.h"
#include "loader.h"
#define MAX_FILE_SIZE 1024 * 16
#define MAX_BUF_LENGTH 1024
#define MAX_COMBO_LENGTH 1024
void load_elements(hashmap *m, char *table) {
FILE *fptr;
fptr = fopen(table, "r");
char *str;
while (1) {
str = calloc(MAX_FILE_SIZE, sizeof(char));
if (!fgets(str, MAX_FILE_SIZE, fptr))
break;
char *combo = strstr(str, ";");
combo[0] = '\0';
combo++;
hashmap_set(m, combo, strlen(combo) - 1, (uintptr_t)str);
}
// todo: properly free this
}
int fix_delim(char *command, char *needle) {
char *delim = strstr(command, needle);
if (!delim)
return 0;
delim[0] = ';';
return 1;
}
int sort_comp(const void *a, const void *b) {
return strcmp(*(char **)a, *(char **)b);
}
int main(int argc, char *argv[]) {
hashmap *elements = hashmap_create();
hashmap *inv = hashmap_create();
@ -58,46 +20,8 @@ int main(int argc, char *argv[]) {
while (1) {
// todo: separate into functions
fgets(command, MAX_BUF_LENGTH - 1, stdin);
while (1) {
int get_out = 0;
get_out |= fix_delim(command, "+");
get_out |= fix_delim(command, ",");
if (!get_out)
break;
}
int cl = strlen(command);
command[cl - 1] = '\0';
for (int i = 0; i < cl - 1; i++) {
command[i] = tolower(command[i]);
}
sort_tmp[0] = command;
int combos = 0;
for (int i = 1; i < MAX_COMBO_LENGTH; i++) {
combos = i;
sort_tmp[i] = strstr(command, ";");
if (sort_tmp[i] == 0)
break;
sort_tmp[i][0] = '\0';
sort_tmp[i]++;
}
qsort(sort_tmp, combos, sizeof(char *), sort_comp);
char *command_re_tmp = command_re;
for (int i = 0; i < combos; i++) {
if (i != 0) {
command_re_tmp[0] = ';';
command_re_tmp++;
}
char *last = sort_tmp[i];
strcpy(command_re_tmp, last);
command_re_tmp += strlen(last);
}
command_re = get_command(command, command_re, sort_tmp);
uintptr_t result;
hashmap_get(elements, command_re, strlen(command_re), &result);