PER Firmware
Loading...
Searching...
No Matches
strbuf.h File Reference

Fixed-size string builder. Useful for building large CMD strings before transmission. More...

#include <stdint.h>
#include <stddef.h>
Include dependency graph for strbuf.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  strbuf_t
 

Macros

#define ALLOCATE_STRBUF(NAME, MAX_SIZE)
 Macro to allocate and initialize a strbuf_t with a fixed-size buffer. Also creates the underlying data buffer in the same scope.
 

Functions

void strbuf_clear (strbuf_t *sb)
 Clears the buffer by resetting length to 0.
 
size_t strbuf_append (strbuf_t *sb, const void *data, size_t length)
 Appends raw data to the buffer. If the data exceeds the buffer size, no data is appended.
 
size_t strbuf_printf (strbuf_t *sb, const char *format,...)
 Appends formatted data to the buffer using printf-style formatting. !
 

Detailed Description

Fixed-size string builder. Useful for building large CMD strings before transmission.

Author
Irving Wang (irvin.nosp@m.gw@p.nosp@m.urdue.nosp@m..edu)

Macro Definition Documentation

◆ ALLOCATE_STRBUF

#define ALLOCATE_STRBUF ( NAME,
MAX_SIZE )
Value:
uint8_t NAME##_data[(MAX_SIZE)]; \
strbuf_t NAME = { \
.data = NAME##_data, \
.length = 0, \
.max_len = (MAX_SIZE), \
}
Definition strbuf.h:15
size_t length
Definition strbuf.h:17

Macro to allocate and initialize a strbuf_t with a fixed-size buffer. Also creates the underlying data buffer in the same scope.

Parameters
NAMEThe name of the strbuf_t variable to create.
MAX_SIZEThe maximum size of the buffer. Usage: ALLOCATE_STRBUF(my_buf, 128); This creates a strbuf_t named my_buf with a buffer of 128 bytes.

Function Documentation

◆ strbuf_append()

size_t strbuf_append ( strbuf_t * sb,
const void * data,
size_t length )

Appends raw data to the buffer. If the data exceeds the buffer size, no data is appended.

Returns
Num bytes written to the buffer

!

Warning
Data is purely a buffer, not a C-string, meaning null terminators do not need to be considered
Here is the caller graph for this function:

◆ strbuf_clear()

void strbuf_clear ( strbuf_t * sb)

Clears the buffer by resetting length to 0.

Here is the caller graph for this function:

◆ strbuf_printf()

size_t strbuf_printf ( strbuf_t * sb,
const char * format,
... )

Appends formatted data to the buffer using printf-style formatting. !

Warning
This function is unsafe if the formatted string exceeds the remaining buffer space. !
The arguments provided may be C-strings and thus, because of the internal use of vsnprintf, you might have to account for a null terminator.
Here is the caller graph for this function: