2 * folded-ctf is an implementation of the folded hierarchy of
3 * classifiers for object detection, developed by Francois Fleuret
6 * Copyright (c) 2008 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of folded-ctf.
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.
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.
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/>.
25 // All this is clearly non-optimal, loaded with news and deletes and
26 // should be rewritten.
30 #include "param_parser.h"
32 ParamParser::ParamParser() : _nb_max(10),
34 _names(new char *[_nb_max]),
35 _values(new char *[_nb_max]),
36 _changed(new bool[_nb_max]) { }
38 ParamParser::~ParamParser() {
39 for(int k = 0; k < _nb; k++) {
48 void ParamParser::add_association(const char *variable_name, const char *variable_value, bool change) {
51 for(n = 0; n < _nb && strcmp(variable_name, _names[n]) != 0; n++);
55 _values[n] = new char[strlen(variable_value) + 1];
56 strcpy(_values[n], variable_value);
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);
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;
73 char *ParamParser::get_association(const char *variable_name) {
75 for(n = 0; n < _nb && strcmp(variable_name, _names[n]) != 0; n++);
76 if(n < _nb) return _values[n];
78 cerr << "Unknown parameter \"" << variable_name << "\", existing ones are" << endl;
79 for(int n = 0; n < _nb; n++)
80 cerr << " \"" << _names[n] << "\"" << endl;
85 int ParamParser::get_association_int(const char *variable_name) {
86 char *u = get_association(variable_name);
89 if((*s < '0' || *s > '9') && *s != '-') {
90 cerr << "Non-numerical value for " << variable_name << " (" << u << ")" << endl;
96 scalar_t ParamParser::get_association_scalar(const char *variable_name) {
97 char *u = get_association(variable_name);
100 if((*s < '0' || *s > '9') && *s != '.' && *s != 'e' && *s != '-') {
101 cerr << "Non-numerical value for " << variable_name << " (" << u << ")" << endl;
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;
115 void ParamParser::parse_options(int argc, char **argv,
116 bool allow_undefined,
117 int *new_argc, char **new_argv) {
121 if(new_argc && new_argv)
122 new_argv[(*new_argc)++] = argv[0];
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++;
131 if(*o) { o++; while(*o) *u++ = *o++; }
133 if(!allow_undefined) get_association(variable_name);
134 add_association(variable_name, variable_value, true);
136 if(new_argc && new_argv)
137 new_argv[(*new_argc)++] = argv[i];
139 cerr << "Can not parse " << argv[i] << endl;
147 void ParamParser::print_all(ostream *os) {
148 for(int n = 0; n < _nb; n++) {
149 (*os) << (_changed[n] ? " * " : " ") << "\"" << _names[n] << "\" \"" << _values[n] << "\"" << endl;