/[projects]/smsdaemon/ConfigFile.cpp
ViewVC logotype

Contents of /smsdaemon/ConfigFile.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 208 - (show annotations) (download)
Sun Dec 21 18:41:08 2008 UTC (15 years, 4 months ago) by torben
File size: 3840 byte(s)
Enable dynamic reload of plugins/tasks when recieving a HUP signal

1 #include "ConfigFile.h"
2 #include "Util.h"
3
4 #include <fstream>
5 #include <sstream>
6 #include <stdexcept>
7
8 #include <stdlib.h>
9
10 const char SEPERATOR = '/';
11
12 std::string trim(std::string const& source, char const* delims = " \t\r\n")
13 {
14 std::string result(source);
15 std::string::size_type index = result.find_last_not_of(delims);
16 if (index != std::string::npos)
17 result.erase(++index);
18
19 index = result.find_first_not_of(delims);
20 if (index != std::string::npos)
21 result.erase(0, index);
22 else
23 result.erase();
24 return result;
25 }
26
27 ConfigFile::ConfigFile()
28 {
29 }
30
31 ConfigFile::ConfigFile(std::string const& configFile)
32 : filename_(configFile)
33 {
34 Open();
35 }
36
37
38 bool ConfigFile::Reload()
39 {
40 content_.clear();
41 return Open();
42 }
43
44 bool ConfigFile::Open(std::string const& configFile)
45 {
46 content_.clear();
47 filename_ = configFile;
48 return Open();
49 }
50
51 bool ConfigFile::Open()
52 {
53 std::ifstream file( filename_.c_str() );
54
55 std::string line;
56 std::string name;
57 std::string value;
58 std::string inSection;
59 int posEqual;
60
61 if (!file)
62 {
63 return false;
64 }
65
66 while (std::getline(file,line))
67 {
68
69 if (! line.length()) continue;
70
71 if (line[0] == '#') continue;
72 if (line[0] == ';') continue;
73
74 if (line[0] == '[')
75 {
76 inSection=trim(line.substr(1,line.find(']')-1));
77 continue;
78 }
79
80 posEqual=line.find('=');
81 name = trim(line.substr(0,posEqual));
82 value = trim(line.substr(posEqual+1));
83
84 content_.insert(std::make_pair(inSection+SEPERATOR+name,Value(value)));
85 }
86 return true;
87 }
88
89 Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry) const
90 {
91
92 content_iterator ci = content_.find(section + SEPERATOR + entry);
93
94 if (ci == content_.end()) throw "does not exist";
95
96 return ci->second;
97 }
98
99 std::vector<Value> ConfigFile::GetValues(std::string const& section, std::string const& entry) const
100 {
101 std::vector<Value> values;
102 std::string search = section + SEPERATOR + entry;
103 std::pair<content_iterator, content_iterator> range = content_.equal_range(search);
104
105 for (content_iterator it=range.first; it != range.second; ++it)
106 {
107 values.push_back( it->second );
108 }
109 return values;
110 }
111
112 Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, double value)
113 {
114 try
115 {
116 return GetValue(section, entry);
117 }
118 catch (const char *)
119 {
120 return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value)))->second;
121 }
122 }
123
124 Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, std::string const& value)
125 {
126 try
127 {
128 return GetValue(section, entry);
129 }
130 catch (const char *)
131 {
132 return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value)))->second;
133 }
134 }
135
136 std::string ConfigFile::DumpConfig() const
137 {
138 std::ostringstream ss;
139 for (content_iterator it=content_.begin(); it!=content_.end(); ++it)
140 {
141 ss << it->first << "=" << it->second.StringValue() << std::endl;
142 }
143 return ss.str();
144 }
145
146
147 namespace ConfigHelper
148 {
149 std::map<std::string, std::string> ParseArguments(const std::string& args)
150 {
151 std::map<std::string, std::string> config;
152
153 std::vector<std::string> sections = Util::str_split(args, ";");
154
155 for (unsigned i=0; i<sections.size(); i++)
156 {
157 std::string current = Util::str_trim(sections[i]);
158 std::vector<std::string> params = Util::str_split( current, "=");
159 if (params.size() != 2)
160 throw std::runtime_error(std::string("syntax error in config line: ") + args);
161
162 std::string key = Util::str_tolower(Util::str_trim(params[0]));
163 std::string val = Util::str_trim(params[1]);
164 config[key] = val;
165 }
166
167 return config;
168 }
169
170 int StringToInt(const std::string& input)
171 {
172 char* endptr = (char*) input.c_str();
173 int retval = strtol(input.c_str(), &endptr, 10);
174
175 if ( *endptr != 0)
176 throw std::runtime_error( std::string("Error: can not convert this to a number: ") + input);
177
178 return retval;
179 }
180 }

  ViewVC Help
Powered by ViewVC 1.1.20