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

Annotation of /smsdaemon/ConfigFile.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 201 - (hide annotations) (download)
Thu Dec 18 23:19:08 2008 UTC (15 years, 5 months ago) by torben
File size: 2408 byte(s)
Make configfile work with duplicate keys

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

  ViewVC Help
Powered by ViewVC 1.1.20