[FFmpeg-devel] [PATCH 1/3] lavu/bprint: implement av_bprint_strftime().

Nicolas George nicolas.george at normalesup.org
Sat Nov 10 19:49:45 CET 2012


TODO bump & APIchanges

Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 libavutil/bprint.c    |   26 ++++++++++++++++++++++++++
 libavutil/bprint.h    |    8 ++++++++
 tests/ref/fate/bprint |    1 +
 3 files changed, 35 insertions(+)

diff --git a/libavutil/bprint.c b/libavutil/bprint.c
index 9d8e7c1..aed1f46 100644
--- a/libavutil/bprint.c
+++ b/libavutil/bprint.c
@@ -21,6 +21,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
+#include <time.h>
 #include "avassert.h"
 #include "bprint.h"
 #include "common.h"
@@ -129,6 +130,25 @@ void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
     av_bprint_grow(buf, n);
 }
 
+void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm)
+{
+    unsigned room;
+    size_t l;
+
+    if (!*fmt)
+        return;
+    while (1) {
+        room = av_bprint_room(buf);
+        if (!room)
+            return;
+        if ((l = strftime(buf->str + buf->len, room, fmt, tm)))
+            break;
+        if (av_bprint_alloc(buf, room <= INT_MAX / 2 ? room * 2 : INT_MAX))
+            return;
+    }
+    av_bprint_grow(buf, l);
+}
+
 void av_bprint_get_buffer(AVBPrint *buf, unsigned size,
                           unsigned char **mem, unsigned *actual_size)
 {
@@ -201,6 +221,7 @@ int main(void)
 {
     AVBPrint b;
     char buf[256];
+    struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 };
 
     av_bprint_init(&b, 0, -1);
     bprint_pascal(&b, 5);
@@ -235,6 +256,11 @@ int main(void)
     bprint_pascal(&b, 25);
     printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len);
 
+    av_bprint_init(&b, 0, -1);
+    av_bprint_strftime(&b, "%Y-%m-%d", &testtime);
+    printf("strftime: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str);
+    av_bprint_finalize(&b, NULL);
+
     return 0;
 }
 
diff --git a/libavutil/bprint.h b/libavutil/bprint.h
index c09b61f..6f2f083 100644
--- a/libavutil/bprint.h
+++ b/libavutil/bprint.h
@@ -125,6 +125,14 @@ void av_bprintf(AVBPrint *buf, const char *fmt, ...) av_printf_format(2, 3);
  */
 void av_bprint_chars(AVBPrint *buf, char c, unsigned n);
 
+struct tm;
+/**
+ * Append a formated date and time to a print buffer.
+ * Note: due do poor design of the standard strftime function, it can only
+ * be used when no overflow is possible.
+ */
+void av_bprint_strftime(AVBPrint *buf, const char *fmt, const struct tm *tm);
+
 /**
  * Allocate bytes in the buffer for external use.
  *
diff --git a/tests/ref/fate/bprint b/tests/ref/fate/bprint
index e027fa1..3c72ac7 100644
--- a/tests/ref/fate/bprint
+++ b/tests/ref/fate/bprint
@@ -12,3 +12,4 @@ Short text in automatic buffer: 174/174
 Long text in automatic buffer: 1000/2834
 Long text count only buffer: 0/2834
 Long text count only buffer: 255/2834
+strftime: 255/10 "2000-12-20"
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list