Module: Mesa
Branch: master
Commit: 864148d69e1ecf6e8ff07b0587627e41823f1cb4
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=864148d69e1ecf6e8ff07b0587627e41823f1cb4

Author: Brian Paul <[email protected]>
Date:   Fri Sep  8 16:41:16 2017 -0600

util: add util_vasprintf() for Windows (v2)

We don't have vasprintf() on Windows so we need to implement it ourselves.

v2: compute actual length of output string, per Nicolai Hähnle.

Reviewed-by: Nicolai Hähnle <[email protected]>

---

 src/util/u_string.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/util/u_string.h b/src/util/u_string.h
index e88e13f42c..48f1125ccf 100644
--- a/src/util/u_string.h
+++ b/src/util/u_string.h
@@ -110,6 +110,27 @@ util_sprintf(char *str, const char *format, ...)
    va_end(ap);
 }
 
+static inline int
+util_vasprintf(char **ret, const char *format, va_list ap)
+{
+   va_list ap_copy;
+
+   /* Compute length of output string first */
+   va_copy(ap_copy, ap);
+   int r = util_vsnprintf(NULL, 0, format, ap);
+   va_end(ap_copy);
+
+   if (r < 0)
+      return -1;
+
+   *ret = (char *) malloc(r + 1);
+   if (!ret)
+      return -1;
+
+   /* Print to buffer */
+   return util_vsnprintf(*ret, r + 1, format, ap);
+}
+
 static inline char *
 util_strchr(const char *s, char c)
 {
@@ -186,6 +207,7 @@ util_strstr(const char *haystack, const char *needle)
 #define util_vsnprintf vsnprintf
 #define util_snprintf snprintf
 #define util_vsprintf vsprintf
+#define util_vasprintf vasprintf
 #define util_sprintf sprintf
 #define util_strchr strchr
 #define util_strcmp strcmp

_______________________________________________
mesa-commit mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/mesa-commit

Reply via email to