#pragma once
#define ALIGN_DOWN_BY(length, alignment) \
((ULONG_PTR)(length) & ~(alignment - 1))
#define ALIGN_UP_BY(length, alignment) \
(ALIGN_DOWN_BY(((ULONG_PTR)(length) + alignment - 1), alignment))
#define ALIGN_DOWN_POINTER_BY(address, alignment) \
((PVOID)((ULONG_PTR)(address) & ~((ULONG_PTR)alignment - 1)))
#define ALIGN_UP_POINTER_BY(address, alignment) \
(ALIGN_DOWN_POINTER_BY(((ULONG_PTR)(address) + alignment - 1), alignment))
#define ALIGN_DOWN(length, type) \
ALIGN_DOWN_BY(length, sizeof(type))
#define ALIGN_UP(length, type) \
ALIGN_UP_BY(length, sizeof(type))
#define ALIGN_DOWN_POINTER(address, type) \
ALIGN_DOWN_POINTER_BY(address, sizeof(type))
#define ALIGN_UP_POINTER(address, type) \
ALIGN_UP_POINTER_BY(address, sizeof(type))
class CLargeFile
{
public:
CLargeFile();
~CLargeFile();
/************************************************************************/
/* open file, return TRUE if success; if FALSE, use GetLastError to get error code
/* error may occur at CreateFile or CreateFileMapping
/************************************************************************/
BOOL OpenFile(LPCTSTR pFilePathName, UINT nPageCount = 3);
/************************************************************************/
/* check if i have opened a file.
/************************************************************************/
BOOL IsOpenFile();
/************************************************************************/
/* get file path name.
/************************************************************************/
LPCTSTR GetFilePathName();
/************************************************************************/
/* just close file, NOTHING saved.
/* if you want to save, call SaveFile() before CloseFile().
/************************************************************************/
void CloseFile();
/************************************************************************/
/* get file size.
/************************************************************************/
DWORD GetFileSizeLow();
DWORD GetFileSizeHigh();
void GetFileSizeEx(LARGE_INTEGER* puFileSize);
/************************************************************************/
/* visit file position. return a pointer point to the data at the position.
/* dwAvalibleSize received the avalible size of the data that you can use,
/* if out of the size, you should call VisitFilePosition()
/* with the new position to get a new pointer and new avalible size.
/************************************************************************/
void* VisitFilePosition(DWORD nVisitLow, DWORD nVisitHigh = 0, DWORD* pdwAvalibleSize = 0);
void* VisitFilePosition(LARGE_INTEGER nVisit, DWORD* pdwAvalibleSize = 0);
protected:
virtual void OnUnmapViewOfFile();
virtual BYTE* OnMapViewOfFile(LARGE_INTEGER nViewStart, DWORD dwMapSize);
LARGE_INTEGER m_nViewStart;
DWORD m_dwMapSize;
DWORD m_dwPageSize;
DWORD m_dwPageCount;
BYTE* m_pView;
private:
void init();
TCHAR m_szFilePathName[MAX_PATH];
HANDLE m_hFile;
HANDLE m_hMap;
LARGE_INTEGER m_nFileSize;
};