[1/9] lavu/avstring: add av_asprintf() function

Message ID 1394127604-9503-2-git-send-email-alessandro@ghedini.me
State New
Headers show

Commit Message

Alessandro Ghedini March 6, 2014, 5:39 p.m.
Original ffmpeg commit 61e2e29 by Clément Bœsch.
---
 doc/APIchanges       |  3 +++
 libavutil/avstring.c | 26 ++++++++++++++++++++++++++
 libavutil/avstring.h | 10 ++++++++++
 libavutil/version.h  |  2 +-
 4 files changed, 40 insertions(+), 1 deletion(-)

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index d6134f9..f8ce3b0 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -13,6 +13,9 @@  libavutil:     2013-12-xx
 
 API changes, most recent first:
 
+2014-03-xx - xxxxxxx - lavu 53.06.0 - avstring.h
+  Add av_asprintf().
+
 2014-xx-xx - xxxxxxx - lavu 53.05.0 - frame.h
   Add av_frame_copy() for copying the frame data.
 
diff --git a/libavutil/avstring.c b/libavutil/avstring.c
index 3ea7be0..a292731 100644
--- a/libavutil/avstring.c
+++ b/libavutil/avstring.c
@@ -108,6 +108,32 @@  size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...)
     return len;
 }
 
+char *av_asprintf(const char *fmt, ...)
+{
+    char *p = NULL;
+    va_list va;
+    int len;
+
+    va_start(va, fmt);
+    len = vsnprintf(NULL, 0, fmt, va);
+    va_end(va);
+    if (len < 0)
+        goto end;
+
+    p = av_malloc(len + 1);
+    if (!p)
+        goto end;
+
+    va_start(va, fmt);
+    len = vsnprintf(p, len + 1, fmt, va);
+    va_end(va);
+    if (len < 0)
+        av_freep(&p);
+
+end:
+    return p;
+}
+
 char *av_d2str(double d)
 {
     char *str = av_malloc(16);
diff --git a/libavutil/avstring.h b/libavutil/avstring.h
index b7d1098..4789cd5 100644
--- a/libavutil/avstring.h
+++ b/libavutil/avstring.h
@@ -131,6 +131,16 @@  size_t av_strlcat(char *dst, const char *src, size_t size);
 size_t av_strlcatf(char *dst, size_t size, const char *fmt, ...) av_printf_format(3, 4);
 
 /**
+ * Print arguments following specified format into a large enough auto
+ * allocated buffer. It is similar to GNU asprintf().
+ * @param fmt printf-compatible format string, specifying how the
+ *            following parameters are used.
+ * @return the allocated string
+ * @note You have to free the string yourself with av_free().
+ */
+char *av_asprintf(const char *fmt, ...) av_printf_format(1, 2);
+
+/**
  * Convert a number to a av_malloced string.
  */
 char *av_d2str(double d);
diff --git a/libavutil/version.h b/libavutil/version.h
index cd0981c..36070b2 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -54,7 +54,7 @@ 
  */
 
 #define LIBAVUTIL_VERSION_MAJOR 53
-#define LIBAVUTIL_VERSION_MINOR  5
+#define LIBAVUTIL_VERSION_MINOR  6
 #define LIBAVUTIL_VERSION_MICRO  0
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \