aboutsummaryrefslogtreecommitdiff
path: root/libft/src/io
diff options
context:
space:
mode:
Diffstat (limited to 'libft/src/io')
-rw-r--r--libft/src/io/ft_getchar.c22
-rw-r--r--libft/src/io/ft_getline.c113
-rw-r--r--libft/src/io/ft_putchar.c18
-rw-r--r--libft/src/io/ft_putchar_fd.c20
-rw-r--r--libft/src/io/ft_putendl.c18
-rw-r--r--libft/src/io/ft_putendl_fd.c21
-rw-r--r--libft/src/io/ft_putnbr.c18
-rw-r--r--libft/src/io/ft_putnbr_fd.c30
-rw-r--r--libft/src/io/ft_putstr.c18
-rw-r--r--libft/src/io/ft_putstr_fd.c20
10 files changed, 298 insertions, 0 deletions
diff --git a/libft/src/io/ft_getchar.c b/libft/src/io/ft_getchar.c
new file mode 100644
index 0000000..9d233c0
--- /dev/null
+++ b/libft/src/io/ft_getchar.c
@@ -0,0 +1,22 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_getchar.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/18 10:29:54 by cacharle #+# #+# */
+/* Updated: 2020/02/14 02:24:56 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+char ft_getchar(void)
+{
+ char c;
+
+ if (read(STDIN_FILENO, &c, 1) < 0)
+ return (-1);
+ return (c);
+}
diff --git a/libft/src/io/ft_getline.c b/libft/src/io/ft_getline.c
new file mode 100644
index 0000000..d1f2330
--- /dev/null
+++ b/libft/src/io/ft_getline.c
@@ -0,0 +1,113 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_getline.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */
+/* Updated: 2020/06/19 17:46:24 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static int st_find_newline(char *str)
+{
+ int i;
+
+ i = -1;
+ while (str[++i])
+ if (str[i] == '\n')
+ return (i);
+ return (-1);
+}
+
+static int st_free_return(char **ptr, char **ptr2, int ret)
+{
+ if (ptr != NULL)
+ {
+ free(*ptr);
+ *ptr = NULL;
+ }
+ if (ptr2 != NULL)
+ {
+ free(*ptr2);
+ *ptr2 = NULL;
+ }
+ return (ret);
+}
+
+static int st_read_line(int fd, char **line, char *rest)
+{
+ int ret;
+ int split_at;
+ char *buf;
+
+ if ((buf = malloc(sizeof(char) * (FTGL_BUFFER_SIZE + 1))) == NULL)
+ return (st_free_return(line, NULL, FTGL_ERROR));
+ while ((ret = read(fd, buf, FTGL_BUFFER_SIZE)) > 0)
+ {
+ buf[ret] = '\0';
+ if ((split_at = st_find_newline(buf)) != -1)
+ {
+ ft_strcpy(rest, buf + split_at + 1);
+ buf[split_at] = '\0';
+ if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL)
+ return (st_free_return(&buf, NULL, FTGL_ERROR));
+ return (st_free_return(&buf, NULL, FTGL_OK));
+ }
+ if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL)
+ return (st_free_return(&buf, NULL, FTGL_ERROR));
+ }
+ if (ret == -1)
+ return (st_free_return(&buf, line, FTGL_ERROR));
+ return (st_free_return(&buf, NULL, ret));
+}
+
+/*
+** if has rest:
+** if rest has newline:
+** push rest until newline in line, shift rest
+** return LINE_READ
+** else:
+** push rest in line
+**
+** while can read fd in buf
+** if buf has newline:
+** push buf until newline in line
+** push buf after newline in rest
+** return LINE_READ
+** push buf in line
+**
+** return FTGL_EOF
+*/
+
+int ft_getline(int fd, char **line)
+{
+ int split_at;
+ static char rest[OPEN_MAX][FTGL_BUFFER_SIZE + 1] = {{0}};
+
+ if (fd < 0 || fd > OPEN_MAX || line == NULL || FTGL_BUFFER_SIZE <= 0)
+ return (FTGL_ERROR);
+ if ((*line = ft_strdup("")) == NULL)
+ return (FTGL_ERROR);
+ if (rest[fd][0] == '\0')
+ return (st_read_line(fd, line, rest[fd]));
+ if ((split_at = st_find_newline(rest[fd])) != -1)
+ {
+ free(*line);
+ if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL)
+ return (FTGL_ERROR);
+ ft_strncpy(*line, rest[fd], split_at);
+ (*line)[split_at] = '\0';
+ ft_strcpy(rest[fd], rest[fd] + split_at + 1);
+ return (FTGL_OK);
+ }
+ free(*line);
+ if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1))))
+ return (FTGL_ERROR);
+ ft_strcpy(*line, rest[fd]);
+ rest[fd][0] = '\0';
+ return (st_read_line(fd, line, rest[fd]));
+}
diff --git a/libft/src/io/ft_putchar.c b/libft/src/io/ft_putchar.c
new file mode 100644
index 0000000..2838f0a
--- /dev/null
+++ b/libft/src/io/ft_putchar.c
@@ -0,0 +1,18 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putchar.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/07 09:53:31 by cacharle #+# #+# */
+/* Updated: 2019/11/20 03:49:14 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void ft_putchar(char c)
+{
+ write(STDOUT_FILENO, &c, 1);
+}
diff --git a/libft/src/io/ft_putchar_fd.c b/libft/src/io/ft_putchar_fd.c
new file mode 100644
index 0000000..97d6f7a
--- /dev/null
+++ b/libft/src/io/ft_putchar_fd.c
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putchar_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/07 10:42:34 by cacharle #+# #+# */
+/* Updated: 2019/11/20 03:49:28 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void ft_putchar_fd(char c, int fd)
+{
+ if (fd < 0 || fd > OPEN_MAX)
+ return ;
+ write(fd, &c, 1);
+}
diff --git a/libft/src/io/ft_putendl.c b/libft/src/io/ft_putendl.c
new file mode 100644
index 0000000..880977e
--- /dev/null
+++ b/libft/src/io/ft_putendl.c
@@ -0,0 +1,18 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putendl.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/07 10:42:54 by cacharle #+# #+# */
+/* Updated: 2019/11/20 04:00:32 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void ft_putendl(char *s)
+{
+ ft_putendl_fd(s, STDOUT_FILENO);
+}
diff --git a/libft/src/io/ft_putendl_fd.c b/libft/src/io/ft_putendl_fd.c
new file mode 100644
index 0000000..a8077fc
--- /dev/null
+++ b/libft/src/io/ft_putendl_fd.c
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putendl_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/07 10:44:06 by cacharle #+# #+# */
+/* Updated: 2019/11/20 04:00:07 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void ft_putendl_fd(char *s, int fd)
+{
+ if (s == NULL || fd < 0 || fd > OPEN_MAX)
+ return ;
+ ft_putstr_fd(s, fd);
+ ft_putchar_fd('\n', fd);
+}
diff --git a/libft/src/io/ft_putnbr.c b/libft/src/io/ft_putnbr.c
new file mode 100644
index 0000000..247df40
--- /dev/null
+++ b/libft/src/io/ft_putnbr.c
@@ -0,0 +1,18 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putnbr.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/07 09:52:33 by cacharle #+# #+# */
+/* Updated: 2019/11/20 03:59:34 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void ft_putnbr(int n)
+{
+ ft_putnbr_fd(n, STDOUT_FILENO);
+}
diff --git a/libft/src/io/ft_putnbr_fd.c b/libft/src/io/ft_putnbr_fd.c
new file mode 100644
index 0000000..169d1b5
--- /dev/null
+++ b/libft/src/io/ft_putnbr_fd.c
@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putnbr_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/07 10:40:35 by cacharle #+# #+# */
+/* Updated: 2019/11/20 03:46:11 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void ft_putnbr_fd(int n, int fd)
+{
+ unsigned int p_n;
+
+ if (fd < 0 || fd > OPEN_MAX)
+ return ;
+ p_n = n;
+ if (n < 0)
+ {
+ ft_putchar_fd('-', fd);
+ p_n = -n;
+ }
+ if (p_n > 9)
+ ft_putnbr_fd(p_n / 10, fd);
+ ft_putchar_fd(p_n % 10 | 0x30, fd);
+}
diff --git a/libft/src/io/ft_putstr.c b/libft/src/io/ft_putstr.c
new file mode 100644
index 0000000..14b01a3
--- /dev/null
+++ b/libft/src/io/ft_putstr.c
@@ -0,0 +1,18 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putstr.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/07 09:52:12 by cacharle #+# #+# */
+/* Updated: 2019/11/20 03:48:48 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void ft_putstr(char const *s)
+{
+ ft_putstr_fd((char*)s, STDOUT_FILENO);
+}
diff --git a/libft/src/io/ft_putstr_fd.c b/libft/src/io/ft_putstr_fd.c
new file mode 100644
index 0000000..d0279ab
--- /dev/null
+++ b/libft/src/io/ft_putstr_fd.c
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_putstr_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/07 10:40:15 by cacharle #+# #+# */
+/* Updated: 2019/11/20 03:47:59 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void ft_putstr_fd(char *s, int fd)
+{
+ if (s == NULL || fd < 0 || fd > OPEN_MAX)
+ return ;
+ write(fd, s, ft_strlen(s));
+}