Libft
Custom implementation of core libc functions with additional utility helpers.
Loading...
Searching...
No Matches
Buffer API

Dynamic growable buffer utilities. More...

Functions

bool buff_init (t_buff *b, size_t initial_cap)
 Initializes a buffer with the specified initial capacity.
bool buff_adjust (t_buff *buff)
 Shrinks buffer capacity to match its current length.
void buff_free (t_buff *b)
 Frees the buffer's internal data.
int buff_get_index (t_buff *buff, char c)
 Finds the index of a character in the buffer.
bool buff_prepend (t_buff *b, const char *str, long n)
 Prepends a string to the beginning of the buffer.
bool buff_insert (t_buff *b, size_t index, const char *str, long n)
 Inserts a string at a specific index in the buffer.
bool buff_append (t_buff *b, const char *str, long n)
 Appends a string to the end of the buffer.
t_buffbuff_dup_n (const t_buff *src, size_t n)
 Duplicates up to n bytes of a buffer into a new buffer.
void buff_rm_part (t_buff *buff, size_t i_start, ssize_t len)
 Removes a portion of the buffer starting at i_start.
bool buff_append_format (t_buff *buff, const char *fstring,...)
 Appends formatted string to buffer using variadic arguments.
bool buff_append_vformat (t_buff *buff, const char *fstring, va_list args)
 Appends formatted string to buffer using va_list.
int buff_read_until (t_buff *buff, int fd, char c)
 Reads from a file descriptor until a specific character is found.
bool buff_read_all (t_buff *buff, int fd)
 Reads all available data from a file descriptor into buffer.

Detailed Description

Dynamic growable buffer utilities.

Functions to initialize, grow, shrink and manipulate dynamic buffers.

Function Documentation

◆ buff_adjust()

bool buff_adjust ( t_buff * buff)

Shrinks buffer capacity to match its current length.

Warning
buff must be initialized before calling this function.
Parameters
buffPointer to an initialized buffer (borrowed).
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buff_append()

bool buff_append ( t_buff * b,
const char * str,
long n )

Appends a string to the end of the buffer.

Buffer is automatically grown if necessary.

Warning
b must be initialized before calling this function.
UB if n > 0 and str is shorter than n bytes.
Parameters
bPointer to an initialized buffer (borrowed).
strString to append (borrowed, not modified).
nNumber of bytes to append, or -1 to use strlen(str).
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buff_append_format()

bool buff_append_format ( t_buff * buff,
const char * fstring,
... )

Appends formatted string to buffer using variadic arguments.

Supports printf formats: %c %s %d %i %u %x %X %p %%.
Supports printf-like flags: - 0 . # + and width.

Warning
buff must be initialized before calling this function.
Parameters
buffPointer to an initialized buffer (borrowed).
fstringFormat string (borrowed).
...Variadic arguments for format specifiers.
Returns
true on success, false on failure.
Here is the call graph for this function:

◆ buff_append_vformat()

bool buff_append_vformat ( t_buff * buff,
const char * fstring,
va_list args )

Appends formatted string to buffer using va_list.

Warning
buff must be initialized before calling this function.
Parameters
buffPointer to an initialized buffer (borrowed).
fstringFormat string (borrowed).
argsVariable argument list.
Returns
true on success, false on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buff_dup_n()

t_buff * buff_dup_n ( const t_buff * src,
size_t n )

Duplicates up to n bytes of a buffer into a new buffer.

Note
Caller owns the returned t_buff and must free both the struct and its internal data (use buff_free then free).
Parameters
srcSource buffer to duplicate (borrowed).
nMaximum number of bytes to copy.
Returns
Pointer to a newly allocated t_buff (owned), or NULL on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buff_free()

void buff_free ( t_buff * b)

Frees the buffer's internal data.

Sets buff->data to NULL after freeing.

Warning
Does not free the t_buff struct itself, only its internal data.
Parameters
bPointer to the buffer (borrowed).
Here is the caller graph for this function:

◆ buff_get_index()

int buff_get_index ( t_buff * buff,
char c )

Finds the index of a character in the buffer.

Warning
buff must be initialized before calling this function.
Parameters
buffPointer to an initialized buffer (borrowed).
cCharacter to find.
Returns
Index of the character, or -1 if not found.
Here is the caller graph for this function:

◆ buff_init()

bool buff_init ( t_buff * b,
size_t initial_cap )

Initializes a buffer with the specified initial capacity.

Note
Cannot fail when initial_cap == 0.
Parameters
bPointer to the buffer structure to initialize (uninitialized).
initial_capInitial capacity of the buffer.
Returns
true on success, false on memory allocation failure.
Here is the caller graph for this function:

◆ buff_insert()

bool buff_insert ( t_buff * b,
size_t index,
const char * str,
long n )

Inserts a string at a specific index in the buffer.

Buffer is automatically grown if necessary.

Warning
b must be initialized before calling this function.
UB if index > b->len.
UB if n > 0 and str is shorter than n bytes.
Parameters
bPointer to an initialized buffer (borrowed).
indexPosition at which to insert the string.
strString to insert (borrowed, not modified).
nNumber of bytes to insert, or -1 to use strlen(str).
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buff_prepend()

bool buff_prepend ( t_buff * b,
const char * str,
long n )

Prepends a string to the beginning of the buffer.

Buffer is automatically grown if necessary.

Warning
b must be initialized before calling this function.
UB if n > 0 and str is shorter than n bytes.
Parameters
bPointer to an initialized buffer (borrowed).
strString to prepend (borrowed, not modified).
nNumber of bytes to prepend, or -1 to use strlen(str).
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buff_read_all()

bool buff_read_all ( t_buff * buff,
int fd )

Reads all available data from a file descriptor into buffer.

Note
On failure, buff->data is NOT automatically freed.
Warning
buff must be initialized before calling this function.
Parameters
buffPointer to an initialized buffer (borrowed).
fdFile descriptor to read from.
Returns
true on success, false if read failed.
Here is the call graph for this function:

◆ buff_read_until()

int buff_read_until ( t_buff * buff,
int fd,
char c )

Reads from a file descriptor until a specific character is found.

Warning
buff must be initialized before calling this function.
Parameters
buffPointer to an initialized buffer (borrowed).
fdFile descriptor to read from.
cCharacter to search for.
Returns
Index of c (>= 0), -1 if EOF reached before c, -2 on error.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ buff_rm_part()

void buff_rm_part ( t_buff * buff,
size_t i_start,
ssize_t len )

Removes a portion of the buffer starting at i_start.

Note
If len < 0, deletes all buffer content from i_start to end.
Warning
buff must be initialized before calling this function.
Parameters
buffPointer to an initialized buffer (borrowed).
i_startStarting index for removal.
lenNumber of bytes to remove, or negative to remove until end.
Here is the call graph for this function:
Here is the caller graph for this function: