update stuff

This commit is contained in:
Maieul BOYER 2024-08-03 00:00:42 +02:00
parent 24b210fe86
commit 709c124028
No known key found for this signature in database
20 changed files with 176 additions and 64 deletions

View file

@ -7,6 +7,7 @@ signal_handler \
GEN_FILES = \ GEN_FILES = \
src/hashmap/env/env \ src/hashmap/env/env \
src/hashmap/env/env_clear \
src/hashmap/env/env_clone \ src/hashmap/env/env_clone \
src/hashmap/env/env_iter \ src/hashmap/env/env_iter \
src/hashmap/env/env_utils \ src/hashmap/env/env_utils \

View file

@ -6,7 +6,7 @@
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ # # By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/08/01 07:16:16 by maiboyer ### ########.fr # # Updated: 2024/08/02 22:42:24 by maiboyer ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 14:41:48 by rparodi #+# #+# */ /* Created: 2024/08/02 14:41:48 by rparodi #+# #+# */
/* Updated: 2024/08/02 18:51:16 by maiboyer ### ########.fr */ /* Updated: 2024/08/02 21:13:33 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -73,7 +73,7 @@ struct s_cmd_pipe
}; };
t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, t_i64 *out); t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, t_i64 *out);
t_error run_command(t_ast_command *command, t_state *state, t_command_result *out); t_error run_command(t_ast_command *command, t_state *state, t_cmd_pipe cmd_pipe, t_command_result *out);
t_error run_expansion(t_ast_expansion *self, t_state *state, t_expansion_result *out); t_error run_expansion(t_ast_expansion *self, t_state *state, t_expansion_result *out);
t_error run_word(t_ast_word *word, t_state *state, t_word_result *out); t_error run_word(t_ast_word *word, t_state *state, t_word_result *out);
@ -97,7 +97,7 @@ t_error run_raw_string(t_ast_raw_string *raw_string, t_state *state, void *out);
t_error run_regex(t_ast_regex *regex, t_state *state, void *out); t_error run_regex(t_ast_regex *regex, t_state *state, void *out);
t_error run_subshell(t_ast_subshell *subshell, t_state *state, void *out); t_error run_subshell(t_ast_subshell *subshell, t_state *state, void *out);
t_error run_until(t_ast_until *until, t_state *state, void *out); t_error run_until(t_ast_until *until, t_state *state, void *out);
t_error run_variable_assignment(t_ast_variable_assignment *variable_assignment, t_state *state, void *out); t_error run_variable_assignment(t_ast_variable_assignment *variable_assignment, t_state *state, bool is_temporary, void *out);
t_error run_while_(t_ast_while *while_, t_state *state, void *out); t_error run_while_(t_ast_while *while_, t_state *state, void *out);
#endif #endif

View file

@ -6,13 +6,14 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */ /* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */
/* Updated: 2024/08/02 19:04:30 by maiboyer ### ########.fr */ /* Updated: 2024/08/02 23:17:47 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "app/state.h" #include "app/state.h"
#include "ast/ast.h" #include "ast/ast.h"
#include "exec/_run_ast.h" #include "exec/_run_ast.h"
#include "app/env.h"
#include "exec/run.h" #include "exec/run.h"
#include "me/convert/numbers_to_str.h" #include "me/convert/numbers_to_str.h"
#include "me/fs/fs.h" #include "me/fs/fs.h"
@ -22,6 +23,7 @@
#include "me/str/str.h" #include "me/str/str.h"
#include "me/string/string.h" #include "me/string/string.h"
#include "me/types.h" #include "me/types.h"
#include "me/vec/vec_ast.h"
#include "me/vec/vec_estr.h" #include "me/vec/vec_estr.h"
#include "me/vec/vec_str.h" #include "me/vec/vec_str.h"
@ -77,7 +79,9 @@ t_error _get_expansion_value(t_ast_expansion *self, t_state *state, t_expansion_
if (self == NULL || state == NULL || out == NULL) if (self == NULL || state == NULL || out == NULL)
return (ERROR); return (ERROR);
hmap_ret = hmap_env_get(state->env, &self->var_name); hmap_ret = hmap_env_get(state->tmp_var, &self->var_name);
if (hmap_ret == NULL)
hmap_ret = hmap_env_get(state->env, &self->var_name);
ret = (t_expansion_result){.exists = hmap_ret == NULL, .value = NULL}; ret = (t_expansion_result){.exists = hmap_ret == NULL, .value = NULL};
if (ret.exists) if (ret.exists)
ret.value = str_clone(*hmap_ret); ret.value = str_clone(*hmap_ret);
@ -455,7 +459,6 @@ t_error _ast_into_str(t_ast_node self, t_state *state, t_vec_str *append)
// End Internals funcs // End Internals funcs
t_error run_expansion(t_ast_expansion *self, t_state *state, t_expansion_result *out); t_error run_expansion(t_ast_expansion *self, t_state *state, t_expansion_result *out);
t_error run_command(t_ast_command *command, t_state *state, t_command_result *out);
t_error run_word(t_ast_word *word, t_state *state, t_word_result *out); t_error run_word(t_ast_word *word, t_state *state, t_word_result *out);
t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, t_i64 *out); t_error run_arithmetic_expansion(t_ast_arithmetic_expansion *arithmetic_expansion, t_state *state, t_i64 *out);
@ -479,7 +482,7 @@ t_error run_raw_string(t_ast_raw_string *raw_string, t_state *state, void *out)
t_error run_regex(t_ast_regex *regex, t_state *state, void *out) NOT_DONE; t_error run_regex(t_ast_regex *regex, t_state *state, void *out) NOT_DONE;
t_error run_subshell(t_ast_subshell *subshell, t_state *state, void *out) NOT_DONE; t_error run_subshell(t_ast_subshell *subshell, t_state *state, void *out) NOT_DONE;
t_error run_until(t_ast_until *until, t_state *state, void *out) NOT_DONE; t_error run_until(t_ast_until *until, t_state *state, void *out) NOT_DONE;
t_error run_variable_assignment(t_ast_variable_assignment *variable_assignment, t_state *state, void *out) NOT_DONE; t_error run_variable_assignment(t_ast_variable_assignment *variable_assignment, t_state *state, bool is_temporary, void *out) NOT_DONE;
t_error run_while_(t_ast_while *while_, t_state *state, void *out) NOT_DONE; t_error run_while_(t_ast_while *while_, t_state *state, void *out) NOT_DONE;
// FUNCTIONS // FUNCTIONS
@ -606,11 +609,23 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection, t_state *state
i++; i++;
} }
redirection.len = 0;
vec_ast_free(redirection);
vec_str_free(filename_args); vec_str_free(filename_args);
info.arguments = args;
//if (build_envp(state, info.arguments));
if (args.len == 0)
{
vec_str_free(args);
return (ERROR);
}
//if (_is_builtin(args.buffer[0]))
// return (_handle_builtin(args, info));
return (ERROR); return (ERROR);
} }
t_error run_command(t_ast_command *command, t_state *state, t_command_result *out) t_error run_command(t_ast_command *command, t_state *state, t_cmd_pipe cmd_pipe, t_command_result *out)
{ {
t_vec_str args; t_vec_str args;
t_vec_str split; t_vec_str split;
@ -622,9 +637,6 @@ t_error run_command(t_ast_command *command, t_state *state, t_command_result *ou
if (command == NULL || state == NULL || out == NULL) if (command == NULL || state == NULL || out == NULL)
return (ERROR); return (ERROR);
env_bck = state->env;
if (hmap_env_clone(env_bck, _clone_env, NULL, &state->env))
return (state->env = env_bck, ERROR);
args = vec_str_new(command->cmd_word.len, str_free); args = vec_str_new(command->cmd_word.len, str_free);
redirection = vec_ast_new(command->suffixes_redirections.len, ast_free); redirection = vec_ast_new(command->suffixes_redirections.len, ast_free);
i = 0; i = 0;
@ -635,7 +647,7 @@ t_error run_command(t_ast_command *command, t_state *state, t_command_result *ou
vec_ast_push(&redirection, tmp); vec_ast_push(&redirection, tmp);
if (tmp->kind == AST_VARIABLE_ASSIGNMENT) if (tmp->kind == AST_VARIABLE_ASSIGNMENT)
{ {
if (run_variable_assignment(&tmp->data.variable_assignment, state, NULL)) if (run_variable_assignment(&tmp->data.variable_assignment, state, true, NULL))
return (ERROR); return (ERROR);
} }
i++; i++;
@ -655,7 +667,7 @@ t_error run_command(t_ast_command *command, t_state *state, t_command_result *ou
i++; i++;
} }
i = 0; i = 0;
if (_spawn_cmd_and_run(args, redirection, state, (t_cmd_pipe){0, 0}, out)) if (_spawn_cmd_and_run(args, redirection, state, cmd_pipe, out))
return (ERROR); return (ERROR);
return (NO_ERROR); return (NO_ERROR);
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 18:41:01 by maiboyer #+# #+# */ /* Created: 2024/05/04 18:41:01 by maiboyer #+# #+# */
/* Updated: 2024/05/09 14:28:42 by rparodi ### ########.fr */ /* Updated: 2024/08/02 22:11:14 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,14 +18,17 @@
# include "me/types.h" # include "me/types.h"
# include "me/vec/vec_str.h" # include "me/vec/vec_str.h"
t_hashmap_env *create_env_map(void); t_hashmap_env *create_env_map(void);
t_error build_envp(t_hashmap_env *envs, t_vec_str *out); t_error build_envp(t_hashmap_env *envs, t_hashmap_env *tmp_vars, \
t_vec_str *out);
struct s_build_envp_state struct s_build_envp_state
{ {
t_vec_str out; t_hashmap_env *tmp_vars;
t_string buf; t_vec_str out;
t_string buf;
}; };
#endif /* ENV_H */ #endif /* ENV_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 15:49:56 by maiboyer #+# #+# */ /* Created: 2024/05/02 15:49:56 by maiboyer #+# #+# */
/* Updated: 2024/07/20 13:45:21 by maiboyer ### ########.fr */ /* Updated: 2024/08/02 22:26:20 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,12 +18,16 @@
# include "me/os/os.h" # include "me/os/os.h"
# include "me/types.h" # include "me/types.h"
typedef struct s_parser typedef struct s_state t_state;
typedef struct s_parser t_parser;
struct s_parser
{ {
t_first_parser *parser; t_first_parser *parser;
} t_parser; };
typedef struct s_state
struct s_state
{ {
t_str prompt; t_str prompt;
t_str str_input; t_str str_input;
@ -31,8 +35,9 @@ typedef struct s_state
t_str *path; t_str *path;
t_parser parser; t_parser parser;
t_hashmap_env *env; t_hashmap_env *env;
t_hashmap_env *tmp_var;
t_node current_node; t_node current_node;
t_process ret; t_process ret;
} t_state; };
#endif /* STATE_H */ #endif /* STATE_H */

