Adding the norm for maiboyerlpb lib

This commit is contained in:
Raphael (rparodi) 2024-07-30 16:15:01 +02:00
parent b693536a90
commit 41589a8a42
16 changed files with 97 additions and 2376 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,27 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* close.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/04 15:56:56 by maiboyer #+# #+# */
/* Updated: 2023/12/10 19:05:48 by maix ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/close.h"
#include "me/types.h"
#include <unistd.h>
bool me_close(int file, t_i32 *error)
{
t_i32 res;
bool out;
res = close(file);
out = res != 0;
if (res && error != NULL)
*error = res;
return (out);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/19 15:53:50 by maiboyer #+# #+# */
/* Updated: 2024/07/07 19:17:05 by maiboyer ### ########.fr */
/* Updated: 2024/07/30 16:05:20 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,17 +21,17 @@
#include <stdio.h>
#include <sys/stat.h>
t_fd_array *get_fd_arrays(void)
t_fd_array *get_fd_arrays(void)
{
static t_fd_array val = {};
static t_fd_array val = {};
return (&val);
}
struct s_file_slot *get_unused_fd_slot(void)
struct s_file_slot *get_unused_fd_slot(void)
{
t_usize i;
t_fd_array *arr;
t_fd_array *arr;
arr = get_fd_arrays();
i = 0;
@ -46,10 +46,10 @@ struct s_file_slot *get_unused_fd_slot(void)
return (NULL);
}
__attribute__((destructor(201))) void close_all_slots(void)
__attribute__((destructor(201))) void close_all_slots(void)
{
t_usize i;
t_fd_array *arr;
t_fd_array *arr;
arr = get_fd_arrays();
i = 0;
@ -57,10 +57,10 @@ __attribute__((destructor(201))) void close_all_slots(void)
close_slot(&arr->storage[i++]);
}
void close_slot(struct s_file_slot *slot)
void close_slot(struct s_file_slot *slot)
{
if (slot == NULL)
return;
return ;
if (slot->ty == SLOT_UNUSED)
;
else if (slot->ty == SLOT_FD)
@ -82,11 +82,11 @@ void close_slot(struct s_file_slot *slot)
|_| |_____/
*/
t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
t_file_perm file_perm)
t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
t_file_perm file_perm)
{
t_fd *fd;
struct s_file_slot *slot;
t_fd *fd;
struct s_file_slot *slot;
int actual_perms;
if (perms & FD_READ && perms & FD_WRITE)
@ -110,14 +110,14 @@ t_fd *open_fd(t_str name, t_fd_perm perms, t_file_open_option open_options,
return (fd);
}
t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count)
t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count)
{
t_isize ret;
t_isize false_ret;
t_isize ret;
t_isize false_ret;
if (read_count == NULL)
read_count = &false_ret;
if (fd == NULL || buffer == NULL || fd->fd == -1 ||
if (fd == NULL || buffer == NULL || fd->fd == -1 || \
!(fd->perms & FD_READ))
return (ERROR);
ret = read(fd->fd, buffer, size);
@ -127,10 +127,10 @@ t_error read_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *read_count)
return (NO_ERROR);
}
t_error write_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *write_count)
t_error write_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *write_count)
{
t_isize ret;
t_isize fake_ret;
t_isize ret;
t_isize fake_ret;
if (write_count == NULL)
write_count = &fake_ret;
@ -143,7 +143,7 @@ t_error write_fd(t_fd *fd, t_u8 *buffer, t_usize size, t_isize *write_count)
return (NO_ERROR);
}
t_error stat_fd(t_fd *fd, t_stat *stat)
t_error stat_fd(t_fd *fd, t_stat *stat)
{
if (fd == NULL || stat == NULL || fd->fd == -1)
return (ERROR);
@ -152,24 +152,24 @@ t_error stat_fd(t_fd *fd, t_stat *stat)
return (NO_ERROR);
}
void close_fd(t_fd *fd)
void close_fd(t_fd *fd)
{
struct s_file_slot *slot;
struct s_file_slot *slot;
if (fd == NULL)
return;
return ;
if (close(fd->fd) == -1)
return;
return ;
slot = (void *)(fd)-offsetof(struct s_file_slot, slot.fd);
mem_set_zero(slot, sizeof(*slot));
}
#define INLINE_BUFFER_SIZE 128
void put_number_fd(t_fd *fd, t_u64 number)
void put_number_fd(t_fd *fd, t_u64 number)
{
t_u8 buffer[INLINE_BUFFER_SIZE];
t_usize i;
t_usize i;
i = 0;
while (number > 0)
@ -181,12 +181,12 @@ void put_number_fd(t_fd *fd, t_u64 number)
write_fd(fd, &buffer[--i], 1, NULL);
}
void put_string_fd(t_fd *fd, t_const_str string)
void put_string_fd(t_fd *fd, t_const_str string)
{
write_fd(fd, (t_u8 *)string, str_len(string), NULL);
}
void put_char_fd(t_fd *fd, t_u8 c)
void put_char_fd(t_fd *fd, t_u8 c)
{
write_fd(fd, &c, 1, NULL);
}
@ -199,10 +199,10 @@ void put_char_fd(t_fd *fd, t_u8 c)
|_____/_____|_| \_\______\_____| |_| \____/|_| \_\ |_|
*/
t_error open_dir(t_str name, t_dir **dir)
t_error open_dir(t_str name, t_dir **dir)
{
t_dir *out;
struct s_file_slot *slot;
t_dir *out;
struct s_file_slot *slot;
slot = get_unused_fd_slot();
out = &slot->slot.dir;
@ -215,9 +215,9 @@ t_error open_dir(t_str name, t_dir **dir)
return (NO_ERROR);
}
t_error read_dir(t_dir *dir, t_dir_entry *out)
t_error read_dir(t_dir *dir, t_dir_entry *out)
{
struct dirent *entry;
struct dirent *entry;
if (dir == NULL || out == NULL || dir->ptr == NULL)
return (ERROR);
@ -229,14 +229,14 @@ t_error read_dir(t_dir *dir, t_dir_entry *out)
return (NO_ERROR);
}
void close_dir(t_dir *dir)
void close_dir(t_dir *dir)
{
struct s_file_slot *slot;
struct s_file_slot *slot;
if (dir == NULL)
return;
return ;
if (closedir(dir->ptr) == -1)
return;
return ;
slot = (void *)(dir)-offsetof(struct s_file_slot, slot.dir);
mem_set_zero(slot, sizeof(*slot));
}
@ -249,10 +249,10 @@ void close_dir(t_dir *dir)
|_| |_____|______|______|
*/
t_error open_file(t_str name, t_mode mode, t_file **file)
t_error open_file(t_str name, t_mode mode, t_file **file)
{
t_file *out;
struct s_file_slot *slot;
t_file *out;
struct s_file_slot *slot;
slot = get_unused_fd_slot();
out = &slot->slot.file;
@ -265,11 +265,11 @@ t_error open_file(t_str name, t_mode mode, t_file **file)
return (NO_ERROR);
}
t_error write_file(t_file *file, t_u8 *buffer, t_usize size,
t_isize *write_count)
t_error write_file(t_file *file, t_u8 *buffer, t_usize size,
t_isize *write_count)
{
t_isize ret;
t_isize fake_ret;
t_isize ret;
t_isize fake_ret;
if (write_count == NULL)
write_count = &fake_ret;
@ -282,11 +282,11 @@ t_error write_file(t_file *file, t_u8 *buffer, t_usize size,
return (NO_ERROR);
}
t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
{
t_isize ret;
t_isize ret;
if (file == NULL || buffer == NULL || read_count == NULL ||
if (file == NULL || buffer == NULL || read_count == NULL || \
file->ptr == NULL)
return (ERROR);
ret = fread(buffer, size, 1, file->ptr);
@ -296,14 +296,14 @@ t_error read_file(t_file *file, t_u8 *buffer, t_usize size, t_isize *read_count)
return (NO_ERROR);
}
void close_file(t_file *file)
void close_file(t_file *file)
{
struct s_file_slot *slot;
struct s_file_slot *slot;
if (file == NULL)
return;
return ;
if (fclose(file->ptr) == -1)
return;
return ;
slot = (void *)(file)-offsetof(struct s_file_slot, slot.file);
mem_set_zero(slot, sizeof(*slot));
}
@ -316,11 +316,11 @@ void close_file(t_file *file)
\_____|______| |_| |_| |______|_| \_\_____/
*/
t_fd *get_stdin(void)
t_fd *get_stdin(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{
@ -335,11 +335,11 @@ t_fd *get_stdin(void)
return (value);
}
t_fd *get_stdout(void)
t_fd *get_stdout(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{
@ -354,11 +354,11 @@ t_fd *get_stdout(void)
return (value);
}
t_fd *get_stderr(void)
t_fd *get_stderr(void)
{
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
t_fd *out;
struct s_file_slot *slot;
static t_fd *value = NULL;
if (value == NULL)
{

View file

@ -1,60 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* open.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 15:29:38 by maiboyer #+# #+# */
/* Updated: 2024/07/10 17:43:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/open.h"
#include <fcntl.h>
t_error me_open(t_const_str path, bool read, bool write, int *file_out)
{
int out;
int flags;
flags = 0;
if (read && write)
flags = O_RDWR;
else if (read)
flags = O_RDONLY;
else if (write)
flags = O_WRONLY;
out = open(path, flags, 0666);
if (out < 0)
return (ERROR);
*file_out = out;
return (NO_ERROR);
}
t_error me_open_truncate(t_const_str path, int *file_out)
{
int out;
int flags;
unlink(path);
flags = O_WRONLY | O_CREAT | O_TRUNC;
out = open(path, flags, 0666);
if (out < 0)
return (ERROR);
*file_out = out;
return (NO_ERROR);
}
t_error me_open_create(t_const_str path, int *file_out)
{
int out;
int flags;
flags = O_WRONLY | O_CREAT | O_APPEND;
out = open(path, flags, 0666);
if (out < 0)
return (ERROR);
*file_out = out;
return (NO_ERROR);
}

View file

@ -1,20 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */
/* Updated: 2023/11/08 13:22:51 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/putchar_fd.h"
#include "me/fs/write.h"
#include "me/str/str.h"
void me_putchar_fd(char chr, int file)
{
me_write(file, (t_u8 *)&chr, 1);
}

View file

@ -1,23 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */
/* Updated: 2023/11/10 16:23:27 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/putstr_fd.h"
#include "me/fs/write.h"
#include "me/str/str.h"
void me_putendl_fd(t_str str, int file)
{
if (str == NULL)
return ;
me_write(file, (t_u8 *)str, str_len(str));
me_write(file, (t_u8 *)"\n", 1);
}

View file

@ -1,54 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 12:45:06 by maiboyer #+# #+# */
/* Updated: 2024/05/01 20:29:59 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/putnbr_fd.h"
#include "me/fs/write.h"
static void me_inner(t_u64 nb, t_str out, t_usize *idx)
{
bool need_print;
t_u64 modulus;
char c;
modulus = 1000000000;
need_print = false;
while (modulus)
{
c = (char)(nb / modulus);
if (c != 0 || need_print)
{
out[(*idx)++] = c + '0';
need_print = true;
}
nb = nb % modulus;
modulus /= 10;
}
}
void me_putnbr_fd(t_i32 n, int file)
{
t_usize idx;
t_i64 nb;
char out[15];
nb = (t_i64)n;
idx = 0;
if (nb < 0)
{
out[idx++] = '-';
nb = -nb;
}
if (nb == 0)
out[idx++] = '0';
me_inner(nb, out, &idx);
me_write(file, (t_u8 *)out, idx);
}

View file

@ -1,22 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 04:42:45 by maiboyer #+# #+# */
/* Updated: 2023/11/10 16:23:44 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/putstr_fd.h"
#include "me/fs/write.h"
#include "me/str/str.h"
void me_putstr_fd(t_str str, int file)
{
if (str == NULL)
return ;
me_write(file, (t_u8 *)str, str_len(str));
}

View file

@ -1,24 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 15:21:19 by maiboyer #+# #+# */
/* Updated: 2023/12/09 18:08:10 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/read.h"
#include <unistd.h>
t_usize me_read(int fd, t_u8 *buffer, t_i64 buffer_max, bool *eof_out)
{
ssize_t out;
out = read(fd, buffer, buffer_max);
if (out == 0 && buffer_max != 0 && eof_out != NULL)
*eof_out = true;
return (out);
}

View file

@ -1,43 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_to_vec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/24 18:38:47 by maiboyer #+# #+# */
/* Updated: 2024/07/10 17:44:22 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/open.h"
#include "me/fs/read.h"
#include "me/mem/mem.h"
#include "me/vec/vec_u8.h"
#define READ_BUFFER_SIZE 4096
bool read_to_vec(t_const_str path, t_vec_u8 *out)
{
t_u8 temp_buffer[READ_BUFFER_SIZE];
t_isize read_amount;
int f;
bool eof;
t_usize current_size;
eof = false;
current_size = 0;
if (out == NULL || me_open(path, true, false, &f))
return (true);
*out = vec_u8_new(READ_BUFFER_SIZE, NULL);
while (!eof)
{
read_amount = me_read(f, temp_buffer, READ_BUFFER_SIZE, &eof);
if (read_amount < 0)
return (true);
vec_u8_reserve(out, current_size + (t_usize)read_amount);
mem_copy(&out->buffer[out->len], temp_buffer, (t_usize)read_amount);
out->len += (t_usize)read_amount;
}
return (false);
}

View file

@ -1,19 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* write.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 15:27:33 by maiboyer #+# #+# */
/* Updated: 2023/11/03 15:44:38 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/write.h"
#include <unistd.h>
bool me_write(int fd, t_u8 *buffer, t_i64 size)
{
return (write(fd, buffer, size) < 0);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 11:08:03 by maiboyer #+# #+# */
/* Updated: 2024/07/10 17:54:01 by maiboyer ### ########.fr */
/* Updated: 2024/07/30 16:09:47 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,8 +24,8 @@
# define BASE_PATH "/no_base_path_defined/"
#endif
#if defined(PRINT_BACKTRACE) || defined(BACKTRACE_DEEP) || true
// #if defined(PRINT_BACKTRACE) || defined(BACKTRACE_DEEP) || true
#if true // TO_REMOVE
# ifndef BACKTRACE_DEEP
# define BACKTRACE_DEEP 256
# endif

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/07/22 15:29:22 by maiboyer ### ########.fr */
/* Updated: 2024/07/30 16:08:20 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -58,9 +58,9 @@ t_error in_path(t_spawn_info *info, t_process *process, t_const_str path_raw,
{
t_vec_str path;
t_usize idx;
(void)(process);
if (str_split(path_raw + 5, ":", &path))
if (str_split(path_raw + 5, ":", &path))
return (string_free(*s), ERROR);
idx = 0;
while (idx < path.len)

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 17:57:04 by maiboyer #+# #+# */
/* Updated: 2024/07/18 13:21:06 by maiboyer ### ########.fr */
/* Updated: 2024/07/30 16:10:07 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,7 @@
#include <stdio.h>
#include <stdlib.h>
t_i32 _atoi_printf(t_const_str s);
t_i32 _atoi_printf(t_const_str s);
bool handle_atoi_stuff(t_const_str fmt, t_usize *c_idx, t_usize *nxt,
t_printf_arg *c_arg)

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 18:06:15 by maiboyer #+# #+# */
/* Updated: 2024/07/18 13:20:35 by maiboyer ### ########.fr */
/* Updated: 2024/07/30 16:11:00 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -53,25 +53,25 @@ void print_sign_if_needed(t_pad_and_stuff_args a, t_printf_arg d,
f(a.sign, a.sign_len, d.p_args);
}
t_i32 _atoi_printf(t_const_str str)
t_i32 _atoi_printf(t_const_str str)
{
t_u64 out;
t_u64 sign;
t_usize i;
t_u64 out;
t_u64 sign;
t_usize i;
out = 0;
i = 0;
sign = 1;
while (me_isspace(str[i]))
i++;
if (str[i] == '+' || str[i] == '-')
if (str[i++] == '-')
sign = -1;
while (me_isdigit(str[i]))
{
out *= 10;
out += str[i] - '0';
i++;
}
return ((t_i32)(out * sign));
out = 0;
i = 0;
sign = 1;
while (me_isspace(str[i]))
i++;
if (str[i] == '+' || str[i] == '-')
if (str[i++] == '-')
sign = -1;
while (me_isdigit(str[i]))
{
out *= 10;
out += str[i] - '0';
i++;
}
return ((t_i32)(out * sign));
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/17 15:56:59 by maiboyer #+# #+# */
/* Updated: 2024/07/22 15:11:14 by maiboyer ### ########.fr */
/* Updated: 2024/07/30 16:11:22 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,11 +15,11 @@
#include "me/vec/vec_str.h"
#include <stdlib.h>
t_error str_split(t_const_str str, t_const_str chr, t_vec_str *out)
t_error str_split(t_const_str str, t_const_str chr, t_vec_str *out)
{
t_vec_str ret;
t_usize idx;
t_string buf;
t_vec_str ret;
t_usize idx;
t_string buf;
if (out == NULL || chr == NULL || str == NULL)
return (ERROR);