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

Annotation of /smsdaemon/ConfigFile.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

1 torben 146 #include "ConfigFile.h"
2 torben 205 #include "Util.h"
3 torben 146
4     #include <fstream>
5 torben 202 #include <sstream>
6 torben 205 #include <stdexcept>
7 torben 146
8 torben 205 #include <stdlib.h>
9    
10 torben 146 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 torben 208 : filename_(configFile)
33 torben 146 {
34 torben 208 Open();
35 torben 146 }
36    
37 torben 208
38     bool ConfigFile::Reload()
39     {
40     content_.clear();
41     return Open();
42     }
43    
44 torben 146 bool ConfigFile::Open(std::string const& configFile)
45     {
46 torben 208 content_.clear();
47     filename_ = configFile;
48     return Open();
49     }
50 torben 146
51 torben 208 bool ConfigFile::Open()
52     {
53     std::ifstream file( filename_.c_str() );
54    
55 torben 146 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 torben 201 content_.insert(std::make_pair(inSection+SEPERATOR+name,Value(value)));
85 torben 146 }
86     return true;
87     }
88    
89     Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry) const
90     {
91    
92 torben 201 content_iterator ci = content_.find(section + SEPERATOR + entry);
93 torben 146
94     if (ci == content_.end()) throw "does not exist";
95    
96     return ci->second;
97     }
98    
99 torben 201 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 torben 146 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 torben 201 return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value)))->second;
121 torben 146 }
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 torben 201 return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value)))->second;
133 torben 146 }
134     }
135    
136 torben 202 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 torben 205
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