libft
Loading...
Searching...
No Matches
Files | Macros | Functions
File Utilities

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.
 

Detailed Description

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:

Macro Definition Documentation

◆ BUFFER_SIZE

#define BUFFER_SIZE   1024

#include <include/ft_file.h>

Default buffer size for reading operations like get_next_line.

◆ MAX_FD

#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.

Function Documentation

◆ extract_line()

static char * extract_line ( char *  remainder)
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.

Parameters
remainderThe remainder buffer.
Returns
New allocated line string, or NULL if empty.
See also
ft_substr

◆ ft_open_file()

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.

Parameters
filenameThe path to the file.
flagsFlags such as O_RDONLY, O_WRONLY, etc.
Returns
The file descriptor on success, or -1 on failure.

If the provided filename is NULL or if open fails, this function reports the error to stderr using perror.

Parameters
filenameThe path to the file to be opened.
flagsFile access flags (e.g., O_RDONLY, O_WRONLY).
Returns
The file descriptor on success, or -1 on failure.
Note
This function is useful for reducing boilerplate when opening files.

◆ ft_read_file()

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.

Parameters
fdThe file descriptor to read from.
bufferThe buffer to store the read data.
sizeThe number of bytes to read.
Returns
The number of bytes read, or -1 on error.

This function validates the file descriptor and buffer before attempting to read. On failure, it prints a message to stderr.

Parameters
fdThe file descriptor to read from.
bufferPointer to the destination buffer.
sizeNumber of bytes to read.
Returns
The number of bytes read on success, or -1 on error.
Note
This function helps avoid common misuse of read().

◆ ft_safe_close()

int ft_safe_close ( int  fd)

#include <include/ft_file.h>

Closes a file descriptor safely.

Safely closes a file descriptor.

Parameters
fdThe file descriptor to close.
Returns
0 on success, or -1 on error.

This function closes a valid file descriptor and reports any error using perror.

Parameters
fdThe file descriptor to close.
Returns
0 on success, or -1 if closing fails.
Note
This function should always be used to close file descriptors in a safe and consistent manner.

◆ get_next_line()

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.

Parameters
fdThe file descriptor to read from.
Returns
A newly allocated string containing the line, or NULL on failure.

This function returns the next line read from fd. It uses static storage to preserve context across calls and handles files independently.

Parameters
fdThe file descriptor to read from.
Returns
The next line, or NULL if EOF or an error occurs.
Note
Caller is responsible for freeing the returned string.
Depends on BUFFER_SIZE and MAX_FD being defined.
See also
read_and_store
extract_line
update_remainder

◆ read_and_store()

static ssize_t read_and_store ( int  fd,
char **  remainder 
)
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.

Parameters
fdThe file descriptor to read from.
remainderPointer to the pointer holding current remainder.
Returns
Number of bytes read on success, -1 on error.
See also
ft_strdup
ft_strchr
ft_strjoin

◆ update_remainder()

static char * update_remainder ( char *  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.

Parameters
remainderThe buffer to update.
Returns
A new remainder string, or NULL if nothing is left.
See also
ft_strlen
ft_substr