[FFmpeg-devel] [PATCH] ffprobe: add CSV-like escaping mode

Stefano Sabatini stefasab at gmail.com
Sat Oct 8 23:05:17 CEST 2011


---
 doc/ffprobe.texi |    4 ++++
 ffprobe.c        |   21 +++++++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/doc/ffprobe.texi b/doc/ffprobe.texi
index 34412fa..7c31d06 100644
--- a/doc/ffprobe.texi
+++ b/doc/ffprobe.texi
@@ -189,6 +189,10 @@ separator character ('|') are escaped using C-like fashioned escaping,
 so that a newline is converted to the sequence "\n", a carriage return
 to "\r", '\' to "\\" and the separator @var{SEP} is converted to
 "\SEP".
+ at item csv
+Perform CSV-like escaping, as described in RFC4180.
+Strings containing containing line breaks, carriage return,
+double quotes, and @var{SEP} are enclosed in double-quotes.
 @end table
 
 @end table
diff --git a/ffprobe.c b/ffprobe.c
index 168560b..41e0a8f 100644
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -401,6 +401,25 @@ static inline void print_c_escaped_str(const char *s, const char sep)
     }
 }
 
+/**
+ * Quote fields containing special characters, check RFC4180.
+ */
+static inline void print_csv_escaped_str(const char *s, const char sep)
+{
+    /* check if it contains line breaks (CRLF), double quotes, and commas */
+    if (strchr(s, '"') || strchr(s, sep) || strchr(s, '\n') || strchr(s, '\r')) {
+        printf("\"");
+        while (*s) {
+            if (*s == '"')
+                printf("\"");
+            printf("%c", *s);
+            s++;
+        }
+        printf("\"");
+    } else
+        printf("%s", s);
+}
+
 typedef struct CompactContext {
     const AVClass *class;
     char *item_sep_str;
@@ -456,6 +475,8 @@ static av_cold int compact_init(WriterContext *wctx, const char *args, void *opa
         compact->print_escaped_str = NULL;
     else if (!strcmp(compact->escape_mode_str, "c"))
         compact->print_escaped_str = print_c_escaped_str;
+    else if (!strcmp(compact->escape_mode_str, "csv"))
+             compact->print_escaped_str = print_csv_escaped_str;
     else {
         av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
         return AVERROR(EINVAL);
-- 
1.7.4.1



More information about the ffmpeg-devel mailing list