summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2015-02-14 17:48:19 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-02-14 17:50:03 -0800
commit12f422a1a3468d5c29e99bb2c388248680227025 (patch)
tree609f354541af59997377beb78d8b487b57528b73
parent87ee8e8aef00f2e183c57a93754717ea41da9179 (diff)
downloadsubsurface-12f422a1a3468d5c29e99bb2c388248680227025.tar.gz
Add helper function to extend C strings
This is trivial to do with Qt, but when we want to be able to do this in C code it takes a little more work. This creates a simple pattern to extend an existing C string. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r--membuffer.c29
-rw-r--r--membuffer.h2
2 files changed, 31 insertions, 0 deletions
diff --git a/membuffer.c b/membuffer.c
index c8a06662f..c7d2bbdd2 100644
--- a/membuffer.c
+++ b/membuffer.c
@@ -228,3 +228,32 @@ void put_quoted(struct membuffer *b, const char *text, int is_attribute, int is_
text = p;
}
}
+
+char *add_to_string_va(const char *old, const char *fmt, va_list args)
+{
+ char *res;
+ struct membuffer o = { 0 }, n = { 0 };
+ put_vformat(&n, fmt, args);
+ put_format(&o, "%s\n%s", old ?: "", mb_cstring(&n));
+ res = strdup(mb_cstring(&o));
+ free_buffer(&o);
+ free_buffer(&n);
+ free((void *)old);
+ return res;
+}
+
+/* this is a convenience function that cleverly adds text to a string, using our membuffer
+ * infrastructure.
+ * WARNING - this will free(old), the intended pattern is
+ * string = add_to_string(string, fmt, ...)
+ */
+char *add_to_string(const char *old, const char *fmt, ...)
+{
+ char *res;
+ va_list args;
+
+ va_start(args, fmt);
+ res = add_to_string_va(old, fmt, args);
+ va_end(args);
+ return res;
+}
diff --git a/membuffer.h b/membuffer.h
index 153815fd8..c0a4f6029 100644
--- a/membuffer.h
+++ b/membuffer.h
@@ -27,6 +27,8 @@ extern void strip_mb(struct membuffer *);
extern const char *mb_cstring(struct membuffer *);
extern __printf(2, 0) void put_vformat(struct membuffer *, const char *, va_list);
extern __printf(2, 3) void put_format(struct membuffer *, const char *fmt, ...);
+extern __printf(2, 0) char *add_to_string_va(const char *old, const char *fmt, va_list args);
+extern __printf(2, 3) char *add_to_string(const char *old, const char *fmt, ...);
/* Output one of our "milli" values with type and pre/post data */
extern void put_milli(struct membuffer *, const char *, int, const char *);