1
0

push something working

This commit is contained in:
2023-12-06 10:22:57 +01:00
parent 1f31c7fce6
commit 1946d4bc41
6 changed files with 80 additions and 57 deletions

View File

@ -6,13 +6,13 @@
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ # # By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/12/01 17:10:39 by adjoly #+# #+# # # Created: 2023/12/01 17:10:39 by adjoly #+# #+# #
# Updated: 2023/12/01 17:20:36 by adjoly ### ########.fr # # Updated: 2023/12/04 17:04:21 by adjoly ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
NAME = get_next_line.a NAME = get_next_line.a
CC = cc CC = clang
SRCS = get_next_line.c \ SRCS = get_next_line.c \
get_next_line_utils.c \ get_next_line_utils.c \
@ -21,13 +21,13 @@ OBJS = $(SRCS:.c=.o)
FLAGS = -Werror -Wall -Wextra FLAGS = -Werror -Wall -Wextra
HEADER = get_next_line.h \ HEADER = get_next_line.h
$(NAME): $(OBJS) $(NAME): $(OBJS)
ar -rcs $(NAME) $(OBJS) $(CC) -o $@ $(SRCS) -g
%.o: %.cc %.o: %.c
$(CC) $(FLAGS) $(HEADER) $< -c -o $@ $(CC) $(FLAGS) -I $(HEADER) $< -c -o $@ -g
all: $(NAME) all: $(NAME)

BIN
a.out

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */ /* Created: 2023/12/01 17:11:59 by adjoly #+# #+# */
/* Updated: 2023/12/01 23:03:24 by adjoly ### ########.fr */ /* Updated: 2023/12/04 18:44:34 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,78 +17,81 @@ int check_line(char *buf)
int i; int i;
i = 0; i = 0;
while (buf[i] || buf[i] != '\n') while (buf[i] && buf[i] != '\n')
i++; i++;
if (buf[i] == '\n') if (buf[i] == '\n')
return (1); return (i + (buf[i] == '\n'));
return (0); return (0);
} }
void read_line(int fd, char *buf)
{
char *tmp;
size_t read_nb;
while (!check_line(buf) && read_nb != 0)
{
tmp = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!tmp)
{
buf = NULL;
return ;
}
read_nb = read(fd, tmp, BUFFER_SIZE);
buf = ft_strjoin(buf, tmp);
free(tmp);
}
}
char *line_to_str(char *buf)
{
char *res;
size_t i;
size_t size_buf;
i = 0;
size_buf = ft_strlen_til_nl(buf);
res = malloc((size_buf + 1) * sizeof(char));
while (i < size_buf)
{
res[i] = buf[i];
if (buf[i] == '\n')
break ;
i++;
}
free (buf);
return (res);
}
char *get_next_line(int fd) char *get_next_line(int fd)
{ {
static char *buf; static char *buf;
char *res; char *res;
char *tmp;
int size;
int bytes_read;
if (BUFFER_SIZE <= 0 || read(fd, &res, 0) < 0 || fd < 0) if (BUFFER_SIZE <= 0 || fd < 0)
return (NULL); return (NULL);
read_line(fd, buf); res = malloc(1);
res = line_to_str(buf); if (!res)
return (NULL);
res[0] = '\0';
if (!buf)
{
buf = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (NULL);
buf[0] = '\0';
}
while (1)
{
tmp = ft_strjoin(res, buf);
if (!tmp)
return (NULL);
free(res);
res = tmp;
size = check_line(res);
bytes_read = read(fd, buf, BUFFER_SIZE);
buf[bytes_read] = 0;
if (bytes_read == 0 && ft_strlen(res) != 0)
{
free(buf);
buf = NULL;
return (res); return (res);
}
else if (bytes_read == 0)
return (NULL);
if (size != 0)
{
tmp = ft_strjoin(res, buf);
if (!tmp)
return (NULL);
free(res);
res = tmp;
ft_strlcpy(buf, &res[size], ft_strlen(&res[size]));
buf[ft_strlen(&res[size])] = 0;
res[size] = 0;
return (res);
}
}
} }
#include <unistd.h> #include <unistd.h>
void ft_putstr(char *str){if (str == NULL){return ;}int i = 0;while(str[i]){write(1, &str[i], 1);i++;}} void ft_putstr(char *str){if (str == NULL){return ;}int i = 0;while(str[i]){write(1, &str[i], 1);i++;}}
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
int main(void) int main(void)
{ {
char *ln; char *ln;
int fd; int fd;
fd = open("test.txt", O_RDONLY); fd = open("test.txt", O_RDONLY);
while (1) while (1)
{ {
ln = get_next_line(fd); ln = get_next_line(fd);
if (ln == NULL) if (ln == NULL)
break ; return (0);
ft_putstr(ln); ft_putstr(ln);
free(ln); free(ln);
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */ /* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
/* Updated: 2023/12/01 22:31:30 by adjoly ### ########.fr */ /* Updated: 2023/12/04 18:38:21 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,5 +24,6 @@ char *get_next_line(int fd);
char *ft_strjoin(char *s1, char *s2); char *ft_strjoin(char *s1, char *s2);
size_t ft_strlen_til_nl(char *s); size_t ft_strlen_til_nl(char *s);
size_t ft_strlen(char *s); size_t ft_strlen(char *s);
size_t ft_strlcpy(char *dst, char *src, size_t size);
#endif #endif

BIN
get_next_line.h.gch Normal file

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */ /* Created: 2023/12/01 17:12:02 by adjoly #+# #+# */
/* Updated: 2023/12/01 22:31:36 by adjoly ### ########.fr */ /* Updated: 2023/12/04 18:42:35 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,8 @@ size_t ft_strlen(char *s)
size_t i; size_t i;
i = 0; i = 0;
if (s[i] == 0)
return (0);
while (s[i]) while (s[i])
i++; i++;
return (i); return (i);
@ -59,3 +61,20 @@ char *ft_strjoin(char *s1, char *s2)
result[i] = '\0'; result[i] = '\0';
return (result); return (result);
} }
size_t ft_strlcpy(char *dst, char *src, size_t size)
{
size_t i;
i = 0;
if (size == 0)
return (ft_strlen(src));
while (i < size - 1 && src[i])
{
dst[i] = src[i];
i++;
}
if (i < size)
dst[i] = '\0';
return (ft_strlen(src));
}