diff options
author | Kostyantyn Ovechko <fastinetserver@gmail.com> | 2010-06-19 06:51:41 +0300 |
---|---|---|
committer | Kostyantyn Ovechko <fastinetserver@gmail.com> | 2010-06-19 06:51:41 +0300 |
commit | ba07d129e1ff9bd4128a4b397a6921d19a4d486f (patch) | |
tree | 5175520777c2ad9870de894492baf397df3493c8 /segget/config.cpp | |
parent | Add support for WHIRLPOOL, SHA512, MD5, CRC32 (diff) | |
download | idfetch-ba07d129e1ff9bd4128a4b397a6921d19a4d486f.tar.gz idfetch-ba07d129e1ff9bd4128a4b397a6921d19a4d486f.tar.bz2 idfetch-ba07d129e1ff9bd4128a4b397a6921d19a4d486f.zip |
Add configuration file segget.conf
Diffstat (limited to 'segget/config.cpp')
-rw-r--r-- | segget/config.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/segget/config.cpp b/segget/config.cpp new file mode 100644 index 0000000..206a960 --- /dev/null +++ b/segget/config.cpp @@ -0,0 +1,103 @@ +#ifndef __CONFIG_H__ +#define __CONFIG_H__ + +#include <string> +#include <map> +#include "str.cpp" +using namespace std; + +map<string,string> content_; + +class ConfigFile { + +public: + + ConfigFile(std::string const& configFile); + + int set(string &dst,string const& section, string const& entry) const; + int set(int &dst,string const& section, string const& entry) const; + int set(bool &dst,string const& section, string const& entry) const; +}; + +#endif +#include <fstream> + +std::string trim(std::string const& source, char const* delims = " \t\r\n") { + std::string result(source); + std::string::size_type index = result.find_last_not_of(delims); + if(index != std::string::npos) + result.erase(++index); + + index = result.find_first_not_of(delims); + if(index != std::string::npos) + result.erase(0, index); + else + result.erase(); + return result; +} + +ConfigFile::ConfigFile(std::string const& configFile) { + std::ifstream file(configFile.c_str()); + + std::string line; + std::string name; + std::string value; + std::string inSection; + int posEqual; + while (std::getline(file,line)) { + + if (! line.length()) continue; + + if (line[0] == '#') continue; + if (line[0] == ';') continue; + + line=noupper(line); + if (line[0] == '[') { + inSection=trim(line.substr(1,line.find(']')-1)); + continue; + } + + posEqual=line.find('='); + name = trim(line.substr(0,posEqual)); + value = trim(line.substr(posEqual+1)); + + content_[inSection+'/'+name]=value; + } +} + +int ConfigFile::set(string &dst, std::string const& section, std::string const& entry) const { + + std::map<std::string,string>::const_iterator ci = content_.find(section + '/' + entry); + + if (ci == content_.end()){ + return 1; + } + else{ + dst=ci->second; + return 0; + } +} +int ConfigFile::set(int &dst, std::string const& section, std::string const& entry) const { + + std::map<std::string,string>::const_iterator ci = content_.find(section + '/' + entry); + + if (ci == content_.end()){ + return 1; + } + else{ + dst=atoi(ci->second.c_str()); + return 0; + } +} +int ConfigFile::set(bool &dst, std::string const& section, std::string const& entry) const { + + std::map<std::string,string>::const_iterator ci = content_.find(section + '/' + entry); + + if (ci == content_.end()){ + return 1; + } + else{ + dst=atoi(ci->second.c_str()); + return 0; + } +}
\ No newline at end of file |