This project has been created as part of the 42 curriculum by kjurkows
Note
Both standard and bonus parts of the project are exactly the same.
Meaning both work exactly the same way, bonus does not add any new functionality.
get_next_line is a function that reads a line from a file descriptor and returns it as a string. It is designed to handle multiple file descriptors simultaneously, allowing for efficient reading of files or input streams.
To compile the get_next_line function, you can use the following command:
cc -Wall -Wextra -Werror -c get_next_line.c get_next_line_utils.cThis will create an objects file named get_next_line*.o that can be linked with your main program.
Link the get_next_line*.o objects file with your main program and include the get_next_line.h header file. You can then call the get_next_line function to read lines from a file descriptor.
To compile the bonus version of the get_next_line function, you can use the following command:
cc -Wall -Wextra -Werror -c get_next_line_bonus.c get_next_line_utils_bonus.cThis will create an object file named get_next_line*_bonus.o that can be linked with your main program.
By default, the buffer size for reading lines is set to 4kB (4096 bytes). If you want to change the buffer size, you can redefine the BUFFER_SIZE macro with -D flag during compilation.
For example, to set the buffer size to 8kB (8192 bytes), you can use the following command:
cc -Wall -Wextra -Werror -D BUFFER_SIZE=8192 -c get_next_line.c get_next_line_utils.c- Peers
AI was only used as an assistant while writing documentation. It was not used to write any code.
Main used IDE was Visual Studio Code
norminette was used to check La Norme compliance
gcc is used for compilation.
For persistence of buffers a special linked list is used.
It is a doubly linked list where each node is a special structure (see units).
The GNL memory is stored in a static variable inside the get_next_line function, allowing it to persist across multiple calls to the function.
Each node of the list contains a buffer and a file descriptor, as well as number of bytes read from the file descriptor and current position in the buffer.
The list is used to store buffers for multiple file descriptors, allowing for efficient reading of lines from different sources.
It also contains a pointer to the next and previous nodes in the list, allowing for efficient traversal of the list.
The GNL linked memory is persistent across multiple calls to the get_next_line function, it's units are only freed when an error occurs or EOF is reached.
The GNL linked memory is managed using two specialized functions: new_gnl_mem and get_gnl_mem:
This function is used to create a new unit of the GNL memory. It takes a file descriptor as an argument and allocates memory for a new node in the linked list. The new node is initialized with the provided file descriptor, and its buffer is allocated with the specified BUFFER_SIZE. The function returns a pointer to the newly created node.
The function also ensures that the new node is properly linked to the existing nodes in the list, maintaining the integrity of the linked list structure.
It also handles error cases, such as memory allocation failures, by returning NULL if the allocation fails.
This function is used to retrieve an existing unit of the GNL memory for a given file descriptor. It takes a file descriptor as an argument and traverses the linked list to find the corresponding node. If no unit exists for the given file descriptor, it calls new_gnl_mem to create a new unit and returns a pointer to it. This allows the get_next_line function to efficiently manage buffers for multiple file descriptors without losing track of their states.
The function also handles error cases, such as memory allocation failures, by returning NULL if the allocation fails.
Reading any data into buffer is done with next_read function.
The function reads data from the file descriptor into the buffer of the corresponding GNL memory unit.
It also ensures that the reading output is always appended into the output string.
next_read function also handles most of the reading errors, such as EOF or EAGAIN, and returns appropriate values to indicate the status of the read operation.
It also handles GNL memory cleanup in case of errors, ensuring that the linked list remains consistent and that memory is properly freed when necessary.
Additionally, a custom string concatenation function called ft_str_append is used to append strings together.
This function takes a pointer to output string and two pointers to chars inside of buffer.
By using pointer arithmetic, it calculates the length of the strings to be concatenated and allocates memory for the new string. It then copies the contents of the two input strings into the newly allocated memory, effectively appending them together.
Such implementation was chosen to avoid unnecessary memory allocations and copying, as it allows for efficient concatenation of strings without the need for temporary buffers.
