7 #include "PvlTokenizer.h"
14 #include "IException.h"
22 PvlTokenizer::PvlTokenizer() {
27 PvlTokenizer::~PvlTokenizer() {
32 void PvlTokenizer::Clear() {
48 void PvlTokenizer::Load(std::istream &stream,
const QString &terminator) {
49 QString upTerminator(terminator.toUpper());
52 bool newlineFound =
false;
55 newlineFound = SkipWhiteSpace(stream);
61 s = ReadComment(stream);
65 if(newlineFound || tokens.size() == 0 || tokens[tokens.size()-1].valueSize() == 0) {
71 tokens.push_back(tokens[tokens.size()-1]);
72 tokens[tokens.size()-2] = t;
84 s = ReadComment(stream);
88 if(newlineFound || tokens.size() == 0 || tokens[tokens.size()-1].valueSize() == 0) {
94 tokens.push_back(tokens[tokens.size()-1]);
95 tokens[tokens.size()-2] = t;
102 s = ReadToken(stream);
110 SkipWhiteSpace(stream);
112 ValidateCharacter(c);
120 if(t.
keyUpper() == upTerminator)
return;
125 SkipWhiteSpace(stream);
128 ValidateCharacter(c);
137 s = ReadToParen(stream);
138 ParseCommaList(t, s);
142 throw IException(e, IException::Unknown, message, _FILEINFO_);
151 s = ReadToBrace(stream);
152 ParseCommaList(t, s);
156 throw IException(e, IException::Unknown, message, _FILEINFO_);
165 s = ReadToDoubleQuote(stream);
169 throw IException(e, IException::Unknown, message, _FILEINFO_);
179 s = ReadToSingleQuote(stream);
183 throw IException(IException::Unknown, message, _FILEINFO_);
191 s = ReadToken(stream);
205 QString PvlTokenizer::ReadComment(std::istream &stream) {
210 while((c !=
'\r') && (c !=
'\n') && (c !=
'\0')) {
213 ValidateCharacter(c);
214 if(c == EOF)
return s;
233 QString PvlTokenizer::ReadToken(std::istream &stream) {
238 while((!isspace(c)) && (c !=
'\0') && (c !=
'=')) {
241 ValidateCharacter(c);
242 if(c == EOF)
return s;
257 bool PvlTokenizer::SkipWhiteSpace(std::istream &stream) {
258 bool foundNewline =
false;
262 ValidateCharacter(c);
263 while((isspace(c)) || (c ==
'\0')) {
270 ValidateCharacter(c);
277 QString PvlTokenizer::ReadToDoubleQuote(std::istream &stream) {
283 ValidateCharacter(c);
286 throw IException(IException::Unknown, message, _FILEINFO_);
294 int pos = s.indexOf(QRegExp(
"[\\n\\r]"));
296 QString first = s.mid(0, pos);
297 bool addspace =
false;
298 if(first[pos-1] ==
' ') addspace =
true;
299 first = first.remove(QRegExp(
"[\\s]*$"));
300 QString second = s.mid(pos + 1);
301 if(second[0] ==
' ') addspace =
true;
302 if(second[0] ==
'\r') addspace =
true;
303 if(second[0] ==
'\n') addspace =
true;
304 second = second.remove(QRegExp(
"^[\\s]*"));
305 if(second[0] ==
',') addspace =
false;
307 if(addspace) s +=
" ";
310 pos = s.indexOf(QRegExp(
"[\\n\\r]"));
315 QString PvlTokenizer::ReadToSingleQuote(std::istream &stream) {
321 ValidateCharacter(c);
324 throw IException(IException::Unknown, message, _FILEINFO_);
332 int pos = s.indexOf(QRegExp(
"[\\n\\r]"));
334 QString first = s.mid(0, pos);
335 bool addspace =
false;
336 if(first[pos-1] ==
' ') addspace =
true;
337 first = first.remove(QRegExp(
"[\\s]*$"));
338 QString second = s.mid(pos + 1);
339 if(second[0] ==
' ') addspace =
true;
340 if(second[0] ==
'\r') addspace =
true;
341 if(second[0] ==
'\n') addspace =
true;
342 second = second.remove(QRegExp(
"^[\\s]*"));
343 if(second[0] ==
',') addspace =
false;
345 if(addspace) s +=
" ";
347 pos = s.indexOf(QRegExp(
"[\\n\\r]"));
353 QString PvlTokenizer::ReadToParen(std::istream &stream) {
356 int leftParenCount = 1;
360 ValidateCharacter(c);
363 throw IException(IException::Unknown, message, _FILEINFO_);
367 s +=
"\"" + ReadToDoubleQuote(stream) +
"\"";
369 catch(IException &) {
371 throw IException(IException::Unknown, message, _FILEINFO_);
376 s +=
"'" + ReadToSingleQuote(stream) +
"'";
378 catch(IException &) {
380 throw IException(IException::Unknown, message, _FILEINFO_);
385 if(leftParenCount > 0) s += (char) c;
389 if(c ==
'(') leftParenCount++;
392 while(leftParenCount > 0);
397 QString PvlTokenizer::ReadToBrace(std::istream &stream) {
400 int leftBraceCount = 1;
404 ValidateCharacter(c);
407 throw IException(IException::Unknown, message, _FILEINFO_);
411 s +=
"\"" + ReadToDoubleQuote(stream) +
"\"";
413 catch(IException &e) {
415 throw IException(IException::Unknown, message, _FILEINFO_);
420 s +=
"'" + ReadToSingleQuote(stream) +
"'";
422 catch(IException &) {
424 throw IException(IException::Unknown, message, _FILEINFO_);
429 if(leftBraceCount > 0) s += (char) c;
433 if(c ==
'{') leftBraceCount++;
436 while(leftBraceCount > 0);
450 stringstream stream(cl.toLatin1().data());
455 SkipWhiteSpace(stream);
458 s += ReadToDoubleQuote(stream);
461 s += ReadToSingleQuote(stream);
465 s += ReadToParen(stream);
470 s += ReadToBrace(stream);
487 vector<Isis::PvlToken> & PvlTokenizer::GetTokenList() {
496 void PvlTokenizer::ValidateCharacter(
int c) {
498 if(isprint(c))
return;
499 if(isspace(c))
return;
500 if(c ==
'\0')
return;
502 QString message =
"ASCII data expected but found unprintable (binary) data";
503 throw IException(IException::Unknown, message, _FILEINFO_);