cctools
Data Structures | Macros | Functions
buffer.h File Reference

String Buffer Operations. More...

#include <stdlib.h>
#include <stdarg.h>

Go to the source code of this file.

Data Structures

struct  buffer
 

Macros

#define buffer_putstring(b, s)   (buffer_putlstring(b,s,strlen(s)))
 Appends the string to the end of the buffer. More...
 
#define buffer_putliteral(b, l)   (buffer_putlstring(b,l "",sizeof(l)-1))
 Appends the string literal to the end of the buffer. More...
 

Functions

void buffer_init (buffer_t *b)
 Initialize a buffer. More...
 
void buffer_ubuf (buffer_t *b, char *buf, size_t len)
 Use the provided buffer as a starting buffer. More...
 
void buffer_max (buffer_t *b, size_t max)
 Set the maximum size of the buffer. More...
 
void buffer_abortonfailure (buffer_t *b, int abortonfailure)
 Set the buffer to call fatal(...) on error instead of returning an error code. More...
 
void buffer_free (buffer_t *b)
 Free any resources and memory in use by a buffer. More...
 
int buffer_putvfstring (buffer_t *b, const char *format, va_list ap)
 Print the formatted output to the buffer. More...
 
int buffer_putfstring (buffer_t *b, const char *format,...) __attribute__((format(printf
 Appends the formatted output to the buffer. More...
 
int buffer_putlstring (buffer_t *b, const char *str, size_t len)
 Appends the string to the end of the buffer. More...
 
const char * buffer_tostring (buffer_t *b, size_t *size)
 Returns the buffer as a string. More...
 
void buffer_rewind (buffer_t *b, size_t n)
 Rewinds the buffer to position n. More...
 
size_t buffer_pos (buffer_t *b)
 Get the current position in the buffer. More...
 

Detailed Description

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.

Macro Definition Documentation

#define buffer_putstring (   b,
 
)    (buffer_putlstring(b,s,strlen(s)))

Appends the string to the end of the buffer.

Length derived via strlen.

Parameters
bThe buffer to fill.
sThe string to append.
Returns
-1 on error.
#define buffer_putliteral (   b,
 
)    (buffer_putlstring(b,l "",sizeof(l)-1))

Appends the string literal to the end of the buffer.

Length derived via sizeof.

Parameters
bThe buffer to fill.
lThe literal string to append.
Returns
-1 on error.

Function Documentation

void buffer_init ( buffer_t b)

Initialize a buffer.

The buffer includes a reasonably sized starting buffer as part of its
definition.  Usually this means for small strings being built, nothing is
ever allocated on the heap. You can specify a larger starting buffer if
this is inadequate.
Parameters
bThe buffer to initialize.
void buffer_ubuf ( buffer_t b,
char *  buf,
size_t  len 
)

Use the provided buffer as a starting buffer.

This function should only be called before any work is done on the buffer.
The user buffer is only used if it is larger than the internal starting
buffer.
Parameters
bThe buffer.
bufA starting buffer to initially use to avoid allocating memory on the heap. (can be NULL)
lenThe length of the buffer. (ignored if buf == NULL)
void buffer_max ( buffer_t b,
size_t  max 
)

Set the maximum size of the buffer.

Parameters
bThe buffer.
maxThe maximum amount of memory to allocate. (0 is unlimited)
void buffer_abortonfailure ( buffer_t b,
int  abortonfailure 
)

Set the buffer to call fatal(...) on error instead of returning an error code.

Parameters
bThe buffer.
abortonfailureKill the process on errors. (you no longer have to check returns)
void buffer_free ( buffer_t b)

Free any resources and memory in use by a buffer.

Parameters
bThe buffer to free.
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.

Parameters
bThe buffer to fill.
formatThe format string.
apThe variable argument list for the format string.
Returns
-1 on error.
int buffer_putfstring ( buffer_t b,
const char *  format,
  ... 
)

Appends the formatted output to the buffer.

The format string follows the same semantics as the UNIX vprintf function.

Parameters
bThe buffer to fill.
formatThe format string.
...The variable arguments for the format string.
Returns
-1 on error.
int buffer_putlstring ( buffer_t b,
const char *  str,
size_t  len 
)

Appends the string to the end of the buffer.

Parameters
bThe buffer to fill.
strThe string to append.
lenThe length of the string.
Returns
-1 on error.
const char* buffer_tostring ( 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.

Parameters
bThe buffer.
sizeThe size of the string is placed in this variable. Can be NULL.
Returns
The buffer as a string with a NUL terminator.
void buffer_rewind ( buffer_t b,
size_t  n 
)

Rewinds the buffer to position n.

Parameters
bThe buffer.
nThe position to rewind to.
size_t buffer_pos ( buffer_t b)

Get the current position in the buffer.

Parameters
bThe buffer.
Returns
The current position.