老外写的一段代码,在Server中编写这个类读取配置文件比较实用
-
- #pragma once
-
- #include
- #include
- #include
- #include
- #include
-
-
-
-
-
-
- class Config {
-
- protected :
- std::string m_Delimiter;
- std::string m_Comment;
- std::map m_Contents;
-
- typedef std::map::iterator mapi;
- typedef std::map::const_iterator mapci;
-
- public :
-
- Config( std::string filename,std::string delimiter = "=" ,std::string comment = "#" );
- Config();
- template < class T> T Read( const std::string& in_key ) const ;
- template < class T> T Read( const std::string& in_key, const T& in_value ) const ;
- template < class T> bool ReadInto( T& out_var, const std::string& in_key ) const ;
- template < class T>
- bool ReadInto( T& out_var, const std::string& in_key, const T& in_value ) const ;
- bool FileExist(std::string filename);
- void ReadFile(std::string filename,std::string delimiter = "=" ,std::string comment = "#" );
-
-
- bool KeyExists( const std::string& in_key ) const ;
-
-
- template < class T> void Add( const std::string& in_key, const T& in_value );
- void Remove( const std::string& in_key );
-
-
- std::string GetDelimiter() const { return m_Delimiter; }
- std::string GetComment() const { return m_Comment; }
- std::string SetDelimiter( const std::string& in_s )
- { std::string old = m_Delimiter; m_Delimiter = in_s; return old; }
- std::string SetComment( const std::string& in_s )
- { std::string old = m_Comment; m_Comment = in_s; return old; }
-
-
- friend std::ostream& operator<<( std::ostream& os, const Config& cf );
- friend std::istream& operator>>( std::istream& is, Config& cf );
-
- protected :
- template < class T> static std::string T_as_string( const T& t );
- template < class T> static T string_as_T( const std::string& s );
- static void Trim( std::string& inout_s );
-
-
-
- public :
- struct File_not_found {
- std::string filename;
- File_not_found( const std::string& filename_ = std::string() )
- : filename(filename_) {} };
- struct Key_not_found {
- std::string key;
- Key_not_found( const std::string& key_ = std::string() )
- : key(key_) {} };
- };
-
-
-
- template < class T>
- std::string Config::T_as_string( const T& t )
- {
-
-
- std::ostringstream ost;
- ost << t;
- return ost.str();
- }
-
-
-
- template < class T>
- T Config::string_as_T( const std::string& s )
- {
-
-
- T t;
- std::istringstream ist(s);
- ist >> t;
- return t;
- }
-
-
-
- template <>
- inline std::string Config::string_as_T( const std::string& s )
- {
-
-
- return s;
- }
-
-
-
- template <>
- inline bool Config::string_as_T< bool >( const std::string& s )
- {
-
-
-
- bool b = true ;
- std::string sup = s;
- for ( std::string::iterator p = sup.begin(); p != sup.end(); ++p )
- *p = toupper(*p);
- if ( sup==std::string( "FALSE" ) || sup==std::string( "F" ) ||
- sup==std::string("NO" ) || sup==std::string( "N" ) ||
- sup==std::string("0" ) || sup==std::string( "NONE" ) )
- b = false ;
- return b;
- }
-
-
- template < class T>
- T Config::Read( const std::string& key ) const
- {
-
- mapci p = m_Contents.find(key);
- if ( p == m_Contents.end() ) throw Key_not_found(key);
- return string_as_T( p->second );
- }
-
-
- template < class T>
- T Config::Read( const std::string& key, const T& value ) const
- {
-
-
- mapci p = m_Contents.find(key);
- if ( p == m_Contents.end() ) return value;
- return string_as_T( p->second );
- }
-
-
- template < class T>
- bool Config::ReadInto( T& var, const std::string& key ) const
- {
-
-
-
- mapci p = m_Contents.find(key);
- bool found = ( p != m_Contents.end() );
- if ( found ) var = string_as_T( p->second );
- return found;
- }
-
-
- template < class T>
- bool Config::ReadInto( T& var, const std::string& key, const T& value ) const
- {
-
-
-
- mapci p = m_Contents.find(key);
- bool found = ( p != m_Contents.end() );
- if ( found )
- var = string_as_T( p->second );
- else
- var = value;
- return found;
- }
-
-
- template < class T>
- void Config::Add( const std::string& in_key, const T& value )
- {
-
- std::string v = T_as_string( value );
- std::string key=in_key;
- trim(key);
- trim(v);
- m_Contents[key] = v;
- return ;
- }
-
-
- #include "Config.h"
-
- using namespace std;
-
-
- Config::Config( string filename, string delimiter,
- string comment )
- : m_Delimiter(delimiter), m_Comment(comment)
- {
-
-
- std::ifstream in( filename.c_str() );
-
- if ( !in ) throw File_not_found( filename );
-
- in >> (*this );
- }
-
-
- Config::Config()
- : m_Delimiter( string(1,'=' ) ), m_Comment( string(1, '#' ) )
- {
-
- }
-
-
-
- bool Config::KeyExists( const string& key ) const
- {
-
- mapci p = m_Contents.find( key );
- return ( p != m_Contents.end() );
- }
-
-
-
- void Config::Trim( string& inout_s )
- {
-
- static const char whitespace[] = " /n/t/v/r/f" ;
- inout_s.erase( 0, inout_s.find_first_not_of(whitespace) );
- inout_s.erase( inout_s.find_last_not_of(whitespace) + 1U );
- }
-
-
- std::ostream& operator<<( std::ostream& os, const Config& cf )
- {
-
- for ( Config::mapci p = cf.m_Contents.begin();
- p != cf.m_Contents.end();
- ++p )
- {
- os << p->first << " " << cf.m_Delimiter << " " ;
- os << p->second << std::endl;
- }
- return os;
- }
-
- void Config::Remove( const string& key )
- {
-
- m_Contents.erase( m_Contents.find( key ) );
- return ;
- }
-
- std::istream& operator>>( std::istream& is, Config& cf )
- {
-
-
- typedef string::size_type pos;
- const string& delim = cf.m_Delimiter;
- const string& comm = cf.m_Comment;
- const pos skip = delim.length();
-
- string nextline = "" ;
-
- while ( is || nextline.length() > 0 )
- {
-
- string line;
- if ( nextline.length() > 0 )
- {
- line = nextline;
- nextline = "" ;
- }
- else
- {
- std::getline( is, line );
- }
-
-
- line = line.substr( 0, line.find(comm) );
-
-
- pos delimPos = line.find( delim );
- if ( delimPos < string::npos )
- {
-
- string key = line.substr( 0, delimPos );
- line.replace( 0, delimPos+skip, "" );
-
-
-
-
- bool terminate = false ;
- while ( !terminate && is )
- {
- std::getline( is, nextline );
- terminate = true ;
-
- string nlcopy = nextline;
- Config::Trim(nlcopy);
- if ( nlcopy == "" ) continue ;
-
- nextline = nextline.substr( 0, nextline.find(comm) );
- if ( nextline.find(delim) != string::npos )
- continue ;
-
- nlcopy = nextline;
- Config::Trim(nlcopy);
- if ( nlcopy != "" ) line += "/n" ;
- line += nextline;
- terminate = false ;
- }
-
-
- Config::Trim(key);
- Config::Trim(line);
- cf.m_Contents[key] = line;
- }
- }
-
- return is;
- }
- bool Config::FileExist(std::string filename)
- {
- bool exist= false ;
- std::ifstream in( filename.c_str() );
- if ( in )
- exist = true ;
- return exist;
- }
-
- void Config::ReadFile( string filename, string delimiter,
- string comment )
- {
- m_Delimiter = delimiter;
- m_Comment = comment;
- std::ifstream in( filename.c_str() );
-
- if ( !in ) throw File_not_found( filename );
-
- in >> (*this );
- }
-
- #include "Config.h"
- int main()
- {
- int port;
- std::string ipAddress;
- std::string username;
- std::string password;
- const char ConfigFile[]= "config.txt" ;
- Config configSettings(ConfigFile);
-
- port = configSettings.Read("port" , 0);
- ipAddress = configSettings.Read("ipAddress" , ipAddress);
- username = configSettings.Read("username" , username);
- password = configSettings.Read("password" , password);
- std::cout<<"port:" <
- std::cout<<"ipAddress:" <
- std::cout<<"username:" <
- std::cout<<"password:" <
-
- return 0;
- }
config.txt的文件内容:
ipAddress=10.10.90.125
port=3001
username=mark
password=2d2df5a
编译运行输出:
port:3001
ipAddress:10.10.90.125
username:mark
password:2d2df5a
这个类还有很多其他的方法,可以调用试试。
转载出处: http://cooker.javaeye.com/blog/777455