Skip to content

Commit

Permalink
strbuf: add strbuf_vaddf
Browse files Browse the repository at this point in the history
In a variable-args function, the code for writing into a strbuf is
non-trivial. We ended up cutting and pasting it in several places
because there was no vprintf-style function for strbufs (which in turn
was held up by a lack of va_copy).

Now that we have a fallback va_copy, we can add strbuf_vaddf, the
strbuf equivalent of vsprintf. And we can clean up the cut and paste
mess.

Signed-off-by: Jeff King <peff@peff.net>
Improved-by: Christian Couder <christian.couder@gmail.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
peff authored and gitster committed Feb 26, 2011
1 parent ab8632a commit ebeb609
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 63 deletions.
14 changes: 1 addition & 13 deletions fsck.c
Expand Up @@ -347,26 +347,14 @@ int fsck_object(struct object *obj, int strict, fsck_error error_func)
int fsck_error_function(struct object *obj, int type, const char *fmt, ...) int fsck_error_function(struct object *obj, int type, const char *fmt, ...)
{ {
va_list ap; va_list ap;
int len;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;


strbuf_addf(&sb, "object %s:", obj->sha1?sha1_to_hex(obj->sha1):"(null)"); strbuf_addf(&sb, "object %s:", obj->sha1?sha1_to_hex(obj->sha1):"(null)");


va_start(ap, fmt); va_start(ap, fmt);
len = vsnprintf(sb.buf + sb.len, strbuf_avail(&sb), fmt, ap); strbuf_vaddf(&sb, fmt, ap);
va_end(ap); va_end(ap);


if (len < 0)
len = 0;
if (len >= strbuf_avail(&sb)) {
strbuf_grow(&sb, len + 2);
va_start(ap, fmt);
len = vsnprintf(sb.buf + sb.len, strbuf_avail(&sb), fmt, ap);
va_end(ap);
if (len >= strbuf_avail(&sb))
die("this should not happen, your snprintf is broken");
}

error("%s", sb.buf); error("%s", sb.buf);
strbuf_release(&sb); strbuf_release(&sb);
return 1; return 1;
Expand Down
15 changes: 1 addition & 14 deletions merge-recursive.c
Expand Up @@ -137,7 +137,6 @@ static void flush_output(struct merge_options *o)
__attribute__((format (printf, 3, 4))) __attribute__((format (printf, 3, 4)))
static void output(struct merge_options *o, int v, const char *fmt, ...) static void output(struct merge_options *o, int v, const char *fmt, ...)
{ {
int len;
va_list ap; va_list ap;


if (!show(o, v)) if (!show(o, v))
Expand All @@ -148,21 +147,9 @@ static void output(struct merge_options *o, int v, const char *fmt, ...)
strbuf_setlen(&o->obuf, o->obuf.len + o->call_depth * 2); strbuf_setlen(&o->obuf, o->obuf.len + o->call_depth * 2);


va_start(ap, fmt); va_start(ap, fmt);
len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap); strbuf_vaddf(&o->obuf, fmt, ap);
va_end(ap); va_end(ap);


if (len < 0)
len = 0;
if (len >= strbuf_avail(&o->obuf)) {
strbuf_grow(&o->obuf, len + 2);
va_start(ap, fmt);
len = vsnprintf(o->obuf.buf + o->obuf.len, strbuf_avail(&o->obuf), fmt, ap);
va_end(ap);
if (len >= strbuf_avail(&o->obuf)) {
die("this should not happen, your snprintf is broken");
}
}
strbuf_setlen(&o->obuf, o->obuf.len + len);
strbuf_add(&o->obuf, "\n", 1); strbuf_add(&o->obuf, "\n", 1);
if (!o->buffer_output) if (!o->buffer_output)
flush_output(o); flush_output(o);
Expand Down
25 changes: 15 additions & 10 deletions strbuf.c
Expand Up @@ -195,24 +195,29 @@ void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)


void strbuf_addf(struct strbuf *sb, const char *fmt, ...) void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
{ {
int len;
va_list ap; va_list ap;
va_start(ap, fmt);
strbuf_vaddf(sb, fmt, ap);
va_end(ap);
}

void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
{
int len;
va_list cp;


if (!strbuf_avail(sb)) if (!strbuf_avail(sb))
strbuf_grow(sb, 64); strbuf_grow(sb, 64);
va_start(ap, fmt); va_copy(cp, ap);
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
va_end(ap); va_end(cp);
if (len < 0) if (len < 0)
die("your vsnprintf is broken"); die("BUG: your vsnprintf is broken (returned %d)", len);
if (len > strbuf_avail(sb)) { if (len > strbuf_avail(sb)) {
strbuf_grow(sb, len); strbuf_grow(sb, len);
va_start(ap, fmt);
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap); len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
va_end(ap); if (len > strbuf_avail(sb))
if (len > strbuf_avail(sb)) { die("BUG: your vsnprintf is broken (insatiable)");
die("this should not happen, your snprintf is broken");
}
} }
strbuf_setlen(sb, sb->len + len); strbuf_setlen(sb, sb->len + len);
} }
Expand Down
2 changes: 2 additions & 0 deletions strbuf.h
Expand Up @@ -120,6 +120,8 @@ extern void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *


__attribute__((format (printf,2,3))) __attribute__((format (printf,2,3)))
extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...); extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
__attribute__((format (printf,2,0)))
extern void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap);


