FFmpeg
ffeval.c
Go to the documentation of this file.
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 "config.h"
22 #include <stdio.h>
23 #include <string.h>
24 #if HAVE_UNISTD_H
25 #include <unistd.h> /* getopt */
26 #endif
27 
28 #include "libavutil/error.h"
29 #include "libavutil/eval.h"
30 #include "libavutil/log.h"
31 #include "libavutil/mem.h"
32 
33 #if !HAVE_GETOPT
34 #include "compat/getopt.c"
35 #endif
36 
37 /**
38  * @file
39  * simple arithmetic expression evaluator
40  */
41 
42 static void usage(void)
43 {
44  printf("Simple expression evalutor, please *don't* turn me to a feature-complete language interpreter\n");
45  printf("usage: ffeval [OPTIONS]\n");
46  printf("\n"
47  "Options:\n"
48  "-e echo each input line on output\n"
49  "-h print this help\n"
50  "-i INFILE set INFILE as input file, stdin if omitted\n"
51  "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
52  "-p PROMPT set output prompt\n");
53 }
54 
55 int main(int argc, char **argv)
56 {
57  int buf_size = 0;
58  char *buf = NULL;
59  const char *outfilename = NULL, *infilename = NULL;
60  FILE *outfile = NULL, *infile = NULL;
61  const char *prompt = "=> ";
62  int count = 0, echo = 0;
63  int c;
64 
65 #define GROW_ARRAY() \
66  do { \
67  if (!av_dynarray2_add((void **)&buf, &buf_size, 1, NULL)) { \
68  av_log(NULL, AV_LOG_ERROR, \
69  "Memory allocation problem occurred\n"); \
70  return 1; \
71  } \
72  } while (0)
73 
74  GROW_ARRAY();
75  while ((c = getopt(argc, argv, "ehi:o:p:")) != -1) {
76  switch (c) {
77  case 'e':
78  echo = 1;
79  break;
80  case 'h':
81  usage();
82  return 0;
83  case 'i':
84  infilename = optarg;
85  break;
86  case 'o':
87  outfilename = optarg;
88  break;
89  case 'p':
90  prompt = optarg;
91  break;
92  case '?':
93  return 1;
94  }
95  }
96 
97  if (!infilename || !strcmp(infilename, "-")) {
98  infilename = "stdin";
99  infile = stdin;
100  } else {
101  infile = fopen(infilename, "r");
102  }
103  if (!infile) {
104  fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
105  return 1;
106  }
107 
108  if (!outfilename || !strcmp(outfilename, "-")) {
109  outfilename = "stdout";
110  outfile = stdout;
111  } else {
112  outfile = fopen(outfilename, "w");
113  }
114  if (!outfile) {
115  fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
116  return 1;
117  }
118 
119  while ((c = fgetc(infile)) != EOF) {
120  if (c == '\n') {
121  double d;
122 
123  buf[count] = 0;
124  if (buf[0] != '#') {
125  int ret = av_expr_parse_and_eval(&d, buf,
126  NULL, NULL,
127  NULL, NULL, NULL, NULL, NULL, 0, NULL);
128  if (echo)
129  fprintf(outfile, "%s ", buf);
130  if (ret >= 0) fprintf(outfile, "%s%f\n", prompt, d);
131  else fprintf(outfile, "%s%f (%s)\n", prompt, d, av_err2str(ret));
132  }
133  count = 0;
134  } else {
135  if (count >= buf_size-1)
136  GROW_ARRAY();
137  buf[count++] = c;
138  }
139  }
140 
141  av_free(buf);
142  return 0;
143 }
outfile
FILE * outfile
Definition: audiogen.c:96
main
int main(int argc, char **argv)
Definition: ffeval.c:55
GROW_ARRAY
#define GROW_ARRAY()
getopt
static int getopt(int argc, char *argv[], char *opts)
Definition: getopt.c:41
usage
static void usage(void)
Definition: ffeval.c:42
NULL
#define NULL
Definition: coverity.c:32
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
error.h
eval.h
av_expr_parse_and_eval
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
Definition: eval.c:776
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
printf
printf("static const uint8_t my_array[100] = {\n")
log.h
ret
ret
Definition: filter_design.txt:187
getopt.c
optarg
static char * optarg
Definition: getopt.c:39
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
d
d
Definition: ffmpeg_filter.c:368