/* 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)
{
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 ;
{
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);
/* 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 */
/* */
/* ************************************************************************** */
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
/* 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 */
/* */
/* ************************************************************************** */
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);
}