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

Contents of /smsdaemon/ConfigFile.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 202 - (show annotations) (download)
Thu Dec 18 23:29:23 2008 UTC (15 years, 5 months ago) by torben
File size: 2660 byte(s)
Enable ConfigFile to return a string containing all config settings.

1 #include "ConfigFile.h"
2
3 #include <fstream>
4 #include <sstream>
5
6 const char SEPERATOR = '/';
7
8 std::string trim(std::string const& source, char const* delims = " \t\r\n")
9 {
10 std::string result(source);
11 std::string::size_type index = result.find_last_not_of(delims);
12 if (index != std::string::npos)
13 result.erase(++index);
14
15 index = result.find_first_not_of(delims);
16 if (index != std::string::npos)
17 result.erase(0, index);
18 else
19 result.erase();
20 return result;
21 }
22
23 ConfigFile::ConfigFile()
24 {
25 }
26
27 ConfigFile::ConfigFile(std::string const& configFile)
28 {
29 Open(configFile);
30 }
31
32 bool ConfigFile::Open(std::string const& configFile)
33 {
34 std::ifstream file(configFile.c_str());
35
36 std::string line;
37 std::string name;
38 std::string value;
39 std::string inSection;
40 int posEqual;
41
42 if (!file)
43 {
44 return false;
45 }
46
47 while (std::getline(file,line))
48 {
49
50 if (! line.length()) continue;
51
52 if (line[0] == '#') continue;
53 if (line[0] == ';') continue;
54
55 if (line[0] == '[')
56 {
57 inSection=trim(line.substr(1,line.find(']')-1));
58 continue;
59 }
60
61 posEqual=line.find('=');
62 name = trim(line.substr(0,posEqual));
63 value = trim(line.substr(posEqual+1));
64
65 content_.insert(std::make_pair(inSection+SEPERATOR+name,Value(value)));
66 }
67 return true;
68 }
69
70 Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry) const
71 {
72
73 content_iterator ci = content_.find(section + SEPERATOR + entry);
74
75 if (ci == content_.end()) throw "does not exist";
76
77 return ci->second;
78 }
79
80 std::vector<Value> ConfigFile::GetValues(std::string const& section, std::string const& entry) const
81 {
82 std::vector<Value> values;
83 std::string search = section + SEPERATOR + entry;
84 std::pair<content_iterator, content_iterator> range = content_.equal_range(search);
85
86 for (content_iterator it=range.first; it != range.second; ++it)
87 {
88 values.push_back( it->second );
89 }
90 return values;
91 }
92
93 Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, double value)
94 {
95 try
96 {
97 return GetValue(section, entry);
98 }
99 catch (const char *)
100 {
101 return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value)))->second;
102 }
103 }
104
105 Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, std::string const& value)
106 {
107 try
108 {
109 return GetValue(section, entry);
110 }
111 catch (const char *)
112 {
113 return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value)))->second;
114 }
115 }
116
117 std::string ConfigFile::DumpConfig() const
118 {
119 std::ostringstream ss;
120 for (content_iterator it=content_.begin(); it!=content_.end(); ++it)
121 {
122 ss << it->first << "=" << it->second.StringValue() << std::endl;
123 }
124 return ss.str();
125 }

  ViewVC Help
Powered by ViewVC 1.1.20