|
libft
|
Helper functions for basic file operations. More...
Files | |
| file | ft_file.h |
| File utility functions for simplified and safe file operations. | |
| file | get_next_line.c |
| Implementation of the get_next_line function. | |
Macros | |
| #define | BUFFER_SIZE 1024 |
Default buffer size for reading operations like get_next_line. | |
| #define | MAX_FD 1024 |
| Maximum number of file descriptors supported simultaneously. | |
Functions | |
| static char * | extract_line (char *remainder) |
| Extracts the next line from the remainder buffer. | |
| int | ft_open_file (const char *filename, int flags) |
| Opens a file with the given flags. | |
| ssize_t | ft_read_file (int fd, void *buffer, size_t size) |
| Reads a specified number of bytes from a file descriptor. | |
| int | ft_safe_close (int fd) |
| Closes a file descriptor safely. | |
| char * | get_next_line (int fd) |
| Reads a line from a file descriptor. | |
| static ssize_t | read_and_store (int fd, char **remainder) |
| Reads from a file descriptor and appends to the remainder buffer. | |
| static char * | update_remainder (char *remainder) |
| Updates the remainder buffer after a line has been extracted. | |
Helper functions for basic file operations.
These functions simplify and secure standard file operations, including reading, opening, closing, and line-by-line parsing.
This group includes:
| #define BUFFER_SIZE 1024 |
#include <include/ft_file.h>
Default buffer size for reading operations like get_next_line.
| #define MAX_FD 1024 |
#include <include/ft_file.h>
Maximum number of file descriptors supported simultaneously.
Used internally to manage static buffers indexed by file descriptors.
|
static |
#include <srcs/file_utils/get_next_line.c>
Extracts the next line from the remainder buffer.
Allocates and returns a new string containing everything from the start of remainder up to and including the first newline, or up to the null terminator if no newline is found.
| remainder | The remainder buffer. |
| int ft_open_file | ( | const char * | filename, |
| int | flags | ||
| ) |
#include <include/ft_file.h>
Opens a file with the given flags.
Safely opens a file with the given flags.
| filename | The path to the file. |
| flags | Flags such as O_RDONLY, O_WRONLY, etc. |
If the provided filename is NULL or if open fails, this function reports the error to stderr using perror.
| filename | The path to the file to be opened. |
| flags | File access flags (e.g., O_RDONLY, O_WRONLY). |
| ssize_t ft_read_file | ( | int | fd, |
| void * | buffer, | ||
| size_t | size | ||
| ) |
#include <include/ft_file.h>
Reads a specified number of bytes from a file descriptor.
Safely reads data from a file descriptor.
| fd | The file descriptor to read from. |
| buffer | The buffer to store the read data. |
| size | The number of bytes to read. |
This function validates the file descriptor and buffer before attempting to read. On failure, it prints a message to stderr.
| fd | The file descriptor to read from. |
| buffer | Pointer to the destination buffer. |
| size | Number of bytes to read. |
read(). | int ft_safe_close | ( | int | fd | ) |
#include <include/ft_file.h>
Closes a file descriptor safely.
Safely closes a file descriptor.
| fd | The file descriptor to close. |
This function closes a valid file descriptor and reports any error using perror.
| fd | The file descriptor to close. |
| char * get_next_line | ( | int | fd | ) |
#include <include/ft_file.h>
Reads a line from a file descriptor.
Reads the next line from a file descriptor.
This function returns a line terminated by a newline character \n, or the end of file.
| fd | The file descriptor to read from. |
This function returns the next line read from fd. It uses static storage to preserve context across calls and handles files independently.
| fd | The file descriptor to read from. |
|
static |
#include <srcs/file_utils/get_next_line.c>
Reads from a file descriptor and appends to the remainder buffer.
Allocates a temporary buffer and appends read data to the remainder until a newline is found or EOF is reached. It handles reallocation and joins partial strings dynamically.
| fd | The file descriptor to read from. |
| remainder | Pointer to the pointer holding current remainder. |
|
static |
#include <srcs/file_utils/get_next_line.c>
Updates the remainder buffer after a line has been extracted.
Frees the old remainder and returns a newly allocated string containing the part of the original string after the first newline.
| remainder | The buffer to update. |