Debian Codegraph @

contrib/ms-sbvar/utilities_dw/ascii/dw_parse_cmd.c

raw
1 /*
2 * Copyright (C) 1996-2011 Daniel Waggoner
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "dw_parse_cmd.h"
19 #include "dw_math.h"
20 #include "dw_std.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25
26 #define ARGUMENT_ID_1 '-'
27 #define ARGUMENT_ID_2 '/'
28
29 /*
30 Searches args for a leading ARGUMENT_ID followed by the character opt. Returns
31 the index if found and -1 otherwise.
32 */
33 int dw_FindArgument(int nargs, char **args, char opt)
34 {
35 int i;
36 for (i=nargs-1; i >= 0; i--)
37 if (((args[i][0] == ARGUMENT_ID_1) || (args[i][0] == ARGUMENT_ID_2)) && (args[i][1] == opt) && (args[i][2] == '\0')) break;
38 return i;
39 }
40
41 /*
42 Searches for the last argument whose leading character is ARGUMENT_ID
43 followed by the character opt. If such an argument is not found, then the
44 integer def is returned. If such an argument is found and there is an i+1
45 argument and its characters form a valid integer, then this integer is
46 returned. Otherwise def is returned.
47 */
48 int dw_ParseInteger(int nargs, char **args, char opt, int def)
49 {
50 int i=dw_FindArgument(nargs,args,opt);
51 if ((i != -1) && (i+1 < nargs) && dw_IsInteger(args[i+1])) return atoi(args[i+1]);
52 return def;
53 }
54
55 /*
56 Searches for the last argument whose leading character is ARGUMENT_ID
57 followed by the character opt. If such an argument is not found, then the
58 double def is returned. If such an argument is found and there is an i+1
59 argument and its characters form a valid floating point number, then this
60 value is returned. Otherwise def is returned.
61 */
62 double dw_ParseFloating(int nargs, char **args, char opt, double def)
63 {
64 int i=dw_FindArgument(nargs,args,opt);
65 if ((i != -1) && (i+1 < nargs) && dw_IsFloat(args[i+1])) return atof(args[i+1]);
66 return def;
67 }
68
69
70 /*
71 Searches for the last argument whose leading character is ARGUMENT_ID
72 followed by the character opt. If such an argument is not found, then the
73 pointer def is returned. If such an argument is found and there is an i+1
74 argument then a pointer to this argument is returned. Otherwise def is
75 returned.
76 */
77 char* dw_ParseString(int nargs, char **args, char opt, const char *def)
78 {
79 int i=dw_FindArgument(nargs,args,opt);
80 if ((i != -1) && (i+1 < nargs)) return args[i+1];
81 return (char*)def;
82 }
83
84 /*
85 Searches args for a leading ARGUMENT_ID followed by the string opt. Returns
86 the index if found and -1 otherwise.
87 */
88 int dw_FindArgument_String(int nargs, char **args, const char *opt)
89 {
90 int i;
91 for (i=nargs-1; i >= 0; i--)
92 if (((args[i][0] == ARGUMENT_ID_1) || (args[i][0] == ARGUMENT_ID_2)) && !strcmp(args[i]+1,opt)) break;
93 return i;
94 }
95
96 /*
97 Searches for the last argument whose leading character is a ARGUMENT_ID
98 followed by the string opt. If such an argument is not found, then the
99 integer def is returned. If such an argument is found and there is an i+1
100 argument and its characters form a valid integer, then this integer is
101 returned. Otherwise def is returned.
102 */
103 int dw_ParseInteger_String(int nargs, char **args, const char *opt, int def)
104 {
105 int i=dw_FindArgument_String(nargs,args,opt);
106 if ((i != -1) && (i+1 < nargs) && dw_IsInteger(args[i+1])) return atoi(args[i+1]);
107 return def;
108 }
109
110 /*
111 Searches for the last argument whose leading character is ARGUMENT_ID
112 followed by the string opt. If such an argument is not found, then the
113 double def is returned. If such an argument is found and there is an i+1
114 argument and its characters form a valid floating point number, then this
115 value is returned. Otherwise def is returned.
116 */
117 double dw_ParseFloating_String(int nargs, char **args, const char *opt, double def)
118 {
119 int i=dw_FindArgument_String(nargs,args,opt);
120 if ((i != -1) && (i+1 < nargs) && dw_IsFloat(args[i+1])) return atof(args[i+1]);
121 return def;
122 }
123
124
125 /*
126 Searches for the last argument whose leading character is ARGUMENT_ID
127 followed by the string opt. If such an argument is not found, then the
128 pointer def is returned. If such an argument is found and there is an i+1
129 argument, then a pointer to this argument is returned. Otherwise def is
130 returned.
131 */
132 char* dw_ParseString_String(int nargs, char **args, const char *opt, const char *def)
133 {
134 int i=dw_FindArgument_String(nargs,args,opt);
135 if ((i != -1) && (i+1 < nargs)) return args[i+1];
136 return (char*)def;
137 }
138