summaryrefslogtreecommitdiffstats
path: root/membuffer.c
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2015-01-24 12:47:26 +1200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-02-15 06:02:24 -0800
commit3521cdcbd6bb2e282249286afe16b066e4af30e8 (patch)
treec7055dd4ca7069ba357ca78d51aa5501cfb330b7 /membuffer.c
parent6397d56af4a6eccf9b2ed748499fe891dcdb64b1 (diff)
downloadsubsurface-3521cdcbd6bb2e282249286afe16b066e4af30e8.tar.gz
membuffer: add helper functions to return regular C strings
The whole "create a string using a printf-like interface" thing is pretty common, and most users then don't necessarily want to deal with the membuffer interfaces around it. So this just creates trivial wrappers to do this, so that you can do s = format_string("%d: %s\n", i, str); or similar things. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'membuffer.c')
-rw-r--r--membuffer.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/membuffer.c b/membuffer.c
index c7d2bbdd2..2889a0cdc 100644
--- a/membuffer.c
+++ b/membuffer.c
@@ -6,12 +6,18 @@
#include "dive.h"
#include "membuffer.h"
-void free_buffer(struct membuffer *b)
+char *detach_buffer(struct membuffer *b)
{
- free(b->buffer);
+ char *result = b->buffer;
b->buffer = NULL;
b->len = 0;
b->alloc = 0;
+ return result;
+}
+
+void free_buffer(struct membuffer *b)
+{
+ free(detach_buffer(b));
}
void flush_buffer(struct membuffer *b, FILE *f)
@@ -100,6 +106,26 @@ void put_vformat(struct membuffer *b, const char *fmt, va_list args)
}
}
+/* Silly helper using membuffer */
+char *vformat_string(const char *fmt, va_list args)
+{
+ struct membuffer mb = { 0 };
+ put_vformat(&mb, fmt, args);
+ mb_cstring(&mb);
+ return detach_buffer(&mb);
+}
+
+char *format_string(const char *fmt, ...)
+{
+ va_list args;
+ char *result;
+
+ va_start(args, fmt);
+ result = vformat_string(fmt, args);
+ va_end(args);
+ return result;
+}
+
void put_format(struct membuffer *b, const char *fmt, ...)
{
va_list args;