aboutsummaryrefslogtreecommitdiff
path: root/get_next_line.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-11-21 02:53:41 +0100
committerCharles <sircharlesaze@gmail.com>2019-11-21 04:03:13 +0100
commitee4b8e5e481850c936c5df10a0d3e70038234754 (patch)
tree5e569b3ebc2bdcafdbd06fef99f32207da29d4ee /get_next_line.c
parentafc8c70a66773563f6e7429b500abcbab631722b (diff)
downloadlibft-ee4b8e5e481850c936c5df10a0d3e70038234754.tar.gz
libft-ee4b8e5e481850c936c5df10a0d3e70038234754.tar.bz2
libft-ee4b8e5e481850c936c5df10a0d3e70038234754.zip
WIP: adding ft_*printf
Diffstat (limited to 'get_next_line.c')
-rw-r--r--get_next_line.c114
1 files changed, 57 insertions, 57 deletions
diff --git a/get_next_line.c b/get_next_line.c
index f7d9c0e..00aca8d 100644
--- a/get_next_line.c
+++ b/get_next_line.c
@@ -6,12 +6,67 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/19 09:08:36 by cacharle #+# #+# */
-/* Updated: 2019/11/04 00:00:16 by cacharle ### ########.fr */
+/* Updated: 2019/11/21 03:39:51 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
+#define HAS_NEWLINE(str, split_at) ((split_at = find_newline(str)) != -1)
+
+static int find_newline(char *str)
+{
+ int i;
+
+ i = -1;
+ while (str[++i])
+ if (str[i] == '\n')
+ return (i);
+ return (-1);
+}
+
+static int 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 read_line(int fd, char **line, char *rest)
+{
+ int ret;
+ int split_at;
+ char *buf;
+
+ if ((buf = malloc(sizeof(char) * (BUFFER_SIZE + 1))) == NULL)
+ return (free_return(line, NULL, STATUS_ERROR));
+ while ((ret = read(fd, buf, BUFFER_SIZE)) > 0)
+ {
+ buf[ret] = '\0';
+ if (HAS_NEWLINE(buf, split_at))
+ {
+ ft_strcpy(rest, buf + split_at + 1);
+ buf[split_at] = '\0';
+ if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL)
+ return (free_return(&buf, NULL, STATUS_ERROR));
+ return (free_return(&buf, NULL, STATUS_LINE));
+ }
+ if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL)
+ return (free_return(&buf, NULL, STATUS_ERROR));
+ }
+ if (ret == -1)
+ return (free_return(&buf, line, STATUS_ERROR));
+ return (free_return(&buf, NULL, ret));
+}
+
/*
** if has rest:
** if rest has newline:
@@ -30,9 +85,7 @@
** return GNL_EOF
*/
-#define HAS_NEWLINE(str, split_at) ((split_at = find_newline(str)) != -1)
-
-int get_next_line(int fd, char **line)
+int get_next_line(int fd, char **line)
{
int split_at;
static char rest[OPEN_MAX][BUFFER_SIZE + 1] = {{0}};
@@ -60,56 +113,3 @@ int get_next_line(int fd, char **line)
rest[fd][0] = '\0';
return (read_line(fd, line, rest[fd]));
}
-
-int read_line(int fd, char **line, char *rest)
-{
- int ret;
- int split_at;
- char *buf;
-
- if ((buf = malloc(sizeof(char) * (BUFFER_SIZE + 1))) == NULL)
- return (free_return(line, NULL, STATUS_ERROR));
- while ((ret = read(fd, buf, BUFFER_SIZE)) > 0)
- {
- buf[ret] = '\0';
- if (HAS_NEWLINE(buf, split_at))
- {
- ft_strcpy(rest, buf + split_at + 1);
- buf[split_at] = '\0';
- if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL)
- return (free_return(&buf, NULL, STATUS_ERROR));
- return (free_return(&buf, NULL, STATUS_LINE));
- }
- if ((*line = ft_strjoin_free(*line, buf, 1)) == NULL)
- return (free_return(&buf, NULL, STATUS_ERROR));
- }
- if (ret == -1)
- return (free_return(&buf, line, STATUS_ERROR));
- return (free_return(&buf, NULL, ret));
-}
-
-int find_newline(char *str)
-{
- int i;
-
- i = -1;
- while (str[++i])
- if (str[i] == '\n')
- return (i);
- return (-1);
-}
-
-int free_return(char **ptr, char **ptr2, int ret)
-{
- if (ptr != NULL)
- {
- free(*ptr);
- *ptr = NULL;
- }
- if (ptr2 != NULL)
- {
- free(*ptr2);
- *ptr2 = NULL;
- }
- return (ret);
-}