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