Skip to content

Commit

Permalink
finished but not pushed
Browse files Browse the repository at this point in the history
  • Loading branch information
loris committed Nov 1, 2023
1 parent f73ec9d commit 5faa6f3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 69 deletions.
Binary file renamed get_next_line → GNL
Binary file not shown.
23 changes: 10 additions & 13 deletions include/get_next_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
/* ::: :::::::: */
/* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgaume <lgaume@student.42lausanne.ch> +#+ +:+ +#+ */
/* By: lgaume <lgaume@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/29 03:47:35 by lgaume #+# #+# */
/* Updated: 2023/10/29 03:47:37 by lgaume ### ########.fr */
/* Created: 2023/11/01 03:36:57 by lgaume #+# #+# */
/* Updated: 2023/11/01 04:27:22 by lgaume ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H

# include <stdlib.h>

# include <sys/types.h>
# include <sys/uio.h>
# include <unistd.h>
# include "stdlib.h"
# include "unistd.h"

# ifndef BUFFER_SIZE
# define BUFFER_SIZE 5
Expand All @@ -27,17 +24,17 @@ typedef struct s_list
{
char *content;
struct s_list *next;
} t_list;
} t_list;

char *get_next_line(int fd);
int found_newline(t_list *stash);
t_list *ft_lst_get_last(t_list *stash);
void read_and_stash(int fd, t_list **stash);
void add_to_stash(t_list **stash, char *buf, int readed);
void add_to_stash(t_list **stash, char *buff, int readed);
t_list *lst_get_last(t_list *lst);
int found_newline(t_list *stash);
void extract_line(t_list *stash, char **line);
void generate_line(char **line, t_list *stash);
void clean_stash(t_list **stash);
int ft_strlen(const char *str);
int ft_strlen(char *s);
void free_stash(t_list *stash);

#endif
38 changes: 19 additions & 19 deletions src/get_next_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgaume <lgaume@student.42lausanne.ch> +#+ +:+ +#+ */
/* By: lgaume <lgaume@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/29 03:48:00 by lgaume #+# #+# */
/* Updated: 2023/10/29 03:48:01 by lgaume ### ########.fr */
/* Created: 2023/11/01 03:37:13 by lgaume #+# #+# */
/* Updated: 2023/11/01 04:38:46 by lgaume ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,15 +26,15 @@ char *get_next_line(int fd)
clean_stash(&stash);
if (!line[0])
{
free_stash(stash);
free(stash);
stash = NULL;
free(line);
return (NULL);
}
return (line);
}

/* Uses read() to add characteres to the stash */
/* Utilise read() pour ajouter des charachteres a la stash */

void read_and_stash(int fd, t_list **stash)
{
Expand All @@ -47,8 +47,8 @@ void read_and_stash(int fd, t_list **stash)
buff = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buff)
return ;
readed = (int)read(fd, buff, BUFFER_SIZE);
if ((!*stash && !readed) || readed == -1)
readed = read(fd, buff, BUFFER_SIZE);
if ((!(*stash) && !readed) || readed == -1)
{
free(buff);
return ;
Expand All @@ -59,13 +59,13 @@ void read_and_stash(int fd, t_list **stash)
}
}

/* Adds the content of the buffer to the end of the stash */
/* Ajoute le contenu du buffer a la stash */

void add_to_stash(t_list **stash, char *buff, int readed)
{
int i;
t_list *last;
t_list *new;
t_list *last;

new = malloc(sizeof(t_list));
if (!new)
Expand All @@ -81,17 +81,17 @@ void add_to_stash(t_list **stash, char *buff, int readed)
i++;
}
new->content[i] = '\0';
if (!*stash)
if (!(*stash))
*stash = new;
else
{
last = ft_lst_get_last(*stash);
last = lst_get_last(*stash);
last->next = new;
}
}

/* Extracts all characters from our stash and stores them in out line.
* stopping after the first \n it encounters */
/* Extrait tous les characteres de la stash and les stockent dans la
* ligne en s'arretant a la premiere occurence de \n. */

