[FFmpeg-devel] [PATCH] tools: add ffescape utility

Clément Bœsch ubitux at gmail.com
Mon Oct 22 14:46:29 CEST 2012


On Mon, Oct 22, 2012 at 02:36:27PM +0200, Stefano Sabatini wrote:
> On date Tuesday 2012-10-16 02:33:59 +0200, Michael Niedermayer encoded:
> > On Mon, Oct 15, 2012 at 10:14:47PM +0200, Stefano Sabatini wrote:
> [...]
> > > +int main(int argc, char **argv)
> > > +{
> > > +    size_t src_buf_size = 256;
> > > +    char *src_buf = av_malloc(src_buf_size), *dst_buf = NULL;
> > 
> > missing malloc failure check
> > 
> > no further comments from me / LGTM if there are no other comments
> 
> Updated to make more extensive use of bprint buffers.
> 
> I'll push this tonight if I read no more comments.
> -- 
> FFmpeg = Fundamentalist and Fancy Mythic Pure Elaborated Guide

> From 2ffcf8574c94393a61483b77471910e5189f87f8 Mon Sep 17 00:00:00 2001
> From: Stefano Sabatini <stefasab at gmail.com>
> Date: Mon, 15 Oct 2012 12:17:15 +0200
> Subject: [PATCH] tools: add ffescape utility
> 
> ---
>  libavutil/Makefile |    2 +-
>  tools/ffescape.c   |  180 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 181 insertions(+), 1 deletions(-)
>  create mode 100644 tools/ffescape.c
> 

please add the program to .gitignore

> diff --git a/libavutil/Makefile b/libavutil/Makefile
> index 63a48be..e0dd816 100644
> --- a/libavutil/Makefile
> +++ b/libavutil/Makefile
> @@ -130,6 +130,6 @@ TESTPROGS = adler32                                                     \
>  
>  TESTPROGS-$(HAVE_LZO1X_999_COMPRESS) += lzo
>  
> -TOOLS = ffeval
> +TOOLS = ffeval ffescape
>  
>  $(SUBDIR)lzo-test$(EXESUF): ELIBS = -llzo2
> diff --git a/tools/ffescape.c b/tools/ffescape.c
> new file mode 100644
> index 0000000..2b65e8f
> --- /dev/null
> +++ b/tools/ffescape.c
> @@ -0,0 +1,180 @@
> +/*
> + * Copyright (c) 2012 Stefano Sabatini
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include "config.h"
> +#if HAVE_UNISTD_H
> +#include <unistd.h>             /* getopt */
> +#endif
> +
> +#include "libavutil/log.h"
> +#include "libavutil/bprint.h"
> +
> +#if !HAVE_GETOPT
> +#include "compat/getopt.c"
> +#endif
> +
> +/**
> + * @file
> + * escaping utility
> + */
> +
> +static void usage(void)
> +{
> +    printf("Escape an input string, adopting the av_get_token() escaping logic\n");
> +    printf("usage: ffescape [OPTIONS]\n");
> +    printf("\n"
> +           "Options:\n"
> +           "-e                echo each input line on output\n"
> +           "-h                print this help\n"
> +           "-i INFILE         set INFILE as input file, stdin if omitted\n"
> +           "-l LEVEL          set the number of escaping levels, 1 if omitted\n"
> +           "-o OUTFILE        set OUTFILE as output file, stdout if omitted\n"
> +           "-p PROMPT         set output prompt, is '=> ' by default\n"
> +           "-s SPECIAL_CHARS  set the list of special characters\n");
> +}
> +
> +#define WHITESPACES " \n\t"
> +
> +static char *escape(const char *src, const char *special_chars)
> +{
> +    AVBPrint dst;
> +    char *ret;
> +
> +    av_bprint_init(&dst, 1, AV_BPRINT_SIZE_UNLIMITED);
> +    for (; *src; src++) {
> +        if ((special_chars && strchr(special_chars, *src)) ||
> +            strchr("'\\" WHITESPACES, *src))
> +            av_bprintf(&dst, "\\%c", *src);
> +        else
> +            av_bprint_chars(&dst, *src, 1);
> +    }
> +
> +    if (!av_bprint_is_complete(&dst)) {
> +        av_bprint_finalize(&dst, NULL);
> +        return NULL;
> +    } else {
> +        av_bprint_finalize(&dst, &ret);
> +        return ret;
> +    }
> +}
> +
[...]
> +    /* grab the input and store it in src */
> +    av_bprint_init(&src, 1, AV_BPRINT_SIZE_UNLIMITED);
> +    while ((c = fgetc(infile)) != EOF)
> +        av_bprint_chars(&src, c, 1);
> +    av_bprint_chars(&src, 0, 1);
> +
> +    if (!av_bprint_is_complete(&src)) {
> +        av_log(NULL, AV_LOG_ERROR, "Could not allocate a buffer for the source string\n");
> +        return 1;
> +    }
> +    av_bprint_finalize(&src, &src_buf);

You don't finalize in case on error, while you did in the escape function.

[...]

Except these details, LGTM, thanks, it will be useful to fix the FATE
failure on MSVC with lavfi filtergraphs.

-- 
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/20121022/87dc6060/attachment.asc>


More information about the ffmpeg-devel mailing list