automatic commit
[folded-ctf.git] / param_parser.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify  //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation.                         //
6 //                                                                       //
7 // This program is distributed in the hope that it will be useful, but   //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of            //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
10 // General Public License for more details.                              //
11 //                                                                       //
12 // You should have received a copy of the GNU General Public License     //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.  //
14 //                                                                       //
15 // Written by Francois Fleuret                                           //
16 // (C) Idiap Research Institute                                          //
17 //                                                                       //
18 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
19 ///////////////////////////////////////////////////////////////////////////
20
21 // All this is clearly non-optimal, loaded with news and deletes and
22 // should be rewritten.
23
24 #include <string.h>
25
26 #include "param_parser.h"
27
28 ParamParser::ParamParser() : _nb_max(10),
29                              _nb(0),
30                              _names(new char *[_nb_max]),
31                              _values(new char *[_nb_max]),
32                              _changed(new bool[_nb_max]) { }
33
34 ParamParser::~ParamParser() {
35   for(int k = 0; k < _nb; k++) {
36     delete[] _names[k];
37     delete[] _values[k];
38   }
39   delete[] _names;
40   delete[] _values;
41   delete[] _changed;
42 }
43
44 void ParamParser::add_association(const char *variable_name, const char *variable_value, bool change) {
45   int n;
46
47   for(n = 0; n < _nb && strcmp(variable_name, _names[n]) != 0; n++);
48
49   if(n < _nb) {
50     delete[] _values[n];
51     _values[n] = new char[strlen(variable_value) + 1];
52     strcpy(_values[n], variable_value);
53     _changed[n] = change;
54   } else {
55     int nm;
56     nm = _nb_max; grow(&nm, _nb, &_names, 2);
57     nm = _nb_max; grow(&nm, _nb, &_values, 2);
58     grow(&_nb_max, _nb, &_changed, 2);
59
60     _names[_nb] = new char[strlen(variable_name) + 1];
61     strcpy(_names[_nb], variable_name);
62     _values[_nb] = new char[strlen(variable_value) + 1];
63     strcpy(_values[_nb], variable_value);
64     _changed[_nb] = change;
65     _nb++;
66   }
67 }
68
69 char *ParamParser::get_association(const char *variable_name) {
70   int n;
71   for(n = 0; n < _nb && strcmp(variable_name, _names[n]) != 0; n++);
72   if(n < _nb) return _values[n];
73   else        {
74     cerr << "Unknown parameter \"" << variable_name << "\", existing ones are" << endl;
75     for(int n = 0; n < _nb; n++)
76       cerr << "   \"" << _names[n] << "\"" << endl;
77     exit(1);
78   }
79 }
80
81 int ParamParser::get_association_int(const char *variable_name) {
82   char *u = get_association(variable_name);
83   char *s = u;
84   while(*s)
85     if((*s < '0' || *s > '9') && *s != '-') {
86       cerr << "Non-numerical value for " << variable_name << " (" << u << ")" << endl;
87       exit(1);
88     } else s++;
89   return atoi(u);
90 }
91
92 scalar_t ParamParser::get_association_scalar(const char *variable_name) {
93   char *u = get_association(variable_name);
94   char *s = u;
95   while(*s)
96     if((*s < '0' || *s > '9') && *s != '.' && *s != 'e' && *s != '-') {
97       cerr << "Non-numerical value for " << variable_name << " (" << u << ")" << endl;
98       exit(1);
99     } else s++;
100   return atof(u);
101 }
102
103 bool ParamParser::get_association_bool(const char *variable_name) {
104   char *value = get_association(variable_name);
105   if(strcasecmp(value, "") == 0 || strcasecmp(value, "y") == 0 || strcasecmp(value, "yes") == 0) return true;
106   if(strcasecmp(value, "n") == 0 || strcasecmp(value, "no") == 0) return false;
107   cerr << "Expects nothing (for yes), or y[es] or n[o] for a boolean argument and got '" << value << "'" << endl;
108   exit(1);
109 }
110
111 void ParamParser::parse_options(int argc, char **argv,
112                                 bool allow_undefined,
113                                 int *new_argc, char **new_argv) {
114
115   int i = 1;
116
117   if(new_argc && new_argv)
118     new_argv[(*new_argc)++] = argv[0];
119
120   while(i < argc) {
121     if(strncmp(argv[i], "--", 2) == 0) {
122       // This is so 70s! I luuuuv it!
123       char variable_name[buffer_size] = "", variable_value[buffer_size] = "";
124       char *o = argv[i] + 2, *s = variable_name, *u = variable_value;
125       while(*o && *o != '=') *s++ = *o++;
126       *s = '\0';
127       if(*o) { o++; while(*o) *u++ = *o++; }
128       *u = '\0';
129       if(!allow_undefined) get_association(variable_name);
130       add_association(variable_name, variable_value, true);
131     } else {
132       if(new_argc && new_argv)
133         new_argv[(*new_argc)++] = argv[i];
134       else {
135         cerr << "Can not parse " << argv[i] << endl;
136         exit(1);
137       }
138     }
139     i++;
140   }
141 }
142
143 void ParamParser::print_all(ostream *os) {
144   for(int n = 0; n < _nb; n++) {
145     (*os) << (_changed[n] ? " * " : "   ") << "\"" << _names[n] << "\" \"" << _values[n] << "\"" << endl;
146   }
147 }
148