remove references to vp8/vp8.h
[libvpx.git] / args.c
blobf2ad697e37b9a358d855da6b289b71886b569a15
1 /*
2 * Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license and patent
5 * grant that can be found in the LICENSE file in the root of the source
6 * tree. All contributing project authors may be found in the AUTHORS
7 * file in the root of the source tree.
8 */
11 #include <stdlib.h>
12 #include <string.h>
13 #include <limits.h>
14 #include "args.h"
16 #ifdef _MSC_VER
17 #define snprintf _snprintf
18 #endif
20 #if defined(__GNUC__) && __GNUC__
21 extern void die(const char *fmt, ...) __attribute__((noreturn));
22 #else
23 extern void die(const char *fmt, ...);
24 #endif
27 struct arg arg_init(char **argv)
29 struct arg a;
31 a.argv = argv;
32 a.argv_step = 1;
33 a.name = NULL;
34 a.val = NULL;
35 a.def = NULL;
36 return a;
39 int arg_match(struct arg *arg_, const struct arg_def *def, char **argv)
41 struct arg arg;
43 if (!argv[0] || argv[0][0] != '-')
44 return 0;
46 arg = arg_init(argv);
48 if (def->short_name
49 && strlen(arg.argv[0]) == strlen(def->short_name) + 1
50 && !strcmp(arg.argv[0] + 1, def->short_name))
53 arg.name = arg.argv[0] + 1;
54 arg.val = def->has_val ? arg.argv[1] : NULL;
55 arg.argv_step = def->has_val ? 2 : 1;
57 else if (def->long_name)
59 int name_len = strlen(def->long_name);
61 if (strlen(arg.argv[0]) >= name_len + 2
62 && arg.argv[0][1] == '-'
63 && !strncmp(arg.argv[0] + 2, def->long_name, name_len)
64 && (arg.argv[0][name_len+2] == '='
65 || arg.argv[0][name_len+2] == '\0'))
68 arg.name = arg.argv[0] + 2;
69 arg.val = arg.name[name_len] == '=' ? arg.name + name_len + 1 : NULL;
70 arg.argv_step = 1;
74 if (arg.name && !arg.val && def->has_val)
75 die("Error: option %s requires argument.\n", arg.name);
77 if (arg.name && arg.val && !def->has_val)
78 die("Error: option %s requires no argument.\n", arg.name);
80 if (arg.name
81 && (arg.val || !def->has_val))
83 arg.def = def;
84 *arg_ = arg;
85 return 1;
88 return 0;
92 const char *arg_next(struct arg *arg)
94 if (arg->argv[0])
95 arg->argv += arg->argv_step;
97 return *arg->argv;
101 char **argv_dup(int argc, const char **argv)
103 char **new_argv = malloc((argc + 1) * sizeof(*argv));
105 memcpy(new_argv, argv, argc * sizeof(*argv));
106 new_argv[argc] = NULL;
107 return new_argv;
111 void arg_show_usage(FILE *fp, const struct arg_def *const *defs)
113 char option_text[40] = {0};
115 for (; *defs; defs++)
117 const struct arg_def *def = *defs;
118 char *short_val = def->has_val ? " <arg>" : "";
119 char *long_val = def->has_val ? "=<arg>" : "";
121 if (def->short_name && def->long_name)
122 snprintf(option_text, 37, "-%s%s, --%s%s",
123 def->short_name, short_val,
124 def->long_name, long_val);
125 else if (def->short_name)
126 snprintf(option_text, 37, "-%s%s",
127 def->short_name, short_val);
128 else if (def->long_name)
129 snprintf(option_text, 37, " --%s%s",
130 def->long_name, long_val);
132 fprintf(fp, " %-37s\t%s\n", option_text, def->desc);
137 unsigned int arg_parse_uint(const struct arg *arg)
139 long int rawval;
140 char *endptr;
142 rawval = strtol(arg->val, &endptr, 10);
144 if (arg->val[0] != '\0' && endptr[0] == '\0')
146 if (rawval >= 0 && rawval <= UINT_MAX)
147 return rawval;
149 die("Option %s: Value %ld out of range for unsigned int\n",
150 arg->name, rawval);
153 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
154 return 0;
158 int arg_parse_int(const struct arg *arg)
160 long int rawval;
161 char *endptr;
163 rawval = strtol(arg->val, &endptr, 10);
165 if (arg->val[0] != '\0' && endptr[0] == '\0')
167 if (rawval >= INT_MIN && rawval <= INT_MAX)
168 return rawval;
170 die("Option %s: Value %ld out of range for signed int\n",
171 arg->name, rawval);
174 die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
175 return 0;
179 struct vpx_rational
181 int num; /**< fraction numerator */
182 int den; /**< fraction denominator */
184 struct vpx_rational arg_parse_rational(const struct arg *arg)
186 long int rawval;
187 char *endptr;
188 struct vpx_rational rat;
190 /* parse numerator */
191 rawval = strtol(arg->val, &endptr, 10);
193 if (arg->val[0] != '\0' && endptr[0] == '/')
195 if (rawval >= INT_MIN && rawval <= INT_MAX)
196 rat.num = rawval;
197 else die("Option %s: Value %ld out of range for signed int\n",
198 arg->name, rawval);
200 else die("Option %s: Expected / at '%c'\n", arg->name, *endptr);
202 /* parse denominator */
203 rawval = strtol(endptr + 1, &endptr, 10);
205 if (arg->val[0] != '\0' && endptr[0] == '\0')
207 if (rawval >= INT_MIN && rawval <= INT_MAX)
208 rat.den = rawval;
209 else die("Option %s: Value %ld out of range for signed int\n",
210 arg->name, rawval);
212 else die("Option %s: Invalid character '%c'\n", arg->name, *endptr);
214 return rat;