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_adjust (t_buff *buff)
 Shrinks buffer capacity to match its current length.
 
bool buff_append (t_buff *dst, const t_buff *src)
 Appends the content of a source buffer to the end of a buffer.
 
bool buff_append_format (t_buff *buff, const char *fstring,...)
 Appends formatted string to buffer using variadic arguments.
 
bool buff_append_n (t_buff *b, const char *str, long n)
 Appends n first bytes of a string to the end of the buffer.
 
bool buff_append_vformat (t_buff *buff, const char *fstring, va_list args)
 Appends formatted string to buffer using va_list.
 
bool buff_cmp (const t_buff *a, const t_buff *b)
 Compares two buffers data byte by byte.
 
bool buff_dup (t_buff *dst, const t_buff *src)
 Copies the content of a source buffer into an existing buffer.
 
bool buff_dup_n (t_buff *dst, const t_buff *src, size_t n)
 Copies up to n bytes from a source buffer into an existing buffer.
 
void buff_free (t_buff *buff)
 Frees the buffer's internal data.
 
void buff_free_void (void *buff)
 Frees a t_buff item through a generic void* callback signature.
 
ssize_t buff_get_index_c (const t_buff *buff, char c)
 Finds the index of the first occurrence of a character in the buffer.
 
ssize_t buff_get_index_s (const t_buff *buff, const char *s, ssize_t slen)
 Finds the first occurrence of a substring in the buffer.
 
char * buff_get_string (const t_buff *buff)
 Returns a newly allocated copy of the buffer content.
 
bool buff_init (t_buff *buff, size_t initial_cap, const char *str, long n)
 Initializes a buffer with the specified initial capacity.
 
bool buff_insert (t_buff *dst, size_t index, const t_buff *src)
 Inserts a source buffer in a buffer at the specified index.
 
bool buff_insert_n (t_buff *b, size_t index, const char *str, long n)
 Inserts n first bytes of a string in the buffer at specified index.
 
bool buff_prepend (t_buff *dst, const t_buff *src)
 Prepends a source buffer to the beginning of a buffer.
 
bool buff_prepend_n (t_buff *b, const char *str, long n)
 Prepends n first bytes of a string to the beginning of the buffer.
 
bool buff_read_all (t_buff *buff, int fd)
 Reads all available data from a file descriptor into buffer.
 
bool buff_read_until_c (t_buff *buff, int fd, char c)
 Reads from a file descriptor until a specific character is found.
 
bool buff_read_until_n (t_buff *buff, int fd, size_t n)
 Reads up to n bytes from a file descriptor into buffer.
 
bool buff_read_until_s (t_buff *buff, int fd, const char *s, ssize_t slen)
 Reads from a file descriptor until a specific substring is found.
 
void buff_rm_part (t_buff *buff, size_t i_start, ssize_t len)
 Removes a portion of the buffer starting at i_start.
 

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 dst,
const t_buff src 
)

Appends the content of a source buffer to the end of a buffer.

Buffer is automatically grown if necessary.

Warning
dst and src must be initialized before calling this function.
Parameters
dstDestination buffer to append to (borrowed).
srcSource buffer to append (borrowed, read-only).
Returns
true on success, false on memory allocation failure.
Here is the call 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, read-only).
...Variadic arguments for format specifiers.
Returns
true on success, false on failure.
Here is the call graph for this function:

◆ buff_append_n()

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

Appends n first bytes of 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, read-only).
nNumber of bytes to append, or -1 to use str_len(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_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, read-only).
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_cmp()

bool buff_cmp ( const t_buff a,
const t_buff b 
)

Compares two buffers data byte by byte.

Warning
a and b must be initialized before calling this function.
Parameters
aFirst buffer to compare (borrowed, read-only).
bSecond buffer to compare (borrowed, read-only).
Returns
true if the buffers have the same length and content, false otherwise.

◆ buff_dup()

bool buff_dup ( t_buff dst,
const t_buff src 
)

Copies the content of a source buffer into an existing buffer.

Warning
dst and src must be initialized before calling this function.
The previous logical content of dst is replaced, whether the function succeeds or fails.
Parameters
dstDestination buffer to overwrite (borrowed, initialized).
srcSource buffer to duplicate (borrowed, initialized).
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:

◆ buff_dup_n()

bool buff_dup_n ( t_buff dst,
const t_buff src,
size_t  n 
)

Copies up to n bytes from a source buffer into an existing buffer.

Warning
dst and src must be initialized before calling this function.
The previous logical content of dst is replaced, whether the function succeeds or fails.
Parameters
dstDestination buffer to overwrite (borrowed, initialized).
srcSource buffer to duplicate (borrowed, initialized).
nMaximum number of bytes to copy.
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_free()

void buff_free ( t_buff buff)

Frees the buffer's internal data.

Sets buff->data to NULL after freeing. Sets buff->len and buff->cap to 0 after freeing.

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

