#if defined(__FLAT__)
#include <common/filemap.hpp>
#include <common/openfile.hpp>

FileMap::FileMap(void)
: mMaxExtentHigh(0L), mMaxExtentLow(0L),
	mPaging(ReadOnly), mProtection(Image), mhFileHandle(0)
{
}

FileMap::FileMap(const FileMap &/*someFileMap*/)
: mMaxExtentHigh(0L), mMaxExtentLow(0L),
	mPaging(ReadOnly), mProtection(Image), mhFileHandle(0)
{
}

FileMap::FileMap(String mapName,DWORD maxExtentHigh,DWORD maxExtentLow,Paging paging,Protection protection)
:	mMaxExtentHigh(maxExtentHigh), mMaxExtentLow(maxExtentLow), mPaging(paging),
	mProtection(protection), mhFileHandle(0)
{
  create(mapName,maxExtentHigh,maxExtentLow,paging,protection);
}

FileMap::FileMap(const FileHandle &someFileHandle)
:	mMaxExtentHigh(0L), mMaxExtentLow(0), mhFileHandle(0)
{
  maxExtentHigh(0L);  
  maxExtentLow(someFileHandle.size());  
  create(someFileHandle);
}

WORD FileMap::create(const FileHandle &someFileHandle)
{
	if(FileHandle::Read==someFileHandle.access())mPaging=ReadOnly;
	else if(FileHandle::Write==someFileHandle.access())mPaging=WriteCopy;
	else if(FileHandle::ReadWrite==someFileHandle.access())mPaging=ReadWrite;
	else mPaging=ReadOnly;
	mProtection=Commit;
	return create(String(""),0L,someFileHandle.size(),mPaging,mProtection,(HANDLE)someFileHandle);
}

WORD FileMap::create(String mapName,DWORD maxExtentHigh,DWORD maxExtentLow,Paging paging,Protection protection,HANDLE hMapFile)
{
	mMaxExtentHigh=maxExtentHigh;
	mMaxExtentLow=maxExtentLow;
	mPaging=paging;
	mProtection=protection;
	destroyMap();
	mhFileHandle=::CreateFileMapping((HANDLE)hMapFile,(LPSECURITY_ATTRIBUTES)0,paging|protection,maxExtentHigh,maxExtentLow,mapName);
  return isOkay();
}

BOOL FileMap::open(const String &strName,DWORD maxExtentHigh,DWORD maxExtentLow,Paging paging,Protection protection,DWORD access,BOOL inherit)
{
  destroyMap();
	if(strName.isNull())return FALSE;
  mhFileHandle=::OpenFileMapping(access,inherit,(LPSTR)(String&)strName);
	mMaxExtentHigh=maxExtentHigh;
	mMaxExtentLow=maxExtentLow;
	mPaging=paging;
	mProtection=protection;
	return isOkay();
}
#endif
