00001
00002
00003
00004
00005
00011 #include "Properties.h"
00012 #include <iostream>
00013 #include <sstream>
00014 #include <ctype.h>
00015
00016
00017
00018
00019
00020
00021 inline std::istream& skip_line(std::istream& s_in)
00022 {
00023 char c =' ';
00024 while ( s_in.get(c) )
00025 #ifdef macintosh
00026 if (c == '\r') break;
00027 #else
00028 if (c == '\n') break;
00029 #endif
00030 return s_in;
00031 }
00032
00033
00034
00035
00036
00037 std::string lower( const std::string& s )
00038 {
00039 std::string tmp = s;
00040 for (int i=tmp.size(); --i >=0;)
00041 tmp[i] = (char)::tolower(s[i]);
00042 return tmp;
00043 }
00044
00045
00046
00047
00048
00049 Properties::Properties()
00050 {}
00051
00052
00053
00054
00055
00056 Properties::~Properties()
00057 {}
00058
00059
00060
00061
00062
00063 void Properties::load(const std::string &fileName)
00064 {
00065 std::ifstream file;
00066 file.open( fileName.c_str(), std::ios::in );
00067
00068 std::string key;
00069 std::string value;
00070 while( !file.eof() )
00071 {
00072 key = "";
00073
00074
00075 file >> std::ws;
00076 std::getline(file, key, '=');
00077 if (key == "") continue;
00078
00079
00080 if ((key[0] == '#') || (key[0] == '!')
00081 || ((key[0] == '/') && (key[1] =='/')))
00082 file >> skip_line;
00083 else {
00084 file >> std::ws;
00085 std::getline(file, value, ';');
00086
00087 size_t ipos = key.find(' ',0);
00088 if (ipos != std::string::npos) key = key.substr(0,ipos);
00089
00090
00091 (*this)[lower(key)] = lower(value);
00092 }
00093 }
00094 file.close();
00095 }
00096
00097
00098
00099
00100 void Properties::store(
00101 const std::string& filename,
00102 const std::string& header) const
00103 {
00104 std::ofstream file;
00105 file.open( filename.c_str(), std::ios::out );
00106 file << "# " << header << std::endl;
00107 print( file );
00108 file.close();
00109 }
00110
00111
00112
00113
00114 void Properties::print( std::ostream& cout ) const
00115 {
00116 std::map<std::string,std::string>::const_iterator curr = _map.begin();
00117 std::map<std::string,std::string>::const_iterator end = _map.end();
00118 while (curr != end) {
00119 if ( (*curr).second != "")
00120 cout << (*curr).first << "=" << (*curr).second << ';' << std::endl;
00121 ++curr;
00122 }
00123 }
00124
00125
00126
00127
00128
00129 std::string& Properties::operator[]( const std::string& key)
00130 {
00131 return _map[key];
00132 }
00133
00134 const std::string& Properties::operator[]( const std::string& key) const
00135 {
00136
00137 return ((std::map<std::string,std::string>&)_map)[key];
00138 }
00139