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 #if HAVE_UNISTD_H
23 #include <unistd.h> /* getopt */
24 #endif
25 
26 #include "libavutil/eval.h"
27 
28 #if !HAVE_GETOPT
29 #include "compat/getopt.c"
30 #endif
31 
32 /**
33  * @file
34  * simple arithmetic expression evaluator
35  */
36 
37 static void usage(void)
38 {
39  printf("Simple expression evalutor, please *don't* turn me to a feature-complete language interpreter\n");
40  printf("usage: ffeval [OPTIONS]\n");
41  printf("\n"
42  "Options:\n"
43  "-e echo each input line on output\n"
44  "-h print this help\n"
45  "-i INFILE set INFILE as input file, stdin if omitted\n"
46  "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
47  "-p PROMPT set output prompt\n");
48 }
49 
50 #define MAX_BLOCK_SIZE SIZE_MAX
51 
52 int main(int argc, char **argv)
53 {
54  size_t buf_size = 256;
55  char *buf = av_malloc(buf_size);
56  const char *outfilename = NULL, *infilename = NULL;
57  FILE *outfile = NULL, *infile = NULL;
58  const char *prompt = "=> ";
59  int count = 0, echo = 0;
60  int c;
61 
63 
64  while ((c = getopt(argc, argv, "ehi:o:p:")) != -1) {
65  switch (c) {
66  case 'e':
67  echo = 1;
68  break;
69  case 'h':
70  usage();
71  return 0;
72  case 'i':
73  infilename = optarg;
74  break;
75  case 'o':
76  outfilename = optarg;
77  break;
78  case 'p':
79  prompt = optarg;
80  break;
81  case '?':
82  return 1;
83  }
84  }
85 
86  if (!infilename || !strcmp(infilename, "-")) {
87  infilename = "stdin";
88  infile = stdin;
89  } else {
90  infile = fopen(infilename, "r");
91  }
92  if (!infile) {
93  fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
94  return 1;
95  }
96 
97  if (!outfilename || !strcmp(outfilename, "-")) {
98  outfilename = "stdout";
99  outfile = stdout;
100  } else {
101  outfile = fopen(outfilename, "w");
102  }
103  if (!outfile) {
104  fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
105  return 1;
106  }
107 
108  while ((c = fgetc(infile)) != EOF) {
109  if (c == '\n') {
110  double d;
111 
112  buf[count] = 0;
113  if (buf[0] != '#') {
114  av_expr_parse_and_eval(&d, buf,
115  NULL, NULL,
116  NULL, NULL, NULL, NULL, NULL, 0, NULL);
117  if (echo)
118  fprintf(outfile, "%s ", buf);
119  fprintf(outfile, "%s%f\n", prompt, d);
120  }
121  count = 0;
122  } else {
123  if (count >= buf_size-1) {
124  if (buf_size == MAX_BLOCK_SIZE) {
125  av_log(NULL, AV_LOG_ERROR, "Memory allocation problem, "
126  "max block size '%zd' reached\n", MAX_BLOCK_SIZE);
127  return 1;
128  }
129  buf_size = FFMIN(buf_size, MAX_BLOCK_SIZE / 2) * 2;
130  buf = av_realloc_f((void *)buf, buf_size, 1);
131  if (!buf) {
132  av_log(NULL, AV_LOG_ERROR, "Memory allocation problem occurred\n");
133  return 1;
134  }
135  }
136  buf[count++] = c;
137  }
138  }
139 
140  av_free(buf);
141  return 0;
142 }
void * av_realloc_f(void *ptr, size_t nelem, size_t elsize)
Allocate or reallocate a block of memory.
Definition: mem.c:168
static void usage(void)
Definition: ffeval.c:37
void av_max_alloc(size_t max)
Set the maximum size that may me allocated in one block.
Definition: mem.c:69
set threshold d
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:701
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
#define FFMIN(a, b)
Definition: common.h:58
NULL
Definition: eval.c:55
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
static int getopt(int argc, char *argv[], char *opts)
Definition: getopt.c:43
void * buf
Definition: avisynth_c.h:594
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
static double c[64]
static char * optarg
Definition: getopt.c:39
#define MAX_BLOCK_SIZE
Definition: ffeval.c:50
printf("static const uint8_t my_array[100] = {\n")
int main(int argc, char **argv)
Definition: ffeval.c:52
void INT64 INT64 count
Definition: avisynth_c.h:594
FILE * outfile
Definition: audiogen.c:96
simple arithmetic expression evaluator