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

Clément Bœsch ubitux at gmail.com
Sun Oct 9 10:06:02 CEST 2011


On Sat, Oct 08, 2011 at 11:05:17PM +0200, Stefano Sabatini wrote:
> ---
>  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,

s/containing//, and plural for "return"? (for consistency)


> +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);
> +}
> +

I guess this might be a bit slow, but let's assume printf implementation
is efficient :)

>  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;

Weird indent.

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20111009/eb33a556/attachment.asc>


More information about the ffmpeg-devel mailing list