Skip to content

Commit

Permalink
added gnl
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeSinLiang committed Jan 11, 2024
1 parent 18e7d0d commit 9c9de2c
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ SRCS_B = ft_lstadd_back.c \
ft_lstlast.c \
ft_lstmap.c \
ft_lstnew.c \
ft_lstsize.c
ft_lstsize.c \
get_next_line.c \
get_next_line_utils.c


CFILES = $(addprefix $(SRCS_DIR), $(SRCS))
CFILES_B = $(addprefix $(SRCS_DIR), $(SRCS_B))

OBJS = ${CFILES:.c=.o}
OBJS_B = ${CFILES_B:.c=.o}
INCS = ./inc
INCS = ./includes
NAME = libft.a
LIBC = ar rcs
LIBR = ranlib
Expand Down
42 changes: 42 additions & 0 deletions includes/get_next_line.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sinlee <sinlee@student42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/25 15:53:31 by sinlee #+# #+# */
/* Updated: 2023/05/31 10:10:15 by sinlee ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef GET_NEXT_LINE_BONUS_H
# define GET_NEXT_LINE_BONUS_H

# include <unistd.h>
# include <stdlib.h>
# include <stdbool.h>
# include <fcntl.h>
# include <limits.h>
# include <string.h>

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

# ifndef BUFFER_SIZE
# define BUFFER_SIZE 5
# endif

size_t ft_strlen(const char *str);
char *ft_substr(char *s, unsigned int start, size_t len);
size_t ft_strlcpy(char *dest, const char *src, size_t size);
int ft_strchr(char *s, char c);
int ft_find_newline_pos(char *str);
char *get_next_line(int fd);
void print_debug(char *str);
char *ft_strjoin(char *s1, char *s2);

#endif
File renamed without changes.
57 changes: 57 additions & 0 deletions src/get_next_line.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "get_next_line.h"
#include <stdio.h>

char *extract_and_ret(char *str, int mode, char *remain_str)
{
int index;
char *extract;

index = ft_strchr(str, '\n');
if (index != -1 && index != BUFFER_SIZE - 1)
{
if (mode == 1 || mode == 2)
{
extract = ft_substr(str, index + 1, ft_strlen(str) + 1);
if (remain_str != NULL && ft_strlen(remain_str) > 0 && mode == 1)
extract = ft_strjoin(remain_str, extract);
free(remain_str);
return (extract);
}
else
return (ft_substr(str, 0, index + 1));
}
else if (ft_strlen(str) > 0 && mode == 0)
return (ft_substr(str, 0, ft_strlen(str) + 1));
else
{
if (mode == 2)
free(str);
return (NULL);
}
}

char *get_next_line(int fd)
{
int read_size;
char *buffer;
static char *remain[1024];
char *buff_all;

if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
read_size = 1;
buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
buff_all = extract_and_ret(remain[fd], 0, remain[fd]);
remain[fd] = extract_and_ret(remain[fd], 2, remain[fd]);
while (ft_strchr(buff_all, '\n') == -1 && read_size != 0)
{
read_size = read(fd, buffer, BUFFER_SIZE);
if (read_size <= 0)
break ;
buffer[read_size] = '\0';
buff_all = ft_strjoin(buff_all, extract_and_ret(buffer, 0, remain[fd]));
remain[fd] = extract_and_ret(buffer, 1, remain[fd]);
}
free(buffer);
return (buff_all);
}
98 changes: 98 additions & 0 deletions src/get_next_line_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "get_next_line.h"
#include <stdio.h>

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

len = 0;
if (str == 0)
return (0);
while (*str++)
len++;
return (len);
}

int ft_strchr(char *s, char c)
{
int i;

i = 0;
if (!s)
return (-1);
while (s[i])
{
if (s[i] == c)
return (i);
i++;
}
return (-1);
}

size_t ft_strlcpy(char *dest, const char *src, size_t size)
{
size_t src_len;
size_t i;

src_len = ft_strlen(src);
if (size == 0)
return (src_len);
i = 0;
while (i < size - 1 && src[i])
{
dest[i] = src[i];
++i;
}
dest[i] = '\0';
return (src_len);
}

char *ft_strjoin(char *s1, char *s2)
{
char *str;
int i;

i = -1;
str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!str)
return (0);
if (s1)
{
while (s1[++i])
str[i] = s1[i];
i = -1;
}
if (s2)
{
while (s2[++i])
str[ft_strlen(s1) + i] = s2[i];
}
free(s2);
str[ft_strlen(s1) + i] = '\0';
free(s1);
return (str);
}

char *ft_substr(char *s, unsigned int start, size_t len)
{
char *substr;

if (start > ft_strlen(s))
{
substr = malloc(sizeof(char) * 1);
substr[0] = '\0';
}
else
{
if (len > ft_strlen(s + start))
len = ft_strlen(s + start);
if (len == 0)
return (NULL);
substr = malloc(sizeof(char) * (len + 1));
if (substr == 0)
return (NULL);
else
ft_strlcpy(substr, s + start, len + 1);
}
return (substr);
}

0 comments on commit 9c9de2c

Please sign in to comment.