Update: stdme/os module
This commit is contained in:
parent
dfb10f3390
commit
16f49181b5
37 changed files with 1201 additions and 345 deletions
|
|
@ -17,6 +17,9 @@ convert/str_to_u8_utils
|
||||||
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
|
||||||
|
vec/vec_str
|
||||||
|
vec/vec_str_functions2
|
||||||
|
vec/vec_str_functions3
|
||||||
vec/vec_u8
|
vec/vec_u8
|
||||||
vec/vec_u8_functions2
|
vec/vec_u8_functions2
|
||||||
vec/vec_u8_functions3
|
vec/vec_u8_functions3
|
||||||
|
|
|
||||||
26
stdme/include/me/os/pipe.h
Normal file
26
stdme/include/me/os/pipe.h
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pipe.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/01/04 17:57:29 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/01/04 17:59:30 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PIPE_H
|
||||||
|
# define PIPE_H
|
||||||
|
|
||||||
|
# include "me/types.h"
|
||||||
|
|
||||||
|
typedef struct s_pipe
|
||||||
|
{
|
||||||
|
t_file read;
|
||||||
|
t_file write;
|
||||||
|
} t_pipe;
|
||||||
|
|
||||||
|
t_error create_pipe(t_pipe *out);
|
||||||
|
|
||||||
|
#endif /* PIPE_H */
|
||||||
145
stdme/include/me/os/process.h
Normal file
145
stdme/include/me/os/process.h
Normal file
|
|
@ -0,0 +1,145 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* process.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/01/03 15:43:08 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/01/06 18:39:58 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef PROCESS_H
|
||||||
|
# define PROCESS_H
|
||||||
|
|
||||||
|
# include "me/types.h"
|
||||||
|
# include "me/vec/vec_str.h"
|
||||||
|
# include "me/vec/vec_u8.h"
|
||||||
|
|
||||||
|
typedef t_i32 t_pid;
|
||||||
|
typedef t_i32 t_exit_code;
|
||||||
|
|
||||||
|
enum e_redirection
|
||||||
|
{
|
||||||
|
R_INHERITED = 0,
|
||||||
|
R_PIPED = 1,
|
||||||
|
R_FD = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
union u_redirection
|
||||||
|
{
|
||||||
|
struct s_fd
|
||||||
|
{
|
||||||
|
t_file value;
|
||||||
|
} fd;
|
||||||
|
struct s_piped
|
||||||
|
{
|
||||||
|
} piped;
|
||||||
|
struct s_inherited
|
||||||
|
{
|
||||||
|
} inherited;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct s_redirection
|
||||||
|
{
|
||||||
|
enum e_redirection tag;
|
||||||
|
union u_redirection vals;
|
||||||
|
} t_redirection;
|
||||||
|
|
||||||
|
static inline t_redirection piped(void)
|
||||||
|
{
|
||||||
|
return ((t_redirection){
|
||||||
|
.tag = R_PIPED,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline t_redirection inherited(void)
|
||||||
|
{
|
||||||
|
return ((t_redirection){
|
||||||
|
.tag = R_INHERITED,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline t_redirection fd(t_file fd)
|
||||||
|
{
|
||||||
|
return ((t_redirection){.tag = R_FD, \
|
||||||
|
.vals = (union u_redirection){.fd = {.value = fd},}});
|
||||||
|
}
|
||||||
|
|
||||||
|
enum e_wrapped_fd_tag
|
||||||
|
{
|
||||||
|
READ_ONLY,
|
||||||
|
WRITE_ONLY,
|
||||||
|
READ_WRITE,
|
||||||
|
INVALID,
|
||||||
|
};
|
||||||
|
|
||||||
|
union u_wrapped_fd
|
||||||
|
{
|
||||||
|
struct s_read_only
|
||||||
|
{
|
||||||
|
t_file fd;
|
||||||
|
} ro;
|
||||||
|
struct s_write_only
|
||||||
|
{
|
||||||
|
t_file fd;
|
||||||
|
} wo;
|
||||||
|
struct s_read_write
|
||||||
|
{
|
||||||
|
t_file fd;
|
||||||
|
} rw;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct s_wrapped_fd
|
||||||
|
{
|
||||||
|
enum e_wrapped_fd_tag tag;
|
||||||
|
union u_wrapped_fd vals;
|
||||||
|
} t_wrapped_fd;
|
||||||
|
|
||||||
|
static inline t_wrapped_fd ro(t_file fd)
|
||||||
|
{
|
||||||
|
return ((t_wrapped_fd){.tag = READ_ONLY,
|
||||||
|
.vals = (union u_wrapped_fd){
|
||||||
|
.ro = {.fd = fd},
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline t_wrapped_fd wo(t_file fd)
|
||||||
|
{
|
||||||
|
return ((t_wrapped_fd){.tag = WRITE_ONLY,
|
||||||
|
.vals = (union u_wrapped_fd){.wo = {.fd = fd}}});
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct s_spawn_info
|
||||||
|
{
|
||||||
|
t_redirection stdin;
|
||||||
|
t_redirection stdout;
|
||||||
|
t_redirection stderr;
|
||||||
|
t_vec_str arguments;
|
||||||
|
t_vec_str environement;
|
||||||
|
t_str binary_path;
|
||||||
|
void (*forked_free)(void *);
|
||||||
|
void *forked_free_args;
|
||||||
|
} t_spawn_info;
|
||||||
|
|
||||||
|
typedef struct s_process
|
||||||
|
{
|
||||||
|
t_wrapped_fd stdin;
|
||||||
|
t_wrapped_fd stdout;
|
||||||
|
t_wrapped_fd stderr;
|
||||||
|
t_pid pid;
|
||||||
|
} t_process;
|
||||||
|
|
||||||
|
typedef struct s_process_output
|
||||||
|
{
|
||||||
|
t_pid pid;
|
||||||
|
t_vec_u8 stdout;
|
||||||
|
t_vec_u8 stderr;
|
||||||
|
t_exit_code exit_code;
|
||||||
|
} t_process_output;
|
||||||
|
|
||||||
|
t_error spawn_process(t_spawn_info info,
|
||||||
|
t_process *process);
|
||||||
|
|
||||||
|
#endif /* PROCESS_H */
|
||||||
|
|
@ -135,6 +135,14 @@ replace.C__TYPEHEADER__ = ''
|
||||||
replace.C__PREFIX__ = "u8"
|
replace.C__PREFIX__ = "u8"
|
||||||
replace.C__PREFIXUP__ = "U8"
|
replace.C__PREFIXUP__ = "U8"
|
||||||
|
|
||||||
|
[[create.vec]]
|
||||||
|
sources_output = "src/vec/"
|
||||||
|
headers_output = "include/me/vec/"
|
||||||
|
replace.C__TYPENAME__ = "t_str"
|
||||||
|
replace.C__TYPEHEADER__ = ''
|
||||||
|
replace.C__PREFIX__ = "str"
|
||||||
|
replace.C__PREFIXUP__ = "STR"
|
||||||
|
|
||||||
[[create.vec]]
|
[[create.vec]]
|
||||||
sources_output = "src/vec/"
|
sources_output = "src/vec/"
|
||||||
headers_output = "include/me/vec/"
|
headers_output = "include/me/vec/"
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,7 @@
|
||||||
#include "me/buffered_str/buf_str.h"
|
#include "me/buffered_str/buf_str.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
|
|
||||||
typedef bool (*t_vec_buf_str_sort_fn)(t_buffer_str *,
|
typedef bool (*t_vec_buf_str_sort_fn)(t_buffer_str *, t_buffer_str *);
|
||||||
t_buffer_str *);
|
|
||||||
typedef void (*t_free_buf_str_item)(t_buffer_str);
|
typedef void (*t_free_buf_str_item)(t_buffer_str);
|
||||||
|
|
||||||
typedef struct s_vec_buf_str
|
typedef struct s_vec_buf_str
|
||||||
|
|
@ -30,14 +29,11 @@ typedef struct s_vec_buf_str
|
||||||
|
|
||||||
t_vec_buf_str vec_buf_str_new(t_usize capacity,
|
t_vec_buf_str vec_buf_str_new(t_usize capacity,
|
||||||
t_free_buf_str_item free_function);
|
t_free_buf_str_item free_function);
|
||||||
t_error vec_buf_str_push(t_vec_buf_str *vec,
|
t_error vec_buf_str_push(t_vec_buf_str *vec, t_buffer_str element);
|
||||||
t_buffer_str element);
|
|
||||||
t_error vec_buf_str_push_front(t_vec_buf_str *vec,
|
t_error vec_buf_str_push_front(t_vec_buf_str *vec,
|
||||||
t_buffer_str element);
|
t_buffer_str element);
|
||||||
t_error vec_buf_str_pop(t_vec_buf_str *vec,
|
t_error vec_buf_str_pop(t_vec_buf_str *vec, t_buffer_str *value);
|
||||||
t_buffer_str *value);
|
t_error vec_buf_str_pop_front(t_vec_buf_str *vec, t_buffer_str *value);
|
||||||
t_error vec_buf_str_pop_front(t_vec_buf_str *vec,
|
|
||||||
t_buffer_str *value);
|
|
||||||
void vec_buf_str_free(t_vec_buf_str vec);
|
void vec_buf_str_free(t_vec_buf_str vec);
|
||||||
t_error vec_buf_str_reserve(t_vec_buf_str *vec,
|
t_error vec_buf_str_reserve(t_vec_buf_str *vec,
|
||||||
t_usize wanted_capacity);
|
t_usize wanted_capacity);
|
||||||
|
|
@ -52,7 +48,8 @@ t_error vec_buf_str_any(t_vec_buf_str *vec,
|
||||||
bool (*fn)(const t_buffer_str *), bool *result);
|
bool (*fn)(const t_buffer_str *), bool *result);
|
||||||
void vec_buf_str_iter(t_vec_buf_str *vec,
|
void vec_buf_str_iter(t_vec_buf_str *vec,
|
||||||
void (*fn)(t_usize index, t_buffer_str *value,
|
void (*fn)(t_usize index, t_buffer_str *value,
|
||||||
void *state), void *state);
|
void *state),
|
||||||
|
void *state);
|
||||||
void vec_buf_str_reverse(t_vec_buf_str *vec);
|
void vec_buf_str_reverse(t_vec_buf_str *vec);
|
||||||
void vec_buf_str_sort(t_vec_buf_str *vec,
|
void vec_buf_str_sort(t_vec_buf_str *vec,
|
||||||
t_vec_buf_str_sort_fn is_sorted);
|
t_vec_buf_str_sort_fn is_sorted);
|
||||||
|
|
|
||||||
57
stdme/output/include/me/vec/vec_str.h
Normal file
57
stdme/output/include/me/vec/vec_str.h
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
#ifndef VEC_U8_H
|
#ifndef VEC_U8_H
|
||||||
#define VEC_U8_H
|
#define VEC_U8_H
|
||||||
|
|
||||||
|
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
|
|
||||||
typedef bool (*t_vec_u8_sort_fn)(t_u8 *, t_u8 *);
|
typedef bool (*t_vec_u8_sort_fn)(t_u8 *, t_u8 *);
|
||||||
|
|
@ -26,25 +27,31 @@ typedef struct s_vec_u8
|
||||||
t_u8 *buffer;
|
t_u8 *buffer;
|
||||||
} t_vec_u8;
|
} t_vec_u8;
|
||||||
|
|
||||||
t_vec_u8 vec_u8_new(t_usize capacity, t_free_u8_item free_function);
|
t_vec_u8 vec_u8_new(t_usize capacity,
|
||||||
|
t_free_u8_item free_function);
|
||||||
t_error vec_u8_push(t_vec_u8 *vec, t_u8 element);
|
t_error vec_u8_push(t_vec_u8 *vec, t_u8 element);
|
||||||
t_error vec_u8_push_front(t_vec_u8 *vec, t_u8 element);
|
t_error vec_u8_push_front(t_vec_u8 *vec,
|
||||||
|
t_u8 element);
|
||||||
t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value);
|
t_error vec_u8_pop(t_vec_u8 *vec, t_u8 *value);
|
||||||
t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value);
|
t_error vec_u8_pop_front(t_vec_u8 *vec, t_u8 *value);
|
||||||
void vec_u8_free(t_vec_u8 vec);
|
void vec_u8_free(t_vec_u8 vec);
|
||||||
t_error vec_u8_reserve(t_vec_u8 *vec, t_usize wanted_capacity);
|
t_error vec_u8_reserve(t_vec_u8 *vec,
|
||||||
t_error vec_u8_find(t_vec_u8 *vec, bool (*fn)(const t_u8 *),
|
t_usize wanted_capacity);
|
||||||
t_usize *index);
|
t_error vec_u8_find(t_vec_u8 *vec,
|
||||||
|
bool (*fn)(const t_u8 *), t_usize *index);
|
||||||
t_error vec_u8_find_starting(t_vec_u8 *vec,
|
t_error vec_u8_find_starting(t_vec_u8 *vec,
|
||||||
bool (*fn)(const t_u8 *), t_usize starting_index,
|
bool (*fn)(const t_u8 *),
|
||||||
t_usize *index);
|
t_usize starting_index, t_usize *index);
|
||||||
t_error vec_u8_all(t_vec_u8 *vec, bool (*fn)(const t_u8 *),
|
t_error vec_u8_all(t_vec_u8 *vec,
|
||||||
bool *result);
|
bool (*fn)(const t_u8 *), bool *result);
|
||||||
t_error vec_u8_any(t_vec_u8 *vec, bool (*fn)(const t_u8 *),
|
t_error vec_u8_any(t_vec_u8 *vec,
|
||||||
bool *result);
|
bool (*fn)(const t_u8 *), bool *result);
|
||||||
void vec_u8_iter(t_vec_u8 *vec, void (*fn)(t_usize index,
|
void vec_u8_iter(t_vec_u8 *vec,
|
||||||
t_u8 *value, void *state), void *state);
|
void (*fn)(t_usize index, t_u8 *value,
|
||||||
|
void *state),
|
||||||
|
void *state);
|
||||||
void vec_u8_reverse(t_vec_u8 *vec);
|
void vec_u8_reverse(t_vec_u8 *vec);
|
||||||
void vec_u8_sort(t_vec_u8 *vec, t_vec_u8_sort_fn is_sorted);
|
void vec_u8_sort(t_vec_u8 *vec,
|
||||||
|
t_vec_u8_sort_fn is_sorted);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,8 @@ t_error checked_mul_i16(t_i16 lhs, t_i16 rhs, t_i16 *out);
|
||||||
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
||||||
t_usize digits_len)
|
t_usize digits_len)
|
||||||
{
|
{
|
||||||
return (radix <= 16 && digits_len <= sizeof(t_i16) * 2
|
return (radix <= 16 &&
|
||||||
- (t_usize)is_signed_type);
|
digits_len <= sizeof(t_i16) * 2 - (t_usize)is_signed_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
||||||
|
|
@ -86,6 +86,7 @@ static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op,
|
||||||
t_i16 result;
|
t_i16 result;
|
||||||
|
|
||||||
result = 0;
|
result = 0;
|
||||||
|
|
||||||
while (*s)
|
while (*s)
|
||||||
{
|
{
|
||||||
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,21 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* str_to_i16_utils.c :+: :+: :+: */
|
/* str_to_i64.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
|
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/03/07 15:24:57 by maiboyer ### ########.fr */
|
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
||||||
#include "me/char/isalpha.h"
|
#include "me/char/isalpha.h"
|
||||||
#include "me/convert/str_to_numbers.h"
|
#include "me/convert/str_to_numbers.h"
|
||||||
#include "me/printf/printf.h"
|
|
||||||
#include "me/string/str_len.h"
|
#include "me/string/str_len.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
|
#include "me/printf/printf.h"
|
||||||
|
|
||||||
t_error checked_add_i16(t_i16 lhs, t_i16 rhs, t_i16 *out)
|
t_error checked_add_i16(t_i16 lhs, t_i16 rhs, t_i16 *out)
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +27,7 @@ t_error checked_add_i16(t_i16 lhs, t_i16 rhs, t_i16 *out)
|
||||||
|
|
||||||
t_error checked_sub_i16(t_i16 lhs, t_i16 rhs, t_i16 *out)
|
t_error checked_sub_i16(t_i16 lhs, t_i16 rhs, t_i16 *out)
|
||||||
{
|
{
|
||||||
if ((((rhs & (1 << (sizeof(t_i16) - 1)) || rhs == 0) || !true) && (lhs \
|
if ((((rhs & (1 << (sizeof(t_i16) - 1)) || rhs == 0) || !true) && (lhs < -32768 + rhs)))
|
||||||
< -32768 + rhs)))
|
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = (t_i16)(lhs - rhs);
|
*out = (t_i16)(lhs - rhs);
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,8 @@ t_error checked_mul_i32(t_i32 lhs, t_i32 rhs, t_i32 *out);
|
||||||
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
||||||
t_usize digits_len)
|
t_usize digits_len)
|
||||||
{
|
{
|
||||||
return (radix <= 16 && digits_len <= sizeof(t_i32) * 2
|
return (radix <= 16 &&
|
||||||
- (t_usize)is_signed_type);
|
digits_len <= sizeof(t_i32) * 2 - (t_usize)is_signed_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
||||||
|
|
@ -86,6 +86,7 @@ static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op,
|
||||||
t_i32 result;
|
t_i32 result;
|
||||||
|
|
||||||
result = 0;
|
result = 0;
|
||||||
|
|
||||||
while (*s)
|
while (*s)
|
||||||
{
|
{
|
||||||
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,21 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* str_to_i32_utils.c :+: :+: :+: */
|
/* str_to_i64.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
|
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/03/07 15:24:49 by maiboyer ### ########.fr */
|
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
||||||
#include "me/char/isalpha.h"
|
#include "me/char/isalpha.h"
|
||||||
#include "me/convert/str_to_numbers.h"
|
#include "me/convert/str_to_numbers.h"
|
||||||
#include "me/printf/printf.h"
|
|
||||||
#include "me/string/str_len.h"
|
#include "me/string/str_len.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
|
#include "me/printf/printf.h"
|
||||||
|
|
||||||
t_error checked_add_i32(t_i32 lhs, t_i32 rhs, t_i32 *out)
|
t_error checked_add_i32(t_i32 lhs, t_i32 rhs, t_i32 *out)
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +27,7 @@ t_error checked_add_i32(t_i32 lhs, t_i32 rhs, t_i32 *out)
|
||||||
|
|
||||||
t_error checked_sub_i32(t_i32 lhs, t_i32 rhs, t_i32 *out)
|
t_error checked_sub_i32(t_i32 lhs, t_i32 rhs, t_i32 *out)
|
||||||
{
|
{
|
||||||
if ((((rhs & (1 << (sizeof(t_i32) - 1)) || rhs == 0) || !true) && (lhs \
|
if ((((rhs & (1 << (sizeof(t_i32) - 1)) || rhs == 0) || !true) && (lhs < -2147483648 + rhs)))
|
||||||
< -2147483648 + rhs)))
|
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = (t_i32)(lhs - rhs);
|
*out = (t_i32)(lhs - rhs);
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,8 @@ t_error checked_mul_i64(t_i64 lhs, t_i64 rhs, t_i64 *out);
|
||||||
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
||||||
t_usize digits_len)
|
t_usize digits_len)
|
||||||
{
|
{
|
||||||
return (radix <= 16 && digits_len <= sizeof(t_i64) * 2
|
return (radix <= 16 &&
|
||||||
- (t_usize)is_signed_type);
|
digits_len <= sizeof(t_i64) * 2 - (t_usize)is_signed_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
||||||
|
|
@ -86,6 +86,7 @@ static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op,
|
||||||
t_i64 result;
|
t_i64 result;
|
||||||
|
|
||||||
result = 0ll;
|
result = 0ll;
|
||||||
|
|
||||||
while (*s)
|
while (*s)
|
||||||
{
|
{
|
||||||
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,21 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* str_to_i64_utils.c :+: :+: :+: */
|
/* str_to_i64.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
|
/* Created: 2024/02/01 21:15:19 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/03/07 15:24:38 by maiboyer ### ########.fr */
|
/* Updated: 2024/02/01 23:18:52 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
||||||
#include "me/char/isalpha.h"
|
#include "me/char/isalpha.h"
|
||||||
#include "me/convert/str_to_numbers.h"
|
#include "me/convert/str_to_numbers.h"
|
||||||
#include "me/printf/printf.h"
|
|
||||||
#include "me/string/str_len.h"
|
#include "me/string/str_len.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
|
#include "me/printf/printf.h"
|
||||||
|
|
||||||
t_error checked_add_i64(t_i64 lhs, t_i64 rhs, t_i64 *out)
|
t_error checked_add_i64(t_i64 lhs, t_i64 rhs, t_i64 *out)
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +27,7 @@ t_error checked_add_i64(t_i64 lhs, t_i64 rhs, t_i64 *out)
|
||||||
|
|
||||||
t_error checked_sub_i64(t_i64 lhs, t_i64 rhs, t_i64 *out)
|
t_error checked_sub_i64(t_i64 lhs, t_i64 rhs, t_i64 *out)
|
||||||
{
|
{
|
||||||
if ((((rhs & (1 << (sizeof(t_i64) - 1)) || rhs == 0) || !true) && (lhs \
|
if ((((rhs & (1 << (sizeof(t_i64) - 1)) || rhs == 0) || !true) && (lhs < -(~9223372036854775807ll + 1) + rhs)))
|
||||||
< -(~9223372036854775807ll + 1) + rhs)))
|
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = (t_i64)(lhs - rhs);
|
*out = (t_i64)(lhs - rhs);
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,8 @@ t_error checked_mul_i8(t_i8 lhs, t_i8 rhs, t_i8 *out);
|
||||||
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
||||||
t_usize digits_len)
|
t_usize digits_len)
|
||||||
{
|
{
|
||||||
return (radix <= 16 && digits_len <= sizeof(t_i8) * 2
|
return (radix <= 16 &&
|
||||||
- (t_usize)is_signed_type);
|
digits_len <= sizeof(t_i8) * 2 - (t_usize)is_signed_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
||||||
|
|
@ -86,6 +86,7 @@ static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op,
|
||||||
t_i8 result;
|
t_i8 result;
|
||||||
|
|
||||||
result = 0;
|
result = 0;
|
||||||
|
|
||||||
while (*s)
|
while (*s)
|
||||||
{
|
{
|
||||||
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,12 @@
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
||||||
#include "me/char/isalpha.h"
|
#include "me/char/isalpha.h"
|
||||||
#include "me/convert/str_to_numbers.h"
|
#include "me/convert/str_to_numbers.h"
|
||||||
#include "me/printf/printf.h"
|
|
||||||
#include "me/string/str_len.h"
|
#include "me/string/str_len.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
|
#include "me/printf/printf.h"
|
||||||
|
|
||||||
t_error checked_add_i8(t_i8 lhs, t_i8 rhs, t_i8 *out)
|
t_error checked_add_i8(t_i8 lhs, t_i8 rhs, t_i8 *out)
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +27,7 @@ t_error checked_add_i8(t_i8 lhs, t_i8 rhs, t_i8 *out)
|
||||||
|
|
||||||
t_error checked_sub_i8(t_i8 lhs, t_i8 rhs, t_i8 *out)
|
t_error checked_sub_i8(t_i8 lhs, t_i8 rhs, t_i8 *out)
|
||||||
{
|
{
|
||||||
if ((((rhs & (1 << (sizeof(t_i8) - 1)) || rhs == 0) || !true) && (lhs < -128
|
if ((((rhs & (1 << (sizeof(t_i8) - 1)) || rhs == 0) || !true) && (lhs < -128 + rhs)))
|
||||||
+ rhs)))
|
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = (t_i8)(lhs - rhs);
|
*out = (t_i8)(lhs - rhs);
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,8 @@ t_error checked_mul_u16(t_u16 lhs, t_u16 rhs, t_u16 *out);
|
||||||
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
||||||
t_usize digits_len)
|
t_usize digits_len)
|
||||||
{
|
{
|
||||||
return (radix <= 16 && digits_len <= sizeof(t_u16) * 2
|
return (radix <= 16 &&
|
||||||
- (t_usize)is_signed_type);
|
digits_len <= sizeof(t_u16) * 2 - (t_usize)is_signed_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
||||||
|
|
@ -86,6 +86,7 @@ static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op,
|
||||||
t_u16 result;
|
t_u16 result;
|
||||||
|
|
||||||
result = 0u;
|
result = 0u;
|
||||||
|
|
||||||
while (*s)
|
while (*s)
|
||||||
{
|
{
|
||||||
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,12 @@
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
||||||
#include "me/char/isalpha.h"
|
#include "me/char/isalpha.h"
|
||||||
#include "me/convert/str_to_numbers.h"
|
#include "me/convert/str_to_numbers.h"
|
||||||
#include "me/printf/printf.h"
|
|
||||||
#include "me/string/str_len.h"
|
#include "me/string/str_len.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
|
#include "me/printf/printf.h"
|
||||||
|
|
||||||
t_error checked_add_u16(t_u16 lhs, t_u16 rhs, t_u16 *out)
|
t_error checked_add_u16(t_u16 lhs, t_u16 rhs, t_u16 *out)
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +27,7 @@ t_error checked_add_u16(t_u16 lhs, t_u16 rhs, t_u16 *out)
|
||||||
|
|
||||||
t_error checked_sub_u16(t_u16 lhs, t_u16 rhs, t_u16 *out)
|
t_error checked_sub_u16(t_u16 lhs, t_u16 rhs, t_u16 *out)
|
||||||
{
|
{
|
||||||
if ((((rhs & (1 << (sizeof(t_u16) - 1)) || rhs == 0) || !false) && (lhs < 0u
|
if ((((rhs & (1 << (sizeof(t_u16) - 1)) || rhs == 0) || !false) && (lhs < 0u + rhs)))
|
||||||
+ rhs)))
|
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = (t_u16)(lhs - rhs);
|
*out = (t_u16)(lhs - rhs);
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,8 @@ t_error checked_mul_u32(t_u32 lhs, t_u32 rhs, t_u32 *out);
|
||||||
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
||||||
t_usize digits_len)
|
t_usize digits_len)
|
||||||
{
|
{
|
||||||
return (radix <= 16 && digits_len <= sizeof(t_u32) * 2
|
return (radix <= 16 &&
|
||||||
- (t_usize)is_signed_type);
|
digits_len <= sizeof(t_u32) * 2 - (t_usize)is_signed_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
||||||
|
|
@ -86,6 +86,7 @@ static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op,
|
||||||
t_u32 result;
|
t_u32 result;
|
||||||
|
|
||||||
result = 0u;
|
result = 0u;
|
||||||
|
|
||||||
while (*s)
|
while (*s)
|
||||||
{
|
{
|
||||||
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,12 @@
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
||||||
#include "me/char/isalpha.h"
|
#include "me/char/isalpha.h"
|
||||||
#include "me/convert/str_to_numbers.h"
|
#include "me/convert/str_to_numbers.h"
|
||||||
#include "me/printf/printf.h"
|
|
||||||
#include "me/string/str_len.h"
|
#include "me/string/str_len.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
|
#include "me/printf/printf.h"
|
||||||
|
|
||||||
t_error checked_add_u32(t_u32 lhs, t_u32 rhs, t_u32 *out)
|
t_error checked_add_u32(t_u32 lhs, t_u32 rhs, t_u32 *out)
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +27,7 @@ t_error checked_add_u32(t_u32 lhs, t_u32 rhs, t_u32 *out)
|
||||||
|
|
||||||
t_error checked_sub_u32(t_u32 lhs, t_u32 rhs, t_u32 *out)
|
t_error checked_sub_u32(t_u32 lhs, t_u32 rhs, t_u32 *out)
|
||||||
{
|
{
|
||||||
if ((((rhs & (1 << (sizeof(t_u32) - 1)) || rhs == 0) || !false) && (lhs < 0u
|
if ((((rhs & (1 << (sizeof(t_u32) - 1)) || rhs == 0) || !false) && (lhs < 0u + rhs)))
|
||||||
+ rhs)))
|
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = (t_u32)(lhs - rhs);
|
*out = (t_u32)(lhs - rhs);
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,8 @@ t_error checked_mul_u64(t_u64 lhs, t_u64 rhs, t_u64 *out);
|
||||||
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
||||||
t_usize digits_len)
|
t_usize digits_len)
|
||||||
{
|
{
|
||||||
return (radix <= 16 && digits_len <= sizeof(t_u64) * 2
|
return (radix <= 16 &&
|
||||||
- (t_usize)is_signed_type);
|
digits_len <= sizeof(t_u64) * 2 - (t_usize)is_signed_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
||||||
|
|
@ -86,6 +86,7 @@ static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op,
|
||||||
t_u64 result;
|
t_u64 result;
|
||||||
|
|
||||||
result = 0llu;
|
result = 0llu;
|
||||||
|
|
||||||
while (*s)
|
while (*s)
|
||||||
{
|
{
|
||||||
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,12 @@
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
||||||
#include "me/char/isalpha.h"
|
#include "me/char/isalpha.h"
|
||||||
#include "me/convert/str_to_numbers.h"
|
#include "me/convert/str_to_numbers.h"
|
||||||
#include "me/printf/printf.h"
|
|
||||||
#include "me/string/str_len.h"
|
#include "me/string/str_len.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
|
#include "me/printf/printf.h"
|
||||||
|
|
||||||
t_error checked_add_u64(t_u64 lhs, t_u64 rhs, t_u64 *out)
|
t_error checked_add_u64(t_u64 lhs, t_u64 rhs, t_u64 *out)
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +27,7 @@ t_error checked_add_u64(t_u64 lhs, t_u64 rhs, t_u64 *out)
|
||||||
|
|
||||||
t_error checked_sub_u64(t_u64 lhs, t_u64 rhs, t_u64 *out)
|
t_error checked_sub_u64(t_u64 lhs, t_u64 rhs, t_u64 *out)
|
||||||
{
|
{
|
||||||
if ((((rhs & (1 << (sizeof(t_u64) - 1)) || rhs == 0) || !false)
|
if ((((rhs & (1 << (sizeof(t_u64) - 1)) || rhs == 0) || !false) && (lhs < 0llu + rhs)))
|
||||||
&& (lhs < 0llu + rhs)))
|
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = (t_u64)(lhs - rhs);
|
*out = (t_u64)(lhs - rhs);
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,8 @@ t_error checked_mul_u8(t_u8 lhs, t_u8 rhs, t_u8 *out);
|
||||||
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
static inline bool can_not_overflow(t_u32 radix, bool is_signed_type,
|
||||||
t_usize digits_len)
|
t_usize digits_len)
|
||||||
{
|
{
|
||||||
return (radix <= 16 && digits_len <= sizeof(t_u8) * 2
|
return (radix <= 16 &&
|
||||||
- (t_usize)is_signed_type);
|
digits_len <= sizeof(t_u8) * 2 - (t_usize)is_signed_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
static inline t_error to_digit(t_u8 ascii, t_u32 radix, t_u32 *out)
|
||||||
|
|
@ -86,6 +86,7 @@ static inline t_error loop_inner(t_const_str s, t_u32 radix, t_u8 op,
|
||||||
t_u8 result;
|
t_u8 result;
|
||||||
|
|
||||||
result = 0u;
|
result = 0u;
|
||||||
|
|
||||||
while (*s)
|
while (*s)
|
||||||
{
|
{
|
||||||
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
if (do_operation(radix, (op & OP_CHK) | OP_MUL, &result))
|
||||||
|
|
|
||||||
|
|
@ -10,11 +10,12 @@
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
||||||
#include "me/char/isalpha.h"
|
#include "me/char/isalpha.h"
|
||||||
#include "me/convert/str_to_numbers.h"
|
#include "me/convert/str_to_numbers.h"
|
||||||
#include "me/printf/printf.h"
|
|
||||||
#include "me/string/str_len.h"
|
#include "me/string/str_len.h"
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
|
#include "me/printf/printf.h"
|
||||||
|
|
||||||
t_error checked_add_u8(t_u8 lhs, t_u8 rhs, t_u8 *out)
|
t_error checked_add_u8(t_u8 lhs, t_u8 rhs, t_u8 *out)
|
||||||
{
|
{
|
||||||
|
|
@ -26,8 +27,7 @@ t_error checked_add_u8(t_u8 lhs, t_u8 rhs, t_u8 *out)
|
||||||
|
|
||||||
t_error checked_sub_u8(t_u8 lhs, t_u8 rhs, t_u8 *out)
|
t_error checked_sub_u8(t_u8 lhs, t_u8 rhs, t_u8 *out)
|
||||||
{
|
{
|
||||||
if ((((rhs & (1 << (sizeof(t_u8) - 1)) || rhs == 0) || !false) && (lhs < 0u
|
if ((((rhs & (1 << (sizeof(t_u8) - 1)) || rhs == 0) || !false) && (lhs < 0u + rhs)))
|
||||||
+ rhs)))
|
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
*out = (t_u8)(lhs - rhs);
|
*out = (t_u8)(lhs - rhs);
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,8 @@
|
||||||
#include "me/vec/vec_buf_str.h"
|
#include "me/vec/vec_buf_str.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
t_error vec_buf_str_find(t_vec_buf_str *vec, bool (*fn)(const t_buffer_str *),
|
t_error vec_buf_str_find(t_vec_buf_str *vec,
|
||||||
t_usize *index)
|
bool (*fn)(const t_buffer_str *), t_usize *index)
|
||||||
{
|
{
|
||||||
t_usize idx;
|
t_usize idx;
|
||||||
|
|
||||||
|
|
@ -38,8 +38,8 @@ t_error vec_buf_str_find(t_vec_buf_str *vec, bool (*fn)(const t_buffer_str *),
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error vec_buf_str_find_starting(t_vec_buf_str *vec,
|
t_error vec_buf_str_find_starting(t_vec_buf_str *vec,
|
||||||
bool (*fn)(const t_buffer_str *), t_usize starting_index,
|
bool (*fn)(const t_buffer_str *),
|
||||||
t_usize *index)
|
t_usize starting_index, t_usize *index)
|
||||||
{
|
{
|
||||||
t_usize idx;
|
t_usize idx;
|
||||||
|
|
||||||
|
|
@ -58,8 +58,8 @@ t_error vec_buf_str_find_starting(t_vec_buf_str *vec,
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error vec_buf_str_all(t_vec_buf_str *vec, bool (*fn)(const t_buffer_str *),
|
t_error vec_buf_str_all(t_vec_buf_str *vec,
|
||||||
bool *result)
|
bool (*fn)(const t_buffer_str *), bool *result)
|
||||||
{
|
{
|
||||||
t_usize idx;
|
t_usize idx;
|
||||||
|
|
||||||
|
|
@ -76,8 +76,8 @@ t_error vec_buf_str_all(t_vec_buf_str *vec, bool (*fn)(const t_buffer_str *),
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error vec_buf_str_any(t_vec_buf_str *vec, bool (*fn)(const t_buffer_str *),
|
t_error vec_buf_str_any(t_vec_buf_str *vec,
|
||||||
bool *result)
|
bool (*fn)(const t_buffer_str *), bool *result)
|
||||||
{
|
{
|
||||||
t_usize idx;
|
t_usize idx;
|
||||||
|
|
||||||
|
|
@ -94,8 +94,10 @@ t_error vec_buf_str_any(t_vec_buf_str *vec, bool (*fn)(const t_buffer_str *),
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
void vec_buf_str_iter(t_vec_buf_str *vec, void (*fn)(t_usize index,
|
void vec_buf_str_iter(t_vec_buf_str *vec,
|
||||||
t_buffer_str *value, void *state), void *state)
|
void (*fn)(t_usize index, t_buffer_str *value,
|
||||||
|
void *state),
|
||||||
|
void *state)
|
||||||
{
|
{
|
||||||
t_usize idx;
|
t_usize idx;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,15 +17,16 @@
|
||||||
#include "me/vec/vec_buf_str.h"
|
#include "me/vec/vec_buf_str.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
t_error vec_buf_str_push_front(t_vec_buf_str *vec, t_buffer_str element)
|
t_error vec_buf_str_push_front(t_vec_buf_str *vec,
|
||||||
|
t_buffer_str element)
|
||||||
{
|
{
|
||||||
t_usize i;
|
t_usize i;
|
||||||
|
|
||||||
if (vec->len == 0)
|
if (vec->len == 0)
|
||||||
return (vec_buf_str_push(vec, element));
|
return (vec_buf_str_push(vec, element));
|
||||||
i = vec->len - 1;
|
i = vec->len - 1;
|
||||||
if (vec->capacity < vec->len + 1 && vec_buf_str_reserve(vec, 3 * vec->len
|
if (vec->capacity < vec->len + 1 &&
|
||||||
/ 2 + 1))
|
vec_buf_str_reserve(vec, 3 * vec->len / 2 + 1))
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
while (i > 0)
|
while (i > 0)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
115
stdme/output/src/vec/vec_str.c
Normal file
115
stdme/output/src/vec/vec_str.c
Normal 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);
|
||||||
|
}
|
||||||
112
stdme/output/src/vec/vec_str_functions2.c
Normal file
112
stdme/output/src/vec/vec_str_functions2.c
Normal 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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
73
stdme/output/src/vec/vec_str_functions3.c
Normal file
73
stdme/output/src/vec/vec_str_functions3.c
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* 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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,7 +17,8 @@
|
||||||
#include "me/vec/vec_u8.h"
|
#include "me/vec/vec_u8.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
t_vec_u8 vec_u8_new(t_usize capacity, t_free_u8_item free_function)
|
t_vec_u8 vec_u8_new(t_usize capacity,
|
||||||
|
t_free_u8_item free_function)
|
||||||
{
|
{
|
||||||
t_vec_u8 out;
|
t_vec_u8 out;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,8 @@
|
||||||
#include "me/vec/vec_u8.h"
|
#include "me/vec/vec_u8.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
t_error vec_u8_find(t_vec_u8 *vec, bool (*fn)(const t_u8 *), t_usize *index)
|
t_error vec_u8_find(t_vec_u8 *vec,
|
||||||
|
bool (*fn)(const t_u8 *), t_usize *index)
|
||||||
{
|
{
|
||||||
t_usize idx;
|
t_usize idx;
|
||||||
|
|
||||||
|
|
@ -36,7 +37,8 @@ t_error vec_u8_find(t_vec_u8 *vec, bool (*fn)(const t_u8 *), t_usize *index)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error vec_u8_find_starting(t_vec_u8 *vec, bool (*fn)(const t_u8 *),
|
t_error vec_u8_find_starting(t_vec_u8 *vec,
|
||||||
|
bool (*fn)(const t_u8 *),
|
||||||
t_usize starting_index, t_usize *index)
|
t_usize starting_index, t_usize *index)
|
||||||
{
|
{
|
||||||
t_usize idx;
|
t_usize idx;
|
||||||
|
|
@ -56,7 +58,8 @@ t_error vec_u8_find_starting(t_vec_u8 *vec, bool (*fn)(const t_u8 *),
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error vec_u8_all(t_vec_u8 *vec, bool (*fn)(const t_u8 *), bool *result)
|
t_error vec_u8_all(t_vec_u8 *vec,
|
||||||
|
bool (*fn)(const t_u8 *), bool *result)
|
||||||
{
|
{
|
||||||
t_usize idx;
|
t_usize idx;
|
||||||
|
|
||||||
|
|
@ -73,7 +76,8 @@ t_error vec_u8_all(t_vec_u8 *vec, bool (*fn)(const t_u8 *), bool *result)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error vec_u8_any(t_vec_u8 *vec, bool (*fn)(const t_u8 *), bool *result)
|
t_error vec_u8_any(t_vec_u8 *vec,
|
||||||
|
bool (*fn)(const t_u8 *), bool *result)
|
||||||
{
|
{
|
||||||
t_usize idx;
|
t_usize idx;
|
||||||
|
|
||||||
|
|
@ -90,8 +94,10 @@ t_error vec_u8_any(t_vec_u8 *vec, bool (*fn)(const t_u8 *), bool *result)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
void vec_u8_iter(t_vec_u8 *vec, void (*fn)(t_usize index, t_u8 *value,
|
void vec_u8_iter(t_vec_u8 *vec,
|
||||||
void *state), void *state)
|
void (*fn)(t_usize index, t_u8 *value,
|
||||||
|
void *state),
|
||||||
|
void *state)
|
||||||
{
|
{
|
||||||
t_usize idx;
|
t_usize idx;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,15 +17,16 @@
|
||||||
#include "me/vec/vec_u8.h"
|
#include "me/vec/vec_u8.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
t_error vec_u8_push_front(t_vec_u8 *vec, t_u8 element)
|
t_error vec_u8_push_front(t_vec_u8 *vec,
|
||||||
|
t_u8 element)
|
||||||
{
|
{
|
||||||
t_usize i;
|
t_usize i;
|
||||||
|
|
||||||
if (vec->len == 0)
|
if (vec->len == 0)
|
||||||
return (vec_u8_push(vec, element));
|
return (vec_u8_push(vec, element));
|
||||||
i = vec->len - 1;
|
i = vec->len - 1;
|
||||||
if (vec->capacity < vec->len + 1 && vec_u8_reserve(vec, 3 * vec->len / 2
|
if (vec->capacity < vec->len + 1 &&
|
||||||
+ 1))
|
vec_u8_reserve(vec, 3 * vec->len / 2 + 1))
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
while (i > 0)
|
while (i > 0)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,10 @@ num/u64/from_bytes
|
||||||
num/u64/rotate
|
num/u64/rotate
|
||||||
num/u8/rotate
|
num/u8/rotate
|
||||||
num/usize/rotate
|
num/usize/rotate
|
||||||
|
os/pipe
|
||||||
|
os/process
|
||||||
|
os/process_inner
|
||||||
|
os/process_inner2
|
||||||
printf/formatter/char
|
printf/formatter/char
|
||||||
printf/formatter/decimal
|
printf/formatter/decimal
|
||||||
printf/formatter/hex
|
printf/formatter/hex
|
||||||
|
|
|
||||||
24
stdme/src/os/pipe.c
Normal file
24
stdme/src/os/pipe.c
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pipe.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/01/04 17:59:48 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/01/04 18:01:42 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "me/os/pipe.h"
|
||||||
|
|
||||||
|
t_error create_pipe(t_pipe *out)
|
||||||
|
{
|
||||||
|
t_file fds[2];
|
||||||
|
|
||||||
|
if (pipe(fds))
|
||||||
|
return (ERROR);
|
||||||
|
out->read = fds[0];
|
||||||
|
out->write = fds[1];
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
141
stdme/src/os/process.c
Normal file
141
stdme/src/os/process.c
Normal file
|
|
@ -0,0 +1,141 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* process.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/01/06 17:50:47 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "me/buffered_str/buf_str.h"
|
||||||
|
#include "me/os/pipe.h"
|
||||||
|
#include "me/os/process.h"
|
||||||
|
#include "me/string/str_find_chr.h"
|
||||||
|
#include "me/string/str_n_compare.h"
|
||||||
|
#include "me/string/str_split.h"
|
||||||
|
#include "me/types.h"
|
||||||
|
#include "me/vec/vec_str.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
bool find_path(const t_str *s);
|
||||||
|
bool find_null(const t_str *s);
|
||||||
|
bool str_start_with(t_const_str s, t_const_str prefix);
|
||||||
|
t_error handle_redirections(t_spawn_info *info, t_process *process);
|
||||||
|
|
||||||
|
t_error spawn_process_exec(t_spawn_info info, t_process *process)
|
||||||
|
{
|
||||||
|
bool res;
|
||||||
|
|
||||||
|
if (info.forked_free)
|
||||||
|
info.forked_free(info.forked_free_args);
|
||||||
|
dup2(info.stdin.vals.fd.value, 0);
|
||||||
|
dup2(info.stdout.vals.fd.value, 1);
|
||||||
|
dup2(info.stderr.vals.fd.value, 2);
|
||||||
|
close(process->stdin.vals.ro.fd);
|
||||||
|
close(process->stdout.vals.ro.fd);
|
||||||
|
close(process->stderr.vals.ro.fd);
|
||||||
|
close(info.stdin.vals.fd.value);
|
||||||
|
close(info.stdout.vals.fd.value);
|
||||||
|
close(info.stderr.vals.fd.value);
|
||||||
|
if (!vec_str_any(&info.arguments, find_null, &res) && res)
|
||||||
|
vec_str_push(&info.arguments, NULL);
|
||||||
|
res = false;
|
||||||
|
if (!vec_str_any(&info.environement, find_null, &res) && res)
|
||||||
|
vec_str_push(&info.environement, NULL);
|
||||||
|
execve(info.binary_path, info.arguments.buffer, info.environement.buffer);
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error in_path(t_spawn_info *info, t_process *process, t_const_str path,
|
||||||
|
t_buffer_str *s)
|
||||||
|
{
|
||||||
|
t_str *splitted_path;
|
||||||
|
t_usize sp_index;
|
||||||
|
|
||||||
|
splitted_path = str_split(path + 5, ':');
|
||||||
|
if (splitted_path == NULL)
|
||||||
|
return (str_free(*s), ERROR);
|
||||||
|
sp_index = 0;
|
||||||
|
while (splitted_path[sp_index])
|
||||||
|
{
|
||||||
|
((void)(process), str_clear(s));
|
||||||
|
push_str_buffer(s, splitted_path[sp_index]);
|
||||||
|
push_str_buffer(s, "/");
|
||||||
|
push_str_buffer(s, info->binary_path);
|
||||||
|
sp_index++;
|
||||||
|
if (access(s->buf, X_OK | R_OK) == 0)
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
sp_index = 0;
|
||||||
|
while (splitted_path[sp_index])
|
||||||
|
free(splitted_path[sp_index++]);
|
||||||
|
free(splitted_path);
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error find_binary(t_spawn_info *info, t_process *process)
|
||||||
|
{
|
||||||
|
t_usize p_idx;
|
||||||
|
t_str *splitted_path;
|
||||||
|
t_buffer_str s;
|
||||||
|
|
||||||
|
(void)(process);
|
||||||
|
splitted_path = NULL;
|
||||||
|
s = alloc_new_buffer(256);
|
||||||
|
if (str_start_with(info->binary_path, "/")
|
||||||
|
|| str_find_chr(info->binary_path, '/') != NULL)
|
||||||
|
push_str_buffer(&s, info->binary_path);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (vec_str_find(&info->environement, find_path, &p_idx))
|
||||||
|
return (str_free(s), ERROR);
|
||||||
|
if (in_path(info, process, info->environement.buffer[p_idx], &s))
|
||||||
|
return (ERROR);
|
||||||
|
}
|
||||||
|
if (access(s.buf, X_OK | R_OK) == 0)
|
||||||
|
{
|
||||||
|
free(info->binary_path);
|
||||||
|
info->binary_path = s.buf;
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
|
return (str_free(s), ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
|
||||||
|
{
|
||||||
|
if (cleanup_process && process->stdin.tag != INVALID)
|
||||||
|
close(process->stdin.vals.ro.fd);
|
||||||
|
if (cleanup_process && process->stdout.tag != INVALID)
|
||||||
|
close(process->stdout.vals.ro.fd);
|
||||||
|
if (cleanup_process && process->stderr.tag != INVALID)
|
||||||
|
close(process->stderr.vals.ro.fd);
|
||||||
|
close(info.stdin.vals.fd.value);
|
||||||
|
close(info.stdout.vals.fd.value);
|
||||||
|
close(info.stderr.vals.fd.value);
|
||||||
|
vec_str_free(info.arguments);
|
||||||
|
vec_str_free(info.environement);
|
||||||
|
free(info.binary_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error spawn_process(t_spawn_info info, t_process *process)
|
||||||
|
{
|
||||||
|
if (handle_redirections(&info, process))
|
||||||
|
return (cleanup(info, process, true), ERROR);
|
||||||
|
if (find_binary(&info, process))
|
||||||
|
return (cleanup(info, process, true), ERROR);
|
||||||
|
process->pid = fork();
|
||||||
|
if (process->pid == 0)
|
||||||
|
(spawn_process_exec(info, process), exit(1));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cleanup(info, process, false);
|
||||||
|
if (process->pid == -1)
|
||||||
|
return (printf("pid\n"), ERROR);
|
||||||
|
}
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
33
stdme/src/os/process_inner.c
Normal file
33
stdme/src/os/process_inner.c
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* process_inner.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/01/04 22:26:10 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "me/types.h"
|
||||||
|
|
||||||
|
bool find_null(const t_str *s)
|
||||||
|
{
|
||||||
|
return (s == NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool str_start_with(t_const_str s, t_const_str prefix)
|
||||||
|
{
|
||||||
|
while (*prefix && *s)
|
||||||
|
{
|
||||||
|
if (*prefix++ != *s++)
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
return (*prefix == '\0');
|
||||||
|
}
|
||||||
|
|
||||||
|
bool find_path(const t_str *s)
|
||||||
|
{
|
||||||
|
return (str_start_with(*s, "PATH="));
|
||||||
|
}
|
||||||
92
stdme/src/os/process_inner2.c
Normal file
92
stdme/src/os/process_inner2.c
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* process_inner2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/01/04 22:27:00 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/01/04 23:01:03 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "me/types.h"
|
||||||
|
#include "me/os/process.h"
|
||||||
|
#include "me/os/pipe.h"
|
||||||
|
|
||||||
|
void handle_redirections_second(t_spawn_info *info, t_process *process)
|
||||||
|
{
|
||||||
|
(void)(process);
|
||||||
|
if (info->stdin.tag == R_INHERITED)
|
||||||
|
{
|
||||||
|
info->stdin = fd(dup(0));
|
||||||
|
process->stdin = ro(dup(0));
|
||||||
|
}
|
||||||
|
if (info->stdout.tag == R_INHERITED)
|
||||||
|
{
|
||||||
|
info->stdout = fd(dup(1));
|
||||||
|
process->stdout = wo(dup(1));
|
||||||
|
}
|
||||||
|
if (info->stderr.tag == R_INHERITED)
|
||||||
|
{
|
||||||
|
info->stderr = fd(dup(2));
|
||||||
|
process->stderr = wo(dup(2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle_redirections_fds(t_spawn_info *info, t_process *process)
|
||||||
|
{
|
||||||
|
if (info->stdin.tag == R_FD)
|
||||||
|
{
|
||||||
|
info->stdin = fd(dup(info->stdin.vals.fd.value));
|
||||||
|
process->stdin = wo(dup(info->stdin.vals.fd.value));
|
||||||
|
}
|
||||||
|
if (info->stdout.tag == R_FD)
|
||||||
|
{
|
||||||
|
info->stdout = fd(dup(info->stdout.vals.fd.value));
|
||||||
|
process->stdout = ro(dup(info->stdout.vals.fd.value));
|
||||||
|
}
|
||||||
|
if (info->stderr.tag == R_FD)
|
||||||
|
{
|
||||||
|
info->stderr = fd(dup(info->stderr.vals.fd.value));
|
||||||
|
process->stderr = ro(dup(info->stderr.vals.fd.value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void redirection_inner(t_spawn_info *info, t_process *process)
|
||||||
|
{
|
||||||
|
process->stderr.tag = INVALID;
|
||||||
|
process->stdout.tag = INVALID;
|
||||||
|
process->stdin.tag = INVALID;
|
||||||
|
handle_redirections_fds(info, process);
|
||||||
|
handle_redirections_second(info, process);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error handle_redirections(t_spawn_info *info, t_process *process)
|
||||||
|
{
|
||||||
|
t_pipe pipe_fd;
|
||||||
|
|
||||||
|
redirection_inner(info, process);
|
||||||
|
if (info->stdin.tag == R_PIPED)
|
||||||
|
{
|
||||||
|
if (create_pipe(&pipe_fd))
|
||||||
|
return (ERROR);
|
||||||
|
process->stdin = wo(pipe_fd.write);
|
||||||
|
info->stdin = fd(pipe_fd.read);
|
||||||
|
}
|
||||||
|
if (info->stdout.tag == R_PIPED)
|
||||||
|
{
|
||||||
|
if (create_pipe(&pipe_fd))
|
||||||
|
return (ERROR);
|
||||||
|
process->stdout = ro(pipe_fd.read);
|
||||||
|
info->stdout = fd(pipe_fd.write);
|
||||||
|
}
|
||||||
|
if (info->stderr.tag == R_PIPED)
|
||||||
|
{
|
||||||
|
if (create_pipe(&pipe_fd))
|
||||||
|
return (ERROR);
|
||||||
|
process->stderr = ro(pipe_fd.read);
|
||||||
|
info->stderr = fd(pipe_fd.write);
|
||||||
|
}
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue