add paths

This commit is contained in:
biglyderv 2025-04-01 21:10:25 -04:00
parent e6c412e661
commit f1bc13d4e2
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
3 changed files with 75 additions and 5 deletions

View file

@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "main.h"
@ -236,6 +237,62 @@ int polls_command(char *command, hashmap *polls, hashmap *cmd) {
return 1;
}
int path_command(char *command, hashmap *elements_rev, hashmap *already_done,
int top) {
char *page;
if (top) {
page = handle_pages(command, "/path ");
if (page == 0)
return 0;
} else {
page = command;
}
page[strlen(page) - 1] = '\0';
if (strlen(page) == 0) return 1;
if (top) {
printf("Path of %s:\n", page);
hashmap_free(already_done);
already_done = hashmap_create();
}
uintptr_t result;
hashmap_get(already_done, page, strlen(page), &result);
if (result != 0)
return 1;
hashmap_set(already_done, page, strlen(page), (uintptr_t)1, 0);
hashmap_get(elements_rev, page, strlen(page) - 1, &result);
if (result == 0 || strlen((char *)result) < 1)
return 1;
printf("%s: %s", command, (char *)result);
// todo: refactor;
char *tmp = (char *)result, *tmp2 = 0;
int combos = 0;
for (int i = 1; i < MAX_COMBO_LENGTH; i++) {
combos = i;
tmp2 = tmp;
tmp = strstr(tmp, ";");
if (tmp == 0 || tmp2 == tmp)
break;
if (strlen(tmp) < 2) {
break;
}
tmp[0] = '\0';
tmp++;
path_command(tmp, elements_rev, already_done, 0);
}
return 1;
}
int slash_command(char *command, hashmap *inv) {
int page = handle_pages_int(command, "/inv ");

View file

@ -4,4 +4,6 @@ int slash_command(char *command, hashmap *inv);
int suggest_command(char *command, char *command_re, hashmap *polls,
char *name);
int help_command(char *command);
int polls_command(char *command, hashmap *polls, hashmap *cmd);
int polls_command(char *command, hashmap *polls, hashmap *cmd);
int path_command(char *command, hashmap *elements_rev, hashmap *already_done,
int top);

View file

@ -10,11 +10,16 @@
// todo: spacing in combos, clean init_tables
void init_tables(hashmap *elements, hashmap *inv, hashmap *polls, int do_inv) {
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_rev, "../elem_data/" combo_file, 2);
load_elements(elements_rev, combo_file, 2) ||
load_elements(elements_rev, "bin/" combo_file, 2);
load_elements(polls, "../elem_data/" poll_file, 2);
if (!do_inv)
@ -36,6 +41,8 @@ int main(int argc, char *argv[]) {
}
hashmap *elements = hashmap_create();
hashmap *elements_rev = hashmap_create();
hashmap *already_done = hashmap_create();
hashmap *inv = hashmap_create();
hashmap *polls = hashmap_create();
@ -45,9 +52,11 @@ int main(int argc, char *argv[]) {
int wasCombination = 0;
init_tables(elements, inv, polls, 1);
init_tables(elements, inv, polls, elements_rev, 1);
printf("user:%s, welcome to Elemental on Command Line!\nType /help for commands.\n", name);
printf("user:%s, welcome to Elemental on Command Line!\nType /help for "
"commands.\n",
name);
int newline = 1;
while (1) {
// todo: separate into functions
@ -82,6 +91,8 @@ int main(int argc, char *argv[]) {
continue;
if (slash_command(command, inv))
continue;
if (path_command(command, elements_rev, already_done, 1))
continue;
int combos = get_command(command, command_re, sort_tmp);
@ -106,7 +117,7 @@ int main(int argc, char *argv[]) {
hashmap_get(elements, command_re, strlen(command_re), &result);
if (result == 0) {
init_tables(elements, inv, polls, 0);
init_tables(elements, inv, polls, elements_rev, 0);
hashmap_get(elements, command_re, strlen(command_re), &result);
}