Debian Codegraph @

src/ConfigVar.cpp

raw
1 //Implements the ConfigVar class
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7 // INCLUDES
8
9 #ifdef HAVE_CONFIG_H
10 #include "e2config.h"
11 #endif
12 #include "ConfigVar.hpp"
13
14 #include <fstream>
15
16 // IMPLEMENTATION
17
18 // constructor
19 ConfigVar::ConfigVar()
20 {
21 }
22
23 // construct & read in the given config file
24 ConfigVar::ConfigVar(const char *filename, const char *delimiter)
25 {
26 readVar(filename, delimiter);
27 }
28
29 // return the value for the named option
30 String ConfigVar::entry(const char *reference)
31 {
32 return params[reference];
33 }
34
35 // same as above, but in handy operator form
36 String ConfigVar::operator[](const char *reference)
37 {
38 return params[reference];
39 }
40
41 // read in options from the given file, splitting option/value at delimiter
42 int ConfigVar::readVar(const char *filename, const char *delimiter)
43 {
44 std::ifstream input(filename);
45 char buffer[2048];
46
47 params.clear();
48
49 if (!input)
50 return 1;
51
52 while (input.getline(buffer, sizeof(buffer))) {
53
54 char *command = strtok(buffer, delimiter);
55 if (!command)
56 continue;
57
58 char *parameter = strtok(NULL, delimiter);
59 if (!parameter)
60 continue;
61
62 // strip delimiters
63 while (*parameter == '"' || *parameter == '\'' || *parameter == ' ')
64 parameter++;
65 int offset = strlen(parameter) - 1;
66
67 while (parameter[offset] == '"' || parameter[offset] == '\'')
68 parameter[offset--] = '\0';
69
70 offset = strlen(command) - 1;
71 while (command[offset] == ' ')
72 command[offset--] = '\0';
73
74 params[command] = parameter;
75 }
76
77 input.close();
78 return 0;
79 }