mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-05 21:42:47 -05:00
Generalise strbuf_catf() into put_fmt().
marshal.h now provides a macro put_fmt() which allows you to write arbitrary printf-formatted data to an arbitrary BinarySink. We already had this facility for strbufs in particular, in the form of strbuf_catf(). That was able to take advantage of knowing the inner structure of a strbuf to minimise memory allocation (it would snprintf directly into the strbuf's existing buffer if possible). For a general black-box BinarySink we can't do that, so instead we dupvprintf into a temporary buffer. For consistency, I've removed strbuf_catf, and converted all uses of it into the new put_fmt - and I've also added an extra vtable method in the BinarySink API, so that put_fmt can still use strbuf_catf's more efficient memory management when talking to a strbuf, and fall back to the simpler strategy when that's not available.
This commit is contained in:
@ -60,10 +60,21 @@ static void strbuf_BinarySink_write(
|
||||
memcpy(strbuf_append(buf_o, len), data, len);
|
||||
}
|
||||
|
||||
static void strbuf_BinarySink_writefmtv(
|
||||
BinarySink *bs, const char *fmt, va_list ap)
|
||||
{
|
||||
strbuf *buf_o = BinarySink_DOWNCAST(bs, strbuf);
|
||||
struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
|
||||
STRBUF_SET_PTR(buf, dupvprintf_inner(buf->visible.s, buf->visible.len,
|
||||
&buf->size, fmt, ap));
|
||||
buf->visible.len += strlen(buf->visible.s + buf->visible.len);
|
||||
}
|
||||
|
||||
static strbuf *strbuf_new_general(bool nm)
|
||||
{
|
||||
struct strbuf_impl *buf = snew(struct strbuf_impl);
|
||||
BinarySink_INIT(&buf->visible, strbuf_BinarySink_write);
|
||||
buf->visible.binarysink_->writefmtv = strbuf_BinarySink_writefmtv;
|
||||
buf->visible.len = 0;
|
||||
buf->size = 512;
|
||||
buf->nm = nm;
|
||||
@ -89,21 +100,6 @@ char *strbuf_to_str(strbuf *buf_o)
|
||||
sfree(buf);
|
||||
return ret;
|
||||
}
|
||||
void strbuf_catfv(strbuf *buf_o, const char *fmt, va_list ap)
|
||||
{
|
||||
struct strbuf_impl *buf = container_of(buf_o, struct strbuf_impl, visible);
|
||||
STRBUF_SET_PTR(buf, dupvprintf_inner(buf->visible.s, buf->visible.len,
|
||||
&buf->size, fmt, ap));
|
||||
buf->visible.len += strlen(buf->visible.s + buf->visible.len);
|
||||
}
|
||||
void strbuf_catf(strbuf *buf_o, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
strbuf_catfv(buf_o, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
strbuf *strbuf_new_for_agent_query(void)
|
||||
{
|
||||
strbuf *buf = strbuf_new();
|
||||
|
Reference in New Issue
Block a user