From 27e64adb38a9a8e6370b5dbd38a9018313884a9c Mon Sep 17 00:00:00 2001 From: mkrylova Date: Mon, 26 Jul 2021 11:09:57 +0300 Subject: [PATCH] 0032490: Data Exchange - provide OSD_FileSystem::OpenOStream() for output streams - provided OSD_FileSystem::OpenOStream() for output streams - replaced OSD_OpenStream() usage with OSD_FileSystem::DefaultFileSystem() --- src/BRepTools/BRepTools.cxx | 27 +-- .../BinLDrivers_DocumentStorageDriver.cxx | 10 +- src/BinTools/BinTools.cxx | 15 +- src/DDocStd/DDocStd_ApplicationCommands.cxx | 7 +- src/Draw/Draw_VariableCommands.cxx | 18 +- src/IGESControl/IGESControl_Writer.cxx | 18 +- src/IGESSelect/IGESSelect_WorkLibrary.cxx | 16 +- src/Image/Image_Texture.cxx | 14 +- src/OSD/OSD_CachedFileSystem.cxx | 23 +++ src/OSD/OSD_CachedFileSystem.hxx | 7 + src/OSD/OSD_FileSystem.cxx | 18 ++ src/OSD/OSD_FileSystem.hxx | 11 ++ src/OSD/OSD_FileSystemSelector.cxx | 46 +++++ src/OSD/OSD_FileSystemSelector.hxx | 7 + src/OSD/OSD_LocalFileSystem.cxx | 15 ++ src/OSD/OSD_LocalFileSystem.hxx | 3 + src/RWGltf/RWGltf_CafWriter.cxx | 98 +++++----- src/StepSelect/StepSelect_WorkLibrary.cxx | 15 +- src/TObjDRAW/TObjDRAW.cxx | 7 +- src/ViewerTest/ViewerTest_OpenGlCommands.cxx | 178 +++++++++--------- src/VrmlAPI/VrmlAPI_Writer.cxx | 76 ++++---- .../XmlLDrivers_DocumentStorageDriver.cxx | 10 +- 22 files changed, 387 insertions(+), 252 deletions(-) diff --git a/src/BRepTools/BRepTools.cxx b/src/BRepTools/BRepTools.cxx index 156ca3fb21..8f0c3b3094 100644 --- a/src/BRepTools/BRepTools.cxx +++ b/src/BRepTools/BRepTools.cxx @@ -38,7 +38,6 @@ #include #include #include -#include #include #include #include @@ -707,12 +706,14 @@ Standard_Boolean BRepTools::Write (const TopoDS_Shape& theShape, const TopTools_FormatVersion theVersion, const Message_ProgressRange& theProgress) { - std::ofstream os; - OSD_OpenStream(os, theFile, std::ios::out); - if (!os.is_open() || !os.good()) + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aStream = aFileSystem->OpenOStream (theFile, std::ios::out); + if (aStream.get() == NULL || !aStream->good()) + { return Standard_False; + } - Standard_Boolean isGood = (os.good() && !os.eof()); + Standard_Boolean isGood = (aStream->good() && !aStream->eof()); if(!isGood) return isGood; @@ -720,19 +721,19 @@ Standard_Boolean BRepTools::Write (const TopoDS_Shape& theShape, SS.SetFormatNb (theVersion); SS.Add (theShape); - os << "DBRep_DrawableShape\n"; // for easy Draw read - SS.Write(os, theProgress); - isGood = os.good(); + *aStream << "DBRep_DrawableShape\n"; // for easy Draw read + SS.Write (*aStream, theProgress); + isGood = aStream->good(); if (isGood) { - SS.Write (theShape, os); + SS.Write (theShape, *aStream); } - os.flush(); - isGood = os.good(); + aStream->flush(); + isGood = aStream->good(); errno = 0; - os.close(); - isGood = os.good() && isGood && !errno; + isGood = aStream->good() && isGood && !errno; + aStream.reset(); return isGood; } diff --git a/src/BinLDrivers/BinLDrivers_DocumentStorageDriver.cxx b/src/BinLDrivers/BinLDrivers_DocumentStorageDriver.cxx index 39ad9b002e..2dd4bacd8b 100644 --- a/src/BinLDrivers/BinLDrivers_DocumentStorageDriver.cxx +++ b/src/BinLDrivers/BinLDrivers_DocumentStorageDriver.cxx @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include @@ -74,12 +74,12 @@ void BinLDrivers_DocumentStorageDriver::Write myFileName = theFileName; - std::ofstream aFileStream; - OSD_OpenStream (aFileStream, theFileName, std::ios::out | std::ios::binary); + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aFileStream = aFileSystem->OpenOStream (theFileName, std::ios::out | std::ios::binary); - if (aFileStream.is_open() && aFileStream.good()) + if (aFileStream.get() != NULL && aFileStream->good()) { - Write(theDocument, aFileStream, theRange); + Write (theDocument, *aFileStream, theRange); } else { diff --git a/src/BinTools/BinTools.cxx b/src/BinTools/BinTools.cxx index 9bffcea881..686a0c600d 100644 --- a/src/BinTools/BinTools.cxx +++ b/src/BinTools/BinTools.cxx @@ -18,7 +18,6 @@ #include #include #include -#include #include //======================================================================= @@ -215,15 +214,15 @@ Standard_Boolean BinTools::Write (const TopoDS_Shape& theShape, const BinTools_FormatVersion theVersion, const Message_ProgressRange& theRange) { - std::ofstream aStream; - aStream.precision (15); - OSD_OpenStream (aStream, theFile, std::ios::out | std::ios::binary); - if (!aStream.good()) + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aStream = aFileSystem->OpenOStream (theFile, std::ios::out | std::ios::binary); + aStream->precision (15); + if (aStream.get() == NULL || !aStream->good()) return Standard_False; - Write (theShape, aStream, theWithTriangles, theWithNormals, theVersion, theRange); - aStream.close(); - return aStream.good(); + Write (theShape, *aStream, theWithTriangles, theWithNormals, theVersion, theRange); + aStream->flush(); + return aStream->good(); } //======================================================================= diff --git a/src/DDocStd/DDocStd_ApplicationCommands.cxx b/src/DDocStd/DDocStd_ApplicationCommands.cxx index a035d99562..f0c16d38b3 100644 --- a/src/DDocStd/DDocStd_ApplicationCommands.cxx +++ b/src/DDocStd/DDocStd_ApplicationCommands.cxx @@ -35,7 +35,6 @@ #include #include -#include #include #include @@ -298,9 +297,9 @@ static Standard_Integer DDocStd_SaveAs (Draw_Interpretor& di, Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator(di, 1); if (anUseStream) { - std::ofstream aFileStream; - OSD_OpenStream (aFileStream, path, std::ios::out | std::ios::binary); - theStatus = A->SaveAs (D, aFileStream, aProgress->Start()); + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aFileStream = aFileSystem->OpenOStream (path, std::ios::out | std::ios::binary); + theStatus = A->SaveAs (D, *aFileStream, aProgress->Start()); } else { diff --git a/src/Draw/Draw_VariableCommands.cxx b/src/Draw/Draw_VariableCommands.cxx index bb502d777e..7368c92ad5 100644 --- a/src/Draw/Draw_VariableCommands.cxx +++ b/src/Draw/Draw_VariableCommands.cxx @@ -91,10 +91,10 @@ static Standard_Integer save (Draw_Interpretor& theDI, } const char* aName = theArgVec[2]; - std::ofstream aStream; - aStream.precision (15); - OSD_OpenStream (aStream, aName, std::ios::out); - if (!aStream.is_open() || !aStream.good()) + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aStream = aFileSystem->OpenOStream (aName, std::ios::out); + aStream->precision (15); + if (aStream.get() == NULL || !aStream->good()) { theDI << "Error: cannot open file for writing " << aName; return 1; @@ -104,21 +104,21 @@ static Standard_Integer save (Draw_Interpretor& theDI, { Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (theDI, 1); Standard_CString aToolTypeName = aDrawable->DynamicType()->Name(); - aStream << aToolTypeName << "\n"; + *aStream << aToolTypeName << "\n"; Draw::SetProgressBar (aProgress); - aDrawable->Save (aStream); + aDrawable->Save (*aStream); } catch (const Standard_NotImplemented& ) { theDI << "Error: no method for saving " << theArgVec[1]; return 1; } - aStream << "\n"; - aStream << "0\n\n"; + *aStream << "\n"; + *aStream << "0\n\n"; Draw::SetProgressBar (Handle(Draw_ProgressIndicator)()); errno = 0; - const Standard_Boolean aRes = aStream.good() && !errno; + const Standard_Boolean aRes = aStream->good() && !errno; if (!aRes) { theDI << "Error: file has not been written"; diff --git a/src/IGESControl/IGESControl_Writer.cxx b/src/IGESControl/IGESControl_Writer.cxx index 3d4913a607..565c1c216b 100644 --- a/src/IGESControl/IGESControl_Writer.cxx +++ b/src/IGESControl/IGESControl_Writer.cxx @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include #include @@ -253,17 +253,21 @@ Standard_Boolean IGESControl_Writer::Write Standard_Boolean IGESControl_Writer::Write (const Standard_CString file, const Standard_Boolean fnes) { - std::ofstream fout; - OSD_OpenStream(fout,file,std::ios::out); - if (!fout) return Standard_False; + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aStream = aFileSystem->OpenOStream (file, std::ios::out); + if (aStream.get() == NULL) + { + return Standard_False; + } #ifdef OCCT_DEBUG std::cout<<" Ecriture fichier ("<< (fnes ? "fnes" : "IGES") <<"): "<flush(); + res = aStream->good() && res && !errno; + aStream.reset(); return res; } diff --git a/src/IGESSelect/IGESSelect_WorkLibrary.cxx b/src/IGESSelect/IGESSelect_WorkLibrary.cxx index a2952aa481..ddb43057b0 100644 --- a/src/IGESSelect/IGESSelect_WorkLibrary.cxx +++ b/src/IGESSelect/IGESSelect_WorkLibrary.cxx @@ -36,7 +36,7 @@ #include #include #include -#include +#include #include #include #include @@ -101,9 +101,10 @@ static Handle(IGESData_FileProtocol) IGESProto; DeclareAndCast(IGESData_Protocol,prot,ctx.Protocol()); if (igesmod.IsNull() || prot.IsNull()) return Standard_False; - std::ofstream fout; - OSD_OpenStream(fout,ctx.FileName(),std::ios::out ); - if (!fout) { + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aStream = aFileSystem->OpenOStream (ctx.FileName(), std::ios::out); + if (aStream.get() == NULL) + { ctx.CCheck(0)->AddFail("IGES File could not be created"); sout<<" - IGES File could not be created : " << ctx.FileName() << std::endl; return 0; } @@ -128,11 +129,12 @@ static Handle(IGESData_FileProtocol) IGESProto; VW.SendModel(prot); sout<<" Write "; if (themodefnes) VW.WriteMode() = 10; - Standard_Boolean status = VW.Print(fout); sout<<" Done"<flush(); + status = aStream->good() && status && !errno; + aStream.reset(); if(errno) sout << strerror(errno) << std::endl; diff --git a/src/Image/Image_Texture.cxx b/src/Image/Image_Texture.cxx index df635ce381..9892b63e76 100644 --- a/src/Image/Image_Texture.cxx +++ b/src/Image/Image_Texture.cxx @@ -20,7 +20,6 @@ #include #include #include -#include IMPLEMENT_STANDARD_RTTIEXT(Image_Texture, Standard_Transient) @@ -316,25 +315,26 @@ TCollection_AsciiString Image_Texture::ProbeImageFileFormat() const // ================================================================ Standard_Boolean Image_Texture::WriteImage (const TCollection_AsciiString& theFile) { - std::ofstream aFileOut; - OSD_OpenStream (aFileOut, theFile.ToCString(), std::ios::out | std::ios::binary | std::ios::trunc); - if (!aFileOut) + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aFileOut = aFileSystem->OpenOStream (theFile, std::ios::out | std::ios::binary | std::ios::trunc); + if (aFileOut.get() == NULL) { Message::SendFail (TCollection_AsciiString ("Error: Unable to create file '") + theFile + "'"); return false; } - if (!WriteImage (aFileOut, theFile)) + if (!WriteImage (*aFileOut, theFile)) { return false; } - aFileOut.close(); - if (!aFileOut.good()) + aFileOut->flush(); + if (!aFileOut->good()) { Message::SendFail (TCollection_AsciiString ("Error: Unable to write file '") + theFile + "'"); return false; } + aFileOut.reset(); return true; } diff --git a/src/OSD/OSD_CachedFileSystem.cxx b/src/OSD/OSD_CachedFileSystem.cxx index 2002bc35ab..e7c119a784 100644 --- a/src/OSD/OSD_CachedFileSystem.cxx +++ b/src/OSD/OSD_CachedFileSystem.cxx @@ -34,6 +34,15 @@ Standard_Boolean OSD_CachedFileSystem::IsOpenIStream (const opencascade::std::sh return OSD_FileSystem::DefaultFileSystem()->IsOpenIStream (theStream); } +//======================================================================= +// function : IsOpenOStream +// purpose : +//======================================================================= +Standard_Boolean OSD_CachedFileSystem::IsOpenOStream (const opencascade::std::shared_ptr& theStream) const +{ + return OSD_FileSystem::DefaultFileSystem()->IsOpenOStream (theStream); +} + //======================================================================= // function : OpenIStream // purpose : @@ -52,6 +61,16 @@ opencascade::std::shared_ptr OSD_CachedFileSystem::OpenIStream (co return myStream.Stream; } +//======================================================================= +// function : OpenOStream +// purpose : +//======================================================================= +opencascade::std::shared_ptr OSD_CachedFileSystem::OpenOStream (const TCollection_AsciiString& theUrl, + const std::ios_base::openmode theMode) +{ + return OSD_FileSystem::DefaultFileSystem()->OpenOStream (theUrl, theMode); +} + //======================================================================= // function : OpenStreamBuffer // purpose : @@ -61,6 +80,10 @@ opencascade::std::shared_ptr OSD_CachedFileSystem::OpenStreamBuf const int64_t theOffset, int64_t* theOutBufSize) { + if ((theMode & std::ios::out) == std::ios::out) + { + return OSD_FileSystem::DefaultFileSystem()->OpenStreamBuffer (theUrl, theMode, theOffset, theOutBufSize); + } if (myStream.Url != theUrl) { myStream.Url = theUrl; diff --git a/src/OSD/OSD_CachedFileSystem.hxx b/src/OSD/OSD_CachedFileSystem.hxx index 37f4153e4e..b7389ec890 100644 --- a/src/OSD/OSD_CachedFileSystem.hxx +++ b/src/OSD/OSD_CachedFileSystem.hxx @@ -34,6 +34,9 @@ public: //! Returns TRUE if current input stream is opened for reading operations. Standard_EXPORT virtual Standard_Boolean IsOpenIStream (const opencascade::std::shared_ptr& theStream) const Standard_OVERRIDE; + //! Returns TRUE if current output stream is opened for writing operations. + Standard_EXPORT virtual Standard_Boolean IsOpenOStream (const opencascade::std::shared_ptr& theStream) const Standard_OVERRIDE; + //! Opens stream for specified file URL for reading operations or returns previously created stream pointing to the same URL. Standard_EXPORT virtual opencascade::std::shared_ptr OpenIStream (const TCollection_AsciiString& theUrl, @@ -41,6 +44,10 @@ public: const int64_t theOffset, const opencascade::std::shared_ptr& theOldStream) Standard_OVERRIDE; + //! Opens stream for specified file URL for writing operations (std::ostream) by calling parent's method. + Standard_EXPORT virtual opencascade::std::shared_ptr OpenOStream (const TCollection_AsciiString& theUrl, + const std::ios_base::openmode theMode) Standard_OVERRIDE; + //! Opens stream buffer for specified file URL. Standard_EXPORT virtual opencascade::std::shared_ptr OpenStreamBuffer (const TCollection_AsciiString& theUrl, diff --git a/src/OSD/OSD_FileSystem.cxx b/src/OSD/OSD_FileSystem.cxx index a0e4f62921..81fc4c2de1 100644 --- a/src/OSD/OSD_FileSystem.cxx +++ b/src/OSD/OSD_FileSystem.cxx @@ -98,3 +98,21 @@ opencascade::std::shared_ptr OSD_FileSystem::OpenIStream (const TC } return aNewStream; } + +//======================================================================= +// function : OpenOStream +// purpose : +//======================================================================= +opencascade::std::shared_ptr OSD_FileSystem::OpenOStream (const TCollection_AsciiString& theUrl, + const std::ios_base::openmode theMode) +{ + opencascade::std::shared_ptr aNewStream; + opencascade::std::shared_ptr aFileBuf = OpenStreamBuffer (theUrl, theMode | std::ios_base::out); + if (aFileBuf.get() == NULL) + { + return opencascade::std::shared_ptr(); + } + + aNewStream.reset(new OSD_OStreamBuffer (theUrl.ToCString(), aFileBuf)); + return aNewStream; +} diff --git a/src/OSD/OSD_FileSystem.hxx b/src/OSD/OSD_FileSystem.hxx index 744fe8f916..19334f9068 100644 --- a/src/OSD/OSD_FileSystem.hxx +++ b/src/OSD/OSD_FileSystem.hxx @@ -35,6 +35,9 @@ public: //! Returns TRUE if current input stream is opened for reading operations. virtual Standard_Boolean IsOpenIStream (const opencascade::std::shared_ptr& theStream) const = 0; + //! Returns TRUE if current output stream is opened for writing operations. + virtual Standard_Boolean IsOpenOStream(const opencascade::std::shared_ptr& theStream) const = 0; + //! Opens stream for specified file URL for reading operations (std::istream). //! Default implementation create a stream from file buffer returned by OSD_FileSystem::OpenFileBuffer(). //! @param theUrl [in] path to open @@ -49,6 +52,14 @@ public: const int64_t theOffset = 0, const opencascade::std::shared_ptr& theOldStream = opencascade::std::shared_ptr()); + //! Opens stream for specified file URL for writing operations (std::ostream). + //! Default implementation create a stream from file buffer returned by OSD_FileSystem::OpenFileBuffer(). + //! @param theUrl [in] path to open + //! @param theMode [in] flags describing the requested output mode for the stream (std::ios_base::out will be implicitly added) + //! @return pointer to newly created opened stream or NULL in case of failure. + Standard_EXPORT virtual opencascade::std::shared_ptr OpenOStream (const TCollection_AsciiString& theUrl, + const std::ios_base::openmode theMode); + //! Opens stream buffer for specified file URL. //! @param theUrl [in] path to open //! @param theMode [in] flags describing the requested input mode for the stream diff --git a/src/OSD/OSD_FileSystemSelector.cxx b/src/OSD/OSD_FileSystemSelector.cxx index 18cff9c0ec..0733ecb276 100644 --- a/src/OSD/OSD_FileSystemSelector.cxx +++ b/src/OSD/OSD_FileSystemSelector.cxx @@ -82,6 +82,31 @@ Standard_Boolean OSD_FileSystemSelector::IsOpenIStream (const opencascade::std:: return false; } +//======================================================================= +// function : IsOpenOStream +// purpose : +//======================================================================= +Standard_Boolean OSD_FileSystemSelector::IsOpenOStream (const opencascade::std::shared_ptr& theStream) const +{ + opencascade::std::shared_ptr aFileStream = opencascade::std::dynamic_pointer_cast (theStream); + if (aFileStream.get() == NULL) + { + return false; + } + for (NCollection_List::Iterator aProtIter (myProtocols); aProtIter.More(); aProtIter.Next()) + { + const Handle(OSD_FileSystem)& aFileSystem = aProtIter.Value(); + if (aFileSystem->IsSupportedPath (TCollection_AsciiString (aFileStream->Url().c_str()))) + { + if (aFileSystem->IsOpenOStream (theStream)) + { + return true; + } + } + } + return false; +} + //======================================================================= // function : OpenIStream // purpose : @@ -106,6 +131,27 @@ opencascade::std::shared_ptr OSD_FileSystemSelector::OpenIStream ( return opencascade::std::shared_ptr(); } +//======================================================================= +// function : OpenOStream +// purpose : +//======================================================================= +opencascade::std::shared_ptr OSD_FileSystemSelector::OpenOStream (const TCollection_AsciiString& theUrl, + const std::ios_base::openmode theMode) +{ + for (NCollection_List::Iterator aProtIter (myProtocols); aProtIter.More(); aProtIter.Next()) + { + const Handle(OSD_FileSystem)& aFileSystem = aProtIter.Value(); + if (aFileSystem->IsSupportedPath (theUrl)) + { + opencascade::std::shared_ptr aStream = aFileSystem->OpenOStream (theUrl, theMode); + if (aStream.get() != NULL) + { + return aStream; + } + } + } + return opencascade::std::shared_ptr(); +} //======================================================================= // function : OpenStreamBuffer diff --git a/src/OSD/OSD_FileSystemSelector.hxx b/src/OSD/OSD_FileSystemSelector.hxx index 79d3adc03d..60aa5e1fd6 100644 --- a/src/OSD/OSD_FileSystemSelector.hxx +++ b/src/OSD/OSD_FileSystemSelector.hxx @@ -43,6 +43,9 @@ public: //! Returns TRUE if current input stream is opened for reading operations. Standard_EXPORT virtual Standard_Boolean IsOpenIStream (const opencascade::std::shared_ptr& theStream) const Standard_OVERRIDE; + //! Returns TRUE if current output stream is opened for writing operations. + Standard_EXPORT virtual Standard_Boolean IsOpenOStream (const opencascade::std::shared_ptr& theStream) const Standard_OVERRIDE; + //! Opens input stream using one of registered protocols. Standard_EXPORT virtual opencascade::std::shared_ptr OpenIStream (const TCollection_AsciiString& theUrl, @@ -50,6 +53,10 @@ public: const int64_t theOffset = 0, const opencascade::std::shared_ptr& theOldStream = opencascade::std::shared_ptr()) Standard_OVERRIDE; + //! Opens output stream using one of registered protocols. + Standard_EXPORT virtual opencascade::std::shared_ptr OpenOStream (const TCollection_AsciiString& theUrl, + const std::ios_base::openmode theMode) Standard_OVERRIDE; + //! Opens stream buffer using one of registered protocols. Standard_EXPORT virtual opencascade::std::shared_ptr OpenStreamBuffer (const TCollection_AsciiString& theUrl, diff --git a/src/OSD/OSD_LocalFileSystem.cxx b/src/OSD/OSD_LocalFileSystem.cxx index b13a466e68..2666bc1d13 100644 --- a/src/OSD/OSD_LocalFileSystem.cxx +++ b/src/OSD/OSD_LocalFileSystem.cxx @@ -42,6 +42,21 @@ Standard_Boolean OSD_LocalFileSystem::IsOpenIStream (const opencascade::std::sha return (aFileBuf != NULL) ? aFileBuf->is_open() : false; } +//======================================================================= +// function : IsOpenOStream +// purpose : +//======================================================================= +Standard_Boolean OSD_LocalFileSystem::IsOpenOStream (const opencascade::std::shared_ptr& theStream) const +{ + opencascade::std::shared_ptr aFileStream = opencascade::std::dynamic_pointer_cast (theStream); + if (aFileStream.get() == NULL) + { + return false; + } + const std::filebuf* aFileBuf = dynamic_cast (aFileStream->rdbuf()); + return (aFileBuf != NULL) ? aFileBuf->is_open() : false; +} + //======================================================================= // function : OpenStreamBuffer // purpose : diff --git a/src/OSD/OSD_LocalFileSystem.hxx b/src/OSD/OSD_LocalFileSystem.hxx index 789461c968..fbe2db9545 100644 --- a/src/OSD/OSD_LocalFileSystem.hxx +++ b/src/OSD/OSD_LocalFileSystem.hxx @@ -31,6 +31,9 @@ public: //! Returns TRUE if current input stream is opened for reading operations. Standard_EXPORT virtual Standard_Boolean IsOpenIStream (const opencascade::std::shared_ptr& theStream) const Standard_OVERRIDE; + //! Returns TRUE if current output stream is opened for writing operations. + Standard_EXPORT virtual Standard_Boolean IsOpenOStream (const opencascade::std::shared_ptr& theStream) const Standard_OVERRIDE; + //! Opens stream buffer for specified file URL. Standard_EXPORT virtual opencascade::std::shared_ptr OpenStreamBuffer (const TCollection_AsciiString& theUrl, diff --git a/src/RWGltf/RWGltf_CafWriter.cxx b/src/RWGltf/RWGltf_CafWriter.cxx index bf4e8d7d62..3888e725f3 100644 --- a/src/RWGltf/RWGltf_CafWriter.cxx +++ b/src/RWGltf/RWGltf_CafWriter.cxx @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -359,10 +358,10 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument myBinDataMap.Clear(); myBinDataLen64 = 0; - std::ofstream aBinFile; - OSD_OpenStream (aBinFile, myBinFileNameFull.ToCString(), std::ios::out | std::ios::binary); - if (!aBinFile.is_open() - || !aBinFile.good()) + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aBinFile = aFileSystem->OpenOStream (myBinFileNameFull, std::ios::out | std::ios::binary); + if (aBinFile.get() == NULL + || !aBinFile->good()) { Message::SendFail (TCollection_AsciiString ("File '") + myBinFileNameFull + "' can not be created"); return false; @@ -373,7 +372,7 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument Standard_Integer aNbAccessors = 0; // write positions - myBuffViewPos.ByteOffset = aBinFile.tellp(); + myBuffViewPos.ByteOffset = aBinFile->tellp(); for (XCAFPrs_DocumentExplorer aDocExplorer (theDocument, theRootLabels, XCAFPrs_DocumentExplorerFlags_OnlyLeafNodes); aDocExplorer.More() && aPSentryBin.More(); aDocExplorer.Next()) { @@ -394,9 +393,9 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument } RWGltf_GltfFace aGltfFace; - saveNodes (aGltfFace, aBinFile, aFaceIter, aNbAccessors); + saveNodes (aGltfFace, *aBinFile, aFaceIter, aNbAccessors); - if (!aBinFile.good()) + if (!aBinFile->good()) { Message::SendFail (TCollection_AsciiString ("File '") + myBinFileNameFull + "' can not be written"); return false; @@ -405,7 +404,7 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument myBinDataMap.Bind (aFaceIter.Face(), aGltfFace); } } - myBuffViewPos.ByteLength = (int64_t )aBinFile.tellp() - myBuffViewPos.ByteOffset; + myBuffViewPos.ByteLength = (int64_t )aBinFile->tellp() - myBuffViewPos.ByteOffset; if (!aPSentryBin.More()) { return false; @@ -413,7 +412,7 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument aPSentryBin.Next(); // write normals - myBuffViewNorm.ByteOffset = aBinFile.tellp(); + myBuffViewNorm.ByteOffset = aBinFile->tellp(); for (XCAFPrs_DocumentExplorer aDocExplorer (theDocument, theRootLabels, XCAFPrs_DocumentExplorerFlags_OnlyLeafNodes); aDocExplorer.More() && aPSentryBin.More(); aDocExplorer.Next()) { @@ -436,16 +435,16 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument continue; } - saveNormals (aGltfFace, aBinFile, aFaceIter, aNbAccessors); + saveNormals (aGltfFace, *aBinFile, aFaceIter, aNbAccessors); - if (!aBinFile.good()) + if (!aBinFile->good()) { Message::SendFail (TCollection_AsciiString ("File '") + myBinFileNameFull + "' can not be written"); return false; } } } - myBuffViewNorm.ByteLength = (int64_t )aBinFile.tellp() - myBuffViewNorm.ByteOffset; + myBuffViewNorm.ByteLength = (int64_t )aBinFile->tellp() - myBuffViewNorm.ByteOffset; if (!aPSentryBin.More()) { return false; @@ -453,7 +452,7 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument aPSentryBin.Next(); // write texture coordinates - myBuffViewTextCoord.ByteOffset = aBinFile.tellp(); + myBuffViewTextCoord.ByteOffset = aBinFile->tellp(); for (XCAFPrs_DocumentExplorer aDocExplorer (theDocument, theRootLabels, XCAFPrs_DocumentExplorerFlags_OnlyLeafNodes); aDocExplorer.More() && aPSentryBin.More(); aDocExplorer.Next()) { @@ -477,16 +476,16 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument continue; } - saveTextCoords (aGltfFace, aBinFile, aFaceIter, aNbAccessors); + saveTextCoords (aGltfFace, *aBinFile, aFaceIter, aNbAccessors); - if (!aBinFile.good()) + if (!aBinFile->good()) { Message::SendFail (TCollection_AsciiString ("File '") + myBinFileNameFull + "' can not be written"); return false; } } } - myBuffViewTextCoord.ByteLength = (int64_t )aBinFile.tellp() - myBuffViewTextCoord.ByteOffset; + myBuffViewTextCoord.ByteLength = (int64_t )aBinFile->tellp() - myBuffViewTextCoord.ByteOffset; if (!aPSentryBin.More()) { return false; @@ -494,7 +493,7 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument aPSentryBin.Next(); // write indices - myBuffViewInd.ByteOffset = aBinFile.tellp(); + myBuffViewInd.ByteOffset = aBinFile->tellp(); for (XCAFPrs_DocumentExplorer aDocExplorer (theDocument, theRootLabels, XCAFPrs_DocumentExplorerFlags_OnlyLeafNodes); aDocExplorer.More() && aPSentryBin.More(); aDocExplorer.Next()) { @@ -518,16 +517,16 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument continue; } - saveIndices (aGltfFace, aBinFile, aFaceIter, aNbAccessors); + saveIndices (aGltfFace, *aBinFile, aFaceIter, aNbAccessors); - if (!aBinFile.good()) + if (!aBinFile->good()) { Message::SendFail (TCollection_AsciiString ("File '") + myBinFileNameFull + "' can not be written"); return false; } } } - myBuffViewInd.ByteLength = (int64_t )aBinFile.tellp() - myBuffViewInd.ByteOffset; + myBuffViewInd.ByteLength = (int64_t )aBinFile->tellp() - myBuffViewInd.ByteOffset; if (myIsBinary && myToEmbedTexturesInGlb) @@ -551,7 +550,7 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument continue; } - myMaterialMap->AddGlbImages (aBinFile, aFaceIter.FaceStyle()); + myMaterialMap->AddGlbImages (*aBinFile, aFaceIter.FaceStyle()); } } } @@ -575,13 +574,14 @@ bool RWGltf_CafWriter::writeBinData (const Handle(TDocStd_Document)& theDocument } // myMaterialMap->FlushGlbBufferViews() will put image bufferView's IDs at the end of list - myBinDataLen64 = aBinFile.tellp(); - aBinFile.close(); - if (!aBinFile.good()) + myBinDataLen64 = aBinFile->tellp(); + aBinFile->flush(); + if (!aBinFile->good()) { Message::SendFail (TCollection_AsciiString ("File '") + myBinFileNameFull + "' can not be written"); return false; } + aBinFile.reset(); return true; } @@ -605,10 +605,10 @@ bool RWGltf_CafWriter::writeJson (const Handle(TDocStd_Document)& theDocument, const Standard_Integer aDefSceneId = 0; const TCollection_AsciiString aFileNameGltf = myFile; - std::ofstream aGltfContentFile; - OSD_OpenStream (aGltfContentFile, aFileNameGltf.ToCString(), std::ios::out | std::ios::binary); - if (!aGltfContentFile.is_open() - || !aGltfContentFile.good()) + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aGltfContentFile = aFileSystem->OpenOStream (aFileNameGltf, std::ios::out | std::ios::binary); + if (aGltfContentFile.get() == NULL + || !aGltfContentFile->good()) { Message::SendFail (TCollection_AsciiString ("File '") + aFileNameGltf + "' can not be created"); return false; @@ -621,11 +621,11 @@ bool RWGltf_CafWriter::writeJson (const Handle(TDocStd_Document)& theDocument, uint32_t aContentLength = 0; uint32_t aContentType = 0x4E4F534A; - aGltfContentFile.write (aMagic, 4); - aGltfContentFile.write ((const char* )&aVersion, sizeof(aVersion)); - aGltfContentFile.write ((const char* )&aLength, sizeof(aLength)); - aGltfContentFile.write ((const char* )&aContentLength, sizeof(aContentLength)); - aGltfContentFile.write ((const char* )&aContentType, sizeof(aContentType)); + aGltfContentFile->write (aMagic, 4); + aGltfContentFile->write ((const char* )&aVersion, sizeof(aVersion)); + aGltfContentFile->write ((const char* )&aLength, sizeof(aLength)); + aGltfContentFile->write ((const char* )&aContentLength, sizeof(aContentLength)); + aGltfContentFile->write ((const char* )&aContentType, sizeof(aContentType)); } // Prepare an indexed map of scene nodes (without assemblies) in correct order. @@ -665,7 +665,7 @@ bool RWGltf_CafWriter::writeJson (const Handle(TDocStd_Document)& theDocument, } } - rapidjson::OStreamWrapper aFileStream (aGltfContentFile); + rapidjson::OStreamWrapper aFileStream (*aGltfContentFile); myWriter.reset (new RWGltf_GltfOStreamWriter (aFileStream)); myWriter->StartObject(); @@ -700,32 +700,32 @@ bool RWGltf_CafWriter::writeJson (const Handle(TDocStd_Document)& theDocument, if (!myIsBinary) { - aGltfContentFile.close(); - if (!aGltfContentFile.good()) + aGltfContentFile->flush(); + if (!aGltfContentFile->good()) { Message::SendFail (TCollection_AsciiString ("File '") + aFileNameGltf + "' can not be written"); return false; } + aGltfContentFile.reset(); return true; } - int64_t aContentLen64 = (int64_t )aGltfContentFile.tellp() - 20; + int64_t aContentLen64 = (int64_t )aGltfContentFile->tellp() - 20; while (aContentLen64 % 4 != 0) { - aGltfContentFile.write (" ", 1); + aGltfContentFile->write (" ", 1); ++aContentLen64; } const uint32_t aBinLength = (uint32_t )myBinDataLen64; const uint32_t aBinType = 0x004E4942; - aGltfContentFile.write ((const char*)&aBinLength, 4); - aGltfContentFile.write ((const char*)&aBinType, 4); + aGltfContentFile->write ((const char*)&aBinLength, 4); + aGltfContentFile->write ((const char*)&aBinType, 4); const int64_t aFullLen64 = aContentLen64 + 20 + myBinDataLen64 + 8; if (aFullLen64 < std::numeric_limits::max()) { { - const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); opencascade::std::shared_ptr aBinFile = aFileSystem->OpenIStream (myBinFileNameFull, std::ios::in | std::ios::binary); if (aBinFile.get() == NULL || !aBinFile->good()) { @@ -741,7 +741,7 @@ bool RWGltf_CafWriter::writeJson (const Handle(TDocStd_Document)& theDocument, { break; } - aGltfContentFile.write (aBuffer, aReadLen); + aGltfContentFile->write (aBuffer, aReadLen); } } OSD_Path aBinFilePath (myBinFileNameFull); @@ -759,17 +759,17 @@ bool RWGltf_CafWriter::writeJson (const Handle(TDocStd_Document)& theDocument, const uint32_t aLength = (uint32_t )aFullLen64; const uint32_t aContentLength = (uint32_t )aContentLen64; - aGltfContentFile.seekp (8); - aGltfContentFile.write ((const char* )&aLength, 4); - aGltfContentFile.write ((const char* )&aContentLength, 4); + aGltfContentFile->seekp (8); + aGltfContentFile->write ((const char* )&aLength, 4); + aGltfContentFile->write ((const char* )&aContentLength, 4); - aGltfContentFile.close(); - if (!aGltfContentFile.good()) + aGltfContentFile->flush(); + if (!aGltfContentFile->good()) { Message::SendFail (TCollection_AsciiString ("File '") + aFileNameGltf + "' can not be written"); return false; } - + aGltfContentFile.reset(); myWriter.reset(); return true; #else diff --git a/src/StepSelect/StepSelect_WorkLibrary.cxx b/src/StepSelect/StepSelect_WorkLibrary.cxx index b1e136a103..4d9f58934a 100644 --- a/src/StepSelect/StepSelect_WorkLibrary.cxx +++ b/src/StepSelect/StepSelect_WorkLibrary.cxx @@ -26,7 +26,7 @@ #include #include #include -#include +#include #include #include #include @@ -100,10 +100,10 @@ Standard_Boolean StepSelect_WorkLibrary::WriteFile DeclareAndCast(StepData_Protocol,stepro,ctx.Protocol()); if (stepmodel.IsNull() || stepro.IsNull()) return Standard_False; - std::ofstream fout; - OSD_OpenStream(fout,ctx.FileName(),std::ios::out|std::ios::trunc); + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aStream = aFileSystem->OpenOStream (ctx.FileName(), std::ios::out | std::ios::trunc); - if (!fout || !fout.is_open()) { + if (aStream.get() == NULL) { ctx.CCheck(0)->AddFail("Step File could not be created"); sout<<" Step File could not be created : " << ctx.FileName() << std::endl; return 0; } @@ -130,12 +130,13 @@ Standard_Boolean StepSelect_WorkLibrary::WriteFile for (chl.Start(); chl.More(); chl.Next()) ctx.CCheck(chl.Number())->GetMessages(chl.Value()); sout<<" Write "; - Standard_Boolean isGood = SW.Print(fout); + Standard_Boolean isGood = SW.Print (*aStream); sout<<" Done"<flush(); + isGood = aStream->good() && isGood && !errno; + aStream.reset(); if(errno) sout << strerror(errno) << std::endl; return isGood; diff --git a/src/TObjDRAW/TObjDRAW.cxx b/src/TObjDRAW/TObjDRAW.cxx index a42e88f16f..af3e257146 100644 --- a/src/TObjDRAW/TObjDRAW.cxx +++ b/src/TObjDRAW/TObjDRAW.cxx @@ -38,7 +38,6 @@ #include #include #include -#include #include @@ -208,9 +207,9 @@ static Standard_Integer saveModel (Draw_Interpretor& di, Standard_Integer argc, } if (anUseStream) { - std::ofstream aFileStream; - OSD_OpenStream (aFileStream, argv[2], std::ios::out | std::ios::binary); - isSaved = aModel->SaveAs (aFileStream); + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aFileStream = aFileSystem->OpenOStream (argv[2], std::ios::out | std::ios::binary); + isSaved = aModel->SaveAs (*aFileStream); } else isSaved = aModel->SaveAs ( TCollection_ExtendedString (argv[2], Standard_True) ); diff --git a/src/ViewerTest/ViewerTest_OpenGlCommands.cxx b/src/ViewerTest/ViewerTest_OpenGlCommands.cxx index 327320449f..dd42d4a8f5 100644 --- a/src/ViewerTest/ViewerTest_OpenGlCommands.cxx +++ b/src/ViewerTest/ViewerTest_OpenGlCommands.cxx @@ -21,7 +21,7 @@ #include #include #include -#include +#include #include #include @@ -637,23 +637,24 @@ static Standard_Integer VListMaterials (Draw_Interpretor& theDI, Graphic3d_Vec4i (5, 1, 4, 8) }; - std::ofstream aMatFile, anObjFile, anHtmlFile; + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aMatFile, anObjFile, aHtmlFile; if (aDumpFile.EndsWith (".obj") || aDumpFile.EndsWith (".mtl")) { const TCollection_AsciiString aMatFilePath = aDumpFile.SubString (1, aDumpFile.Length() - 3) + "mtl"; const TCollection_AsciiString anObjFilePath = aDumpFile.SubString (1, aDumpFile.Length() - 3) + "obj"; - OSD_OpenStream (aMatFile, aMatFilePath.ToCString(), std::ios::out | std::ios::binary); - if (!aMatFile.is_open()) + aMatFile = aFileSystem->OpenOStream (aMatFilePath, std::ios::out | std::ios::binary); + if (aMatFile.get() == NULL) { Message::SendFail ("Error: unable creating material file"); return 0; } if (!aDumpFile.EndsWith (".mtl")) { - OSD_OpenStream (anObjFile, anObjFilePath.ToCString(), std::ios::out | std::ios::binary); - if (!anObjFile.is_open()) + anObjFile = aFileSystem->OpenOStream (anObjFilePath, std::ios::out | std::ios::binary); + if (anObjFile.get() == NULL) { Message::SendFail ("Error: unable creating OBJ file"); return 0; @@ -661,19 +662,19 @@ static Standard_Integer VListMaterials (Draw_Interpretor& theDI, TCollection_AsciiString anMtlName, aFolder; OSD_Path::FolderAndFileFromPath (aMatFilePath, aFolder, anMtlName); - anObjFile << "mtllib " << anMtlName << "\n"; + *anObjFile << "mtllib " << anMtlName << "\n"; } } else if (aDumpFile.EndsWith (".htm") || aDumpFile.EndsWith (".html")) { - OSD_OpenStream (anHtmlFile, aDumpFile.ToCString(), std::ios::out | std::ios::binary); - if (!anHtmlFile.is_open()) + aHtmlFile = aFileSystem->OpenOStream (aDumpFile, std::ios::out | std::ios::binary); + if (aHtmlFile.get() == NULL) { Message::SendFail ("Error: unable creating HTML file"); return 0; } - anHtmlFile << "\n" + *aHtmlFile << "\n" "OCCT Material table\n" "\n" "\n" @@ -743,45 +744,45 @@ static Standard_Integer VListMaterials (Draw_Interpretor& theDI, const Graphic3d_Vec3 aSpecular = (Graphic3d_Vec3 )aMat.SpecularColor(); const Graphic3d_Vec3 anEmission = (Graphic3d_Vec3 )aMat.EmissiveColor(); const Standard_Real aShiness = aMat.Shininess() * 1000.0; - if (aMatFile.is_open()) + if (aMatFile.get() != NULL) { - aMatFile << "newmtl " << aMatName << "\n"; - aMatFile << "Ka " << Quantity_Color::Convert_LinearRGB_To_sRGB (anAmbient) << "\n"; - aMatFile << "Kd " << Quantity_Color::Convert_LinearRGB_To_sRGB (aDiffuse) << "\n"; - aMatFile << "Ks " << Quantity_Color::Convert_LinearRGB_To_sRGB (aSpecular) << "\n"; - aMatFile << "Ns " << aShiness << "\n"; + *aMatFile << "newmtl " << aMatName << "\n"; + *aMatFile << "Ka " << Quantity_Color::Convert_LinearRGB_To_sRGB (anAmbient) << "\n"; + *aMatFile << "Kd " << Quantity_Color::Convert_LinearRGB_To_sRGB (aDiffuse) << "\n"; + *aMatFile << "Ks " << Quantity_Color::Convert_LinearRGB_To_sRGB (aSpecular) << "\n"; + *aMatFile << "Ns " << aShiness << "\n"; if (aMat.Transparency() >= 0.0001) { - aMatFile << "Tr " << aMat.Transparency() << "\n"; + *aMatFile << "Tr " << aMat.Transparency() << "\n"; } - aMatFile << "\n"; + *aMatFile << "\n"; } - else if (anHtmlFile.is_open()) + else if (aHtmlFile.get() != NULL) { - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; } else { @@ -808,20 +809,20 @@ static Standard_Integer VListMaterials (Draw_Interpretor& theDI, theDI << " RefractionIndex: " << aMat.RefractionIndex() << "\n"; } - if (anObjFile.is_open()) + if (anObjFile.get() != NULL) { - anObjFile << "g " << aMatName << "\n"; - anObjFile << "usemtl " << aMatName << "\n"; + *anObjFile << "g " << aMatName << "\n"; + *anObjFile << "usemtl " << aMatName << "\n"; for (Standard_Integer aVertIter = 0; aVertIter < 8; ++aVertIter) { - anObjFile << "v " << (aBoxVerts[aVertIter] + Graphic3d_Vec3 (3.0f * anX, -3.0f * anY, 0.0f)) << "\n"; + *anObjFile << "v " << (aBoxVerts[aVertIter] + Graphic3d_Vec3 (3.0f * anX, -3.0f * anY, 0.0f)) << "\n"; } - anObjFile << "s off\n"; + *anObjFile << "s off\n"; for (Standard_Integer aFaceIter = 0; aFaceIter < 6; ++aFaceIter) { - anObjFile << "f " << (aBoxQuads[aFaceIter] + Graphic3d_Vec4i (8 * aMatIndex)) << "\n"; + *anObjFile << "f " << (aBoxQuads[aFaceIter] + Graphic3d_Vec4i (8 * aMatIndex)) << "\n"; } - anObjFile << "\n"; + *anObjFile << "\n"; if (++anX > 5) { anX = 0; @@ -830,9 +831,9 @@ static Standard_Integer VListMaterials (Draw_Interpretor& theDI, } } - if (anHtmlFile.is_open()) + if (aHtmlFile.get() != NULL) { - anHtmlFile << "
" << aMat.StringName() << "" << (aMat.MaterialType() == Graphic3d_MATERIAL_PHYSIC ? "PHYSIC" : "ASPECT") << "" << aMat.Transparency() << "" << formatSvgColoredRect (aMat.PBRMaterial().Color().GetRGB()) << (Graphic3d_Vec3 )aMat.PBRMaterial().Color().GetRGB() << "" << aMat.PBRMaterial().Metallic() << "" << aMat.PBRMaterial().NormalizedRoughness() << "" << formatSvgColoredRect (Quantity_Color (aMat.PBRMaterial().Emission())) << aMat.PBRMaterial().Emission() << "" << aMat.PBRMaterial().IOR() << "" << formatSvgColoredRect (Quantity_Color (anAmbient)) << anAmbient << "" << formatSvgColoredRect (Quantity_Color (aDiffuse)) << aDiffuse << "" << formatSvgColoredRect (Quantity_Color (aSpecular)) << aSpecular << "" << formatSvgColoredRect (Quantity_Color (anEmission)) << anEmission << "" << aMat.Shininess() << "" << aMat.BSDF().Kc << "" << aMat.BSDF().Kd << "" << aMat.BSDF().Ks << "" << aMat.BSDF().Kt << "" << aMat.BSDF().Le << "" << aMat.BSDF().Absorption << "" << fresnelModelString (aMat.BSDF().FresnelCoat.FresnelType()) << "" << fresnelModelString (aMat.BSDF().FresnelBase.FresnelType()) << "" << aMat.RefractionIndex() << "
" << aMat.StringName() << "" << (aMat.MaterialType() == Graphic3d_MATERIAL_PHYSIC ? "PHYSIC" : "ASPECT") << "" << aMat.Transparency() << "" << formatSvgColoredRect (aMat.PBRMaterial().Color().GetRGB()) << (Graphic3d_Vec3 )aMat.PBRMaterial().Color().GetRGB() << "" << aMat.PBRMaterial().Metallic() << "" << aMat.PBRMaterial().NormalizedRoughness() << "" << formatSvgColoredRect (Quantity_Color (aMat.PBRMaterial().Emission())) << aMat.PBRMaterial().Emission() << "" << aMat.PBRMaterial().IOR() << "" << formatSvgColoredRect (Quantity_Color (anAmbient)) << anAmbient << "" << formatSvgColoredRect (Quantity_Color (aDiffuse)) << aDiffuse << "" << formatSvgColoredRect (Quantity_Color (aSpecular)) << aSpecular << "" << formatSvgColoredRect (Quantity_Color (anEmission)) << anEmission << "" << aMat.Shininess() << "" << aMat.BSDF().Kc << "" << aMat.BSDF().Kd << "" << aMat.BSDF().Ks << "" << aMat.BSDF().Kt << "" << aMat.BSDF().Le << "" << aMat.BSDF().Absorption << "" << fresnelModelString (aMat.BSDF().FresnelCoat.FresnelType()) << "" << fresnelModelString (aMat.BSDF().FresnelBase.FresnelType()) << "" << aMat.RefractionIndex() << "
\n\n\n"; + *aHtmlFile << "\n\n\n"; } return 0; } @@ -892,7 +893,8 @@ static Standard_Integer VListColors (Draw_Interpretor& theDI, } } - std::ofstream anHtmlFile; + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aHtmlFile; TCollection_AsciiString aFileNameBase, aFolder; if (aDumpFile.EndsWith (".htm") || aDumpFile.EndsWith (".html")) @@ -929,13 +931,13 @@ static Standard_Integer VListColors (Draw_Interpretor& theDI, if (!aDumpFile.IsEmpty()) { - OSD_OpenStream (anHtmlFile, aDumpFile.ToCString(), std::ios::out | std::ios::binary); - if (!anHtmlFile.is_open()) + aHtmlFile = aFileSystem->OpenOStream (aDumpFile, std::ios::out | std::ios::binary); + if (aHtmlFile.get() == NULL) { Message::SendFail ("Error: unable creating HTML file"); return 0; } - anHtmlFile << "\n" + *aHtmlFile << "\n" << "OCCT Color table\n" << "\n" << "\n" @@ -957,7 +959,7 @@ static Standard_Integer VListColors (Draw_Interpretor& theDI, const TCollection_AsciiString aColName = Quantity_Color::StringName (aColIter.Value()); const TCollection_AsciiString anSRgbHex = Quantity_Color::ColorToHex (aCol); const Graphic3d_Vec3i anSRgbInt ((Graphic3d_Vec3 )aCol * 255.0f); - if (anHtmlFile.is_open()) + if (aHtmlFile.get() != NULL) { const TCollection_AsciiString anImgPath = aFileNameBase + "_" + aColName + ".png"; if (!aView.IsNull()) @@ -972,14 +974,14 @@ static Standard_Integer VListColors (Draw_Interpretor& theDI, } } - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; - anHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; + *aHtmlFile << "\n"; } else { @@ -994,9 +996,9 @@ static Standard_Integer VListColors (Draw_Interpretor& theDI, ViewerTest::RemoveView (aView); } - if (anHtmlFile.is_open()) + if (aHtmlFile.get() != NULL) { - anHtmlFile << "
       
