[FFmpeg-devel] [PATCH 1/2] Add av_bprint_fd_contents()

Andrey Utkin andrey.krieger.utkin at gmail.com
Thu Jul 3 13:37:08 CEST 2014


Thanks to all for comments. With AVBPrint API the task got much simpler.
---8<---
---
 libavutil/bprint.c | 17 +++++++++++++++++
 libavutil/bprint.h |  7 +++++++
 2 files changed, 24 insertions(+)

diff --git a/libavutil/bprint.c b/libavutil/bprint.c
index 0a0d078..becb4d8 100644
--- a/libavutil/bprint.c
+++ b/libavutil/bprint.c
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
+#include <unistd.h>
 #include "avassert.h"
 #include "avstring.h"
 #include "bprint.h"
@@ -304,6 +305,22 @@ void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_cha
     }
 }
 
+int av_bprint_fd_contents(AVBPrint *pb, int fd)
+{
+    int ret;
+    char buf[1024];
+    while (1) {
+        ret = read(fd, buf, sizeof(buf));
+        if (!ret)
+            return 0;
+        else if (ret < 0)
+            return AVERROR(errno);
+        av_bprint_append_data(pb, buf, ret);
+        if (!av_bprint_is_complete(pb))
+            return AVERROR(ENOMEM);
+    }
+}
+
 #ifdef TEST
 
 #undef printf
diff --git a/libavutil/bprint.h b/libavutil/bprint.h
index 839ec1e..1b23b9f 100644
--- a/libavutil/bprint.h
+++ b/libavutil/bprint.h
@@ -213,4 +213,11 @@ int av_bprint_finalize(AVBPrint *buf, char **ret_str);
 void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
                       enum AVEscapeMode mode, int flags);
 
+/**
+ * Read contents of fd into print buffer up to EOF.
+ *
+ * @return 0 for success, error code otherwise
+ */
+int av_bprint_fd_contents(AVBPrint *pb, int fd);
+
 #endif /* AVUTIL_BPRINT_H */
-- 
1.8.3.2



More information about the ffmpeg-devel mailing list