summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2024-03-25 14:20:51 +0100
committerDominik Kaiser2024-03-25 14:20:51 +0100
commitac2a1018fe004bf158025eacb6bf78c2bd3fce7a (patch)
treee0662c38967cc2c7e8716a36a324b7419bcfe524
parent33a83031dd615a342e2acf33cc134bb0a2a4acb8 (diff)
downloadget_next_line-ac2a1018fe004bf158025eacb6bf78c2bd3fce7a.tar.gz
get_next_line-ac2a1018fe004bf158025eacb6bf78c2bd3fce7a.zip
Refactor some more
Should be ready for eval now
-rw-r--r--get_next_line.c69
-rw-r--r--get_next_line.h6
-rw-r--r--get_next_line_utils.c32
3 files changed, 44 insertions, 63 deletions
diff --git a/get_next_line.c b/get_next_line.c
index 86451d8..4cfdf0f 100644
--- a/get_next_line.c
+++ b/get_next_line.c
@@ -6,45 +6,16 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 14:13:51 by dkaiser #+# #+# */
-/* Updated: 2024/03/25 13:01:16 by dkaiser ### ########.fr */
+/* Updated: 2024/03/25 14:20:22 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
-char *str_add_buffer(char *old_str, char *buf, int buf_len)
+int get_next_line_len(char *buf, int pos)
{
- char *result;
- int len;
- int i;
+ int len;
- if (!old_str)
- len = buf_len;
- else
- len = ft_strlen(old_str) + buf_len;
- result = malloc(sizeof(char) * (len + 1));
- if (!result)
- return (NULL);
- result[len] = '\0';
- i = 0;
- while (old_str && old_str[i])
- {
- result[i] = old_str[i];
- i++;
- }
- while (i < len)
- result[i++] = *(buf++);
- free(old_str);
- return (result);
-}
-
-void get_next_line_rec(int fd, char *buf, char **result, int pos)
-{
- int len;
- int i;
- int readlen;
-
- readlen = 0;
len = 0;
while (pos + len < BUFFER_SIZE)
{
@@ -57,24 +28,28 @@ void get_next_line_rec(int fd, char *buf, char **result, int pos)
break ;
len++;
}
+ return (len);
+}
+
+void get_next_line_rec(int fd, char *buf, char **result, int pos)
+{
+ int len;
+ int i;
+ int readlen;
+
+ len = get_next_line_len(buf, pos);
*result = str_add_buffer(*result, buf + pos, len);
if (pos + len == BUFFER_SIZE && buf[BUFFER_SIZE - 1] == '\n')
- {
- while (pos < BUFFER_SIZE)
- buf[pos++] = '\0';
- return ;
- }
- len = pos + len;
- while (pos < len)
- buf[pos++] = '\0';
- if (pos == BUFFER_SIZE && *result)
+ return (clear_buffer(buf, pos));
+ i = 0;
+ while (i < len)
+ buf[pos + i++] = '\0';
+ if (pos + len == BUFFER_SIZE && *result)
{
readlen = read(fd, buf, BUFFER_SIZE);
if (readlen < 0)
{
- i = 0;
- while (i < BUFFER_SIZE)
- buf[i++] = '\0';
+ clear_buffer(buf, 0);
free(*result);
*result = NULL;
return ;
@@ -100,11 +75,7 @@ char *get_next_line(int fd)
{
readlen = read(fd, buf, BUFFER_SIZE);
if (readlen < 0)
- {
- i = 0;
- while (i < BUFFER_SIZE)
- buf[i++] = '\0';
- }
+ clear_buffer(buf, 0);
else if (readlen > 0)
return (get_next_line(fd));
return (NULL);
diff --git a/get_next_line.h b/get_next_line.h
index f0b0006..63c477b 100644
--- a/get_next_line.h
+++ b/get_next_line.h
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 14:14:07 by dkaiser #+# #+# */
-/* Updated: 2024/03/22 12:04:28 by dkaiser ### ########.fr */
+/* Updated: 2024/03/25 14:19:27 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,6 +21,6 @@
int ft_strlen(const char *str);
char *get_next_line(int fd);
-char *str_realloc(char *str, size_t size);
-
+void clear_buffer(char *buf, int start);
+char *str_add_buffer(char *old_str, char *buf, int buf_len);
#endif // GET_NEXT_LINE_H
diff --git a/get_next_line_utils.c b/get_next_line_utils.c
index 690be2a..3f25bba 100644
--- a/get_next_line_utils.c
+++ b/get_next_line_utils.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 14:14:59 by dkaiser #+# #+# */
-/* Updated: 2024/03/25 11:53:28 by dkaiser ### ########.fr */
+/* Updated: 2024/03/25 14:18:50 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,24 +22,34 @@ int ft_strlen(const char *str)
return (len);
}
-char *str_realloc(char *str, size_t size)
+void clear_buffer(char *buf, int start)
+{
+ while (start < BUFFER_SIZE)
+ buf[start++] = '\0';
+}
+
+char *str_add_buffer(char *old_str, char *buf, int buf_len)
{
char *result;
- size_t i;
+ int len;
+ int i;
- result = malloc(size);
+ if (!old_str)
+ len = buf_len;
+ else
+ len = ft_strlen(old_str) + buf_len;
+ result = malloc(sizeof(char) * (len + 1));
if (!result)
- {
- free(str);
return (NULL);
- }
+ result[len] = '\0';
i = 0;
- while (str[i])
+ while (old_str && old_str[i])
{
- result[i] = str[i];
+ result[i] = old_str[i];
i++;
}
- while (i < size)
- result[i++] = '\0';
+ while (i < len)
+ result[i++] = *(buf++);
+ free(old_str);
return (result);
}