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

Diff of /smsdaemon/ConfigFile.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 146 by torben, Sun Dec 7 20:06:12 2008 UTC revision 208 by torben, Sun Dec 21 18:41:08 2008 UTC
# Line 1  Line 1 
1  #include "ConfigFile.h"  #include "ConfigFile.h"
2    #include "Util.h"
3    
4  #include <fstream>  #include <fstream>
5    #include <sstream>
6    #include <stdexcept>
7    
8    #include <stdlib.h>
9    
10  const char SEPERATOR = '/';  const char SEPERATOR = '/';
11    
# Line 24  ConfigFile::ConfigFile() Line 29  ConfigFile::ConfigFile()
29  }  }
30    
31  ConfigFile::ConfigFile(std::string const& configFile)  ConfigFile::ConfigFile(std::string const& configFile)
32            : filename_(configFile)
33    {
34            Open();
35    }
36    
37    
38    bool ConfigFile::Reload()
39  {  {
40          Open(configFile);          content_.clear();
41            return Open();
42  }  }
43    
44  bool ConfigFile::Open(std::string const& configFile)  bool ConfigFile::Open(std::string const& configFile)
45  {  {
46          std::ifstream file(configFile.c_str());          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;          std::string line;
56          std::string name;          std::string name;
# Line 61  bool ConfigFile::Open(std::string const& Line 81  bool ConfigFile::Open(std::string const&
81                  name  = trim(line.substr(0,posEqual));                  name  = trim(line.substr(0,posEqual));
82                  value = trim(line.substr(posEqual+1));                  value = trim(line.substr(posEqual+1));
83    
84                    content_.insert(std::make_pair(inSection+SEPERATOR+name,Value(value)));
                 content_[inSection+SEPERATOR+name]=Value(value);  
85          }          }
86          return true;          return true;
87  }  }
# Line 70  bool ConfigFile::Open(std::string const& Line 89  bool ConfigFile::Open(std::string const&
89  Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry) const  Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry) const
90  {  {
91    
92          std::map<std::string,Value>::const_iterator ci = content_.find(section + SEPERATOR + entry);          content_iterator ci = content_.find(section + SEPERATOR + entry);
93    
94          if (ci == content_.end()) throw "does not exist";          if (ci == content_.end()) throw "does not exist";
95    
96          return ci->second;          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)  Value const& ConfigFile::GetValue(std::string const& section, std::string const& entry, double value)
113  {  {
114          try          try
# Line 85  Value const& ConfigFile::GetValue(std::s Line 117  Value const& ConfigFile::GetValue(std::s
117          }          }
118          catch (const char *)          catch (const char *)
119          {          {
120                  return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value))).first->second;                  return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value)))->second;
121          }          }
122  }  }
123    
# Line 97  Value const& ConfigFile::GetValue(std::s Line 129  Value const& ConfigFile::GetValue(std::s
129          }          }
130          catch (const char *)          catch (const char *)
131          {          {
132                  return content_.insert(std::make_pair(section+SEPERATOR+entry, Value(value))).first->second;                  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    }

Legend:
Removed from v.146  
changed lines
  Added in v.208

  ViewVC Help
Powered by ViewVC 1.1.20