void extract_line(t_list *stash, char **line)
{
Expand All @@ -111,7 +111,7 @@ void extract_line(t_list *stash, char **line)
{
if (stash->content[i] == '\n')
{
(*line)[j++] = stash->content[i];
(*line)[j++] = '\n';
break ;
}
(*line)[j++] = stash->content[i++];
Expand All @@ -121,27 +121,27 @@ void extract_line(t_list *stash, char **line)
(*line)[j] = '\0';
}

/* This function clears the stash so only the characters that have not
* been returned at the end of get_next_line() remain in the STATIC stash */
/* Nettoie la stash pour que seuls les characteres qui n'ont pas
* ete retournes reste dans la STATIC stash */

void clean_stash(t_list **stash)
{
t_list *last;
t_list *clean;
t_list *last;
int i;
int j;

clean = malloc(sizeof(t_list));
if (!clean)
return ;
clean->next = NULL;
last = ft_lst_get_last(*stash);
last = lst_get_last(*stash);
i = 0;
while (last->content[i] && last->content[i] != '\n')
i++;
if (last->content && last->content[i] == '\n')
i++;
clean->content = malloc(sizeof(char) * (ft_strlen(last->content) - i + 1));
clean->content = malloc(sizeof(char) * (ft_strlen(last->content) - 1 + 1));
if (!clean->content)
return ;
j = 0;
Expand Down
59 changes: 25 additions & 34 deletions src/get_next_line_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgaume <lgaume@student.42lausanne.ch> +#+ +:+ +#+ */
/* By: lgaume <lgaume@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/29 03:48:09 by lgaume #+# #+# */
/* Updated: 2023/10/29 03:48:12 by lgaume ### ########.fr */
/* Created: 2023/11/01 03:37:22 by lgaume #+# #+# */
/* Updated: 2023/11/01 04:27:43 by lgaume ### ########.fr */
/* */
/* ************************************************************************** */

#include "../include/get_next_line.h"

/* Looks for a newline character in the given linked list. */
t_list *lst_get_last(t_list *stash)
{
t_list *lst;

lst = stash;
while (lst && lst->next)
lst = lst->next;
return (lst);
}

int found_newline(t_list *stash)
{
Expand All @@ -21,7 +29,7 @@ int found_newline(t_list *stash)

if (!stash)
return (0);
current = ft_lst_get_last(stash);
current = lst_get_last(stash);
i = 0;
while (current->content[i])
{
Expand All @@ -32,21 +40,6 @@ int found_newline(t_list *stash)
return (0);
}

/* Returns a pointer to the last element in the stash */

t_list *ft_lst_get_last(t_list *stash)
{
t_list *current;

current = stash;
while (current && current->next)
current = current->next;
return (current);
}

/* Calculates the number of chars in the current line,
* including the trailing \n if there is one, and allocates memoru. */

void generate_line(char **line, t_list *stash)
{
int i;
Expand All @@ -63,15 +56,23 @@ void generate_line(char **line, t_list *stash)
len++;
break ;
}
len++;
i++;
len++;
}
stash = stash ->next;
stash = stash->next;
}
*line = malloc(sizeof(char) * (len + 1));
*line = (char *)malloc(sizeof(char) * (len + 1));
}

/* Frees the entire stash. */
int ft_strlen(char *s)
{
int len;

len = 0;
while (s[len])
len++;
return (len);
}

void free_stash(t_list *stash)
{
Expand All @@ -87,13 +88,3 @@ void free_stash(t_list *stash)
current = next;
}
}

int ft_strlen(const char *str)
{
int len;

len = 0;
while (str[len])
len++;
return (len);
}
6 changes: 3 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgaume <lgaume@student.42lausanne.ch> +#+ +:+ +#+ */
/* By: lgaume <lgaume@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/29 03:48:45 by lgaume #+# #+# */
/* Updated: 2023/10/29 03:49:50 by lgaume ### ########.fr */
/* Updated: 2023/11/01 04:43:44 by lgaume ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -23,9 +23,9 @@ int main(void)
while (1)
{
line = get_next_line(fd);
printf("%s", line);
if (line == NULL)
break ;
printf("%s", line);
free(line);
}
return (0);
Expand Down

0 comments on commit 5faa6f3

Please sign in to comment.