File: args-parser.h

package info (click to toggle)
fflas-ffpack 1.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,128 kB
  • ctags: 1,369
  • sloc: sh: 10,959; cpp: 7,935; makefile: 263; perl: 11; csh: 5
file content (336 lines) | stat: -rw-r--r-- 10,707 bytes parent folder | download | duplicates (2)
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
/* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
/* utils/args-parser.C
 * Copyright (C) 2001, 2002 Bradford Hovinen
 *
 * Written by Bradford Hovinen <hovinen@cis.udel.edu>
 * Modified by Dmitriy Morozov <linbox@foxcub.org>. May 27, 2002.
 * Modified 2011 Brice Boyer (more types,...)
 *
 * Added parametrization to the VectorCategory tags to make them fit the
 * Rootbeer meeting design of VectorCategories being parametrized by
 * VectorTraits.
 *
 * ------------------------------------
 * ========LICENCE========
 * This file is part of the library FFLAS-FFPACK.
 *
 * FFLAS-FFPACK is free software: you can redistribute it and/or modify
 * it under the terms of the  GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * ========LICENCE========
 */

#ifndef __FFLASFFPACK_args_parser_H
#define __FFLASFFPACK_args_parser_H

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
#include <list>
#include <stdlib.h>
#include "fflas-ffpack/utils/print-utils.h"

enum ArgumentType {
	TYPE_NONE, TYPE_INT, TYPE_INTEGER, TYPE_DOUBLE, TYPE_INTLIST, TYPE_STR
};
#define TYPE_BOOL TYPE_NONE

#define END_OF_ARGUMENTS \
{ '\0', "\0", "\0", TYPE_NONE, NULL }

struct Argument
{
	char             c;
	const char      *example    ;
	const char      *helpString ;
	ArgumentType     type       ;
	void            *data       ;
};
// example may be passed as null and will be generated intelligently
// eg "-b {YN+-}" for bools, "-v v" for all else

namespace FFLAS {
    void parseArguments (int argc, char **argv, Argument *args, bool printDefaults = true);
}


/** writes the values of all arguments, preceded by the programName */
std::ostream& writeCommandString (std::ostream& os, Argument *args, char* programName);

void printHelpMessage (const char *program, Argument *args, bool printDefaults = false)
{
	int i, l;

	// Skip past libtool prefix in program name
	if (!strncmp (program, "lt-", strlen ("lt-")))
		program += strlen ("lt-");

	std::cout << "Usage: " << program << " [options] [<report file>]" << std::endl;
	std::cout << std::endl;
	std::cout << "Where [options] are the following:" << std::endl;

    bool messageboolean(false),messageprimality(false);

	for (i = 0; args[i].c != '\0'; ++i) {
		if (args[i].example != 0) {
			std::cout << "  " << args[i].example;
			l = 10 - (int)strlen (args[i].example);
			do std::cout << ' '; while (--l > 0);
		}
		else if (args[i].type == TYPE_NONE) {
			std::cout << "  -" << args[i].c << " {YN+-} ";
            messageboolean = true;
        }
		else
			std::cout << "  -" << args[i].c << ' ' << args[i].c << "      ";

		std::cout << args[i].helpString;
        if (strncmp(args[i].helpString,"Operate over the \"field\"",24) == 0)
            messageprimality = true;
		if (printDefaults) {
			l = 54 - (int)strlen (args[i].helpString);
			do std::cout << ' '; while (--l > 0);
			std::cout << " (default ";
			switch (args[i].type) {
			case TYPE_NONE:
				std::cout << ((*(bool *)args[i].data)?"ON":"OFF");
				break;
			case TYPE_INT:
				std::cout << *(int *) args[i].data;
				break;
			case TYPE_INTEGER:
				std::cout << *(long int *) args[i].data;
				break;
			case TYPE_DOUBLE:
				std::cout << *(double *) args[i].data;
				break;
			case TYPE_INTLIST:
				std::cout << *(std::list<int> *) args[i].data ;
				break;
			case TYPE_STR:
				std::cout << *(std::string *) args[i].data ;
				break;
			}
			std::cout << ")";
		}
		std::cout << std::endl;
	}

	std::cout << "  -h or -?  Display this message" << std::endl;
	if (messageboolean) 
        std::cout << "For boolean switches, the argument may be omitted, meaning the switch should be ON" << std::endl;
	std::cout << std::endl;
	std::cout << "If <report file> is '-' the report is written to std output.  If <report file> is" << std::endl;
	std::cout << "not given, then no detailed reporting is done. This is suitable if you wish only" << std::endl;
	std::cout << "to determine whether the tests succeeded." << std::endl;
	std::cout << std::endl;
	if (messageprimality) 
        std::cout << "[1] N.B. This program does not verify the primality of Q, and does not use a" << std::endl 
                  << "    field extension in the event that Q=p^n, n > 1" << std::endl;
	std::cout << std::endl;
}

/* Find an argument in the argument list for a character */

Argument *findArgument (Argument *args, char c)
{
	int i;

	for (i = 0; args[i].c != '\0' && args[i].c != c; ++i) ;

	if (args[i].c != '\0')
		return &(args[i]);
	else
		return (Argument *) 0;
}

/* Parse command line arguments */

/*! @internal
 *  @brief transforms a string list of ints to a list of int
 *  string "12,13,15"  is turned into list of ints {12,13,15}
 *  @param outlist list once converted
 *  @param instring list to be converted
 *  @return status message.
 */
int getListArgs(std::list<int> & outlist, std::string & instring)
{
	int start = 0 ;
	int count = 0 ;
	size_t i = 0 ;
	for( ; i < instring.size() ; ++i) {
		if (isdigit(instring[i])) {
			++count;
			continue ;
		}
		if (ispunct(instring[i])) {
			if (!count) {
				std::cout  << std::endl << "ill formed list " << instring << std::endl;
				for (size_t sp = 0 ; sp < 16+i ; ++sp)
					std::cout << '-' ;
				std::cout << '^' << std::endl;
				return(1);
			}
			int j = atoi(instring.substr((size_t)start,(size_t)count).c_str());
			outlist.push_front(j);
			count =  0 ;
			start = int(i+1) ;
		}
		else {
			std::cout << std::endl << "ill formed list " << instring << std::endl;
			for (size_t sp = 0 ; sp < 16+i ; ++sp)
				std::cout << '-' ;
			std::cout << '^' << std::endl;
			return(1);
		}

	}
	std::cout << std::endl;
	if (!count) {
		std::cout  << std::endl << "ill formed list " << instring << std::endl;
		for (size_t sp = 0 ; sp < 15+i ; ++sp)
			std::cout << '-' ;
		std::cout << '^' << std::endl;
		return(1);
	}

	int j = atoi(instring.substr((size_t)start,(size_t)count).c_str());
	outlist.push_front(j);

	return 0 ;
}


namespace FFLAS {
    void parseArguments (int argc, char **argv, Argument *args, bool printDefaults)
    {
        int i;
        Argument *current;

        for (i = 1; i < argc; ++i) {
                // std::cout << "i=" << i << std::endl;
            if (argv[i][0] == '-') {
                if (argv[i][1] == 0) {
                    std::cout << "Writing report data to cout (intermingled with brief report)" << std::endl << std::endl;
                    std::cout.flush ();
                }
                else if (argv[i][1] == 'h' || argv[i][1] == '?') {
                    printHelpMessage (argv[0], args, printDefaults);
                    exit (1);
                }
                else if ((current = findArgument (args, argv[i][1])) != (Argument *) 0) {
                    switch (current->type) {
                        case TYPE_NONE:
                        {
                            if (argc == i+1 || (argv[i+1][0] == '-' && argv[i+1][1] != '\0')) {
                                    // if at last argument, or next argument is a switch, set to true
                                *(bool *) current->data = true;
                                break;
                            }
                            *(bool *) current->data =
                                (argv[i+1][0] == '+'
                                 || argv[i+1][0] == 'Y'
                                 || argv[i+1][0] == 'y'
                                 || argv[i+1][0] == 'T'
                                 || argv[i+1][0] == 't') ;
                            ++i;
                        }
                        break;

                        case TYPE_INT:
                        {
                            *(int *) current->data = atoi (argv[i+1]);
                            ++i;
                        }
                        break;

                        case TYPE_INTEGER:
                        {
                            long int tmp = atoi(argv[i+1]);
                            *(long int *) current->data = tmp;
                        }
                        ++i;
                        break;

                        case TYPE_DOUBLE:
                        {
                            *(double *) current->data = atof (argv[i+1]);
                            ++i;
                        }
                        break;

                        case TYPE_INTLIST:
                        {
                            std::string lst = argv[i+1] ;
                            std::list<int> LST ;
                            getListArgs(LST,lst);
                            *(std::list<int> *) current->data = LST ;
                            ++i;
                        }
                        break;

                        case TYPE_STR:
                        {
                            *(std::string *) current->data = argv[i+1] ;
                            ++i;
                        }
                        break;

                    }
                } else {
                    std::cerr << "ERROR: Bad argument " << argv[i] << std::endl;
                    break;
                }
            } else {
                std::cout << "Writing report data to " << argv[i] << std::endl << std::endl;
                std::cout.flush ();
            }
        }
    }
}


std::ostream& writeCommandString (std::ostream& os, Argument *args, char* programName)
{
	os << programName;
	for (int i = 0; args[i].c != '\0'; ++i) {
		os << " -" << args[i].c;
		switch (args[i].type) {
		case TYPE_NONE:
			if (! (*(bool *)args[i].data)) os << " N";
			break;
		case TYPE_INT:
			os << ' ' << *(int *) args[i].data;
			break;
		case TYPE_INTEGER:
			os << ' ' << *(long int *) args[i].data;
			break;
		case TYPE_DOUBLE:
			os << ' ' << *(double *) args[i].data;
			break;
		case TYPE_INTLIST:
			os << ' ' << *(std::list<int> *) args[i].data;
			break;
		case TYPE_STR:
			os << ' ' << *(std::string *) args[i].data;
			break;
		}
	}
	return os << std::endl;
}


#endif // __FFLASFFPACK_args_parser_H