/[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 205 by torben, Fri Dec 19 22:29:25 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 61  bool ConfigFile::Open(std::string const& Line 66  bool ConfigFile::Open(std::string const&
66                  name  = trim(line.substr(0,posEqual));                  name  = trim(line.substr(0,posEqual));
67                  value = trim(line.substr(posEqual+1));                  value = trim(line.substr(posEqual+1));
68    
69                    content_.insert(std::make_pair(inSection+SEPERATOR+name,Value(value)));
                 content_[inSection+SEPERATOR+name]=Value(value);  
70          }          }
71          return true;          return true;
72  }  }
# Line 70  bool ConfigFile::Open(std::string const& Line 74  bool ConfigFile::Open(std::string const&
74  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
75  {  {
76    
77          std::map<std::string,Value>::const_iterator ci = content_.find(section + SEPERATOR + entry);          content_iterator ci = content_.find(section + SEPERATOR + entry);
78    
79          if (ci == content_.end()) throw "does not exist";          if (ci == content_.end()) throw "does not exist";
80    
81          return ci->second;          return ci->second;
82  }  }
83    
84    std::vector<Value> ConfigFile::GetValues(std::string const& section, std::string const& entry) const
85    {
86            std::vector<Value> values;
87            std::string search = section + SEPERATOR + entry;
88            std::pair<content_iterator, content_iterator> range = content_.equal_range(search);
89    
90            for (content_iterator it=range.first; it != range.second; ++it)
91            {
92                    values.push_back( it->second );
93            }
94            return values;
95    }
96    
97  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)
98  {  {
99          try          try
# Line 85  Value const& ConfigFile::GetValue(std::s Line 102  Value const& ConfigFile::GetValue(std::s
102          }          }
103          catch (const char *)          catch (const char *)
104          {          {
105                  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;
106          }          }
107  }  }
108    
# Line 97  Value const& ConfigFile::GetValue(std::s Line 114  Value const& ConfigFile::GetValue(std::s
114          }          }
115          catch (const char *)          catch (const char *)
116          {          {
117                  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;
118          }          }
119  }  }
120    
121    std::string ConfigFile::DumpConfig() const
122    {
123            std::ostringstream ss;  
124            for (content_iterator it=content_.begin(); it!=content_.end(); ++it)
125            {
126                    ss << it->first << "=" << it->second.StringValue() << std::endl;
127            }
128            return ss.str();
129    }
130    
131    
132    namespace ConfigHelper
133    {
134        std::map<std::string, std::string> ParseArguments(const std::string& args)
135            {
136                    std::map<std::string, std::string> config;
137    
138                    std::vector<std::string> sections = Util::str_split(args, ";");
139    
140                    for (unsigned i=0; i<sections.size(); i++)
141                    {
142                            std::string current = Util::str_trim(sections[i]);
143                            std::vector<std::string> params = Util::str_split( current, "=");
144                            if (params.size() != 2)
145                                    throw std::runtime_error(std::string("syntax error in config line: ") + args);
146    
147                            std::string key = Util::str_tolower(Util::str_trim(params[0]));
148                            std::string val = Util::str_trim(params[1]);
149                            config[key] = val;
150                    }
151            
152                    return config;
153            }
154    
155            int StringToInt(const std::string& input)
156            {
157                    char* endptr = (char*) input.c_str();
158                    int retval = strtol(input.c_str(), &endptr, 10);
159    
160                    if ( *endptr != 0)
161                            throw std::runtime_error( std::string("Error: can not convert this to a number: ") + input);
162    
163                    return retval;
164            }
165    }

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

  ViewVC Help
Powered by ViewVC 1.1.20