openfoam/applications/test/fstreamPointer/Test-fstreamPointer.C
Mark Olesen 6e2b7be983 ENH: direct access to wrapped ifstream/ofstream with compression (#1805)
- previously hidden as Detail::[IO]FstreamAllocator, now exposed
  directly as [io]fstreamPointer, which allows reuse for
  std::ifstream, std::ofstream wrapping, without the additional
  ISstream, OSstream layers.

  These stream pointers have some characteristics similar to a
  unique_ptr.

- restrict direct gzstream usage to two files (fstreamPointers.C,
  gzstream.C) which improves localization and makes it simpler to
  enable/disable with the `HAVE_LIBZ` define.

  The HAVE_LIBZ define is currently simply hard-coded in the
  Make/options.

  If compiled WITHOUT libz support:
    - reading gz files : FatalError
    - writing gz files : emit warning and downgrade to uncompressed
    - warn if compression is specified in the case controlDict
      and downgrade to uncompressed

ENH: minor updates to gzstream interface for C++11

- support construct/open with std::string for the file names.

CONFIG: provisioning for have_libz detection as wmake/script
2020-08-10 12:40:08 +02:00

115 lines
2.7 KiB
C

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
Application
Test-fstreamPointer
Description
Low-level fstream tests
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "refPtr.H"
#include "fstreamPointer.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noBanner();
argList::noParallel();
argList::addOption
(
"output",
"file",
"Output file name for cat"
);
argList::addArgument("file1");
argList::addArgument("...");
argList::addArgument("fileN");
argList::noMandatoryArgs();
#include "setRootCase.H"
if (args.size() <= 1)
{
InfoErr<< "\nNo input files specified .. stopping\n" << endl;
return 0;
}
refPtr<std::ostream> osRef;
fileName outputName;
if (args.readIfPresent("output", outputName))
{
InfoErr<< "output: " << outputName;
IOstreamOption::compressionType comp(IOstreamOption::UNCOMPRESSED);
if (outputName.hasExt("gz"))
{
comp = IOstreamOption::COMPRESSED;
outputName.removeExt();
InfoErr<< " [compress]";
}
InfoErr<< nl;
osRef.reset(ofstreamPointer(outputName, comp).release());
}
else
{
osRef.ref(std::cout);
InfoErr<< "output: stdout" << nl;
}
auto& os = osRef.ref();
for (label argi = 1; argi < args.size(); ++argi)
{
const fileName inputName(args[argi]);
InfoErr<< "input: " << inputName;
ifstreamPointer isPtr(inputName);
if (!isPtr.get() || !isPtr->good())
{
InfoErr<< " (not good)" << nl;
continue;
}
InfoErr<< nl;
auto& is = *isPtr;
// Loop getting single characters
// - not efficient, but that is not the test here anyhow
char c;
while (is.get(c))
{
os << c;
}
}
InfoErr<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //