3
\brief Implementation of the options class and corresponding C-style interface
4
\defgroup LIBOPTIONS liboptions: A library to read user options
13
#include <libipv1/ip_lib.h>
14
#include <libutil/libutil.h>
16
#include "liboptions.h"
22
Options* _default_psi_options_;
25
* Initialize the options interface. Must be called before using the C-style functions.
29
_default_psi_options_ = new Options();
30
_default_psi_options_->add_int_option("CHARGE",0);
31
_default_psi_options_->add_int_option("MULTP",1);
35
* Close the options interface. Must be called before the end of the program when using the C-style interface.
39
delete _default_psi_options_;
43
* Read the options from the input file.
47
_default_psi_options_->read_options();
54
return(_default_psi_options_->print());
58
* Add a boolean option.
59
* @param cstr_option a string containing the parameter name
60
* @param bool_default the default value for this option
62
void options_add_bool(const char* cstr_option,bool bool_default)
64
_default_psi_options_->add_bool_option(cstr_option,bool_default);
68
* Add an integer option.
69
* @param cstr_option a string containing the parameter name
70
* @param int_default the default value for this option
72
void options_add_int(const char* cstr_option,int int_default)
74
_default_psi_options_->add_int_option(cstr_option,int_default);
78
* Add a double option.
79
* @param cstr_option a string containing the parameter name
80
* @param double_default the default value for this option
82
void options_add_double(const char* cstr_option,double double_default)
84
_default_psi_options_->add_double_option(cstr_option,double_default);
88
* Add a string option.
89
* @param cstr_option a string containing the parameter name
90
* @param cstr_default the default value for this option
92
void options_add_str(const char* cstr_option,const char* cstr_default)
94
_default_psi_options_->add_str_option(cstr_option,cstr_default);
98
* Add a string option with a restricted number of choices.
99
* @param cstr_option a string containing the parameter name
100
* @param cstr_default the default value for this option
101
* @param cstr_choices a list of choices separated by a space
103
void options_add_str_with_choices(const char* cstr_option,const char* cstr_default,const char* cstr_choices)
105
_default_psi_options_->add_str_option_with_choices(cstr_option,cstr_default,cstr_choices);
109
* Get the value of a boolean option
111
bool options_get_bool(const char* cstr_option){
112
return(_default_psi_options_->get_bool_option(cstr_option));
116
* Get the value of an integer option
118
int options_get_int(const char* cstr_option){
119
return(_default_psi_options_->get_int_option(cstr_option));
122
// double options_get_double(char* cstr_option){
123
// return(_default_psi_options_->get_double_option(cstr_option));
127
* Get the value of a string option
129
std::string options_get_str(const char* cstr_option){
130
return(_default_psi_options_->get_str_option(cstr_option));
143
void Options::add_bool_option(const char* cstr_option,bool bool_default)
145
string str_option(cstr_option);
146
// Make sure that the option that we are adding is not already present
147
BoolOptionsMap::iterator it = bool_options.find(str_option);
148
if(it==bool_options.end()){
149
bool_options[str_option];
150
bool_options[str_option].option = bool_default;
152
fprintf(outfile,"\n Options: add_bool_option(%s), option %s already declared",cstr_option,cstr_option);
156
void Options::add_int_option(const char* cstr_option,int int_default)
158
string str_option(cstr_option);
159
// Make sure that the option that we are adding is not already present
160
IntOptionsMap::iterator it = int_options.find(str_option);
161
if(it==int_options.end()){
162
int_options[str_option];
163
int_options[str_option].option = int_default;
165
fprintf(outfile,"\n Options: add_bool_option(%s), option %s already declared",cstr_option,cstr_option);
169
void Options::add_double_option(const char* cstr_option,double double_default)
171
string str_option(cstr_option);
172
// Make sure that the option that we are adding is not already present
173
DoubleOptionsMap::iterator it = double_options.find(str_option);
174
if(it==double_options.end()){
175
double_options[str_option];
176
double_options[str_option].option = double_default;
178
fprintf(outfile,"\n Options: add_bool_option(%s), option %s already declared",cstr_option,cstr_option);
183
void Options::add_str_option(const char* cstr_option,const char* cstr_default)
185
add_str_option_with_choices(cstr_option,cstr_default,"");
188
void Options::add_str_option_with_choices(const char* cstr_option,const char* cstr_default,const char* cstr_choices)
190
string str_option(cstr_option);
191
string str_default(cstr_default);
192
string str_choices(cstr_choices);
193
// Make sure that the option that we are adding is not already present
194
StringOptionsMap::iterator it = string_options.find(str_option);
195
if(it==string_options.end()){
196
string_options[str_option];
197
string_options[str_option].option = str_default;
198
string_options[str_option].choices = str_choices;
200
fprintf(outfile,"\n Options: add_str_option(%s), option %s already declared",cstr_option,cstr_option);
204
bool Options::get_bool_option(const char* cstr_option)
206
string str_option(cstr_option);
207
// Make sure that the option that we are adding is available
208
BoolOptionsMap::iterator it = bool_options.find(str_option);
209
if(it!=bool_options.end()){
210
return(bool_options[str_option].option);
212
fprintf(outfile,"\n Options: get_str_option(%s), option %s is not available",cstr_option,cstr_option);
219
int Options::get_int_option(const char* cstr_option)
221
string str_option(cstr_option);
222
// Make sure that the option that we are adding is available
223
IntOptionsMap::iterator it = int_options.find(str_option);
224
if(it!=int_options.end()){
225
return(int_options[str_option].option);
227
fprintf(outfile,"\n Options: get_str_option(%s), option %s is not available",cstr_option,cstr_option);
234
std::string Options::get_str_option(const char* cstr_option)
236
string str_option(cstr_option);
237
// Make sure that the option that we are adding is available
238
StringOptionsMap::iterator it = string_options.find(str_option);
239
if(it!=string_options.end()){
240
return(string_options[str_option].option);
242
fprintf(outfile,"\n Options: get_str_option(%s), option %s is not available",cstr_option,cstr_option);
249
void Options::set_bool_option(const char* cstr_option, bool bool_value)
251
string str_option(cstr_option);
252
bool_options[str_option].option = bool_value;
255
void Options::set_int_option(const char* cstr_option, int int_value)
257
string str_option(cstr_option);
258
int_options[str_option].option = int_value;
261
void Options::set_double_option(const char* cstr_option, double double_value)
263
string str_option(cstr_option);
264
double_options[str_option].option = double_value;
267
void Options::set_str_option(const char* cstr_option, const char* cstr_value)
269
string str_option(cstr_option);
270
string_options[str_option].option = string(cstr_value);
273
void Options::read_options()
276
for(BoolOptionsMap::iterator it = bool_options.begin();it != bool_options.end();++it){
280
for(IntOptionsMap::iterator it = int_options.begin();it != int_options.end();++it){
284
for(DoubleOptionsMap::iterator it = double_options.begin();it != double_options.end();++it){
288
for(StringOptionsMap::iterator it = string_options.begin();it != string_options.end();++it){
294
void Options::read_bool(BoolOptionsMap::iterator& it)
297
char* cstr_label = new char [it->first.length () + 1];
298
memset (cstr_label, 0x00, it->first.length () + 1);
299
strcpy (cstr_label, it->first.c_str ());
301
int status = ip_boolean(cstr_label,&int_value,0);
302
if (status == IPE_OK){
303
it->second.option = bool(int_value);
308
void Options::read_int(IntOptionsMap::iterator& it)
311
char* cstr_label = new char [it->first.length () + 1];
312
memset (cstr_label, 0x00, it->first.length () + 1);
313
strcpy (cstr_label, it->first.c_str ());
315
int status = ip_data(cstr_label,const_cast<char *>("%d"),&int_value,0);
316
if (status == IPE_OK){
317
it->second.option = int_value;
322
void Options::read_double(DoubleOptionsMap::iterator& it)
325
char* cstr_label = new char [it->first.length () + 1];
326
memset (cstr_label, 0x00, it->first.length () + 1);
327
strcpy (cstr_label, it->first.c_str ());
329
int status = ip_data(cstr_label,const_cast<char *>("%f"),&double_value,0);
330
if (status == IPE_OK){
331
it->second.option = double_value;
337
void Options::read_string(StringOptionsMap::iterator& it)
339
char* cstr_value = NULL;
340
char* cstr_label = new char [it->first.length () + 1];
341
memset (cstr_label, 0x00, it->first.length () + 1);
342
strcpy (cstr_label, it->first.c_str ());
344
int status = ip_string(cstr_label,&cstr_value,0);
345
if (status == IPE_OK){
346
it->second.option = string(cstr_value);
347
// Check if there are restricted choices
348
if(it->second.choices.size()>0){
349
bool wrong_input = true;
350
vector<string> choices = split(it->second.choices);
351
for(int i=0;i<choices.size();++i){
352
if(it->second.option==choices[i])
356
fprintf(outfile,"\n\n Option %s has the wrong value (%s).\n Possible choices are\n %s\n",it->first.c_str(),it->second.option.c_str(),it->second.choices.c_str());
367
void Options::print()
369
int max_option_width = 0;
370
for(BoolOptionsMap::iterator it = bool_options.begin();it != bool_options.end();++it)
371
if(it->first.size() > max_option_width)
372
max_option_width = it->first.size();
374
for(IntOptionsMap::iterator it = int_options.begin();it != int_options.end();++it)
375
if(it->first.size() > max_option_width)
376
max_option_width = it->first.size();
378
for(DoubleOptionsMap::iterator it = double_options.begin();it != double_options.end();++it)
379
if(it->first.size() > max_option_width)
380
max_option_width = it->first.size();
382
for(StringOptionsMap::iterator it = string_options.begin();it != string_options.end();++it)
383
if(it->first.size() > max_option_width)
384
max_option_width = it->first.size();
386
fprintf(outfile,"\n\n Options:");
387
fprintf(outfile,"\n ----------------------------------------------------------------------------");
388
for(BoolOptionsMap::iterator it = bool_options.begin();it != bool_options.end();++it){
389
fprintf(outfile,"\n %s",it->first.c_str());
390
for(int n =0; n < max_option_width - it->first.size(); ++n) fprintf(outfile," ");
391
fprintf(outfile," = %s",it->second.option ? "TRUE" : "FALSE" );
393
for(IntOptionsMap::iterator it = int_options.begin();it != int_options.end();++it){
394
fprintf(outfile,"\n %s",it->first.c_str());
395
for(int n =0; n < max_option_width - it->first.size(); ++n) fprintf(outfile," ");
396
fprintf(outfile," = %-d",it->second.option);
398
for(DoubleOptionsMap::iterator it = double_options.begin();it != double_options.end();++it){
399
fprintf(outfile,"\n %s",it->first.c_str());
400
for(int n =0; n < max_option_width - it->first.size(); ++n) fprintf(outfile," ");
401
fprintf(outfile," = %-lf",it->second.option);
403
for(StringOptionsMap::iterator it = string_options.begin();it != string_options.end();++it){
404
fprintf(outfile,"\n %s",it->first.c_str());
405
for(int n =0; n < max_option_width - it->first.size(); ++n) fprintf(outfile," ");
406
fprintf(outfile," = %s",it->second.option.c_str());
408
fprintf(outfile,"\n ----------------------------------------------------------------------------");