ENH: ISstream: have #{ #} delimiters for verbatim strings

This commit is contained in:
mattijs
2011-02-18 18:07:02 +00:00
parent 93f408d584
commit 6d0c6483eb
3 changed files with 124 additions and 19 deletions

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -27,6 +27,7 @@ License
#include "int.H" #include "int.H"
#include "token.H" #include "token.H"
#include <cctype> #include <cctype>
#include "IOstreams.H"
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
@ -109,6 +110,27 @@ char Foam::ISstream::nextValid()
} }
void Foam::ISstream::readWordToken(token& t)
{
word* wPtr = new word;
if (read(*wPtr).bad())
{
delete wPtr;
t.setBad();
}
else if (token::compound::isCompound(*wPtr))
{
t = token::compound::New(*wPtr, *this).ptr();
delete wPtr;
}
else
{
t = wPtr;
}
}
Foam::Istream& Foam::ISstream::read(token& t) Foam::Istream& Foam::ISstream::read(token& t)
{ {
static const int maxLen = 128; static const int maxLen = 128;
@ -181,7 +203,44 @@ Foam::Istream& Foam::ISstream::read(token& t)
return *this; return *this;
} }
// Verbatim string
case token::HASH :
{
char nextC;
if (read(nextC).bad())
{
// Return hash as word
t = token(word(c));
return *this;
}
else if (nextC == token::BEGIN_BLOCK)
{
// Verbatim string
string* sPtr = new string;
if (readVerbatim(*sPtr).bad())
{
delete sPtr;
t.setBad();
}
else
{
t = sPtr;
}
return *this;
}
else
{
// Word beginning with #
putback(nextC);
putback(c);
readWordToken(t);
return *this;
}
}
// Number: integer or floating point // Number: integer or floating point
// //
@ -302,22 +361,7 @@ Foam::Istream& Foam::ISstream::read(token& t)
default: default:
{ {
putback(c); putback(c);
word* wPtr = new word; readWordToken(t);
if (read(*wPtr).bad())
{
delete wPtr;
t.setBad();
}
else if (token::compound::isCompound(*wPtr))
{
t = token::compound::New(*wPtr, *this).ptr();
delete wPtr;
}
else
{
t = wPtr;
}
return *this; return *this;
} }
@ -504,6 +548,60 @@ Foam::Istream& Foam::ISstream::read(string& str)
} }
Foam::Istream& Foam::ISstream::readVerbatim(string& str)
{
static const int maxLen = 1024;
static const int errLen = 80; // truncate error message for readability
static char buf[maxLen];
char c;
register int nChar = 0;
while (get(c))
{
if (c == token::HASH)
{
char nextC;
get(nextC);
if (nextC == token::END_BLOCK)
{
buf[nChar] = '\0';
str = buf;
return *this;
}
else
{
putback(nextC);
}
}
buf[nChar++] = c;
if (nChar == maxLen)
{
buf[errLen] = '\0';
FatalIOErrorIn("ISstream::readVerbatim(string&)", *this)
<< "string \"" << buf << "...\"\n"
<< " is too long (max. " << maxLen << " characters)"
<< exit(FatalIOError);
return *this;
}
}
// don't worry about a dangling backslash if string terminated prematurely
buf[errLen] = buf[nChar] = '\0';
FatalIOErrorIn("ISstream::readVerbatim(string&)", *this)
<< "problem while reading string \"" << buf << "...\""
<< exit(FatalIOError);
return *this;
}
Foam::Istream& Foam::ISstream::read(label& val) Foam::Istream& Foam::ISstream::read(label& val)
{ {
is_ >> val; is_ >> val;

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -64,9 +64,15 @@ class ISstream
char nextValid(); char nextValid();
void readWordToken(token&);
// Private Member Functions // Private Member Functions
//- Read a verbatim string (excluding block delimiters).
Istream& readVerbatim(string&);
//- Disallow default bitwise assignment //- Disallow default bitwise assignment
void operator=(const ISstream&); void operator=(const ISstream&);

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -104,6 +104,7 @@ public:
END_BLOCK = '}', END_BLOCK = '}',
COLON = ':', COLON = ':',
COMMA = ',', COMMA = ',',
HASH = '#',
BEGIN_STRING = '"', BEGIN_STRING = '"',
END_STRING = BEGIN_STRING, END_STRING = BEGIN_STRING,