1 /*
2 * (C) 2001 by Argonne National Laboratory.
3 * See COPYRIGHT in top-level directory.
4 */
5
6 #include <stdio.h>
7 #if HAVE_STDLIB_H
8 #include <stdlib.h>
9 #endif
10 #include "args.h"
11 #include "lists.h"
12 #include <string.h>
13
14 #ifndef DEBUG
15 #define DEBUG 0
16 #endif
17
GetIntArg(argc,argv,switchName,val)18 GetIntArg( argc, argv, switchName, val )
19 int *argc, *val;
20 char **argv, *switchName;
21 {
22 int i, nremove, j; /* nremove - number of arguments to remove */
23
24 for (i=1; i<*argc; i++) { /* loop through all args */
25 if (!strcmp( switchName, argv[i] )) { /* if this is the switch we want, */
26 if (i+1<*argc) { /* make sure there is one more */
27 if (sscanf( argv[i+1], "%d", val )) {
28 nremove = 2; /* got valid value */
29 } else {
30 nremove = 1; /* didn't get valid value, but */
31 /* don't throw away the next arg. */
32 }
33 } else {
34 nremove = 1;
35 }
36 for (i+=nremove; i<*argc; i++) { /* move everyone else down */
37 argv[i-nremove]=argv[i];
38 }
39 (*argc)-=nremove;
40 if (nremove==2) return 1; /* if we got a value, return */
41 i--;
42 }
43 }
44 return 0;
45 }
46
47
48
GetDoubleArg(argc,argv,switchName,val)49 GetDoubleArg( argc, argv, switchName, val )
50 int *argc;
51 double *val;
52 char **argv, *switchName;
53 {
54 int i, nremove, j; /* nremove - number of arguments to remove */
55
56 for (i=1; i<*argc; i++) { /* loop through all args */
57 if (!strcmp( switchName, argv[i] )) { /* if this is the switch we want, */
58 if (i+1<*argc) { /* make sure there is one more */
59 if (sscanf( argv[i+1], "%lf", val )) {
60 nremove = 2; /* got valid value */
61 } else {
62 nremove = 1; /* didn't get valid value, but */
63 /* don't throw away the next arg. */
64 }
65 } else {
66 nremove = 1;
67 }
68 for (i+=nremove; i<*argc; i++) { /* move everyone else down */
69 argv[i-nremove]=argv[i];
70 }
71 (*argc)-=nremove;
72 if (nremove==2) return 1; /* if we got a value, return */
73 i--;
74 }
75 }
76 return 0;
77 }
78
79
80
GetStringArg(argc,argv,switchName,val)81 GetStringArg( argc, argv, switchName, val )
82 int *argc;
83 char **argv, *switchName, **val;
84 {
85 int i, nremove, j; /* nremove - number of arguments to remove */
86 char *readPtr;
87
88 for (i=1; i<*argc; i++) { /* loop through all args */
89 if (!strcmp( switchName, argv[i] )) { /* if this is the switch we want, */
90 if (i+1<*argc) { /* make sure there is one more */
91 *val = argv[i+1];
92 nremove = 2;
93 } else {
94 nremove = 1;
95 }
96 for (i+=nremove; i<*argc; i++) { /* move everyone else down */
97 argv[i-nremove]=argv[i];
98 }
99 (*argc)-=nremove;
100 if (nremove==2) return 1; /* if we got a value, return */
101 i--;
102 }
103 }
104 return 0;
105 }
106
107
108
IsArgPresent(argc,argv,switchName)109 IsArgPresent( argc, argv, switchName )
110 int *argc;
111 char **argv, *switchName;
112 {
113 int i, returnVal;
114
115 returnVal = 0;
116 for (i=1; i<*argc; i++) { /* loop through all args */
117 /*
118 printf( "Comparing :%s: and :%s:\n", switchName, argv[i] );
119 */
120 if (!strcmp( switchName, argv[i] )) { /* if this is the switch we want, */
121 /*
122 printf( "YUP!" );
123 */
124 for (i++; i<*argc; i++) { /* slide everything on down */
125 argv[i-1]=argv[i];
126 }
127 (*argc)--;
128 i--;
129 returnVal = 1;
130 }
131 }
132 return returnVal;
133 }
134
135
136
137
GetArgAdjacentString(argc,argv,switchName,value)138 GetArgAdjacentString( argc, argv, switchName, value )
139 int *argc;
140 char **argv, *switchName, **value;
141 {
142 int argNum, i, str_len;
143 xpand_list_String *listStr;
144 char *readPtr, *start, *end, *theString;
145
146 listStr = String_CreateList(10);
147
148 for (argNum=1; argNum<*argc; argNum++) {
149 readPtr = strstr( argv[argNum], switchName );
150 if (readPtr==argv[argNum]) {
151 /* we want to find the switch at the beginning of an argument */
152 #if DEBUG
153 fprintf( stderr, "Found %s in %s\n", switchName, argv[argNum] );
154 #endif
155 readPtr = argv[argNum] + strlen( switchName );
156 while (*readPtr) {
157 String_AddItem( listStr, *readPtr ); /* add a character */
158 readPtr++;
159 } /* done copying list */
160 String_AddItem( listStr, '\0' ); /* terminate the string */
161
162 for (argNum++; argNum < *argc; argNum++) {
163 argv[argNum-1] = argv[argNum]; /* shift remaining arguments down */
164 }
165 (*argc)--;
166
167 ListClose( listStr, theString, str_len );
168 #if DEBUG
169 fprintf( stderr, "Returning string: %s\n", theString );
170 #endif
171 *value = theString;
172 return 1;
173 } /* if strstr(... switch ) */
174 } /* keep looking for the switch */
175 return 0; /* didn't even find the switch */
176 }
177
178
179
180
GetIntListArg(argc,argv,switchName,intList,listLen)181 GetIntListArg( argc, argv, switchName, intList, listLen )
182 int *argc;
183 char **argv, *switchName;
184 int **intList, *listLen;
185 {
186 char *list, *token;
187 xpand_list_Int *tempIntList;
188 int temp_int;
189
190 tempIntList = Int_CreateList(10);
191
192 if (!GetArgAdjacentString( argc, argv, switchName, &list )) {
193 return 0;
194 }
195 token = strtok( list, "," );
196 while (token) {
197 if (sscanf( token, "%d", &temp_int ))
198 Int_AddItem( tempIntList, temp_int );
199 token = strtok( (char*)0, "," );
200 }
201
202 ListClose( tempIntList, *intList, *listLen );
203 free( list );
204 return 1;
205 }
206
GetStringListArg(argc,argv,switchName,strList,listLen)207 GetStringListArg( argc, argv, switchName, strList, listLen )
208 int *argc, *listLen;
209 char **argv, *switchName, ***strList;
210 {
211 char *list, *token, *str_dup;
212 int temp_int;
213 xpand_list_Strings *tempStrList;
214
215 tempStrList = Strings_CreateList( 10 );
216 if (!GetArgAdjacentString( argc, argv, switchName, &list )) {
217 return 0;
218 }
219 token = strtok( list, "," );
220 while (token) {
221 str_dup = (char *) malloc( (strlen(token) + 1) * sizeof( char ) );
222 strcpy( str_dup, token );
223 Strings_AddItem( tempStrList, str_dup );
224 #if DEBUG
225 fprintf( stderr, "arg: get string list item :%s:\n", token );
226 #endif
227 token = strtok( (char*)0, "," );
228 }
229 ListClose( tempStrList, *strList, *listLen );
230 free( list );
231 return 1;
232 }
233
234