extern size_t strbuf_fread(struct strbuf *, size_t, FILE *); extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
/* XXX: if read fails, any partial read is undone */ /* XXX: if read fails, any partial read is undone */
Expand Down
32 changes: 6 additions & 26 deletions trace.c
Expand Up @@ -64,28 +64,18 @@ static const char err_msg[] = "Could not trace into fd given by "


void trace_printf(const char *fmt, ...) void trace_printf(const char *fmt, ...)
{ {
struct strbuf buf; struct strbuf buf = STRBUF_INIT;
va_list ap; va_list ap;
int fd, len, need_close = 0; int fd, need_close = 0;


fd = get_trace_fd(&need_close); fd = get_trace_fd(&need_close);
if (!fd) if (!fd)
return; return;


set_try_to_free_routine(NULL); /* is never reset */ set_try_to_free_routine(NULL); /* is never reset */
strbuf_init(&buf, 64);
va_start(ap, fmt); va_start(ap, fmt);
len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); strbuf_vaddf(&buf, fmt, ap);
va_end(ap); va_end(ap);
if (len >= strbuf_avail(&buf)) {
strbuf_grow(&buf, len - strbuf_avail(&buf) + 128);
va_start(ap, fmt);
len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
va_end(ap);
if (len >= strbuf_avail(&buf))
die("broken vsnprintf");
}
strbuf_setlen(&buf, len);


write_or_whine_pipe(fd, buf.buf, buf.len, err_msg); write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
strbuf_release(&buf); strbuf_release(&buf);
Expand All @@ -96,28 +86,18 @@ void trace_printf(const char *fmt, ...)


void trace_argv_printf(const char **argv, const char *fmt, ...) void trace_argv_printf(const char **argv, const char *fmt, ...)
{ {
struct strbuf buf; struct strbuf buf = STRBUF_INIT;
va_list ap; va_list ap;
int fd, len, need_close = 0; int fd, need_close = 0;


fd = get_trace_fd(&need_close); fd = get_trace_fd(&need_close);
if (!fd) if (!fd)
return; return;


set_try_to_free_routine(NULL); /* is never reset */ set_try_to_free_routine(NULL); /* is never reset */
strbuf_init(&buf, 64);
va_start(ap, fmt); va_start(ap, fmt);
len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap); strbuf_vaddf(&buf, fmt, ap);
va_end(ap); va_end(ap);
if (len >= strbuf_avail(&buf)) {
strbuf_grow(&buf, len - strbuf_avail(&buf) + 128);
va_start(ap, fmt);
len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
va_end(ap);
if (len >= strbuf_avail(&buf))
die("broken vsnprintf");
}
strbuf_setlen(&buf, len);


sq_quote_argv(&buf, argv, 0); sq_quote_argv(&buf, argv, 0);
strbuf_addch(&buf, '\n'); strbuf_addch(&buf, '\n');
Expand Down

0 comments on commit ebeb609

Please sign in to comment.