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

void PureViewOfFile::createView(const FileMap &someFileMap)
{
  createView(someFileMap,0L,0L,someFileMap.maxExtentLow());
}

void PureViewOfFile::createView(const FileMap &someFileMap,DWORD offsetHigh,DWORD offsetLow,DWORD byteCount)
{
	Access accessRights;
	SYSTEM_INFO systemInfo;

  destroyView();
	::GetSystemInfo(&systemInfo);
	if(FileMap::ReadOnly==someFileMap.paging())accessRights=Read;
	else if(FileMap::ReadWrite==someFileMap.paging())accessRights=Write;
	else accessRights=Copy;
	if(offsetLow&&(offsetLow%systemInfo.dwAllocationGranularity))
	{
		DWORD adjOffset((offsetLow/systemInfo.dwAllocationGranularity)*systemInfo.dwAllocationGranularity);
		byteCount+=(offsetLow-adjOffset);
		mnpFirstValidAddress=mnpPointer=::MapViewOfFile((HANDLE)someFileMap,accessRights,offsetHigh,adjOffset,byteCount);
		mnpCursor=mnpFirstValidAddress;
		mnpLastValidAddress=(BYTE*)mnpFirstValidAddress+(byteCount-1L);
		seek(offsetLow-adjOffset,PureViewOfFile::SeekSet);
		mnpFirstValidAddress=mnpCursor;
	}
	else
	{
		mnpFirstValidAddress=mnpPointer=::MapViewOfFile((HANDLE)someFileMap,accessRights,offsetHigh,offsetLow,byteCount);
		mnpCursor=mnpFirstValidAddress;
		mnpLastValidAddress=(BYTE*)mnpFirstValidAddress+(byteCount-1L);
	}
}

void PureViewOfFile::createView(DWORD byteCount,DWORD baseAddress)
{
	destroyView();
	mDisposition=Keep;
	mnpFirstValidAddress=mnpPointer=(void*)baseAddress;
	mnpCursor=mnpFirstValidAddress;
  mnpLastValidAddress=(BYTE*)mnpFirstValidAddress+(byteCount-1L);
}

void PureViewOfFile::destroyView(void)
{
  if(!mnpPointer)return;
	if(Delete==mDisposition)::UnmapViewOfFile(mnpPointer);
	mnpPointer=0;
	mnpFirstValidAddress=0;
	mnpLastValidAddress=0;
	mnpCursor=0;
  mDisposition=Delete;
}

DWORD PureViewOfFile::peek(BYTE *npBuffer,DWORD sizeBuffer)
{
	BYTE *npPeekCursor=(BYTE*)mnpCursor;

	if(!npPeekCursor)return FALSE;
	for(DWORD itemIndex=0;itemIndex<sizeBuffer;itemIndex++)
	{
		if(npPeekCursor>mnpLastValidAddress)return itemIndex;
		*(npBuffer+itemIndex)=*((BYTE*)npPeekCursor);
		npPeekCursor=((BYTE*)npPeekCursor+sizeof(BYTE));
	}
	return itemIndex;
}

WORD PureViewOfFile::seek(DWORD seekPos,SeekMethod seekMethod)
{
	WORD returnCode(FALSE);

	switch(seekMethod)
	{
		case SeekSet :
			if((char*)mnpFirstValidAddress+seekPos>mnpLastValidAddress)break;
			mnpCursor=(char*)mnpFirstValidAddress+seekPos;
			returnCode=TRUE;
			break;
		case SeekCurr :
			if((char*)mnpCursor+seekPos>mnpLastValidAddress)break;
			mnpCursor=(char*)mnpCursor+seekPos;
			break;
		case SeekEnd :
			if((char*)mnpCursor-seekPos<mnpFirstValidAddress)break;
			mnpCursor=(char*)mnpCursor-seekPos;
			break;
	}
	return returnCode;
}

WORD PureViewOfFile::find(const String &findString)
{
	char *npCursor;
	DWORD stringLength;

	if(findString.isNull())return FALSE;
	if(0==(npCursor=(char*)mnpCursor))return FALSE;
	stringLength=findString.length();
	while(npCursor<(char*)mnpLastValidAddress)
	{
		if(*(npCursor)==*((char*)findString))
		{
			if(npCursor+stringLength>mnpLastValidAddress)return FALSE;
			if(!::memcmp(npCursor,(char*)findString,stringLength))
			{mnpCursor=npCursor;return TRUE;}
		}
		npCursor++;
	}
	return FALSE;
}

WORD PureViewOfFile::write(const String &lineString)
{
	DWORD lineLength(lineString.length());
	if(!lineLength)return FALSE;
	for(DWORD colIndex=0;colIndex<lineLength;colIndex++)
	{
    BYTE charByte(*((char*)(((String&)lineString))+colIndex));
		if(!write(charByte))return FALSE;
	}
	return TRUE;
}

WORD PureViewOfFile::write(BYTE *npBuffer,DWORD sizeBuffer)
{
	for(DWORD itemIndex=0;itemIndex<sizeBuffer;itemIndex++)
		if(!write(*(npBuffer+itemIndex)))return FALSE;
	return TRUE;
}

WORD PureViewOfFile::writeLine(const String &lineString)
{
	if(lineString.isNull())return FALSE;
	if(!write(lineString))return FALSE;
	if(!write(CarriageReturn))return FALSE;
	if(!write(LineFeed))return FALSE;
  return TRUE;
}

DWORD PureViewOfFile::getLine(String &lineString)
{
  int granularity(1024);
	int blockLength(granularity);
	BYTE charByte;
	BYTE *ptrLine;
	WORD bytesRead(0);

	lineString.reserve(blockLength);
	ptrLine=(BYTE*)(char*)lineString;
	while(TRUE)
	{
	  if(!read(charByte))break;
		if(bytesRead>=blockLength-1)
		{
		  *ptrLine=0;
			String tmpString(lineString);
			int nextIndex(ptrLine-(BYTE*)(char*)lineString);
			lineString.reserve(blockLength+granularity);
			::memcpy((char*)lineString,(char*)tmpString,blockLength);
			blockLength+=granularity;
			ptrLine=(BYTE*)(char*)lineString+nextIndex;
		}
		if(CarriageReturn==charByte)
		{
		  if(!read(charByte))break;
			if(LineFeed==charByte)
			{
			  *ptrLine=0;
				return TRUE;
			}
		}
		else if(NullChar==charByte)
		{
		  *ptrLine++=0;
			return bytesRead;
		}
		else 
		{
		  *ptrLine++=charByte;
			bytesRead++;
		}
	}
	return bytesRead;
}
#endif



