2
* xml.cpp is part of Brewtarget, and is Copyright Philip G. Lee
3
* (rocketman768@gmail.com), 2009.
5
* Brewtarget is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* Brewtarget is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
26
// Returns true if str1 begins with str2
27
bool beginsWith( const string &str1, const string &str2 )
29
if( str1.size() < str2.size() )
32
return ( str1.compare( 0, str2.size(), str2 ) == 0 );
35
std::string replaceXmlCodes( const std::string &str )
37
std::string::size_type ndx;
40
// This iterative approach doesn't exactly conform to standards. During
41
// processing, it may create other false XML codes which will get translated.
44
ndx = ret.find("&");
45
if( ndx >= ret.size() || ndx == std::string::npos )
48
ret.insert( ndx, "&" );
52
ndx = ret.find("<");
53
if( ndx >= ret.size() || ndx == std::string::npos )
56
ret.insert( ndx, "<" );
60
ndx = ret.find(">");
61
if( ndx >= ret.size() || ndx == std::string::npos )
64
ret.insert( ndx, ">" );
68
ndx = ret.find(""");
69
if( ndx >= ret.size() || ndx == std::string::npos )
72
ret.insert( ndx, "\"" );
76
ndx = ret.find("'");
77
if( ndx >= ret.size() || ndx == std::string::npos )
80
ret.insert( ndx, "\'" );
86
std::string removeQuotes( const std::string &str )
88
std::string::size_type quoteNdx, quot, apos;
92
quot = ret.find( "\"", 0 );
93
apos = ret.find( "\'", 0 );
94
quoteNdx = quot < apos? quot : apos; // Minimum
96
if( quoteNdx == std::string::npos )
97
return ret; // There were no quotes or apostrophes, so, don't process.
101
ret.erase( 0, quoteNdx+1 );
103
quoteNdx = ret.find(c);
104
if( quoteNdx == std::string::npos )
105
return ""; // There was a beginning quote or apostrophe, but no matching one...error.
107
ret.erase( quoteNdx, ret.size() - quoteNdx );
112
Xml::Xml( istream &in )
116
while( getline( in, tmp ) )
117
//xmlTextVec.push_back(tmp);
120
//iter = xmlTextVec.begin();
122
// Transform everything to lowercase
123
// so that comparing will be easier.
124
transform( xmlText.begin(), xmlText.end(), xmlText.begin(), ToLower() );
126
iter = xmlText.begin();
130
void Xml::resetIter()
132
iter = xmlText.begin();
135
string Xml::nextTag()
137
string::const_iterator b, e;
143
while( *iter != '<' && iter != end )
149
while( *iter != '>' && iter != end )
155
// Leave the iterator on the character after
159
} while( beginsWith( ret, "!--" ) ); // This excludes comment lines.
164
string Xml::nextNonTag()
166
string::const_iterator b, e;
171
while( *e != '<' && *e != '>' )
180
string Xml::getStringVal( const string &closeTag, bool &success, const string &errMsg )
182
string ret = nextNonTag();
188
if( ! beginsWith( nextTag(), closeTag ) )
189
cerr << errMsg << endl;
194
double Xml::getDoubleVal( const string &closeTag, bool &success, const string &errMsg )
198
const char *txt = nextNonTag().c_str();
200
ret = strtod( txt, &endptr );
205
cerr << errMsg << endl;
210
if( ! beginsWith( nextTag(), closeTag ) )
211
cerr << errMsg << endl;
216
long int Xml::getLongVal( const string &closeTag, bool &success, const string &errMsg )
220
const char *txt = nextNonTag().c_str();
222
ret = strtol( txt, &endptr, 10 );
227
cerr << errMsg << endl;
232
if( ! beginsWith( nextTag(), closeTag ) )
233
cerr << errMsg << endl;
240
return (iter == end);