View file

@ -18,6 +18,7 @@ sources = [
"stdme/generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__", "stdme/generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__",
"stdme/generic_sources/src/hashmap/C__PREFIX___iter.c__TEMPLATE__", "stdme/generic_sources/src/hashmap/C__PREFIX___iter.c__TEMPLATE__",
"stdme/generic_sources/src/hashmap/C__PREFIX___clone.c__TEMPLATE__", "stdme/generic_sources/src/hashmap/C__PREFIX___clone.c__TEMPLATE__",
"stdme/generic_sources/src/hashmap/C__PREFIX___clear.c__TEMPLATE__",
] ]
replace.C__VALTYPE__ = "type" replace.C__VALTYPE__ = "type"
replace.C__KEYTYPE__ = "type" replace.C__KEYTYPE__ = "type"

View file

@ -35,7 +35,7 @@ typedef struct s_kv_env
/// @typedef A function that hashes a key /// @typedef A function that hashes a key
typedef void (*t_hash_env_fn)(t_hasher *hasher, t_str *key); typedef void (*t_hash_env_fn)(t_hasher *hasher, t_str *key);
/// @typedef A function that drops a key-value pair /// @typedef A function that drops a key-value pair
typedef void (*t_drop_env_fn)(t_kv_env val); typedef void (*t_free_env_fn)(t_kv_env val);
/// @typedef A function that compares two keys and returns true if they are equal /// @typedef A function that compares two keys and returns true if they are equal
typedef bool (*t_eq_env_fn)(t_str *lhs, t_str *rhs); typedef bool (*t_eq_env_fn)(t_str *lhs, t_str *rhs);
@ -57,7 +57,7 @@ typedef struct s_entry_env
/// @var hasher The hasher function /// @var hasher The hasher function
/// @var hfunc The hash function /// @var hfunc The hash function
/// @var cfunc The comparison function /// @var cfunc The comparison function
/// @var drop The drop function /// @var free The free function
typedef struct s_hashmap_env typedef struct s_hashmap_env
{ {
t_entry_env **buckets; t_entry_env **buckets;
@ -65,28 +65,32 @@ typedef struct s_hashmap_env
t_hasher hasher; t_hasher hasher;
t_hash_env_fn hfunc; t_hash_env_fn hfunc;
t_eq_env_fn cfunc; t_eq_env_fn cfunc;
t_drop_env_fn drop; t_free_env_fn free;
} t_hashmap_env; } t_hashmap_env;
/// @brief Creates a new hashmap with the given hash, comparison, and drop functions /// @brief Creates a new hashmap with the given hash, comparison, and free functions
/// @param hash The hash function /// @param hash The hash function
/// @param cmp The comparison function /// @param cmp The comparison function
/// @param drop The drop function /// @param free The free function
/// @return A new hashmap /// @return A new hashmap
t_hashmap_env *hmap_env_new(t_hash_env_fn hash, t_eq_env_fn cmp, t_drop_env_fn drop); t_hashmap_env *hmap_env_new(t_hash_env_fn hash, t_eq_env_fn cmp, t_free_env_fn free);
/// @brief Creates a new hashmap with the given hash, comparison, and drop functions /// @brief Creates a new hashmap with the given hash, comparison, and free functions
/// @param hash The hash function /// @param hash The hash function
/// @param cmp The comparison function /// @param cmp The comparison function
/// @param drop The drop function /// @param free The free function
/// @param cap The number of buckets /// @param cap The number of buckets
/// @return A new hashmap /// @return A new hashmap
t_hashmap_env *hmap_env_new_with_buckets(t_hash_env_fn hash, t_eq_env_fn cmp, t_drop_env_fn drop, size_t cap); t_hashmap_env *hmap_env_new_with_buckets(t_hash_env_fn hash, t_eq_env_fn cmp, t_free_env_fn free, size_t cap);
/// @brief Free the hashmap and all of its entries /// @brief Free the hashmap and all of its entries
/// @param hmap The hashmap to free /// @param hmap The hashmap to free
void hmap_env_free(t_hashmap_env *hmap); void hmap_env_free(t_hashmap_env *hmap);
/// @brief Clear the hashmap, removing all of its entries
/// @param hmap The hashmap to clear
void hmap_env_clear(t_hashmap_env *hmap);
/// @brief Inserts a key-value pair into the hashmap /// @brief Inserts a key-value pair into the hashmap
/// @param hmap The hashmap /// @param hmap The hashmap
/// @param key The key /// @param key The key

