aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libft_io.h15
-rw-r--r--src/io/ft_putnbr_base.c18
-rw-r--r--src/io/ft_putnbr_base_fd.c33
-rw-r--r--src/io/ft_read_fd.c36
-rw-r--r--src/io/ft_read_file.c33
5 files changed, 134 insertions, 1 deletions
diff --git a/include/libft_io.h b/include/libft_io.h
index 76bce17..869bb86 100644
--- a/include/libft_io.h
+++ b/include/libft_io.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/31 10:35:43 by cacharle #+# #+# */
-/* Updated: 2020/02/14 02:23:43 by cacharle ### ########.fr */
+/* Updated: 2020/02/22 10:36:44 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,9 +15,14 @@
# include <unistd.h>
# include <stdlib.h>
+# include <fcntl.h>
# include <limits.h>
# include "libft.h"
+/*
+** output
+*/
+
void ft_putendl(char *s);
void ft_putchar(char c);
void ft_putstr(char const *s);
@@ -26,8 +31,16 @@ void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
void ft_putendl_fd(char *s, int fd);
void ft_putnbr_fd(int n, int fd);
+void ft_putnbr_base(int n, char *base);
+void ft_putnbr_base_fd(int n, char *base, int fd);
+
+/*
+** input
+*/
char ft_getchar(void);
+char *ft_read_fd(int fd);
+char *ft_read_file(char *filename);
# ifndef FTNL_BUFFER_SIZE
# define FTNL_BUFFER_SIZE 32
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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}