[FFmpeg-devel] [PATCH] ffprobe: make escape mode configurable in the compact writer

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


CSV escaping mode is compatible with RFC4180.
---
 doc/ffprobe.texi |   23 +++++++++++++++++------
 ffprobe.c        |   21 +++++++++++++++++----
 2 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/doc/ffprobe.texi b/doc/ffprobe.texi
index 17e0b19..34412fa 100644
--- a/doc/ffprobe.texi
+++ b/doc/ffprobe.texi
@@ -156,12 +156,6 @@ If no option is specifid, the output has the form:
 SECTION|key1=val1| ... |keyN=valN
 @end example
 
-Strings containing a newline ('\n') or carriage return ('\r'), the
-escaping character ('\') or the item 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".
-
 This writer accepts options as a list of @var{key}=@var{value} pairs,
 separated by ":".
 
@@ -180,6 +174,23 @@ It must be a single printable character, it is "|" by default.
 @item nokey, nk
 If set to 1 specify not to print the key of each field. Its default
 value is 0.
+
+ at item escape_mode
+Set the escape mode to use.
+
+It can assume one of the values
+ at table @option
+ at item none
+Perform no escaping.
+ at item c
+Perform C-like escaping. Strings containing a newline ('\n') or
+carriage return ('\r'), the escaping character ('\') or the item
+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 end table
+
 @end table
 
 @section json
diff --git a/ffprobe.c b/ffprobe.c
index bbdb941..168560b 100644
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -386,7 +386,7 @@ static Writer default_writer = {
  * Escape \n, \r, \\ and sep characters contained in s, and print the
  * resulting string.
  */
-static inline void print_escaped_str(const char *s, const char sep)
+static inline void print_c_escaped_str(const char *s, const char sep)
 {
     while (*s) {
         switch (*s) {
@@ -406,6 +406,8 @@ typedef struct CompactContext {
     char *item_sep_str;
     char item_sep;
     int nokey;
+    char *escape_mode_str;
+    void (*print_escaped_str)(const char *s, const char sep);
 } CompactContext;
 
 #define OFFSET(x) offsetof(CompactContext, x)
@@ -415,6 +417,7 @@ static const AVOption compact_options[]= {
     {"s",        "set item separator", OFFSET(item_sep_str), FF_OPT_TYPE_STRING, {.str="|"},  CHAR_MIN, CHAR_MAX },
     {"nokey",    "force no key printing", OFFSET(nokey),     FF_OPT_TYPE_INT,    {.dbl=0},     0,        1        },
     {"nk",       "force no key printing", OFFSET(nokey),     FF_OPT_TYPE_INT,    {.dbl=0},     0,        1        },
+    {"escape",   "set escape mode", OFFSET(escape_mode_str), FF_OPT_TYPE_STRING, {.str="c"},  CHAR_MIN, CHAR_MAX },
     {NULL},
 };
 
@@ -449,6 +452,15 @@ static av_cold int compact_init(WriterContext *wctx, const char *args, void *opa
     }
     compact->item_sep = compact->item_sep_str[0];
 
+    if (!strcmp(compact->escape_mode_str, "none"))
+        compact->print_escaped_str = NULL;
+    else if (!strcmp(compact->escape_mode_str, "c"))
+        compact->print_escaped_str = print_c_escaped_str;
+    else {
+        av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
+        return AVERROR(EINVAL);
+    }
+
     return 0;
 }
 
@@ -457,6 +469,7 @@ static av_cold void compact_uninit(WriterContext *wctx)
     CompactContext *compact = wctx->priv;
 
     av_freep(&compact->item_sep_str);
+    av_freep(&compact->escape_mode_str);
 }
 
 static void compact_print_section_header(WriterContext *wctx, const char *section)
@@ -478,7 +491,7 @@ static void compact_print_str(WriterContext *wctx, const char *key, const char *
     if (wctx->nb_item) printf("%c", compact->item_sep);
     if (!compact->nokey)
         printf("%s=", key);
-    print_escaped_str(value, compact->item_sep);
+    compact->print_escaped_str(value, compact->item_sep);
 }
 
 static void compact_print_int(WriterContext *wctx, const char *key, int value)
@@ -500,10 +513,10 @@ static void compact_show_tags(WriterContext *wctx, AVDictionary *dict)
         if (wctx->nb_item) printf("%c", compact->item_sep);
         if (!compact->nokey) {
             printf("TAG:");
-            print_escaped_str(tag->key, compact->item_sep);
+            compact->print_escaped_str(tag->key, compact->item_sep);
             printf("=");
         }
-        print_escaped_str(tag->value, compact->item_sep);
+        compact->print_escaped_str(tag->value, compact->item_sep);
     }
 }
 
-- 
1.7.4.1



More information about the ffmpeg-devel mailing list