~ubuntu-branches/ubuntu/intrepid/dansguardian/intrepid-security

« back to all changes in this revision

Viewing changes to src/ConfigVar.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alexander Wirt
  • Date: 2008-04-06 14:47:06 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080406144706-2r26l1rougdmb1sd
Tags: 2.9.9.3-2
This time build with gcc 4.3 (Closes: #454889) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//Implements the ConfigVar class
 
2
 
 
3
//Please refer to http://dansguardian.org/?page=copyright2
 
4
//for the license for this code.
 
5
 
 
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.
 
10
//
 
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.
 
15
//
 
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
 
19
 
 
20
// INCLUDES
 
21
 
 
22
#include "ConfigVar.hpp"
 
23
 
 
24
#include <fstream>
 
25
 
 
26
 
 
27
// IMPLEMENTATION
 
28
 
 
29
// constructor
 
30
ConfigVar::ConfigVar()
 
31
{
 
32
}
 
33
 
 
34
// construct & read in the given config file
 
35
ConfigVar::ConfigVar(const char *filename, const char *delimiter)
 
36
{
 
37
        readVar(filename, delimiter);
 
38
}
 
39
 
 
40
// return the value for the named option
 
41
String ConfigVar::entry(const char *reference)
 
42
{
 
43
        return params[reference];
 
44
}
 
45
 
 
46
// same as above, but in handy operator form
 
47
String ConfigVar::operator[] (const char *reference)
 
48
{
 
49
        return params[reference];
 
50
}
 
51
 
 
52
// read in options from the given file, splitting option/value at delimiter
 
53
int ConfigVar::readVar(const char *filename, const char *delimiter)
 
54
{
 
55
        std::ifstream input(filename);
 
56
        char buffer[2048];
 
57
 
 
58
        params.clear();
 
59
 
 
60
        if (!input)
 
61
                return 1;
 
62
 
 
63
        while (input.getline(buffer, sizeof(buffer))) {
 
64
 
 
65
                char *command = strtok(buffer, delimiter);
 
66
                if (!command)
 
67
                        continue;
 
68
 
 
69
                char *parameter = strtok(NULL, delimiter);
 
70
                if (!parameter)
 
71
                        continue;
 
72
 
 
73
                // strip delimiters
 
74
                while (*parameter == '"' || *parameter == '\'' || *parameter == ' ')
 
75
                        parameter++;
 
76
                int offset = strlen(parameter) - 1;
 
77
 
 
78
                while (parameter[offset] == '"' || parameter[offset] == '\'')
 
79
                        parameter[offset--] = '\0';
 
80
 
 
81
                offset = strlen(command) - 1;
 
82
                while (command[offset] == ' ')
 
83
                        command[offset--] = '\0';
 
84
 
 
85
                params[command] = parameter;
 
86
        }
 
87
 
 
88
        input.close();
 
89
        return 0;
 
90
}