| Bsoft 2.0.5 Bernard's software package

options.cpp
Go to the documentation of this file.
1
9 #include "options.h"
10 #include "utilities.h"
11
12 #define MAX_TAG_LEN 20
13
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16
17
40 Boption* get_option_list(const char* use[], int argc, char* argv[], int& optind)
41 {
42 if ( argc < 2 ) {
43 usage(use, 0);
44 return NULL;
45 }
46
47 int i, v(1);
48 Bstring usetag, tag, value;
49 Boption* option = NULL;
50 Boption* curropt = NULL;
51 Boption* nuopt = NULL;
52
53 for ( i=1; i<argc; i++ ) {
54 if ( strncmp(argv[i], "-verbose", strlen(argv[i])) == 0 && i<argc-1 ) {
55 if ( argv[i+1][0] != '-' )
56 verbose = get_option_verbose(argv[i+1]);
57 }
58 if ( strncmp(argv[i], "-help", 5) == 0 ) {
59 usage(use, 1);
60 return NULL;
61 }
62 }
63
64 if ( verbose & VERB_DEBUG ) {
65 cout << "DEBUG get_option_list:";
66 for ( i=0; i<argc; i++ ) cout << " " << argv[i];
67 cout << endl;
68 }
69
70 optind = 1;
71 for ( i=1; i<argc && v; i+=v ) {
72 // cout << i << tab << argv[i] << endl;
73 if ( argv[i][0] == '-' ) {
74 nuopt = new Boption(argv[i], argv[i+1], use);
75 if ( !option ) option = curropt = nuopt;
76 else {
77 curropt->next = nuopt;
78 curropt = curropt->next;
79 }
80 if ( curropt->value.length() ) v = 2;
81 else v = 1;
82 optind = i + v;
83 if ( verbose & VERB_DEBUG ) {
84 if ( v == 1 ) cout << "DEBUG get_option_list: tag=" << curropt->tag << endl;
85 else cout << "DEBUG get_option_list: tag=" << curropt->tag << " value=" << curropt->value << endl;
86 }
87 }
88 }
89
90 if ( verbose & VERB_DEBUG ) {
91 cout << "DEBUG get_option_list: optind=" << optind;
92 if ( optind < argc ) cout << " arg=" << argv[optind] << endl;
93 cout << endl;
94 }
95
96 if ( option->errors() ) {
97 cerr << "Option errors: " << option->errors() << endl;
98 bexit(-1);
99 }
100
101 return option;
102 }
103
109 int option_kill(Boption* option)
110 {
111 Boption *curropt, *curropt2;
112
113 for ( curropt = option; curropt; ) {
114 curropt2 = curropt->next;
115 curropt->tag = 0;
116 curropt->value = 0;
117 delete curropt;
118 curropt = curropt2;
119 }
120
121 return 0;
122 }
123
143 int get_option_verbose(char* optarg)
144 {
145 Bstring arg(optarg);
146 return get_option_verbose(arg);
147 }
148
149 int get_option_verbose(Bstring& optarg)
150 {
151 verbose = optarg.integer();
152
153 Bstring arg = optarg.lower();
154
155 if ( arg.contains("non") ) verbose |= VERB_NONE;
156 if ( arg.contains("res") ) verbose |= VERB_RESULT;
157 if ( arg.contains("lab") ) verbose |= VERB_LABEL;
158 if ( arg.contains("pro") ) verbose |= VERB_PROCESS;
159 if ( arg.contains("sta") ) verbose |= VERB_STATS;
160 if ( arg.contains("ful") ) verbose |= VERB_FULL;
161 if ( arg.contains("tim") ) verbose |= VERB_TIME;
162 if ( arg.contains("mem") ) verbose |= VERB_MEMORY;
163 if ( arg.contains("deb") ) verbose |= VERB_DEBUG;
164 if ( arg.contains("star") ) verbose |= VERB_DEBUG_STAR;
165 if ( arg.contains("dm") ) verbose |= VERB_DEBUG_DM;
166
167 return verbose;
168 }
169
175 double get_option_mass(Bstring& optarg)
176 {
177 double mass = optarg.real();
178
179 if ( optarg.contains("k") || optarg.contains("K") )
180 mass *= 1e3;
181 else if ( optarg.contains("m") || optarg.contains("M") )
182 mass *= 1e6;
183 else if ( optarg.contains("g") || optarg.contains("G") )
184 mass *= 1e9;
185
186 return mass;
187 }
188
189
usage
void usage(const char **use, int all)
Prints usage information.
Definition: utilities.cpp:119
VERB_STATS
#define VERB_STATS
Definition: utilities.h:108
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Bstring::real
double real()
Definition: Bstring.cpp:545
VERB_NONE
#define VERB_NONE
Definition: utilities.h:104
get_option_verbose
int get_option_verbose(char *optarg)
Sets the verbose option.
Definition: options.cpp:143
utilities.h
Header file for general utilities.
Boption::value
Bstring value
Definition: options.h:30
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Boption::tag
Bstring tag
Definition: options.h:29
Bstring::length
long length() const
Definition: Bstring.cpp:263
get_option_mass
double get_option_mass(Bstring &optarg)
Returns the adjusted mass based on the added character.
Definition: options.cpp:175
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
VERB_MEMORY
#define VERB_MEMORY
Definition: utilities.h:111
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:584
Boption
A class for a command line input option.
Definition: options.h:26
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
VERB_DEBUG_DM
#define VERB_DEBUG_DM
Definition: utilities.h:115
use
const char * use[]
Definition: b3v.cpp:19
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:105
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
Bstring::lower
Bstring lower()
Definition: Bstring.cpp:308
Boption::errors
int errors()
Definition: options.h:79
Bstring::integer
long integer()
Definition: Bstring.cpp:539
VERB_DEBUG_STAR
#define VERB_DEBUG_STAR
Definition: utilities.h:113
verbose
int verbose
options.h
Header file for option handlers.
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273