FastFileManager.cpp
资源名称:DXGuide.zip [点击查看]
上传用户:wymy58
上传日期:2007-01-07
资源大小:2086k
文件大小:3k
源码类别:
DirextX编程
开发平台:
Visual C++
- // Written by FangHong, 1998
- // Based DX5SDK samples "FastFile"
- /*
- Jun-03-1998, change compare() to direct compare names.
- Jun-02-1998, first version.
- */
- #include "stdafx.h"
- #include "FastFileManager.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CFastFileManager::CFastFileManager(void)
- {
- m_hFileMapping = NULL;
- m_hFile = NULL;
- m_pBase = NULL;
- ZeroMemory(&m_head, sizeof(PACK_FILE_HEAD));
- m_aFileEntry = NULL;
- }
- // Initialize for fast file access.
- // The master file are specified.
- // get a file handle array
- BOOL CFastFileManager::Create(LPCTSTR lpstrName)
- {
- // try our resourse file first
- HRSRC h;
- if (h = ::FindResource(NULL, lpstrName, RT_RCDATA))
- {
- m_pBase = (BYTE*)::LockResource(::LoadResource(NULL, h));
- if (m_pBase == NULL)
- {
- TRACE0( "FastFileInit: unable to lock resourcen" );
- return FALSE;
- }
- TRACE1("FastFileInit: opened resource: %s n", lpstrName);
- }
- else
- {
- // create a memory mapped file for the master file
- m_hFile = ::CreateFile(lpstrName, GENERIC_READ,
- FILE_SHARE_READ, NULL,
- OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
- if (m_hFile == NULL || m_hFile == (HANDLE)HFILE_ERROR)
- {
- TRACE1("FastFileInit: CreateFile(%s)n", lpstrName);
- m_hFile = NULL;
- return FALSE;
- }
- m_hFileMapping = ::CreateFileMapping(m_hFile, NULL, PAGE_READONLY, 0, 0, NULL);
- if (m_hFileMapping == NULL)
- {
- TRACE0( "FastFileInit: CreateFileMapping Failedn" );
- return FALSE;
- }
- m_pBase = (BYTE*)::MapViewOfFile(m_hFileMapping, FILE_MAP_READ, 0, 0, 0);
- if (m_pBase == NULL)
- {
- TRACE0( "FastFileInit: MapViewOfFile Failedn" );
- return FALSE;
- }
- }
- // get initial data from the memory mapped file
- m_head = *((PACK_FILE_HEAD*)m_pBase);
- m_aFileEntry = (FILE_ENTRY*)(m_pBase + sizeof(PACK_FILE_HEAD));
- return TRUE;
- }
- // Clean up resources
- CFastFileManager::~CFastFileManager(void)
- {
- // dont unmap things if we have locks out standing
- if (m_hFileMapping != NULL && m_pBase)
- {
- ::UnmapViewOfFile(m_pBase);
- }
- if (m_hFileMapping != NULL)
- {
- ::CloseHandle(m_hFileMapping);
- m_hFileMapping = NULL;
- }
- if (m_hFile != NULL)
- {
- ::CloseHandle(m_hFile);
- m_hFile = NULL;
- }
- }
- // Search the directory for the file, and return a file handle if found.
- FILE_ENTRY* CFastFileManager::GetFileEntryInfo(LPCSTR lpstrName)
- {
- ASSERT(m_aFileEntry != NULL);
- ASSERT(lpstrName != NULL && lpstrName[0] != 0);
- FILE_ENTRY* pfe = (FILE_ENTRY*)::bsearch(/*&fe*/lpstrName, m_aFileEntry, m_head.dwEntryCount, sizeof(FILE_ENTRY), CFastFileManager::Compare);
- return pfe;
- }
- // bsearch comparison routine
- int AFX_CDECL CFastFileManager::Compare(const void* p1, const void* p2)
- {
- return (::_stricmp((LPCSTR)p1, ((FILE_ENTRY*)(p2))->szFileName));
- }