Started from buttom go to the sky

This commit is contained in:
Raphaël 2024-04-28 19:59:01 +02:00
parent 96215449bd
commit f811e55dea
4781 changed files with 10121 additions and 1743 deletions

27
stdme/src/fs/close.c Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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(t_file file, t_i32 *error)
{
t_i32 res;
bool out;
res = close(file);
out = res != 0;
if (res && error != NULL)
*error = res;
return (out);
}

60
stdme/src/fs/open.c Normal file
View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* open.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 15:29:38 by maiboyer #+# #+# */
/* Updated: 2024/01/06 18:19:11 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/open.h"
#include <fcntl.h>
t_error me_open(t_const_str path, bool read, bool write, t_file *file_out)
{
t_file 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, t_file *file_out)
{
t_file 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, t_file *file_out)
{
t_file 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);
}

20
stdme/src/fs/putchar_fd.c Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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/string/str_len.h"
void me_putchar_fd(char chr, t_file file)
{
me_write(file, (t_u8 *)&chr, 1);
}

23
stdme/src/fs/putendl_fd.c Normal file
View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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/string/str_len.h"
void me_putendl_fd(t_str str, t_file file)
{
if (str == NULL)
return ;
me_write(file, (t_u8 *)str, str_len(str));
me_write(file, (t_u8 *)"\n", 1);
}

54
stdme/src/fs/putnbr_fd.c Normal file
View file

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 12:45:06 by maiboyer #+# #+# */
/* Updated: 2024/04/28 19:42:10 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, t_file file)
{
t_usize idx;
t_u64 nb;
char out[15];
nb = (t_u64)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);
}

22
stdme/src/fs/putstr_fd.c Normal file
View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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/string/str_len.h"
void me_putstr_fd(t_str str, t_file file)
{
if (str == NULL)
return ;
me_write(file, (t_u8 *)str, str_len(str));
}

24
stdme/src/fs/read.c Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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(t_file 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

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_to_vec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/24 18:38:47 by maiboyer #+# #+# */
/* Updated: 2023/12/30 18:15:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/open.h"
#include "me/fs/read.h"
#include "me/mem/mem_copy.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;
t_file 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);
}

19
stdme/src/fs/write.c Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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(t_file fd, t_u8 *buffer, t_i64 size)
{
return (write(fd, buffer, size) < 0);
}