Exemple #1
0
static inline unsigned char *buf_get_target_for(struct Buf *buf, unsigned len)
{
	if (buf->pos + len <= buf->alloc)
		return buf->ptr + buf->pos;
	else
		return buf_enlarge(buf, len);
}
Exemple #2
0
static void buf_puts(struct buffer *bs, const char *s)
{
	size_t len = strlen(s);

	buf_enlarge(bs, len + 1);
	memcpy(bs->data + bs->used, s, len + 1);
	bs->used += len;
}
Exemple #3
0
static inline int buf_put(struct Buf *buf, unsigned char c)
{
	if (buf->pos < buf->alloc) {
		buf->ptr[buf->pos++] = c;
		return 1;
	} else if (buf_enlarge(buf, 1)) {
		buf->ptr[buf->pos++] = c;
		return 1;
	}
	return 0;
}
Exemple #4
0
static void buf_putc_careful(struct buffer *bs, int c)
{
	if (isprint(c) || c == '\a' || c == '\t' || c == '\r' || c == '\n') {
		buf_enlarge(bs, 1);
		bs->data[bs->used++] = c;
	} else if (!isascii(c))
		buf_printf(bs, "\\%3o", (unsigned char)c);
	else {
		char tmp[] = { '^', c ^ 0x40, '\0' };
		buf_puts(bs, tmp);
	}
}
Exemple #5
0
/*
 * Add <len> bytes, starting at <data> to <buf>.
 */
Buffer *bufAdd(Buffer *buf, const void *data, size_t len)
{
    dbgAssert(stderr, len >= 0, "bufAdd called with length %zd", len);

    buf_enlarge(buf, len);

    memcpy(buf->data + buf->act_len, data, len);

    buf->act_len += len;

    buf->data[buf->act_len] = '\0';

    return buf;
}
Exemple #6
0
static void buf_printf(struct buffer *bs, const char *fmt, ...)
{
	int rc;
	va_list ap;
	size_t limit;

	buf_enlarge(bs, 0);	/* default size */
	limit = bs->sz - bs->used;

	va_start(ap, fmt);
	rc = vsnprintf(bs->data + bs->used, limit, fmt, ap);
	va_end(ap);

	if (rc >= 0 && (size_t) rc >= limit) {	/* not enough, enlarge */
		buf_enlarge(bs, (size_t)rc + 1);
		limit = bs->sz - bs->used;
		va_start(ap, fmt);
		rc = vsnprintf(bs->data  + bs->used, limit, fmt, ap);
		va_end(ap);
	}

	if (rc > 0)
		bs->used += rc;
}
Exemple #7
0
/*
 * Append a string to <buf>, formatted according to <fmt> and with the subsequent parameters
 * contained in <ap>.
 */
Buffer *bufAddV(Buffer *buf, const char *fmt, va_list ap)
{
    size_t size;
    va_list my_ap;

    va_copy(my_ap, ap);
    size = vsnprintf(NULL, 0, fmt, my_ap);
    va_end(my_ap);

    buf_enlarge(buf, size + 1);

    va_copy(my_ap, ap);
    vsnprintf(buf->data + buf->act_len, size + 1, fmt, my_ap);
    va_end(my_ap);

    buf->act_len += size;

    return buf;
}