PER Firmware
Loading...
Searching...
No Matches
strbuf

The strbuf module provides a simple and efficient way to handle "dynamic" strings in C. Data is still allocated statically, but the module allows you to append data to a buffer while keeping track of the current length and ensuring that you do not exceed the allocated size.

Usage Example:

#include "strbuf.h"
#define CMD_LEN 2
const uint8_t WRCFGA[CMD_LEN] = { 0x00, 0x01 };
const char *globalstring = "hello";
// Allocate the buffer with size of 32 bytes
ALLOCATE_STRBUF(test_buf, 32);
int main() {
char *localstring = "world";
// You can check the return value to see if the operation was successful
if (0 == strbuf_append(&test_buf, WRCFGA, CMD_LEN)) {
return 1; // failure
}
// Reset the buffer
strbuf_clear(&test_buf);
// Store "Hello World"
strbuf_append(&test_buf, globalstring, 5);
strbuf_append(&test_buf, " ", 1);
strbuf_append(&test_buf, localstring, 5);
return 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.
Definition strbuf.c:25
void strbuf_clear(strbuf_t *sb)
Clears the buffer by resetting length to 0.
Definition strbuf.c:17
Fixed-size string builder. Useful for building large CMD strings before transmission.
#define ALLOCATE_STRBUF(NAME, MAX_SIZE)
Macro to allocate and initialize a strbuf_t with a fixed-size buffer. Also creates the underlying dat...
Definition strbuf.h:34