" << aColName << "
" << anSRgbHex << "
(" << anSRgbInt.r() << " " << anSRgbInt.g() << " " << anSRgbInt.b() << ")(" << aCol.Red() << " " << aCol.Green() << " " << aCol.Blue() << ")
       
" << aColName << "
" << anSRgbHex << "
(" << anSRgbInt.r() << " " << anSRgbInt.g() << " " << anSRgbInt.b() << ")(" << aCol.Red() << " " << aCol.Green() << " " << aCol.Blue() << ")
\n\n\n"; + *aHtmlFile << "\n\n\n"; } return 0; } @@ -1085,20 +1087,19 @@ static Standard_Integer VGenEnvLUT (Draw_Interpretor&, aNbSamples = 1024; } - std::ofstream aFile; + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aFile = aFileSystem->OpenOStream (aFilePath, std::ios::out | std::ios::trunc); - OSD_OpenStream (aFile, aFilePath, std::ios::out | std::ios::binary); - - if (!aFile.good()) + if (aFile.get() == NULL || !aFile->good()) { Message::SendFail() << "Error: unable to write to " << aFilePath; return 1; } - aFile << "//this file has been generated by vgenenvlut draw command\n"; - aFile << "static unsigned int Textures_EnvLUTSize = " << aTableSize << ";\n\n"; - aFile << "static float Textures_EnvLUT[] =\n"; - aFile << "{\n"; + *aFile << "//this file has been generated by vgenenvlut draw command\n"; + *aFile << "static unsigned int Textures_EnvLUTSize = " << aTableSize << ";\n\n"; + *aFile << "static float Textures_EnvLUT[] =\n"; + *aFile << "{\n"; Handle(Image_PixMap) aPixMap = new Image_PixMap(); aPixMap->InitZero (Image_Format_RGF, aTableSize, aTableSize); @@ -1112,44 +1113,43 @@ static Standard_Integer VGenEnvLUT (Draw_Interpretor&, aCounter = 0; for (int x = 0; x < aTableSize; ++x) { - aFile << envLutWriteToFile (aPixMap->Value(aTableSize - 1 - y, x).x()) << ","; - aFile << envLutWriteToFile (aPixMap->Value(aTableSize - 1 - y, x).y()) << ","; + *aFile << envLutWriteToFile (aPixMap->Value(aTableSize - 1 - y, x).x()) << ","; + *aFile << envLutWriteToFile (aPixMap->Value(aTableSize - 1 - y, x).y()) << ","; if (++aCounter % aNumbersInRow == 0) { - aFile << "\n"; + *aFile << "\n"; } else if (x != aTableSize - 1) { - aFile << " "; + *aFile << " "; } } - aFile << "\n"; + *aFile << "\n"; if (aTableSize % aNumbersInRow != 0) { - aFile << "\n"; + *aFile << "\n"; } } aCounter = 0; for (int x = 0; x < aTableSize - 1; ++x) { - aFile << envLutWriteToFile (aPixMap->Value(0, x).x()) << ","; - aFile << envLutWriteToFile (aPixMap->Value(0, x).y()) << ","; + *aFile << envLutWriteToFile (aPixMap->Value(0, x).x()) << ","; + *aFile << envLutWriteToFile (aPixMap->Value(0, x).y()) << ","; if (++aCounter % aNumbersInRow == 0) { - aFile << "\n"; + *aFile << "\n"; } else { - aFile << " "; + *aFile << " "; } } - aFile << envLutWriteToFile (aPixMap->Value(0, aTableSize - 1).x()) << ","; - aFile << envLutWriteToFile (aPixMap->Value(0, aTableSize - 1).y()) << "\n"; + *aFile << envLutWriteToFile (aPixMap->Value(0, aTableSize - 1).x()) << ","; + *aFile << envLutWriteToFile (aPixMap->Value(0, aTableSize - 1).y()) << "\n"; - aFile << "};"; - aFile.close(); + *aFile << "};"; return 0; } diff --git a/src/VrmlAPI/VrmlAPI_Writer.cxx b/src/VrmlAPI/VrmlAPI_Writer.cxx index c4964b5970..9b884a0d38 100644 --- a/src/VrmlAPI/VrmlAPI_Writer.cxx +++ b/src/VrmlAPI/VrmlAPI_Writer.cxx @@ -13,7 +13,7 @@ #include -#include +#include #include #include #include @@ -225,9 +225,9 @@ Standard_Boolean VrmlAPI_Writer::write_v1(const TopoDS_Shape& aShape,const Stand { OSD_Path thePath(aFile); TCollection_AsciiString theFile;thePath.SystemName(theFile); - std::ofstream outfile; - OSD_OpenStream(outfile, theFile.ToCString(), std::ios::out); - if (!outfile) + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr anOutFile = aFileSystem->OpenOStream (theFile, std::ios::out); + if (anOutFile.get() == NULL) { return Standard_False; } @@ -308,16 +308,16 @@ Standard_Boolean VrmlAPI_Writer::write_v1(const TopoDS_Shape& aShape,const Stand XUp, YUp, ZUp, Camera, Light); - Vrml::VrmlHeaderWriter(outfile); + Vrml::VrmlHeaderWriter (*anOutFile); if (myRepresentation == VrmlAPI_BothRepresentation) - Vrml::CommentWriter(" This file contents both Shaded and Wire Frame representation of selected Shape ",outfile); + Vrml::CommentWriter(" This file contents both Shaded and Wire Frame representation of selected Shape ", *anOutFile); if (myRepresentation == VrmlAPI_ShadedRepresentation) - Vrml::CommentWriter(" This file contents only Shaded representation of selected Shape ",outfile); + Vrml::CommentWriter(" This file contents only Shaded representation of selected Shape ", *anOutFile); if (myRepresentation == VrmlAPI_WireFrameRepresentation) - Vrml::CommentWriter(" This file contents only Wire Frame representation of selected Shape ",outfile); + Vrml::CommentWriter(" This file contents only Wire Frame representation of selected Shape ", *anOutFile); Vrml_Separator S1; - S1.Print(outfile); - projector->Add(outfile); + S1.Print (*anOutFile); + projector->Add (*anOutFile); Light = VrmlConverter_DirectionLight; Camera = VrmlConverter_OrthographicCamera; Handle(VrmlConverter_Projector) projector1 = new VrmlConverter_Projector (Shapes, @@ -326,32 +326,32 @@ Standard_Boolean VrmlAPI_Writer::write_v1(const TopoDS_Shape& aShape,const Stand XUp, YUp, ZUp, Camera, Light); - projector1->Add(outfile); + projector1->Add (*anOutFile); Vrml_Separator S2; - S2.Print(outfile); + S2.Print (*anOutFile); if ( (myRepresentation == VrmlAPI_ShadedRepresentation || myRepresentation == VrmlAPI_BothRepresentation) && hasTriangles) { Vrml_Group Group1; - Group1.Print(outfile); + Group1.Print (*anOutFile); Vrml_Instancing I2 ("Shaded representation of shape"); - I2.DEF(outfile); - VrmlConverter_ShadedShape::Add(outfile,aShape,myDrawer); - Group1.Print(outfile); + I2.DEF (*anOutFile); + VrmlConverter_ShadedShape::Add (*anOutFile,aShape,myDrawer); + Group1.Print (*anOutFile); } if (myRepresentation == VrmlAPI_WireFrameRepresentation || myRepresentation == VrmlAPI_BothRepresentation) { Vrml_Group Group2; - Group2.Print(outfile); + Group2.Print (*anOutFile); Vrml_Instancing I3 ("Wire Frame representation of shape"); - I3.DEF(outfile); - VrmlConverter_WFDeflectionShape::Add(outfile,aShape,myDrawer); - Group2.Print(outfile); + I3.DEF (*anOutFile); + VrmlConverter_WFDeflectionShape::Add (*anOutFile,aShape,myDrawer); + Group2.Print (*anOutFile); } - S2.Print(outfile); - S1.Print(outfile); + S2.Print (*anOutFile); + S1.Print (*anOutFile); - outfile.close(); - return outfile.good(); + anOutFile->flush(); + return anOutFile->good(); } Standard_Boolean VrmlAPI_Writer::write_v2(const TopoDS_Shape& aShape,const Standard_CString aFile) const @@ -369,15 +369,15 @@ Standard_Boolean VrmlAPI_Writer::write_v2(const TopoDS_Shape& aShape,const Stand aConv.AddShape(aShape); aConv.Convert(anExtFace, anExtEdge); - std::ofstream anOutStream; - OSD_OpenStream(anOutStream, aFile, std::ios::out); - if (anOutStream) + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr anOutStream = aFileSystem->OpenOStream (aFile, std::ios::out); + if (anOutStream.get() != NULL) { - anOutStream << aScene; - anOutStream.close(); - return anOutStream.good(); + *anOutStream << aScene; + anOutStream->flush(); + return anOutStream->good(); } - + anOutStream.reset(); return Standard_False; } @@ -394,15 +394,15 @@ Standard_Boolean VrmlAPI_Writer::WriteDoc( VrmlData_ShapeConvert aConv(aScene, theScale); aConv.ConvertDocument(theDoc); - std::ofstream anOutStream; - OSD_OpenStream(anOutStream, theFile, std::ios::out); - if (anOutStream) + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr anOutStream = aFileSystem->OpenOStream (theFile, std::ios::out); + if (anOutStream.get() != NULL) { - anOutStream << aScene; - anOutStream.close(); - return anOutStream.good(); + *anOutStream << aScene; + anOutStream->flush(); + return anOutStream->good(); } - + anOutStream.reset(); return Standard_False; } diff --git a/src/XmlLDrivers/XmlLDrivers_DocumentStorageDriver.cxx b/src/XmlLDrivers/XmlLDrivers_DocumentStorageDriver.cxx index 2c7215b806..f1176e6929 100644 --- a/src/XmlLDrivers/XmlLDrivers_DocumentStorageDriver.cxx +++ b/src/XmlLDrivers/XmlLDrivers_DocumentStorageDriver.cxx @@ -24,7 +24,7 @@ #include #include #include -#include +#include #include #include #include @@ -98,12 +98,12 @@ void XmlLDrivers_DocumentStorageDriver::Write (const Handle(CDM_Document)& { myFileName = theFileName; - std::ofstream aFileStream; - OSD_OpenStream (aFileStream, theFileName, std::ios::out); + const Handle(OSD_FileSystem)& aFileSystem = OSD_FileSystem::DefaultFileSystem(); + opencascade::std::shared_ptr aFileStream = aFileSystem->OpenOStream (theFileName, std::ios::out); - if (aFileStream.is_open() && aFileStream.good()) + if (aFileStream.get() != NULL && aFileStream->good()) { - Write (theDocument, aFileStream, theRange); + Write (theDocument, *aFileStream, theRange); } else {