Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
jellysg authored Mar 20, 2024
0 parents commit 81e9337
Show file tree
Hide file tree
Showing 9 changed files with 614 additions and 0 deletions.
133 changes: 133 additions & 0 deletions get_next_line.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "get_next_line.h"

void prepare_list_from_file(int fd, t_list **list)
{
char *buffer;
int num_bytes;

num_bytes = 1;
buffer = malloc(sizeof(char) * (BUFFER_SIZE + 1));
while (!found_newline(*list) && num_bytes != 0)
{
if (buffer == NULL)
return ;
num_bytes = read(fd, buffer, BUFFER_SIZE);
if ((*list == NULL && num_bytes == 0) || num_bytes == -1)
{
free(buffer);
if (num_bytes == -1)
free_list(*list);
*list = NULL;
return ;
}
buffer[num_bytes] = '\0';
append_buffer_to_list(list, buffer, num_bytes);
}
free(buffer);
}

void append_buffer_to_list(t_list **list, char *buffer, int num_bytes)
{
int i;
t_list *last;
t_list *new_node;

new_node = malloc(sizeof(t_list));
new_node->content = malloc(sizeof(char) * (num_bytes + 1));
if (new_node == NULL
|| new_node->content == NULL)
return ;
new_node->next = NULL;
i = 0;
while (buffer[i] && i < num_bytes)
{
new_node->content[i] = buffer[i];
i++;
}
new_node->content[i] = '\0';
if (*list == NULL)
*list = new_node;
else
{
last = ft_lstlast(*list);
last->next = new_node;
}
}

void read_line_from_list(t_list *list, char **line)
{
int i;
int j;

if (list == NULL)
return ;
prepare_line(line, list);
if (*line == NULL)
return ;
j = 0;
while (list)
{
i = 0;
while (list->content[i])
{
if (list->content[i] == '\n')
{
(*line)[j++] = list->content[i];
break ;
}
(*line)[j++] = list->content[i++];
}
list = list->next;
}
(*line)[j] = '\0';
}

t_list *remove_last_element(t_list **lst)
{
t_list *last;
t_list *clean_node;
int i;

clean_node = malloc(sizeof(t_list));
if (lst == NULL || clean_node == NULL)
{
*lst = clean_node;
return (NULL);
}
clean_node->next = NULL;
last = ft_lstlast(*lst);
i = 0;
while (last->content[i] && last->content[i] != '\n')
i++;
if (last->content[i] == '\n')
i++;
clean_node->content = remove_last_element_extra(last, i);
if (clean_node->content == NULL)
return (NULL);
free_list(*lst);
*lst = clean_node;
return (clean_node);
}

char *get_next_line(int fd)
{
static t_list *list = NULL;
char *line;

if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
line = NULL;
prepare_list_from_file(fd, &list);
if (list == NULL)
return (NULL);
read_line_from_list(list, &line);
remove_last_element(&list);
if (line[0] == '\0')
{
free_list(list);
list = NULL;
free(line);
return (NULL);
}
return (line);
}
30 changes: 30 additions & 0 deletions get_next_line.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H

# ifndef BUFFER_SIZE
# define BUFFER_SIZE 1
# endif

# include <stdlib.h>
# include <unistd.h>
# include <stdbool.h>
# include <fcntl.h>
# include <stdio.h>

typedef struct s_list
{
char *content;
struct s_list *next;
} t_list;

t_list *ft_lstlast(t_list *lst);
t_list *remove_last_element(t_list **lst);
char *remove_last_element_extra(t_list *last, int i);
char *get_next_line(int fd);
int ft_strlen(const char *str);
bool found_newline(t_list *list);
void free_list(t_list *list);
void append_buffer_to_list(t_list **list, char *buffer, int num_bytes);
void prepare_line(char **line, t_list *list);

#endif
133 changes: 133 additions & 0 deletions get_next_line_bonus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "get_next_line_bonus.h"

