Append directly to base string in vstr printf hooks
This commit is contained in:
@@ -141,14 +141,13 @@ static int custom_arginfo(const struct printf_info *info, size_t n, int *argtype
|
||||
*/
|
||||
static int custom_fmt_cb(Vstr_base *base, size_t pos, Vstr_fmt_spec *fmt_spec)
|
||||
{
|
||||
int i, written;
|
||||
char buf[PRINTF_BUF_LEN];
|
||||
int i;
|
||||
const void *args[ARGS_MAX];
|
||||
printf_hook_spec_t spec;
|
||||
printf_hook_handler_t *handler = printf_hooks[SPEC_TO_INDEX(fmt_spec->name[0])];
|
||||
printf_hook_data_t data = {
|
||||
.buf = buf,
|
||||
.buflen = sizeof(buf),
|
||||
.base = base,
|
||||
.pos = pos,
|
||||
};
|
||||
|
||||
for (i = 0; i < handler->numargs; i++)
|
||||
@@ -168,11 +167,7 @@ static int custom_fmt_cb(Vstr_base *base, size_t pos, Vstr_fmt_spec *fmt_spec)
|
||||
spec.minus = fmt_spec->fmt_minus;
|
||||
spec.width = fmt_spec->fmt_field_width;
|
||||
|
||||
written = handler->hook(&data, &spec, args);
|
||||
if (written > 0)
|
||||
{
|
||||
vstr_add_buf(base, pos, buf, written);
|
||||
}
|
||||
handler->hook(&data, &spec, args);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -115,27 +115,22 @@ int vstr_wrapper_vasprintf(char **str, const char *format, va_list ap);
|
||||
struct printf_hook_data_t {
|
||||
|
||||
/**
|
||||
* Buffer to write to
|
||||
* Base to append printf to
|
||||
*/
|
||||
char *buf;
|
||||
Vstr_base *base;
|
||||
|
||||
/**
|
||||
* Size of the buffer
|
||||
* Position in base to write to
|
||||
*/
|
||||
size_t buflen;
|
||||
size_t pos;
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper macro to be used in printf hook callbacks.
|
||||
*/
|
||||
#define print_in_hook(data, fmt, ...) ({\
|
||||
int _written = snprintf(data->buf, data->buflen, fmt, ##__VA_ARGS__);\
|
||||
if (_written < 0 || _written >= data->buflen)\
|
||||
{\
|
||||
_written = data->buflen - 1;\
|
||||
}\
|
||||
data->buf += _written;\
|
||||
data->buflen -= _written;\
|
||||
int _written = vstr_add_fmt(data->base, data->pos, fmt, ##__VA_ARGS__);\
|
||||
data->pos += _written;\
|
||||
_written;\
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user