OSDN Git Service

cleanup SoftFFmpegVideo
[android-x86/external-stagefright-plugins.git] / ffmpeg / tools / ffeval.c
1 /*
2  * Copyright (c) 2012 Stefano Sabatini
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include <unistd.h>             /* getopt */
22 #include "libavutil/eval.h"
23
24 /**
25  * @file
26  * simple arithmetic expression evaluator
27  */
28
29 static void usage(void)
30 {
31     printf("Simple expression evalutor, please *don't* turn me to a feature-complete language interpreter\n");
32     printf("usage: ffeval [OPTIONS]\n");
33     printf("\n"
34            "Options:\n"
35            "-e                echo each input line on output\n"
36            "-h                print this help\n"
37            "-i INFILE         set INFILE as input file, stdin if omitted\n"
38            "-o OUTFILE        set OUTFILE as output file, stdout if omitted\n"
39            "-p PROMPT         set output prompt\n");
40 }
41
42 #define MAX_BLOCK_SIZE SIZE_MAX
43
44 int main(int argc, char **argv)
45 {
46     size_t buf_size = 256;
47     char *buf = av_malloc(buf_size);
48     const char *outfilename = NULL, *infilename = NULL;
49     FILE *outfile = NULL, *infile = NULL;
50     const char *prompt = "=> ";
51     int count = 0, echo = 0;
52     char c;
53
54     av_max_alloc(MAX_BLOCK_SIZE);
55
56     while ((c = getopt(argc, argv, "ehi:o:p:")) != -1) {
57         switch (c) {
58         case 'e':
59             echo = 1;
60             break;
61         case 'h':
62             usage();
63             return 0;
64         case 'i':
65             infilename = optarg;
66             break;
67         case 'o':
68             outfilename = optarg;
69             break;
70         case 'p':
71             prompt = optarg;
72             break;
73         case '?':
74             return 1;
75         }
76     }
77
78     if (!infilename || !strcmp(infilename, "-"))
79         infilename = "/dev/stdin";
80     infile = fopen(infilename, "r");
81     if (!infile) {
82         fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
83         return 1;
84     }
85
86     if (!outfilename || !strcmp(outfilename, "-"))
87         outfilename = "/dev/stdout";
88     outfile = fopen(outfilename, "w");
89     if (!outfile) {
90         fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
91         return 1;
92     }
93
94     while ((c = fgetc(infile)) != EOF) {
95         if (c == '\n') {
96             double d;
97
98             buf[count] = 0;
99             if (buf[0] != '#') {
100                 av_expr_parse_and_eval(&d, buf,
101                                        NULL, NULL,
102                                        NULL, NULL, NULL, NULL, NULL, 0, NULL);
103                 if (echo)
104                     fprintf(outfile, "%s ", buf);
105                 fprintf(outfile, "%s%f\n", prompt, d);
106             }
107             count = 0;
108         } else {
109             if (count >= buf_size-1) {
110                 if (buf_size == MAX_BLOCK_SIZE) {
111                     av_log(NULL, AV_LOG_ERROR, "Memory allocation problem, "
112                            "max block size '%zd' reached\n", MAX_BLOCK_SIZE);
113                     return 1;
114                 }
115                 buf_size = FFMIN(buf_size, MAX_BLOCK_SIZE / 2) * 2;
116                 buf = av_realloc_f((void *)buf, buf_size, 1);
117                 if (!buf) {
118                     av_log(NULL, AV_LOG_ERROR, "Memory allocation problem occurred\n");
119                     return 1;
120                 }
121             }
122             buf[count++] = c;
123         }
124     }
125
126     av_free(buf);
127     return 0;
128 }