From 6d0c6483eb8054b18da08199e7e234077ea7b142 Mon Sep 17 00:00:00 2001 From: mattijs Date: Fri, 18 Feb 2011 18:07:02 +0000 Subject: [PATCH] ENH: ISstream: have #{ #} delimiters for verbatim strings --- src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C | 132 +++++++++++++++--- src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H | 8 +- src/OpenFOAM/db/IOstreams/token/token.H | 3 +- 3 files changed, 124 insertions(+), 19 deletions(-) diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C index b35ad6d026..6bbb130a49 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -27,6 +27,7 @@ License #include "int.H" #include "token.H" #include +#include "IOstreams.H" // * * * * * * * * * * * * * * 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) { static const int maxLen = 128; @@ -181,7 +203,44 @@ Foam::Istream& Foam::ISstream::read(token& t) 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 // @@ -302,22 +361,7 @@ Foam::Istream& Foam::ISstream::read(token& t) default: { putback(c); - 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; - } + readWordToken(t); 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) { is_ >> val; diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H index 22d17e6214..664d1ee531 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H +++ b/src/OpenFOAM/db/IOstreams/Sstreams/ISstream.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -64,9 +64,15 @@ class ISstream char nextValid(); + void readWordToken(token&); // Private Member Functions + + //- Read a verbatim string (excluding block delimiters). + Istream& readVerbatim(string&); + + //- Disallow default bitwise assignment void operator=(const ISstream&); diff --git a/src/OpenFOAM/db/IOstreams/token/token.H b/src/OpenFOAM/db/IOstreams/token/token.H index 2c9eb97c6b..7c91758c1a 100644 --- a/src/OpenFOAM/db/IOstreams/token/token.H +++ b/src/OpenFOAM/db/IOstreams/token/token.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -104,6 +104,7 @@ public: END_BLOCK = '}', COLON = ':', COMMA = ',', + HASH = '#', BEGIN_STRING = '"', END_STRING = BEGIN_STRING,