diff --git a/src/OpenFOAM/db/IOobject/IOobject.H b/src/OpenFOAM/db/IOobject/IOobject.H index f71badbcca..999b48551d 100644 --- a/src/OpenFOAM/db/IOobject/IOobject.H +++ b/src/OpenFOAM/db/IOobject/IOobject.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -374,6 +374,9 @@ public: //- Write header bool writeHeader(Ostream&) const; + //- Write header. Allow override of type + bool writeHeader(Ostream&, const word& objectType) const; + // Error Handling diff --git a/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C b/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C index 88a25abcbb..638d3e2095 100644 --- a/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C +++ b/src/OpenFOAM/db/IOobject/IOobjectWriteHeader.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -31,7 +31,7 @@ Description // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -bool Foam::IOobject::writeHeader(Ostream& os) const +bool Foam::IOobject::writeHeader(Ostream& os, const word& type) const { if (!os.good()) { @@ -46,7 +46,7 @@ bool Foam::IOobject::writeHeader(Ostream& os) const << "FoamFile\n{\n" << " version " << os.version() << ";\n" << " format " << os.format() << ";\n" - << " class " << type() << ";\n"; + << " class " << type << ";\n"; if (note().size()) { @@ -63,4 +63,10 @@ bool Foam::IOobject::writeHeader(Ostream& os) const } +bool Foam::IOobject::writeHeader(Ostream& os) const +{ + return writeHeader(os, type()); +} + + // ************************************************************************* // diff --git a/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormat.C b/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormat.C index aa9028e463..1ba560f824 100644 --- a/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormat.C +++ b/src/edgeMesh/edgeFormats/emesh/EMESHedgeFormat.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -27,6 +27,8 @@ License #include "IOobject.H" #include "IFstream.H" #include "clock.H" +#include "Time.H" +#include "featureEdgeMesh.H" // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -48,18 +50,53 @@ bool Foam::fileFormats::EMESHedgeFormat::read { clear(); - IFstream is(filename); - if (!is.good()) + fileName dir = filename.path(); + fileName caseName = dir.name(); + fileName rootPath = dir.path(); + + // Construct dummy time to use as an objectRegistry + Time dummyTime + ( + ".", //rootPath, + ".", //caseName, + "system", //systemName, + "constant", //constantName, + false //enableFunctionObjects + ); + + // Construct IOobject to re-use the headerOk & readHeader + // (so we can read ascii and binary) + IOobject io + ( + filename, + dummyTime, + IOobject::NO_READ, + IOobject::NO_WRITE, + false + ); + + if (!io.headerOk()) { - FatalErrorIn - ( - "fileFormats::EMESHedgeFormat::read(const fileName&)" - ) + FatalErrorIn("fileFormats::EMESHedgeFormat::read(const fileName&)") << "Cannot read file " << filename << exit(FatalError); } - return read(is, this->storedPoints(), this->storedEdges()); + + autoPtr isPtr(new IFstream(io.filePath())); + bool ok = false; + if (isPtr().good()) + { + Istream& is = isPtr(); + ok = io.readHeader(is); + + if (ok) + { + ok = read(is, this->storedPoints(), this->storedEdges()); + } + } + + return ok; } @@ -81,32 +118,6 @@ bool Foam::fileFormats::EMESHedgeFormat::read << exit(FatalError); } - token firstToken(is); - - // swallow IOobject header - if (!is.good()) - { - FatalIOErrorIn - ( - "fileFormats::EMESHedgeFormat::read" - "(Istream&, pointField&, edgeList&)", - is - ) - << "First token could not be read" << nl - << exit(FatalIOError); - - return false; - } - else if (firstToken.isWord() && firstToken.wordToken() == "FoamFile") - { - // read and discard - dictionary headerDict(is); - } - else - { - is.putBack(firstToken); - } - // read points: is >> pointLst; @@ -157,30 +168,54 @@ void Foam::fileFormats::EMESHedgeFormat::write const edgeMesh& mesh ) { - OFstream os(filename); - if (!os.good()) + // Construct dummy time to use as an objectRegistry + Time dummyTime + ( + ".", //rootPath, + ".", //caseName, + "system", //systemName, + "constant", //constantName, + false //enableFunctionObjects + ); + + // Construct IOobject to re-use the writeHeader + IOobject io + ( + filename, + dummyTime, + IOobject::NO_READ, + IOobject::NO_WRITE, + false + ); + io.note() = "written " + clock::dateTime(); + + // Note: always write ascii + autoPtr osPtr(new OFstream(filename)); + + if (!osPtr().good()) { - FatalErrorIn + FatalIOErrorIn ( "fileFormats::EMESHedgeFormat::write" - "(const fileName&, const edgeMesh&)" - ) - << "Cannot open file for writing " << filename - << exit(FatalError); + "(const fileName&, const edgeMesh&)", + osPtr() + ) << "Cannot open file for writing " << filename + << exit(FatalIOError); } + OFstream& os = osPtr(); + bool ok = io.writeHeader(os, featureEdgeMesh::typeName); - // just emit some information until we get a nicer IOobject - IOobject::writeBanner(os) - << "FoamFile\n{\n" - << " version " << os.version() << ";\n" - << " format " << os.format() << ";\n" - << " class " << "featureEdgeMesh" << ";\n" - << " note " << "written " + clock::dateTime() << ";\n" - << " object " << filename.name() << ";\n" - << "}" << nl; - - IOobject::writeDivider(os); + if (!ok) + { + FatalIOErrorIn + ( + "fileFormats::EMESHedgeFormat::write" + "(const fileName&, const edgeMesh&)", + os + ) << "Cannot write header" + << exit(FatalIOError); + } write(os, mesh.points(), mesh.edges());