Bsoft  2.1.0
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 shell
16 extern string command; // Command line
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  cmd_line(argc, argv);
48 
49  int i, v(1);
50  Bstring usetag, tag, value;
51  Boption* option = NULL;
52  Boption* curropt = NULL;
53  Boption* nuopt = NULL;
54 
55  for ( i=1; i<argc; i++ ) {
56  if ( strncmp(argv[i], "-verbose", strlen(argv[i])) == 0 && i<argc-1 ) {
57  if ( argv[i+1][0] != '-' )
58  verbose = get_option_verbose(argv[i+1]);
59  }
60  if ( strncmp(argv[i], "-help", 5) == 0 ) {
61  usage(use, 1);
62  return NULL;
63  }
64  }
65 
66  if ( verbose & VERB_DEBUG ) {
67  cout << "DEBUG get_option_list:";
68  for ( i=0; i<argc; i++ ) cout << " " << argv[i];
69  cout << endl;
70  }
71 
72  optind = 1;
73  for ( i=1; i<argc && v; i+=v ) {
74 // cout << i << tab << argv[i] << endl;
75  if ( argv[i][0] == '-' ) {
76  nuopt = new Boption(argv[i], argv[i+1], use);
77  if ( !option ) option = curropt = nuopt;
78  else {
79  curropt->next = nuopt;
80  curropt = curropt->next;
81  }
82  if ( curropt->value.length() ) v = 2;
83  else v = 1;
84  optind = i + v;
85  if ( verbose & VERB_DEBUG ) {
86  if ( v == 1 ) cout << "DEBUG get_option_list: tag=" << curropt->tag << endl;
87  else cout << "DEBUG get_option_list: tag=" << curropt->tag << " value=" << curropt->value << endl;
88  }
89  }
90  }
91 
92  if ( verbose & VERB_DEBUG ) {
93  cout << "DEBUG get_option_list: optind=" << optind;
94  if ( optind < argc ) cout << " arg=" << argv[optind] << endl;
95  cout << endl;
96  }
97 
98  if ( option->errors() ) {
99  cerr << "Option errors: " << option->errors() << endl;
100  bexit(-1);
101  }
102 
103  return option;
104 }
105 
111 int option_kill(Boption* option)
112 {
113  Boption *curropt, *curropt2;
114 
115  for ( curropt = option; curropt; ) {
116  curropt2 = curropt->next;
117  curropt->tag = 0;
118  curropt->value = 0;
119  delete curropt;
120  curropt = curropt2;
121  }
122 
123  return 0;
124 }
125 
145 int get_option_verbose(char* optarg)
146 {
147  Bstring arg(optarg);
148  return get_option_verbose(arg);
149 }
150 
152 {
153  verbose = optarg.integer();
154 
155  Bstring arg = optarg.lower();
156 
157  if ( arg.contains("non") ) verbose |= VERB_NONE;
158  if ( arg.contains("res") ) verbose |= VERB_RESULT;
159  if ( arg.contains("lab") ) verbose |= VERB_LABEL;
160  if ( arg.contains("pro") ) verbose |= VERB_PROCESS;
161  if ( arg.contains("sta") ) verbose |= VERB_STATS;
162  if ( arg.contains("ful") ) verbose |= VERB_FULL;
163  if ( arg.contains("tim") ) verbose |= VERB_TIME;
164  if ( arg.contains("mem") ) verbose |= VERB_MEMORY;
165  if ( arg.contains("deb") ) verbose |= VERB_DEBUG;
166  if ( arg.contains("star") ) verbose |= VERB_DEBUG_STAR;
167  if ( arg.contains("dm") ) verbose |= VERB_DEBUG_DM;
168 
169  return verbose;
170 }
171 
177 double get_option_mass(Bstring& optarg)
178 {
179  double mass = optarg.real();
180 
181  if ( optarg.contains("k") || optarg.contains("K") )
182  mass *= 1e3;
183  else if ( optarg.contains("m") || optarg.contains("M") )
184  mass *= 1e6;
185  else if ( optarg.contains("g") || optarg.contains("G") )
186  mass *= 1e9;
187 
188  return mass;
189 }
190 
191 
Boption
A class for a command line input option.
Definition: options.h:26
VERB_MEMORY
#define VERB_MEMORY
Definition: utilities.h:115
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bstring
Definition: Bstring.h:29
verbose
int verbose
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::next
Boption * next
Definition: options.h:28
use
const char * use[]
Definition: b3v.cpp:19
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bstring::real
double real()
Definition: Bstring.cpp:553
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
options.h
Header file for option handlers.
command
string command
Definition: utilities.cpp:20
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Boption::errors
int errors()
Definition: options.h:79
Boption::tag
Bstring tag
Definition: options.h:29
VERB_DEBUG_DM
#define VERB_DEBUG_DM
Definition: utilities.h:119
VERB_NONE
#define VERB_NONE
Definition: utilities.h:108
Boption::value
Bstring value
Definition: options.h:30
VERB_RESULT
#define VERB_RESULT
Definition: utilities.h:109
VERB_DEBUG_STAR
#define VERB_DEBUG_STAR
Definition: utilities.h:117
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
get_option_verbose
int get_option_verbose(char *optarg)
Sets the verbose option.
Definition: options.cpp:145
utilities.h
Header file for general utilities.
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:177
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
VERB_STATS
#define VERB_STATS
Definition: utilities.h:112
usage
void usage(const char **use, int all)
Prints usage information.
Definition: utilities.cpp:126
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bstring::lower
Bstring lower()
Definition: Bstring.cpp:316
cmd_line
int cmd_line(int argc, char **argv)
Reconstructs the command line as a global string.
Definition: utilities.cpp:36