aboutsummaryrefslogtreecommitdiff
path: root/src/io
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
commit5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde (patch)
tree80911dc3c32e9f230750e7e1042d413dfb6efab2 /src/io
parentee32953ea79616e72f5428cdf40c834714a891c9 (diff)
parentb96b82194ccad2cddbb46b77aa1962a57c47ff44 (diff)
downloadlibft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.gz
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.bz2
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.zip
Merge branch 'master' into ft_ssl
Diffstat (limited to 'src/io')
-rw-r--r--src/io/ft_getfile.c (renamed from src/io/ft_read_file.c)24
-rw-r--r--src/io/ft_getfile_fd.c40
-rw-r--r--src/io/ft_getline.c82
-rw-r--r--src/io/ft_next_line.c113
-rw-r--r--src/io/ft_printf/internals/list.c4
-rw-r--r--src/io/ft_printf/internals/parse.c2
-rw-r--r--src/io/ft_read_fd.c36
7 files changed, 134 insertions, 167 deletions
diff --git a/src/io/ft_read_file.c b/src/io/ft_getfile.c
index 90ee38d..6ae4833 100644
--- a/src/io/ft_read_file.c
+++ b/src/io/ft_getfile.c
@@ -1,33 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* ft_read_file.c :+: :+: :+: */
+/* ft_getfile.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 */
+/* Updated: 2020/08/02 11:01:06 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft_io.h"
-char *ft_read_file(char *filename)
+int ft_getfile(char *filename, t_ftmem *mem)
{
int fd;
- char *file;
- if ((fd = open(filename, O_RDONLY)) < 0 || fd > OPEN_MAX)
- return (NULL);
- if ((file = ft_read_fd(fd)) == NULL)
+ if ((fd = open(filename, O_RDONLY)) < 0)
+ return (-1);
+ if (ft_getfile_fd(fd, mem) < 0)
{
- free(file);
- return (NULL);
+ close(fd);
+ return (-1);
}
- if (close(fd) < 0)
- {
- free(file);
- return (NULL);
- }
- return (file);
+ return (close(fd));
}
diff --git a/src/io/ft_getfile_fd.c b/src/io/ft_getfile_fd.c
new file mode 100644
index 0000000..91ebf7f
--- /dev/null
+++ b/src/io/ft_getfile_fd.c
@@ -0,0 +1,40 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_getfile_fd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/05/11 09:52:32 by charles #+# #+# */
+/* Updated: 2020/08/02 10:55:46 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_io.h"
+
+/*
+** \brief Read a file in a memory buffer
+** \param fd File descriptor to read from
+** \param mem Pointer to mem struct (buffer and buffer size)
+** \return -1 on error, 0 otherwise
+*/
+
+int ft_getfile_fd(int fd, t_ftmem *mem)
+{
+ char buf[FT_GETFILE_BUFFER_SIZE];
+ int ret;
+
+ if (fd < 0 || fd > OPEN_MAX || mem == NULL
+ || (mem->data = malloc(1)) == NULL)
+ return (-1);
+ mem->size = 0;
+ while ((ret = read(fd, buf, FT_GETFILE_BUFFER_SIZE)) > 0)
+ {
+ if ((mem->data = ft_memjoinf1(mem->data, mem->size, buf, ret)) == NULL)
+ return (-1);
+ mem->size += ret;
+ }
+ if (ret == -1)
+ free(mem->data);
+ return (ret);
+}
diff --git a/src/io/ft_getline.c b/src/io/ft_getline.c
new file mode 100644
index 0000000..067e66c
--- /dev/null
+++ b/src/io/ft_getline.c
@@ -0,0 +1,82 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_getline.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */
+/* Updated: 2020/05/12 21:32:50 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static int st_read_line(int fd, char **line, char *rest)
+{
+ int ret;
+ char *cut;
+ static char buf[FT_GETLINE_BUFFER_SIZE + 1] = {'\0'};
+
+ while ((ret = read(fd, buf, FT_GETLINE_BUFFER_SIZE)) > 0)
+ {
+ buf[ret] = '\0';
+ if ((cut = ft_strchr(buf, '\n')) != NULL)
+ {
+ ft_strcpy(rest, cut + 1);
+ *cut = '\0';
+ }
+ if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL)
+ return (FT_ERROR);
+ if (cut != NULL)
+ return (FT_LINE);
+ }
+ if (ret == -1)
+ free(line);
+ return (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 FTNL_EOF
+*/
+
+int ft_getline(int fd, char **line)
+{
+ char *cut;
+ static char rest[OPEN_MAX][FT_GETLINE_BUFFER_SIZE + 1] = {{'\0'}};
+
+ if (fd < 0 || fd > OPEN_MAX || line == NULL)
+ return (FT_ERROR);
+ if (rest[fd][0] == '\0')
+ {
+ if ((*line = ft_strdup("")) == NULL)
+ return (FT_ERROR);
+ return (st_read_line(fd, line, rest[fd]));
+ }
+ if ((cut = ft_strchr(rest[fd], '\n')) != NULL)
+ {
+ *cut = '\0';
+ if ((*line = ft_strdup(rest[fd])) == NULL)
+ return (FT_ERROR);
+ ft_strmove(rest[fd], cut + 1);
+ return (FT_LINE);
+ }
+ if ((*line = ft_strdup(rest[fd])) == NULL)
+ return (FT_ERROR);
+ rest[fd][0] = '\0';
+ return (st_read_line(fd, line, rest[fd]));
+}
diff --git a/src/io/ft_next_line.c b/src/io/ft_next_line.c
deleted file mode 100644
index 0f4cc2c..0000000
--- a/src/io/ft_next_line.c
+++ /dev/null
@@ -1,113 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* ft_next_line.c :+: :+: :+: */
-/* +:+ +:+ +:+ */
-/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
-/* +#+#+#+#+#+ +#+ */
-/* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */
-/* Updated: 2020/02/14 03:38:01 by cacharle ### ########.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) * (FTNL_BUFFER_SIZE + 1))) == NULL)
- return (st_free_return(line, NULL, FTNL_STATUS_ERROR));
- while ((ret = read(fd, buf, FTNL_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, FTNL_STATUS_ERROR));
- return (st_free_return(&buf, NULL, FTNL_STATUS_LINE));
- }
- if ((*line = ft_strjoinf(*line, buf, FT_STRJOINF_FST)) == NULL)
- return (st_free_return(&buf, NULL, FTNL_STATUS_ERROR));
- }
- if (ret == -1)
- return (st_free_return(&buf, line, FTNL_STATUS_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 FTNL_EOF
-*/
-
-int ft_next_line(int fd, char **line)
-{
- int split_at;
- static char rest[OPEN_MAX][FTNL_BUFFER_SIZE + 1] = {{0}};
-
- if (fd < 0 || fd > OPEN_MAX || line == NULL || FTNL_BUFFER_SIZE <= 0)
- return (FTNL_STATUS_ERROR);
- if ((*line = ft_strdup("")) == NULL)
- return (FTNL_STATUS_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 (FTNL_STATUS_ERROR);
- ft_strncpy(*line, rest[fd], split_at);
- (*line)[split_at] = '\0';
- ft_strcpy(rest[fd], rest[fd] + split_at + 1);
- return (FTNL_STATUS_LINE);
- }
- free(*line);
- if (!(*line = (char*)malloc(sizeof(char) * (ft_strlen(rest[fd]) + 1))))
- return (FTNL_STATUS_ERROR);
- ft_strcpy(*line, rest[fd]);
- rest[fd][0] = '\0';
- return (st_read_line(fd, line, rest[fd]));
-}
diff --git a/src/io/ft_printf/internals/list.c b/src/io/ft_printf/internals/list.c
index 99491f4..37f8013 100644
--- a/src/io/ft_printf/internals/list.c
+++ b/src/io/ft_printf/internals/list.c
@@ -18,7 +18,7 @@ t_flist *list_new(t_pformat *content)
if ((lst = (t_flist*)malloc(sizeof(t_flist))) == NULL)
return (NULL);
- lst->content = content;
+ lst->data = content;
lst->next = NULL;
return (lst);
}
@@ -47,7 +47,7 @@ void list_pop_front(t_flist **lst)
if (lst == NULL || *lst == NULL)
return ;
tmp = (*lst)->next;
- free((*lst)->content);
+ free((*lst)->data);
free(*lst);
*lst = tmp;
}
diff --git a/src/io/ft_printf/internals/parse.c b/src/io/ft_printf/internals/parse.c
index 33928a0..4650481 100644
--- a/src/io/ft_printf/internals/parse.c
+++ b/src/io/ft_printf/internals/parse.c
@@ -28,7 +28,7 @@ int parse(const char *format, t_flist **flist)
if ((tmp = list_new(parsed)) == NULL)
return ((int)list_destroy(flist));
list_push_front(flist, tmp);
- format += (*flist)->content->fmt_len;
+ format += (*flist)->data->fmt_len;
}
*flist = list_reverse(*flist);
return (1);
diff --git a/src/io/ft_read_fd.c b/src/io/ft_read_fd.c
deleted file mode 100644
index 758216a..0000000
--- a/src/io/ft_read_fd.c
+++ /dev/null
@@ -1,36 +0,0 @@
-/* ************************************************************************** */
-/* */
-/* ::: :::::::: */
-/* 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);
-}