2 * svrt is the ``Synthetic Visual Reasoning Test'', an image
3 * generator for evaluating classification performance of machine
4 * learning systems, humans and primates.
6 * Copyright (c) 2009 Idiap Research Institute, http://www.idiap.ch/
7 * Written by Francois Fleuret <francois.fleuret@idiap.ch>
9 * This file is part of svrt.
11 * svrt is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 3 as
13 * published by the Free Software Foundation.
15 * svrt 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 selector. 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 long int ParamParser::get_association_long_int(const char *variable_name) {
97 char *u = get_association(variable_name);
100 if((*s < '0' || *s > '9') && *s != '-') {
101 cerr << "Non-numerical value for " << variable_name << " (" << u << ")" << endl;
107 scalar_t ParamParser::get_association_scalar(const char *variable_name) {
108 char *u = get_association(variable_name);
111 if((*s < '0' || *s > '9') && *s != '.' && *s != 'e' && *s != '-') {
112 cerr << "Non-numerical value for " << variable_name << " (" << u << ")" << endl;
118 bool ParamParser::get_association_bool(const char *variable_name) {
119 char *value = get_association(variable_name);
120 if(strcasecmp(value, "") == 0 || strcasecmp(value, "y") == 0 || strcasecmp(value, "yes") == 0) return true;
121 if(strcasecmp(value, "n") == 0 || strcasecmp(value, "no") == 0) return false;
122 cerr << "Expects nothing (for yes), or y[es] or n[o] for a boolean argument and got '" << value << "'" << endl;
126 void ParamParser::parse_options(int argc, char **argv,
127 bool allow_undefined,
128 int *new_argc, char **new_argv) {
132 if(new_argc && new_argv)
133 new_argv[(*new_argc)++] = argv[0];
136 if(strncmp(argv[i], "--", 2) == 0) {
137 // This is so 70s! I luuuuv it!
138 char variable_name[buffer_size] = "", variable_value[buffer_size] = "";
139 char *o = argv[i] + 2, *s = variable_name, *u = variable_value;
140 while(*o && *o != '=') *s++ = *o++;
142 if(*o) { o++; while(*o) *u++ = *o++; }
144 if(!allow_undefined) get_association(variable_name);
145 add_association(variable_name, variable_value, true);
147 if(new_argc && new_argv)
148 new_argv[(*new_argc)++] = argv[i];
150 cerr << "Can not parse " << argv[i] << endl;
158 void ParamParser::print_all(ostream *os) {
159 for(int n = 0; n < _nb; n++) {
160 (*os) << (_changed[n] ? " * " : " ") << "\"" << _names[n] << "\" \"" << _values[n] << "\"" << endl;