kill warning
[libav.git] / cmdutils.c
CommitLineData
01310af2
FB
1/*
2 * Various utilities for command line tools
3 * Copyright (c) 2000-2003 Fabrice Bellard
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
5509bffa 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
01310af2 18 */
02d504a7 19#define HAVE_AV_CONFIG_H
01310af2 20#include "avformat.h"
d705e4a6 21#include "common.h"
01310af2
FB
22
23#include "cmdutils.h"
24
02d504a7 25void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
01310af2
FB
26{
27 const OptionDef *po;
02d504a7 28 int first;
01310af2 29
02d504a7
FB
30 first = 1;
31 for(po = options; po->name != NULL; po++) {
32 char buf[64];
33 if ((po->flags & mask) == value) {
34 if (first) {
35 printf("%s", msg);
36 first = 0;
37 }
0ecca7a4 38 pstrcpy(buf, sizeof(buf), po->name);
02d504a7 39 if (po->flags & HAS_ARG) {
0ecca7a4
MN
40 pstrcat(buf, sizeof(buf), " ");
41 pstrcat(buf, sizeof(buf), po->argname);
01310af2 42 }
02d504a7 43 printf("-%-17s %s\n", buf, po->help);
01310af2
FB
44 }
45 }
46}
47
fccfc475 48static const OptionDef* find_option(const OptionDef *po, const char *name){
8bbf6db9
MN
49 while (po->name != NULL) {
50 if (!strcmp(name, po->name))
51 break;
52 po++;
53 }
54 return po;
55}
56
01310af2
FB
57void parse_options(int argc, char **argv, const OptionDef *options)
58{
59 const char *opt, *arg;
60 int optindex;
61 const OptionDef *po;
62
63 /* parse options */
64 optindex = 1;
65 while (optindex < argc) {
66 opt = argv[optindex++];
115329f1 67
01310af2 68 if (opt[0] == '-' && opt[1] != '\0') {
8bbf6db9
MN
69 po= find_option(options, opt + 1);
70 if (!po->name)
71 po= find_option(options, "default");
01310af2 72 if (!po->name) {
8bbf6db9 73unknown_opt:
01310af2
FB
74 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
75 exit(1);
76 }
77 arg = NULL;
78 if (po->flags & HAS_ARG) {
79 arg = argv[optindex++];
80 if (!arg) {
81 fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
82 exit(1);
83 }
84 }
85 if (po->flags & OPT_STRING) {
86 char *str;
02d504a7 87 str = av_strdup(arg);
01310af2
FB
88 *po->u.str_arg = str;
89 } else if (po->flags & OPT_BOOL) {
90 *po->u.int_arg = 1;
26d4f26b
MN
91 } else if (po->flags & OPT_INT) {
92 *po->u.int_arg = atoi(arg);
1f631450
MN
93 } else if (po->flags & OPT_FLOAT) {
94 *po->u.float_arg = atof(arg);
8bbf6db9
MN
95 } else if (po->flags & OPT_FUNC2) {
96 if(po->u.func2_arg(opt+1, arg)<0)
97 goto unknown_opt;
01310af2 98 } else {
bb270c08 99 po->u.func_arg(arg);
01310af2
FB
100 }
101 } else {
102 parse_arg_file(opt);
103 }
104 }
105}
106
107void print_error(const char *filename, int err)
108{
109 switch(err) {
110 case AVERROR_NUMEXPECTED:
111 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
112 "Use '%%d' to specify the image number:\n"
113 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
115329f1 114 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
01310af2
FB
115 filename);
116 break;
117 case AVERROR_INVALIDDATA:
118 fprintf(stderr, "%s: Error while parsing header\n", filename);
119 break;
120 case AVERROR_NOFMT:
121 fprintf(stderr, "%s: Unknown format\n", filename);
122 break;
45ce5ddb
KS
123 case AVERROR_IO:
124 fprintf(stderr, "%s: I/O error occured\n"
bb270c08
DB
125 "Usually that means that input file is truncated and/or corrupted.\n",
126 filename);
45ce5ddb
KS
127 break;
128 case AVERROR_NOMEM:
129 fprintf(stderr, "%s: memory allocation error occured\n", filename);
130 break;
01310af2
FB
131 default:
132 fprintf(stderr, "%s: Error while opening file\n", filename);
133 break;
134 }
135}