[FFmpeg-devel] [PATCH] bprint: add av_bprint_bytes().

Nicolas George nicolas.george at normalesup.org
Wed Jun 13 21:09:13 CEST 2012


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


Looks it may be useful at some places, for example in Clément's SAMI
demuxer.


diff --git a/libavutil/bprint.c b/libavutil/bprint.c
index f9d5d6a..2f9279d 100644
--- a/libavutil/bprint.c
+++ b/libavutil/bprint.c
@@ -128,6 +128,24 @@ void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
     av_bprint_grow(buf, n);
 }
 
+void av_bprint_bytes(AVBPrint *buf, const char *str, unsigned len)
+{
+    unsigned room, real_len;
+
+    while (1) {
+        room = av_bprint_room(buf);
+        if (len < room)
+            break;
+        if (av_bprint_alloc(buf, len))
+            break;
+    }
+    if (room) {
+        real_len = FFMIN(len, room - 1);
+        memcpy(buf->str + buf->len, str, real_len);
+    }
+    av_bprint_grow(buf, len);
+}
+
 void av_bprint_clear(AVBPrint *buf)
 {
     if (buf->len) {
@@ -222,6 +240,10 @@ 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_bytes(&b, "Hello world!", 11);
+    printf("Unterminated byte string: \"%s\"\n", b.str);
+
     return 0;
 }
 
diff --git a/libavutil/bprint.h b/libavutil/bprint.h
index 60e464e..7227a4f 100644
--- a/libavutil/bprint.h
+++ b/libavutil/bprint.h
@@ -121,6 +121,14 @@ void av_bprintf(AVBPrint *buf, const char *fmt, ...) av_printf_format(2, 3);
 void av_bprint_chars(AVBPrint *buf, char c, unsigned n);
 
 /**
+ * Append a constant byte string to a print buffer.
+ *
+ * The string can contain arbitrary binary data and does not need to be
+ * 0-terminated.
+ */
+void av_bprint_bytes(AVBPrint *buf, const char *str, unsigned len);
+
+/**
  * Reset the string to "" but keep internal allocated data.
  */
 void av_bprint_clear(AVBPrint *buf);
diff --git a/tests/ref/fate/bprint b/tests/ref/fate/bprint
index e027fa1..543b4bc 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
+Unterminated byte string: "Hello world"
-- 
1.7.10



More information about the ffmpeg-devel mailing list