From aac10822d304e202970b7d2c7f647dd26d396709 Mon Sep 17 00:00:00 2001 From: Maix0 Date: Tue, 13 Aug 2024 16:01:45 +0200 Subject: [PATCH] Fixed waiting on previous command for pipeline to spawn next --- Filelist.sh.mk | 3 + exec/src/builtins/cd.c | 10 +- exec/src/builtins/pwd.c | 8 +- exec/src/run_ast.c | 45 +++++--- flake.lock | 173 +++++++++++++++++++++++++++- flake.nix | 42 ++++--- input.toml | 8 ++ output/include/me/vec/vec_pid.h | 123 ++++++++++++++++++++ output/src/hashmap/env/env_utils.c | 4 +- output/src/vec/pid/pid.c | 95 +++++++++++++++ output/src/vec/pid/pid_functions2.c | 112 ++++++++++++++++++ output/src/vec/pid/pid_functions3.c | 84 ++++++++++++++ output/src/vec/str/str_functions2.c | 24 ++-- 13 files changed, 679 insertions(+), 52 deletions(-) create mode 100644 output/include/me/vec/vec_pid.h create mode 100644 output/src/vec/pid/pid.c create mode 100644 output/src/vec/pid/pid_functions2.c create mode 100644 output/src/vec/pid/pid_functions3.c diff --git a/Filelist.sh.mk b/Filelist.sh.mk index 47da9e72..8ff03a5a 100644 --- a/Filelist.sh.mk +++ b/Filelist.sh.mk @@ -17,6 +17,9 @@ src/vec/ast/ast_functions3 \ src/vec/estr/estr \ src/vec/estr/estr_functions2 \ src/vec/estr/estr_functions3 \ +src/vec/pid/pid \ +src/vec/pid/pid_functions2 \ +src/vec/pid/pid_functions3 \ src/vec/str/str \ src/vec/str/str_functions2 \ src/vec/str/str_functions3 \ diff --git a/exec/src/builtins/cd.c b/exec/src/builtins/cd.c index c8811ef9..15dc14f7 100644 --- a/exec/src/builtins/cd.c +++ b/exec/src/builtins/cd.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */ -/* Updated: 2024/08/12 17:25:25 by maiboyer ### ########.fr */ +/* Updated: 2024/08/13 15:13:11 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ t_error builtin_cd____(t_state *state, t_builtin_spawn_info info, t_i32 *exit_co { const t_str key = "HOME"; t_str *home; + t_str path; if (info.args.len <= 1) { @@ -30,10 +31,11 @@ t_error builtin_cd____(t_state *state, t_builtin_spawn_info info, t_i32 *exit_co home = hmap_env_get(state->env, (t_str *)&key); if (home == NULL || *home == NULL) return (*exit_code = 0, NO_ERROR); - if (chdir(*home) == -1) - return (*exit_code = 2, me_printf_fd(info.stderr, "cd: Unable to change directory\n"), NO_ERROR); + path = *home; } - else if (chdir(info.args.buffer[1]) == -1) + else + path = info.args.buffer[1]; + if (chdir(path) == -1) return (*exit_code = 2, me_printf_fd(info.stderr, "cd: Unable to change directory\n"), NO_ERROR); return (*exit_code = 0, NO_ERROR); } diff --git a/exec/src/builtins/pwd.c b/exec/src/builtins/pwd.c index 26f2f611..3cf1d9cf 100644 --- a/exec/src/builtins/pwd.c +++ b/exec/src/builtins/pwd.c @@ -6,12 +6,12 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/07 13:58:37 by rparodi #+# #+# */ -/* Updated: 2024/08/12 18:14:08 by rparodi ### ########.fr */ +/* Updated: 2024/08/13 15:14:47 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ #include "exec/builtins.h" -#include "me/mem/mem.h" +#include "me/printf/printf.h" #include "me/string/string.h" #include "me/types.h" #include @@ -26,9 +26,9 @@ t_error builtin_pwd___(t_state *state, t_builtin_spawn_info info, t_i32 *exit_co if (errno == ERANGE) string_reserve(&s, s.capacity * 3); else - return (string_free(s), ERROR); + return (*exit_code = 1, string_free(s), me_printf_fd(info.stderr, "cd: Unable to get current directory\n"), NO_ERROR); } - printf("%s\n", s.buf); + me_printf_fd(info.stdout, "%s\n", s.buf); string_free(s); return (*exit_code = 0, NO_ERROR); } diff --git a/exec/src/run_ast.c b/exec/src/run_ast.c index 35fd2057..d187febf 100644 --- a/exec/src/run_ast.c +++ b/exec/src/run_ast.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */ -/* Updated: 2024/08/12 16:57:47 by maiboyer ### ########.fr */ +/* Updated: 2024/08/13 15:59:30 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ #include "me/types.h" #include "me/vec/vec_ast.h" #include "me/vec/vec_estr.h" +#include "me/vec/vec_pid.h" #include "me/vec/vec_str.h" #include #include @@ -87,10 +88,7 @@ t_error _get_expansion_value(t_ast_expansion *self, t_state *state, t_expansion_ hmap_ret = hmap_env_get(state->env, &self->var_name); ret = (t_expansion_result){.exists = (hmap_ret != NULL), .value = NULL}; if (ret.exists) - { ret.value = *hmap_ret; - printf("source = %p; ret = %p\n", *hmap_ret, ret.value); - } return (*out = ret, NO_ERROR); } @@ -269,7 +267,9 @@ t_str _get_ifs_value(t_state *state) ifs_key = "IFS"; ifs = NULL; - ifs_entry = hmap_env_get(state->env, (t_str *)&ifs_key); + ifs_entry = hmap_env_get(state->tmp_var, (t_str *)&ifs_key); + if (ifs_entry != NULL) + ifs_entry = hmap_env_get(state->env, (t_str *)&ifs_key); if (ifs_entry != NULL) ifs = *ifs_entry; if (ifs == NULL) @@ -536,6 +536,8 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, t_pipeline_result t_error ret; t_command_result cmd_result; t_ast_node tmp_ast; + t_vec_pid pids; + int waitpid_status; if (pipeline == NULL || state == NULL || out == NULL) return (ERROR); @@ -543,6 +545,8 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, t_pipeline_result ret = NO_ERROR; cmd_pipe.input = NULL; cmd_pipe.create_output = true; + out->exit = 127; + pids = vec_pid_new(32, NULL); while (i < pipeline->statements.len - 1) { child = pipeline->statements.buffer[i]; @@ -555,7 +559,7 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, t_pipeline_result } else { - out->exit = cmd_result.exit; + vec_pid_push(&pids, cmd_result.process.pid); close_fd(cmd_pipe.input); if (cmd_result.process.stdout != NULL) cmd_pipe.input = cmd_result.process.stdout; @@ -580,7 +584,7 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, t_pipeline_result } else { - out->exit = cmd_result.exit; + vec_pid_push(&pids, cmd_result.process.pid); close_fd(cmd_pipe.input); if (cmd_result.process.stdout != NULL) close_fd(cmd_result.process.stdout); @@ -591,7 +595,20 @@ t_error run_pipeline(t_ast_pipeline *pipeline, t_state *state, t_pipeline_result } } } - + if (pids.len != 0) + { + while (waitpid(pids.buffer[pids.len - 1], &waitpid_status, 0) < 0) + ; + while (waitpid(-1, NULL, 0) != -1) + ; + if (WIFEXITED(waitpid_status)) + out->exit = WEXITSTATUS(waitpid_status); + if (WIFSIGNALED(waitpid_status)) + out->exit = WTERMSIG(waitpid_status); + } + else + out->exit = 127; + vec_pid_free(pids); return (ret); } @@ -748,15 +765,7 @@ t_error _handle_builtin(t_spawn_info info, t_state *state, t_cmd_pipe cmd_pipe, if (out->process.pid == -1) out->exit = 126; else - { - int status; - if (waitpid(out->process.pid, &status, 0) == -1) - return (ERROR); - if (WIFEXITED(status)) - out->exit = WEXITSTATUS(status); - if (WIFSIGNALED(status)) - out->exit = WTERMSIG(status); - } + out->exit = -1; } else { @@ -889,6 +898,8 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection, t_state *state signal(SIGQUIT, SIG_IGN); if (spawn_process(info, &out->process)) return (close_fd(cmd_pipe.input), out->exit = 127, ERROR); + if (cmd_pipe.create_output || cmd_pipe.input != NULL) + return (out->exit = -1, NO_ERROR); int status; if (waitpid(out->process.pid, &status, 0) == -1) return (ERROR); diff --git a/flake.lock b/flake.lock index 2804b574..c969b891 100644 --- a/flake.lock +++ b/flake.lock @@ -18,7 +18,128 @@ "type": "github" } }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1705309234, + "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_3": { + "inputs": { + "systems": "systems_3" + }, + "locked": { + "lastModified": 1705309234, + "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "generic_c": { + "inputs": { + "flake-utils": "flake-utils_2", + "naersk": "naersk", + "nixpkgs": "nixpkgs_2", + "rust-overlay": "rust-overlay" + }, + "locked": { + "lastModified": 1706829609, + "narHash": "sha256-A0imQ9AIJafdL1/+j/1b3G7bm2j+N+VhzTsvKikKjz4=", + "owner": "Maix0", + "repo": "generic_c", + "rev": "a470c2c5a8c8aadc852a7a50d72853f2a3873595", + "type": "github" + }, + "original": { + "owner": "Maix0", + "repo": "generic_c", + "type": "github" + } + }, + "naersk": { + "inputs": { + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1698420672, + "narHash": "sha256-/TdeHMPRjjdJub7p7+w55vyABrsJlt5QkznPYy55vKA=", + "owner": "nix-community", + "repo": "naersk", + "rev": "aeb58d5e8faead8980a807c840232697982d47b9", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "naersk", + "type": "github" + } + }, "nixpkgs": { + "locked": { + "lastModified": 1706683685, + "narHash": "sha256-FtPPshEpxH/ewBOsdKBNhlsL2MLEFv1hEnQ19f/bFsQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5ad9903c16126a7d949101687af0aa589b1d7d3d", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1706683685, + "narHash": "sha256-FtPPshEpxH/ewBOsdKBNhlsL2MLEFv1hEnQ19f/bFsQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "5ad9903c16126a7d949101687af0aa589b1d7d3d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1706487304, + "narHash": "sha256-LE8lVX28MV2jWJsidW13D2qrHU/RUUONendL2Q/WlJg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "90f456026d284c22b3e3497be980b2e47d0b28ac", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_4": { "locked": { "lastModified": 1723047874, "narHash": "sha256-i6shgus4+OUD8vg0TZ1qhDvKBfvaWi+SK1xbBSY0z+c=", @@ -36,7 +157,27 @@ "root": { "inputs": { "flake-utils": "flake-utils", - "nixpkgs": "nixpkgs" + "generic_c": "generic_c", + "nixpkgs": "nixpkgs_4" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils_3", + "nixpkgs": "nixpkgs_3" + }, + "locked": { + "lastModified": 1706753617, + "narHash": "sha256-ZKqTFzhFwSWFEpQTJ0uXnfJBs5Y/po9/8TK4bzssdbs=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "58be43ae223034217ea1bd58c73210644031b687", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" } }, "systems": { @@ -53,6 +194,36 @@ "repo": "default", "type": "github" } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_3": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 30c3bd37..00f0704b 100644 --- a/flake.nix +++ b/flake.nix @@ -1,9 +1,9 @@ { description = "Flake utils demo"; - + inputs.nixpkgs.url = "github:nixos/nixpkgs"; inputs.flake-utils.url = "github:numtide/flake-utils"; - # inputs.generic_c.url = "github:Maix0/generic_c"; + inputs.generic_c.url = "github:Maix0/generic_c"; # inputs.c_formatter_42.url = "github:Maix0/c_formatter_42-flake"; # inputs.rust-overlay.url = "github:oxalica/rust-overlay"; @@ -11,14 +11,15 @@ self, nixpkgs, flake-utils, - # generic_c, + generic_c, # c_formatter_42, # rust-overlay, }: flake-utils.lib.eachDefaultSystem ( system: let pkgs = nixpkgs.legacyPackages.${system}; - /*import nixpkgs { + /* + import nixpkgs { inherit system; overlays = [ (import rust-overlay) @@ -29,21 +30,28 @@ }; }) ]; - };*/ + }; + */ in { devShell = pkgs.mkShell { - packages = with pkgs; [ - # clang-analyzer - clang - gnumake - # generic_c.packages.${system}.default - # c_formatter_42.packages.${system}.default - # llvmPackages.bintools - # norminette - # tokei - # coreutils - #]; - ] ++ (if system == "x86_64-linux" then [valgrind valgrind.dev] else []); + packages = with pkgs; + [ + clang-analyzer + clang + gnumake + generic_c.packages.${system}.default + # c_formatter_42.packages.${system}.default + llvmPackages.bintools + # norminette + # tokei + # coreutils + #]; + ] + ++ ( + if system == "x86_64-linux" + then [valgrind valgrind.dev] + else [] + ); #ASAN_OPTIONS = "strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1"; }; } diff --git a/input.toml b/input.toml index 7fce45eb..4352cc2b 100644 --- a/input.toml +++ b/input.toml @@ -72,3 +72,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_pid" +replace.C__TYPEHEADER__ = '#include "me/os/os.h"' +replace.C__PREFIX__ = "pid" +replace.C__PREFIXUP__ = "PID" diff --git a/output/include/me/vec/vec_pid.h b/output/include/me/vec/vec_pid.h new file mode 100644 index 00000000..647f66e0 --- /dev/null +++ b/output/include/me/vec/vec_pid.h @@ -0,0 +1,123 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_pid.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/04 18:46:53 by maiboyer #+# #+# */ +/* Updated: 2023/12/09 17:53:00 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef VEC_PID_H +#define VEC_PID_H + +#include "me/os/os.h" +#include "me/types.h" + +/// @brief A function that takes two t_pid and compare them +typedef bool (*t_vec_pid_sort_fn)(t_pid *, t_pid *); +/// @brief A function that free an t_pid +typedef void (*t_free_pid_item)(t_pid); + +/// @brief A dynamic array of t_pid +typedef struct s_vec_pid +{ + t_free_pid_item free_func; + t_usize len; + t_usize capacity; + t_pid *buffer; +} t_vec_pid; + +/// @brief Create a new vec_pid with a given capacity +/// @param capacity The capacity of the new vec_pid (in terms of elements) +/// @param free_function The function that will be used to free the elements of the vec_pid +t_vec_pid vec_pid_new(t_usize capacity, t_free_pid_item free_function); +/// @brief Push an element to the last position of the vec_pid +/// @param vec The vec_pid to push the element to +/// @param element The element to push +t_error vec_pid_push(t_vec_pid *vec, t_pid element); + +/// @brief Push an element to the first position of the vec_pid +/// @param vec The vec_pid to push the element to +/// @param element The element to push +/// @note This operation is O(n) +t_error vec_pid_push_front(t_vec_pid *vec, t_pid element); + +/// @brief Get the last element from the vec_pid, and remove it from the vec_pid +/// @param vec The vec_pid to get the element from +/// @param[out] out The last element of the vec_pid +/// @return true if the operation failed, false otherwise +t_error vec_pid_pop(t_vec_pid *vec, t_pid *value); + + +/// @brief Get the first element from the vec_pid, and remove it from the vec_pid +/// @param vec The vec_pid to get the element from +/// @param[out] out The first element of the vec_pid +/// @return true if the operation failed, false otherwise +/// @note This operation is O(n) +t_error vec_pid_pop_front(t_vec_pid *vec, t_pid *value); + +/// @brief Free the vector and all its elements +/// @param vec The vec_pid to free +void vec_pid_free(t_vec_pid vec); + +/// @brief Make the vec_pid at least the given capacity +/// @param vec The vec_pid to reserve +/// @param wanted_capacity The minimum capacity to reserve +/// @return true if the operation failed, false otherwise +t_error vec_pid_reserve(t_vec_pid *vec, t_usize wanted_capacity); + +/// @brief Run the function and returns the index of the first element that returns true +/// @param vec The vec_pid 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_pid_find(t_vec_pid *vec, bool (*fn)(const t_pid *), 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_pid 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_pid_find_starting(t_vec_pid *vec, bool (*fn)(const t_pid *), t_usize starting_index, t_usize *index); + +/// @brief Run the function on every element of the vec_pid and returns if all elements returned true +/// @param vec The vec_pid 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_pid is empty, result will be true +t_error vec_pid_all(t_vec_pid *vec, bool (*fn)(const t_pid *), bool *result); + +/// @brief Run the function on every element of the vec_pid and returns if any element returned true +/// @param vec The vec_pid 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_pid is empty, result will be false +t_error vec_pid_any(t_vec_pid *vec, bool (*fn)(const t_pid *), bool *result); + +/// @brief Run the function on every element of the vec_pid +/// @param vec The vec_pid to iterate over +/// @param fn The function to run on each element +/// @param state The state to pass to the function +void vec_pid_iter(t_vec_pid *vec, void (*fn)(t_usize index, t_pid *value, void *state), void *state); + +/// @brief Reverse the order of the elements in the vec_pid +/// @param vec The vec_pid to reverse +void vec_pid_reverse(t_vec_pid *vec); + +/// @brief Sort the elements of the vec_pid +/// @param vec The vec_pid to sort +/// @param is_sorted The function to use to compare the elements +void vec_pid_sort(t_vec_pid *vec, t_vec_pid_sort_fn is_sorted); + +/// @brief Get a pointer to the last element of the vec_pid +/// @param vec The vec_pid to get the element from +/// @param[out] out A pointer to the last element of the vec_pid +/// @return true if the operation failed, false otherwise +t_error vec_pid_back(t_vec_pid *vec, t_pid **out); + +#endif diff --git a/output/src/hashmap/env/env_utils.c b/output/src/hashmap/env/env_utils.c index 48c5a30c..2d9f53fd 100644 --- a/output/src/hashmap/env/env_utils.c +++ b/output/src/hashmap/env/env_utils.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* env_utils.c :+: :+: :+: */ +/* hashmap_env_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/06 10:58:20 by maiboyer #+# #+# */ -/* Updated: 2024/08/12 17:14:03 by maiboyer ### ########.fr */ +/* Updated: 2023/12/11 15:35:37 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/output/src/vec/pid/pid.c b/output/src/vec/pid/pid.c new file mode 100644 index 00000000..da4c4bbb --- /dev/null +++ b/output/src/vec/pid/pid.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_pid.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_pid.h" +#include + +t_vec_pid vec_pid_new(t_usize capacity, + t_free_pid_item free_function) +{ + t_vec_pid out; + + out = (t_vec_pid){0}; + out.free_func = free_function; + out.buffer = mem_alloc_array(capacity, sizeof(t_pid)); + if (out.buffer) + out.capacity = capacity; + return (out); +} + +/// Return true in case of an error +t_error vec_pid_push(t_vec_pid *vec, t_pid element) +{ + if (vec == NULL) + return (ERROR); + vec_pid_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_pid_reserve(t_vec_pid *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_pid)); + 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_pid_pop(t_vec_pid *vec, t_pid *value) +{ + t_pid temp_value; + t_pid *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_pid)); + return (NO_ERROR); +} + +/// This function is safe to call with `free_elem` being NULL +void vec_pid_free(t_vec_pid 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/pid/pid_functions2.c b/output/src/vec/pid/pid_functions2.c new file mode 100644 index 00000000..e6a59d29 --- /dev/null +++ b/output/src/vec/pid/pid_functions2.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_pid.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_pid.h" +#include + +t_error vec_pid_find(t_vec_pid *vec, + bool (*fn)(const t_pid *), 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_pid *)&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_pid_find_starting(t_vec_pid *vec, + bool (*fn)(const t_pid *), + 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_pid *)&vec->buffer[idx])) + { + *index = idx; + return (NO_ERROR); + } + idx++; + } + return (ERROR); +} + +t_error vec_pid_all(t_vec_pid *vec, + bool (*fn)(const t_pid *), 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_pid *)&vec->buffer[idx])) + *result = false; + idx++; + } + return (ERROR); +} + +t_error vec_pid_any(t_vec_pid *vec, + bool (*fn)(const t_pid *), 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_pid *)&vec->buffer[idx])) + *result = true; + idx++; + } + return (ERROR); +} + +void vec_pid_iter(t_vec_pid *vec, + void (*fn)(t_usize index, t_pid *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/pid/pid_functions3.c b/output/src/vec/pid/pid_functions3.c new file mode 100644 index 00000000..a68f76c4 --- /dev/null +++ b/output/src/vec/pid/pid_functions3.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vec_pid.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_pid.h" +#include + +t_error vec_pid_push_front(t_vec_pid *vec, + t_pid element) +{ + t_usize i; + + if (vec->len == 0) + return (vec_pid_push(vec, element)); + i = vec->len - 1; + if (vec->capacity < vec->len + 1 && + vec_pid_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_pid_pop_front(t_vec_pid *vec, t_pid *value) +{ + t_usize i; + + if (vec->len <= 1) + return (vec_pid_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_pid_reverse(t_vec_pid *vec) +{ + t_pid 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_pid_back(t_vec_pid *vec, t_pid **out) +{ + t_pid *temporary; + + if (out == NULL) + out = &temporary; + if (vec->len != 0) + return (*out = &vec->buffer[vec->len - 1], true); + return (false); +} diff --git a/output/src/vec/str/str_functions2.c b/output/src/vec/str/str_functions2.c index 6f87845c..af61b8e1 100644 --- a/output/src/vec/str/str_functions2.c +++ b/output/src/vec/str/str_functions2.c @@ -1,21 +1,24 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* str_functions2.c :+: :+: :+: */ +/* vec_str.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/30 17:59:28 by maiboyer #+# #+# */ -/* Updated: 2024/08/03 16:23:05 by maiboyer ### ########.fr */ +/* 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_str.h" #include -t_error vec_str_find(t_vec_str *vec, bool (*fn)(const t_str *), t_usize *index) +t_error vec_str_find(t_vec_str *vec, + bool (*fn)(const t_str *), t_usize *index) { t_usize idx; @@ -34,7 +37,9 @@ t_error vec_str_find(t_vec_str *vec, bool (*fn)(const t_str *), t_usize *index) 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_error vec_str_find_starting(t_vec_str *vec, + bool (*fn)(const t_str *), + t_usize starting_index, t_usize *index) { t_usize idx; @@ -53,7 +58,8 @@ t_error vec_str_find_starting(t_vec_str *vec, bool (*fn)(const t_str *), t_usize return (ERROR); } -t_error vec_str_all(t_vec_str *vec, bool (*fn)(const t_str *), bool *result) +t_error vec_str_all(t_vec_str *vec, + bool (*fn)(const t_str *), bool *result) { t_usize idx; @@ -70,7 +76,8 @@ t_error vec_str_all(t_vec_str *vec, bool (*fn)(const t_str *), bool *result) return (ERROR); } -t_error vec_str_any(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) { t_usize idx; @@ -87,7 +94,10 @@ t_error vec_str_any(t_vec_str *vec, bool (*fn)(const t_str *), bool *result) return (ERROR); } -void vec_str_iter(t_vec_str *vec, void (*fn)(t_usize index, t_str *value, void *state), void *state) +void vec_str_iter(t_vec_str *vec, + void (*fn)(t_usize index, t_str *value, + void *state), + void *state) { t_usize idx;