Commit 1d866d7f authored by Mark Olesen's avatar Mark Olesen
Browse files

reworked IOstreams

- Istream and Ostream now retain backslashes when reading/writing strings.
  The previous implementation simply discarded them, except when used to
  escape a double-quote or a newline. It is now vitally important to retain
  them, eg for quoting regular expression meta-characters.

  The backslash continues to be used as an escape character for double-quote
  and newline, but otherwise get passed through "as-is" without any other
  special meaning (ie, they are *NOT* C-style strings). This helps avoid
  'backslash hell'!
  For example,
     string:   "match real dots \.+, question mark \? or any char .*"
     C-style:  "match real dots \\.+, question mark \\? or any char .*"

- combined subfiles in db/IOstreams, some had more copyright info than code
- OPstreamI.H contained only private methods, moved into OPstream.C

Are these really correct?
   IOstreams/Istream.H:#   include "HashTable.C"
   token/token.H:#define NoHashTableC
parent cf488912
......@@ -48,11 +48,16 @@ int main(int argc, char *argv[])
Info<< string(test).expand() << endl;
string test2("~OpenFOAM/controlDict");
Info<< test2.expand() << endl;
Info<< test2 << " => " << test2.expand() << endl;
string s;
Sin.getLine(s);
Info<< s.expand() << endl;
string s2(s.expand());
cout<< "output string with " << s2.length() << " characters\n";
cout<< "ostream<< >" << s2 << "<\n";
Info<< "Ostream<< >" << s2 << "<\n";
Info << "End\n" << endl;
......
......@@ -65,21 +65,15 @@ $(Streams)/token/tokenIO.C
IOstreams = $(Streams)/IOstreams
$(IOstreams)/IOstream.C
$(IOstreams)/versionNumber.C
$(IOstreams)/Istream.C
$(IOstreams)/IOprint.C
$(IOstreams)/IOcheck.C
$(IOstreams)/Ostream.C
Sstreams = $(Streams)/Sstreams
$(Sstreams)/ISread.C
$(Sstreams)/ISnextValid.C
$(Sstreams)/ISreadToken.C
$(Sstreams)/ISstream.C
$(Sstreams)/OSstream.C
$(Sstreams)/SstreamsPrint.C
$(Sstreams)/readHexLabel.C
$(Sstreams)/OSwrite.C
$(Sstreams)/Sprint.C
$(Sstreams)/prefixOSstream/prefixOSwrite.C
$(Sstreams)/prefixOSstream/prefixOSprint.C
$(Sstreams)/prefixOSstream.C
gzstream = $(Streams)/gzstream
$(gzstream)/gzstream.C
......@@ -89,19 +83,17 @@ $(Fstreams)/IFstream.C
$(Fstreams)/OFstream.C
Tstreams = $(Streams)/Tstreams
$(Tstreams)/ITread.C
$(Tstreams)/Tprint.C
$(Tstreams)/ITstream.C
StringStreams = $(Streams)/StringStreams
$(StringStreams)/StringStreamPrint.C
$(StringStreams)/StringStreamsPrint.C
Pstreams = $(Streams)/Pstreams
$(Pstreams)/Pstream.C
$(Pstreams)/PstreamCommsStruct.C
$(Pstreams)/IPread.C
$(Pstreams)/OPwrite.C
$(Pstreams)/Pprint.C
$(Pstreams)/IPreadToken.C
$(Pstreams)/IPstream.C
$(Pstreams)/OPstream.C
$(Pstreams)/PstreamsPrint.C
dictionary = db/dictionary
$(dictionary)/dictionary.C
......@@ -335,17 +327,15 @@ $(globalMeshData)/globalIndex.C
$(polyMesh)/syncTools/syncTools.C
zones = $(polyMesh)/zones
cellZone = $(zones)/cellZone
cellZone = $(polyMesh)/zones/cellZone
$(cellZone)/cellZone.C
$(cellZone)/newCellZone.C
faceZone = $(zones)/faceZone
faceZone = $(polyMesh)/zones/faceZone
$(faceZone)/faceZone.C
$(faceZone)/newFaceZone.C
pointZone = $(zones)/pointZone
pointZone = $(polyMesh)/zones/pointZone
$(pointZone)/pointZone.C
$(pointZone)/newPointZone.C
......@@ -462,6 +452,7 @@ $(Fields)/diagTensorField/diagTensorIOField.C
$(Fields)/symmTensorField/symmTensorIOField.C
$(Fields)/tensorField/tensorIOField.C
$(Fields)/transformField/transformField.C
pointPatchFields = fields/pointPatchFields
$(pointPatchFields)/pointPatchField/pointPatchFields.C
......
......@@ -28,18 +28,16 @@ License
#include "OSspecific.H"
#include "gzstream.h"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
defineTypeNameAndDebug(IFstream, 0);
defineTypeNameAndDebug(IFstream, 0);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
:
ifPtr_(NULL),
compression_(IOstream::UNCOMPRESSED)
......@@ -48,10 +46,8 @@ IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
{
if (IFstream::debug)
{
Info<< "IFstreamAllocator::IFstreamAllocator"
"(const fileName& pathname) : "
"can't open null file "
<< endl;
Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
"cannot open null file " << endl;
}
}
......@@ -62,10 +58,8 @@ IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
{
if (IFstream::debug)
{
Info<< "IFstreamAllocator::IFstreamAllocator"
"(const fileName& pathname) : "
"decompressing " << pathname + ".gz"
<< endl;
Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
"decompressing " << pathname + ".gz" << endl;
}
delete ifPtr_;
......@@ -80,18 +74,18 @@ IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
}
IFstreamAllocator::~IFstreamAllocator()
Foam::IFstreamAllocator::~IFstreamAllocator()
{
delete ifPtr_;
}
istream& IFstreamAllocator::stdStream()
std::istream& Foam::IFstreamAllocator::stdStream()
{
if (!ifPtr_)
{
FatalErrorIn("IFstreamAllocator::stdStream()")
<< "No stream allocated." << abort(FatalError);
<< "No stream allocated" << abort(FatalError);
}
return *ifPtr_;
}
......@@ -99,7 +93,7 @@ istream& IFstreamAllocator::stdStream()
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
IFstream::IFstream
Foam::IFstream::IFstream
(
const fileName& pathname,
streamFormat format,
......@@ -120,15 +114,15 @@ IFstream::IFstream
setClosed();
setState(ifPtr_->rdstate());
if (!good())
{
if (debug)
{
Info<< "IFstream::IFstream(const fileName& pathname,"
Info<< "IFstream::IFstream(const fileName&,"
"streamFormat format=ASCII,"
"versionNumber version=currentVersion) : "
"couldn't open File for input"
"could not open file for input"
<< endl << info() << endl;
}
......@@ -138,20 +132,20 @@ IFstream::IFstream
{
setOpened();
}
lineNumber_ = 1;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
IFstream::~IFstream()
Foam::IFstream::~IFstream()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void IFstream::print(Ostream& os) const
void Foam::IFstream::print(Ostream& os) const
{
// Print File data
os << "IFstream: ";
......@@ -159,10 +153,9 @@ void IFstream::print(Ostream& os) const
}
//- Return a non-const reference to const Istream
// Needed for read-constructors where the stream argument is temporary:
// e.g. thing thisThing(IFstream("thingFileName")());
IFstream& IFstream::operator()() const
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
Foam::IFstream& Foam::IFstream::operator()() const
{
if (!good())
{
......@@ -183,8 +176,4 @@ IFstream& IFstream::operator()() const
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
......@@ -28,18 +28,17 @@ License
#include "OSspecific.H"
#include "gzstream.h"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(OFstream, 0);
}
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
defineTypeNameAndDebug(OFstream, 0);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
OFstreamAllocator::OFstreamAllocator
Foam::OFstreamAllocator::OFstreamAllocator
(
const fileName& pathname,
IOstream::compressionType compression
......@@ -51,11 +50,8 @@ OFstreamAllocator::OFstreamAllocator
{
if (OFstream::debug)
{
Info
<< "OFstreamAllocator::OFstreamAllocator"
"(const fileName& pathname) : "
"can't open null file "
<< endl;
Info<< "OFstreamAllocator::OFstreamAllocator(const fileName&) : "
"cannot open null file " << endl;
}
}
......@@ -80,13 +76,13 @@ OFstreamAllocator::OFstreamAllocator
}
OFstreamAllocator::~OFstreamAllocator()
Foam::OFstreamAllocator::~OFstreamAllocator()
{
delete ofPtr_;
}
ostream& OFstreamAllocator::stdStream()
std::ostream& Foam::OFstreamAllocator::stdStream()
{
if (!ofPtr_)
{
......@@ -99,7 +95,7 @@ ostream& OFstreamAllocator::stdStream()
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
OFstream::OFstream
Foam::OFstream::OFstream
(
const fileName& pathname,
streamFormat format,
......@@ -112,17 +108,16 @@ OFstream::OFstream
pathname_(pathname)
{
setClosed();
setState(ofPtr_->rdstate());
if (!good())
{
if (debug)
{
Info<< "IFstream::IFstream(const fileName& pathname,"
Info<< "IFstream::IFstream(const fileName&,"
"streamFormat format=ASCII,"
"versionNumber version=currentVersion) : "
"couldn't open File for input\n"
"could not open file for input\n"
"in stream " << info() << Foam::endl;
}
......@@ -132,29 +127,24 @@ OFstream::OFstream
{
setOpened();
}
lineNumber_ = 1;
}
// * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * //
OFstream::~OFstream()
Foam::OFstream::~OFstream()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void OFstream::print(Ostream& os) const
void Foam::OFstream::print(Ostream& os) const
{
// Print File data
os << " OFstream: ";
OSstream::print(os);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Description
IOstream check.
\*---------------------------------------------------------------------------*/
#include "error.H"
#include "IOstream.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
fileName IOstream::name_("IOstream");
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// check file status for given operation
bool IOstream::check(const char* operation) const
{
if (bad())
{
FatalIOErrorIn
(
"IOstream::check(const char* operation) const", *this
) << "IOstream::check(const char* operation) : "
<< "error in IOstream " << name() << " for operation "
<< operation
<< exit(FatalIOError);
}
return !bad();
}
//- Check IOstream status for given operation
// print IOstream state if error has occured and exit
void IOstream::fatalCheck(const char* operation) const
{
if (bad())
{
FatalIOErrorIn
(
"IOstream::fatalCheck(const char* operation) const", *this
) << "IOstream::check(const char* operation) : "
<< "error in IOstream " << name() << " for operation "
<< operation
<< exit(FatalIOError);
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Description
Prints out a description of the IOstream to Serr.
\*---------------------------------------------------------------------------*/
#include "IOstreams.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
void IOstream::print(Ostream& os) const
{
os << "IOstream: " << "Version " << version_ << ", format ";
switch (format_)
{
case ASCII:
os << "ASCII";
break;
case BINARY:
os << "BINARY";
break;
}
os << ", line " << lineNumber();
if (opened())
{
os << ", OPENED";
}
if (closed())
{
os << ", CLOSED";
}
if (good())
{
os << ", GOOD";
}
if (eof())
{
os << ", EOF";
}
if (fail())
{
os << ", FAIL";
}
if (bad())
{
os << ", BAD";
}
os << endl;
}
void IOstream::print(Ostream& os, const int streamState) const
{
if (streamState == ios_base::goodbit)
{
os << "ios_base::goodbit set : the last operation on stream succeeded"
<< endl;
}
else if (streamState & ios_base::badbit)
{
os << "ios_base::badbit set : characters possibly lost"
<< endl;
}
else if (streamState & ios_base::failbit)
{
os << "ios_base::failbit set : some kind of formatting error"
<< endl;
}
else if (streamState & ios_base::eofbit)
{
os << "ios_base::eofbit set : at end of stream"
<< endl;
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
......@@ -28,15 +28,17 @@ Description
#include "IOstream.H"
#include "error.H"
#include <sstream>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
Foam::fileName Foam::IOstream::name_("IOstream");
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
IOstream::streamFormat IOstream::formatEnum(const word& format)
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
Foam::IOstream::streamFormat
Foam::IOstream::formatEnum(const word& format)
{
if (format == "ascii")
{
......@@ -49,8 +51,7 @@ IOstream::streamFormat IOstream::formatEnum(const word& format)
else
{
WarningIn("IOstream::formatEnum(const word&)")
<< "bad format specifier "
<< format << " using ASCII"
<< "bad format specifier '" << format << "', using 'ascii'"
<< endl;
return IOstream::ASCII;
......@@ -58,7 +59,8 @@ IOstream::streamFormat IOstream::formatEnum(const word& format)
}
IOstream::compressionType IOstream::compressionEnum(const word& compression)
Foam::IOstream::compressionType
Foam::IOstream::compressionEnum(const word& compression)
{
if (compression == "uncompressed")
{
......@@ -71,10 +73,8 @@ IOstream::compressionType IOstream::compressionEnum(const word& compression)
else
{
WarningIn("IOstream::compressionEnum(const word&)")
<< "bad compression specifier "
<< '\'' << compression << '\''
<< ", use 'compressed' or 'uncompressed'. "
"Defaulting to uncompressed"
<< "bad compression specifier '" << compression
<< "', using 'uncompressed'"
<< endl;
return IOstream::UNCOMPRESSED;
......@@ -82,9 +82,125 @@ IOstream::compressionType IOstream::compressionEnum(const word& compression)
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::IOstream::check(const char* operation) const
{
if (bad())
{
FatalIOErrorIn
(
"IOstream::check(const char*) const", *this
) << "error in IOstream " << name() << " for operation " << operation
<< exit(FatalIOError);
}