1
//Implements the ConfigVar class
3
//Please refer to http://dansguardian.org/?page=copyright2
4
//for the license for this code.
6
// This program is free software; you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published by
8
// the Free Software Foundation; either version 2 of the License, or
9
// (at your option) any later version.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
#include "ConfigVar.hpp"
30
ConfigVar::ConfigVar()
34
// construct & read in the given config file
35
ConfigVar::ConfigVar(const char *filename, const char *delimiter)
37
readVar(filename, delimiter);
40
// return the value for the named option
41
String ConfigVar::entry(const char *reference)
43
return params[reference];
46
// same as above, but in handy operator form
47
String ConfigVar::operator[] (const char *reference)
49
return params[reference];
52
// read in options from the given file, splitting option/value at delimiter
53
int ConfigVar::readVar(const char *filename, const char *delimiter)
55
std::ifstream input(filename);
63
while (input.getline(buffer, sizeof(buffer))) {
65
char *command = strtok(buffer, delimiter);
69
char *parameter = strtok(NULL, delimiter);
74
while (*parameter == '"' || *parameter == '\'' || *parameter == ' ')
76
int offset = strlen(parameter) - 1;
78
while (parameter[offset] == '"' || parameter[offset] == '\'')
79
parameter[offset--] = '\0';
81
offset = strlen(command) - 1;
82
while (command[offset] == ' ')
83
command[offset--] = '\0';
85
params[command] = parameter;