Example #1
0
/* strbuf_append_fmt_retry() can be used when the there is no known
 * upper bound for the output string. */
void strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...)
{
    va_list arg;
    int fmt_len, try1;
    int empty_len;

    /* If the first attempt to append fails, resize the buffer appropriately
     * and try again */
    for (try1 = 0; ; try1++) {
        va_start(arg, fmt);
        /* Append the new formatted string */
        /* fmt_len is the length of the string required, excluding the
         * trailing NULL */
        empty_len = strbuf_empty_length(s);
        /* Add 1 since there is also space to store the terminating NULL. */
        fmt_len = vsnprintf(s->buf + s->length, empty_len + 1, fmt, arg);
        va_end(arg);

        if (fmt_len <= empty_len)
            break;  /* SUCCESS */
        if (try1 > 0)
            die("BUG: length of formatted string changed");

        strbuf_resize(s, s->length + fmt_len);
    }

    s->length += fmt_len;
}
Example #2
0
File: strbuf.c Project: whatot/ma_c
void strbuf_append_string(strbuf_t *s, const char *str) {
	int space, i;

	space = strbuf_empty_length(s);

	for (i = 0; str[i]; i++) {
		if (space < 1) {
			strbuf_resize(s, s->length + 1);
			space = strbuf_empty_length(s);
		}

		s->buf[s->length] = str[i];
		s->length++;
		space--;
	}
}