◆ buff_free_void()

void buff_free_void ( void *  buff)

Frees a t_buff item through a generic void* callback signature.

Wrapper around buff_free() intended for APIs that expect void (*)(void *), such as vector_free().

Warning
buff must point to an initialized t_buff.
Does not free the t_buff struct itself, only its internal data.
Parameters
buffPointer to a t_buff item passed as void* (borrowed).
Here is the call graph for this function:

◆ buff_get_index_c()

ssize_t buff_get_index_c ( const t_buff buff,
char  c 
)

Finds the index of the first occurrence of a character in the buffer.

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

◆ buff_get_index_s()

ssize_t buff_get_index_s ( const t_buff buff,
const char *  s,
ssize_t  slen 
)

Finds the first occurrence of a substring in the buffer.

Warning
buff must be initialized before calling this function.
Parameters
buffPointer to an initialized buffer (borrowed, read-only).
sSubstring to find (borrowed, read-only).
slenNumber of bytes in s, or -1 to use str_len(s).
Returns
Index of the substring, or -1 if not found.
Here is the call graph for this function:

◆ buff_get_string()

char * buff_get_string ( const t_buff buff)

Returns a newly allocated copy of the buffer content.

If the buffer data is not null-terminated, a trailing '\0' is added in the returned string.

Warning
buff must be initialized before calling this function.
Note
Caller owns the returned string and must free it.
The caller remains owner of the t_buff.
The t_buff remains owner of the buff->data.
Parameters
buffPointer to an initialized buffer (borrowed, read-only).
Returns
Newly allocated string (owned by caller), or NULL on failure.
Here is the call graph for this function:

◆ buff_init()

bool buff_init ( t_buff buff,
size_t  initial_cap,
const char *  str,
long  n 
)

Initializes a buffer with the specified initial capacity.

If str is not NULL, it is appended after initialization. If str is NULL, n is ignored.

Note
Cannot fail only when initial_cap == 0 and str == NULL.
Parameters
buffPointer to the buffer structure to initialize (borrowed, uninitialized).
initial_capInitial capacity of the buffer.
strOptional string to append after initialization (borrowed, read-only).
nNumber of bytes to append, or -1 to use str_len(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_insert()

bool buff_insert ( t_buff dst,
size_t  index,
const t_buff src 
)

Inserts a source buffer in a buffer at the specified index.

Buffer is automatically grown if necessary.

Warning
dst and src must be initialized before calling this function.
index must be in range [0, dst->len].
Parameters
dstDestination buffer to insert into (borrowed).
indexInsertion index.
srcSource buffer to insert (borrowed, read-only).
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:

◆ buff_insert_n()

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

Inserts n first bytes of a string in the buffer at specified index.

Buffer is automatically grown if necessary.

Warning
b must be initialized before calling this function.
index must be in range [0, b->len].
UB if n > 0 and str is shorter than n bytes.
Parameters
bPointer to an initialized buffer (borrowed).
indexInsertion index.
strString to insert (borrowed, read-only).
nNumber of bytes to insert, or -1 to use str_len(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 dst,
const t_buff src 
)

Prepends a source buffer to the beginning of a buffer.

Buffer is automatically grown if necessary.

Warning
dst and src must be initialized before calling this function.
Parameters
dstDestination buffer to prepend to (borrowed).
srcSource buffer to prepend (borrowed, read-only).
Returns
true on success, false on memory allocation failure.
Here is the call graph for this function:

◆ buff_prepend_n()

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

Prepends n first bytes of 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, read-only).
nNumber of bytes to prepend, or -1 to use str_len(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
Interrupted reads (EINTR) are retried.
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 (totally or partially).
Here is the call graph for this function:

◆ buff_read_until_c()

bool buff_read_until_c ( t_buff buff,
int  fd,
char  c 
)

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

Note
Interrupted reads (EINTR) are retried.
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.
cCharacter to search for.
Returns
true if c or EOF was encountered, false on memory or read error.
Here is the call graph for this function:

◆ buff_read_until_n()

bool buff_read_until_n ( t_buff buff,
int  fd,
size_t  n 
)

Reads up to n bytes from a file descriptor into buffer.

Note
Interrupted reads (EINTR) are retried.
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.
nNumber of bytes to read.
Returns
true on success or EOF, false on memory or read error.
Here is the call graph for this function:

◆ buff_read_until_s()

bool buff_read_until_s ( t_buff buff,
int  fd,
const char *  s,
ssize_t  slen 
)

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

Note
Interrupted reads (EINTR) are retried.
Warning
buff must be initialized before calling this function.
Parameters
buffPointer to an initialized buffer (borrowed).
fdFile descriptor to read from.
sTarget substring to search for (borrowed, read-only).
slenLength of s, or -1 to use str_len(s).
Returns
true if s or EOF was encountered, false on memory or read error.
Here is the call 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: