~ubuntu-branches/ubuntu/quantal/psicode/quantal

« back to all changes in this revision

Viewing changes to src/lib/liboptions/liboptions.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck, Michael Banck, Daniel Leidert
  • Date: 2009-02-23 00:12:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090223001202-rutldoy3dimfpesc
Tags: 3.4.0-1
* New upstream release.

[ Michael Banck ]
* debian/patches/01_DESTDIR.dpatch: Refreshed.
* debian/patches/02_FHS.dpatch: Removed, applied upstream.
* debian/patches/03_debian_docdir: Likewise.
* debian/patches/04_man.dpatch: Likewise.
* debian/patches/06_466828_fix_gcc_43_ftbfs.dpatch: Likewise.
* debian/patches/07_464867_move_executables: Fixed and refreshed.
* debian/patches/00list: Adjusted.
* debian/control: Improved description.
* debian/patches-held: Removed.
* debian/rules (install/psi3): Do not ship the ruby bindings for now.

[ Daniel Leidert ]
* debian/rules: Fix txtdir via DEB_MAKE_INSTALL_TARGET.
* debian/patches/01_DESTDIR.dpatch: Refreshed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*!
 
2
  \file
 
3
  \brief Implementation of the options class and corresponding C-style interface
 
4
  \defgroup LIBOPTIONS liboptions: A library to read user options
 
5
  \ingroup LIBOPTIONS
 
6
*/
 
7
 
 
8
#include <vector>
 
9
#include <cstdlib>
 
10
#include <cstring>
 
11
#include <algorithm>
 
12
 
 
13
#include <libipv1/ip_lib.h>
 
14
#include <libutil/libutil.h>
 
15
 
 
16
#include "liboptions.h"
 
17
 
 
18
extern FILE* outfile;
 
19
 
 
20
namespace psi {
 
21
 
 
22
Options* _default_psi_options_;
 
23
 
 
24
/*!
 
25
 * Initialize the options interface. Must be called before using the C-style functions. 
 
26
 */
 
27
void options_init()
 
28
{
 
29
  _default_psi_options_ = new Options();
 
30
  _default_psi_options_->add_int_option("CHARGE",0);
 
31
  _default_psi_options_->add_int_option("MULTP",1);
 
32
}
 
33
 
 
34
/*!
 
35
 * Close the options interface. Must be called before the end of the program when using the C-style interface. 
 
36
 */
 
37
void options_close()
 
38
{
 
39
  delete _default_psi_options_;
 
40
}
 
41
 
 
42
/*!
 
43
 * Read the options from the input file. 
 
44
 */
 
45
void options_read()
 
46
{
 
47
  _default_psi_options_->read_options();
 
48
}
 
49
 
 
50
/*!
 
51
 * Print the options. 
 
52
 */
 
53
void options_print(){
 
54
  return(_default_psi_options_->print());    
 
55
}
 
56
 
 
57
/*!
 
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
 
61
 */
 
62
void options_add_bool(const char* cstr_option,bool bool_default)
 
63
{
 
64
  _default_psi_options_->add_bool_option(cstr_option,bool_default);
 
65
}
 
66
 
 
67
/*!
 
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
 
71
 */
 
72
void options_add_int(const char* cstr_option,int int_default)
 
73
{
 
74
  _default_psi_options_->add_int_option(cstr_option,int_default);
 
75
}
 
76
 
 
77
/*!
 
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
 
81
 */
 
82
void options_add_double(const char* cstr_option,double double_default)
 
83
{
 
84
  _default_psi_options_->add_double_option(cstr_option,double_default);
 
85
}
 
86
 
 
87
/*!
 
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
 
91
 */
 
92
void options_add_str(const char* cstr_option,const char* cstr_default)
 
93
{
 
94
  _default_psi_options_->add_str_option(cstr_option,cstr_default);
 
95
}
 
96
 
 
97
/*!
 
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
 
102
 */
 
103
void options_add_str_with_choices(const char* cstr_option,const char* cstr_default,const char* cstr_choices)
 
104
{
 
105
  _default_psi_options_->add_str_option_with_choices(cstr_option,cstr_default,cstr_choices);
 
106
}
 
107
 
 
108
/*!
 
109
 * Get the value of a boolean option
 
110
 */
 
111
bool options_get_bool(const char* cstr_option){
 
112
  return(_default_psi_options_->get_bool_option(cstr_option));    
 
113
}
 
114
 
 
115
/*!
 
116
 * Get the value of an integer option
 
117
 */
 
118
int options_get_int(const char* cstr_option){
 
119
  return(_default_psi_options_->get_int_option(cstr_option));    
 
120
}
 
121
 
 
122
// double options_get_double(char* cstr_option){
 
123
//   return(_default_psi_options_->get_double_option(cstr_option));    
 
124
// }
 
125
 
 
126
/*!
 
127
 * Get the value of a string option
 
128
 */
 
129
std::string options_get_str(const char* cstr_option){
 
130
  return(_default_psi_options_->get_str_option(cstr_option));    
 
131
}
 
132
 
 
133
using namespace std;
 
134
 
 
135
Options::Options()
 
136
{
 
137
}
 
138
 
 
139
Options::~Options()
 
140
{
 
141
}
 
142
 
 
143
void Options::add_bool_option(const char* cstr_option,bool bool_default)
 
144
{
 
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;
 
151
  }else{
 
152
    fprintf(outfile,"\n  Options: add_bool_option(%s), option %s already declared",cstr_option,cstr_option);
 
153
  }
 
154
}
 
155
 
 
156
void Options::add_int_option(const char* cstr_option,int int_default)
 
157
{
 
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;
 
164
  }else{
 
165
    fprintf(outfile,"\n  Options: add_bool_option(%s), option %s already declared",cstr_option,cstr_option);
 
166
  }
 
167
}
 
168
 
 
169
void Options::add_double_option(const char* cstr_option,double double_default)
 
170
{
 
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;
 
177
  }else{
 
178
    fprintf(outfile,"\n  Options: add_bool_option(%s), option %s already declared",cstr_option,cstr_option);
 
179
  }
 
180
}
 
181
 
 
182
 
 
183
void Options::add_str_option(const char* cstr_option,const char* cstr_default)
 
184
{
 
185
  add_str_option_with_choices(cstr_option,cstr_default,"");
 
186
}
 
187
 
 
188
void Options::add_str_option_with_choices(const char* cstr_option,const char* cstr_default,const char* cstr_choices)
 
189
{
 
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;
 
199
  }else{
 
200
    fprintf(outfile,"\n  Options: add_str_option(%s), option %s already declared",cstr_option,cstr_option);
 
201
  }
 
202
}
 
203
 
 
204
bool Options::get_bool_option(const char* cstr_option)
 
205
{
 
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);
 
211
  }else{
 
212
    fprintf(outfile,"\n  Options: get_str_option(%s), option %s is not available",cstr_option,cstr_option);
 
213
    fflush(outfile);
 
214
    abort();
 
215
    return(false);
 
216
  }
 
217
}
 
218
 
 
219
int Options::get_int_option(const char* cstr_option)
 
220
{
 
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);
 
226
  }else{
 
227
    fprintf(outfile,"\n  Options: get_str_option(%s), option %s is not available",cstr_option,cstr_option);
 
228
    fflush(outfile);
 
229
    abort();
 
230
    return(0);
 
231
  }
 
232
}
 
233
 
 
234
std::string Options::get_str_option(const char* cstr_option)
 
235
{
 
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);
 
241
  }else{
 
242
    fprintf(outfile,"\n  Options: get_str_option(%s), option %s is not available",cstr_option,cstr_option);
 
243
    fflush(outfile);
 
244
    abort();
 
245
    return("NA");
 
246
  }
 
247
}
 
248
 
 
249
void Options::set_bool_option(const char* cstr_option, bool bool_value)
 
250
{
 
251
  string str_option(cstr_option);
 
252
  bool_options[str_option].option  = bool_value;
 
253
}
 
254
 
 
255
void Options::set_int_option(const char* cstr_option, int int_value)
 
256
{
 
257
  string str_option(cstr_option);
 
258
  int_options[str_option].option  = int_value;
 
259
}
 
260
 
 
261
void Options::set_double_option(const char* cstr_option, double double_value)
 
262
{
 
263
  string str_option(cstr_option);
 
264
  double_options[str_option].option  = double_value;
 
265
}
 
266
 
 
267
void Options::set_str_option(const char* cstr_option, const char* cstr_value)
 
268
{
 
269
  string str_option(cstr_option);
 
270
  string_options[str_option].option  = string(cstr_value);
 
271
}
 
272
 
 
273
void Options::read_options()
 
274
{
 
275
  // Bool
 
276
  for(BoolOptionsMap::iterator it = bool_options.begin();it != bool_options.end();++it){
 
277
    read_bool(it);
 
278
  }
 
279
  // Int
 
280
  for(IntOptionsMap::iterator it = int_options.begin();it != int_options.end();++it){
 
281
    read_int(it);
 
282
  }
 
283
  // Int
 
284
  for(DoubleOptionsMap::iterator it = double_options.begin();it != double_options.end();++it){
 
285
    read_double(it);
 
286
  }
 
287
  // String Options
 
288
  for(StringOptionsMap::iterator it = string_options.begin();it != string_options.end();++it){
 
289
    read_string(it);
 
290
  }
 
291
 
 
292
}
 
293
 
 
294
void Options::read_bool(BoolOptionsMap::iterator& it)
 
295
{
 
296
  int int_value;
 
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 ());
 
300
 
 
301
  int status = ip_boolean(cstr_label,&int_value,0);
 
302
  if (status == IPE_OK){
 
303
    it->second.option = bool(int_value);
 
304
  }
 
305
  delete[] cstr_label;
 
306
}
 
307
 
 
308
void Options::read_int(IntOptionsMap::iterator& it)
 
309
{
 
310
  int int_value;
 
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 ());
 
314
 
 
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;
 
318
  }
 
319
  delete[] cstr_label;
 
320
}
 
321
 
 
322
void Options::read_double(DoubleOptionsMap::iterator& it)
 
323
{
 
324
  double double_value;
 
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 ());
 
328
 
 
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;
 
332
  }
 
333
  delete[] cstr_label;
 
334
}
 
335
 
 
336
 
 
337
void Options::read_string(StringOptionsMap::iterator& it)
 
338
{
 
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 ());
 
343
 
 
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])
 
353
          wrong_input = false;
 
354
      }
 
355
      if(wrong_input){
 
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());
 
357
        fflush(outfile);
 
358
        abort();
 
359
      }
 
360
    }
 
361
    if(cstr_value!=NULL)
 
362
      free(cstr_value);
 
363
  }
 
364
  delete[] cstr_label;
 
365
}
 
366
 
 
367
void Options::print()
 
368
{
 
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();
 
373
  
 
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();
 
377
  
 
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();
 
381
  
 
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();
 
385
 
 
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" );
 
392
  }
 
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);
 
397
  }
 
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);
 
402
  }
 
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());
 
407
  }
 
408
  fprintf(outfile,"\n  ----------------------------------------------------------------------------");
 
409
}
 
410
 
 
411
}