00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "ConfigFile.h"
00028
00029 using std::string;
00030
00031 ConfigFile::ConfigFile( string filename, string delimiter,
00032 string comment, string sentry )
00033 : myDelimiter(delimiter), myComment(comment), mySentry(sentry)
00034 {
00035
00036
00037 std::ifstream in( filename.c_str() );
00038
00039 if( !in ) throw file_not_found( filename );
00040
00041 in >> (*this);
00042 }
00043
00044
00045 ConfigFile::ConfigFile()
00046 : myDelimiter( string(1,'=') ), myComment( string(1,'#') )
00047 {
00048
00049 }
00050
00051
00052 void ConfigFile::remove( const string& key )
00053 {
00054
00055 myContents.erase( myContents.find( key ) );
00056 return;
00057 }
00058
00059
00060 bool ConfigFile::keyExists( const string& key ) const
00061 {
00062
00063 mapci p = myContents.find( key );
00064 return ( p != myContents.end() );
00065 }
00066
00067
00068
00069 void ConfigFile::trim( string& s )
00070 {
00071
00072 static const char whitespace[] = " \n\t\v\r\f";
00073 s.erase( 0, s.find_first_not_of(whitespace) );
00074 s.erase( s.find_last_not_of(whitespace) + 1U );
00075 }
00076
00077
00078 std::ostream& operator<<( std::ostream& os, const ConfigFile& cf )
00079 {
00080
00081 for( ConfigFile::mapci p = cf.myContents.begin();
00082 p != cf.myContents.end();
00083 ++p )
00084 {
00085 os << p->first << " " << cf.myDelimiter << " ";
00086 os << p->second << std::endl;
00087 }
00088 return os;
00089 }
00090
00091
00092 std::istream& operator>>( std::istream& is, ConfigFile& cf )
00093 {
00094
00095
00096 typedef string::size_type pos;
00097 const string& delim = cf.myDelimiter;
00098 const string& comm = cf.myComment;
00099 const string& sentry = cf.mySentry;
00100 const pos skip = delim.length();
00101
00102 string nextline = "";
00103
00104 while( is || nextline.length() > 0 )
00105 {
00106
00107 string line;
00108 if( nextline.length() > 0 )
00109 {
00110 line = nextline;
00111 nextline = "";
00112 }
00113 else
00114 {
00115 std::getline( is, line );
00116 }
00117
00118
00119 line = line.substr( 0, line.find(comm) );
00120
00121
00122 if( sentry != "" && line.find(sentry) != string::npos ) return is;
00123
00124
00125 pos delimPos = line.find( delim );
00126 if( delimPos < string::npos )
00127 {
00128
00129 string key = line.substr( 0, delimPos );
00130 line.replace( 0, delimPos+skip, "" );
00131
00132
00133
00134
00135 bool terminate = false;
00136 while( !terminate && is )
00137 {
00138 std::getline( is, nextline );
00139 terminate = true;
00140
00141 string nlcopy = nextline;
00142 ConfigFile::trim(nlcopy);
00143 if( nlcopy == "" ) continue;
00144
00145 nextline = nextline.substr( 0, nextline.find(comm) );
00146 if( nextline.find(delim) != string::npos )
00147 continue;
00148 if( sentry != "" && nextline.find(sentry) != string::npos )
00149 continue;
00150
00151 nlcopy = nextline;
00152 ConfigFile::trim(nlcopy);
00153 if( nlcopy != "" ) line += "\n";
00154 line += nextline;
00155 terminate = false;
00156 }
00157
00158
00159 ConfigFile::trim(key);
00160 ConfigFile::trim(line);
00161 cf.myContents[key] = line;
00162 }
00163 }
00164
00165 return is;
00166 }