Name

ne_buffer_append, ne_buffer_zappend, ne_buffer_qappend, ne_buffer_concat, ne_buffer_snprintf — append data to a string buffer

Synopsis

#include <ne_string.h>
void ne_buffer_append(ne_buffer *buf, const char *string, size_t len);
void ne_buffer_zappend(ne_buffer *buf, const char *string);
void ne_buffer_qappend(ne_buffer *buf, const char *string, size_t len);
void ne_buffer_concat(ne_buffer *buf, ...);
size_t ne_buffer_snprintf(ne_buffer *buf, size_t max, const char *format, ...);

Description

The ne_buffer_append and ne_buffer_zappend functions append a string to the end of a buffer; extending the buffer as necessary. The len passed to ne_buffer_append specifies the length of the string to append; there must be no NUL terminator in the first len bytes of the string. ne_buffer_zappend must be passed a NUL-terminated string.

The ne_buffer_qappend function behaves similarly to ne_buffer_append, except that any non-ASCII characters or ASCII control characters are escaped using C-style hex escaping. For example, the C string "foo<TAB>bar", where TAB represents ASCII character 9, would be appended as "foo\x09bar". Any NUL bytes within the length specified are also escaped. This function is useful to make strings from untrusted sources safe for logging or output in a user interface.

The ne_buffer_concat function takes a variable-length argument list; each argument must be a char * pointer to a NUL-terminated string. A NULL pointer must be given as the last argument to mark the end of the list. The strings are appended to the buffer in the order given. None of the strings passed to ne_buffer_concat are modified.

The ne_buffer_snprintf function behaves like snprintf, appending the output string formatted according to the format parameter. At most max bytes are written including the trailing NUL terminator.

Return value

The ne_buffer_snprintf function returns the number of bytes appended excluding the trailing NUL terminator.

Examples

The following code will output "Hello, world. And goodbye.".

ne_buffer *buf = ne_buffer_create();
ne_buffer_zappend(buf, "Hello");
ne_buffer_concat(buf, ", world. ", "And ", NULL);
ne_buffer_snprintf(buf, 10, "%s.", "goodbye");
puts(buf->data);
ne_buffer_destroy(buf);

See also

ne_buffer, ne_buffer_create, ne_buffer_destroy