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