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