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