Added env hashmap with build_env function to build envp

This commit is contained in:
Maix0 2024-05-04 19:21:56 +02:00
parent f35e986145
commit f86947a852
54 changed files with 2010 additions and 105 deletions

View file

@ -6,7 +6,7 @@
# By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ # # By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/04/28 17:28:30 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 ?= OBJDIRNAME ?=
# Flags # 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 # Sources
LIB = ./libft/ft_bzero.c \ LIB = ./libft/ft_bzero.c \
./libft/ft_calloc.c \ ./libft/ft_calloc.c \

View file

@ -1,9 +1,15 @@
src/vec/vec_parser_heredoc.c src/hashmap/env/env.c
src/vec/vec_parser_heredoc_functions2.c src/hashmap/env/env_iter.c
src/vec/vec_parser_heredoc_functions3.c src/hashmap/env/env_utils.c
src/vec/vec_parser_range.c src/vec/parser_heredoc/parser_heredoc.c
src/vec/vec_parser_range_functions2.c src/vec/parser_heredoc/parser_heredoc_functions2.c
src/vec/vec_parser_range_functions3.c src/vec/parser_heredoc/parser_heredoc_functions3.c
src/vec/vec_reduce_action.c src/vec/parser_range/parser_range.c
src/vec/vec_reduce_action_functions2.c src/vec/parser_range/parser_range_functions2.c
src/vec/vec_reduce_action_functions3.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

31
includes/app/env.h Normal file
View file

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

