mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-08-04 13:13:25 +03:00
0032099: Visualization - define OSD_FileSystem class managing opening of file streams
1) Remove unnecessary includes of "Standard_OStream.hxx" file and add it only into files where it is really used. 2) Create the base interface for a file stream provider OSD_FileSystem and its inheritor OSD_CachedFileSystem that keeping last stream to be reused for opening a stream with the same URL. 3) Use OSD_CachedFileSystem object instead of RWGltf_GltfSharedIStream during Gltf reading.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
OSD.cxx
|
||||
OSD.hxx
|
||||
OSD_CachedFileSystem.cxx
|
||||
OSD_CachedFileSystem.hxx
|
||||
OSD_Chronometer.cxx
|
||||
OSD_Chronometer.hxx
|
||||
OSD_Directory.cxx
|
||||
@@ -39,12 +41,18 @@ OSD_FileIterator.cxx
|
||||
OSD_FileIterator.hxx
|
||||
OSD_FileNode.cxx
|
||||
OSD_FileNode.hxx
|
||||
OSD_FileSystem.cxx
|
||||
OSD_FileSystem.hxx
|
||||
OSD_FileSystemSelector.cxx
|
||||
OSD_FileSystemSelector.hxx
|
||||
OSD_FromWhere.hxx
|
||||
OSD_Function.hxx
|
||||
OSD_Host.cxx
|
||||
OSD_Host.hxx
|
||||
OSD_KindFile.hxx
|
||||
OSD_LoadMode.hxx
|
||||
OSD_LocalFileSystem.cxx
|
||||
OSD_LocalFileSystem.hxx
|
||||
OSD_LockType.hxx
|
||||
OSD_MAllocHook.cxx
|
||||
OSD_MAllocHook.hxx
|
||||
@@ -82,6 +90,7 @@ OSD_SIGQUIT.hxx
|
||||
OSD_SIGSEGV.hxx
|
||||
OSD_SIGSYS.hxx
|
||||
OSD_SingleProtection.hxx
|
||||
OSD_StreamBuffer.hxx
|
||||
OSD_SysType.hxx
|
||||
OSD_Thread.cxx
|
||||
OSD_Thread.hxx
|
||||
|
71
src/OSD/OSD_CachedFileSystem.cxx
Normal file
71
src/OSD/OSD_CachedFileSystem.cxx
Normal file
@@ -0,0 +1,71 @@
|
||||
// Copyright (c) 2021 OPEN CASCADE SAS
|
||||
//
|
||||
// This file is part of Open CASCADE Technology software library.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under
|
||||
// the terms of the GNU Lesser General Public License version 2.1 as published
|
||||
// by the Free Software Foundation, with special exception defined in the file
|
||||
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
|
||||
// distribution for complete text of the license and disclaimer of any warranty.
|
||||
//
|
||||
// Alternatively, this file may be used under the terms of Open CASCADE
|
||||
// commercial license or contractual agreement.
|
||||
|
||||
#include <OSD_CachedFileSystem.hxx>
|
||||
#include <OSD_OpenFile.hxx>
|
||||
|
||||
IMPLEMENT_STANDARD_RTTIEXT(OSD_CachedFileSystem, OSD_FileSystem)
|
||||
|
||||
//=======================================================================
|
||||
// function : IsSupportedPath
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
Standard_Boolean OSD_CachedFileSystem::IsSupportedPath (const TCollection_AsciiString& theUrl) const
|
||||
{
|
||||
return OSD_FileSystem::DefaultFileSystem()->IsSupportedPath (theUrl);
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : IsOpenIStream
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
Standard_Boolean OSD_CachedFileSystem::IsOpenIStream (const opencascade::std::shared_ptr<std::istream>& theStream) const
|
||||
{
|
||||
return OSD_FileSystem::DefaultFileSystem()->IsOpenIStream (theStream);
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : OpenIStream
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
opencascade::std::shared_ptr<std::istream> OSD_CachedFileSystem::OpenIStream (const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theParams,
|
||||
const int64_t theOffset,
|
||||
const opencascade::std::shared_ptr<std::istream>& /*theOldStream*/)
|
||||
{
|
||||
if (myStream.Url != theUrl)
|
||||
{
|
||||
myStream.Url = theUrl;
|
||||
myStream.Reset();
|
||||
}
|
||||
myStream.Stream = OSD_FileSystem::DefaultFileSystem()->OpenIStream (theUrl, theParams, theOffset, myStream.Stream);
|
||||
return myStream.Stream;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : OpenStreamBuffer
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
opencascade::std::shared_ptr<std::streambuf> OSD_CachedFileSystem::OpenStreamBuffer (const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theMode,
|
||||
const int64_t theOffset,
|
||||
int64_t* theOutBufSize)
|
||||
{
|
||||
if (myStream.Url != theUrl)
|
||||
{
|
||||
myStream.Url = theUrl;
|
||||
myStream.Reset();
|
||||
}
|
||||
myStream.StreamBuf = OSD_FileSystem::DefaultFileSystem()->OpenStreamBuffer (theUrl, theMode, theOffset, theOutBufSize);
|
||||
return myStream.StreamBuf;
|
||||
}
|
73
src/OSD/OSD_CachedFileSystem.hxx
Normal file
73
src/OSD/OSD_CachedFileSystem.hxx
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright (c) 2021 OPEN CASCADE SAS
|
||||
//
|
||||
// This file is part of Open CASCADE Technology software library.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under
|
||||
// the terms of the GNU Lesser General Public License version 2.1 as published
|
||||
// by the Free Software Foundation, with special exception defined in the file
|
||||
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
|
||||
// distribution for complete text of the license and disclaimer of any warranty.
|
||||
//
|
||||
// Alternatively, this file may be used under the terms of Open CASCADE
|
||||
// commercial license or contractual agreement.
|
||||
|
||||
#ifndef _OSD_CachedFileSystem_HeaderFile
|
||||
#define _OSD_CachedFileSystem_HeaderFile
|
||||
|
||||
#include <OSD_FileSystem.hxx>
|
||||
|
||||
//! File system keeping last stream created by OSD_FileSystem::DefaultFileSystem() to be reused for opening a stream with the same URL.
|
||||
//! Note that as file is kept in opened state, application will need destroying this object to ensure all files being closed.
|
||||
//! This interface could be handy in context of reading numerous objects pointing to the same file (at different offset).
|
||||
//! Make sure to create a dedicated OSD_CachedFileSystem for each working thread to avoid data races.
|
||||
class OSD_CachedFileSystem : public OSD_FileSystem
|
||||
{
|
||||
DEFINE_STANDARD_RTTIEXT(OSD_CachedFileSystem, OSD_FileSystem)
|
||||
public:
|
||||
|
||||
//! Constructor.
|
||||
OSD_CachedFileSystem() {}
|
||||
|
||||
//! Returns TRUE if URL defines a supported protocol.
|
||||
Standard_EXPORT virtual Standard_Boolean IsSupportedPath (const TCollection_AsciiString& theUrl) const Standard_OVERRIDE;
|
||||
|
||||
//! Returns TRUE if current input stream is opened for reading operations.
|
||||
Standard_EXPORT virtual Standard_Boolean IsOpenIStream (const opencascade::std::shared_ptr<std::istream>& 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<std::istream> OpenIStream
|
||||
(const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theParams,
|
||||
const int64_t theOffset,
|
||||
const opencascade::std::shared_ptr<std::istream>& theOldStream) Standard_OVERRIDE;
|
||||
|
||||
//! Opens stream buffer for specified file URL.
|
||||
Standard_EXPORT virtual opencascade::std::shared_ptr<std::streambuf> OpenStreamBuffer
|
||||
(const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theMode,
|
||||
const int64_t theOffset = 0,
|
||||
int64_t* theOutBufSize = NULL) Standard_OVERRIDE;
|
||||
|
||||
protected:
|
||||
|
||||
// Auxiliary structure to save shared stream with path to it.
|
||||
struct OSD_CachedStream
|
||||
{
|
||||
TCollection_AsciiString Url;
|
||||
opencascade::std::shared_ptr<std::istream> Stream;
|
||||
opencascade::std::shared_ptr<std::streambuf> StreamBuf;
|
||||
|
||||
void Reset()
|
||||
{
|
||||
Stream.reset();
|
||||
StreamBuf.reset();
|
||||
}
|
||||
};
|
||||
|
||||
protected:
|
||||
|
||||
OSD_CachedStream myStream;
|
||||
|
||||
};
|
||||
|
||||
#endif // _OSD_CachedFileSystem_HeaderFile
|
100
src/OSD/OSD_FileSystem.cxx
Normal file
100
src/OSD/OSD_FileSystem.cxx
Normal file
@@ -0,0 +1,100 @@
|
||||
// Copyright (c) 2021 OPEN CASCADE SAS
|
||||
//
|
||||
// This file is part of Open CASCADE Technology software library.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under
|
||||
// the terms of the GNU Lesser General Public License version 2.1 as published
|
||||
// by the Free Software Foundation, with special exception defined in the file
|
||||
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
|
||||
// distribution for complete text of the license and disclaimer of any warranty.
|
||||
//
|
||||
// Alternatively, this file may be used under the terms of Open CASCADE
|
||||
// commercial license or contractual agreement.
|
||||
|
||||
#include <OSD_FileSystem.hxx>
|
||||
#include <OSD_FileSystemSelector.hxx>
|
||||
#include <OSD_LocalFileSystem.hxx>
|
||||
|
||||
IMPLEMENT_STANDARD_RTTIEXT(OSD_FileSystem, Standard_Transient)
|
||||
|
||||
//=======================================================================
|
||||
// function : createDefaultFileSystem
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
static Handle(OSD_FileSystem) createDefaultFileSystem()
|
||||
{
|
||||
Handle(OSD_FileSystemSelector) aSystem = new OSD_FileSystemSelector();
|
||||
aSystem->AddProtocol (new OSD_LocalFileSystem());
|
||||
return aSystem;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : OSD_FileSystem
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
OSD_FileSystem::OSD_FileSystem()
|
||||
{
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : ~OSD_FileSystem
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
OSD_FileSystem::~OSD_FileSystem()
|
||||
{
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : DefaultFileSystem
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
const Handle(OSD_FileSystem)& OSD_FileSystem::DefaultFileSystem()
|
||||
{
|
||||
static const Handle(OSD_FileSystem) aDefSystem = createDefaultFileSystem();
|
||||
return aDefSystem;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : openIStream
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
opencascade::std::shared_ptr<std::istream> OSD_FileSystem::OpenIStream (const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theMode,
|
||||
const int64_t theOffset,
|
||||
const opencascade::std::shared_ptr<std::istream>& theOldStream)
|
||||
{
|
||||
Standard_ASSERT_RAISE (theOffset >= -1, "Incorrect negative stream position during stream opening");
|
||||
|
||||
opencascade::std::shared_ptr<std::istream> aNewStream;
|
||||
opencascade::std::shared_ptr<OSD_IStreamBuffer> anOldStream = opencascade::std::dynamic_pointer_cast<OSD_IStreamBuffer> (theOldStream);
|
||||
if (anOldStream.get() != NULL
|
||||
&& theUrl.IsEqual (anOldStream->Url().c_str())
|
||||
&& IsOpenIStream (anOldStream))
|
||||
{
|
||||
if (!anOldStream->good())
|
||||
{
|
||||
// Reset flags without re-opening
|
||||
anOldStream->clear();
|
||||
}
|
||||
aNewStream = anOldStream;
|
||||
if (theOffset >= 0)
|
||||
{
|
||||
aNewStream->seekg ((std::streamoff )theOffset, std::ios_base::beg);
|
||||
}
|
||||
}
|
||||
if (aNewStream.get() == NULL)
|
||||
{
|
||||
opencascade::std::shared_ptr<std::streambuf> aFileBuf = OpenStreamBuffer (theUrl, theMode | std::ios_base::in);
|
||||
if (aFileBuf.get() == NULL)
|
||||
{
|
||||
return opencascade::std::shared_ptr<std::istream>();
|
||||
}
|
||||
|
||||
aNewStream.reset (new OSD_IStreamBuffer (theUrl.ToCString(), aFileBuf));
|
||||
if (theOffset > 0)
|
||||
{
|
||||
aNewStream->seekg ((std::streamoff )theOffset, std::ios_base::beg);
|
||||
}
|
||||
}
|
||||
return aNewStream;
|
||||
}
|
69
src/OSD/OSD_FileSystem.hxx
Normal file
69
src/OSD/OSD_FileSystem.hxx
Normal file
@@ -0,0 +1,69 @@
|
||||
// Copyright (c) 2021 OPEN CASCADE SAS
|
||||
//
|
||||
// This file is part of Open CASCADE Technology software library.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under
|
||||
// the terms of the GNU Lesser General Public License version 2.1 as published
|
||||
// by the Free Software Foundation, with special exception defined in the file
|
||||
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
|
||||
// distribution for complete text of the license and disclaimer of any warranty.
|
||||
//
|
||||
// Alternatively, this file may be used under the terms of Open CASCADE
|
||||
// commercial license or contractual agreement.
|
||||
|
||||
#ifndef _OSD_FileSystem_HeaderFile
|
||||
#define _OSD_FileSystem_HeaderFile
|
||||
|
||||
#include <OSD_StreamBuffer.hxx>
|
||||
#include <TCollection_AsciiString.hxx>
|
||||
|
||||
//! Base interface for a file stream provider.
|
||||
//! It is intended to be implemented for specific file protocol.
|
||||
class OSD_FileSystem : public Standard_Transient
|
||||
{
|
||||
DEFINE_STANDARD_RTTIEXT(OSD_FileSystem, Standard_Transient)
|
||||
public:
|
||||
|
||||
//! Returns a global file system, which a selector between registered file systems.
|
||||
Standard_EXPORT static const Handle(OSD_FileSystem)& DefaultFileSystem();
|
||||
|
||||
public:
|
||||
|
||||
//! Returns TRUE if URL defines a supported protocol.
|
||||
virtual Standard_Boolean IsSupportedPath (const TCollection_AsciiString& theUrl) const = 0;
|
||||
|
||||
//! Returns TRUE if current input stream is opened for reading operations.
|
||||
virtual Standard_Boolean IsOpenIStream (const opencascade::std::shared_ptr<std::istream>& 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
|
||||
//! @param theMode [in] flags describing the requested input mode for the stream (std::ios_base::in will be implicitly added)
|
||||
//! @param theOffset [in] expected stream position from the begining of the file (beginning of the stream by default);
|
||||
//! -1 would keep seek position undefined (in case of re-using theOldStream)
|
||||
//! @param theOldStream [in] a pointer to existing stream pointing to theUrl to be reused (without re-opening)
|
||||
//! @return pointer to newly created opened stream, to theOldStream if it can be reused or NULL in case of failure.
|
||||
Standard_EXPORT virtual opencascade::std::shared_ptr<std::istream> OpenIStream
|
||||
(const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theMode,
|
||||
const int64_t theOffset = 0,
|
||||
const opencascade::std::shared_ptr<std::istream>& theOldStream = opencascade::std::shared_ptr<std::istream>());
|
||||
|
||||
//! 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
|
||||
//! @param theOffset [in] expected stream position from the begining of the buffer (beginning of the stream buffer by default)
|
||||
//! @param theOutBufSize [out] total buffer size (only if buffer is opened for read)
|
||||
//! @return pointer to newly created opened stream buffer or NULL in case of failure.
|
||||
virtual opencascade::std::shared_ptr<std::streambuf> OpenStreamBuffer (const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theMode,
|
||||
const int64_t theOffset = 0,
|
||||
int64_t* theOutBufSize = NULL) = 0;
|
||||
|
||||
//! Constructor.
|
||||
Standard_EXPORT OSD_FileSystem();
|
||||
|
||||
//! Destructor.
|
||||
Standard_EXPORT virtual ~OSD_FileSystem();
|
||||
};
|
||||
#endif // _OSD_FileSystem_HeaderFile
|
132
src/OSD/OSD_FileSystemSelector.cxx
Normal file
132
src/OSD/OSD_FileSystemSelector.cxx
Normal file
@@ -0,0 +1,132 @@
|
||||
// Copyright (c) 2021 OPEN CASCADE SAS
|
||||
//
|
||||
// This file is part of Open CASCADE Technology software library.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under
|
||||
// the terms of the GNU Lesser General Public License version 2.1 as published
|
||||
// by the Free Software Foundation, with special exception defined in the file
|
||||
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
|
||||
// distribution for complete text of the license and disclaimer of any warranty.
|
||||
//
|
||||
// Alternatively, this file may be used under the terms of Open CASCADE
|
||||
// commercial license or contractual agreement.
|
||||
|
||||
#include <OSD_FileSystemSelector.hxx>
|
||||
|
||||
IMPLEMENT_STANDARD_RTTIEXT(OSD_FileSystemSelector, OSD_FileSystem)
|
||||
|
||||
//=======================================================================
|
||||
// function : AddProtocol
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
void OSD_FileSystemSelector::AddProtocol (const Handle(OSD_FileSystem)& theFileSystem, bool theIsPreferred)
|
||||
{
|
||||
myProtocols.Remove (theFileSystem); // avoid duplicates
|
||||
if (theIsPreferred)
|
||||
{
|
||||
myProtocols.Prepend (theFileSystem);
|
||||
}
|
||||
else
|
||||
{
|
||||
myProtocols.Append (theFileSystem);
|
||||
}
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : RemoveProtocol
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
void OSD_FileSystemSelector::RemoveProtocol (const Handle(OSD_FileSystem)& theFileSystem)
|
||||
{
|
||||
myProtocols.Remove (theFileSystem);
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : IsSupportedPath
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
Standard_Boolean OSD_FileSystemSelector::IsSupportedPath (const TCollection_AsciiString& theUrl) const
|
||||
{
|
||||
for (NCollection_List<Handle(OSD_FileSystem)>::Iterator aProtIter(myProtocols); aProtIter.More(); aProtIter.Next())
|
||||
{
|
||||
if (aProtIter.Value()->IsSupportedPath (theUrl))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : IsOpenIStream
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
Standard_Boolean OSD_FileSystemSelector::IsOpenIStream (const opencascade::std::shared_ptr<std::istream>& theStream) const
|
||||
{
|
||||
opencascade::std::shared_ptr<OSD_IStreamBuffer> aFileStream = opencascade::std::dynamic_pointer_cast<OSD_IStreamBuffer> (theStream);
|
||||
if (aFileStream.get() == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
for (NCollection_List<Handle(OSD_FileSystem)>::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->IsOpenIStream (theStream))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : OpenIStream
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
opencascade::std::shared_ptr<std::istream> OSD_FileSystemSelector::OpenIStream (const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theMode,
|
||||
const int64_t theOffset,
|
||||
const opencascade::std::shared_ptr<std::istream>& theOldStream)
|
||||
{
|
||||
for (NCollection_List<Handle(OSD_FileSystem)>::Iterator aProtIter (myProtocols); aProtIter.More(); aProtIter.Next())
|
||||
{
|
||||
const Handle(OSD_FileSystem)& aFileSystem = aProtIter.Value();
|
||||
if (aFileSystem->IsSupportedPath (theUrl))
|
||||
{
|
||||
opencascade::std::shared_ptr<std::istream> aStream = aFileSystem->OpenIStream (theUrl, theMode, theOffset, theOldStream);
|
||||
if (aStream.get() != NULL)
|
||||
{
|
||||
return aStream;
|
||||
}
|
||||
}
|
||||
}
|
||||
return opencascade::std::shared_ptr<std::istream>();
|
||||
}
|
||||
|
||||
|
||||
//=======================================================================
|
||||
// function : OpenStreamBuffer
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
opencascade::std::shared_ptr<std::streambuf> OSD_FileSystemSelector::OpenStreamBuffer (const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theMode,
|
||||
const int64_t theOffset,
|
||||
int64_t* theOutBufSize)
|
||||
{
|
||||
for (NCollection_List<Handle(OSD_FileSystem)>::Iterator aProtIter (myProtocols); aProtIter.More(); aProtIter.Next())
|
||||
{
|
||||
const Handle(OSD_FileSystem)& aFileSystem = aProtIter.Value();
|
||||
if (aFileSystem->IsSupportedPath (theUrl))
|
||||
{
|
||||
opencascade::std::shared_ptr<std::streambuf> aBuf = aFileSystem->OpenStreamBuffer (theUrl, theMode, theOffset, theOutBufSize);
|
||||
if (aBuf.get() != NULL)
|
||||
{
|
||||
return aBuf;
|
||||
}
|
||||
}
|
||||
}
|
||||
return opencascade::std::shared_ptr<std::streambuf>();
|
||||
}
|
66
src/OSD/OSD_FileSystemSelector.hxx
Normal file
66
src/OSD/OSD_FileSystemSelector.hxx
Normal file
@@ -0,0 +1,66 @@
|
||||
// Copyright (c) 2021 OPEN CASCADE SAS
|
||||
//
|
||||
// This file is part of Open CASCADE Technology software library.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under
|
||||
// the terms of the GNU Lesser General Public License version 2.1 as published
|
||||
// by the Free Software Foundation, with special exception defined in the file
|
||||
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
|
||||
// distribution for complete text of the license and disclaimer of any warranty.
|
||||
//
|
||||
// Alternatively, this file may be used under the terms of Open CASCADE
|
||||
// commercial license or contractual agreement.
|
||||
|
||||
#ifndef _OSD_FileSystemSelector_HeaderFile
|
||||
#define _OSD_FileSystemSelector_HeaderFile
|
||||
|
||||
#include <OSD_FileSystem.hxx>
|
||||
|
||||
#include <NCollection_List.hxx>
|
||||
|
||||
//! File system implementation which tried to open stream using registered list of file systems.
|
||||
class OSD_FileSystemSelector : public OSD_FileSystem
|
||||
{
|
||||
DEFINE_STANDARD_RTTIEXT(OSD_FileSystemSelector, OSD_FileSystem)
|
||||
public:
|
||||
|
||||
//! Constructor.
|
||||
OSD_FileSystemSelector() {}
|
||||
|
||||
//! Registers file system within the global file system selector returned by OSD_FileSystem::DefaultFileSystem().
|
||||
//! @param theFileSystem [in] file system to register
|
||||
//! @param theIsPreferred [in] add to the beginning of the list when TRUE, or add to the end otherwise
|
||||
Standard_EXPORT void AddProtocol (const Handle(OSD_FileSystem)& theFileSystem, bool theIsPreferred = false);
|
||||
|
||||
//! Unregisters file system within the global file system selector returned by OSD_FileSystem::DefaultFileSystem().
|
||||
Standard_EXPORT void RemoveProtocol (const Handle(OSD_FileSystem)& theFileSystem);
|
||||
|
||||
public:
|
||||
|
||||
//! Returns TRUE if URL defines a supported protocol.
|
||||
Standard_EXPORT virtual bool IsSupportedPath (const TCollection_AsciiString& theUrl) const Standard_OVERRIDE;
|
||||
|
||||
//! Returns TRUE if current input stream is opened for reading operations.
|
||||
Standard_EXPORT virtual Standard_Boolean IsOpenIStream (const opencascade::std::shared_ptr<std::istream>& theStream) const Standard_OVERRIDE;
|
||||
|
||||
//! Opens input stream using one of registered protocols.
|
||||
Standard_EXPORT virtual opencascade::std::shared_ptr<std::istream> OpenIStream
|
||||
(const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theMode,
|
||||
const int64_t theOffset = 0,
|
||||
const opencascade::std::shared_ptr<std::istream>& theOldStream = opencascade::std::shared_ptr<std::istream>()) Standard_OVERRIDE;
|
||||
|
||||
//! Opens stream buffer using one of registered protocols.
|
||||
Standard_EXPORT virtual opencascade::std::shared_ptr<std::streambuf> OpenStreamBuffer
|
||||
(const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theMode,
|
||||
const int64_t theOffset = 0,
|
||||
int64_t* theOutBufSize = NULL) Standard_OVERRIDE;
|
||||
|
||||
protected:
|
||||
|
||||
NCollection_List<Handle(OSD_FileSystem)> myProtocols;
|
||||
|
||||
};
|
||||
|
||||
#endif // _OSD_FileSystemSelector_HeaderFile
|
76
src/OSD/OSD_LocalFileSystem.cxx
Normal file
76
src/OSD/OSD_LocalFileSystem.cxx
Normal file
@@ -0,0 +1,76 @@
|
||||
// Copyright (c) 2021 OPEN CASCADE SAS
|
||||
//
|
||||
// This file is part of Open CASCADE Technology software library.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under
|
||||
// the terms of the GNU Lesser General Public License version 2.1 as published
|
||||
// by the Free Software Foundation, with special exception defined in the file
|
||||
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
|
||||
// distribution for complete text of the license and disclaimer of any warranty.
|
||||
//
|
||||
// Alternatively, this file may be used under the terms of Open CASCADE
|
||||
// commercial license or contractual agreement.
|
||||
|
||||
#include <OSD_LocalFileSystem.hxx>
|
||||
#include <OSD_OpenFile.hxx>
|
||||
#include <OSD_Path.hxx>
|
||||
#include <Standard_Assert.hxx>
|
||||
|
||||
IMPLEMENT_STANDARD_RTTIEXT(OSD_LocalFileSystem, OSD_FileSystem)
|
||||
|
||||
//=======================================================================
|
||||
// function : IsSupportedPath
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
Standard_Boolean OSD_LocalFileSystem::IsSupportedPath (const TCollection_AsciiString& theUrl) const
|
||||
{
|
||||
return !OSD_Path::IsRemoteProtocolPath (theUrl.ToCString());
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : IsOpenIStream
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
Standard_Boolean OSD_LocalFileSystem::IsOpenIStream (const opencascade::std::shared_ptr<std::istream>& theStream) const
|
||||
{
|
||||
opencascade::std::shared_ptr<OSD_IStreamBuffer> aFileStream = opencascade::std::dynamic_pointer_cast<OSD_IStreamBuffer> (theStream);
|
||||
if (aFileStream.get() == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
const std::filebuf* aFileBuf = dynamic_cast<const std::filebuf*> (aFileStream->rdbuf());
|
||||
return (aFileBuf != NULL) ? aFileBuf->is_open() : false;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
// function : OpenStreamBuffer
|
||||
// purpose :
|
||||
//=======================================================================
|
||||
opencascade::std::shared_ptr<std::streambuf> OSD_LocalFileSystem::OpenStreamBuffer (const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theMode,
|
||||
const int64_t theOffset,
|
||||
int64_t* theOutBufSize)
|
||||
{
|
||||
Standard_ASSERT_RAISE (theOffset >= 0, "Incorrect negative stream position during stream buffer opening");
|
||||
opencascade::std::shared_ptr<std::filebuf> aNewBuf(new std::filebuf());
|
||||
if (!OSD_OpenStream (*aNewBuf, TCollection_ExtendedString(theUrl), theMode))
|
||||
{
|
||||
return opencascade::std::shared_ptr<std::streambuf>();
|
||||
}
|
||||
// if buffer is opened for read, find the file size
|
||||
if (theOutBufSize && ((theMode & std::ios::in) != 0))
|
||||
{
|
||||
*theOutBufSize = (int64_t )aNewBuf->pubseekoff (0, std::ios_base::end, std::ios_base::in);
|
||||
if (aNewBuf->pubseekoff ((std::streamoff )theOffset, std::ios_base::beg, std::ios_base::in) < 0)
|
||||
{
|
||||
*theOutBufSize = 0;
|
||||
return opencascade::std::shared_ptr<std::streambuf>();
|
||||
}
|
||||
}
|
||||
else if (theOffset > 0 && aNewBuf->pubseekoff ((std::streamoff )theOffset, std::ios_base::beg,
|
||||
(theMode & std::ios::in) != 0 ? std::ios_base::in : std::ios_base::out) < 0)
|
||||
{
|
||||
return opencascade::std::shared_ptr<std::streambuf>();
|
||||
}
|
||||
return aNewBuf;
|
||||
}
|
41
src/OSD/OSD_LocalFileSystem.hxx
Normal file
41
src/OSD/OSD_LocalFileSystem.hxx
Normal file
@@ -0,0 +1,41 @@
|
||||
// Copyright (c) 2021 OPEN CASCADE SAS
|
||||
//
|
||||
// This file is part of Open CASCADE Technology software library.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under
|
||||
// the terms of the GNU Lesser General Public License version 2.1 as published
|
||||
// by the Free Software Foundation, with special exception defined in the file
|
||||
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
|
||||
// distribution for complete text of the license and disclaimer of any warranty.
|
||||
//
|
||||
// Alternatively, this file may be used under the terms of Open CASCADE
|
||||
// commercial license or contractual agreement.
|
||||
|
||||
#ifndef _OSD_LocalFileSystem_HeaderFile
|
||||
#define _OSD_LocalFileSystem_HeaderFile
|
||||
|
||||
#include <OSD_FileSystem.hxx>
|
||||
|
||||
//! A file system opening local files (or files from mount systems).
|
||||
class OSD_LocalFileSystem : public OSD_FileSystem
|
||||
{
|
||||
DEFINE_STANDARD_RTTIEXT(OSD_LocalFileSystem, OSD_FileSystem)
|
||||
public:
|
||||
|
||||
//! Constructor.
|
||||
OSD_LocalFileSystem() {}
|
||||
|
||||
//! Returns TRUE if URL defines a supported protocol.
|
||||
Standard_EXPORT virtual Standard_Boolean IsSupportedPath (const TCollection_AsciiString& theUrl) const Standard_OVERRIDE;
|
||||
|
||||
//! Returns TRUE if current input stream is opened for reading operations.
|
||||
Standard_EXPORT virtual Standard_Boolean IsOpenIStream (const opencascade::std::shared_ptr<std::istream>& theStream) const Standard_OVERRIDE;
|
||||
|
||||
//! Opens stream buffer for specified file URL.
|
||||
Standard_EXPORT virtual opencascade::std::shared_ptr<std::streambuf> OpenStreamBuffer
|
||||
(const TCollection_AsciiString& theUrl,
|
||||
const std::ios_base::openmode theMode,
|
||||
const int64_t theOffset = 0,
|
||||
int64_t* theOutBufSize = NULL) Standard_OVERRIDE;
|
||||
};
|
||||
#endif // _OSD_LocalFileSystem_HeaderFile
|
48
src/OSD/OSD_StreamBuffer.hxx
Normal file
48
src/OSD/OSD_StreamBuffer.hxx
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright (c) 2021 OPEN CASCADE SAS
|
||||
//
|
||||
// This file is part of Open CASCADE Technology software library.
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it under
|
||||
// the terms of the GNU Lesser General Public License version 2.1 as published
|
||||
// by the Free Software Foundation, with special exception defined in the file
|
||||
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
|
||||
// distribution for complete text of the license and disclaimer of any warranty.
|
||||
//
|
||||
// Alternatively, this file may be used under the terms of Open CASCADE
|
||||
// commercial license or contractual agreement.
|
||||
|
||||
#ifndef _OSD_StreamBuffer_HeaderFile
|
||||
#define _OSD_StreamBuffer_HeaderFile
|
||||
|
||||
#include <Standard_Std.hxx>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
//! A file stream implementation initialized from std::shared_ptr<std::streambuf>.
|
||||
template <typename T>
|
||||
class OSD_StreamBuffer : public T
|
||||
{
|
||||
public:
|
||||
|
||||
//! Main constructor.
|
||||
OSD_StreamBuffer (const std::string& theUrl,
|
||||
const opencascade::std::shared_ptr<std::streambuf>& theBuffer)
|
||||
: T (theBuffer.get()), myUrl (theUrl), myBuffer (theBuffer) {}
|
||||
|
||||
//! Return an opened URL.
|
||||
const std::string& Url() const { return myUrl; }
|
||||
|
||||
protected:
|
||||
|
||||
std::string myUrl;
|
||||
opencascade::std::shared_ptr<std::streambuf> myBuffer;
|
||||
};
|
||||
|
||||
typedef OSD_StreamBuffer<std::istream> OSD_IStreamBuffer;
|
||||
typedef OSD_StreamBuffer<std::ostream> OSD_OStreamBuffer;
|
||||
typedef OSD_StreamBuffer<std::iostream> OSD_IOStreamBuffer;
|
||||
|
||||
#endif // _OSD_StreamBuffer_HeaderFile
|
Reference in New Issue
Block a user