37 PvlTokenizer::PvlTokenizer() {
42 PvlTokenizer::~PvlTokenizer() {
47 void PvlTokenizer::Clear() {
63 void PvlTokenizer::Load(std::istream &stream,
const QString &terminator) {
64 QString upTerminator(terminator.toUpper());
67 bool newlineFound =
false;
70 newlineFound = SkipWhiteSpace(stream);
76 s = ReadComment(stream);
80 if(newlineFound || tokens.size() == 0 || tokens[tokens.size()-1].valueSize() == 0) {
86 tokens.push_back(tokens[tokens.size()-1]);
87 tokens[tokens.size()-2] = t;
99 s = ReadComment(stream);
103 if(newlineFound || tokens.size() == 0 || tokens[tokens.size()-1].valueSize() == 0) {
109 tokens.push_back(tokens[tokens.size()-1]);
110 tokens[tokens.size()-2] = t;
117 s = ReadToken(stream);
125 SkipWhiteSpace(stream);
127 ValidateCharacter(c);
135 if(t.
keyUpper() == upTerminator)
return;
140 SkipWhiteSpace(stream);
143 ValidateCharacter(c);
152 s = ReadToParen(stream);
153 ParseCommaList(t, s);
166 s = ReadToBrace(stream);
167 ParseCommaList(t, s);
180 s = ReadToDoubleQuote(stream);
194 s = ReadToSingleQuote(stream);
206 s = ReadToken(stream);
220 QString PvlTokenizer::ReadComment(std::istream &stream) {
225 while((c !=
'\r') && (c !=
'\n') && (c !=
'\0')) {
228 ValidateCharacter(c);
229 if(c == EOF)
return s;
248 QString PvlTokenizer::ReadToken(std::istream &stream) {
253 while((!isspace(c)) && (c !=
'\0') && (c !=
'=')) {
256 ValidateCharacter(c);
257 if(c == EOF)
return s;
272 bool PvlTokenizer::SkipWhiteSpace(std::istream &stream) {
273 bool foundNewline =
false;
277 ValidateCharacter(c);
278 while((isspace(c)) || (c ==
'\0')) {
285 ValidateCharacter(c);
292 QString PvlTokenizer::ReadToDoubleQuote(std::istream &stream) {
298 ValidateCharacter(c);
309 int pos = s.indexOf(QRegExp(
"[\\n\\r]"));
311 QString first = s.mid(0, pos);
312 bool addspace =
false;
313 if(first[pos-1] ==
' ') addspace =
true;
314 first = first.remove(QRegExp(
"[\\s]*$"));
315 QString second = s.mid(pos + 1);
316 if(second[0] ==
' ') addspace =
true;
317 if(second[0] ==
'\r') addspace =
true;
318 if(second[0] ==
'\n') addspace =
true;
319 second = second.remove(QRegExp(
"^[\\s]*"));
320 if(second[0] ==
',') addspace =
false;
322 if(addspace) s +=
" ";
325 pos = s.indexOf(QRegExp(
"[\\n\\r]"));
330 QString PvlTokenizer::ReadToSingleQuote(std::istream &stream) {
336 ValidateCharacter(c);
339 throw IException(IException::Unknown, message,
_FILEINFO_);
347 int pos = s.indexOf(QRegExp(
"[\\n\\r]"));
349 QString first = s.mid(0, pos);
350 bool addspace =
false;
351 if(first[pos-1] ==
' ') addspace =
true;
352 first = first.remove(QRegExp(
"[\\s]*$"));
353 QString second = s.mid(pos + 1);
354 if(second[0] ==
' ') addspace =
true;
355 if(second[0] ==
'\r') addspace =
true;
356 if(second[0] ==
'\n') addspace =
true;
357 second = second.remove(QRegExp(
"^[\\s]*"));
358 if(second[0] ==
',') addspace =
false;
360 if(addspace) s +=
" ";
362 pos = s.indexOf(QRegExp(
"[\\n\\r]"));
368 QString PvlTokenizer::ReadToParen(std::istream &stream) {
371 int leftParenCount = 1;
375 ValidateCharacter(c);
378 throw IException(IException::Unknown, message,
_FILEINFO_);
382 s +=
"\"" + ReadToDoubleQuote(stream) +
"\"";
384 catch(IException &) {
386 throw IException(IException::Unknown, message,
_FILEINFO_);
391 s +=
"'" + ReadToSingleQuote(stream) +
"'";
393 catch(IException &) {
395 throw IException(IException::Unknown, message,
_FILEINFO_);
400 if(leftParenCount > 0) s += (char) c;
404 if(c ==
'(') leftParenCount++;
407 while(leftParenCount > 0);
412 QString PvlTokenizer::ReadToBrace(std::istream &stream) {
415 int leftBraceCount = 1;
419 ValidateCharacter(c);
422 throw IException(IException::Unknown, message,
_FILEINFO_);
426 s +=
"\"" + ReadToDoubleQuote(stream) +
"\"";
428 catch(IException &e) {
430 throw IException(IException::Unknown, message,
_FILEINFO_);
435 s +=
"'" + ReadToSingleQuote(stream) +
"'";
437 catch(IException &) {
439 throw IException(IException::Unknown, message,
_FILEINFO_);
444 if(leftBraceCount > 0) s += (char) c;
448 if(c ==
'{') leftBraceCount++;
451 while(leftBraceCount > 0);
465 stringstream stream(cl.toLatin1().data());
470 SkipWhiteSpace(stream);
473 s += ReadToDoubleQuote(stream);
476 s += ReadToSingleQuote(stream);
480 s += ReadToParen(stream);
485 s += ReadToBrace(stream);
502 vector<Isis::PvlToken> & PvlTokenizer::GetTokenList() {
511 void PvlTokenizer::ValidateCharacter(
int c) {
513 if(isprint(c))
return;
514 if(isspace(c))
return;
515 if(c ==
'\0')
return;
517 QString message =
"ASCII data expected but found unprintable (binary) data";
QString KeywordValueBad(const QString &key)
This error should be used when a supplied keyword does not appear in the list (e.g., an Isis cube label).
Namespace for the standard library.
void addValue(const QString &v)
Adds a value to the value-vector.
QString key() const
Returns the token keyword.
Container for Keyword-value pair.
#define _FILEINFO_
Macro for the filename and line number.
Namespace for ISIS/Bullet specific routines.
QString keyUpper() const
Returns the token keyword in all uppercase characters.
QString MissingDelimiter(const char delimiter)
This error should be used when a delimiter is missing.