• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

tools/ffeval.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012 Stefano Sabatini
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include <unistd.h>             /* getopt */
00022 #include "libavutil/eval.h"
00023 
00029 static void usage(void)
00030 {
00031     printf("Simple expression evalutor, please *don't* turn me to a feature-complete language interpreter\n");
00032     printf("usage: ffeval [OPTIONS]\n");
00033     printf("\n"
00034            "Options:\n"
00035            "-e                echo each input line on output\n"
00036            "-h                print this help\n"
00037            "-i INFILE         set INFILE as input file, stdin if omitted\n"
00038            "-o OUTFILE        set OUTFILE as output file, stdout if omitted\n"
00039            "-p PROMPT         set output prompt\n");
00040 }
00041 
00042 #define MAX_BLOCK_SIZE SIZE_MAX
00043 
00044 int main(int argc, char **argv)
00045 {
00046     size_t buf_size = 256;
00047     char *buf = av_malloc(buf_size);
00048     const char *outfilename = NULL, *infilename = NULL;
00049     FILE *outfile = NULL, *infile = NULL;
00050     const char *prompt = "=> ";
00051     int count = 0, echo = 0;
00052     int c;
00053 
00054     av_max_alloc(MAX_BLOCK_SIZE);
00055 
00056     while ((c = getopt(argc, argv, "ehi:o:p:")) != -1) {
00057         switch (c) {
00058         case 'e':
00059             echo = 1;
00060             break;
00061         case 'h':
00062             usage();
00063             return 0;
00064         case 'i':
00065             infilename = optarg;
00066             break;
00067         case 'o':
00068             outfilename = optarg;
00069             break;
00070         case 'p':
00071             prompt = optarg;
00072             break;
00073         case '?':
00074             return 1;
00075         }
00076     }
00077 
00078     if (!infilename || !strcmp(infilename, "-"))
00079         infilename = "/dev/stdin";
00080     infile = fopen(infilename, "r");
00081     if (!infile) {
00082         fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
00083         return 1;
00084     }
00085 
00086     if (!outfilename || !strcmp(outfilename, "-"))
00087         outfilename = "/dev/stdout";
00088     outfile = fopen(outfilename, "w");
00089     if (!outfile) {
00090         fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
00091         return 1;
00092     }
00093 
00094     while ((c = fgetc(infile)) != EOF) {
00095         if (c == '\n') {
00096             double d;
00097 
00098             buf[count] = 0;
00099             if (buf[0] != '#') {
00100                 av_expr_parse_and_eval(&d, buf,
00101                                        NULL, NULL,
00102                                        NULL, NULL, NULL, NULL, NULL, 0, NULL);
00103                 if (echo)
00104                     fprintf(outfile, "%s ", buf);
00105                 fprintf(outfile, "%s%f\n", prompt, d);
00106             }
00107             count = 0;
00108         } else {
00109             if (count >= buf_size-1) {
00110                 if (buf_size == MAX_BLOCK_SIZE) {
00111                     av_log(NULL, AV_LOG_ERROR, "Memory allocation problem, "
00112                            "max block size '%zd' reached\n", MAX_BLOCK_SIZE);
00113                     return 1;
00114                 }
00115                 buf_size = FFMIN(buf_size, MAX_BLOCK_SIZE / 2) * 2;
00116                 buf = av_realloc_f((void *)buf, buf_size, 1);
00117                 if (!buf) {
00118                     av_log(NULL, AV_LOG_ERROR, "Memory allocation problem occurred\n");
00119                     return 1;
00120                 }
00121             }
00122             buf[count++] = c;
00123         }
00124     }
00125 
00126     av_free(buf);
00127     return 0;
00128 }
Generated on Fri Feb 1 2013 14:34:57 for FFmpeg by doxygen 1.7.1