changeset 319:4077eff1dd39

Added terminateing version of asprintf.
author Sascha Wilde <wilde@intevation.de>
date Fri, 04 Apr 2014 17:58:14 +0200
parents 6ccaf25219fd
children 1628615d904e
files common/strhelp.c common/strhelp.h
diffstat 2 files changed, 28 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/common/strhelp.c	Fri Apr 04 17:13:35 2014 +0200
+++ b/common/strhelp.c	Fri Apr 04 17:58:14 2014 +0200
@@ -1,4 +1,8 @@
+/* Needed to get asprintf */
+#define _GNU_SOURCE 1
+
 #include <ctype.h>
+#include <stdarg.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -194,6 +198,21 @@
     free (p);
 }
 
+int
+xasprintf (char **strp, const char *fmt, ...)
+{
+  int ret;
+  va_list ap;
+  va_start(ap, fmt);
+  ret = vasprintf(strp, fmt, ap);
+  va_end(ap);
+
+  if (ret == -1)
+    out_of_core();
+
+  return ret;
+}
+
 #ifdef WIN32
 /* Adapted from GPGOL rev. e512053 */
 char *
--- a/common/strhelp.h	Fri Apr 04 17:13:35 2014 +0200
+++ b/common/strhelp.h	Fri Apr 04 17:58:14 2014 +0200
@@ -23,6 +23,15 @@
 void xfree ( void *p );
 
 /**
+ * @brief Terminating variant of asprintf
+ *
+ * This function behaves exactly like asprintf(3) but will terminate
+ * when an error occures (usally that means that memoy allocation
+ * failed).
+ */
+int xasprintf (char **strp, const char *fmt, ...);
+
+/**
  * @brief Returns the length of the given %NULL-terminated
  * string array str_array.
  * @param[in] str_array a %NULL-terminated array of strings

http://wald.intevation.org/projects/trustbridge/