2012-01-16 01:05:07 +03:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2012-10-15 22:46:33 +03:00
|
|
|
#include "config.h"
|
2021-07-23 14:06:58 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2012-10-15 22:46:33 +03:00
|
|
|
#if HAVE_UNISTD_H
|
2012-01-16 01:05:07 +03:00
|
|
|
#include <unistd.h> /* getopt */
|
2012-10-15 22:46:33 +03:00
|
|
|
#endif
|
|
|
|
|
2021-07-23 14:06:58 +02:00
|
|
|
#include "libavutil/error.h"
|
2012-01-16 01:05:07 +03:00
|
|
|
#include "libavutil/eval.h"
|
2021-07-23 14:06:58 +02:00
|
|
|
#include "libavutil/log.h"
|
2013-04-27 23:46:49 +03:00
|
|
|
#include "libavutil/mem.h"
|
2012-01-16 01:05:07 +03:00
|
|
|
|
2012-10-15 22:46:33 +03:00
|
|
|
#if !HAVE_GETOPT
|
|
|
|
#include "compat/getopt.c"
|
|
|
|
#endif
|
|
|
|
|
2012-01-16 01:05:07 +03:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* simple arithmetic expression evaluator
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
printf("Simple expression evalutor, please *don't* turn me to a feature-complete language interpreter\n");
|
|
|
|
printf("usage: ffeval [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"
|
|
|
|
"-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
|
|
|
|
"-p PROMPT set output prompt\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2013-04-27 23:46:49 +03:00
|
|
|
int buf_size = 0;
|
|
|
|
char *buf = NULL;
|
2012-01-16 01:05:07 +03:00
|
|
|
const char *outfilename = NULL, *infilename = NULL;
|
|
|
|
FILE *outfile = NULL, *infile = NULL;
|
|
|
|
const char *prompt = "=> ";
|
|
|
|
int count = 0, echo = 0;
|
2012-10-13 21:10:29 +03:00
|
|
|
int c;
|
2012-01-16 01:05:07 +03:00
|
|
|
|
2013-04-27 23:46:49 +03:00
|
|
|
#define GROW_ARRAY() \
|
|
|
|
do { \
|
|
|
|
if (!av_dynarray2_add((void **)&buf, &buf_size, 1, NULL)) { \
|
|
|
|
av_log(NULL, AV_LOG_ERROR, \
|
|
|
|
"Memory allocation problem occurred\n"); \
|
|
|
|
return 1; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2012-01-16 01:05:07 +03:00
|
|
|
|
2013-04-27 23:46:49 +03:00
|
|
|
GROW_ARRAY();
|
2012-01-16 01:05:07 +03:00
|
|
|
while ((c = getopt(argc, argv, "ehi:o:p:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'e':
|
|
|
|
echo = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
case 'i':
|
|
|
|
infilename = optarg;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
outfilename = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
prompt = optarg;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-15 22:53:35 +03:00
|
|
|
if (!infilename || !strcmp(infilename, "-")) {
|
|
|
|
infilename = "stdin";
|
|
|
|
infile = stdin;
|
|
|
|
} else {
|
|
|
|
infile = fopen(infilename, "r");
|
|
|
|
}
|
2012-01-16 01:05:07 +03:00
|
|
|
if (!infile) {
|
|
|
|
fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-15 22:53:35 +03:00
|
|
|
if (!outfilename || !strcmp(outfilename, "-")) {
|
|
|
|
outfilename = "stdout";
|
|
|
|
outfile = stdout;
|
|
|
|
} else {
|
|
|
|
outfile = fopen(outfilename, "w");
|
|
|
|
}
|
2012-01-16 01:05:07 +03:00
|
|
|
if (!outfile) {
|
|
|
|
fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((c = fgetc(infile)) != EOF) {
|
|
|
|
if (c == '\n') {
|
|
|
|
double d;
|
|
|
|
|
|
|
|
buf[count] = 0;
|
|
|
|
if (buf[0] != '#') {
|
2013-06-02 23:48:03 +03:00
|
|
|
int ret = av_expr_parse_and_eval(&d, buf,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL, NULL, NULL, NULL, 0, NULL);
|
2012-01-16 01:05:07 +03:00
|
|
|
if (echo)
|
|
|
|
fprintf(outfile, "%s ", buf);
|
2013-06-02 23:48:03 +03:00
|
|
|
if (ret >= 0) fprintf(outfile, "%s%f\n", prompt, d);
|
2014-07-04 17:39:29 +03:00
|
|
|
else fprintf(outfile, "%s%f (%s)\n", prompt, d, av_err2str(ret));
|
2012-01-16 01:05:07 +03:00
|
|
|
}
|
|
|
|
count = 0;
|
|
|
|
} else {
|
2013-04-27 23:46:49 +03:00
|
|
|
if (count >= buf_size-1)
|
|
|
|
GROW_ARRAY();
|
2012-01-16 01:05:07 +03:00
|
|
|
buf[count++] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
av_free(buf);
|
|
|
|
return 0;
|
|
|
|
}
|