-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jean Teissier
authored and
Jean Teissier
committed
May 31, 2024
1 parent
b7cde3d
commit 7ba3c28
Showing
5 changed files
with
292 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/25 14:12:35 by jteissie #+# #+# */ | ||
/* Updated: 2024/05/30 20:58:19 by jteissie ### ########.fr */ | ||
/* Updated: 2024/05/31 13:46:19 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -25,29 +25,29 @@ char *ft_str_rejoin(char *stash, char *add) | |
return (free(stash), joined); | ||
} | ||
|
||
char *fetch_line(char *stash, int fd, int *status) | ||
char *fetch_line(char *line_stash, int fd, int *status) | ||
{ | ||
char *read_buff; | ||
|
||
if (!stash) | ||
if (!line_stash) | ||
return (NULL); | ||
read_buff = ft_calloc(BUFFER_SIZE + 1, sizeof(char)); | ||
if (!read_buff) | ||
return (free(stash), NULL); | ||
while (!find_eol(stash)) | ||
return (free(line_stash), NULL); | ||
while (!find_eol(line_stash)) | ||
{ | ||
*status = read(fd, read_buff, BUFFER_SIZE); | ||
if (*status <= 0) | ||
{ | ||
free(read_buff); | ||
if (ft_strlen(stash) != 0) | ||
return (stash); | ||
return (free(stash), NULL); | ||
if (ft_strlen(line_stash) != 0) | ||
return (line_stash); | ||
return (free(line_stash), NULL); | ||
} | ||
stash = ft_str_rejoin(stash, read_buff); | ||
line_stash = ft_str_rejoin(line_stash, read_buff); | ||
ft_bzero(read_buff, BUFFER_SIZE + 1); | ||
} | ||
return (free(read_buff), stash); | ||
return (free(read_buff), line_stash); | ||
} | ||
|
||
void get_stash(char *stash, char *line) | ||
|
@@ -134,7 +134,7 @@ int main(int argc, char *argv[]) | |
int fd; | ||
char *line; | ||
(void)argc; | ||
fd = open(argv[1], O_RDONLY); | ||
line = get_next_line(fd); | ||
while (line) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,19 +6,19 @@ | |
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/25 14:11:27 by jteissie #+# #+# */ | ||
/* Updated: 2024/05/30 20:56:20 by jteissie ### ########.fr */ | ||
/* Updated: 2024/05/30 11:26:26 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
#ifndef GET_NEXT_LINE_H | ||
# define GET_NEXT_LINE_H | ||
# ifndef BUFFER_SIZE | ||
# define BUFFER_SIZE 1000000 | ||
# define BUFFER_SIZE 1 | ||
# endif | ||
# include <unistd.h> | ||
# include <stdlib.h> | ||
|
||
char *get_next_line(int fd); | ||
char *fetch_line(char *stash, int fd, int *status); | ||
char *fetch_line(char *line_stash, int fd, int *status); | ||
char *trim_buff(char *read_buff); | ||
void get_stash(char *stash, char *line); | ||
char *ft_str_rejoin(char *stash, char *append); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* get_next_line_bonus.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/25 14:12:35 by jteissie #+# #+# */ | ||
/* Updated: 2024/05/31 13:46:35 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "get_next_line_bonus.h" | ||
|
||
char *ft_str_rejoin(char *stash, char *add) | ||
{ | ||
char *joined; | ||
|
||
if (!stash) | ||
return (NULL); | ||
joined = ft_calloc((ft_strlen(stash) + ft_strlen(add) + 1), sizeof(char)); | ||
if (!joined) | ||
return (free(stash), NULL); | ||
copy_and_cat(joined, stash, add); | ||
return (free(stash), joined); | ||
} | ||
|
||
char *fetch_line(char *line_stash, int fd, int *status) | ||
{ | ||
char *read_buff; | ||
|
||
if (!line_stash) | ||
return (NULL); | ||
read_buff = ft_calloc(BUFFER_SIZE + 1, sizeof(char)); | ||
if (!read_buff) | ||
return (free(line_stash), NULL); | ||
while (!find_eol(line_stash)) | ||
{ | ||
*status = read(fd, read_buff, BUFFER_SIZE); | ||
if (*status <= 0) | ||
{ | ||
free(read_buff); | ||
if (ft_strlen(line_stash) != 0) | ||
return (line_stash); | ||
return (free(line_stash), NULL); | ||
} | ||
line_stash = ft_str_rejoin(line_stash, read_buff); | ||
ft_bzero(read_buff, BUFFER_SIZE + 1); | ||
} | ||
return (free(read_buff), line_stash); | ||
} | ||
|
||
void get_stash(char *stash, char *line) | ||
{ | ||
int i; | ||
int stash_i; | ||
|
||
i = 0; | ||
stash_i = 0; | ||
if (!line) | ||
return ; | ||
while (line[i]) | ||
{ | ||
if (line[i] == '\n') | ||
{ | ||
i++; | ||
break ; | ||
} | ||
i++; | ||
} | ||
while (line[i]) | ||
{ | ||
stash[stash_i] = line[i]; | ||
line[i] = '\0'; | ||
stash_i++; | ||
i++; | ||
} | ||
stash[stash_i] = '\0'; | ||
} | ||
|
||
char *trim_line(char *untrimmed) | ||
{ | ||
int i; | ||
char *trimmed; | ||
|
||
i = 0; | ||
if (!untrimmed) | ||
return (NULL); | ||
while (untrimmed[i]) | ||
i++; | ||
trimmed = ft_calloc(i + 1, sizeof(char)); | ||
if (!trimmed) | ||
return (free(untrimmed), NULL); | ||
i = 0; | ||
while (untrimmed[i]) | ||
{ | ||
trimmed[i] = untrimmed[i]; | ||
i++; | ||
} | ||
trimmed[i] = '\0'; | ||
return (free(untrimmed), trimmed); | ||
} | ||
|
||
char *get_next_line(int fd) | ||
{ | ||
static char stash[1024][BUFFER_SIZE + 1]; | ||
char *line; | ||
int status; | ||
|
||
status = 0; | ||
if (fd == -1 || BUFFER_SIZE <= 0) | ||
return (NULL); | ||
line = ft_calloc(1, sizeof(char)); | ||
if (ft_strlen(stash[fd])) | ||
line = ft_str_rejoin(line, stash[fd]); | ||
line = fetch_line(line, fd, &status); | ||
if (status == -1) | ||
{ | ||
ft_bzero(stash[fd], BUFFER_SIZE + 1); | ||
return (free(line), NULL); | ||
} | ||
get_stash(stash[fd], line); | ||
line = trim_line(line); | ||
if (!line) | ||
return (NULL); | ||
return (line); | ||
} | ||
|
||
// #include <fcntl.h> | ||
// #include <stdio.h> | ||
|
||
// int main(int argc, char *argv[]) | ||
// { | ||
// int fd; | ||
// int fd2; | ||
// int fd3; | ||
// char *line; | ||
// char *line2; | ||
// char *line3; | ||
// (void)argc; | ||
|
||
// fd = open(argv[1], O_RDONLY); | ||
// fd2 = open(argv[2], O_RDONLY); | ||
// fd3 = open(argv[3], O_RDONLY); | ||
// line = get_next_line(fd); | ||
// line2 = get_next_line(fd2); | ||
// line3 = get_next_line(fd3); | ||
// while (line && line2 && line3) | ||
// { | ||
// printf("%s", line); | ||
// printf("%s", line2); | ||
// printf("%s", line3); | ||
// free(line); | ||
// line = get_next_line(fd); | ||
// free(line2); | ||
// line2 = get_next_line(fd2); | ||
// free(line3); | ||
// line3 = get_next_line(fd3); | ||
// } | ||
// free(line); | ||
// free(line2); | ||
// close(fd); | ||
// return (0); | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* get_next_line_bonus.h :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/25 14:11:27 by jteissie #+# #+# */ | ||
/* Updated: 2024/05/31 13:23:14 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
#ifndef GET_NEXT_LINE_BONUS_H | ||
# define GET_NEXT_LINE_BONUS_H | ||
# ifndef BUFFER_SIZE | ||
# define BUFFER_SIZE 1 | ||
# endif | ||
# include <unistd.h> | ||
# include <stdlib.h> | ||
|
||
char *get_next_line(int fd); | ||
char *fetch_line(char *line_stash, int fd, int *status); | ||
char *trim_buff(char *read_buff); | ||
void get_stash(char *stash, char *line); | ||
char *ft_str_rejoin(char *stash, char *append); | ||
int ft_strlen(char *str); | ||
int find_eol(char *str); | ||
void *ft_calloc(size_t nmemb, size_t size); | ||
void ft_bzero(void *s, size_t n); | ||
void copy_and_cat(char *out, char *cpy_src, char *cat_src); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* get_next_line_utils_bonus.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/05/27 16:04:51 by jteissie #+# #+# */ | ||
/* Updated: 2024/05/31 13:12:34 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "get_next_line_bonus.h" | ||
|
||
int ft_strlen(char *str) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
while (str[i]) | ||
{ | ||
i++; | ||
} | ||
return (i); | ||
} | ||
|
||
void ft_bzero(void *s, size_t n) | ||
{ | ||
unsigned int i; | ||
unsigned char *str; | ||
|
||
i = 0; | ||
str = s; | ||
while (i < n) | ||
str[i++] = '\0'; | ||
} | ||
|
||
void copy_and_cat(char *out, char *cpy_src, char *cat_src) | ||
{ | ||
int i; | ||
int src_i; | ||
|
||
i = 0; | ||
src_i = 0; | ||
while (cpy_src[src_i]) | ||
{ | ||
out[i] = cpy_src[src_i]; | ||
i++; | ||
src_i++; | ||
} | ||
src_i = 0; | ||
while (cat_src[src_i]) | ||
out[i++] = cat_src[src_i++]; | ||
out[i] = '\0'; | ||
} | ||
|
||
void *ft_calloc(size_t nmemb, size_t size) | ||
{ | ||
void *ptr; | ||
size_t total_size; | ||
|
||
total_size = size * nmemb; | ||
if (total_size < size && total_size != 0) | ||
return (NULL); | ||
ptr = malloc(total_size); | ||
if (!ptr) | ||
return (NULL); | ||
ft_bzero(ptr, nmemb); | ||
return (ptr); | ||
} | ||
|
||
int find_eol(char *str) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
while (str[i]) | ||
{ | ||
if (str[i] == '\n') | ||
return (1); | ||
i++; | ||
} | ||
return (0); | ||
} |