-
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
1 parent
8470c23
commit b69caea
Showing
3 changed files
with
245 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* get_next_line_bonus.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: tchow-so <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/11/27 09:23:49 by tchow-so #+# #+# */ | ||
/* Updated: 2024/01/03 11:26:11 by tchow-so ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
/* The bonus part of this project is about programming a function that returns | ||
a line read from a file descriptor, being able to handle multiple | ||
file descriptors at the same time. */ | ||
|
||
//#include <fcntl.h> | ||
#include "get_next_line_bonus.h" | ||
|
||
char *read_from_fd(char *str, int fd); | ||
char *ft_get_line(char *str); | ||
char *clear_line(char *str); | ||
|
||
char *get_next_line(int fd) | ||
{ | ||
static char *str[FOPEN_MAX]; | ||
char *line; | ||
|
||
if (fd < 0 || fd > FOPEN_MAX || BUFFER_SIZE <= 0) | ||
return (NULL); | ||
if (!str[fd]) | ||
{ | ||
str[fd] = malloc(1); | ||
str[fd][0] = '\0'; | ||
} | ||
str[fd] = read_from_fd(str[fd], fd); | ||
if (!str[fd]) | ||
return (NULL); | ||
line = ft_get_line(str[fd]); | ||
str[fd] = clear_line(str[fd]); | ||
return (line); | ||
} | ||
|
||
/* read BUFFER_SIZE bytes from file (identified by fd) */ | ||
char *read_from_fd(char *str, int fd) | ||
{ | ||
int bytes_read; | ||
char *buffer; | ||
|
||
buffer = malloc(sizeof(char) * (BUFFER_SIZE + 1)); | ||
if (!buffer) | ||
return (NULL); | ||
bytes_read = 1; | ||
while (!ft_strchr_gnl(str, '\n') && bytes_read != 0) | ||
{ | ||
bytes_read = read(fd, buffer, BUFFER_SIZE); | ||
if (bytes_read == -1) | ||
{ | ||
free(str); | ||
free(buffer); | ||
return (NULL); | ||
} | ||
buffer[bytes_read] = '\0'; | ||
str = ft_strjoin_gnl(str, buffer); | ||
} | ||
free(buffer); | ||
return (str); | ||
} | ||
|
||
/* get line from 'str' and terminate it */ | ||
char *ft_get_line(char *str) | ||
{ | ||
int i; | ||
char *line; | ||
|
||
i = 0; | ||
if (str[i] == '\0') | ||
return (NULL); | ||
while (str[i] && str[i] != '\n') | ||
i++; | ||
if (str[i] == '\0') | ||
line = malloc(i + 1); | ||
else | ||
line = malloc(i + 2); | ||
if (!line) | ||
return (NULL); | ||
i = 0; | ||
while (str[i] && str[i] != '\n') | ||
{ | ||
line[i] = str[i]; | ||
i++; | ||
} | ||
if (str[i] == '\n') | ||
line[i++] = '\n'; | ||
line[i] = '\0'; | ||
return (line); | ||
} | ||
|
||
/* clear previous line from 'str' */ | ||
char *clear_line(char *str) | ||
{ | ||
int i; | ||
int j; | ||
char *tmp; | ||
|
||
i = 0; | ||
while (str[i] && (str[i] != '\n')) | ||
i++; | ||
if (!str[i]) | ||
{ | ||
free(str); | ||
return (NULL); | ||
} | ||
tmp = malloc(sizeof(char) * (ft_strlen_gnl(str) - i + 1)); | ||
if (!tmp) | ||
{ | ||
free(tmp); | ||
return (NULL); | ||
} | ||
i++; | ||
j = 0; | ||
while (str[i]) | ||
tmp[j++] = str[i++]; | ||
tmp[j] = '\0'; | ||
free(str); | ||
return (tmp); | ||
} | ||
// PROGRAM MAIN FUNCTION (testing) | ||
/*int main(void) | ||
{ | ||
int fd0; | ||
int fd1; | ||
int fd2; | ||
char *str; | ||
fd0 = open("file.txt", O_RDONLY); | ||
fd1 = open("file1.txt", O_RDONLY); | ||
fd2 = open("file2.txt", O_RDONLY); | ||
str = get_next_line(fd0); | ||
printf("%s", str); | ||
free(str); | ||
str = get_next_line(fd1); | ||
printf("%s", str); | ||
free(str); | ||
str = get_next_line(fd2); | ||
printf("%s", str); | ||
free(str); | ||
str = get_next_line(fd0); | ||
printf("%s", str); | ||
free(str); | ||
close(fd0); | ||
close(fd1); | ||
close(fd2); | ||
return (0); | ||
}*/ | ||
// FOPEN_MAX | ||
/* This macro constant expands to an integral expression that represents the | ||
maximum number of files that can be opened simultaneously. */ |
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,29 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* get_next_line_bonus.h :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: tchow-so <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/11/27 09:00:03 by tchow-so #+# #+# */ | ||
/* Updated: 2024/01/03 10:20:28 by tchow-so ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef GET_NEXT_LINE_BONUS_H | ||
# define GET_NEXT_LINE_BONUS_H | ||
|
||
# ifndef BUFFER_SIZE | ||
# define BUFFER_SIZE 10 | ||
# endif | ||
|
||
# include <unistd.h> | ||
# include <stdlib.h> | ||
# include <stdio.h> | ||
|
||
char *get_next_line(int fd); | ||
int ft_strlen_gnl(char *str); | ||
char *ft_strchr_gnl(const char *str, int c); | ||
char *ft_strjoin_gnl(char *str, char *next_buffer); | ||
|
||
#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,58 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* get_next_line_utils_bonus.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: tchow-so <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/11/29 13:12:24 by tchow-so #+# #+# */ | ||
/* Updated: 2024/01/03 10:20:23 by tchow-so ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "get_next_line_bonus.h" | ||
|
||
/* calculate length of string */ | ||
int ft_strlen_gnl(char *str) | ||
{ | ||
int len; | ||
|
||
len = 0; | ||
while (str[len] != '\0') | ||
len++; | ||
return (len); | ||
} | ||
|
||
/* find char in string */ | ||
char *ft_strchr_gnl(const char *str, int c) | ||
{ | ||
while ((*str) && ((unsigned char)*str != (unsigned char)c)) | ||
str++; | ||
if ((unsigned char)*str == (unsigned char)c) | ||
return ((char *)str); | ||
return (NULL); | ||
} | ||
|
||
/* join two strings */ | ||
char *ft_strjoin_gnl(char *str, char *next_buffer) | ||
{ | ||
char *new_str; | ||
char *ptr_str; | ||
int len; | ||
int i; | ||
|
||
ptr_str = str; | ||
len = ft_strlen_gnl(str) + ft_strlen_gnl(next_buffer); | ||
new_str = malloc(sizeof(char) * (len + 1)); | ||
if (!new_str) | ||
return (NULL); | ||
i = 0; | ||
while (*str) | ||
new_str[i++] = *str++; | ||
while (*next_buffer) | ||
new_str[i++] = *next_buffer++; | ||
new_str[i] = '\0'; | ||
if (ptr_str) | ||
free(ptr_str); | ||
return (new_str); | ||
} |