View file

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

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 15:49:56 by maiboyer #+# #+# */ /* Created: 2024/05/02 15:49:56 by maiboyer #+# #+# */
/* Updated: 2024/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 #define STATE_H
#include "app/node.h" #include "app/node.h"
#include "me/hashmap/hashmap_env.h"
#include "me/types.h" #include "me/types.h"
typedef struct s_parser typedef struct s_parser
@ -27,8 +28,8 @@ typedef struct s_utils
t_str str_input; t_str str_input;
t_str *strs_input; t_str *strs_input;
t_str *path; t_str *path;
t_str *envp;
t_parser parser; t_parser parser;
t_hashmap_env *env;
t_node current_node; t_node current_node;
} t_utils; } t_utils;

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:41:15 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 "me/types.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include <readline/history.h> #include <readline/history.h>
#include <readline/readline.h> #include <readline/readline.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>

View file

@ -1,9 +1,9 @@
[definition.vec] [definition.vec]
headers = ["stdme/generic_sources/header/vec_C__PREFIX__.h__TEMPLATE__"] headers = ["stdme/generic_sources/header/vec_C__PREFIX__.h__TEMPLATE__"]
sources = [ sources = [
"stdme/generic_sources/src/vec_C__PREFIX__.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___functions2.c__TEMPLATE__",
"stdme/generic_sources/src/vec_C__PREFIX___functions3.c__TEMPLATE__", "stdme/generic_sources/src/vec/C__PREFIX___functions3.c__TEMPLATE__",
] ]
replace.C__TYPENAME__ = "type" replace.C__TYPENAME__ = "type"
replace.C__TYPEHEADER__ = "header_include" replace.C__TYPEHEADER__ = "header_include"
@ -14,8 +14,9 @@ replace.C__PREFIXUP__ = "prefix"
[definition.hashmap] [definition.hashmap]
headers = ["stdme/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__"] headers = ["stdme/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__"]
sources = [ sources = [
"stdme/generic_sources/src/hashmap_C__PREFIX__.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___utils.c__TEMPLATE__",
"stdme/generic_sources/src/hashmap/C__PREFIX___iter.c__TEMPLATE__",
] ]
replace.C__VALTYPE__ = "type" replace.C__VALTYPE__ = "type"
replace.C__KEYTYPE__ = "type" replace.C__KEYTYPE__ = "type"
@ -26,8 +27,8 @@ replace.C__PREFIXUP__ = "prefix"
[definition.str_to_num] [definition.str_to_num]
headers = [] headers = []
sources = [ sources = [
"stdme/generic_sources/src/str_to_C__PREFIX__.c__TEMPLATE__", "stdme/generic_sources/src/convert/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___utils.c__TEMPLATE__",
] ]
replace.C__TYPE__ = "type" replace.C__TYPE__ = "type"
replace.C__UNSIGNED_TYPE__ = "type" replace.C__UNSIGNED_TYPE__ = "type"
@ -38,7 +39,7 @@ replace.C__ZERO__ = "value"
replace.C__SIGNED_TYPE__ = "bool" replace.C__SIGNED_TYPE__ = "bool"
[[create.vec]] [[create.vec]]
sources_output = "src/vec/" sources_output = "src/vec/C__PREFIX__/"
headers_output = "include/me/vec/" headers_output = "include/me/vec/"
replace.C__TYPENAME__ = "t_parser_range" replace.C__TYPENAME__ = "t_parser_range"
replace.C__TYPEHEADER__ = '#include "parser/types/types_parser_range.h"' replace.C__TYPEHEADER__ = '#include "parser/types/types_parser_range.h"'
@ -46,7 +47,7 @@ replace.C__PREFIX__ = "parser_range"
replace.C__PREFIXUP__ = "PARSER_RANGE" replace.C__PREFIXUP__ = "PARSER_RANGE"
[[create.vec]] [[create.vec]]
sources_output = "src/vec/" sources_output = "src/vec/C__PREFIX__/"
headers_output = "include/me/vec/" headers_output = "include/me/vec/"
replace.C__TYPENAME__ = "t_heredoc" replace.C__TYPENAME__ = "t_heredoc"
replace.C__TYPEHEADER__ = '#include "parser/types/types_heredoc.h"' replace.C__TYPEHEADER__ = '#include "parser/types/types_heredoc.h"'
@ -54,9 +55,18 @@ replace.C__PREFIX__ = "parser_heredoc"
replace.C__PREFIXUP__ = "PARSER_HEREDOC" replace.C__PREFIXUP__ = "PARSER_HEREDOC"
[[create.vec]] [[create.vec]]
sources_output = "src/vec/" sources_output = "src/vec/C__PREFIX__/"
headers_output = "include/me/vec/" headers_output = "include/me/vec/"
replace.C__TYPENAME__ = "t_reduce_action" replace.C__TYPENAME__ = "t_reduce_action"
replace.C__TYPEHEADER__ = '#include "parser/types/types_reduce_action.h"' replace.C__TYPEHEADER__ = '#include "parser/types/types_reduce_action.h"'
replace.C__PREFIX__ = "reduce_action" replace.C__PREFIX__ = "reduce_action"
replace.C__PREFIXUP__ = "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"

View file

@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashmap_env.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef HASHMAP_ENV_H
#define HASHMAP_ENV_H
#define DEFAULT_BUCKETS 750
#include "me/hash/hasher.h"
#include "me/types.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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

View file

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_str.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_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

116
output/src/hashmap/env/env.c vendored Normal file
View file

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

39
output/src/hashmap/env/env_iter.c vendored Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashmap_env.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/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);
}

56
output/src/hashmap/env/env_utils.c vendored Normal file
View file

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

115
output/src/vec/str/str.c Normal file
View file

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_str.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_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 <stdlib.h>
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);
}

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_str.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_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 <stdlib.h>
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++;
}
}

View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_str.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_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 <stdlib.h>
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);
}

74
sources/env.c Normal file
View file

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

View file

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

View file

@ -6,47 +6,47 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/13 20:26:13 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" #include "../includes/minishell.h"
void ft_exec_cmd(t_utils *utils, t_str cmd, t_str cmd_args[]) // void ft_exec_cmd(t_utils *utils, t_str cmd, t_str cmd_args[])
{ // {
if (execve(cmd, cmd_args, utils->envp) == -1) // if (execve(cmd, cmd_args, utils->envp) == -1)
{ // {
printf("ERROR >\n execve\n %s\n", cmd); // printf("ERROR >\n execve\n %s\n", cmd);
ft_free_strs((char **)cmd_args); // ft_free_strs((char **)cmd_args);
ft_exit(utils, 1); // ft_exit(utils, 1);
} // }
} // }
//
void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i) // void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i)
{ // {
pid_t pid; // pid_t pid;
t_i32 options; // t_i32 options;
t_str *args; // t_str *args;
t_usize k; // t_usize k;
// t_str cmd; // // t_str cmd;
//
k = prev_i; // k = prev_i;
args = (t_str *)malloc(sizeof(t_str) * (i + 2)); // args = (t_str *)malloc(sizeof(t_str) * (i + 2));
while (prev_i < i) // while (prev_i < i)
{ // {
printf("ft_other_cmd = %s\n", shcat->strs_input[prev_i]); // printf("ft_other_cmd = %s\n", shcat->strs_input[prev_i]);
args[k] = malloc(ft_strlen(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])); // ft_strlcpy(args[k], shcat->strs_input[prev_i], ft_strlen(shcat->strs_input[prev_i]));
prev_i++; // prev_i++;
k++; // k++;
} // }
args[k] = NULL; // args[k] = NULL;
// cmd = ft_check_cmds(shcat, args[tmp]); // // cmd = ft_check_cmds(shcat, args[tmp]);
options = 0; // options = 0;
pid = fork(); // pid = fork();
if (pid == -1) // if (pid == -1)
ft_exit(shcat, 1); // ft_exit(shcat, 1);
// if (pid == 0) // // if (pid == 0)
// ft_exec_cmd(shcat, cmd, args); // // ft_exec_cmd(shcat, cmd, args);
waitpid(pid, NULL, options); // waitpid(pid, NULL, options);
} // }

View file

@ -6,15 +6,16 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/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/node.h"
#include "app/signal_handler.h" #include "app/signal_handler.h"
#include "gmr/symbols.h" #include "gmr/symbols.h"
#include "me/string/str_len.h" #include "me/string/str_len.h"
#include "minishell.h"
#include "parser/api.h" #include "parser/api.h"
t_first_parser *ts_parser_new(); t_first_parser *ts_parser_new();
@ -55,25 +56,25 @@ t_node parse_str(t_parser *parser, t_const_str input)
return (parse_to_nodes(parser->parser, input)); return (parse_to_nodes(parser->parser, input));
} }
void ft_check(t_utils *shcat, char **input) // void ft_check(t_utils *shcat, char **input)
{ // {
t_usize i; // t_usize i;
t_usize prev_i; // t_usize prev_i;
//
i = 0; // i = 0;
prev_i = 0; // prev_i = 0;
while (input[i] != NULL) // while (input[i] != NULL)
{ // {
if (ft_strcmp(input[i], "exit") == 0) // if (ft_strcmp(input[i], "exit") == 0)
ft_exit(shcat, 0); // ft_exit(shcat, 0);
else if (ft_strcmp(input[i], "pwd") == 0) // else if (ft_strcmp(input[i], "pwd") == 0)
ft_pwd(); // ft_pwd();
else // else
ft_other_cmd(shcat, i, prev_i); // ft_other_cmd(shcat, i, prev_i);
prev_i = i; // prev_i = 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);
@ -163,6 +164,7 @@ t_i32 main(t_i32 argc, t_str argv[], t_str envp[])
if (install_signal()) if (install_signal())
return (1); return (1);
utils = (t_utils){}; utils = (t_utils){};
utils.env = create_env_map();
utils.parser = create_myparser(); utils.parser = create_myparser();
utils.name_shell = "\001\x1B[93m\002" utils.name_shell = "\001\x1B[93m\002"
"42sh" "42sh"

View file

@ -1,4 +1,6 @@
env.c
exec/handle_concat.c exec/handle_concat.c
exec/handle_expension.c
exec/handle_raw_word.c exec/handle_raw_word.c
exec/handle_word.c exec/handle_word.c
ft_cmd.c ft_cmd.c

View file

@ -14,6 +14,15 @@ convert/str_to_u64
convert/str_to_u64_utils convert/str_to_u64_utils
convert/str_to_u8 convert/str_to_u8
convert/str_to_u8_utils 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
vec/vec_buf_str_functions2 vec/vec_buf_str_functions2
vec/vec_buf_str_functions3 vec/vec_buf_str_functions3

View file

@ -73,4 +73,10 @@ t_entry_C__PREFIX__ *hashmap_get_entry_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
C__KEYTYPE__ *key, C__KEYTYPE__ *key,
t_entry_C__PREFIX__ **prev); 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 #endif

View file

@ -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); entry = hashmap_get_entry_C__PREFIX__(hmap, hashed_key, &key, &prev);
if (entry == NULL) if (entry == NULL)
{ {
entry = mem_alloc(sizeof(t_entry_tile)); entry = mem_alloc(sizeof(t_entry_C__PREFIX__));
entry->hash_id = hashed_key; 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; entry->next = NULL;
if (prev == NULL) if (prev == NULL)
hmap->buckets[hashed_key % hmap->num_buckets] = entry; hmap->buckets[hashed_key % hmap->num_buckets] = entry;

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashmap_C__PREFIX__.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/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);
}

View file

@ -27,7 +27,7 @@ C__VALTYPE__ *get_hashmap_C__PREFIX__(t_hashmap_C__PREFIX__ *hmap,
hmap->hfunc(&hmap->hasher, key); hmap->hfunc(&hmap->hasher, key);
hashed_key = hasher_reset_and_finish(&hmap->hasher); 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) if (entry == NULL)
return (NULL); return (NULL);
return (&entry->kv.val); return (&entry->kv.val);

View file

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

View file

@ -1,9 +1,9 @@
[definition.vec] [definition.vec]
headers = ["generic_sources/header/vec_C__PREFIX__.h__TEMPLATE__"] headers = ["generic_sources/header/vec_C__PREFIX__.h__TEMPLATE__"]
sources = [ sources = [
"generic_sources/src/vec_C__PREFIX__.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___functions2.c__TEMPLATE__",
"generic_sources/src/vec_C__PREFIX___functions3.c__TEMPLATE__", "generic_sources/src/vec/C__PREFIX___functions3.c__TEMPLATE__",
] ]
replace.C__TYPENAME__ = "type" replace.C__TYPENAME__ = "type"
replace.C__TYPEHEADER__ = "header_include" replace.C__TYPEHEADER__ = "header_include"
@ -14,8 +14,8 @@ replace.C__PREFIXUP__ = "prefix"
[definition.hashmap] [definition.hashmap]
headers = ["generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__"] headers = ["generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__"]
sources = [ sources = [
"generic_sources/src/hashmap_C__PREFIX__.c__TEMPLATE__", "generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__",
"generic_sources/src/hashmap_C__PREFIX___utils.c__TEMPLATE__", "generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__",
] ]
replace.C__VALTYPE__ = "type" replace.C__VALTYPE__ = "type"
replace.C__KEYTYPE__ = "type" replace.C__KEYTYPE__ = "type"
@ -26,8 +26,8 @@ replace.C__PREFIXUP__ = "prefix"
[definition.str_to_num] [definition.str_to_num]
headers = [] headers = []
sources = [ sources = [
"generic_sources/src/str_to_C__PREFIX__.c__TEMPLATE__", "generic_sources/src/convert/str_to_C__PREFIX__.c__TEMPLATE__",
"generic_sources/src/str_to_C__PREFIX___utils.c__TEMPLATE__", "generic_sources/src/convert/str_to_C__PREFIX___utils.c__TEMPLATE__",
] ]
replace.C__TYPE__ = "type" replace.C__TYPE__ = "type"
replace.C__UNSIGNED_TYPE__ = "type" replace.C__UNSIGNED_TYPE__ = "type"
@ -128,7 +128,7 @@ replace.C__SIGNED_TYPE__ = "false"
[[create.vec]] [[create.vec]]
sources_output = "src/vec/" sources_output = "src/vec/C__PREFIX__"
headers_output = "include/me/vec/" headers_output = "include/me/vec/"
replace.C__TYPENAME__ = "t_u8" replace.C__TYPENAME__ = "t_u8"
replace.C__TYPEHEADER__ = '' replace.C__TYPEHEADER__ = ''
@ -136,7 +136,7 @@ replace.C__PREFIX__ = "u8"
replace.C__PREFIXUP__ = "U8" replace.C__PREFIXUP__ = "U8"
[[create.vec]] [[create.vec]]
sources_output = "src/vec/" sources_output = "src/vec/C__PREFIX__"
headers_output = "include/me/vec/" headers_output = "include/me/vec/"
replace.C__TYPENAME__ = "t_str" replace.C__TYPENAME__ = "t_str"
replace.C__TYPEHEADER__ = '' replace.C__TYPEHEADER__ = ''
@ -144,7 +144,7 @@ replace.C__PREFIX__ = "str"
replace.C__PREFIXUP__ = "STR" replace.C__PREFIXUP__ = "STR"
[[create.vec]] [[create.vec]]
sources_output = "src/vec/" sources_output = "src/vec/C__PREFIX__"
headers_output = "include/me/vec/" headers_output = "include/me/vec/"
replace.C__TYPENAME__ = "t_buffer_str" replace.C__TYPENAME__ = "t_buffer_str"
replace.C__TYPEHEADER__ = '#include "me/buffered_str/buf_str.h"' replace.C__TYPEHEADER__ = '#include "me/buffered_str/buf_str.h"'

View file

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_buf_str.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_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 <stdlib.h>
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);
}

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_buf_str.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_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 <stdlib.h>
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++;
}
}

View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_buf_str.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_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 <stdlib.h>
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);
}

View file

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_str.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_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 <stdlib.h>
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);
}

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_str.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_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 <stdlib.h>
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++;
}
}

View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_str.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_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 <stdlib.h>
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);
}

View file

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_u8.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_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 <stdlib.h>
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);
}

View file

@ -0,0 +1,112 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_u8.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_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 <stdlib.h>
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++;
}
}

View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_u8.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_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 <stdlib.h>
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);
}

View file

@ -41,6 +41,7 @@ fs/write
gnl/get_next_line gnl/get_next_line
hash/hasher hash/hasher
hash/hash_signed hash/hash_signed
hash/hash_str
hash/hash_unsigned hash/hash_unsigned
hash/sip/sip13 hash/sip/sip13
hash/sip/sip_utils hash/sip/sip_utils
@ -91,6 +92,7 @@ printf/matchers
printf/printf printf/printf
printf/vprintf printf/vprintf
string/str_clone string/str_clone
string/str_compare
string/str_find_chr string/str_find_chr
string/str_find_rev_chr string/str_find_rev_chr
string/str_find_str string/str_find_str

20
stdme/src/hash/hash_str.c Normal file
View file

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

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_compare.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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]);
}