-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgeneric_opts.hh
400 lines (364 loc) · 12.9 KB
/
generic_opts.hh
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* {{{
This file is part of gapc (GAPC - Grammars, Algebras, Products - Compiler;
a system to compile algebraic dynamic programming programs)
Copyright (C) 2008-2011 Georg Sauthoff
email: gsauthof@techfak.uni-bielefeld.de or gsauthof@sdf.lonestar.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
}}} */
#ifndef RTLIB_GENERIC_OPTS_HH_
#define RTLIB_GENERIC_OPTS_HH_
extern "C" {
#include <getopt.h>
#include <unistd.h>
#include <ctype.h>
#include <stdio.h>
}
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <vector>
#include <string>
#include <exception>
#include <utility>
#include <cassert>
#ifdef CHECKPOINTING_INTEGRATED
#include "boost/filesystem.hpp"
#endif
// define _XOPEN_SOURCE=500
namespace gapc {
class OptException : public std::exception {
private:
std::string msg;
public:
explicit OptException(const std::string &s) : std::exception(), msg(s) {
}
~OptException() throw() { }
const char* what() const throw() {
return msg.c_str();
}
};
class Opts {
private:
Opts(const Opts&);
Opts &operator=(const Opts&);
int parse_checkpointing_interval(const std::string &interval) {
// parse the user-specified checkpointing interval
std::stringstream tmp_interval(interval);
std::string val;
std::vector<int> interval_vals;
try {
// parse the checkpointing interval the user specified
while (std::getline(tmp_interval, val, ':')) {
// split the interval string at ':' and store values
interval_vals.push_back(std::stoi(val));
}
if (interval_vals.size() != 4) {
throw std::exception();
}
} catch (const std::exception &e) {
throw OptException("Invalid interval format! "
"Must be d:h:m:s (e.g. 0:0:1:0).");
}
// calculate the interval length (in seconds)
int cp_interval = interval_vals[0] * 86400 + interval_vals[1] * 3600 +
interval_vals[2] * 60 + interval_vals[3];
if (cp_interval <= 0) {
throw OptException("Interval cannot be <= 0 (is " +
std::to_string(cp_interval) + ").");
}
return cp_interval;
}
public:
typedef std::vector<std::pair<const char*, unsigned> > inputs_t;
inputs_t inputs;
bool window_mode;
unsigned int window_size;
unsigned int window_increment;
unsigned int delta;
unsigned int repeats;
unsigned k;
#ifdef CHECKPOINTING_INTEGRATED
size_t checkpoint_interval; // default interval: 3600s (1h)
boost::filesystem::path checkpoint_out_path; // default path: cwd
boost::filesystem::path checkpoint_in_path; // default: empty
std::string user_file_prefix;
bool keep_archives; // default: delete after calculations completed
#endif
int argc;
char **argv;
Opts()
:
#ifdef WINDOW_MODE
window_mode(true),
#else
window_mode(false),
#endif
window_size(0),
window_increment(0),
delta(0),
repeats(1),
k(3),
#ifdef CHECKPOINTING_INTEGRATED
checkpoint_interval(DEFAULT_CHECKPOINT_INTERVAL),
checkpoint_out_path(boost::filesystem::current_path()),
checkpoint_in_path(boost::filesystem::path("")),
user_file_prefix(""),
keep_archives(false),
#endif
argc(0),
argv(0) {}
~Opts() {
for (inputs_t::iterator i = inputs.begin(); i != inputs.end(); ++i)
delete[] (*i).first;
}
void help(char **argv) {
std::cout << argv[0] << " ("
#ifdef WINDOW_MODE
<< " (-[wi] [0-9]+)*"
#endif
#ifdef LIBRNA_RNALIB_H_
<< " (-[tT] [0-9]+)? (-P PARAM-file)?"
#endif
<< " (-[drk] [0-9]+)* (-h)? (INPUT|-f INPUT-file)\n"
<< "--help ,-h print this help message\n"
#ifdef CHECKPOINTING_INTEGRATED
<< "--checkpointInterval,-p d:h:m:s specify the periodic "
<< "checkpointing\n"
<< " interval,default: 0:1:0:0 "
<< "(1h)\n"
<< "--checkpointOutput,-O PATH/PREFIX set path where to store "
<< "the checkpoints,\n"
<< " default: current working "
<< "directory\n"
<< " Optional: add custom prefix "
<< "for generated\n"
<< " files to PATH (e.g. PATH:\n"
<< " \"/path/to/dir/file_prefix\""
<< "\n"
<< " will set PATH to \"/path/to/"
<< "dir/\"\n"
<< " and PREFIX to \"file_prefix\""
<< ").\n"
<< " Make sure to add a \"/\" to\n"
<< " the end of PATH if you don't"
<< "\n"
<< " wish to add a custom prefix "
<< "to the files.\n"
<< "--checkpointInput,-I LOGFILE set the path to the Logfile\n"
<< " of the checkpoints you wish "
<< "to load.\n"
<< " (This file was generated "
<< "along\n"
<< " with the checkpoint archives."
<< "\n"
<< " If it isn't available\n"
<< " add the path to each archive "
<< "to a \n"
<< " text file and provide the \n"
<< " path to this file).\n"
<< "--keepArchives,-K don't delete checkpointing "
<< "archives\n"
<< " after the program finished "
<< "its calculations\n"
#endif
<< "\n"
#if defined(GAPC_CALL_STRING) && defined(GAPC_VERSION_STRING)
<< "GAPC call: \"" << GAPC_CALL_STRING << "\"\n"
<< "GAPC version: \"" << GAPC_VERSION_STRING << "\"\n"
#endif
<< "\n";
}
void parse(int argc, char **argv) {
int o = 0;
char *input = 0;
const option long_opts[] = {
{"help", no_argument, nullptr, 'h'},
{"checkpointInterval", required_argument, nullptr, 'p'},
{"checkpointOutput", required_argument, nullptr, 'O'},
{"checkpointInput", required_argument, nullptr, 'I'},
{"keepArchives", no_argument, nullptr, 'K'},
{nullptr, no_argument, nullptr, 0}};
this->argc = argc;
this->argv = argv;
#ifdef LIBRNA_RNALIB_H_
char *par_filename = 0;
#endif
while ((o = getopt_long(argc, argv, ":f:"
#ifdef WINDOW_MODE
"w:i:"
#endif
#ifdef LIBRNA_RNALIB_H_
"t:T:P:"
#endif
#ifdef CHECKPOINTING_INTEGRATED
"p:I:KO:"
#endif
"hd:r:k:H:", long_opts, nullptr)) != -1) {
switch (o) {
case 'f' :
{
std::ifstream file(optarg);
file.exceptions(std::ios_base::badbit |
std::ios_base::failbit |
std::ios_base::eofbit);
std::filebuf *buffer = file.rdbuf();
size_t size = buffer->pubseekoff(0, std::ios::end, std::ios::in);
buffer->pubseekpos(0, std::ios::in);
input = new char[size+1];
assert(input);
buffer->sgetn(input, size);
input[size] = 0;
char *end = input+size;
for (char *i = input; i != end; ) {
char *s = std::strchr(i, '\n');
if (s)
*s = 0;
size_t x = std::strlen(i)+1;
char *j = new char[x];
std::strncpy(j, i, x);
inputs.push_back(std::make_pair(j, x-1));
if (s)
i = s + 1;
else
break;
}
delete[] input;
}
break;
case 'w' :
window_size = std::atoi(optarg);
break;
case 'i' :
window_increment = std::atoi(optarg);
break;
#ifdef LIBRNA_RNALIB_H_
case 'T' :
case 't' :
temperature = std::atof(optarg);
break;
case 'P' :
par_filename = optarg;
break;
#endif
case 'k' :
k = std::atoi(optarg);
break;
case 'h' :
help(argv);
std::exit(0);
break;
case 'd' :
delta = std::atoi(optarg);
break;
case 'r' :
repeats = std::atoi(optarg);
break;
#ifdef CHECKPOINTING_INTEGRATED
case 'p' :
checkpoint_interval = parse_checkpointing_interval(optarg);
break;
case 'I' :
{
boost::filesystem::path arg_path(optarg);
if (arg_path.is_absolute()) {
checkpoint_in_path = arg_path;
} else {
checkpoint_in_path = boost::filesystem::current_path() / arg_path;
}
if (!boost::filesystem::exists(checkpoint_in_path) ||
!boost::filesystem::is_regular_file(checkpoint_in_path)) {
throw OptException("Logfile could not be found at path \"" +
checkpoint_in_path.string() +
"\"!");
}
// check if current user has read permissions
// for checkpoint input directory
if (access(checkpoint_in_path.c_str(), R_OK) != 0) {
throw OptException("Missing read permissions for"
" Logfile \""
+ checkpoint_in_path.string()
+ "\"!");
}
break;
}
case 'O' :
{
boost::filesystem::path arg_path(optarg);
boost::filesystem::path out_path = arg_path.parent_path();
if (out_path.is_absolute()) {
checkpoint_out_path = out_path;
} else {
checkpoint_out_path /= out_path;
}
user_file_prefix = arg_path.filename().string();
if (!boost::filesystem::exists(checkpoint_out_path) ||
!boost::filesystem::is_directory(checkpoint_out_path)) {
throw OptException("The output path \"" +
checkpoint_out_path.string() +
"\" is not a directory!");
}
// check if current user has write permissions
// for checkpoint output directory
if (access(checkpoint_out_path.c_str(), W_OK) != 0) {
throw OptException("Missing write permissions for"
" output path \""
+ checkpoint_out_path.string()
+ "\"!");
}
break;
}
case 'K' :
keep_archives = true;
break;
#endif
case '?' :
case ':' :
{
std::ostringstream os;
os << "Missing argument of " << char(optopt);
throw OptException(os.str());
}
default:
{
std::ostringstream os;
os << "Unknown Option: " << char(o);
throw OptException(os.str());
}
}
}
if (!input) {
if (optind == argc)
throw OptException("Missing input sequence or no -f.");
for (; optind < argc; ++optind) {
input = new char[std::strlen(argv[optind])+1];
snprintf(input, std::strlen(argv[optind])+1, "%s", argv[optind]);
unsigned n = std::strlen(input);
inputs.push_back(std::make_pair(input, n));
}
}
if (window_mode) {
if (!window_size)
throw OptException("window size (-w) is zero");
if (!window_increment)
throw OptException("window increment (-i) is zero");
if (window_increment >= window_size )
throw OptException("window_increment >= window_size");
}
#ifdef LIBRNA_RNALIB_H_
librna_read_param_file(par_filename);
#endif
}
};
} // namespace gapc
#endif // RTLIB_GENERIC_OPTS_HH_