Skip to content

Commit

Permalink
all test ok
Browse files Browse the repository at this point in the history
  • Loading branch information
axelcacerest committed Sep 11, 2023
1 parent dce3248 commit fda232c
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 6 deletions.
1 change: 1 addition & 0 deletions 1char.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
4 changes: 4 additions & 0 deletions b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Hola como
estas, este es
un test para
el gnl
6 changes: 5 additions & 1 deletion get_next_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/02 07:52:22 by acaceres #+# #+# */
/* Updated: 2023/09/10 05:46:09 by acaceres ### ########.fr */
/* Updated: 2023/09/11 06:10:13 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,10 +19,14 @@ char *get_next_line(int fd)
{
static t_list *lst;
char *line;
char *tmp;

line = NULL;
tmp = NULL;
if (fd < 0 || BUFFER_SIZE < 1 || read(fd, 0, 0) == -1)
return (ft_lstclear(&lst, free), NULL);
if (BUFFER_SIZE == 1)
return (buff_size_1(fd, line, tmp));
create_list(fd, &lst);
line = set_line(&lst, line);
if (!line)
Expand Down
3 changes: 2 additions & 1 deletion get_next_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/10 05:27:38 by acaceres #+# #+# */
/* Updated: 2023/09/10 05:27:40 by acaceres ### ########.fr */
/* Updated: 2023/09/11 06:10:25 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,6 +30,7 @@ typedef struct s_list {
char *get_next_line(int fd);
void ft_lstclear(t_list **lst, void (*del)(void*));
void ft_lstadd_back(t_list **lst, t_list *new);
char *buff_size_1(int fd, char *line, char *tmp);
t_list *create_node(char *content);

#endif
7 changes: 6 additions & 1 deletion get_next_line_bonus.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/10 05:22:15 by acaceres #+# #+# */
/* Updated: 2023/09/10 05:23:42 by acaceres ### ########.fr */
/* Updated: 2023/09/11 06:11:04 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -47,6 +47,7 @@ static ssize_t line_len(t_list **lst)
ssize_t len;

len = 0;
tmp = NULL;
if (!*lst)
return (0);
tmp = *lst;
Expand Down Expand Up @@ -113,10 +114,14 @@ char *get_next_line(int fd)
{
static t_list *lst[FD_MAX];
char *line;
char *tmp;

line = NULL;
tmp = NULL;
if (fd < 0 || BUFFER_SIZE < 1 || read(fd, 0, 0) == -1)
return (ft_lstclear(&lst[fd], free), NULL);
if (BUFFER_SIZE == 1)
return (buff_size_1(fd, line, tmp));
create_list(fd, &lst[fd]);
line = set_line(&lst[fd], line);
if (!line)
Expand Down
3 changes: 2 additions & 1 deletion get_next_line_bonus.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/10 05:26:30 by acaceres #+# #+# */
/* Updated: 2023/09/11 04:04:32 by acaceres ### ########.fr */
/* Updated: 2023/09/11 06:11:17 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -33,6 +33,7 @@ typedef struct s_list {
char *get_next_line(int fd);
void ft_lstclear(t_list **lst, void (*del)(void*));
void ft_lstadd_back(t_list **lst, t_list *new);
char *buff_size_1(int fd, char *line, char *tmp);
t_list *create_node(char *content);

#endif
58 changes: 57 additions & 1 deletion get_next_line_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/02 07:58:06 by acaceres #+# #+# */
/* Updated: 2023/09/11 04:03:46 by acaceres ### ########.fr */
/* Updated: 2023/09/11 06:10:37 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -77,3 +77,59 @@ t_list *create_node(char *content)
node->content[i] = '\0';
return (node->len = i, node->next = NULL, node);
}

char *ft_strjoin_gnl(char const *s1, char const *s2)
{
char *new_str;
int i;
int s1_len;
int s2_len;

i = 0;
s1_len = 0;
s2_len = 0;
if (!s2)
return (0);
if (!s1)
s1 = "";
while (s1[s1_len])
s1_len++;
while (s2[s2_len])
s2_len++;
new_str = malloc(((s1_len + s2_len) + 1) * sizeof(char));
if (!new_str)
return (0);
while (*s1)
new_str[i++] = *s1++;
while (*s2)
new_str[i++] = *s2++;
return (new_str[i] = '\0', new_str);
}

char *buff_size_1(int fd, char *line, char *tmp)
{
char *buff;
int _r;

_r = 1;
buff = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buff)
return (NULL);
while (_r > 0)
{
_r = read(fd, buff, BUFFER_SIZE);
if (_r == -1)
return (free(buff), NULL);
buff[_r] = '\0';
tmp = line;
line = ft_strjoin_gnl(line, buff);
if (!line)
return (free(buff), NULL);
free(tmp);
if (buff[0] == '\n')
break ;
}
if (line[0] == '\0')
return (free(buff), free(line), NULL);
return (free(buff), line);
}
59 changes: 58 additions & 1 deletion get_next_line_utils_bonus.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/10 05:24:19 by acaceres #+# #+# */
/* Updated: 2023/09/11 04:03:54 by acaceres ### ########.fr */
/* Updated: 2023/09/11 06:16:10 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -77,3 +77,60 @@ t_list *create_node(char *content)
node->content[i] = '\0';
return (node->len = i, node->next = NULL, node);
}

char *ft_strjoin_gnl(char const *s1, char const *s2)
{
char *new_str;
int i;
int s1_len;
int s2_len;

i = 0;
s1_len = 0;
s2_len = 0;
if (!s2)
return (0);
if (!s1)
s1 = "";
while (s1[s1_len])
s1_len++;
while (s2[s2_len])
s2_len++;
new_str = malloc(((s1_len + s2_len) + 1) * sizeof(char));
if (!new_str)
return (0);
while (*s1)
new_str[i++] = *s1++;
while (*s2)
new_str[i++] = *s2++;
return (new_str[i] = '\0', new_str);
}

char *buff_size_1(int fd, char *line, char *tmp)
{
char *buff;
int _r;

_r = 1;
buff = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buff)
return (NULL);
while (_r > 0)
{
_r = read(fd, buff, BUFFER_SIZE);
if (_r == -1)
return (free(buff), NULL);
buff[_r] = '\0';
tmp = line;
line = ft_strjoin_gnl(tmp, buff);
if (!line)
return (free(buff), NULL);
free(tmp);
if (buff[0] == '\n')
break ;
}
free(buff);
if (line[0] == '\0')
return (free(line), NULL);
return (line);
}
32 changes: 32 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/11 12:59:41 by acaceres #+# #+# */
/* Updated: 2023/09/11 06:13:48 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */

#include "get_next_line_bonus.h"
#include <stdio.h>
#include <fcntl.h>

int main(void)
{
int fd;
char *line;

fd = open("b.txt", O_RDONLY);
while (1)
{
line = get_next_line(fd);
printf("line: %s", line);
free(line);
if (!line)
return (0);
}
return (0);
}

0 comments on commit fda232c

Please sign in to comment.