View file

@ -19,15 +19,15 @@
t_hashmap_env *hmap_env_new(t_hash_env_fn hfunc, t_hashmap_env *hmap_env_new(t_hash_env_fn hfunc,
t_eq_env_fn cfunc, t_eq_env_fn cfunc,
t_drop_env_fn drop) t_free_env_fn free)
{ {
return ( return (
hmap_env_new_with_buckets(hfunc, cfunc, drop, DEFAULT_BUCKETS)); hmap_env_new_with_buckets(hfunc, cfunc, free, DEFAULT_BUCKETS));
} }
t_hashmap_env *hmap_env_new_with_buckets( t_hashmap_env *hmap_env_new_with_buckets(
t_hash_env_fn hfunc, t_eq_env_fn cfunc, t_hash_env_fn hfunc, t_eq_env_fn cfunc,
t_drop_env_fn drop, t_usize buckets) t_free_env_fn free, t_usize buckets)
{ {
t_hashmap_env *hmap; t_hashmap_env *hmap;
@ -39,7 +39,7 @@ t_hashmap_env *hmap_env_new_with_buckets(
hmap->hasher = hasher_sip13_new(); hmap->hasher = hasher_sip13_new();
hmap->hfunc = hfunc; hmap->hfunc = hfunc;
hmap->cfunc = cfunc; hmap->cfunc = cfunc;
hmap->drop = drop; hmap->free = free;
if (hmap->buckets == NULL) if (hmap->buckets == NULL)
return ((void)mem_free(hmap), NULL); return ((void)mem_free(hmap), NULL);
return (hmap); return (hmap);
@ -57,7 +57,7 @@ void hmap_env_free(t_hashmap_env *hmap)
entry = hmap->buckets[index]; entry = hmap->buckets[index];
while (entry != NULL) while (entry != NULL)
{ {
hmap->drop(entry->kv); hmap->free(entry->kv);
tmp = entry->next; tmp = entry->next;
mem_free(entry); mem_free(entry);
entry = tmp; entry = tmp;
@ -117,7 +117,7 @@ bool hmap_env_insert(t_hashmap_env *hmap, t_str key,
} }
else else
{ {
hmap->drop(entry->kv); hmap->free(entry->kv);
entry->kv.key = key; entry->kv.key = key;
entry->kv.val = value; entry->kv.val = value;
return (true); return (true);

40
output/src/hashmap/env/env_clear.c vendored Normal file
View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashmap_env.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/types.h"
#include "me/hashmap/hashmap_env.h"
#include "me/mem/mem.h"
void hmap_env_clear(t_hashmap_env *self)
{
t_usize bucket_id;
t_entry_env *cur;
t_entry_env *next;
bucket_id = 0;
while (bucket_id < self->num_buckets)
{
cur = self->buckets[bucket_id];
while (cur != NULL)
{
next = cur->next;
self->free(cur->kv);
mem_free(cur);
cur = next;
}
bucket_id++;
}
}

View file

@ -27,7 +27,7 @@ t_error hmap_env_clone(t_hashmap_env *self,
t_hashmap_env *ret; t_hashmap_env *ret;
bucket_id = 0; bucket_id = 0;
ret = hmap_env_new_with_buckets(self->hfunc, self->cfunc, self->drop, self->num_buckets); ret = hmap_env_new_with_buckets(self->hfunc, self->cfunc, self->free, self->num_buckets);
if (ret == NULL) if (ret == NULL)
return (ERROR); return (ERROR);
while (bucket_id < self->num_buckets) while (bucket_id < self->num_buckets)

View file

@ -48,7 +48,7 @@ void hmap_env_remove(t_hashmap_env *hmap, t_str *key)
hmap->buckets[hashed_key % hmap->num_buckets] = entry->next; hmap->buckets[hashed_key % hmap->num_buckets] = entry->next;
else else
prev->next = entry->next; prev->next = entry->next;
hmap->drop(entry->kv); hmap->free(entry->kv);
mem_free(entry); mem_free(entry);
hmap->buckets[hashed_key % hmap->num_buckets] = NULL; hmap->buckets[hashed_key % hmap->num_buckets] = NULL;
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 18:32:50 by maiboyer #+# #+# */ /* Created: 2024/05/04 18:32:50 by maiboyer #+# #+# */
/* Updated: 2024/08/02 12:09:05 by rparodi ### ########.fr */ /* Updated: 2024/08/02 22:37:23 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -68,10 +68,11 @@ t_error _build_envp_iterator(t_usize idx, const t_str *key, t_str *val,
return (NO_ERROR); return (NO_ERROR);
} }
t_error build_envp(t_hashmap_env *envs, t_vec_str *envp) t_error build_envp(t_hashmap_env *envs, t_hashmap_env *tmp_vars, t_vec_str *envp)
{ {
struct s_build_envp_state state; struct s_build_envp_state state;
state.tmp_vars = tmp_vars;
state.buf = string_new(8096); state.buf = string_new(8096);
state.out = vec_str_new(1024, (void (*)(t_str))mem_free); state.out = vec_str_new(1024, (void (*)(t_str))mem_free);
if (hmap_env_iter(envs, _build_envp_iterator, &state)) if (hmap_env_iter(envs, _build_envp_iterator, &state))

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/08/02 12:16:09 by rparodi ### ########.fr */ /* Updated: 2024/08/02 23:00:50 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -184,6 +184,7 @@ t_i32 main(t_i32 argc, t_str argv[], t_str envp[])
(void)argc; (void)argc;
(void)argv; (void)argv;
(void)envp; (void)envp;
me_abort("abort");
if (install_signal()) if (install_signal())
me_abort("Unable to install signals"); me_abort("Unable to install signals");
state = (t_state){}; state = (t_state){};

View file

@ -35,7 +35,7 @@ typedef struct s_kv_C__PREFIX__
/// @typedef A function that hashes a key /// @typedef A function that hashes a key
typedef void (*t_hash_C__PREFIX___fn)(t_hasher *hasher, C__KEYTYPE__ *key); typedef void (*t_hash_C__PREFIX___fn)(t_hasher *hasher, C__KEYTYPE__ *key);
/// @typedef A function that drops a key-value pair /// @typedef A function that drops a key-value pair
typedef void (*t_drop_C__PREFIX___fn)(t_kv_C__PREFIX__ val); typedef void (*t_free_C__PREFIX___fn)(t_kv_C__PREFIX__ val);
/// @typedef A function that compares two keys and returns true if they are equal /// @typedef A function that compares two keys and returns true if they are equal
typedef bool (*t_eq_C__PREFIX___fn)(C__KEYTYPE__ *lhs, C__KEYTYPE__ *rhs); typedef bool (*t_eq_C__PREFIX___fn)(C__KEYTYPE__ *lhs, C__KEYTYPE__ *rhs);
@ -57,7 +57,7 @@ typedef struct s_entry_C__PREFIX__
/// @var hasher The hasher function /// @var hasher The hasher function
/// @var hfunc The hash function /// @var hfunc The hash function
/// @var cfunc The comparison function /// @var cfunc The comparison function
/// @var drop The drop function /// @var free The free function
typedef struct s_hashmap_C__PREFIX__ typedef struct s_hashmap_C__PREFIX__
{ {
t_entry_C__PREFIX__ **buckets; t_entry_C__PREFIX__ **buckets;
@ -65,28 +65,32 @@ typedef struct s_hashmap_C__PREFIX__
t_hasher hasher; t_hasher hasher;
t_hash_C__PREFIX___fn hfunc; t_hash_C__PREFIX___fn hfunc;
t_eq_C__PREFIX___fn cfunc; t_eq_C__PREFIX___fn cfunc;
t_drop_C__PREFIX___fn drop; t_free_C__PREFIX___fn free;
} t_hashmap_C__PREFIX__; } t_hashmap_C__PREFIX__;
/// @brief Creates a new hashmap with the given hash, comparison, and drop functions /// @brief Creates a new hashmap with the given hash, comparison, and free functions
/// @param hash The hash function /// @param hash The hash function
/// @param cmp The comparison function /// @param cmp The comparison function
/// @param drop The drop function /// @param free The free function
/// @return A new hashmap /// @return A new hashmap
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_drop_C__PREFIX___fn drop); t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_free_C__PREFIX___fn free);
/// @brief Creates a new hashmap with the given hash, comparison, and drop functions /// @brief Creates a new hashmap with the given hash, comparison, and free functions
/// @param hash The hash function /// @param hash The hash function
/// @param cmp The comparison function /// @param cmp The comparison function
/// @param drop The drop function /// @param free The free function
/// @param cap The number of buckets /// @param cap The number of buckets
/// @return A new hashmap /// @return A new hashmap
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new_with_buckets(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_drop_C__PREFIX___fn drop, size_t cap); t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new_with_buckets(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_free_C__PREFIX___fn free, size_t cap);
/// @brief Free the hashmap and all of its entries /// @brief Free the hashmap and all of its entries
/// @param hmap The hashmap to free /// @param hmap The hashmap to free
void hmap_C__PREFIX___free(t_hashmap_C__PREFIX__ *hmap); void hmap_C__PREFIX___free(t_hashmap_C__PREFIX__ *hmap);
/// @brief Clear the hashmap, removing all of its entries
/// @param hmap The hashmap to clear
void hmap_C__PREFIX___clear(t_hashmap_C__PREFIX__ *hmap);
/// @brief Inserts a key-value pair into the hashmap /// @brief Inserts a key-value pair into the hashmap
/// @param hmap The hashmap /// @param hmap The hashmap
/// @param key The key /// @param key The key

View file

@ -19,15 +19,15 @@
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new(t_hash_C__PREFIX___fn hfunc, t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new(t_hash_C__PREFIX___fn hfunc,
t_eq_C__PREFIX___fn cfunc, t_eq_C__PREFIX___fn cfunc,
t_drop_C__PREFIX___fn drop) t_free_C__PREFIX___fn free)
{ {
return ( return (
hmap_C__PREFIX___new_with_buckets(hfunc, cfunc, drop, DEFAULT_BUCKETS)); hmap_C__PREFIX___new_with_buckets(hfunc, cfunc, free, DEFAULT_BUCKETS));
} }
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new_with_buckets( t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new_with_buckets(
t_hash_C__PREFIX___fn hfunc, t_eq_C__PREFIX___fn cfunc, t_hash_C__PREFIX___fn hfunc, t_eq_C__PREFIX___fn cfunc,
t_drop_C__PREFIX___fn drop, t_usize buckets) t_free_C__PREFIX___fn free, t_usize buckets)
{ {
t_hashmap_C__PREFIX__ *hmap; t_hashmap_C__PREFIX__ *hmap;
@ -39,7 +39,7 @@ t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new_with_buckets(
hmap->hasher = hasher_sip13_new(); hmap->hasher = hasher_sip13_new();
hmap->hfunc = hfunc; hmap->hfunc = hfunc;
hmap->cfunc = cfunc; hmap->cfunc = cfunc;
hmap->drop = drop; hmap->free = free;
if (hmap->buckets == NULL) if (hmap->buckets == NULL)
return ((void)mem_free(hmap), NULL); return ((void)mem_free(hmap), NULL);
return (hmap); return (hmap);
@ -57,7 +57,7 @@ void hmap_C__PREFIX___free(t_hashmap_C__PREFIX__ *hmap)
entry = hmap->buckets[index]; entry = hmap->buckets[index];
while (entry != NULL) while (entry != NULL)
{ {
hmap->drop(entry->kv); hmap->free(entry->kv);
tmp = entry->next; tmp = entry->next;
mem_free(entry); mem_free(entry);
entry = tmp; entry = tmp;
@ -117,7 +117,7 @@ bool hmap_C__PREFIX___insert(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
} }
else else
{ {
hmap->drop(entry->kv); hmap->free(entry->kv);
entry->kv.key = key; entry->kv.key = key;
entry->kv.val = value; entry->kv.val = value;
return (true); return (true);

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashmap_C__PREFIX__.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/types.h"
#include "me/hashmap/hashmap_C__PREFIX__.h"
#include "me/mem/mem.h"
void hmap_C__PREFIX___clear(t_hashmap_C__PREFIX__ *self)
{
t_usize bucket_id;
t_entry_C__PREFIX__ *cur;
t_entry_C__PREFIX__ *next;
bucket_id = 0;
while (bucket_id < self->num_buckets)
{
cur = self->buckets[bucket_id];
while (cur != NULL)
{
next = cur->next;
self->free(cur->kv);
mem_free(cur);
cur = next;
}
bucket_id++;
}
}

View file

@ -27,7 +27,7 @@ t_error hmap_C__PREFIX___clone(t_hashmap_C__PREFIX__ *self,
t_hashmap_C__PREFIX__ *ret; t_hashmap_C__PREFIX__ *ret;
bucket_id = 0; bucket_id = 0;
ret = hmap_C__PREFIX___new_with_buckets(self->hfunc, self->cfunc, self->drop, self->num_buckets); ret = hmap_C__PREFIX___new_with_buckets(self->hfunc, self->cfunc, self->free, self->num_buckets);
if (ret == NULL) if (ret == NULL)
return (ERROR); return (ERROR);
while (bucket_id < self->num_buckets) while (bucket_id < self->num_buckets)

View file

@ -48,7 +48,7 @@ void hmap_C__PREFIX___remove(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key)
hmap->buckets[hashed_key % hmap->num_buckets] = entry->next; hmap->buckets[hashed_key % hmap->num_buckets] = entry->next;
else else
prev->next = entry->next; prev->next = entry->next;
hmap->drop(entry->kv); hmap->free(entry->kv);
mem_free(entry); mem_free(entry);
hmap->buckets[hashed_key % hmap->num_buckets] = NULL; hmap->buckets[hashed_key % hmap->num_buckets] = NULL;
} }

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* process_inner.c :+: :+: :+: */ /* process_iterator_function.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */ /* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */
/* Updated: 2024/08/01 06:37:51 by maiboyer ### ########.fr */ /* Updated: 2024/08/02 20:26:13 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,12 +14,12 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
bool find_null(const t_str *s) bool _find_null(const t_str *s)
{ {
return (s == NULL); return (s == NULL);
} }
bool find_path(const t_str *s) bool _find_path(const t_str *s)
{ {
t_str ss; t_str ss;