Added redirection vector and type

This commit is contained in:
Maieul BOYER 2024-07-30 15:58:07 +02:00
parent 970507dfdd
commit aa4b508b10
No known key found for this signature in database
8 changed files with 431 additions and 3 deletions

View file

@ -21,6 +21,9 @@ src/vec/ast/ast_functions3 \
src/vec/estr/estr \ src/vec/estr/estr \
src/vec/estr/estr_functions2 \ src/vec/estr/estr_functions2 \
src/vec/estr/estr_functions3 \ 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 \
src/vec/str/str_functions2 \ src/vec/str/str_functions2 \
src/vec/str/str_functions3 \ src/vec/str/str_functions3 \

View file

@ -1,5 +1,8 @@
SRC_FILES = \ SRC_FILES = \
from_node \ from_node \
from_node/ast_free \
from_node/ast_free_scripting \
from_node/from_node \
not_done_function \ not_done_function \
not_done_print \ not_done_print \
print_ast/ast_print_command \ print_ast/ast_print_command \

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/29 17:24:19 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); void mem_free(void *ptr);
if (self.kind == EXEC_REDIR_FD) 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) 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 */ #endif /* _REDIRECTION_H */

View file

@ -71,3 +71,11 @@ replace.C__TYPENAME__ = "t_expandable_str"
replace.C__TYPEHEADER__ = '#include "exec/_tuple_expanded_str.h"' replace.C__TYPEHEADER__ = '#include "exec/_tuple_expanded_str.h"'
replace.C__PREFIX__ = "estr" replace.C__PREFIX__ = "estr"
replace.C__PREFIXUP__ = "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"

View file

@ -0,0 +1,123 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_redir.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_redir.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
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);
}

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_redir.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
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++;
}
}

View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_redir.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
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);
}