28 #if !defined(_MEMORYFILESTREAMIMPL_H_INCLUDED_)
29 #define _MEMORYFILESTREAMIMPL_H_INCLUDED_
35 #ifdef OD_HAVE_UNISTD_FILE
39 #ifdef OD_HAVE_WINDOWS_STORAGE_FILE
40 #include <windows.storage.h>
43 #ifdef OD_HAVE_WRL_FILE
44 #include <wrl/client.h>
45 #include <wrl/wrappers/corewrappers.h>
55 #define TEMPFILE_PREFIX "tx$"
65 if (CoCreateGuid(&result) < 0)
67 tmpDir = tmpDir.
format(L
"%ls%llx%x%x.tmp", tmpDir.
c_str(), result.Data1, result.Data2, result.Data3);
69 #elif defined(_WIN32) // Win 32 + 64 bit
71 ::GetTempFileName(tmpDir.
c_str(), !prefix.isEmpty() ?
OdString(prefix).c_str() : L
"tmp", 0, tmpFile);
82 strcat(tmpFile, (
const char*)dstBuf.
asArrayPtr());
84 strcat(tmpFile, !prefix.isEmpty() ? prefix.c_str() :
"tmp");
85 #ifdef OD_HAVE_MKSTEMPS_FUNC
86 strcat(tmpFile,
"XXXXXX.tmp");
87 int fileDescriptor = mkstemps(tmpFile, 4);
89 strcat(tmpFile,
"XXXXXX");
90 int fileDescriptor = mkstemp(tmpFile);
92 if (fileDescriptor == -1)
97 close(fileDescriptor);
99 OdAnsiString ansiTmpFile(tmpFile);
145 template <
class TBase = OdMemFileStreamDummyBase>
173 bool releaseDiskFile(
bool deleteIt)
176 if (deleteIt && !m_sTmpFileName.
isEmpty()) {
177 #ifdef OD_HAVE_REMOVE_FUNC
178 remove(m_sTmpFileName);
180 DeleteFile(
OdString(m_sTmpFileName).c_str());
183 m_sTmpFileName.
empty();
203 releaseDiskFile(
true);
208 m_pHostAppServices = pHostAppServices;
216 inline void setBufferSize(
OdUInt32 pageSize) { m_dataBuffer.
resize(pageSize); };
235 inline PAGE* activePage()
const {
return m_activePage; };
242 inline void setActivePage(
PAGE* pPage) { m_activePage = pPage; };
250 inline bool isActivePage(
PAGE* pPage)
const {
return (pPage == m_activePage); };
258 inline bool isModified(
PAGE* pPage)
const {
return pPage->
m_modified; };
266 inline void setModified(
PAGE* pPage,
bool modified) { pPage->
m_modified = modified; };
304 void initializePage(
PAGE* pPage)
309 pPage->
m_data = dataBuffer(pPage);
310 setModified(pPage,
false);
311 setSaved(pPage,
false);
312 setFilePosition(pPage, 0);
320 if (!m_pTmpFile.isNull())
324 if (m_pHostAppServices)
326 m_sTmpFileName = TmpFileHelper::getTempFile(m_pHostAppServices);
335 throw OdError(eNullObjectPointer);
344 bool savePage(
PAGE* pPage)
346 if (pPage && isSaved(pPage) && !isModified(pPage))
349 if (!pPage || diskFile().isNull())
359 setFilePosition(pPage, diskFile()->
tell());
361 diskFile()->putBytes(dataBuffer(pPage), dataSize(pPage));
363 setModified(pPage,
false);
364 setSaved(pPage,
true);
374 bool loadPage(
PAGE* pPage)
378 if (isActivePage(pPage) && !isModified(pPage))
381 if (isSaved(pPage) && !diskFile().isNull())
384 diskFile()->getBytes(dataBuffer(pPage), dataSize(pPage));
387 setModified(pPage,
false);
388 setActivePage(pPage);
397 bool ensurePage(
PAGE* pPage)
400 return isActivePage(pPage) ?
true : switchToPage(pPage);
410 bool switchToPage(
PAGE* pPage)
413 if (isActivePage(pPage))
415 savePage(activePage());
416 return loadPage(pPage);
428 return isActivePage(pPage) ?
true : savePage(activePage());
439 bool wasSaved = isSaved(pPage);
440 setModified(pPage,
false);
441 setSaved(pPage,
false);
442 if (!wasSaved || diskFile().isNull())
448 diskFile()->truncate();
453 PageSwitcher m_pageSwitcher;
627 m_pCurrPage = m_pFirstPage;
644 m_pLastPage = m_pCurrPage;
669 template <
class TBase>
672 PAGE* pNext, *pCurr = m_pFirstPage;
677 if (pageSwitcher().activePage() == pCurr)
687 template <
class TBase>
694 pageSwitcher().initializePage(pPage);
703 pageSwitcher().switchToPage(m_pCurrPage);
709 m_pFirstPage = m_pCurrPage = pPage;
711 pageSwitcher().switchToPage(m_pCurrPage);
717 template <
class TBase>
733 m_nCurPos += leftInCurPage();
734 pageSwitcher().switchToPage(m_pCurrPage);
740 template <
class TBase>
747 nNewPos = m_nEndPos +
offset;
750 nNewPos = m_nCurPos +
offset;
765 if(nNewPos != m_nCurPos)
767 if (nNewPos > m_nEndPos)
770 OdInt64 nFromEnd = m_nEndPos - nNewPos;
772 bool bForward =
false;
788 m_pCurrPage = m_pLastPage;
795 m_pCurrPage = m_pLastPage;
803 m_pCurrPage = m_pFirstPage;
811 m_pCurrPage = m_pFirstPage;
816 m_pCurrPage = m_pLastPage;
823 nNewPos = nNewPos / m_nPageDataSize * m_nPageDataSize ;
836 pageSwitcher().switchToPage(m_pCurrPage);
841 template <
class TBase>
845 if(m_nCurPos < m_nEndPos)
847 pageSwitcher().ensurePage(m_pCurrPage);
848 OdUInt32 nPosInPage = posInCurPage();
849 ret = m_pCurrPage->
m_data[nPosInPage];
851 if ((nPosInPage + 1) == m_nPageDataSize)
861 template <
class TBase>
867 OdUInt64 nNewPos = m_nCurPos + nLen;
868 if(nNewPos <= m_nEndPos)
874 pageSwitcher().ensurePage(m_pCurrPage);
875 ::memcpy(pDest, currPos(), nToCopy);
882 nToCopy =
odmin(m_nPageDataSize, nLen);
883 ::memcpy(pDest, m_pCurrPage->
m_data, nToCopy);
888 if(m_nCurPos && (m_nCurPos % m_nPageDataSize)==0)
897 template <
class TBase>
904 pageSwitcher().ensurePage(m_pCurrPage);
905 pageSwitcher().setModified(m_pCurrPage,
true);
906 OdUInt64 nPosInPage = posInCurPage();
907 m_pCurrPage->
m_data[nPosInPage] = val;
909 m_nEndPos =
odmax(m_nCurPos, m_nEndPos);
910 if ((nPosInPage + 1) == m_nPageDataSize)
916 template <
class TBase>
927 pageSwitcher().ensurePage(m_pCurrPage);
928 pageSwitcher().setModified(m_pCurrPage,
true);
929 ::memcpy(currPos(), pSrc, nToCopy);
936 pageSwitcher().setModified(m_pCurrPage,
true);
937 nToCopy =
odmin(m_nPageDataSize, nLen);
938 ::memcpy(m_pCurrPage->
m_data, pSrc, nToCopy);
942 m_nCurPos += nToCopy;
943 if(m_nCurPos && (m_nCurPos % m_nPageDataSize)==0)
947 m_nEndPos =
odmax(m_nCurPos, m_nEndPos);
952 #endif // !defined(_MEMORYFILESTREAMIMPL_H_INCLUDED_)