From 970507dfdd2d3d4eb933a60590598025f75e9c85 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Tue, 30 Jul 2024 15:03:00 +0200 Subject: [PATCH 1/2] Normed and fixed stuff --- ast/src/from_node/ast_free.c | 23 ++++-- stdme/src/fs/fs_internal.c | 136 +++++++++++++++++------------------ 2 files changed, 83 insertions(+), 76 deletions(-) diff --git a/ast/src/from_node/ast_free.c b/ast/src/from_node/ast_free.c index d88b4000..c72aeb4f 100644 --- a/ast/src/from_node/ast_free.c +++ b/ast/src/from_node/ast_free.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/30 14:29:42 by rparodi #+# #+# */ -/* Updated: 2024/07/30 14:52:31 by rparodi ### ########.fr */ +/* Updated: 2024/07/30 14:58:13 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,14 @@ #include "parser/api.h" #include + +void ast_free_arith(t_ast_node elem); +void ast_free_condition(t_ast_node elem); +void ast_free_exec(t_ast_node elem); +void ast_free_loop(t_ast_node elem); +void ast_free_other(t_ast_node elem); +void ast_free_redirection(t_ast_node elem); + void ast_free_arith(t_ast_node elem) { if (elem->kind == AST_ARITHMETIC_EXPANSION) @@ -86,13 +94,8 @@ void ast_free_redirection(t_ast_node elem) } } -void ast_free(t_ast_node elem) +void ast_free_other(t_ast_node elem) { - ast_free_arith(elem); - ast_free_loop(elem); - ast_free_condition(elem); - ast_free_exec(elem); - ast_free_redirection(elem); if (elem->kind == AST_COMPOUND_STATEMENT) { vec_ast_free(elem->data.compound_statement.body); @@ -119,6 +122,12 @@ void ast_free(t_ast_node elem) { if (elem == NULL) return ; + ast_free_arith(elem); + ast_free_loop(elem); + ast_free_condition(elem); + ast_free_exec(elem); + ast_free_other(elem); + ast_free_redirection(elem); if (elem->kind == AST_RAW_STRING) mem_free(elem->data.raw_string.str); if (elem->kind == AST_WORD) diff --git a/stdme/src/fs/fs_internal.c b/stdme/src/fs/fs_internal.c index dff2b60d..10adf6fb 100644 --- a/stdme/src/fs/fs_internal.c +++ b/stdme/src/fs/fs_internal.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/19 15:53:50 by maiboyer #+# #+# */ -/* Updated: 2024/07/07 19:17:05 by maiboyer ### ########.fr */ +/* Updated: 2024/07/30 15:01:04 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,23 +15,22 @@ #include "me/str/str.h" #include "me/types.h" #include "unistd.h" - #include #include #include #include -t_fd_array *get_fd_arrays(void) +t_fd_array *get_fd_arrays(void) { - static t_fd_array val = {}; + static t_fd_array val = {}; return (&val); } -struct s_file_slot *get_unused_fd_slot(void) +struct s_file_slot *get_unused_fd_slot(void) { t_usize i; - t_fd_array *arr; + t_fd_array *arr; arr = get_fd_arrays(); i = 0; @@ -41,15 +40,15 @@ struct s_file_slot *get_unused_fd_slot(void) return (&arr->storage[i]); i++; } - me_abort( - "Unable to find slot for a file descriptor, increate FILE_SLOT_LEN"); + me_abort("Unable to find slot for a file descriptor, " \ + "increate FILE_SLOT_LEN"); return (NULL); } -__attribute__((destructor(201))) void close_all_slots(void) +__attribute__((destructor(201))) void close_all_slots(void) { t_usize i; - t_fd_array *arr; + t_fd_array *arr; arr = get_fd_arrays(); i = 0; @@ -57,10 +56,10 @@ __attribute__((destructor(201))) void close_all_slots(void) close_slot(&arr->storage[i++]); } -void close_slot(struct s_file_slot *slot) +void close_slot(struct s_file_slot *slot) { if (slot == NULL) - return; + return ; if (slot->ty == SLOT_UNUSED) ; else if (slot->ty == SLOT_FD) @@ -82,11 +81,11 @@ void close_slot(struct s_file_slot *slot) |_| |_____/ */ -t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options, - t_file_perm file_perm) +t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options, + t_file_perm file_perm) { - t_fd *fd; - struct s_file_slot *slot; + t_fd *fd; + struct s_file_slot *slot; int actual_perms; if (perms & FD_READ && perms & FD_WRITE) @@ -110,15 +109,14 @@ t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options, return (fd); } -t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count) +t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count) { - t_isize ret; - t_isize false_ret; + t_isize ret; + t_isize false_ret; if (read_count == NULL) read_count = &false_ret; - if (fd == NULL || buffer == NULL || fd->fd == -1 || - !(fd->perms & FD_READ)) + if (fd == NULL || buffer == NULL || fd->fd == -1 || !(fd->perms & FD_READ)) return (ERROR); ret = read(fd->fd, buffer, size); if (ret == -1) @@ -127,10 +125,10 @@ t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count) return (NO_ERROR); } -t_error write_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *write_count) +t_error write_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *write_count) { - t_isize ret; - t_isize fake_ret; + t_isize ret; + t_isize fake_ret; if (write_count == NULL) write_count = &fake_ret; @@ -143,7 +141,7 @@ t_error write_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *write_count) return (NO_ERROR); } -t_error stat_fd(t_fd *fd, t_stat *stat) +t_error stat_fd(t_fd *fd, t_stat *stat) { if (fd == NULL || stat == NULL || fd->fd == -1) return (ERROR); @@ -152,24 +150,24 @@ t_error stat_fd(t_fd *fd, t_stat *stat) return (NO_ERROR); } -void close_fd(t_fd *fd) +void close_fd(t_fd *fd) { - struct s_file_slot *slot; + struct s_file_slot *slot; if (fd == NULL) - return; + return ; if (close(fd->fd) == -1) - return; + return ; slot = (void *)(fd)-offsetof(struct s_file_slot, slot.fd); mem_set_zero(slot, sizeof(*slot)); } #define INLINE_BUFFER_SIZE 128 -void put_number_fd(t_fd *fd, t_u64 number) +void put_number_fd(t_fd *fd, t_u64 number) { t_u8 buffer[INLINE_BUFFER_SIZE]; - t_usize i; + t_usize i; i = 0; while (number > 0) @@ -181,12 +179,12 @@ void put_number_fd(t_fd *fd, t_u64 number) write_fd(fd, &buffer[--i], 1, NULL); } -void put_string_fd(t_fd *fd, t_const_str string) +void put_string_fd(t_fd *fd, t_const_str string) { write_fd(fd, (t_u8 *)string, str_len(string), NULL); } -void put_char_fd(t_fd *fd, t_u8 c) +void put_char_fd(t_fd *fd, t_u8 c) { write_fd(fd, &c, 1, NULL); } @@ -199,10 +197,10 @@ void put_char_fd(t_fd *fd, t_u8 c) |_____/_____|_| \_\______\_____| |_| \____/|_| \_\ |_| */ -t_error open_dir(t_str name, t_dir **dir) +t_error open_dir(t_str name, t_dir **dir) { - t_dir *out; - struct s_file_slot *slot; + t_dir *out; + struct s_file_slot *slot; slot = get_unused_fd_slot(); out = &slot->slot.dir; @@ -215,9 +213,9 @@ t_error open_dir(t_str name, t_dir **dir) return (NO_ERROR); } -t_error read_dir(t_dir *dir, t_dir_entry *out) +t_error read_dir(t_dir *dir, t_dir_entry *out) { - struct dirent *entry; + struct dirent *entry; if (dir == NULL || out == NULL || dir->ptr == NULL) return (ERROR); @@ -229,14 +227,14 @@ t_error read_dir(t_dir *dir, t_dir_entry *out) return (NO_ERROR); } -void close_dir(t_dir *dir) +void close_dir(t_dir *dir) { - struct s_file_slot *slot; + struct s_file_slot *slot; if (dir == NULL) - return; + return ; if (closedir(dir->ptr) == -1) - return; + return ; slot = (void *)(dir)-offsetof(struct s_file_slot, slot.dir); mem_set_zero(slot, sizeof(*slot)); } @@ -249,10 +247,10 @@ void close_dir(t_dir *dir) |_| |_____|______|______| */ -t_error open_file(t_str name, t_mode mode, t_file **file) +t_error open_file(t_str name, t_mode mode, t_file **file) { - t_file *out; - struct s_file_slot *slot; + t_file *out; + struct s_file_slot *slot; slot = get_unused_fd_slot(); out = &slot->slot.file; @@ -265,11 +263,11 @@ t_error open_file(t_str name, t_mode mode, t_file **file) return (NO_ERROR); } -t_error write_file(t_file *file, t_u8 *buffer, t_usize size, - t_isize *write_count) +t_error write_file(t_file *file, t_u8 *buffer, t_usize size, + t_isize *write_count) { - t_isize ret; - t_isize fake_ret; + t_isize ret; + t_isize fake_ret; if (write_count == NULL) write_count = &fake_ret; @@ -282,12 +280,12 @@ t_error write_file(t_file *file, t_u8 *buffer, t_usize size, return (NO_ERROR); } -t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count) +t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count) { - t_isize ret; + t_isize ret; - if (file == NULL || buffer == NULL || read_count == NULL || - file->ptr == NULL) + if (file == NULL || buffer == NULL || read_count == NULL + || file->ptr == NULL) return (ERROR); ret = fread(buffer, size, 1, file->ptr); if (ret == -1) @@ -296,14 +294,14 @@ t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count) return (NO_ERROR); } -void close_file(t_file *file) +void close_file(t_file *file) { - struct s_file_slot *slot; + struct s_file_slot *slot; if (file == NULL) - return; + return ; if (fclose(file->ptr) == -1) - return; + return ; slot = (void *)(file)-offsetof(struct s_file_slot, slot.file); mem_set_zero(slot, sizeof(*slot)); } @@ -316,11 +314,11 @@ void close_file(t_file *file) \_____|______| |_| |_| |______|_| \_\_____/ */ -t_fd *get_stdin(void) +t_fd *get_stdin(void) { - t_fd *out; - struct s_file_slot *slot; - static t_fd *value = NULL; + t_fd *out; + struct s_file_slot *slot; + static t_fd *value = NULL; if (value == NULL) { @@ -335,11 +333,11 @@ t_fd *get_stdin(void) return (value); } -t_fd *get_stdout(void) +t_fd *get_stdout(void) { - t_fd *out; - struct s_file_slot *slot; - static t_fd *value = NULL; + t_fd *out; + struct s_file_slot *slot; + static t_fd *value = NULL; if (value == NULL) { @@ -354,11 +352,11 @@ t_fd *get_stdout(void) return (value); } -t_fd *get_stderr(void) +t_fd *get_stderr(void) { - t_fd *out; - struct s_file_slot *slot; - static t_fd *value = NULL; + t_fd *out; + struct s_file_slot *slot; + static t_fd *value = NULL; if (value == NULL) { From aa4b508b1098056194f15d94cb24e3efee2d4438 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Tue, 30 Jul 2024 15:58:07 +0200 Subject: [PATCH 2/2] Added redirection vector and type --- Filelist.sh.mk | 3 + ast/Filelist.ast.mk | 3 + exec/include/exec/spawn_cmd/_redirection.h | 6 +- input.toml | 8 ++ output/include/me/vec/vec_redir.h | 123 +++++++++++++++++++++ output/src/vec/redir/redir.c | 95 ++++++++++++++++ output/src/vec/redir/redir_functions2.c | 112 +++++++++++++++++++ output/src/vec/redir/redir_functions3.c | 84 ++++++++++++++ 8 files changed, 431 insertions(+), 3 deletions(-) create mode 100644 output/include/me/vec/vec_redir.h create mode 100644 output/src/vec/redir/redir.c create mode 100644 output/src/vec/redir/redir_functions2.c create mode 100644 output/src/vec/redir/redir_functions3.c diff --git a/Filelist.sh.mk b/Filelist.sh.mk index 1056a031..376e0564 100644 --- a/Filelist.sh.mk +++ b/Filelist.sh.mk @@ -21,6 +21,9 @@ src/vec/ast/ast_functions3 \ src/vec/estr/estr \ src/vec/estr/estr_functions2 \ src/vec/estr/estr_functions3 \ +src/vec/redir/redir \ +src/vec/redir/redir_functions2 \ +src/vec/redir/redir_functions3 \ src/vec/str/str \ src/vec/str/str_functions2 \ src/vec/str/str_functions3 \ diff --git a/ast/Filelist.ast.mk b/ast/Filelist.ast.mk index 579d4b85..c28e3edd 100644 --- a/ast/Filelist.ast.mk +++ b/ast/Filelist.ast.mk @@ -1,5 +1,8 @@ SRC_FILES = \ from_node \ +from_node/ast_free \ +from_node/ast_free_scripting \ +from_node/from_node \ not_done_function \ not_done_print \ print_ast/ast_print_command \ diff --git a/exec/include/exec/spawn_cmd/_redirection.h b/exec/include/exec/spawn_cmd/_redirection.h index 442d78a5..6a7f0f87 100644 --- a/exec/include/exec/spawn_cmd/_redirection.h +++ b/exec/include/exec/spawn_cmd/_redirection.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/29 17:24:19 by maiboyer #+# #+# */ -/* Updated: 2024/07/30 14:30:45 by maiboyer ### ########.fr */ +/* Updated: 2024/07/30 15:56:33 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,9 +54,9 @@ static inline void free_redirection(t_exec_redirect self) void mem_free(void *ptr); if (self.kind == EXEC_REDIR_FD) - (void)(self.fd.file_path != NULL && (mem_free(self.fd.file_path), 1)); + (void)(self.fd.file_path != NULL && (mem_free((void *)self.fd.file_path), 1)); if (self.kind == EXEC_REDIR_HEREDOC) - (void)(self.heredoc.data != NULL && (mem_free(self.heredoc.data), 1)); + (void)(self.heredoc.data != NULL && (mem_free((void *)self.heredoc.data), 1)); } #endif /* _REDIRECTION_H */ diff --git a/input.toml b/input.toml index 92139bf6..a091dc5b 100644 --- a/input.toml +++ b/input.toml @@ -71,3 +71,11 @@ replace.C__TYPENAME__ = "t_expandable_str" replace.C__TYPEHEADER__ = '#include "exec/_tuple_expanded_str.h"' replace.C__PREFIX__ = "estr" replace.C__PREFIXUP__ = "ESTR" + +[[create.vec]] +sources_output = "src/vec/C__PREFIX__/" +headers_output = "include/me/vec/" +replace.C__TYPENAME__ = "t_exec_redirect" +replace.C__TYPEHEADER__ = '#include "exec/spawn_cmd/_redirection.h"' +replace.C__PREFIX__ = "redir" +replace.C__PREFIXUP__ = "REDIR" diff --git a/output/include/me/vec/vec_redir.h b/output/include/me/vec/vec_redir.h new file mode 100644 index 00000000..2458d269 --- /dev/null +++ b/output/include/me/vec/vec_redir.h @@ -0,0 +1,123 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_redir.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_REDIR_H +#define VEC_REDIR_H + +#include "exec/spawn_cmd/_redirection.h" +#include "me/types.h" + +/// @brief A function that takes two t_exec_redirect and compare them +typedef bool (*t_vec_redir_sort_fn)(t_exec_redirect *, t_exec_redirect *); +/// @brief A function that free an t_exec_redirect +typedef void (*t_free_redir_item)(t_exec_redirect); + +/// @brief A dynamic array of t_exec_redirect +typedef struct s_vec_redir +{ + t_free_redir_item free_func; + t_usize len; + t_usize capacity; + t_exec_redirect *buffer; +} t_vec_redir; + +/// @brief Create a new vec_redir with a given capacity +/// @param capacity The capacity of the new vec_redir (in terms of elements) +/// @param free_function The function that will be used to free the elements of the vec_redir +t_vec_redir vec_redir_new(t_usize capacity, t_free_redir_item free_function); +/// @brief Push an element to the last position of the vec_redir +/// @param vec The vec_redir to push the element to +/// @param element The element to push +t_error vec_redir_push(t_vec_redir *vec, t_exec_redirect element); + +/// @brief Push an element to the first position of the vec_redir +/// @param vec The vec_redir to push the element to +/// @param element The element to push +/// @note This operation is O(n) +t_error vec_redir_push_front(t_vec_redir *vec, t_exec_redirect element); + +/// @brief Get the last element from the vec_redir, and remove it from the vec_redir +/// @param vec The vec_redir to get the element from +/// @param[out] out The last element of the vec_redir +/// @return true if the operation failed, false otherwise +t_error vec_redir_pop(t_vec_redir *vec, t_exec_redirect *value); + + +/// @brief Get the first element from the vec_redir, and remove it from the vec_redir +/// @param vec The vec_redir to get the element from +/// @param[out] out The first element of the vec_redir +/// @return true if the operation failed, false otherwise +/// @note This operation is O(n) +t_error vec_redir_pop_front(t_vec_redir *vec, t_exec_redirect *value); + +/// @brief Free the vector and all its elements +/// @param vec The vec_redir to free +void vec_redir_free(t_vec_redir vec); + +/// @brief Make the vec_redir at least the given capacity +/// @param vec The vec_redir to reserve +/// @param wanted_capacity The minimum capacity to reserve +/// @return true if the operation failed, false otherwise +t_error vec_redir_reserve(t_vec_redir *vec, t_usize wanted_capacity); + +/// @brief Run the function and returns the index of the first element that returns true +/// @param vec The vec_redir to search in +/// @param fn The function to run on each element +/// @param[out] index The index of the first element that returns true +t_error vec_redir_find(t_vec_redir *vec, bool (*fn)(const t_exec_redirect *), t_usize *index); + + +/// @brief Run the function and returns the index of the first element that returns true, but starting at index starting_index +/// @param vec The vec_redir to search in +/// @param fn The function to run on each element +/// @param starting_index The index to start the search from +/// @param[out] index The index of the first element that returns true +t_error vec_redir_find_starting(t_vec_redir *vec, bool (*fn)(const t_exec_redirect *), t_usize starting_index, t_usize *index); + +/// @brief Run the function on every element of the vec_redir and returns if all elements returned true +/// @param vec The vec_redir to search in +/// @param fn The function to run on each element +/// @param[out] result The result of the operation +/// @return true if the operation failed, false otherwise +/// @note If the vec_redir is empty, result will be true +t_error vec_redir_all(t_vec_redir *vec, bool (*fn)(const t_exec_redirect *), bool *result); + +/// @brief Run the function on every element of the vec_redir and returns if any element returned true +/// @param vec The vec_redir to search in +/// @param fn The function to run on each element +/// @param[out] result The result of the operation +/// @return true if the operation failed, false otherwise +/// @note If the vec_redir is empty, result will be false +t_error vec_redir_any(t_vec_redir *vec, bool (*fn)(const t_exec_redirect *), bool *result); + +/// @brief Run the function on every element of the vec_redir +/// @param vec The vec_redir to iterate over +/// @param fn The function to run on each element +/// @param state The state to pass to the function +void vec_redir_iter(t_vec_redir *vec, void (*fn)(t_usize index, t_exec_redirect *value, void *state), void *state); + +/// @brief Reverse the order of the elements in the vec_redir +/// @param vec The vec_redir to reverse +void vec_redir_reverse(t_vec_redir *vec); + +/// @brief Sort the elements of the vec_redir +/// @param vec The vec_redir to sort +/// @param is_sorted The function to use to compare the elements +void vec_redir_sort(t_vec_redir *vec, t_vec_redir_sort_fn is_sorted); + +/// @brief Get a pointer to the last element of the vec_redir +/// @param vec The vec_redir to get the element from +/// @param[out] out A pointer to the last element of the vec_redir +/// @return true if the operation failed, false otherwise +t_error vec_redir_back(t_vec_redir *vec, t_exec_redirect **out); + +#endif diff --git a/output/src/vec/redir/redir.c b/output/src/vec/redir/redir.c new file mode 100644 index 00000000..adae6259 --- /dev/null +++ b/output/src/vec/redir/redir.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_redir.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.h" +#include "me/types.h" +#include "me/vec/vec_redir.h" +#include + +t_vec_redir vec_redir_new(t_usize capacity, + t_free_redir_item free_function) +{ + t_vec_redir out; + + out = (t_vec_redir){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_exec_redirect)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_redir_push(t_vec_redir *vec, t_exec_redirect element) +{ + if (vec == NULL) + return (ERROR); + vec_redir_reserve(vec, vec->len + 1); + vec->buffer[vec->len] = element; + vec->len += 1; + return (NO_ERROR); +} + +/// Return true in case of an error +t_error vec_redir_reserve(t_vec_redir *vec, t_usize wanted_capacity) +{ + 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; + vec->buffer = + mem_realloc_array(vec->buffer, new_capacity, sizeof(t_exec_redirect)); + 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_redir_pop(t_vec_redir *vec, t_exec_redirect *value) +{ + t_exec_redirect temp_value; + t_exec_redirect *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_exec_redirect)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_redir_free(t_vec_redir vec) +{ + if (vec.buffer == NULL) + return; + if (vec.free_func) + { + while (vec.len) + { + vec.free_func(vec.buffer[vec.len - 1]); + vec.len--; + } + } + mem_free(vec.buffer); +} diff --git a/output/src/vec/redir/redir_functions2.c b/output/src/vec/redir/redir_functions2.c new file mode 100644 index 00000000..f67ba5fd --- /dev/null +++ b/output/src/vec/redir/redir_functions2.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_redir.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.h" +#include "me/mem/mem.h" +#include "me/mem/mem.h" +#include "me/types.h" +#include "me/vec/vec_redir.h" +#include + +t_error vec_redir_find(t_vec_redir *vec, + bool (*fn)(const t_exec_redirect *), t_usize *index) +{ + t_usize idx; + + if (vec == NULL || fn == NULL || index == NULL) + return (ERROR); + idx = 0; + while (idx < vec->len) + { + if (fn((const t_exec_redirect *)&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_redir_find_starting(t_vec_redir *vec, + bool (*fn)(const t_exec_redirect *), + 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((const t_exec_redirect *)&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_redir_all(t_vec_redir *vec, + bool (*fn)(const t_exec_redirect *), 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((const t_exec_redirect *)&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_redir_any(t_vec_redir *vec, + bool (*fn)(const t_exec_redirect *), 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((const t_exec_redirect *)&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_redir_iter(t_vec_redir *vec, + void (*fn)(t_usize index, t_exec_redirect *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/redir/redir_functions3.c b/output/src/vec/redir/redir_functions3.c new file mode 100644 index 00000000..f68a773b --- /dev/null +++ b/output/src/vec/redir/redir_functions3.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_redir.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.h" +#include "me/mem/mem.h" +#include "me/mem/mem.h" +#include "me/types.h" +#include "me/vec/vec_redir.h" +#include + +t_error vec_redir_push_front(t_vec_redir *vec, + t_exec_redirect element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_redir_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && + vec_redir_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_redir_pop_front(t_vec_redir *vec, t_exec_redirect *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_redir_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_redir_reverse(t_vec_redir *vec) +{ + t_exec_redirect 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_redir_back(t_vec_redir *vec, t_exec_redirect **out) +{ + t_exec_redirect *temporary; + + if (out == NULL) + out = &temporary; + if (vec->len != 0) + return (*out = &vec->buffer[vec->len - 1], true); + return (false); +}