From ca68aa1e6fca81213d19431439ad0b31863fe10c Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 22 Feb 2020 10:37:27 +0100 Subject: Added ft_putnbr_base{_fd}, ft_read_fd, ft_read_file --- src/io/ft_putnbr_base.c | 18 ++++++++++++++++++ src/io/ft_putnbr_base_fd.c | 33 +++++++++++++++++++++++++++++++++ src/io/ft_read_fd.c | 36 ++++++++++++++++++++++++++++++++++++ src/io/ft_read_file.c | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 src/io/ft_putnbr_base.c create mode 100644 src/io/ft_putnbr_base_fd.c create mode 100644 src/io/ft_read_fd.c create mode 100644 src/io/ft_read_file.c (limited to 'src') diff --git a/src/io/ft_putnbr_base.c b/src/io/ft_putnbr_base.c new file mode 100644 index 0000000..878c538 --- /dev/null +++ b/src/io/ft_putnbr_base.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/22 09:52:45 by cacharle #+# #+# */ +/* Updated: 2020/02/22 09:53:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_io.h" + +void ft_putnbr_base(int n, char *base) +{ + ft_putnbr_base_fd(n, base, STDOUT_FILENO); +} diff --git a/src/io/ft_putnbr_base_fd.c b/src/io/ft_putnbr_base_fd.c new file mode 100644 index 0000000..ea8516e --- /dev/null +++ b/src/io/ft_putnbr_base_fd.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_base_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/22 09:48:23 by cacharle #+# #+# */ +/* Updated: 2020/02/22 09:53:41 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_io.h" + +void ft_putnbr_base_fd(int n, char *base, int fd) +{ + unsigned int p_n; + int radix; + + if (fd < 0 || fd > OPEN_MAX) + return ; + p_n = n; + if (n < 0) + { + ft_putchar_fd('-', fd); + p_n = -n; + } + radix = ft_strlen(base); + if (p_n > 9) + ft_putnbr_base_fd(p_n / radix, base, fd); + ft_putchar_fd(base[p_n % radix], fd); +} diff --git a/src/io/ft_read_fd.c b/src/io/ft_read_fd.c new file mode 100644 index 0000000..758216a --- /dev/null +++ b/src/io/ft_read_fd.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_read_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/22 10:26:44 by cacharle #+# #+# */ +/* Updated: 2020/02/22 10:36:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +#define FT_READ_FD_BUF_SIZE 2048 + +char *ft_read_fd(int fd) +{ + int ret; + char *file; + char buf[FT_READ_FD_BUF_SIZE + 1]; + + if ((file = ft_strdup("")) == NULL) + return (NULL); + while ((ret = read(fd, buf, FT_READ_FD_BUF_SIZE)) > 0) + { + buf[ret] = '\0'; + ft_strjoinf(file, buf, FT_STRJOINF_FST); + } + if (ret == -1) + { + free(file); + return (NULL); + } + return (file); +} diff --git a/src/io/ft_read_file.c b/src/io/ft_read_file.c new file mode 100644 index 0000000..90ee38d --- /dev/null +++ b/src/io/ft_read_file.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_read_file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/22 10:32:47 by cacharle #+# #+# */ +/* Updated: 2020/02/22 10:35:07 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_io.h" + +char *ft_read_file(char *filename) +{ + int fd; + char *file; + + if ((fd = open(filename, O_RDONLY)) < 0 || fd > OPEN_MAX) + return (NULL); + if ((file = ft_read_fd(fd)) == NULL) + { + free(file); + return (NULL); + } + if (close(fd) < 0) + { + free(file); + return (NULL); + } + return (file); +} -- cgit