void prepare_list_from_file(int fd, t_list **list)
{
char *buffer;
int num_bytes;

num_bytes = 1;
buffer = malloc(sizeof(char) * (BUFFER_SIZE + 1));
while (!found_newline(*list) && num_bytes != 0)
{
if (buffer == NULL)
return ;
num_bytes = read(fd, buffer, BUFFER_SIZE);
if ((*list == NULL && num_bytes == 0) || num_bytes == -1)
{
free(buffer);
if (num_bytes == -1)
free_list(*list);
*list = NULL;
return ;
}
buffer[num_bytes] = '\0';
append_buffer_to_list(list, buffer, num_bytes);
}
free(buffer);
}

void append_buffer_to_list(t_list **list, char *buffer, int num_bytes)
{
int i;
t_list *last;
t_list *new_node;

new_node = malloc(sizeof(t_list));
new_node->content = malloc(sizeof(char) * (num_bytes + 1));
if (new_node == NULL
|| new_node->content == NULL)
return ;
new_node->next = NULL;
i = 0;
while (buffer[i] && i < num_bytes)
{
new_node->content[i] = buffer[i];
i++;
}
new_node->content[i] = '\0';
if (*list == NULL)
*list = new_node;
else
{
last = ft_lstlast(*list);
last->next = new_node;
}
}

void read_line_from_list(t_list *list, char **line)
{
int i;
int j;

if (list == NULL)
return ;
prepare_line(line, list);
if (*line == NULL)
return ;
j = 0;
while (list)
{
i = 0;
while (list->content[i])
{
if (list->content[i] == '\n')
{
(*line)[j++] = list->content[i];
break ;
}
(*line)[j++] = list->content[i++];
}
list = list->next;
}
(*line)[j] = '\0';
}

t_list *remove_last_element(t_list **lst)
{
t_list *last;
t_list *clean_node;
int i;

clean_node = malloc(sizeof(t_list));
if (lst == NULL || clean_node == NULL)
{
*lst = clean_node;
return (NULL);
}
clean_node->next = NULL;
last = ft_lstlast(*lst);
i = 0;
while (last->content[i] && last->content[i] != '\n')
i++;
if (last->content[i] == '\n')
i++;
clean_node->content = remove_last_element_extra(last, i);
if (clean_node->content == NULL)
return (NULL);
free_list(*lst);
*lst = clean_node;
return (clean_node);
}

char *get_next_line(int fd)
{
static t_list *list[4096];
char *line;

if (fd < 0 || BUFFER_SIZE <= 0 || fd >= 4096)
return (NULL);
line = NULL;
prepare_list_from_file(fd, &list[fd]);
if (list[fd] == NULL)
return (NULL);
read_line_from_list(list[fd], &line);
remove_last_element(&list[fd]);
if (line[0] == '\0')
{
free_list(list[fd]);
list[fd] = NULL;
free(line);
return (NULL);
}
return (line);
}
30 changes: 30 additions & 0 deletions get_next_line_bonus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef GET_NEXT_LINE_BONUS_H
# define GET_NEXT_LINE_BONUS_H

# ifndef BUFFER_SIZE
# define BUFFER_SIZE 5
# endif

# include <stdlib.h>
# include <unistd.h>
# include <stdbool.h>
# include <fcntl.h>
# include <stdio.h>

typedef struct s_list
{
char *content;
struct s_list *next;
} t_list;

t_list *ft_lstlast(t_list *lst);
t_list *remove_last_element(t_list **lst);
char *remove_last_element_extra(t_list *last, int i);
char *get_next_line(int fd);
int ft_strlen(const char *str);
bool found_newline(t_list *list);
void free_list(t_list *list);
void append_buffer_to_list(t_list **list, char *buffer, int num_bytes);
void prepare_line(char **line, t_list *list);

#endif
Loading

0 comments on commit 81e9337

Please sign in to comment.