[FFmpeg-devel] [PATCH] ffprobe: add compact and compactnk writers
Alexander Strasser
eclipse7 at gmx.net
Sun Sep 25 16:26:55 CEST 2011
Hi
Stefano Sabatini wrote:
> On date Sunday 2011-09-25 02:43:49 +0200, Clément Bœsch encoded:
> > On Sun, Sep 25, 2011 at 02:09:24AM +0200, Stefano Sabatini wrote:
> > > From: Stefano Sabatini <stefano.sabatini-lala at poste.it>
[...]
> > > +static void compactnk_print_int(const char *key, int value)
> > > +{
> > > + printf("%d", value);
> > > +}
> > > +
> > > +static void compactnk_print_str(const char *key, const char *value)
> > > +{
> > > + printf("%s", value);
> > > +}
> > >
> >
>
> > Maybe some quoting would be worth here? It's sometimes hard to tell what
> > are the limit of a key. Another solution might be to use some '|'.
> >
> > Those are just suggestions, I won't push for this.
>
> I added escaping (which may be used e.g. for CSV output), and change
I think for CSV output it would be better to adhere to RFC 4180.
Probably with the exception of "Each line should contain the same
number of fields throughout the file.", but that is "should" anyway.
[...]
> Subject: [PATCH] ffprobe: add compact and compactnk writers
>
> ---
> doc/ffprobe.texi | 23 +++++++++++++++++
> ffprobe.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
> 2 files changed, 93 insertions(+), 1 deletions(-)
>
> diff --git a/doc/ffprobe.texi b/doc/ffprobe.texi
> index f8310a8..86f102f 100644
> --- a/doc/ffprobe.texi
> +++ b/doc/ffprobe.texi
> @@ -105,6 +105,29 @@ keyN=valN
> [/SECTION]
> @end example
>
> + at item compact, compactnk
> +Compact format.
> +
> +With the @option{compact} format, each section is printed on a single
> +line, and has the form:
> + at example
> +SECTION: key1=val1|key2=val2| ... |keyN=valN
> + at end example
> +
> + at option{compactnk} is similar to @option{compact}, but no key is shown,
> +so each section is printed on a single line and has the form:
> + at example
> +SECTION: val1|val2| ... |valN
> + at 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 is converted to "\|".
> +
> +You may need to de-escape the strings after parsing.
I think you should usually do the de-escaping while parsing or else
you split the fields incorrectly in the first place if the separator
appears somewhere in the data.
> +
> @item json
> JSON based format.
>
> diff --git a/ffprobe.c b/ffprobe.c
> index cabc8c8..d011403 100644
> --- a/ffprobe.c
> +++ b/ffprobe.c
> @@ -238,6 +238,55 @@ static void default_print_footer(const char *section)
> printf("\n[/%s]", section);
> }
>
> +/* Compact output */
> +
> +/**
> + * Escape \n, \t, \\ and sep characters contained in s, and print the
> + * resulting string.
> + */
> +static inline void print_escaped_str(const char *s, const char sep)
> +{
> + while (*s) {
> + switch (*s) {
> + case '\n': printf("\\n"); break;
> + case '\r': printf("\\r"); break;
> + case '\\': printf("\\\\"); break;
> + default:
> + if (*s == sep) printf("\\%c", sep);
> + else printf("%c", *s);
> + }
> + s++;
> + }
> +}
> +
> +#define COMPACT_ITEM_SEP_CHAR '|'
> +#define COMPACT_ITEM_SEP "|"
> +
> +static void compact_print_header(const char *section)
> +{
> + printf("%s: ", section);
> +}
> +
> +static void compact_print_footer(const char *section)
> +{
> + printf("\n");
> +}
> +
> +static void compact_print_str(const char *key, const char *value)
> +{
> + printf("%s=", key);
> + print_escaped_str(value, COMPACT_ITEM_SEP_CHAR);
> +}
IMHO the key should also be escaped, or are you sure that it will
never conflict? It is also more future proof.
[...]
Nice new output writers BTW :)
Alexander
More information about the ffmpeg-devel
mailing list