-
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
9c69873
commit 71b7e57
Showing
5 changed files
with
255 additions
and
134 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 |
---|---|---|
@@ -1,67 +1,143 @@ | ||
// the buffer is treated as a C-style string | ||
void create_list(t_list **list, int fd) | ||
{ | ||
int char_read; //num of chars read by the read() function | ||
char *buf; //buffer for readin data from file | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* get_next_line.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: aantonie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/03/03 15:47:51 by aantonie #+# #+# */ | ||
/* Updated: 2024/03/09 15:37:24 by aantonie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
// scan line if '\n' is present | ||
// докато while цикъла търси newline char | ||
// аргумента е *list, защото функцияат ще търси в *list | ||
while (!found_newline(*list)) | ||
{ | ||
buf = malloc(BUFFER_SIZE + 1); | ||
if (NULL == buf) | ||
return ; | ||
#include "get_next_line.h" | ||
#include <stdio.h> | ||
|
||
char_read = read(fd, buf, BUFFER_SIZE);//reads data from a fd into the buffer | ||
if (char_read == 0) | ||
{ | ||
free(buf); | ||
return ; | ||
} | ||
buf[char_read] = '\0'; | ||
// Append the node | ||
append(list, buf); | ||
} | ||
} | ||
char *ft_free(char *buffer, char *buf) | ||
{ | ||
char *temp; | ||
|
||
temp = ft_strjoin(buffer, buf); | ||
return (temp); | ||
} | ||
|
||
/** | ||
* @brief 1. extracts the current line from the buffer | ||
* @brief 2. stops when it encounters an '\0' or '\n' | ||
* @brief 3. allocates memory for the esxtracted line | ||
* @brief 4. returns the exracted line | ||
* @note - ! The extracted line | ||
* @return pointer to the allocated memory containing the extracted line | ||
* @brief 5. Handle eol character - '\0': | ||
* - line[i++] = '\n' means we go to the next line | ||
*/ | ||
char *ft_extract_line(char *buffer) | ||
{ | ||
int i; | ||
char *line; | ||
|
||
/* | ||
Reads a line from a file descriptor and returns that line as a dynamically allocated string. | ||
*/ | ||
#include "get_next_line.h" | ||
i = 0; | ||
if (!buffer[i]) | ||
return (NULL); | ||
while (buffer[i] != '\0' && buffer[i] != '\n') | ||
i++; | ||
line = ft_calloc(i + 2, sizeof(char)); | ||
if (!line) | ||
return (free(buffer), NULL); | ||
i = 0; | ||
while (buffer[i] != '\0' && buffer[i] != '\n') | ||
{ | ||
line[i] = buffer[i]; | ||
i++; | ||
} | ||
if (buffer[i] != '\0' && buffer[i] == '\n') | ||
line[i++] = '\n'; | ||
return (line); | ||
} | ||
|
||
char *get_next_line(int fd) | ||
/** | ||
* @brief 1. skips the current line | ||
* @brief 2. extracts the next line from the buffer | ||
* @brief 3. stops at the end of the buffer or the next '\n' | ||
* @brief 4. allocates memory for the extracted line | ||
* @brief 5. frees the memory of the current buffer | ||
* @return pointer to the allocated memory containing the extracted next line | ||
*/ | ||
char *ft_next_line(char *buffer) | ||
{ | ||
static t_list *list = NULL; | ||
char *next_line; | ||
int i; | ||
int j; | ||
char *new_line; | ||
|
||
i = 0; | ||
while (buffer[i] != '\0' && buffer[i] != '\n') | ||
i++; | ||
if (buffer[i] == '\0') | ||
{ | ||
free(buffer); | ||
return (NULL); | ||
} | ||
new_line = ft_calloc(ft_strlen(buffer) - i + 1, sizeof(char)); | ||
if (!new_line) | ||
return (free(buffer), NULL); | ||
i++; | ||
j = 0; | ||
while (buffer[i] != '\0') | ||
{ | ||
new_line[j] = buffer[i]; | ||
j++; | ||
i++; | ||
} | ||
free(buffer); | ||
return (new_line); | ||
} | ||
|
||
// fd are only positives | read gives -1 if there is a problem with reading | ||
if (fd < 0 || BUFFER_SIZE <= 0) | ||
return NULL; | ||
/** | ||
* @note 1. ft_calloc(1, 1) | ||
- effectively creates an empty string. | ||
- allocates memory for a single character and initializes it to '\0' | ||
- call is used to allocate memory for an empty string. | ||
* @brief 4. create a buffer and allocate memory for it( +1 is for '\0') | ||
* @brief 5. if no bytes are read -> free the buffer | ||
* @brief 6. read() reaturns -1 if an error occurs while reading the file | ||
*/ | ||
char *read_from_file(int fd, char *line) | ||
{ | ||
int byte_read; | ||
char temp[BUFFER_SIZE + 1]; | ||
|
||
// Create my list until I encounter '\n' | ||
create_list(&list, fd); | ||
byte_read = 1; | ||
while (byte_read > 0) | ||
{ | ||
byte_read = read(fd, temp, BUFFER_SIZE); | ||
if (byte_read < 0) | ||
{ | ||
free(line); | ||
return (NULL); | ||
} | ||
temp[byte_read] = '\0'; | ||
line = ft_strjoin(line, temp); | ||
if (ft_strchr(temp, '\n')) | ||
break ; | ||
} | ||
return (line); | ||
} | ||
|
||
int len_to_newline(t_list *list) | ||
/** | ||
* @brief 1. creates a buffer, that stores the whole text in the file | ||
* @brief 2. the content of the string that buffer points to is what changes. | ||
*/ | ||
char *get_next_line(int fd) | ||
{ | ||
int i; | ||
int len; | ||
static char *buffer; | ||
char *current_line; | ||
|
||
if (list == NULL) | ||
return 0; | ||
len = 0; | ||
while (list) | ||
{ | ||
i = 0; | ||
while (/* condition */) | ||
{ | ||
/* code */ | ||
} | ||
|
||
} | ||
|
||
|
||
} | ||
if (fd < 0 || BUFFER_SIZE <= 0) | ||
return (NULL); | ||
buffer = read_from_file(fd, buffer); | ||
if (buffer == NULL) | ||
return (NULL); | ||
current_line = ft_extract_line(buffer); | ||
buffer = ft_next_line(buffer); | ||
return (current_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 |
---|---|---|
|
@@ -5,38 +5,31 @@ | |
/* +:+ +:+ +:+ */ | ||
/* By: aantonie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2023/11/27 13:26:45 by aantonie #+# #+# */ | ||
/* Updated: 2023/11/27 15:15:26 by aantonie ### ########.fr */ | ||
/* Created: 2024/02/26 23:09:25 by aantonie #+# #+# */ | ||
/* Updated: 2024/03/06 20:00:00 by aantonie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef GET_NEXT_LINE_H | ||
# define GET_NEXT_LINE_H | ||
|
||
#ifndef BUFFER_SIZE | ||
# define BUFFER_SIZE 10 | ||
|
||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <stddef.h> | ||
|
||
// node of the linked list | ||
typedef struct s_list | ||
{ | ||
char *str_buf; | ||
struct s_list *next_str; | ||
} t_list; | ||
|
||
int found_newline(t_list *list); | ||
t_list *find_last_node(t_list *list); | ||
char *get_line(t_list *list); | ||
void copy_string(t_list, char *str); | ||
int len_to_newline(t_list) | ||
|
||
|
||
|
||
# ifndef BUFFER_SIZE | ||
# define BUFFER_SIZE 42 | ||
# endif | ||
|
||
# include <fcntl.h> | ||
# include <stdio.h> | ||
# include <stdlib.h> | ||
# include <string.h> | ||
# include <unistd.h> | ||
|
||
char *get_next_line(int fd); | ||
|
||
// utils | ||
void *ft_calloc(size_t count, size_t size); | ||
void *ft_bzero(void *str, size_t n); | ||
char *ft_strchr(char *s, int c); | ||
char *ft_strjoin(char *str1, char *str2); | ||
size_t ft_strlen(char *s); | ||
|
||
#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,108 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* get_next_line_utils.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: aantonie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/02/26 23:09:32 by aantonie #+# #+# */ | ||
/* Updated: 2024/03/09 15:30:42 by aantonie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
// bzero, calloc, strlen, strchr, strjoin | ||
#include "get_next_line.h" | ||
|
||
/** | ||
* @brief 1. Create a new string with the size of both strings | ||
* @note 2. strlen() does not include terminating 0 | ||
* @note 3. After concatenating the 2 strings I must initialize | ||
* the last index to 0 to properly null terminate the new string | ||
*/ | ||
char *ft_strjoin(char *s1, char *s2) | ||
{ | ||
char *str; | ||
size_t i; | ||
size_t j; | ||
|
||
i = 0; | ||
if (!s1 && !s2) | ||
return (NULL); | ||
str = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(char)); | ||
while (s1 && s1[i] != 0) | ||
{ | ||
str[i] = s1[i]; | ||
i++; | ||
} | ||
j = 0; | ||
while (s2 && s2[j] != 0) | ||
{ | ||
str[i + j] = s2[j]; | ||
j++; | ||
} | ||
str[ft_strlen(s1) + ft_strlen(s2)] = '\0'; | ||
if (s1) | ||
free(s1); | ||
return (str); | ||
} | ||
|
||
void *ft_calloc(size_t count, size_t size) | ||
{ | ||
void *ptr; | ||
|
||
if (count >= 2147483647 || size >= 2147483647) | ||
return (NULL); | ||
ptr = malloc(size * count); | ||
if (!ptr) | ||
return (NULL); | ||
ft_bzero(ptr, count * size); | ||
return (ptr); | ||
} | ||
|
||
/** | ||
* @param 1. *str - a pointer to the located character | ||
* @note 2. int c - the ascii number of the char the function is looking for | ||
*/ | ||
char *ft_strchr(char *s, int c) | ||
{ | ||
char searched_char; | ||
char *str; | ||
|
||
str = (char *)s; | ||
searched_char = (char)c; | ||
while (*str != searched_char && *str != '\0') | ||
str++; | ||
if (*str == searched_char) | ||
return (str); | ||
else | ||
return (NULL); | ||
} | ||
|
||
size_t ft_strlen(char *str) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
if (!str) | ||
return (0); | ||
while (str[i]) | ||
i++; | ||
return (i); | ||
} | ||
|
||
/** | ||
* @note cast becausee str is of type pointer to void - void* | ||
* @note -- I need str to be of type pointer to char - char* | ||
*/ | ||
void *ft_bzero(void *str, size_t n) | ||
{ | ||
size_t i; | ||
|
||
i = 0; | ||
while (i < n) | ||
{ | ||
*((unsigned char *)str + i) = '\0'; | ||
i++; | ||
} | ||
return (str); | ||
} |
Oops, something went wrong.