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