From f86947a852c0d584e9e447bb4354a993138898e5 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Sat, 4 May 2024 19:21:56 +0200 Subject: [PATCH] Added env hashmap with build_env function to build envp --- Minishell.mk | 4 +- gen.list | 24 ++-- includes/app/env.h | 31 +++++ includes/app/node/handle_expension.h | 22 ++++ includes/app/state.h | 17 +-- includes/minishell.h | 4 +- input.toml | 30 +++-- output/include/me/hashmap/hashmap_env.h | 82 +++++++++++++ output/include/me/vec/vec_str.h | 58 +++++++++ output/src/hashmap/env/env.c | 116 ++++++++++++++++++ output/src/hashmap/env/env_iter.c | 39 ++++++ output/src/hashmap/env/env_utils.c | 56 +++++++++ .../parser_heredoc.c} | 0 .../parser_heredoc_functions2.c} | 0 .../parser_heredoc_functions3.c} | 0 .../parser_range.c} | 0 .../parser_range_functions2.c} | 0 .../parser_range_functions3.c} | 0 .../reduce_action.c} | 0 .../reduce_action_functions2.c} | 0 .../reduce_action_functions3.c} | 0 output/src/vec/str/str.c | 115 +++++++++++++++++ output/src/vec/str/str_functions2.c | 112 +++++++++++++++++ output/src/vec/str/str_functions3.c | 84 +++++++++++++ sources/env.c | 74 +++++++++++ sources/exec/handle_expension.c | 40 ++++++ sources/ft_cmd.c | 78 ++++++------ sources/main.c | 46 +++---- src.list | 2 + stdme/gen.list | 9 ++ .../header/hashmap_C__PREFIX__.h__TEMPLATE__ | 6 + .../str_to_C__PREFIX__.c__TEMPLATE__ | 0 .../str_to_C__PREFIX___utils.c__TEMPLATE__ | 0 .../C__PREFIX__.c__TEMPLATE__} | 4 +- .../hashmap/C__PREFIX___iter.c__TEMPLATE__ | 39 ++++++ .../C__PREFIX___utils.c__TEMPLATE__} | 2 +- .../C__PREFIX__.c__TEMPLATE__} | 0 .../C__PREFIX___functions2.c__TEMPLATE__} | 0 .../C__PREFIX___functions3.c__TEMPLATE__} | 0 .../C__PREFIX___sort.c__TEMPLATE__} | 0 stdme/include/me/string/str_compare.h | 20 +++ stdme/input.toml | 20 +-- stdme/output/src/vec/buf_str/buf_str.c | 115 +++++++++++++++++ .../src/vec/buf_str/buf_str_functions2.c | 112 +++++++++++++++++ .../src/vec/buf_str/buf_str_functions3.c | 84 +++++++++++++ stdme/output/src/vec/str/str.c | 115 +++++++++++++++++ stdme/output/src/vec/str/str_functions2.c | 112 +++++++++++++++++ stdme/output/src/vec/str/str_functions3.c | 84 +++++++++++++ stdme/output/src/vec/u8/u8.c | 115 +++++++++++++++++ stdme/output/src/vec/u8/u8_functions2.c | 112 +++++++++++++++++ stdme/output/src/vec/u8/u8_functions3.c | 84 +++++++++++++ stdme/src.list | 2 + stdme/src/hash/hash_str.c | 20 +++ stdme/src/string/str_compare.c | 26 ++++ 54 files changed, 2010 insertions(+), 105 deletions(-) create mode 100644 includes/app/env.h create mode 100644 includes/app/node/handle_expension.h create mode 100644 output/include/me/hashmap/hashmap_env.h create mode 100644 output/include/me/vec/vec_str.h create mode 100644 output/src/hashmap/env/env.c create mode 100644 output/src/hashmap/env/env_iter.c create mode 100644 output/src/hashmap/env/env_utils.c rename output/src/vec/{vec_parser_heredoc.c => parser_heredoc/parser_heredoc.c} (100%) rename output/src/vec/{vec_parser_heredoc_functions2.c => parser_heredoc/parser_heredoc_functions2.c} (100%) rename output/src/vec/{vec_parser_heredoc_functions3.c => parser_heredoc/parser_heredoc_functions3.c} (100%) rename output/src/vec/{vec_parser_range.c => parser_range/parser_range.c} (100%) rename output/src/vec/{vec_parser_range_functions2.c => parser_range/parser_range_functions2.c} (100%) rename output/src/vec/{vec_parser_range_functions3.c => parser_range/parser_range_functions3.c} (100%) rename output/src/vec/{vec_reduce_action.c => reduce_action/reduce_action.c} (100%) rename output/src/vec/{vec_reduce_action_functions2.c => reduce_action/reduce_action_functions2.c} (100%) rename output/src/vec/{vec_reduce_action_functions3.c => reduce_action/reduce_action_functions3.c} (100%) create mode 100644 output/src/vec/str/str.c create mode 100644 output/src/vec/str/str_functions2.c create mode 100644 output/src/vec/str/str_functions3.c create mode 100644 sources/env.c create mode 100644 sources/exec/handle_expension.c rename stdme/generic_sources/src/{ => convert}/str_to_C__PREFIX__.c__TEMPLATE__ (100%) rename stdme/generic_sources/src/{ => convert}/str_to_C__PREFIX___utils.c__TEMPLATE__ (100%) rename stdme/generic_sources/src/{hashmap_C__PREFIX__.c__TEMPLATE__ => hashmap/C__PREFIX__.c__TEMPLATE__} (96%) create mode 100644 stdme/generic_sources/src/hashmap/C__PREFIX___iter.c__TEMPLATE__ rename stdme/generic_sources/src/{hashmap_C__PREFIX___utils.c__TEMPLATE__ => hashmap/C__PREFIX___utils.c__TEMPLATE__} (96%) rename stdme/generic_sources/src/{vec_C__PREFIX__.c__TEMPLATE__ => vec/C__PREFIX__.c__TEMPLATE__} (100%) rename stdme/generic_sources/src/{vec_C__PREFIX___functions2.c__TEMPLATE__ => vec/C__PREFIX___functions2.c__TEMPLATE__} (100%) rename stdme/generic_sources/src/{vec_C__PREFIX___functions3.c__TEMPLATE__ => vec/C__PREFIX___functions3.c__TEMPLATE__} (100%) rename stdme/generic_sources/src/{vec_C__PREFIX___sort.c__TEMPLATE__ => vec/C__PREFIX___sort.c__TEMPLATE__} (100%) create mode 100644 stdme/include/me/string/str_compare.h create mode 100644 stdme/output/src/vec/buf_str/buf_str.c create mode 100644 stdme/output/src/vec/buf_str/buf_str_functions2.c create mode 100644 stdme/output/src/vec/buf_str/buf_str_functions3.c create mode 100644 stdme/output/src/vec/str/str.c create mode 100644 stdme/output/src/vec/str/str_functions2.c create mode 100644 stdme/output/src/vec/str/str_functions3.c create mode 100644 stdme/output/src/vec/u8/u8.c create mode 100644 stdme/output/src/vec/u8/u8_functions2.c create mode 100644 stdme/output/src/vec/u8/u8_functions3.c create mode 100644 stdme/src/hash/hash_str.c create mode 100644 stdme/src/string/str_compare.c diff --git a/Minishell.mk b/Minishell.mk index 2fce1588..74e8722e 100644 --- a/Minishell.mk +++ b/Minishell.mk @@ -6,7 +6,7 @@ # By: maiboyer +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/04/28 17:28:30 by maiboyer #+# #+# # -# Updated: 2024/05/02 15:14:11 by maiboyer ### ########.fr # +# Updated: 2024/05/04 19:20:22 by maiboyer ### ########.fr # # # # **************************************************************************** # @@ -15,7 +15,7 @@ OBJDIRNAME ?= # Flags -CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD -lreadline -I./includes -I./output/include +CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD -lreadline -I./includes -I./output/include -I./stdme/output/include # Sources LIB = ./libft/ft_bzero.c \ ./libft/ft_calloc.c \ diff --git a/gen.list b/gen.list index 1bca00f9..3018dcfc 100644 --- a/gen.list +++ b/gen.list @@ -1,9 +1,15 @@ -src/vec/vec_parser_heredoc.c -src/vec/vec_parser_heredoc_functions2.c -src/vec/vec_parser_heredoc_functions3.c -src/vec/vec_parser_range.c -src/vec/vec_parser_range_functions2.c -src/vec/vec_parser_range_functions3.c -src/vec/vec_reduce_action.c -src/vec/vec_reduce_action_functions2.c -src/vec/vec_reduce_action_functions3.c +src/hashmap/env/env.c +src/hashmap/env/env_iter.c +src/hashmap/env/env_utils.c +src/vec/parser_heredoc/parser_heredoc.c +src/vec/parser_heredoc/parser_heredoc_functions2.c +src/vec/parser_heredoc/parser_heredoc_functions3.c +src/vec/parser_range/parser_range.c +src/vec/parser_range/parser_range_functions2.c +src/vec/parser_range/parser_range_functions3.c +src/vec/reduce_action/reduce_action.c +src/vec/reduce_action/reduce_action_functions2.c +src/vec/reduce_action/reduce_action_functions3.c +src/vec/str/str.c +src/vec/str/str_functions2.c +src/vec/str/str_functions3.c diff --git a/includes/app/env.h b/includes/app/env.h new file mode 100644 index 00000000..bc67b286 --- /dev/null +++ b/includes/app/env.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/04 18:41:01 by maiboyer #+# #+# */ +/* Updated: 2024/05/04 19:08:13 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ENV_H +#define ENV_H + +#include "me/buffered_str/buf_str.h" +#include "me/hashmap/hashmap_env.h" +#include "me/types.h" +#include "me/vec/vec_str.h" + +t_hashmap_env *create_env_map(void); + +t_error build_envp(t_hashmap_env *envs, t_vec_str *out); + +struct s_build_envp_state +{ + t_vec_str out; + t_buffer_str buf; +}; + +#endif /* ENV_H */ diff --git a/includes/app/node/handle_expension.h b/includes/app/node/handle_expension.h new file mode 100644 index 00000000..deafd4c1 --- /dev/null +++ b/includes/app/node/handle_expension.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* handle_expension.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/03 15:24:56 by maiboyer #+# #+# */ +/* Updated: 2024/05/03 15:25:18 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HANDLE_EXPENSION_H +#define HANDLE_EXPENSION_H + +#include "app/node.h" +#include "app/state.h" +#include "me/types.h" + +t_error handle_expension(t_node *self, t_utils *shcat, t_str *ret); + +#endif /* HANDLE_EXPENSION_H */ diff --git a/includes/app/state.h b/includes/app/state.h index 96e465bb..a3cef93a 100644 --- a/includes/app/state.h +++ b/includes/app/state.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/02 15:49:56 by maiboyer #+# #+# */ -/* Updated: 2024/05/02 15:51:38 by maiboyer ### ########.fr */ +/* Updated: 2024/05/04 18:40:53 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #define STATE_H #include "app/node.h" +#include "me/hashmap/hashmap_env.h" #include "me/types.h" typedef struct s_parser @@ -23,13 +24,13 @@ typedef struct s_parser typedef struct s_utils { - t_str name_shell; - t_str str_input; - t_str *strs_input; - t_str *path; - t_str *envp; - t_parser parser; - t_node current_node; + t_str name_shell; + t_str str_input; + t_str *strs_input; + t_str *path; + t_parser parser; + t_hashmap_env *env; + t_node current_node; } t_utils; #endif /* STATE_H */ diff --git a/includes/minishell.h b/includes/minishell.h index aa22bae0..6491cb55 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 14:41:15 by rparodi #+# #+# */ -/* Updated: 2024/05/02 15:51:57 by maiboyer ### ########.fr */ +/* Updated: 2024/05/03 15:22:24 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,10 +18,10 @@ #include "me/types.h" #include +#include #include #include #include -#include #include #include #include diff --git a/input.toml b/input.toml index 8afc11ce..e31e986c 100644 --- a/input.toml +++ b/input.toml @@ -1,9 +1,9 @@ [definition.vec] headers = ["stdme/generic_sources/header/vec_C__PREFIX__.h__TEMPLATE__"] sources = [ - "stdme/generic_sources/src/vec_C__PREFIX__.c__TEMPLATE__", - "stdme/generic_sources/src/vec_C__PREFIX___functions2.c__TEMPLATE__", - "stdme/generic_sources/src/vec_C__PREFIX___functions3.c__TEMPLATE__", + "stdme/generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__", + "stdme/generic_sources/src/vec/C__PREFIX___functions2.c__TEMPLATE__", + "stdme/generic_sources/src/vec/C__PREFIX___functions3.c__TEMPLATE__", ] replace.C__TYPENAME__ = "type" replace.C__TYPEHEADER__ = "header_include" @@ -14,8 +14,9 @@ replace.C__PREFIXUP__ = "prefix" [definition.hashmap] headers = ["stdme/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__"] sources = [ - "stdme/generic_sources/src/hashmap_C__PREFIX__.c__TEMPLATE__", - "stdme/generic_sources/src/hashmap_C__PREFIX___utils.c__TEMPLATE__", + "stdme/generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__", + "stdme/generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__", + "stdme/generic_sources/src/hashmap/C__PREFIX___iter.c__TEMPLATE__", ] replace.C__VALTYPE__ = "type" replace.C__KEYTYPE__ = "type" @@ -26,8 +27,8 @@ replace.C__PREFIXUP__ = "prefix" [definition.str_to_num] headers = [] sources = [ - "stdme/generic_sources/src/str_to_C__PREFIX__.c__TEMPLATE__", - "stdme/generic_sources/src/str_to_C__PREFIX___utils.c__TEMPLATE__", + "stdme/generic_sources/src/convert/str_to_C__PREFIX__.c__TEMPLATE__", + "stdme/generic_sources/src/convert/str_to_C__PREFIX___utils.c__TEMPLATE__", ] replace.C__TYPE__ = "type" replace.C__UNSIGNED_TYPE__ = "type" @@ -38,7 +39,7 @@ replace.C__ZERO__ = "value" replace.C__SIGNED_TYPE__ = "bool" [[create.vec]] -sources_output = "src/vec/" +sources_output = "src/vec/C__PREFIX__/" headers_output = "include/me/vec/" replace.C__TYPENAME__ = "t_parser_range" replace.C__TYPEHEADER__ = '#include "parser/types/types_parser_range.h"' @@ -46,7 +47,7 @@ replace.C__PREFIX__ = "parser_range" replace.C__PREFIXUP__ = "PARSER_RANGE" [[create.vec]] -sources_output = "src/vec/" +sources_output = "src/vec/C__PREFIX__/" headers_output = "include/me/vec/" replace.C__TYPENAME__ = "t_heredoc" replace.C__TYPEHEADER__ = '#include "parser/types/types_heredoc.h"' @@ -54,9 +55,18 @@ replace.C__PREFIX__ = "parser_heredoc" replace.C__PREFIXUP__ = "PARSER_HEREDOC" [[create.vec]] -sources_output = "src/vec/" +sources_output = "src/vec/C__PREFIX__/" headers_output = "include/me/vec/" replace.C__TYPENAME__ = "t_reduce_action" replace.C__TYPEHEADER__ = '#include "parser/types/types_reduce_action.h"' replace.C__PREFIX__ = "reduce_action" replace.C__PREFIXUP__ = "REDUCE_ACTION" + +[[create.hashmap]] +sources_output = "src/hashmap/C__PREFIX__/" +headers_output = "include/me/hashmap/" +replace.C__VALTYPE__ = "t_str" +replace.C__KEYTYPE__ = "t_str" +replace.C__TYPEHEADER__ = '' +replace.C__PREFIX__ = "env" +replace.C__PREFIXUP__ = "ENV" diff --git a/output/include/me/hashmap/hashmap_env.h b/output/include/me/hashmap/hashmap_env.h new file mode 100644 index 00000000..27b96f88 --- /dev/null +++ b/output/include/me/hashmap/hashmap_env.h @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashmap_env.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HASHMAP_ENV_H +#define HASHMAP_ENV_H + +#define DEFAULT_BUCKETS 750 + + + +#include "me/hash/hasher.h" +#include "me/types.h" +#include +#include +#include +#include +#include + +typedef struct s_kv_env +{ + t_str key; + t_str val; +} t_kv_env; + +typedef void (*t_hash_env_fn)(t_hasher *hasher, t_str *key); +typedef void (*t_drop_env_fn)(t_kv_env val); +typedef bool (*t_eq_env_fn)(t_str *lhs, t_str *rhs); + +typedef struct s_entry_env +{ + t_usize hash_id; + t_kv_env kv; + struct s_entry_env *next; +} t_entry_env; + +typedef struct s_hashmap_env +{ + t_entry_env **buckets; + t_usize num_buckets; + t_hasher hasher; + t_hash_env_fn hfunc; + t_eq_env_fn cfunc; + t_drop_env_fn drop; +} t_hashmap_env; + +t_hashmap_env *new_hashmap_env(t_hash_env_fn hash, + t_eq_env_fn cmp, + t_drop_env_fn drop); +t_hashmap_env *new_hashmap_with_buckets_env( + t_hash_env_fn hash, t_eq_env_fn cmp, + t_drop_env_fn drop, size_t cap); + +void drop_hashmap_env(t_hashmap_env *hmap); + +void insert_hashmap_env(t_hashmap_env *hmap, t_str key, + t_str value); + +t_str *get_hashmap_env(t_hashmap_env *hmap, + t_str *key); +void remove_hashmap_env(t_hashmap_env *hmap, t_str *key); + +t_entry_env *hashmap_get_entry_env(t_hashmap_env *hmap, + t_usize hash, + t_str *key, + t_entry_env **prev); + +t_error hashmap_env_iter(t_hashmap_env *self, + t_error (*func)(t_usize idx, + const t_str *key, + t_str *val, void *ctx), + void *ctx); + +#endif diff --git a/output/include/me/vec/vec_str.h b/output/include/me/vec/vec_str.h new file mode 100644 index 00000000..deaf0727 --- /dev/null +++ b/output/include/me/vec/vec_str.h @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_STR_H +#define VEC_STR_H + + +#include "me/types.h" + +typedef bool (*t_vec_str_sort_fn)(t_str *, t_str *); +typedef void (*t_free_str_item)(t_str); + +typedef struct s_vec_str +{ + t_free_str_item free_func; + t_usize len; + t_usize capacity; + t_str *buffer; +} t_vec_str; + +t_vec_str vec_str_new(t_usize capacity, + t_free_str_item free_function); +t_error vec_str_push(t_vec_str *vec, t_str element); +t_error vec_str_push_front(t_vec_str *vec, + t_str element); +t_error vec_str_pop(t_vec_str *vec, t_str *value); +t_error vec_str_pop_front(t_vec_str *vec, t_str *value); +void vec_str_free(t_vec_str vec); +t_error vec_str_reserve(t_vec_str *vec, + t_usize wanted_capacity); +t_error vec_str_find(t_vec_str *vec, + bool (*fn)(const t_str *), t_usize *index); +t_error vec_str_find_starting(t_vec_str *vec, + bool (*fn)(const t_str *), + t_usize starting_index, t_usize *index); +t_error vec_str_all(t_vec_str *vec, + bool (*fn)(const t_str *), bool *result); +t_error vec_str_any(t_vec_str *vec, + bool (*fn)(const t_str *), bool *result); +void vec_str_iter(t_vec_str *vec, + void (*fn)(t_usize index, t_str *value, + void *state), + void *state); +void vec_str_reverse(t_vec_str *vec); +void vec_str_sort(t_vec_str *vec, + t_vec_str_sort_fn is_sorted); +t_error vec_str_back(t_vec_str *vec, t_str **out); + +#endif diff --git a/output/src/hashmap/env/env.c b/output/src/hashmap/env/env.c new file mode 100644 index 00000000..ead07456 --- /dev/null +++ b/output/src/hashmap/env/env.c @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashmap_env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/06 10:58:20 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 15:32:51 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/hasher.h" +#include "me/hash/sip.h" +#include "me/hashmap/hashmap_env.h" +#include "me/mem/mem_alloc.h" +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/types.h" +#include + +t_hashmap_env *new_hashmap_env(t_hash_env_fn hfunc, + t_eq_env_fn cfunc, + t_drop_env_fn drop) +{ + return (new_hashmap_with_buckets_env(hfunc, cfunc, drop, + DEFAULT_BUCKETS)); +} + +t_hashmap_env *new_hashmap_with_buckets_env( + t_hash_env_fn hfunc, t_eq_env_fn cfunc, + t_drop_env_fn drop, t_usize buckets) +{ + t_hashmap_env *hmap; + + hmap = mem_alloc(sizeof(*hmap)); + if (hmap == NULL) + return (NULL); + hmap->buckets = mem_alloc_array(buckets, sizeof(t_entry_env *)); + hmap->num_buckets = buckets; + hmap->hasher = hasher_sip13_new(); + hmap->hfunc = hfunc; + hmap->cfunc = cfunc; + hmap->drop = drop; + if (hmap->buckets == NULL) + return ((void)free(hmap), NULL); + return (hmap); +} + +void drop_hashmap_env(t_hashmap_env *hmap) +{ + t_usize index; + + index = 0; + while (index < hmap->num_buckets) + { + if (hmap->buckets[index]) + { + hmap->drop(hmap->buckets[index]->kv); + free(hmap->buckets[index]); + } + index++; + } + hasher_finish(&hmap->hasher); + free(hmap->buckets); + free(hmap); +} + +t_entry_env *hashmap_get_entry_env(t_hashmap_env *hmap, + t_usize hashed_key, + t_str *key, + t_entry_env **prev) +{ + t_entry_env *entry; + + entry = hmap->buckets[hashed_key % hmap->num_buckets]; + while (entry != NULL) + { + if (!hmap->cfunc(&entry->kv.key, key)) + { + *prev = entry; + entry = entry->next; + } + else + { + return (entry); + } + } + return (NULL); +} + +void insert_hashmap_env(t_hashmap_env *hmap, t_str key, + t_str value) +{ + t_usize hashed_key; + t_entry_env *prev; + t_entry_env *entry; + + hmap->hfunc(&hmap->hasher, &key); + hashed_key = hasher_reset_and_finish(&hmap->hasher); + prev = NULL; + entry = hashmap_get_entry_env(hmap, hashed_key, &key, &prev); + if (entry == NULL) + { + entry = mem_alloc(sizeof(t_entry_env)); + entry->hash_id = hashed_key; + entry->kv = (t_kv_env){.key = key, .val = value}; + entry->next = NULL; + if (prev == NULL) + hmap->buckets[hashed_key % hmap->num_buckets] = entry; + else + prev->next = entry; + } + else + entry->kv.val = value; +} diff --git a/output/src/hashmap/env/env_iter.c b/output/src/hashmap/env/env_iter.c new file mode 100644 index 00000000..7f1c3668 --- /dev/null +++ b/output/src/hashmap/env/env_iter.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashmap_env.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hashmap/hashmap_env.h" + +t_error hashmap_env_iter(t_hashmap_env *self, + t_error (*func)(t_usize idx, + const t_str *key, + t_str *val, void *ctx), + void *ctx) +{ + t_usize bucket_id; + t_usize all_id; + t_entry_env *cur; + + bucket_id = 0; + all_id = 0; + while (bucket_id < self->num_buckets) + { + cur = self->buckets[bucket_id]; + while (cur != NULL) + { + if (func(all_id++, &cur->kv.key, &cur->kv.val, ctx)) + return (ERROR); + cur = cur->next; + } + bucket_id++; + } + return (NO_ERROR); +} diff --git a/output/src/hashmap/env/env_utils.c b/output/src/hashmap/env/env_utils.c new file mode 100644 index 00000000..2cde33a1 --- /dev/null +++ b/output/src/hashmap/env/env_utils.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashmap_env_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/06 10:58:20 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 15:35:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/sip.h" +#include "me/hashmap/hashmap_env.h" +#include "me/mem/mem_alloc.h" +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/types.h" +#include + +t_str *get_hashmap_env(t_hashmap_env *hmap, + t_str *key) +{ + t_usize hashed_key; + t_entry_env *entry; + t_entry_env *prev; + + hmap->hfunc(&hmap->hasher, key); + hashed_key = hasher_reset_and_finish(&hmap->hasher); + entry = hashmap_get_entry_env(hmap, hashed_key, key, &prev); + if (entry == NULL) + return (NULL); + return (&entry->kv.val); +} + +void remove_hashmap_env(t_hashmap_env *hmap, t_str *key) +{ + t_usize hashed_key; + t_entry_env *prev; + t_entry_env *entry; + + hmap->hfunc(&hmap->hasher, key); + hashed_key = hasher_reset_and_finish(&hmap->hasher); + hmap->hasher = hasher_sip13_new(); + prev = NULL; + entry = hashmap_get_entry_env(hmap, hashed_key, key, &prev); + if (entry == NULL) + return; + if (prev == NULL) + hmap->buckets[hashed_key % hmap->num_buckets] = entry->next; + else + prev->next = entry->next; + hmap->drop(entry->kv); + free(entry); + hmap->buckets[hashed_key % hmap->num_buckets] = NULL; +} diff --git a/output/src/vec/vec_parser_heredoc.c b/output/src/vec/parser_heredoc/parser_heredoc.c similarity index 100% rename from output/src/vec/vec_parser_heredoc.c rename to output/src/vec/parser_heredoc/parser_heredoc.c diff --git a/output/src/vec/vec_parser_heredoc_functions2.c b/output/src/vec/parser_heredoc/parser_heredoc_functions2.c similarity index 100% rename from output/src/vec/vec_parser_heredoc_functions2.c rename to output/src/vec/parser_heredoc/parser_heredoc_functions2.c diff --git a/output/src/vec/vec_parser_heredoc_functions3.c b/output/src/vec/parser_heredoc/parser_heredoc_functions3.c similarity index 100% rename from output/src/vec/vec_parser_heredoc_functions3.c rename to output/src/vec/parser_heredoc/parser_heredoc_functions3.c diff --git a/output/src/vec/vec_parser_range.c b/output/src/vec/parser_range/parser_range.c similarity index 100% rename from output/src/vec/vec_parser_range.c rename to output/src/vec/parser_range/parser_range.c diff --git a/output/src/vec/vec_parser_range_functions2.c b/output/src/vec/parser_range/parser_range_functions2.c similarity index 100% rename from output/src/vec/vec_parser_range_functions2.c rename to output/src/vec/parser_range/parser_range_functions2.c diff --git a/output/src/vec/vec_parser_range_functions3.c b/output/src/vec/parser_range/parser_range_functions3.c similarity index 100% rename from output/src/vec/vec_parser_range_functions3.c rename to output/src/vec/parser_range/parser_range_functions3.c diff --git a/output/src/vec/vec_reduce_action.c b/output/src/vec/reduce_action/reduce_action.c similarity index 100% rename from output/src/vec/vec_reduce_action.c rename to output/src/vec/reduce_action/reduce_action.c diff --git a/output/src/vec/vec_reduce_action_functions2.c b/output/src/vec/reduce_action/reduce_action_functions2.c similarity index 100% rename from output/src/vec/vec_reduce_action_functions2.c rename to output/src/vec/reduce_action/reduce_action_functions2.c diff --git a/output/src/vec/vec_reduce_action_functions3.c b/output/src/vec/reduce_action/reduce_action_functions3.c similarity index 100% rename from output/src/vec/vec_reduce_action_functions3.c rename to output/src/vec/reduce_action/reduce_action_functions3.c diff --git a/output/src/vec/str/str.c b/output/src/vec/str/str.c new file mode 100644 index 00000000..5b377023 --- /dev/null +++ b/output/src/vec/str/str.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_str.h" +#include + +t_vec_str vec_str_new(t_usize capacity, + t_free_str_item free_function) +{ + t_vec_str out; + + out = (t_vec_str){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_str)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_str_push(t_vec_str *vec, t_str element) +{ + t_str *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (vec->len + 1 > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (vec->len + 1 > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_str)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + vec->buffer[vec->len] = element; + vec->len += 1; + return (NO_ERROR); +} + +/// Return true in case of an error +t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity) +{ + t_str *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (wanted_capacity > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (wanted_capacity > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_str)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + return (NO_ERROR); +} + +/// Return true if the vector is empty +/// This function is safe to call with value being NULL +t_error vec_str_pop(t_vec_str *vec, t_str *value) +{ + t_str temp_value; + t_str *ptr; + + if (vec == NULL) + return (ERROR); + ptr = value; + if (vec->len == 0) + return (ERROR); + if (value == NULL) + ptr = &temp_value; + vec->len--; + *ptr = vec->buffer[vec->len]; + mem_set_zero(&vec->buffer[vec->len], sizeof(t_str)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_str_free(t_vec_str vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/output/src/vec/str/str_functions2.c b/output/src/vec/str/str_functions2.c new file mode 100644 index 00000000..0b3a88bb --- /dev/null +++ b/output/src/vec/str/str_functions2.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_str.h" +#include + +t_error vec_str_find(t_vec_str *vec, + bool (*fn)(const t_str *), t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = 0; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_str_find_starting(t_vec_str *vec, + bool (*fn)(const t_str *), + t_usize starting_index, t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = starting_index; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_str_all(t_vec_str *vec, + bool (*fn)(const t_str *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = true; + while (*result && idx < vec->len) + { + if (!fn(&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_str_any(t_vec_str *vec, + bool (*fn)(const t_str *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = false; + while (*result && idx < vec->len) + { + if (fn(&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_str_iter(t_vec_str *vec, + void (*fn)(t_usize index, t_str *value, + void *state), + void *state) +{ + t_usize idx; + + if (vec == NULL || fn == NULL) + return; + idx = 0; + while (idx < vec->len) + { + fn(idx, &vec->buffer[idx], state); + idx++; + } +} diff --git a/output/src/vec/str/str_functions3.c b/output/src/vec/str/str_functions3.c new file mode 100644 index 00000000..b0fd48e7 --- /dev/null +++ b/output/src/vec/str/str_functions3.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_str.h" +#include + +t_error vec_str_push_front(t_vec_str *vec, + t_str element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_str_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && + vec_str_reserve(vec, 3 * vec->len / 2 + 1)) + return (ERROR); + while (i > 0) + { + vec->buffer[i + 1] = vec->buffer[i]; + i--; + } + vec->buffer[1] = vec->buffer[0]; + vec->buffer[0] = element; + vec->len++; + return (NO_ERROR); +} + +t_error vec_str_pop_front(t_vec_str *vec, t_str *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_str_pop(vec, value)); + i = 0; + *value = vec->buffer[0]; + vec->len--; + while (i < vec->len) + { + vec->buffer[i] = vec->buffer[i + 1]; + i++; + } + mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer)); + return (NO_ERROR); +} + +void vec_str_reverse(t_vec_str *vec) +{ + t_str temporary; + t_usize i; + + i = 0; + while (i < vec->len / 2) + { + temporary = vec->buffer[vec->len - 1 - i]; + vec->buffer[vec->len - 1 - i] = vec->buffer[i]; + vec->buffer[i] = temporary; + i++; + } +} + +t_error vec_str_back(t_vec_str *vec, t_str **out) +{ + t_str *temporary; + + if (out == NULL) + out = &temporary; + if (vec->len != 0) + return (*out = &vec->buffer[vec->len - 1], true); + return (false); +} diff --git a/sources/env.c b/sources/env.c new file mode 100644 index 00000000..4020c7d1 --- /dev/null +++ b/sources/env.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/04 18:32:50 by maiboyer #+# #+# */ +/* Updated: 2024/05/04 19:16:37 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/env.h" +#include "me/buffered_str/buf_str.h" +#include "me/hash/hasher.h" +#include "me/hashmap/hashmap_env.h" +#include "me/string/str_compare.h" +#include "me/types.h" +#include "me/vec/vec_str.h" +#include + +static void _hash_str(t_hasher *hasher, t_str *s) +{ + hasher_write_str(hasher, *s); +} + +static bool _cmp_str(t_str *s1, t_str *s2) +{ + return (str_compare(*s1, *s2)); +} + +static void _free_env(t_kv_env kv) +{ + if (kv.key) + free(kv.key); + if (kv.val) + free(kv.val); +} + +t_hashmap_env *create_env_map(void) +{ + return (new_hashmap_env(_hash_str, _cmp_str, _free_env)); +} + +t_error _build_envp_iterator(t_usize idx, const t_str *key, t_str *val, + void *ctx) +{ + struct s_build_envp_state *s; + + (void)(idx); + s = ctx; + if (push_str_buffer(&s->buf, *key) && push_str_char(&s->buf, '=') && + push_str_buffer(&s->buf, *val)) + return (vec_str_free(s->out), ERROR); + if (vec_str_push(&s->out, s->buf.buf)) + return (str_free(s->buf), ERROR); + s->buf = alloc_new_buffer(50); + return (NO_ERROR); +} + +t_error build_envp(t_hashmap_env *envs, t_vec_str *envp) +{ + struct s_build_envp_state state; + + state.out = vec_str_new(envs->num_buckets, (void (*)(t_str))free); + state.buf = alloc_new_buffer(50); + if (hashmap_env_iter(envs, _build_envp_iterator, &state)) + return (ERROR); + if (vec_str_push(&state.out, NULL)) + return (ERROR); + *envp = state.out; + str_free(state.buf); + return (NO_ERROR); +} diff --git a/sources/exec/handle_expension.c b/sources/exec/handle_expension.c new file mode 100644 index 00000000..89b40872 --- /dev/null +++ b/sources/exec/handle_expension.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* handle_expension.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/03 15:24:25 by maiboyer #+# #+# */ +/* Updated: 2024/05/04 18:28:39 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "app/node/handle_expension.h" +#include "gmr/symbols.h" +#include "me/types.h" + +t_error handle_expension_complex(t_node *self, t_utils *shcat, t_str *ret) +{ + (void)(self); + (void)(shcat); + (void)(ret); + return (ERROR); +} +t_error handle_expension_simple(t_node *self, t_utils *shcat, t_str *ret) +{ + (void)(self); + (void)(shcat); + (void)(ret); + return (ERROR); +} + +t_error handle_expension(t_node *self, t_utils *shcat, t_str *ret) +{ + if (self->childs_count != 0 && self->childs[0].kind == anon_sym_DOLLAR) + return (handle_expension_simple(self, shcat, ret)); + if (self->childs_count != 0 && + self->childs[0].kind == anon_sym_DOLLAR_LBRACE) + return (handle_expension_complex(self, shcat, ret)); + return (ERROR); +} diff --git a/sources/ft_cmd.c b/sources/ft_cmd.c index 766be78c..b5c175e9 100644 --- a/sources/ft_cmd.c +++ b/sources/ft_cmd.c @@ -6,47 +6,47 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/13 20:26:13 by rparodi #+# #+# */ -/* Updated: 2024/04/30 21:31:27 by maiboyer ### ########.fr */ +/* Updated: 2024/05/04 18:59:58 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/minishell.h" -void ft_exec_cmd(t_utils *utils, t_str cmd, t_str cmd_args[]) -{ - if (execve(cmd, cmd_args, utils->envp) == -1) - { - printf("ERROR >\n execve\n %s\n", cmd); - ft_free_strs((char **)cmd_args); - ft_exit(utils, 1); - } -} - -void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i) -{ - pid_t pid; - t_i32 options; - t_str *args; - t_usize k; - // t_str cmd; - - k = prev_i; - args = (t_str *)malloc(sizeof(t_str) * (i + 2)); - while (prev_i < i) - { - printf("ft_other_cmd = %s\n", shcat->strs_input[prev_i]); - args[k] = malloc(ft_strlen(shcat->strs_input[prev_i])); - ft_strlcpy(args[k], shcat->strs_input[prev_i], ft_strlen(shcat->strs_input[prev_i])); - prev_i++; - k++; - } - args[k] = NULL; - // cmd = ft_check_cmds(shcat, args[tmp]); - options = 0; - pid = fork(); - if (pid == -1) - ft_exit(shcat, 1); - // if (pid == 0) - // ft_exec_cmd(shcat, cmd, args); - waitpid(pid, NULL, options); -} +// void ft_exec_cmd(t_utils *utils, t_str cmd, t_str cmd_args[]) +// { +// if (execve(cmd, cmd_args, utils->envp) == -1) +// { +// printf("ERROR >\n execve\n %s\n", cmd); +// ft_free_strs((char **)cmd_args); +// ft_exit(utils, 1); +// } +// } +// +// void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i) +// { +// pid_t pid; +// t_i32 options; +// t_str *args; +// t_usize k; +// // t_str cmd; +// +// k = prev_i; +// args = (t_str *)malloc(sizeof(t_str) * (i + 2)); +// while (prev_i < i) +// { +// printf("ft_other_cmd = %s\n", shcat->strs_input[prev_i]); +// args[k] = malloc(ft_strlen(shcat->strs_input[prev_i])); +// ft_strlcpy(args[k], shcat->strs_input[prev_i], ft_strlen(shcat->strs_input[prev_i])); +// prev_i++; +// k++; +// } +// args[k] = NULL; +// // cmd = ft_check_cmds(shcat, args[tmp]); +// options = 0; +// pid = fork(); +// if (pid == -1) +// ft_exit(shcat, 1); +// // if (pid == 0) +// // ft_exec_cmd(shcat, cmd, args); +// waitpid(pid, NULL, options); +// } diff --git a/sources/main.c b/sources/main.c index 33740396..85005b36 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,15 +6,16 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ -/* Updated: 2024/05/02 15:26:55 by maiboyer ### ########.fr */ +/* Updated: 2024/05/04 19:21:16 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../includes/minishell.h" +#include "app/env.h" #include "app/node.h" #include "app/signal_handler.h" #include "gmr/symbols.h" #include "me/string/str_len.h" +#include "minishell.h" #include "parser/api.h" t_first_parser *ts_parser_new(); @@ -55,27 +56,27 @@ t_node parse_str(t_parser *parser, t_const_str input) return (parse_to_nodes(parser->parser, input)); } -void ft_check(t_utils *shcat, char **input) -{ - t_usize i; - t_usize prev_i; +// void ft_check(t_utils *shcat, char **input) +// { +// t_usize i; +// t_usize prev_i; +// +// i = 0; +// prev_i = 0; +// while (input[i] != NULL) +// { +// if (ft_strcmp(input[i], "exit") == 0) +// ft_exit(shcat, 0); +// else if (ft_strcmp(input[i], "pwd") == 0) +// ft_pwd(); +// else +// ft_other_cmd(shcat, i, prev_i); +// prev_i = i; +// i++; +// } +// } - i = 0; - prev_i = 0; - while (input[i] != NULL) - { - if (ft_strcmp(input[i], "exit") == 0) - ft_exit(shcat, 0); - else if (ft_strcmp(input[i], "pwd") == 0) - ft_pwd(); - else - ft_other_cmd(shcat, i, prev_i); - prev_i = i; - i++; - } -} - -t_error handle_concat(t_node *self, t_utils* shcat, t_str *ret); +t_error handle_concat(t_node *self, t_utils *shcat, t_str *ret); void print_node_concat(t_node *self) { @@ -163,6 +164,7 @@ t_i32 main(t_i32 argc, t_str argv[], t_str envp[]) if (install_signal()) return (1); utils = (t_utils){}; + utils.env = create_env_map(); utils.parser = create_myparser(); utils.name_shell = "\001\x1B[93m\002" "42sh" diff --git a/src.list b/src.list index 41fb1256..22c57427 100644 --- a/src.list +++ b/src.list @@ -1,4 +1,6 @@ +env.c exec/handle_concat.c +exec/handle_expension.c exec/handle_raw_word.c exec/handle_word.c ft_cmd.c diff --git a/stdme/gen.list b/stdme/gen.list index e71784aa..67b75729 100644 --- a/stdme/gen.list +++ b/stdme/gen.list @@ -14,6 +14,15 @@ convert/str_to_u64 convert/str_to_u64_utils convert/str_to_u8 convert/str_to_u8_utils +vec/buf_str/buf_str +vec/buf_str/buf_str_functions2 +vec/buf_str/buf_str_functions3 +vec/str/str +vec/str/str_functions2 +vec/str/str_functions3 +vec/u8/u8 +vec/u8/u8_functions2 +vec/u8/u8_functions3 vec/vec_buf_str vec/vec_buf_str_functions2 vec/vec_buf_str_functions3 diff --git a/stdme/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__ b/stdme/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__ index 78aca68f..cdd51c4f 100644 --- a/stdme/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__ +++ b/stdme/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__ @@ -73,4 +73,10 @@ t_entry_C__PREFIX__ *hashmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *key, t_entry_C__PREFIX__ **prev); +t_error hashmap_C__PREFIX___iter(t_hashmap_C__PREFIX__ *self, + t_error (*func)(t_usize idx, + const C__KEYTYPE__ *key, + C__VALTYPE__ *val, void *ctx), + void *ctx); + #endif diff --git a/stdme/generic_sources/src/str_to_C__PREFIX__.c__TEMPLATE__ b/stdme/generic_sources/src/convert/str_to_C__PREFIX__.c__TEMPLATE__ similarity index 100% rename from stdme/generic_sources/src/str_to_C__PREFIX__.c__TEMPLATE__ rename to stdme/generic_sources/src/convert/str_to_C__PREFIX__.c__TEMPLATE__ diff --git a/stdme/generic_sources/src/str_to_C__PREFIX___utils.c__TEMPLATE__ b/stdme/generic_sources/src/convert/str_to_C__PREFIX___utils.c__TEMPLATE__ similarity index 100% rename from stdme/generic_sources/src/str_to_C__PREFIX___utils.c__TEMPLATE__ rename to stdme/generic_sources/src/convert/str_to_C__PREFIX___utils.c__TEMPLATE__ diff --git a/stdme/generic_sources/src/hashmap_C__PREFIX__.c__TEMPLATE__ b/stdme/generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__ similarity index 96% rename from stdme/generic_sources/src/hashmap_C__PREFIX__.c__TEMPLATE__ rename to stdme/generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__ index 17a66925..d1beb3f6 100644 --- a/stdme/generic_sources/src/hashmap_C__PREFIX__.c__TEMPLATE__ +++ b/stdme/generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__ @@ -102,9 +102,9 @@ void insert_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key, entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, &key, &prev); if (entry == NULL) { - entry = mem_alloc(sizeof(t_entry_tile)); + entry = mem_alloc(sizeof(t_entry_C__PREFIX__)); entry->hash_id = hashed_key; - entry->kv = (t_kv_tile){.key = key, .val = value}; + entry->kv = (t_kv_C__PREFIX__){.key = key, .val = value}; entry->next = NULL; if (prev == NULL) hmap->buckets[hashed_key % hmap->num_buckets] = entry; diff --git a/stdme/generic_sources/src/hashmap/C__PREFIX___iter.c__TEMPLATE__ b/stdme/generic_sources/src/hashmap/C__PREFIX___iter.c__TEMPLATE__ new file mode 100644 index 00000000..1f292b83 --- /dev/null +++ b/stdme/generic_sources/src/hashmap/C__PREFIX___iter.c__TEMPLATE__ @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashmap_C__PREFIX__.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */ +/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hashmap/hashmap_C__PREFIX__.h" + +t_error hashmap_C__PREFIX___iter(t_hashmap_C__PREFIX__ *self, + t_error (*func)(t_usize idx, + const C__KEYTYPE__ *key, + C__VALTYPE__ *val, void *ctx), + void *ctx) +{ + t_usize bucket_id; + t_usize all_id; + t_entry_C__PREFIX__ *cur; + + bucket_id = 0; + all_id = 0; + while (bucket_id < self->num_buckets) + { + cur = self->buckets[bucket_id]; + while (cur != NULL) + { + if (func(all_id++, &cur->kv.key, &cur->kv.val, ctx)) + return (ERROR); + cur = cur->next; + } + bucket_id++; + } + return (NO_ERROR); +} diff --git a/stdme/generic_sources/src/hashmap_C__PREFIX___utils.c__TEMPLATE__ b/stdme/generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__ similarity index 96% rename from stdme/generic_sources/src/hashmap_C__PREFIX___utils.c__TEMPLATE__ rename to stdme/generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__ index 83c542f0..00e4e2de 100644 --- a/stdme/generic_sources/src/hashmap_C__PREFIX___utils.c__TEMPLATE__ +++ b/stdme/generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__ @@ -27,7 +27,7 @@ C__VALTYPE__ *get_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap, hmap->hfunc(&hmap->hasher, key); hashed_key = hasher_reset_and_finish(&hmap->hasher); - entry = hashmap_get_entry_tile(hmap, hashed_key, key, &prev); + entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, key, &prev); if (entry == NULL) return (NULL); return (&entry->kv.val); diff --git a/stdme/generic_sources/src/vec_C__PREFIX__.c__TEMPLATE__ b/stdme/generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__ similarity index 100% rename from stdme/generic_sources/src/vec_C__PREFIX__.c__TEMPLATE__ rename to stdme/generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__ diff --git a/stdme/generic_sources/src/vec_C__PREFIX___functions2.c__TEMPLATE__ b/stdme/generic_sources/src/vec/C__PREFIX___functions2.c__TEMPLATE__ similarity index 100% rename from stdme/generic_sources/src/vec_C__PREFIX___functions2.c__TEMPLATE__ rename to stdme/generic_sources/src/vec/C__PREFIX___functions2.c__TEMPLATE__ diff --git a/stdme/generic_sources/src/vec_C__PREFIX___functions3.c__TEMPLATE__ b/stdme/generic_sources/src/vec/C__PREFIX___functions3.c__TEMPLATE__ similarity index 100% rename from stdme/generic_sources/src/vec_C__PREFIX___functions3.c__TEMPLATE__ rename to stdme/generic_sources/src/vec/C__PREFIX___functions3.c__TEMPLATE__ diff --git a/stdme/generic_sources/src/vec_C__PREFIX___sort.c__TEMPLATE__ b/stdme/generic_sources/src/vec/C__PREFIX___sort.c__TEMPLATE__ similarity index 100% rename from stdme/generic_sources/src/vec_C__PREFIX___sort.c__TEMPLATE__ rename to stdme/generic_sources/src/vec/C__PREFIX___sort.c__TEMPLATE__ diff --git a/stdme/include/me/string/str_compare.h b/stdme/include/me/string/str_compare.h new file mode 100644 index 00000000..e3a64537 --- /dev/null +++ b/stdme/include/me/string/str_compare.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_compare.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 18:53:47 by maiboyer #+# #+# */ +/* Updated: 2024/05/04 18:37:55 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_COMPARE_H +# define STR_COMPARE_H + +# include "me/types.h" + +bool str_compare(t_const_str lhs, t_const_str rhs); + +#endif diff --git a/stdme/input.toml b/stdme/input.toml index 0077675f..0c3da5e6 100644 --- a/stdme/input.toml +++ b/stdme/input.toml @@ -1,9 +1,9 @@ [definition.vec] headers = ["generic_sources/header/vec_C__PREFIX__.h__TEMPLATE__"] sources = [ - "generic_sources/src/vec_C__PREFIX__.c__TEMPLATE__", - "generic_sources/src/vec_C__PREFIX___functions2.c__TEMPLATE__", - "generic_sources/src/vec_C__PREFIX___functions3.c__TEMPLATE__", + "generic_sources/src/vec/C__PREFIX__.c__TEMPLATE__", + "generic_sources/src/vec/C__PREFIX___functions2.c__TEMPLATE__", + "generic_sources/src/vec/C__PREFIX___functions3.c__TEMPLATE__", ] replace.C__TYPENAME__ = "type" replace.C__TYPEHEADER__ = "header_include" @@ -14,8 +14,8 @@ replace.C__PREFIXUP__ = "prefix" [definition.hashmap] headers = ["generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__"] sources = [ - "generic_sources/src/hashmap_C__PREFIX__.c__TEMPLATE__", - "generic_sources/src/hashmap_C__PREFIX___utils.c__TEMPLATE__", + "generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__", + "generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__", ] replace.C__VALTYPE__ = "type" replace.C__KEYTYPE__ = "type" @@ -26,8 +26,8 @@ replace.C__PREFIXUP__ = "prefix" [definition.str_to_num] headers = [] sources = [ - "generic_sources/src/str_to_C__PREFIX__.c__TEMPLATE__", - "generic_sources/src/str_to_C__PREFIX___utils.c__TEMPLATE__", + "generic_sources/src/convert/str_to_C__PREFIX__.c__TEMPLATE__", + "generic_sources/src/convert/str_to_C__PREFIX___utils.c__TEMPLATE__", ] replace.C__TYPE__ = "type" replace.C__UNSIGNED_TYPE__ = "type" @@ -128,7 +128,7 @@ replace.C__SIGNED_TYPE__ = "false" [[create.vec]] -sources_output = "src/vec/" +sources_output = "src/vec/C__PREFIX__" headers_output = "include/me/vec/" replace.C__TYPENAME__ = "t_u8" replace.C__TYPEHEADER__ = '' @@ -136,7 +136,7 @@ replace.C__PREFIX__ = "u8" replace.C__PREFIXUP__ = "U8" [[create.vec]] -sources_output = "src/vec/" +sources_output = "src/vec/C__PREFIX__" headers_output = "include/me/vec/" replace.C__TYPENAME__ = "t_str" replace.C__TYPEHEADER__ = '' @@ -144,7 +144,7 @@ replace.C__PREFIX__ = "str" replace.C__PREFIXUP__ = "STR" [[create.vec]] -sources_output = "src/vec/" +sources_output = "src/vec/C__PREFIX__" headers_output = "include/me/vec/" replace.C__TYPENAME__ = "t_buffer_str" replace.C__TYPEHEADER__ = '#include "me/buffered_str/buf_str.h"' diff --git a/stdme/output/src/vec/buf_str/buf_str.c b/stdme/output/src/vec/buf_str/buf_str.c new file mode 100644 index 00000000..8dd32251 --- /dev/null +++ b/stdme/output/src/vec/buf_str/buf_str.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_buf_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_buf_str.h" +#include + +t_vec_buf_str vec_buf_str_new(t_usize capacity, + t_free_buf_str_item free_function) +{ + t_vec_buf_str out; + + out = (t_vec_buf_str){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_buffer_str)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_buf_str_push(t_vec_buf_str *vec, t_buffer_str element) +{ + t_buffer_str *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (vec->len + 1 > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (vec->len + 1 > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_buffer_str)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_buffer_str)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + vec->buffer[vec->len] = element; + vec->len += 1; + return (NO_ERROR); +} + +/// Return true in case of an error +t_error vec_buf_str_reserve(t_vec_buf_str *vec, t_usize wanted_capacity) +{ + t_buffer_str *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (wanted_capacity > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (wanted_capacity > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_buffer_str)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_buffer_str)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + return (NO_ERROR); +} + +/// Return true if the vector is empty +/// This function is safe to call with value being NULL +t_error vec_buf_str_pop(t_vec_buf_str *vec, t_buffer_str *value) +{ + t_buffer_str temp_value; + t_buffer_str *ptr; + + if (vec == NULL) + return (ERROR); + ptr = value; + if (vec->len == 0) + return (ERROR); + if (value == NULL) + ptr = &temp_value; + vec->len--; + *ptr = vec->buffer[vec->len]; + mem_set_zero(&vec->buffer[vec->len], sizeof(t_buffer_str)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_buf_str_free(t_vec_buf_str vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/stdme/output/src/vec/buf_str/buf_str_functions2.c b/stdme/output/src/vec/buf_str/buf_str_functions2.c new file mode 100644 index 00000000..6d7fa65c --- /dev/null +++ b/stdme/output/src/vec/buf_str/buf_str_functions2.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_buf_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_buf_str.h" +#include + +t_error vec_buf_str_find(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = 0; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_buf_str_find_starting(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), + t_usize starting_index, t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = starting_index; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_buf_str_all(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = true; + while (*result && idx < vec->len) + { + if (!fn(&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_buf_str_any(t_vec_buf_str *vec, + bool (*fn)(const t_buffer_str *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = false; + while (*result && idx < vec->len) + { + if (fn(&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_buf_str_iter(t_vec_buf_str *vec, + void (*fn)(t_usize index, t_buffer_str *value, + void *state), + void *state) +{ + t_usize idx; + + if (vec == NULL || fn == NULL) + return; + idx = 0; + while (idx < vec->len) + { + fn(idx, &vec->buffer[idx], state); + idx++; + } +} diff --git a/stdme/output/src/vec/buf_str/buf_str_functions3.c b/stdme/output/src/vec/buf_str/buf_str_functions3.c new file mode 100644 index 00000000..8b2205b4 --- /dev/null +++ b/stdme/output/src/vec/buf_str/buf_str_functions3.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_buf_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_buf_str.h" +#include + +t_error vec_buf_str_push_front(t_vec_buf_str *vec, + t_buffer_str element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_buf_str_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && + vec_buf_str_reserve(vec, 3 * vec->len / 2 + 1)) + return (ERROR); + while (i > 0) + { + vec->buffer[i + 1] = vec->buffer[i]; + i--; + } + vec->buffer[1] = vec->buffer[0]; + vec->buffer[0] = element; + vec->len++; + return (NO_ERROR); +} + +t_error vec_buf_str_pop_front(t_vec_buf_str *vec, t_buffer_str *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_buf_str_pop(vec, value)); + i = 0; + *value = vec->buffer[0]; + vec->len--; + while (i < vec->len) + { + vec->buffer[i] = vec->buffer[i + 1]; + i++; + } + mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer)); + return (NO_ERROR); +} + +void vec_buf_str_reverse(t_vec_buf_str *vec) +{ + t_buffer_str temporary; + t_usize i; + + i = 0; + while (i < vec->len / 2) + { + temporary = vec->buffer[vec->len - 1 - i]; + vec->buffer[vec->len - 1 - i] = vec->buffer[i]; + vec->buffer[i] = temporary; + i++; + } +} + +t_error vec_buf_str_back(t_vec_buf_str *vec, t_buffer_str **out) +{ + t_buffer_str *temporary; + + if (out == NULL) + out = &temporary; + if (vec->len != 0) + return (*out = &vec->buffer[vec->len - 1], true); + return (false); +} diff --git a/stdme/output/src/vec/str/str.c b/stdme/output/src/vec/str/str.c new file mode 100644 index 00000000..5b377023 --- /dev/null +++ b/stdme/output/src/vec/str/str.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_str.h" +#include + +t_vec_str vec_str_new(t_usize capacity, + t_free_str_item free_function) +{ + t_vec_str out; + + out = (t_vec_str){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_str)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_str_push(t_vec_str *vec, t_str element) +{ + t_str *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (vec->len + 1 > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (vec->len + 1 > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_str)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + vec->buffer[vec->len] = element; + vec->len += 1; + return (NO_ERROR); +} + +/// Return true in case of an error +t_error vec_str_reserve(t_vec_str *vec, t_usize wanted_capacity) +{ + t_str *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (wanted_capacity > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (wanted_capacity > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_str)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_str)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + return (NO_ERROR); +} + +/// Return true if the vector is empty +/// This function is safe to call with value being NULL +t_error vec_str_pop(t_vec_str *vec, t_str *value) +{ + t_str temp_value; + t_str *ptr; + + if (vec == NULL) + return (ERROR); + ptr = value; + if (vec->len == 0) + return (ERROR); + if (value == NULL) + ptr = &temp_value; + vec->len--; + *ptr = vec->buffer[vec->len]; + mem_set_zero(&vec->buffer[vec->len], sizeof(t_str)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_str_free(t_vec_str vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/stdme/output/src/vec/str/str_functions2.c b/stdme/output/src/vec/str/str_functions2.c new file mode 100644 index 00000000..0b3a88bb --- /dev/null +++ b/stdme/output/src/vec/str/str_functions2.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_str.h" +#include + +t_error vec_str_find(t_vec_str *vec, + bool (*fn)(const t_str *), t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = 0; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_str_find_starting(t_vec_str *vec, + bool (*fn)(const t_str *), + t_usize starting_index, t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = starting_index; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_str_all(t_vec_str *vec, + bool (*fn)(const t_str *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = true; + while (*result && idx < vec->len) + { + if (!fn(&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_str_any(t_vec_str *vec, + bool (*fn)(const t_str *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = false; + while (*result && idx < vec->len) + { + if (fn(&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_str_iter(t_vec_str *vec, + void (*fn)(t_usize index, t_str *value, + void *state), + void *state) +{ + t_usize idx; + + if (vec == NULL || fn == NULL) + return; + idx = 0; + while (idx < vec->len) + { + fn(idx, &vec->buffer[idx], state); + idx++; + } +} diff --git a/stdme/output/src/vec/str/str_functions3.c b/stdme/output/src/vec/str/str_functions3.c new file mode 100644 index 00000000..b0fd48e7 --- /dev/null +++ b/stdme/output/src/vec/str/str_functions3.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_str.h" +#include + +t_error vec_str_push_front(t_vec_str *vec, + t_str element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_str_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && + vec_str_reserve(vec, 3 * vec->len / 2 + 1)) + return (ERROR); + while (i > 0) + { + vec->buffer[i + 1] = vec->buffer[i]; + i--; + } + vec->buffer[1] = vec->buffer[0]; + vec->buffer[0] = element; + vec->len++; + return (NO_ERROR); +} + +t_error vec_str_pop_front(t_vec_str *vec, t_str *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_str_pop(vec, value)); + i = 0; + *value = vec->buffer[0]; + vec->len--; + while (i < vec->len) + { + vec->buffer[i] = vec->buffer[i + 1]; + i++; + } + mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer)); + return (NO_ERROR); +} + +void vec_str_reverse(t_vec_str *vec) +{ + t_str temporary; + t_usize i; + + i = 0; + while (i < vec->len / 2) + { + temporary = vec->buffer[vec->len - 1 - i]; + vec->buffer[vec->len - 1 - i] = vec->buffer[i]; + vec->buffer[i] = temporary; + i++; + } +} + +t_error vec_str_back(t_vec_str *vec, t_str **out) +{ + t_str *temporary; + + if (out == NULL) + out = &temporary; + if (vec->len != 0) + return (*out = &vec->buffer[vec->len - 1], true); + return (false); +} diff --git a/stdme/output/src/vec/u8/u8.c b/stdme/output/src/vec/u8/u8.c new file mode 100644 index 00000000..8a4bfc5b --- /dev/null +++ b/stdme/output/src/vec/u8/u8.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_u8.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 18:46:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:54:11 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_u8.h" +#include + +t_vec_u8 vec_u8_new(t_usize capacity, + t_free_u8_item free_function) +{ + t_vec_u8 out; + + out = (t_vec_u8){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_u8)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_u8_push(t_vec_u8 *vec, t_u8 element) +{ + t_u8 *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (vec->len + 1 > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (vec->len + 1 > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_u8)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_u8)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + vec->buffer[vec->len] = element; + vec->len += 1; + return (NO_ERROR); +} + +/// Return true in case of an error +t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity) +{ + t_u8 *temp_buffer; + size_t new_capacity; + + if (vec == NULL) + return (ERROR); + if (wanted_capacity > vec->capacity) + { + new_capacity = (vec->capacity * 3) / 2 + 1; + while (wanted_capacity > new_capacity) + new_capacity = (new_capacity * 3) / 2 + 1; + temp_buffer = mem_alloc_array(new_capacity, sizeof(t_u8)); + if (temp_buffer == NULL) + return (ERROR); + mem_copy(temp_buffer, vec->buffer, vec->len * sizeof(t_u8)); + free(vec->buffer); + vec->buffer = temp_buffer; + vec->capacity = new_capacity; + } + return (NO_ERROR); +} + +/// Return true if the vector is empty +/// This function is safe to call with value being NULL +t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value) +{ + t_u8 temp_value; + t_u8 *ptr; + + if (vec == NULL) + return (ERROR); + ptr = value; + if (vec->len == 0) + return (ERROR); + if (value == NULL) + ptr = &temp_value; + vec->len--; + *ptr = vec->buffer[vec->len]; + mem_set_zero(&vec->buffer[vec->len], sizeof(t_u8)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_u8_free(t_vec_u8 vec) +{ + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + free(vec.buffer); +} diff --git a/stdme/output/src/vec/u8/u8_functions2.c b/stdme/output/src/vec/u8/u8_functions2.c new file mode 100644 index 00000000..bb567f29 --- /dev/null +++ b/stdme/output/src/vec/u8/u8_functions2.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_u8.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_u8.h" +#include + +t_error vec_u8_find(t_vec_u8 *vec, + bool (*fn)(const t_u8 *), t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = 0; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_u8_find_starting(t_vec_u8 *vec, + bool (*fn)(const t_u8 *), + t_usize starting_index, t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = starting_index; + while (idx < vec->len) + { + if (fn(&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_u8_all(t_vec_u8 *vec, + bool (*fn)(const t_u8 *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = true; + while (*result && idx < vec->len) + { + if (!fn(&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_u8_any(t_vec_u8 *vec, + bool (*fn)(const t_u8 *), bool *result) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || result == NULL) + return (ERROR); + idx = 0; + *result = false; + while (*result && idx < vec->len) + { + if (fn(&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_u8_iter(t_vec_u8 *vec, + void (*fn)(t_usize index, t_u8 *value, + void *state), + void *state) +{ + t_usize idx; + + if (vec == NULL || fn == NULL) + return; + idx = 0; + while (idx < vec->len) + { + fn(idx, &vec->buffer[idx], state); + idx++; + } +} diff --git a/stdme/output/src/vec/u8/u8_functions3.c b/stdme/output/src/vec/u8/u8_functions3.c new file mode 100644 index 00000000..f244463c --- /dev/null +++ b/stdme/output/src/vec/u8/u8_functions3.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_u8.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ +/* Updated: 2023/12/30 17:59:28 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/mem/mem_alloc_array.h" +#include "me/mem/mem_copy.h" +#include "me/mem/mem_set_zero.h" +#include "me/types.h" +#include "me/vec/vec_u8.h" +#include + +t_error vec_u8_push_front(t_vec_u8 *vec, + t_u8 element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_u8_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && + vec_u8_reserve(vec, 3 * vec->len / 2 + 1)) + return (ERROR); + while (i > 0) + { + vec->buffer[i + 1] = vec->buffer[i]; + i--; + } + vec->buffer[1] = vec->buffer[0]; + vec->buffer[0] = element; + vec->len++; + return (NO_ERROR); +} + +t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_u8_pop(vec, value)); + i = 0; + *value = vec->buffer[0]; + vec->len--; + while (i < vec->len) + { + vec->buffer[i] = vec->buffer[i + 1]; + i++; + } + mem_set_zero(&vec->buffer[i], sizeof(*vec->buffer)); + return (NO_ERROR); +} + +void vec_u8_reverse(t_vec_u8 *vec) +{ + t_u8 temporary; + t_usize i; + + i = 0; + while (i < vec->len / 2) + { + temporary = vec->buffer[vec->len - 1 - i]; + vec->buffer[vec->len - 1 - i] = vec->buffer[i]; + vec->buffer[i] = temporary; + i++; + } +} + +t_error vec_u8_back(t_vec_u8 *vec, t_u8 **out) +{ + t_u8 *temporary; + + if (out == NULL) + out = &temporary; + if (vec->len != 0) + return (*out = &vec->buffer[vec->len - 1], true); + return (false); +} diff --git a/stdme/src.list b/stdme/src.list index cb42365a..fdb253e8 100644 --- a/stdme/src.list +++ b/stdme/src.list @@ -41,6 +41,7 @@ fs/write gnl/get_next_line hash/hasher hash/hash_signed +hash/hash_str hash/hash_unsigned hash/sip/sip13 hash/sip/sip_utils @@ -91,6 +92,7 @@ printf/matchers printf/printf printf/vprintf string/str_clone +string/str_compare string/str_find_chr string/str_find_rev_chr string/str_find_str diff --git a/stdme/src/hash/hash_str.c b/stdme/src/hash/hash_str.c new file mode 100644 index 00000000..463e3181 --- /dev/null +++ b/stdme/src/hash/hash_str.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hash_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/04 19:17:11 by maiboyer #+# #+# */ +/* Updated: 2024/05/04 19:18:26 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/hash/hasher.h" +#include "me/string/str_len.h" +#include "me/types.h" + +void hasher_write_str(t_hasher *hasher, t_str s) +{ + hasher_write_bytes(hasher, (t_u8 *)s, str_len(s)); +} diff --git a/stdme/src/string/str_compare.c b/stdme/src/string/str_compare.c new file mode 100644 index 00000000..5a2db02f --- /dev/null +++ b/stdme/src/string/str_compare.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str_compare.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/04 18:53:47 by maiboyer #+# #+# */ +/* Updated: 2024/05/04 18:37:40 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/string/str_compare.h" + +// PLEASE FIX THIS FUNCTION IF NEEDED ! +bool str_compare(t_const_str lhs, t_const_str rhs) +{ + t_usize index; + + if (lhs == NULL || rhs == NULL) + return (lhs == rhs); + index = 0; + while (lhs[index] && rhs[index] && lhs[index] == rhs[index]) + index++; + return ((t_i32)(t_u8)lhs[index] - (t_i32)(t_u8)rhs[index]); +}