From bc5663168d8fd42704d6a55af5a8c9992c75f6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Tue, 22 Sep 2015 16:32:41 +0200 Subject: text: add text_{v,}printf function Convenient way to insert formated data into a Text. --- text.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'text.c') diff --git a/text.c b/text.c index 8653cc0..94a32f9 100644 --- a/text.c +++ b/text.c @@ -634,6 +634,24 @@ bool text_insert(Text *txt, size_t pos, const char *data, size_t len) { return true; } +bool text_printf(Text *txt, size_t pos, const char *format, ...) { + va_list ap; + va_start(ap, format); + bool ret = text_vprintf(txt, pos, format, ap); + va_end(ap); + return ret; +} + +bool text_vprintf(Text *txt, size_t pos, const char *format, va_list ap) { + int len = vsnprintf(NULL, 0, format, ap); + if (len == -1) + return false; + char *buf = malloc(len+1); + bool ret = buf && (vsnprintf(buf, len+1, format, ap) == len) && text_insert(txt, pos, buf, len); + free(buf); + return ret; +} + static size_t action_undo(Text *txt, Action *a) { size_t pos = EPOS; for (Change *c = a->change; c; c = c->next) { -- cgit v1.2.3