wsdlpull svntrunk
ConfigFile.cpp
Go to the documentation of this file.
1// ConfigFile.cpp
2
3#include "ConfigFile.h"
4
5using std::string;
6
7ConfigFile::ConfigFile( string filename, bool isList,string delimiter,
8 string comment, string sentry )
9 : myDelimiter(delimiter), myComment(comment), mySentry(sentry),file(filename),listmode(isList)
10{
11 // Construct a ConfigFile, getting keys and values from given file
12
13 std::ifstream in( filename.c_str() );
14
15 if( !in ) throw file_not_found( filename );
16
17 in >> (*this);
18}
19
20
22 : myDelimiter( string(1,'=') ), myComment( string(2,'#') ),listmode(false)
23{
24 // Construct a ConfigFile without a file; empty
25}
26
27// Construct the ConfigFile from a given file if its present
28void ConfigFile::load(string filename,bool isList)
29{
30 file=filename;
31 listmode=isList;
32 std::ifstream in( filename.c_str() );
33 if( in )
34 in>>(*this);
35 in.close();
36}
37
38// Save the ConfigFile into a given file
40{
41 std::ofstream out( file.c_str() );
42 if( !out ) throw file_not_found( file );
43 out<<(*this);
44 out.close();
45}
46
47void ConfigFile::remove( const string& key )
48{
49 // Remove key and its value
50 myContents.erase( myContents.find( key ) );
51 return;
52}
53
54
55bool ConfigFile::keyExists( const string& key ) const
56{
57 // Indicate whether key is found
58 mapci p = myContents.find( key );
59 return ( p != myContents.end() );
60}
61
62
63/* static */
64void ConfigFile::trim( string& s )
65{
66 // Remove leading and trailing whitespace
67 static const char whitespace[] = " \n\t\v\r\f";
68 s.erase( 0, s.find_first_not_of(whitespace) );
69 s.erase( s.find_last_not_of(whitespace) + 1U );
70}
71
72
73std::ostream& operator<<( std::ostream& os, const ConfigFile& cf )
74{
75 // Save a ConfigFile to os
76 for( ConfigFile::mapci p = cf.myContents.begin();
77 p != cf.myContents.end();
78 ++p )
79 {
80 os << p->first << " " ;
81 if(!cf.listmode)
82 {
83 os<<cf.myDelimiter << " ";
84 os << p->second ;
85 }
86 os<<std::endl;
87 }
88 return os;
89}
90
91
92std::istream& operator>>( std::istream& is, ConfigFile& cf )
93{
94 // Load a ConfigFile from is
95 // Read in keys and values, keeping internal whitespace
96 typedef string::size_type pos;
97 const string& delim = cf.myDelimiter; // separator
98 const string& comm = cf.myComment; // comment
99 const string& sentry = cf.mySentry; // end of file sentry
100 const pos skip = delim.length(); // length of separator
101
102 string nextline = ""; // might need to read ahead to see where value ends
103
104 while( is || nextline.length() > 0 )
105 {
106 // Read an entire line at a time
107 string line;
108 if( nextline.length() > 0 )
109 {
110 line = nextline; // we read ahead; use it now
111 nextline = "";
112 }
113 else
114 {
115 std::getline( is, line );
116 }
117
118 // Ignore comments
119 line = line.substr( 0, line.find(comm) );
120
121 // Check for end of file sentry
122 if( sentry != "" && line.find(sentry) != string::npos ) return is;
123
124 // Parse the line if it contains a delimiter
125 pos delimPos = line.find( delim );
126 if( delimPos < string::npos )
127 {
128 // Extract the key
129 string key = line.substr( 0, delimPos );
130 line.replace( 0, delimPos+skip, "" );
131
132 // See if value continues on the next line
133 // Stop at blank line, next line with a key, end of stream,
134 // or end of file sentry
135 bool terminate = false;
136 while( !terminate && is )
137 {
138 std::getline( is, nextline );
139 terminate = true;
140
141 string nlcopy = nextline;
142 ConfigFile::trim(nlcopy);
143 if( nlcopy == "" ) continue;
144
145 nextline = nextline.substr( 0, nextline.find(comm) );
146 if( nextline.find(delim) != string::npos )
147 continue;
148 if( sentry != "" && nextline.find(sentry) != string::npos )
149 continue;
150
151 nlcopy = nextline;
152 ConfigFile::trim(nlcopy);
153 if( nlcopy != "" ) line += "\n";
154 line += nextline;
155 terminate = false;
156 }
157
158 // Store key and value
159 ConfigFile::trim(key);
160 ConfigFile::trim(line);
161 cf.myContents[key] = line; // overwrites if key is repeated
162 }
163 else if(cf.listmode)
164 {
165 ConfigFile::trim(line);
166 cf.myContents[line]=" ";
167 }
168 }
169
170 return is;
171}
std::istream & operator>>(std::istream &is, ConfigFile &cf)
Definition: ConfigFile.cpp:92
std::ostream & operator<<(std::ostream &os, const ConfigFile &cf)
Definition: ConfigFile.cpp:73
bool listmode
Definition: ConfigFile.h:65
string myComment
Definition: ConfigFile.h:58
static void trim(string &s)
Definition: ConfigFile.cpp:64
std::map< string, string > myContents
Definition: ConfigFile.h:60
bool keyExists(const string &key) const
Definition: ConfigFile.cpp:55
std::string file
Definition: ConfigFile.h:64
void load(string filename, bool isList=false)
Definition: ConfigFile.cpp:28
void save()
Definition: ConfigFile.cpp:39
string myDelimiter
Definition: ConfigFile.h:57
string mySentry
Definition: ConfigFile.h:59
std::map< string, string >::const_iterator mapci
Definition: ConfigFile.h:63
void remove(const string &key)
Definition: ConfigFile.cpp:47