[FFmpeg-devel] [PATCH] lavf/aviobuf: add ff_get_line2() variant

Stefano Sabatini stefasab at gmail.com
Thu Oct 13 20:40:59 EEST 2016


This allows to probe if the read line was partially discarded.
---
 libavformat/aviobuf.c  | 10 +++++++++-
 libavformat/internal.h | 14 ++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 134d627..28183b4 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -764,18 +764,26 @@ unsigned int avio_rb32(AVIOContext *s)
 
 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
 {
-    int i = 0;
+    return ff_get_line2(s, buf, maxlen, NULL);
+}
+
+int ff_get_line2(AVIOContext *s, char *buf, int maxlen, int *readlen)
+{
+    int i = 0, j = 0;
     char c;
 
     do {
         c = avio_r8(s);
         if (c && i < maxlen-1)
             buf[i++] = c;
+        j++;
     } while (c != '\n' && c != '\r' && c);
     if (c == '\r' && avio_r8(s) != '\n' && !avio_feof(s))
         avio_skip(s, -1);
 
     buf[i] = 0;
+    if (readlen)
+        *readlen = j;
     return i;
 }
 
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 49244fa..cffc787 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -274,6 +274,20 @@ void ff_put_v(AVIOContext *bc, uint64_t val);
  */
 int ff_get_line(AVIOContext *s, char *buf, int maxlen);
 
+/**
+ * Read a whole line of text from AVIOContext. Stop reading after reaching
+ * either a \\n, a \\0 or EOF. The returned string is always \\0-terminated,
+ * and may be truncated if the buffer is too small.
+ *
+ * @param s the read-only AVIOContext
+ * @param buf buffer to store the read line
+ * @param maxlen size of the buffer
+ * @param readlen length of the read line, including the terminating newline character
+ * @return the length of the string written in the buffer, not including the
+ *         final \\0
+ */
+int ff_get_line2(AVIOContext *s, char *buf, int maxlen, int *readlen);
+
 #define SPACE_CHARS " \t\r\n"
 
 /**
-- 
1.9.1



More information about the ffmpeg-devel mailing list