aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-10-11 14:30:20 +0200
committerCharles <sircharlesaze@gmail.com>2019-10-11 14:30:20 +0200
commite0abc6358c4cacc840ac8b3a9a92900f8ce49f0a (patch)
tree72d0cdedd11b10b593cf7547716a9e2e82f5c378
parentc24fb630859217dbf8cce9f834fe1c4a058d17a0 (diff)
downloadget_next_line-e0abc6358c4cacc840ac8b3a9a92900f8ce49f0a.tar.gz
get_next_line-e0abc6358c4cacc840ac8b3a9a92900f8ce49f0a.tar.bz2
get_next_line-e0abc6358c4cacc840ac8b3a9a92900f8ce49f0a.zip
Rewrite with dynamic buffer
- ft_strappend: ft_strcat with resize - still malloc issues in ft_strappend
-rw-r--r--get_next_line.c83
-rw-r--r--get_next_line.h15
-rw-r--r--get_next_line_utils.c70
3 files changed, 124 insertions, 44 deletions
diff --git a/get_next_line.c b/get_next_line.c
index 6098cec..94ef3d8 100644
--- a/get_next_line.c
+++ b/get_next_line.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/08 10:37:41 by cacharle #+# #+# */
-/* Updated: 2019/10/11 11:21:32 by cacharle ### ########.fr */
+/* Updated: 2019/10/11 14:29:11 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,45 +23,68 @@
** at each stack pop store local buf on the allocated line
*/
-int get_next_line(int fd, char **line)
+int get_next_line(int fd, char **line)
{
int ret;
int split_at;
- char local_buf[BUFFER_SIZE + 1];
- static int line_len = 0;
- static char rest_buf[BUFFER_SIZE + 1] = {0};
+ char buf[BUFFER_SIZE + 1];
+ static char *rest = NULL;
- if (fd < 0 || line == NULL
- || (ret = read_after(fd, local_buf, rest_buf)) == -1)
+ if (fd < 0 || line == NULL)
return (ERROR);
- split_at = find_newline(local_buf);
- if (split_at != -1 || ret == 0)
+ while ((ret = read(fd, buf, BUFFER_SIZE)) > 0 || !empty_rest(rest))
{
- if ((*line = malloc(sizeof(char) * (line_len + split_at + 1))) == NULL)
+ buf[ret] = 0;
+ rest = ft_strappend(rest, buf);
+ if ((split_at = find_newline(rest)) == -1 && ret == 0)
+ split_at = ft_strlen(rest);
+ if (split_at == -1)
+ continue ;
+ if ((*line = (char*)malloc(sizeof(char) * (split_at + 1))) == NULL)
return (ERROR);
- ft_strncpy(rest_buf, local_buf + split_at + 1,
- ft_strlen(local_buf) - split_at);
- ft_strncpy(*line + line_len, local_buf, split_at);
- return (ret == 0 && local_buf[0] == 0 ? END_OF_FILE : LINE_READ);
+ ft_strncpy(*line, rest, split_at);
+ (*line)[split_at] = '\0';
+ return ((rest = shift_rest(rest, split_at)) == NULL
+ ? ERROR : LINE_READ);
}
- line_len += BUFFER_SIZE;
- if ((ret = get_next_line(fd, line)) == -1)
- return (ERROR);
- line_len -= BUFFER_SIZE;
- ft_strncpy(*line + line_len, local_buf, BUFFER_SIZE);
- return (ret);
+ free(rest);
+ rest = NULL;
+ return (ret == -1 ? ERROR : END_OF_FILE);
}
-int read_after(int fd, char *local_buf, char *rest_buf)
+char *shift_rest(char *rest, int split_at)
{
- int offset;
- int ret;
+ char *copy;
+ int len;
- offset = ft_strlen(rest_buf);
- ft_strncpy(local_buf, rest_buf, offset);
- rest_buf[0] = '\0';
- if ((ret = read(fd, local_buf + offset, BUFFER_SIZE - offset)) == -1)
- return (ERROR);
- local_buf[offset + ret] = '\0';
- return (ret);
+ len = ft_strlen(rest) - split_at + 1;
+ if ((copy = (char*)malloc(sizeof(char) * len)) == NULL)
+ return (NULL);
+ ft_strncpy(copy, rest + split_at + 1, len);
+ free(rest);
+ if ((rest = (char*)malloc(sizeof(char) * len)) == NULL)
+ return (NULL);
+ ft_strncpy(rest, copy, len);
+ free(copy);
+ return (rest);
+}
+
+int find_newline(char *str)
+{
+ int i;
+
+ i = -1;
+ while (str[++i])
+ if (str[i] == '\n')
+ return (i);
+ return (-1);
+}
+
+t_bool empty_rest(char *rest)
+{
+ if (rest == NULL)
+ return (TRUE);
+ if (rest[0] == 0)
+ return (TRUE);
+ return (FALSE);
}
diff --git a/get_next_line.h b/get_next_line.h
index 650330f..9f7aabd 100644
--- a/get_next_line.h
+++ b/get_next_line.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/09 13:52:59 by cacharle #+# #+# */
-/* Updated: 2019/10/11 11:22:20 by cacharle ### ########.fr */
+/* Updated: 2019/10/11 14:27:22 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,19 +21,28 @@
# define END_OF_FILE 0
# define ERROR -1
+# define TRUE 1
+# define FALSE 0
+
+typedef int t_bool;
+
/*
** get_next_line.c
*/
int get_next_line(int fd, char **line);
-int read_after(int fd, char *buf, char *rest_buf);
+char *shift_rest(char *rest, int split_at);
+int find_newline(char *str);
+t_bool empty_rest(char *rest);
/*
** get_next_line_utils.c - helper functions
*/
-int find_newline(char *str);
char *ft_strncpy(char *dest, const char *src, int n);
int ft_strlen(char *str);
+char *ft_strcat(char *dest, const char *src);
+char *ft_strcpy(char *dest, const char *src);
+char *ft_strappend(char *dest, char *src);
#endif
diff --git a/get_next_line_utils.c b/get_next_line_utils.c
index 8fbc78a..e024c12 100644
--- a/get_next_line_utils.c
+++ b/get_next_line_utils.c
@@ -6,20 +6,12 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/08 08:52:59 by cacharle #+# #+# */
-/* Updated: 2019/10/11 09:50:44 by cacharle ### ########.fr */
+/* Updated: 2019/10/11 14:29:34 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
-int find_newline(char *str)
-{
- int i;
-
- i = -1;
- while (str[++i])
- if (str[i] == '\n')
- return (i);
- return (-1);
-}
+#include <stdlib.h>
+#include "get_next_line.h"
char *ft_strncpy(char *dest, const char *src, int n)
{
@@ -45,3 +37,59 @@ int ft_strlen(char *str)
counter++;
return (counter);
}
+
+char *ft_strappend(char *dest, char *src)
+{
+ void *copy;
+
+ if (dest == NULL)
+ {
+ if ((dest = (char*)malloc(sizeof(char) * (ft_strlen(src) + 1))) == NULL)
+ return (NULL);
+ ft_strcpy(dest, src);
+ return (dest);
+ }
+ if ((copy = (char*)malloc(sizeof(char) * (ft_strlen(dest) + 1))) == NULL)
+ return (NULL);
+ ft_strcpy(copy, dest);
+ /* free(dest); */
+ if ((dest = (char*)malloc(sizeof(char)
+ * (ft_strlen(dest) + ft_strlen(src) + 1))) == NULL)
+ return (NULL);
+ ft_strcpy(dest, copy);
+ free(copy);
+ ft_strcat(dest, src);
+ return (dest);
+}
+
+char *ft_strcpy(char *dest, const char *src)
+{
+ int i;
+
+ i = 0;
+ while (src[i])
+ {
+ dest[i] = src[i];
+ i++;
+ }
+ dest[i] = '\0';
+ return (dest);
+}
+
+char *ft_strcat(char *dest, const char *src)
+{
+ int i;
+ int j;
+
+ i = 0;
+ while (dest[i])
+ i++;
+ j = 0;
+ while (src[j])
+ {
+ dest[i + j] = src[j];
+ j++;
+ }
+ dest[i + j] = '\0';
+ return (dest);
+}