forked from kettner/hydrotrend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hydrotrend_cli.c
300 lines (264 loc) · 7.63 KB
/
hydrotrend_cli.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*-------------------------------------------------------------------------------------------
* hydrocommandLine.c
*
* Author: Albert Kettner, March 2006
*
* HydroCommandline handles the commandline imput to set 1) the output file name, 2) the output
* directory structure. If the program starts without any extra commandline options, all file
* names with start with HYDRO and the output will be written to a HYDRO_OUTPUT directory.
*
* Variable Def.Location Type Units Usage
* -------- ------------ ---- ----- -----
*
*
*-------------------------------------------------------------------------------------------*/
#include "hydrotrend_cli.h"
#include "hydroparams.h"
#include "bmi_hydrotrend.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef WITH_GETOPT
# include <getopt.h>
#endif
#define MAXLENGTH (80)
#define HT_DEFAULT_PREFIX "HYDRO"
#define HT_DEFAULT_IN_DIR "./HYDRO_IN"
#define HT_DEFAULT_OUT_DIR "HYDRO_OUTPUT"
#define TRUE (1)
#define FALSE (0)
int is_valid_str (char *str);
char *to_upper_str (char *str);
#ifndef WITH_GETOPT
static char *help_msg[] = {
"Usage: hydrotrend [indir [outdir]]",
"Options:",
" -v Print version number and exit",
" -?,-h Print this information and exit",
NULL
};
ht_args_st *
parse_command_line (int argc, char *argv[])
{
ht_args_st *args = malloc(sizeof(ht_args_st));
int arg;
int help_flag = 0;
int version_flag = 0;
for (arg=0; arg<argc; arg++) {
if (strcmp(argv[arg], "-h") == 0 || strcmp(argv[arg], "-?") == 0) {
help_flag = 1;
}
if (strcmp(argv[arg], "-v") == 0) {
version_flag = 1;
}
}
if (help_flag) {
char **str;
for (str = help_msg; *str; str++)
fprintf (stdout, "%s\n", *str);
exit (EXIT_SUCCESS);
}
if (version_flag) {
fprintf(
stdout,
"HydroTrend version %d.%d.%d\n",
HT_MAJOR_VERSION, HT_MINOR_VERSION, HT_MICRO_VERSION
);
exit (EXIT_SUCCESS);
}
args->in_file = strdup(HT_DEFAULT_PREFIX);
if (argc == 1) {
args->in_dir = strdup(HT_DEFAULT_IN_DIR);
args->out_dir = strdup(HT_DEFAULT_OUT_DIR);
} else if (argc == 2) {
args->in_dir = strdup(argv[1]);
args->out_dir = strdup(HT_DEFAULT_OUT_DIR);
} else if (argc == 3 ) {
args->in_dir = strdup(argv[1]);
args->out_dir = strdup(argv[2]);
} else {
fprintf(stderr, "incorrect arguments. %s -h for help", argv[0]);
exit(EXIT_FAILURE);
}
return args;
}
#else
static int verbose_flag = 0;
static int version_flag = 0;
static int help_flag = 0;
static struct option ht_long_opts[] = {
{"verbose", no_argument, &verbose_flag, 1},
{"brief", no_argument, &verbose_flag, 0},
{"version", no_argument, &version_flag, 1},
{"help", no_argument, &help_flag, 1},
{"prefix", required_argument, NULL, 'p'},
{"in-dir", required_argument, NULL, 'S'},
{"out-dir", required_argument, NULL, 'D'},
{NULL, 0, NULL, 0}
};
static char *help_msg[] = {
"Usage: hydrotrend [options] [prefix [directory]]",
"Options:",
" -V or --verbose Be verbose",
" --brief Be terse",
" -v or --version Print version number and exit",
" -?,-h or --help Print this information and exit",
" -p, --prefix=PREFIX Use PREFIX.IN as input file",
" -S, --in-dir=DIR Path to input file(s)",
" -D, --out-dir=DIR Put output in directory DIR",
NULL
};
/** Parse command line options for hydrotrend.
Usage: hydrotrend [options] [prefix [directory]]
file is the prefix for the HydroTrend output files. If not given, HYDRO is
used. If directory is given output files will be written to that directory.
If not given, HYDRO_OUT is used.
Use: 'hydrotrend --help' for a full list of command line options.
*/
ht_args_st *
parse_command_line (int argc, char *argv[])
{
ht_args_st *args = NULL;
if (argv)
{
int ch;
char *in_file_prefix = NULL;
char *in_dir = NULL;
char *out_dir = NULL;
while ((ch =
getopt_long (argc, argv, "vVh?p:D:S:", ht_long_opts,
NULL)) != -1)
{
switch (ch)
{
case 'p':
in_file_prefix = strdup (optarg);
break;
case 'S':
in_dir = strdup (optarg);
break;
case 'D':
out_dir = strdup (optarg);
break;
case 'v':
version_flag = 1;
break;
case 'V':
verbose_flag = 1;
break;
case '?':
case 'h':
help_flag = 1;
break;
case 0:
break;
default:
fprintf (stderr, "Error: Unknown option %c\n", ch);
exit (EXIT_FAILURE);
}
}
if (help_flag)
{
char **str;
for (str = help_msg; *str; str++)
fprintf (stdout, "%s\n", *str);
exit (EXIT_SUCCESS);
}
if (version_flag)
{
fprintf (stdout, "HydroTrend version %d.%d.%d\n",
HT_MAJOR_VERSION, HT_MINOR_VERSION, HT_MICRO_VERSION);
exit (EXIT_SUCCESS);
}
if (optind < argc)
{
if (!in_file_prefix)
in_file_prefix = strdup (argv[optind++]);
else
{
fprintf (stderr, "Error: Prefix specifed twice\n");
exit (EXIT_FAILURE);
}
}
if (!in_file_prefix)
in_file_prefix = strdup (HT_DEFAULT_PREFIX);
if (optind < argc)
{
fprintf (stderr, "optind = %d\n", optind);
if (!out_dir)
out_dir = strdup (argv[optind++]);
else
{
fprintf (stderr, "Error: Output directory specifed twice\n");
exit (EXIT_FAILURE);
}
}
if (!out_dir)
out_dir = NULL;
//out_dir = strdup (HT_DEFAULT_OUT_DIR);
if (!in_dir)
in_dir = strdup (HT_DEFAULT_IN_DIR);
if (verbose_flag)
verbose = 1;
else
verbose = 0;
/*
if (!is_valid_str (in_file_prefix) || !is_valid_str (out_dir))
{
fprintf (stderr, " HydroTrend ERROR: Incorrect command line \n");
fprintf (stderr,
" Prefix/directory can only contain alpha numeric characters\n");
exit (EXIT_FAILURE);
}
else
{
to_upper_str (in_file_prefix);
to_upper_str (out_dir);
}
*/
if (verbose)
{
fprintf (stderr, "Using input file %s.IN\n", in_file_prefix);
fprintf (stderr, "Using input directory %s\n", in_dir);
if (out_dir)
fprintf (stderr, "Using output directory %s\n", out_dir);
else
fprintf (stderr, "Using output directory specified in input file\n");
}
args = malloc( sizeof(ht_args_st) );
args->in_file = in_file_prefix;
args->in_dir = in_dir;
args->out_dir = out_dir;
strcpy (commandlinearg[0], in_file_prefix);
if (out_dir)
strcpy (commandlinearg[1], out_dir);
else
commandlinearg[1][0] = '\0';
}
return args;
}
#endif
int
is_valid_str (char *str)
{
int is_valid = 1;
if (strchr (str, '%') || strchr (str, '*') ||
strchr (str, '#') || strchr (str, '@') ||
strchr (str, '-') || strchr (str, '^') ||
strchr (str, '"') || strchr (str, '?') ||
strchr (str, '!') || strchr (str, '.') ||
strchr (str, ',') || strchr (str, '$'))
is_valid = 0;
return is_valid;
}
char *
to_upper_str (char *str)
{
if (str)
{
int i;
for (i = 0; i < strlen (str); i++)
str[i] = toupper (str[i]);
}
return str;
}