Add strbuf_reset()

This commit is contained in:
Andrew Bettison 2012-06-25 16:31:19 +09:30
parent 8e0ba3f168
commit 5cff81e0c0
2 changed files with 22 additions and 1 deletions

View File

@ -26,8 +26,14 @@ static inline size_t min(size_t a, size_t b) {
strbuf strbuf_init(strbuf sb, char *buffer, size_t size)
{
sb->start = sb->current = buffer;
sb->start = buffer;
sb->end = sb->start + size - 1;
return strbuf_reset(sb);
}
strbuf strbuf_reset(strbuf sb)
{
sb->current = sb->start;
if (sb->start && sb->end >= sb->start) {
*sb->start = '\0';
*sb->end = '\0'; // should never get overwritten

View File

@ -184,6 +184,20 @@ __STRBUF_INLINE strbuf strbuf_make(char *buffer, size_t size) {
}
/** Reset a strbuf. The current position is set to the start of the buffer, so
* the next append will write at the start of the buffer. The prior contents
* of the buffer are forgotten and will be overwritten.
*
* Immediately following strbuf_reset(sb), the following properties hold:
* strbuf_len(sb) == 0
* strbuf_count(sb) == 0
* strbuf_str()[0] == '\0'
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
strbuf strbuf_reset(strbuf sb);
/** Append a null-terminated string to the strbuf up to a maximum number,
* truncating if necessary to avoid buffer overrun, and terminating with a nul
* which is not counted in the maximum. Return a pointer to the strbuf so that
@ -257,6 +271,7 @@ strbuf strbuf_putc(strbuf sb, char ch);
int strbuf_sprintf(strbuf sb, const char *fmt, ...);
int strbuf_vsprintf(strbuf sb, const char *fmt, va_list ap);
/** Return a pointer to the current null-terminated string in the strbuf.
*
* This is the same as the 'buffer' argument passed to the most recent