Game Development Reference
In-Depth Information
3. We use the following function to open the ile and create the memory mapping:
bool RawFile::Open( const string& FileName,
const string& VirtualFileName )
{
4. At irst, we need to obtain a valid ile descriptor associated with the ile:
#ifdef OS_WINDOWS
FMapFile = (void*)CreateFileA( FFileName.c_str(),
GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS,
NULL );
#else
FFileHandle = open( FileName.c_str(), O_RDONLY );
if ( FFileHandle == -1 )
{
FFileData = NULL;
FSize = 0;
}
#endif
5. Using the ile descriptor, we can create a ile mapping. Here we omit error checks for
the sake of clarity. However, the example in the supplementary materials contains
more error checks:
#ifdef OS_WINDOWS
FMapHandle = (void*)CreateFileMapping( ( HANDLE )FMapFile,
NULL, PAGE_READONLY, 0, 0, NULL );
FFileData = (Lubyte*)MapViewOfFile((HANDLE)FMapHandle,
FILE_MAP_READ, 0, 0, 0 );
DWORD dwSizeLow = 0, dwSizeHigh = 0;
dwSizeLow = ::GetFileSize( FMapFile, &dwSizeHigh );
FSize = ((uint64)dwSizeHigh << 32) | (uint64)dwSizeLow;
#else
struct stat FileInfo;
fstat( FFileHandle, &FileInfo );
FSize = static_cast<uint64>( FileInfo.st_size );
FFileData = (Lubyte*) mmap(NULL, FSize, PROT_READ,
MAP_PRIVATE, FFileHandle, 0 );
close( FFileHandle );
#endif
return true;
}
 
Search WWH ::




Custom Search