String Buffer Operations. More...
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
Go to the source code of this file.
Data Structures | |
struct | buffer |
Defines | |
#define | buffer_dup(b, buf) (buffer_dupl(b,buf,NULL)) |
Make a heap allocated copy of the buffer. | |
#define | buffer_putstring(b, s) (buffer_putlstring(b,s,strlen(s))) |
Appends the string to the end of the buffer. | |
#define | buffer_putliteral(b, l) (buffer_putlstring(b,l "",sizeof(l)-1)) |
Appends the string literal to the end of the buffer. | |
#define | buffer_tostring(b) buffer_tolstring(b, NULL) |
Returns the buffer as a string. | |
#define | BUFFER_STACK(name, size) |
Allocate a buffer named `name' on the stack of at most `size' bytes. | |
#define | BUFFER_STACK_ABORT(name, size) |
Allocate a buffer named `name' on the stack of at most `size' bytes. | |
#define | BUFFER_STACK_PRINT(name, size,...) |
Allocate and print to a buffer named `name' on the stack of at most `size' bytes. | |
Functions | |
void | buffer_init (buffer_t *b) |
Initialize a buffer. | |
void | buffer_ubuf (buffer_t *b, char *buf, size_t len) |
Use the provided buffer as a starting buffer. | |
void | buffer_max (buffer_t *b, size_t max) |
Set the maximum size of the buffer. | |
void | buffer_abortonfailure (buffer_t *b, int abortonfailure) |
Set the buffer to call fatal(. | |
void | buffer_free (buffer_t *b) |
Free any resources and memory in use by a buffer. | |
int | buffer_dupl (buffer_t *b, char **buf, size_t *l) |
Make a heap allocated copy of the buffer. | |
int | buffer_putvfstring (buffer_t *b, const char *format, va_list ap) |
Print the formatted output to the buffer. | |
int | buffer_putfstring (buffer_t *b, const char *format,...) __attribute__((format(printf |
Appends the formatted output to the buffer. | |
int | buffer_putlstring (buffer_t *b, const char *str, size_t len) |
Appends the string to the end of the buffer. | |
const char * | buffer_tolstring (buffer_t *b, size_t *size) |
Returns the buffer as a string. | |
void | buffer_rewind (buffer_t *b, size_t n) |
Rewinds the buffer to position n. | |
size_t | buffer_pos (buffer_t *b) |
Get the current position in the buffer. |
String Buffer Operations.
You can use the buffer in the same way you would print to a file. Use the buffer to do formatted printing. When you are done retrieve the final string using buffer_tostring.
#define buffer_dup | ( | b, | |||
buf | ) | (buffer_dupl(b,buf,NULL)) |
#define buffer_putstring | ( | b, | |||
s | ) | (buffer_putlstring(b,s,strlen(s))) |
#define buffer_putliteral | ( | b, | |||
l | ) | (buffer_putlstring(b,l "",sizeof(l)-1)) |
#define buffer_tostring | ( | b | ) | buffer_tolstring(b, NULL) |
#define BUFFER_STACK | ( | name, | |||
size | ) |
buffer_t name;\ char name##_ubuf[size > BUFFER_INISIZ ? size : 1]; /* Unfortunately, we can't conditionally allocate this ubuf array. Use char[1] if less than BUFFER_INISIZ */\ buffer_init(&name);\ buffer_max(&name, size);\ buffer_ubuf(&name, name##_ubuf, size);
Allocate a buffer named `name' on the stack of at most `size' bytes.
Does not abort on failure, but hits the max size and drops further bytes written. You can turn on aborts on failure manually using buffer_abortonfailure or using BUFFER_STACK_ABORT. You do not need to call buffer_free(name) because nothing is ever allocated on the heap. This is defined as a macro.
#define BUFFER_STACK_ABORT | ( | name, | |||
size | ) |
BUFFER_STACK(name,size);\ buffer_abortonfailure(&name, 1);
Allocate a buffer named `name' on the stack of at most `size' bytes.
This works the same as BUFFER_STACK but also sets the abort flag on the buffer. This is defined as a macro.
#define BUFFER_STACK_PRINT | ( | name, | |||
size, | |||||
... | ) |
BUFFER_STACK(name,size);\ buffer_putfstring(&name, __VA_ARGS__);
Allocate and print to a buffer named `name' on the stack of at most `size' bytes.
This macro uses BUFFER_STACK to allocate the buffer. Variable arguments are passed to buffer_putfstring, starting with the format string.
void buffer_init | ( | buffer_t * | b | ) |
void buffer_ubuf | ( | buffer_t * | b, | |
char * | buf, | |||
size_t | len | |||
) |
void buffer_max | ( | buffer_t * | b, | |
size_t | max | |||
) |
void buffer_abortonfailure | ( | buffer_t * | b, | |
int | abortonfailure | |||
) |
void buffer_free | ( | buffer_t * | b | ) |
int buffer_dupl | ( | buffer_t * | b, | |
char ** | buf, | |||
size_t * | l | |||
) |
int buffer_putvfstring | ( | buffer_t * | b, | |
const char * | format, | |||
va_list | ap | |||
) |
Print the formatted output to the buffer.
The format string follows the same semantics as the UNIX vprintf function. buffer_putvfstring does not call the variable argument macros va_(start|end) on ap.
b | The buffer to fill. | |
format | The format string. | |
ap | The variable argument list for the format string. |
int buffer_putfstring | ( | buffer_t * | b, | |
const char * | format, | |||
... | ||||
) |
int buffer_putlstring | ( | buffer_t * | b, | |
const char * | str, | |||
size_t | len | |||
) |
const char* buffer_tolstring | ( | buffer_t * | b, | |
size_t * | size | |||
) |
Returns the buffer as a string.
The string is no longer valid after deleting the buffer. A final ASCII NUL character is guaranteed to terminate the string.
b | The buffer. | |
size | The size of the string is placed in this variable. Can be NULL. |
void buffer_rewind | ( | buffer_t * | b, | |
size_t | n | |||
) |