]> de.git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - util.c
added util_vasprintf/util_asprintf .. so we can stop assuming a certian static array...
[xonotic/gmqcc.git] / util.c
diff --git a/util.c b/util.c
index b2b092d13828a7be9609fc08b68b380c46071f86..8671faa651a18fd9cb488893583afc380c1286fe 100644 (file)
--- a/util.c
+++ b/util.c
@@ -573,6 +573,38 @@ void util_htdel(hash_table_t *ht) {
     mem_d(ht);
 }
 
+/*
+ * Portable implementation of vasprintf/asprintf. Assumes vsnprintf
+ * exists, otherwise compiler error.
+ */
+int util_vasprintf(char **ret, const char *fmt, va_list args) {
+    int     read;
+    va_list copy;
+    va_copy(copy, args);
+
+    *ret = 0;
+    if ((read = vsnprintf(NULL, 0, fmt, args)) >= 0) {
+        char *buffer;
+        if  ((buffer = (char*)mem_a(read + 1))) {
+            if ((read = vsnprintf(buffer, read + 1, fmt, copy)) < 0)
+                mem_d(buffer);
+            else
+                *ret = buffer;
+        }
+    }
+    va_end(copy);
+    return read;
+}
+int util_asprintf(char **ret, const char *fmt, ...) {
+    va_list  args;
+    int      read;
+    va_start(args, fmt);
+    read = util_vasprintf(ret, fmt, args);
+    va_end  (args);
+
+    return read;
+}
+
 /*
  * Implementation of the Mersenne twister PRNG (pseudo random numer
  * generator).  Implementation of MT19937.  Has a period of 2^19937-1