28#if !defined(_MEMORYFILESTREAMIMPL_H_INCLUDED_)
29#define _MEMORYFILESTREAMIMPL_H_INCLUDED_
36#ifdef OD_HAVE_UNISTD_FILE
40#ifdef OD_HAVE_WINDOWS_STORAGE_FILE
41#include <windows.storage.h>
44#ifdef OD_HAVE_WRL_FILE
45#include <wrl/client.h>
46#include <wrl/wrappers/corewrappers.h>
56#define TEMPFILE_PREFIX "tx$"
66 if (CoCreateGuid(&result) < 0)
68 tmpDir = tmpDir.
format(L
"%ls%llx%x%x.tmp", tmpDir.
c_str(), result.Data1, result.Data2, result.Data3);
72 ::GetTempFileName(tmpDir.
c_str(), !prefix.isEmpty() ?
OdString(prefix).c_str() : L
"tmp", 0, tmpFile);
83 strcat(tmpFile, (
const char*)dstBuf.
asArrayPtr());
85 strcat(tmpFile, !prefix.isEmpty() ? prefix.c_str() :
"tmp");
86#ifdef OD_HAVE_MKSTEMPS_FUNC
87 strcat(tmpFile,
"XXXXXX.tmp");
88 int fileDescriptor = mkstemps(tmpFile, 4);
90 strcat(tmpFile,
"XXXXXX");
91 int fileDescriptor = mkstemp(tmpFile);
93 if (fileDescriptor == -1)
98 close(fileDescriptor);
100 OdAnsiString ansiTmpFile(tmpFile);
146template <
class TBase = OdMemFileStreamDummyBase>
174 bool releaseDiskFile(
bool deleteIt)
177 if (deleteIt && !m_sTmpFileName.
isEmpty()) {
178#ifdef OD_HAVE_REMOVE_FUNC
179 remove(m_sTmpFileName);
181 DeleteFile(
OdString(m_sTmpFileName).c_str());
184 m_sTmpFileName.
empty();
204 releaseDiskFile(
true);
207 void setAppServices(OdDbBaseHostAppServices* pHostAppServices)
209 m_pHostAppServices = pHostAppServices;
217 inline void setBufferSize(
OdUInt32 pageSize) { m_dataBuffer.resize(pageSize); };
224 inline OdUInt8* dataBuffer(PAGE* pPage) {
return m_dataBuffer.asArrayPtr();};
231 inline OdUInt32 dataSize(PAGE* pPage) {
return m_dataBuffer.length(); };
236 inline PAGE* activePage()
const {
return m_activePage; };
243 inline void setActivePage(PAGE* pPage) { m_activePage = pPage; };
251 inline bool isActivePage(PAGE* pPage)
const {
return (pPage == m_activePage); };
259 inline bool isModified(PAGE* pPage)
const {
return pPage->
m_modified; };
267 inline void setModified(PAGE* pPage,
bool modified) { pPage->
m_modified = modified; };
275 inline bool isSaved(PAGE* pPage)
const {
return pPage->
m_savedToFile; };
283 inline void setSaved(PAGE* pPage,
bool saved) { pPage->
m_savedToFile = saved; };
290 inline OdUInt64 filePosition(PAGE* pPage)
const {
return pPage->
m_nFilePos; };
298 inline void setFilePosition(PAGE* pPage, OdUInt64 seekPos) { pPage->
m_nFilePos = seekPos; };
305 void initializePage(PAGE* pPage)
310 pPage->
m_data = dataBuffer(pPage);
311 setModified(pPage,
false);
312 setSaved(pPage,
false);
313 setFilePosition(pPage, 0);
321 if (!m_pTmpFile.isNull())
325 if (m_pHostAppServices)
327 m_sTmpFileName = TmpFileHelper::getTempFile(m_pHostAppServices);
336 throw OdError(eNullObjectPointer);
345 bool savePage(PAGE* pPage)
347 if (pPage && isSaved(pPage) && !isModified(pPage))
350 if (!pPage || diskFile().isNull())
360 setFilePosition(pPage, diskFile()->
tell());
362 diskFile()->putBytes(dataBuffer(pPage), dataSize(pPage));
364 setModified(pPage,
false);
365 setSaved(pPage,
true);
375 bool loadPage(PAGE* pPage)
379 if (isActivePage(pPage) && !isModified(pPage))
382 if (isSaved(pPage) && !diskFile().isNull())
385 diskFile()->getBytes(dataBuffer(pPage), dataSize(pPage));
388 setModified(pPage,
false);
389 setActivePage(pPage);
398 bool ensurePage(PAGE* pPage)
401 return isActivePage(pPage) ?
true : switchToPage(pPage);
411 bool switchToPage(PAGE* pPage)
414 if (isActivePage(pPage))
416 savePage(activePage());
417 return loadPage(pPage);
429 return isActivePage(pPage) ?
true : savePage(activePage());
440 bool wasSaved = isSaved(pPage);
441 setModified(pPage,
false);
442 setSaved(pPage,
false);
443 if (!wasSaved || diskFile().isNull())
449 diskFile()->truncate();
454 PageSwitcher m_pageSwitcher;
494 if(m_pCurrPage->m_pPrevPage)
498 m_pCurrPage = m_pCurrPage->m_pPrevPage;
628 m_pCurrPage = m_pFirstPage;
645 m_pLastPage = m_pCurrPage;
670template <
class TBase>
673 PAGE* pNext, *pCurr = m_pFirstPage;
688template <
class TBase>
700 m_pLastPage->m_pNextPage = pPage;
710 m_pFirstPage = m_pCurrPage = pPage;
718template <
class TBase>
728 if(!m_pCurrPage->m_pNextPage)
731 if(m_pCurrPage->m_pNextPage)
733 m_pCurrPage = m_pCurrPage->m_pNextPage;
741template <
class TBase>
773 bool bForward =
false;
777 if (nNewPos >= m_pCurrPage->m_nPageStartAddr)
789 m_pCurrPage = m_pLastPage;
794 if (nFromEnd < ((
OdInt64)nNewPos - (
OdInt64)m_pCurrPage->m_nPageStartAddr))
796 m_pCurrPage = m_pLastPage;
802 if (nNewPos < (m_pCurrPage->m_nPageStartAddr - nNewPos))
804 m_pCurrPage = m_pFirstPage;
812 m_pCurrPage = m_pFirstPage;
817 m_pCurrPage = m_pLastPage;
825 while(m_pCurrPage && m_pCurrPage->m_nPageStartAddr < nNewPos)
827 m_pCurrPage = m_pCurrPage->m_pNextPage;
832 while(m_pCurrPage->m_nPageStartAddr >
m_nCurPos)
834 m_pCurrPage = m_pCurrPage->m_pPrevPage;
842template <
class TBase>
850 ret = m_pCurrPage->m_data[nPosInPage];
853 m_pCurrPage = m_pCurrPage->m_pNextPage;
862template <
class TBase>
876 ::memcpy(pDest,
currPos(), nToCopy);
884 ::memcpy(pDest, m_pCurrPage->m_data, nToCopy);
890 m_pCurrPage = m_pCurrPage->m_pNextPage;
898template <
class TBase>
908 m_pCurrPage->m_data[nPosInPage] = val;
913 m_pCurrPage = m_pCurrPage->m_pNextPage;
917template <
class TBase>
930 ::memcpy(
currPos(), pSrc, nToCopy);
939 ::memcpy(m_pCurrPage->m_data, pSrc, nToCopy);
946 m_pCurrPage = m_pCurrPage->m_pNextPage;
OdSmartPtr< OdStreamBuf > OdStreamBufPtr
ALLOCDLL_EXPORT void * odrxAlloc(size_t nBytes)
ALLOCDLL_EXPORT void odrxFree(void *pMemBlock)
OdArray< char, OdMemoryAllocator< char > > OdAnsiCharArray
OdArray< OdChar, OdMemoryAllocator< OdChar > > OdCharArray
FIRSTDLL_EXPORT OdRxSystemServices * odrxSystemServices()
const T * asArrayPtr() const
void reserve(size_type reserveLength)
static void unicodeToUtf8(const OdChar *srcBuf, int srcSize, OdAnsiCharArray &dstBuf)
static bool utf8ToUnicode(const char *srcBuf, int srcSize, OdCharArray &dstBuf)
virtual OdString getTempPath() const
OdUInt64 seek(OdInt64 offset, OdDb::FilerSeekType whence)
void reserve(OdUInt64 nSize)
void putByte(OdUInt8 val)
void putBytes(const void *buffer, OdUInt32 nLen)
void setPageDataSize(OdUInt32 nPageSize)
OdUInt32 leftInCurPage() const
OdUInt64 curPageNo() const
void getBytes(void *buffer, OdUInt32 nLen)
void seekNextPage(bool bCreateNew=false)
void setAppServices(OdDbBaseHostAppServices *pHostAppServices)
OdUInt64 allocated() const
friend class OdMemoryStream
OdUInt32 posInCurPage() const
OdMemFileStreamImpl(OdUInt32 nPageDataSize=0x00200000)
const OdUInt8 * currPos() const
PageSwitcher & pageSwitcher()
OdUInt32 pageDataSize() const
virtual OdStreamBufPtr createFile(const OdString &filename, Oda::FileAccessMode accessMode=Oda::kFileRead, Oda::FileShareMode shareMode=Oda::kShareDenyNo, Oda::FileCreationDisposition creationDisposition=Oda::kOpenExisting)
OdString & format(const OdChar *formatString,...)
const OdChar * c_str() const
FIRSTDLL_EXPORT_STATIC static const OdString kEmpty
OdUInt64 m_nPageStartAddr