|
//====================================================================
// Although great care has gone into developing this software,
// it is provided without any guarantee of reliability, accuracy
// of information, or correctness of operation. I am not responsible
// for any damages that may occur as a result of using this software.
// Use this software entirely at your own risk.
// Copyright 2003, Chris Richardson
//
// Description: Memory mapped file stream.
//
//====================================================================
#include "stdafx.h"
#include "MMFStream.h"
//////////////////////////////////////////////////////////////////////
// CMMFStream Implementation.
//////////////////////////////////////////////////////////////////////
CMMFStream::CMMFStream() :
c_hFile( INVALID_HANDLE_VALUE ),
c_hMapping( NULL ),
c_pcBuf( NULL ),
c_pvBaseAddress( NULL ),
c_ulBytesLeft( 0 )
{
}
//
// ------------------------------------------------------------------
//
CMMFStream::~CMMFStream()
{
if( c_hFile != INVALID_HANDLE_VALUE )
{
Close();
}
}
//
// ------------------------------------------------------------------
//
ParseStatus CMMFStream::Open( const TCHAR * p_pszPath )
{
if( c_hFile != INVALID_HANDLE_VALUE )
return PARSE_STATUS_FILE_ALREADY_OPEN;
// Open a handle to the file.
c_hFile = CreateFile( p_pszPath,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0,
NULL );
if( c_hFile == INVALID_HANDLE_VALUE )
return PARSE_STATUS_FILE_OPEN_FAILED;
c_ulBytesLeft = GetFileSize( c_hFile, NULL );
if( c_ulBytesLeft > 0 )
{
// Create the file mapping object.
c_hMapping = CreateFileMapping( c_hFile,
NULL,
PAGE_READONLY,
0,
0,
NULL );
if( !c_hMapping )
{
CloseHandle( c_hFile );
c_hFile = INVALID_HANDLE_VALUE;
return PARSE_STATUS_FILE_OPEN_FAILED;
}
// Map the file into memory.
c_pvBaseAddress = MapViewOfFile( c_hMapping,
FILE_MAP_READ,
0,
0,
0 );
if( !c_pvBaseAddress )
{
Close();
return PARSE_STATUS_FILE_OPEN_FAILED;
}
c_pcBuf = (TCHAR*)c_pvBaseAddress;
}
_tcscpy( c_szFileName, p_pszPath );
return PARSE_STATUS_OK;
}
//
// ------------------------------------------------------------------
//
ParseStatus CMMFStream::Close()
{
if( c_hFile == INVALID_HANDLE_VALUE )
return PARSE_STATUS_FILE_NOT_OPEN;
// Clean up.
if( c_hMapping )
{
UnmapViewOfFile( c_pvBaseAddress );
CloseHandle( c_hMapping );
c_hMapping = NULL;
}
c_pvBaseAddress = NULL;
c_pcBuf = NULL;
c_ulBytesLeft = 0;
CloseHandle( c_hFile );
c_hFile = INVALID_HANDLE_VALUE;
memset( c_szFileName, 0, sizeof( c_szFileName ) );
return PARSE_STATUS_OK;
}
//
// ------------------------------------------------------------------
//
TCHAR * CMMFStream::GetS( TCHAR * p_pszStr, int p_iLength )
{
TCHAR * a_pszRet = p_pszStr;
for( int i = 0; i<p_iLength; ++i )
{
*p_pszStr = *c_pcBuf;
if( !c_ulBytesLeft || *p_pszStr == _T('\n') )
break;
p_pszStr++;
c_pcBuf++;
c_ulBytesLeft--;
}
return a_pszRet;
}
//
// ------------------------------------------------------------------
//
TCHAR CMMFStream::GetC()
{
if( !c_ulBytesLeft )
return _TEOF;
c_ulBytesLeft--;
return *(c_pcBuf++);
}
//
// ------------------------------------------------------------------
//
TCHAR CMMFStream::UngetC( TCHAR p_cC )
{
if( p_cC == _TEOF )
return _TEOF;
c_ulBytesLeft++;
return (*c_pcBuf--);
}
//
// ------------------------------------------------------------------
//
unsigned long CMMFStream::GetPos()
{
return c_pcBuf - (TCHAR*)c_pvBaseAddress;
}
//
// ------------------------------------------------------------------
//
void CMMFStream::SetPos( unsigned long p_ulPos )
{
if( p_ulPos > (c_pcBuf - (TCHAR*)c_pvBaseAddress) )
return;
c_pcBuf = ((TCHAR*)c_pvBaseAddress) + p_ulPos;
}
//
// ------------------------------------------------------------------
//
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.