Herb C Reference
Loading...
Searching...
No Matches
buffer.h File Reference
#include <stdbool.h>
#include <stdlib.h>

Go to the source code of this file.

Data Structures

struct  BUFFER_STRUCT
 

Typedefs

typedef struct BUFFER_STRUCT buffer_T
 

Functions

bool buffer_init (buffer_T *buffer)
 
buffer_T buffer_new (void)
 
bool buffer_increase_capacity (buffer_T *buffer, size_t additional_capacity)
 Increases the capacity of the buffer if needed to accommodate additional content.
 
bool buffer_has_capacity (buffer_T *buffer, size_t required_length)
 
bool buffer_expand_capacity (buffer_T *buffer)
 Expands the capacity of the buffer by doubling its current capacity.
 
bool buffer_expand_if_needed (buffer_T *buffer, size_t required_length)
 Expands the capacity of the buffer if needed to accommodate additional content.
 
bool buffer_resize (buffer_T *buffer, size_t new_capacity)
 Resizes the capacity of the buffer to the specified new capacity.
 
void buffer_append (buffer_T *buffer, const char *text)
 Appends a null-terminated string to the buffer.
 
void buffer_append_with_length (buffer_T *buffer, const char *text, size_t length)
 Appends a string of specified length to the buffer.
 
void buffer_append_char (buffer_T *buffer, char character)
 
void buffer_append_repeated (buffer_T *buffer, char character, size_t length)
 
void buffer_append_whitespace (buffer_T *buffer, size_t length)
 
void buffer_prepend (buffer_T *buffer, const char *text)
 
void buffer_concat (buffer_T *destination, buffer_T *source)
 
char * buffer_value (const buffer_T *buffer)
 
size_t buffer_length (const buffer_T *buffer)
 
size_t buffer_capacity (const buffer_T *buffer)
 
size_t buffer_sizeof (void)
 
void buffer_clear (buffer_T *buffer)
 
void buffer_free (buffer_T *buffer)
 

Typedef Documentation

◆ buffer_T

typedef struct BUFFER_STRUCT buffer_T

Function Documentation

◆ buffer_init()

bool buffer_init ( buffer_T buffer)

◆ buffer_new()

buffer_T buffer_new ( void  )

◆ buffer_increase_capacity()

bool buffer_increase_capacity ( buffer_T buffer,
const size_t  additional_capacity 
)

Increases the capacity of the buffer if needed to accommodate additional content.

This function only handles memory allocation and does not modify the buffer content or null termination.

Parameters
bufferThe buffer to increase capacity for
additional_capacityThe additional length needed beyond current buffer capacity
Returns
true if capacity was increased, false if reallocation failed

◆ buffer_has_capacity()

bool buffer_has_capacity ( buffer_T buffer,
size_t  required_length 
)

◆ buffer_expand_capacity()

bool buffer_expand_capacity ( buffer_T buffer)

Expands the capacity of the buffer by doubling its current capacity.

This function is a convenience function that calls buffer_increase_capacity with a factor of 2.

Parameters
bufferThe buffer to expand capacity for
Returns
true if capacity was increased, false if reallocation failed

◆ buffer_expand_if_needed()

bool buffer_expand_if_needed ( buffer_T buffer,
const size_t  required_length 
)

Expands the capacity of the buffer if needed to accommodate additional content.

This function is a convenience function that calls buffer_has_capacity and buffer_expand_capacity.

Parameters
bufferThe buffer to expand capacity for
required_lengthThe additional length needed beyond current buffer capacity
Returns
true if capacity was increased, false if reallocation failed

◆ buffer_resize()

bool buffer_resize ( buffer_T buffer,
const size_t  new_capacity 
)

Resizes the capacity of the buffer to the specified new capacity.

Parameters
bufferThe buffer to resize
new_capacityThe new capacity to resize the buffer to
Returns
true if capacity was resized, false if reallocation failed

◆ buffer_append()

void buffer_append ( buffer_T buffer,
const char *  text 
)

Appends a null-terminated string to the buffer.

Note
This function requires that 'text' is a properly null-terminated string. When reading data from files or other non-string sources, ensure the data is null-terminated before calling this function, or use buffer_append_with_length instead.
Parameters
bufferThe buffer to append to
textA null-terminated string to append
Returns
void

◆ buffer_append_with_length()

void buffer_append_with_length ( buffer_T buffer,
const char *  text,
const size_t  length 
)

Appends a string of specified length to the buffer.

Unlike buffer_append(), this function does not require the text to be null-terminated as it uses the provided length instead of strlen(). This is particularly useful when working with data from files, network buffers, or other non-null-terminated sources.

Parameters
bufferThe buffer to append to
textThe text to append (doesn't need to be null-terminated)
lengthThe number of bytes to append from text
Returns
void

◆ buffer_append_char()

void buffer_append_char ( buffer_T buffer,
char  character 
)

◆ buffer_append_repeated()

void buffer_append_repeated ( buffer_T buffer,
char  character,
size_t  length 
)

◆ buffer_append_whitespace()

void buffer_append_whitespace ( buffer_T buffer,
size_t  length 
)

◆ buffer_prepend()

void buffer_prepend ( buffer_T buffer,
const char *  text 
)

◆ buffer_concat()

void buffer_concat ( buffer_T destination,
buffer_T source 
)

◆ buffer_value()

char * buffer_value ( const buffer_T buffer)

◆ buffer_length()

size_t buffer_length ( const buffer_T buffer)

◆ buffer_capacity()

size_t buffer_capacity ( const buffer_T buffer)

◆ buffer_sizeof()

size_t buffer_sizeof ( void  )

◆ buffer_clear()

void buffer_clear ( buffer_T buffer)

◆ buffer_free()

void buffer_free ( buffer_T buffer)