2012-05-19 04:32:03 +00:00
|
|
|
/*
|
|
|
|
Serval string buffer primitives
|
2012-07-30 05:20:08 +00:00
|
|
|
Copyright (C) 2012 Serval Project Inc.
|
2012-05-20 03:32:41 +00:00
|
|
|
|
2012-05-19 04:32:03 +00:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
2012-05-20 03:32:41 +00:00
|
|
|
|
2012-05-19 04:32:03 +00:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2012-05-20 03:32:41 +00:00
|
|
|
|
2012-05-19 04:32:03 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __STRBUF_H__
|
|
|
|
#define __STRBUF_H__
|
|
|
|
|
|
|
|
/*
|
|
|
|
A strbuf provides a convenient set of primitives for assembling a
|
2012-07-26 07:17:17 +00:00
|
|
|
nul-terminated string in a fixed-size, caller-provided backing buffer,
|
2012-05-19 04:32:03 +00:00
|
|
|
using a sequence of append operations.
|
|
|
|
|
2012-08-06 06:09:08 +00:00
|
|
|
An append operation that would overflow the buffer is truncated with a nul
|
|
|
|
terminator and the "overrun" property of the strbuf becomes true until the
|
|
|
|
next strbuf_init() or strbuf_trunc(). Any append to an overrun strbuf will
|
|
|
|
be fully truncated, ie, nothing more will be appended to the buffer.
|
2012-05-20 03:32:41 +00:00
|
|
|
|
2012-05-19 04:32:03 +00:00
|
|
|
The string in the buffer is guaranteed to always be nul terminated, which
|
|
|
|
means that the maximum strlen() of the assembled string is one less than
|
|
|
|
the buffer size. In other words, the following invariants always hold:
|
|
|
|
strbuf_len(sb) < strbuf_size(sb)
|
|
|
|
strbuf_str(sb)[strbuf_len(sb)] == '\0'
|
|
|
|
|
|
|
|
char buf[100];
|
2013-03-26 07:54:13 +00:00
|
|
|
struct strbuf b;
|
2012-05-19 04:32:03 +00:00
|
|
|
strbuf_init(&b, buf, sizeof buf);
|
2012-05-20 03:32:41 +00:00
|
|
|
strbuf_puts(&b, "text");
|
2012-05-19 04:32:03 +00:00
|
|
|
strbuf_sprintf(&b, "fmt", val...);
|
2013-09-23 01:41:58 +00:00
|
|
|
if (strbuf_overrun(&b))
|
2012-05-19 04:32:03 +00:00
|
|
|
// error...
|
|
|
|
else
|
|
|
|
// use buf
|
|
|
|
|
|
|
|
A strbuf counts the total number of chars appended to it, even ones that
|
|
|
|
were truncated. This count is always available via strbuf_count().
|
|
|
|
|
|
|
|
A NULL buffer can be provided. This causes the strbuf operations to
|
|
|
|
perform all character counting and truncation calculations as usual, but
|
2012-08-06 06:09:08 +00:00
|
|
|
not actually assemble the string; it is as though the strbuf is permanently
|
|
|
|
overrun, but no nul terminator is appended. This allows a strbuf to be
|
|
|
|
used for calculating the size needed for a buffer, which the caller may
|
|
|
|
then allocate and replay the same operations to fill.
|
|
|
|
|
|
|
|
A buffer length of -1 can be given. This causes the strbuf operations to
|
|
|
|
treat the buffer as unlimited in size. This is useful for when the caller
|
|
|
|
is 100% certain that the strbuf will not be overrun. For example, if the
|
|
|
|
required buffer size was already computed by a preliminary run of the same
|
|
|
|
strbuf operations on a NULL buffer, and the necessary size allocated.
|
|
|
|
|
|
|
|
The strbuf operations will never write any data beyond the length of the
|
|
|
|
assembled string plus one for the nul terminator. So, for example, the
|
|
|
|
following code will never alter buf[4]:
|
|
|
|
|
|
|
|
char buf[5];
|
|
|
|
buf[4] = 'x';
|
|
|
|
strbuf b;
|
|
|
|
strbuf_init(b, buf, sizeof buf);
|
|
|
|
strbuf_puts(&b, "abc");
|
|
|
|
assert buf[4] == 'x'; // always passes
|
2012-05-19 04:32:03 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2013-12-11 04:04:34 +00:00
|
|
|
#include <stdint.h> // for SIZE_MAX on Debian/Unbuntu/...
|
|
|
|
#include <limits.h> // for SIZE_MAX on Android
|
2012-05-19 04:32:03 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2012-05-20 03:32:41 +00:00
|
|
|
#include <alloca.h>
|
2014-01-20 05:07:14 +00:00
|
|
|
#include <assert.h>
|
2012-05-20 03:32:41 +00:00
|
|
|
|
|
|
|
#ifndef __STRBUF_INLINE
|
|
|
|
# if __GNUC__ && !__GNUC_STDC_INLINE__
|
|
|
|
# define __STRBUF_INLINE extern inline
|
|
|
|
# else
|
|
|
|
# define __STRBUF_INLINE inline
|
|
|
|
# endif
|
|
|
|
#endif
|
2012-05-19 04:32:03 +00:00
|
|
|
|
2012-05-20 03:32:41 +00:00
|
|
|
struct strbuf {
|
2012-08-06 06:09:08 +00:00
|
|
|
char *start; // NULL after strbuf_init(buffer=NULL)
|
|
|
|
char *end; // NULL after strbuf_init(size=-1), otherwise end=&start[size-1]
|
2012-05-19 04:32:03 +00:00
|
|
|
char *current;
|
2012-05-20 03:32:41 +00:00
|
|
|
};
|
|
|
|
|
2012-07-05 05:28:28 +00:00
|
|
|
/* Static constant for initialising a struct strbuf to empty:
|
|
|
|
* struct strbuf ssb = STRUCT_STRBUF_EMPTY;
|
|
|
|
* Immediately following this assignment, the following properties hold:
|
|
|
|
* strbuf_is_empty(&ssb)
|
|
|
|
* strbuf_len(&ssb) == 0
|
|
|
|
* strbuf_count(&ssb) == 0
|
|
|
|
* strbuf_str(&ssb) == NULL
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-07-13 03:00:59 +00:00
|
|
|
#define STRUCT_STRBUF_EMPTY ((struct strbuf){NULL, NULL, NULL})
|
2012-07-05 05:28:28 +00:00
|
|
|
|
2012-05-20 03:32:41 +00:00
|
|
|
typedef struct strbuf *strbuf;
|
|
|
|
typedef const struct strbuf *const_strbuf;
|
|
|
|
|
|
|
|
/** The number of bytes occupied by a strbuf (not counting its backing buffer).
|
|
|
|
*/
|
|
|
|
#define SIZEOF_STRBUF (sizeof(struct strbuf))
|
|
|
|
|
2012-06-12 08:42:36 +00:00
|
|
|
/** Convenience macro for allocating a strbuf and its backing buffer on the
|
2012-05-20 03:32:41 +00:00
|
|
|
* stack within the calling function. The returned strbuf is only valid for
|
|
|
|
* the duration of the function, so it must not be returned. See alloca(3) for
|
|
|
|
* more information.
|
|
|
|
*
|
|
|
|
* void func() {
|
|
|
|
* strbuf b = strbuf_alloca(1024);
|
|
|
|
* strbuf_puts(b, "some text");
|
|
|
|
* strbuf_puts(b, " some more text");
|
|
|
|
* printf("%s\n", strbuf_str(b));
|
|
|
|
* }
|
2012-06-12 08:42:36 +00:00
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
2012-05-20 03:32:41 +00:00
|
|
|
*/
|
2012-08-06 06:09:08 +00:00
|
|
|
#define strbuf_alloca(size) strbuf_make(alloca(SIZEOF_STRBUF + (size)), SIZEOF_STRBUF + (size))
|
2012-05-20 03:32:41 +00:00
|
|
|
|
2014-01-20 05:07:14 +00:00
|
|
|
/** Convenience macro that calls strbuf_alloca() to allocate a large enough
|
|
|
|
* buffer to hold the entire content produced by a given expression that
|
|
|
|
* appends to the strbuf. The first strbuf_alloca() will use the supplied
|
|
|
|
* initial length, and if that overruns, then a second strbuf_alloca() will use
|
|
|
|
* the strbuf_count() from the first pass, so as long as the expression is
|
|
|
|
* stable (ie, always produces the same output), the final assert() will not
|
|
|
|
* be triggered.
|
|
|
|
*
|
|
|
|
* strbuf b;
|
|
|
|
* STRBUF_ALLOCA_FIT(b, 20, (strbuf_append_variable_content(b, ...)));
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
#define STRBUF_ALLOCA_FIT(__SB, __INITIAL_LEN, __EXPR) \
|
|
|
|
do { \
|
|
|
|
__SB = strbuf_alloca((__INITIAL_LEN) + 1); \
|
|
|
|
__EXPR; \
|
|
|
|
if (strbuf_overrun(__SB)) { \
|
|
|
|
__SB = strbuf_alloca(strbuf_count(__SB) + 1); \
|
|
|
|
__EXPR; \
|
|
|
|
} \
|
|
|
|
assert(!strbuf_overrun(__SB)); \
|
|
|
|
} while (0)
|
2012-05-20 03:32:41 +00:00
|
|
|
|
2012-06-12 08:42:36 +00:00
|
|
|
/** Convenience macro for filling a strbuf from the calling function's
|
2013-09-19 07:04:02 +00:00
|
|
|
* printf(3)-like variadic arguments.
|
2012-05-20 03:32:41 +00:00
|
|
|
*
|
2012-06-12 08:42:36 +00:00
|
|
|
* #include <stdarg.h>
|
|
|
|
*
|
|
|
|
* void funcf(const char *format, ...) {
|
2012-07-05 05:28:28 +00:00
|
|
|
* strbuf b = strbuf_alloca(1024);
|
|
|
|
* strbuf_va_printf(b, format);
|
2012-06-12 08:42:36 +00:00
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
#define strbuf_va_printf(sb,fmt) do { \
|
|
|
|
va_list __strbuf_ap; \
|
|
|
|
va_start(__strbuf_ap, fmt); \
|
2013-09-19 07:04:02 +00:00
|
|
|
strbuf_vsprintf(sb, (fmt), __strbuf_ap); \
|
|
|
|
va_end(__strbuf_ap); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/** Convenience macro for filling a strbuf from the calling function's va_list
|
|
|
|
* variadic argument pointer.
|
|
|
|
*
|
|
|
|
* #include <stdarg.h>
|
|
|
|
*
|
|
|
|
* void funcf(const char *format, va_list ap) {
|
|
|
|
* strbuf b = strbuf_alloca(1024);
|
|
|
|
* strbuf_va_vprintf(b, format, ap);
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
#define strbuf_va_vprintf(sb,fmt,ap) do { \
|
|
|
|
va_list __strbuf_ap; \
|
|
|
|
va_copy(__strbuf_ap, (ap)); \
|
|
|
|
strbuf_vsprintf(sb, (fmt), __strbuf_ap); \
|
2012-06-12 08:42:36 +00:00
|
|
|
va_end(__strbuf_ap); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/** Convenience macro to allocate a strbuf for use within the calling function,
|
|
|
|
* based on a caller-supplied backing buffer. The returned strbuf is only valid
|
|
|
|
* for the duration of the function, so it must not be returned. See alloca(3)
|
|
|
|
* for more information. However, the backing buffer may have any scope.
|
|
|
|
*
|
2012-05-20 03:32:41 +00:00
|
|
|
* void func(char *buf, size_t len) {
|
|
|
|
* strbuf b = strbuf_local(buf, len);
|
|
|
|
* strbuf_puts(b, "some text");
|
|
|
|
* strbuf_puts(b, " some more text");
|
|
|
|
* printf("%s\n", strbuf_str(b));
|
|
|
|
* }
|
2012-06-12 08:42:36 +00:00
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
2012-05-20 03:32:41 +00:00
|
|
|
*/
|
|
|
|
#define strbuf_local(buf,len) strbuf_init(alloca(SIZEOF_STRBUF), (buf), (len))
|
2012-05-19 04:32:03 +00:00
|
|
|
|
2012-05-20 03:32:41 +00:00
|
|
|
|
|
|
|
/** Initialise a strbuf with a caller-supplied backing buffer. The current
|
|
|
|
* backing buffer and its contents are forgotten, and all strbuf operations
|
|
|
|
* henceforward will operate on the new backing buffer. Returns its first
|
|
|
|
* argument.
|
2012-05-19 04:32:03 +00:00
|
|
|
*
|
|
|
|
* Immediately following strbuf_init(sb,b,n), the following properties hold:
|
|
|
|
* strbuf_str(sb) == b
|
|
|
|
* strbuf_size(sb) == n
|
|
|
|
* strbuf_len(sb) == 0
|
|
|
|
* strbuf_count(sb) == 0
|
|
|
|
* b == NULL || b[0] == '\0'
|
|
|
|
*
|
2012-07-05 05:28:28 +00:00
|
|
|
* If the 'buffer' argument is NULL, the strbuf is marked as "empty" and all
|
|
|
|
* subsequent strbuf operations will all act as usual with the sole exception
|
|
|
|
* that no chars will be copied into a backing buffer. This allows strbuf to
|
|
|
|
* be used for summing the lengths of strings.
|
2012-05-19 04:32:03 +00:00
|
|
|
*
|
2012-05-20 03:32:41 +00:00
|
|
|
* If the 'size' argument is zero, then strbuf does not write into its backing
|
|
|
|
* buffer, not even a terminating nul.
|
2012-05-19 04:32:03 +00:00
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-08-06 06:09:08 +00:00
|
|
|
strbuf strbuf_init(strbuf sb, char *buffer, ssize_t size);
|
2012-05-19 04:32:03 +00:00
|
|
|
|
2012-05-20 03:32:41 +00:00
|
|
|
|
|
|
|
/** Initialise a strbuf and its backing buffer inside the caller-supplied
|
|
|
|
* buffer of the given size. If the 'size' argument is less than
|
|
|
|
* SIZEOF_STRBUF, then strbuf_make() returns NULL.
|
|
|
|
*
|
|
|
|
* Immediately following sb = strbuf_make(buf,len) where len >= SIZEOF_STRBUF,
|
|
|
|
* the following properties hold:
|
|
|
|
* (char*) sb == buf
|
|
|
|
* strbuf_str(sb) == &buf[SIZEOF_STRBUF];
|
|
|
|
* strbuf_size(sb) == len - SIZEOF_STRBUF;
|
|
|
|
* strbuf_len(sb) == 0
|
|
|
|
* strbuf_count(sb) == 0
|
2012-07-05 05:28:28 +00:00
|
|
|
* strbuf_str(sb)[0] == '\0'
|
2012-05-20 03:32:41 +00:00
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
__STRBUF_INLINE strbuf strbuf_make(char *buffer, size_t size) {
|
|
|
|
return size < SIZEOF_STRBUF ? NULL : strbuf_init((strbuf) buffer, buffer + SIZEOF_STRBUF, size - SIZEOF_STRBUF);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-25 07:01:19 +00:00
|
|
|
/** 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
|
2012-07-05 05:28:28 +00:00
|
|
|
* strbuf_str(sb) == NULL || strbuf_str(sb)[0] == '\0'
|
2012-06-25 07:01:19 +00:00
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_reset(strbuf sb);
|
|
|
|
|
|
|
|
|
2012-07-26 07:17:17 +00:00
|
|
|
/** Append a nul-terminated string to the strbuf up to a maximum number,
|
2012-05-20 03:32:41 +00:00
|
|
|
* 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
|
|
|
|
* concatenations can be chained in a single line: eg,
|
2013-04-04 07:11:18 +00:00
|
|
|
* strbuf_ncat(strbuf_ncat(sb, "abc", 1), "def", 2) gives a strbuf containing
|
|
|
|
* "ade";
|
2012-05-19 04:32:03 +00:00
|
|
|
*
|
|
|
|
* After these operations:
|
|
|
|
* n = strbuf_len(sb);
|
|
|
|
* c = strbuf_count(sb);
|
|
|
|
* strbuf_ncat(text, len);
|
|
|
|
* the following invariants hold:
|
2012-05-20 03:32:41 +00:00
|
|
|
* strbuf_count(sb) == c + min(strlen(text), len)
|
2012-05-19 04:32:03 +00:00
|
|
|
* strbuf_len(sb) >= n
|
|
|
|
* strbuf_len(sb) <= n + len
|
2012-05-20 03:32:41 +00:00
|
|
|
* strbuf_len(sb) <= n + strlen(text)
|
2012-05-19 04:32:03 +00:00
|
|
|
* strbuf_str(sb) == NULL || strbuf_len(sb) == n || strncmp(strbuf_str(sb) + n, text, strbuf_len(sb) - n) == 0
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-05-20 03:32:41 +00:00
|
|
|
strbuf strbuf_ncat(strbuf sb, const char *text, size_t len);
|
2012-05-19 04:32:03 +00:00
|
|
|
|
|
|
|
|
2012-07-26 07:17:17 +00:00
|
|
|
/** Append a nul-terminated string to the strbuf, truncating if necessary to
|
2012-05-19 04:32:03 +00:00
|
|
|
* avoid buffer overrun. Return a pointer to the strbuf so that concatenations
|
2012-05-20 03:32:41 +00:00
|
|
|
* can be chained in a single line: strbuf_puts(strbuf_puts(sb, "a"), "b");
|
2012-05-19 04:32:03 +00:00
|
|
|
*
|
|
|
|
* After these operations:
|
|
|
|
* n = strbuf_len(sb);
|
|
|
|
* c = strbuf_count(sb);
|
2012-05-20 03:32:41 +00:00
|
|
|
* strbuf_puts(text);
|
2012-05-19 04:32:03 +00:00
|
|
|
* the following invariants hold:
|
|
|
|
* strbuf_count(sb) == c + strlen(text)
|
|
|
|
* strbuf_len(sb) >= n
|
|
|
|
* strbuf_len(sb) <= n + strlen(text)
|
|
|
|
* strbuf_str(sb) == NULL || strbuf_len(sb) == n || strncmp(strbuf_str(sb) + n, text, strbuf_len(sb) - n) == 0
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-05-20 03:32:41 +00:00
|
|
|
strbuf strbuf_puts(strbuf sb, const char *text);
|
|
|
|
|
|
|
|
|
2013-10-09 08:24:21 +00:00
|
|
|
/** Append binary data strbuf, as up to 'len' characters of uppercase
|
|
|
|
* hexadecimal format, truncating if necessary to avoid buffer overrun. Return
|
|
|
|
* a pointer to the strbuf.
|
2012-07-20 08:47:04 +00:00
|
|
|
*
|
|
|
|
* After these operations:
|
|
|
|
* n = strbuf_len(sb);
|
|
|
|
* c = strbuf_count(sb);
|
2013-10-09 08:24:21 +00:00
|
|
|
* strbuf_tohex(len, data);
|
2012-07-20 08:47:04 +00:00
|
|
|
* the following invariants hold:
|
2013-10-09 08:24:21 +00:00
|
|
|
* strbuf_count(sb) == c + len
|
2012-07-20 08:47:04 +00:00
|
|
|
* strbuf_len(sb) >= n
|
2013-10-09 08:24:21 +00:00
|
|
|
* strbuf_len(sb) <= n + len
|
2012-07-20 08:47:04 +00:00
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2013-10-09 08:24:21 +00:00
|
|
|
strbuf strbuf_tohex(strbuf sb, size_t strlen, const unsigned char *data);
|
2012-07-20 08:47:04 +00:00
|
|
|
|
|
|
|
|
2012-05-20 03:32:41 +00:00
|
|
|
/** Append a single character to the strbuf if there is space, and place a
|
|
|
|
* terminating nul after it. Return a pointer to the strbuf so that
|
|
|
|
* concatenations can be chained in a single line.
|
|
|
|
*
|
|
|
|
* After these operations:
|
|
|
|
* n = strbuf_len(sb);
|
|
|
|
* c = strbuf_count(sb);
|
|
|
|
* strbuf_putc(ch);
|
|
|
|
* the following invariants hold:
|
|
|
|
* strbuf_count(sb) == c + 1
|
|
|
|
* strbuf_len(sb) >= n
|
|
|
|
* strbuf_len(sb) <= n + 1
|
|
|
|
* strbuf_str(sb) == NULL || strbuf_len(sb) == n || strbuf_str(sb)[n] == ch
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_putc(strbuf sb, char ch);
|
2012-05-19 04:32:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
/** Append the results of sprintf(fmt,...) to the string buffer, truncating if
|
2015-03-16 12:03:11 +00:00
|
|
|
* necessary to avoid buffer overrun. Return a pointer to the strbuf.
|
2012-05-19 04:32:03 +00:00
|
|
|
*
|
2012-05-20 03:32:41 +00:00
|
|
|
* This is equivalent to char tmp[...]; sprintf(tmp, fmt, ...); strbuf_puts(tmp);
|
2012-05-19 04:32:03 +00:00
|
|
|
* assuming that tmp[] is large enough to contain the entire string produced by
|
|
|
|
* the sprintf().
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2015-03-16 12:03:11 +00:00
|
|
|
strbuf strbuf_sprintf(strbuf sb, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
|
|
|
|
strbuf strbuf_vsprintf(strbuf sb, const char *fmt, va_list ap);
|
2012-05-19 04:32:03 +00:00
|
|
|
|
2012-06-25 07:01:19 +00:00
|
|
|
|
2012-07-26 07:17:17 +00:00
|
|
|
/** Return a pointer to the current nul-terminated string in the strbuf.
|
2012-05-19 04:32:03 +00:00
|
|
|
*
|
|
|
|
* This is the same as the 'buffer' argument passed to the most recent
|
|
|
|
* strbuf_init(). If the caller still has that pointer, then can safely use it
|
|
|
|
* instead of calling strbuf_str().
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-05-20 03:32:41 +00:00
|
|
|
__STRBUF_INLINE char *strbuf_str(const_strbuf sb) {
|
2012-05-19 04:32:03 +00:00
|
|
|
return sb->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-26 07:17:17 +00:00
|
|
|
/** Return a pointer to the nul-terminator at the end of the string in the
|
|
|
|
* strbuf.
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
__STRBUF_INLINE char *strbuf_end(const_strbuf sb) {
|
2012-08-06 06:09:08 +00:00
|
|
|
return sb->end && sb->current > sb->end ? sb->end : sb->current;
|
2012-07-26 07:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-19 04:32:03 +00:00
|
|
|
/** Return a pointer to the substring starting at a given offset. If the
|
|
|
|
* offset is negative, then it is taken from the end of the string, ie, the
|
|
|
|
* length of the string is added to it. The returned pointer always points
|
|
|
|
* within the string. If offset >= strbuf_len(sb), it points to the
|
|
|
|
* terminating nul. If offset <= -strbuf_len(sb) then it points to
|
|
|
|
* strbuf_str(sb).
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-05-20 03:32:41 +00:00
|
|
|
char *strbuf_substr(const_strbuf sb, int offset);
|
2012-05-19 04:32:03 +00:00
|
|
|
|
|
|
|
|
2012-07-10 10:06:25 +00:00
|
|
|
/** Truncate the string in the strbuf to a given offset. If the offset is
|
|
|
|
* negative, then it is taken from the end of the string, ie, the length of the
|
|
|
|
* string is added to it. If the string is shorter than the given offset, then
|
|
|
|
* it is unchanged. Otherwise, a terminating nul char is written at the offset
|
|
|
|
* and the string's length truncated accordingly. Return a pointer to the
|
|
|
|
* strbuf so that operations can be chained in a single line.
|
|
|
|
*
|
2012-07-11 02:11:15 +00:00
|
|
|
* After the operations:
|
|
|
|
* count = strbuf_count(sb);
|
2012-07-10 10:06:25 +00:00
|
|
|
* len = strbuf_len(sb);
|
|
|
|
* strbuf_trunc(sb, off);
|
|
|
|
* the following invariants hold:
|
2012-07-11 02:11:15 +00:00
|
|
|
* if count <= off, sb is unchanged:
|
|
|
|
* strbuf_count(sb) == count
|
|
|
|
* strbuf_len(sb) == len
|
|
|
|
* if len <= off < count:
|
|
|
|
* strbuf_count(sb) == off
|
|
|
|
* strbuf_len(sb) == len
|
|
|
|
* if 0 <= off < len:
|
2012-07-10 10:06:25 +00:00
|
|
|
* strbuf_count(sb) == off
|
|
|
|
* strbuf_len(sb) == off
|
2012-07-11 02:11:15 +00:00
|
|
|
* if -len <= off < 0:
|
2012-07-10 10:06:25 +00:00
|
|
|
* strbuf_count(sb) == len + off
|
|
|
|
* strbuf_len(sb) == len + off
|
2012-07-11 02:11:15 +00:00
|
|
|
* if off < -len:
|
2012-07-10 10:06:25 +00:00
|
|
|
* strbuf_count(sb) == 0
|
|
|
|
* strbuf_len(sb) == 0
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
strbuf strbuf_trunc(strbuf sb, int offset);
|
|
|
|
|
|
|
|
|
2012-07-05 05:28:28 +00:00
|
|
|
/** Return true if the given strbuf is "empty", ie, not modified since being
|
|
|
|
* initialised to STRUCT_STRBUF_EMPTY or with strbuf_init(sb, NULL, 0);
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
__STRBUF_INLINE size_t strbuf_is_empty(const_strbuf sb) {
|
2012-07-13 03:00:59 +00:00
|
|
|
return sb->start == NULL && sb->end == NULL && sb->current == NULL;
|
2012-07-05 05:28:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-04 07:11:18 +00:00
|
|
|
/** Return the size of the backing buffer. Return -1 if the buffer is of
|
|
|
|
* undefined size.
|
2012-05-19 04:32:03 +00:00
|
|
|
*
|
|
|
|
* This is the same as the 'size' argument passed to the most recent
|
|
|
|
* strbuf_init().
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-08-06 06:09:08 +00:00
|
|
|
__STRBUF_INLINE ssize_t strbuf_size(const_strbuf sb) {
|
|
|
|
return sb->end ? sb->end - sb->start + 1 : -1;
|
2012-05-19 04:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Return length of current string in the strbuf, not counting the terminating
|
|
|
|
* nul.
|
|
|
|
*
|
|
|
|
* Invariant: strbuf_len(sb) == strlen(strbuf_str(sb))
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-05-20 03:32:41 +00:00
|
|
|
__STRBUF_INLINE size_t strbuf_len(const_strbuf sb) {
|
2012-08-06 06:09:08 +00:00
|
|
|
return strbuf_end(sb) - sb->start;
|
2012-05-19 04:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-09 05:59:06 +00:00
|
|
|
/** Return remaining space in the strbuf, not counting the terminating nul.
|
|
|
|
* Return SIZE_MAX if the strbuf is of undefined size.
|
2013-04-04 07:11:18 +00:00
|
|
|
*
|
|
|
|
* Invariant: strbuf_size(sb) == -1 || strbuf_remaining(sb) == strbuf_size(sb) - strbuf_len(sb)
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
__STRBUF_INLINE size_t strbuf_remaining(const_strbuf sb) {
|
2013-12-09 05:59:06 +00:00
|
|
|
return !sb->end ? SIZE_MAX : sb->current > sb->end ? 0 : (size_t)(sb->end - sb->current);
|
2013-04-04 07:11:18 +00:00
|
|
|
}
|
|
|
|
|
2012-05-19 04:32:03 +00:00
|
|
|
/** Return the number of chars appended to the strbuf so far, not counting the
|
|
|
|
* terminating nul.
|
|
|
|
*
|
|
|
|
* Invariant: strbuf_len(sb) <= strbuf_count(sb)
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-05-20 03:32:41 +00:00
|
|
|
__STRBUF_INLINE size_t strbuf_count(const_strbuf sb) {
|
2012-05-19 04:32:03 +00:00
|
|
|
return sb->current - sb->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Return true iff the strbuf has been overrun, ie, any appended string has
|
|
|
|
* been truncated since strbuf_init().
|
|
|
|
*
|
|
|
|
* Invariant: strbuf_overrun(sb) == strbuf_count(sb) != strbuf_len(sb)
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
2012-05-20 03:32:41 +00:00
|
|
|
__STRBUF_INLINE int strbuf_overrun(const_strbuf sb) {
|
2012-08-06 06:09:08 +00:00
|
|
|
return sb->end && sb->current > sb->end;
|
2012-05-19 04:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // __STRBUF_H__
|