|
// DirMng.cpp
//
#include "stdafx.h"
#include "DirMng.h"
#include "SrchDir.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CDirManager g_DirManager;
/////////////////////////////////////////////////////////////////////////////
// CDirManager
IMPLEMENT_DYNCREATE(CDirManager, CObject)
/////////////////////////////////////////////////////////////////////////////
// CDirManager construction/destruction
CDirManager::CDirManager()
{
// TODO: add construction code here
lstrcpy(m_szFiles, "*.jpg;*.jpeg;*.gif;*.bmp;*.dib;*.png;*.tif;*.tiff;*.wmf;*.emf;*.ico");
m_uiTotalFilesInDir = 0;
m_pInfoHeader = NULL;
m_pCurrentInfo = NULL;
}
CDirManager::~CDirManager()
{
ClearBuffer();
}
/////////////////////////////////////////////////////////////////////////////
// CDirManager diagnostics
#ifdef _DEBUG
void CDirManager::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// ͨ�ö��ּ����㷨, ����˵���μ�bsearch
// pHead: ������ָ�� pValue��Ҫ������ֵ
// lElements��������Ԫ�ظ��� iSize������Ԫ����ռ�ֽ���
// compare����������Ԫ�ش�С�Ļص�����
// ����ֵ���Ҳ���ʱ������ӽ���ֵ
void* FindEle(void* pHead, void* pValue,
long lElements, int iSize,
int (__cdecl* compare) (const void *, const void *))
{
BYTE *pHigh, *pLow, *pMid;
pLow = (BYTE*)pHead;
pHigh = (BYTE*)pHead;
pHigh += ((lElements-1)*iSize);
while (pLow <= pHigh)
{
// �����۰�ָ��
{
unsigned long u = pHigh-pLow;
u /= iSize;
u /= 2;
u *= iSize;
pMid = pLow;
pMid += u;
}
int iCalc = (*compare)((const void*)pValue, (const void*)pMid);
if (iCalc < 0)
pHigh = pMid - iSize;
else if (iCalc > 0)
pLow = pMid + iSize;
else
return (void*)pMid;
}
BYTE* pRet = max(pLow, pHigh);
pRet = max((BYTE*)pHead, pRet);
pRet = min(pRet, ((BYTE*)pHead)+((lElements-1)*iSize));
return pRet;
}
/////////////////////////////////////////////////////////////////////////////
void CDirManager::ClearBuffer()
{
m_uiTotalFilesInDir = 0;
if (m_pInfoHeader)
{
delete [] m_pInfoHeader;
m_pInfoHeader = NULL;
}
}
void CDirManager::OpenDir(const char* pszDir)
{
ClearBuffer();
if (pszDir == NULL)
return;
if (*pszDir == '\0')
return;
{
CDirCounter dircounter;
m_uiTotalFilesInDir = dircounter.GetFilesInDir(pszDir, m_szFiles);
m_pInfoHeader = new FILE_INFO[m_uiTotalFilesInDir];
}
if (m_pInfoHeader)
{
CDirInfo dirinfo;
dirinfo.GetDirInfo(pszDir, m_szFiles, m_pInfoHeader);
}
else
m_uiTotalFilesInDir = 0;
Resort();
m_pCurrentInfo = m_pInfoHeader;
}
void CDirManager::OpenFileInDir(const char* pszFile)
{
char szDir[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
if (pszFile && (*pszFile != '\0'))
{
_splitpath(pszFile, drive, dir, NULL, NULL);
lstrcpy(szDir, drive);
lstrcat(szDir, dir);
OpenDir(szDir);
AllocateFile(pszFile);
}
}
const char* CDirManager::NextFile()
{
BOOL bLastOne = FALSE;
if (m_uiTotalFilesInDir == 0)
{
m_pCurrentInfo = NULL;
MessageBeep(0xFFFFFFFF);
bLastOne = TRUE;
}
else
{
m_pCurrentInfo++;
if (m_pCurrentInfo >= (m_pInfoHeader + m_uiTotalFilesInDir))
{
if (m_uiTotalFilesInDir)
m_pCurrentInfo = m_pInfoHeader + m_uiTotalFilesInDir - 1;
MessageBeep(0xFFFFFFFF);
bLastOne = TRUE;
}
}
if (bLastOne) return NULL;
if (m_pCurrentInfo == NULL)
return NULL;
return (const char*)m_pCurrentInfo->strFileName;
}
const char* CDirManager::PreFile()
{
//if (m_pCurrentInfo == NULL) return NULL;
//if (m_pInfoHeader == NULL) return NULL;
BOOL bLastOne = FALSE;
if (m_uiTotalFilesInDir == 0)
{
m_pCurrentInfo = NULL;
MessageBeep(0xFFFFFFFF);
bLastOne = TRUE;
}
else
{
m_pCurrentInfo--;
if (m_pCurrentInfo < m_pInfoHeader)
{
m_pCurrentInfo = m_pInfoHeader;
MessageBeep(0xFFFFFFFF);
bLastOne = TRUE;
}
}
if (bLastOne) return NULL;
if (m_pCurrentInfo == NULL)
return NULL;
return (const char*)m_pCurrentInfo->strFileName;
}
const char* CDirManager::FirstFile()
{
if (m_pCurrentInfo == NULL) return NULL;
if (m_pInfoHeader == NULL) return NULL;
if (m_uiTotalFilesInDir == 0) return NULL;
m_pCurrentInfo = m_pInfoHeader;
return (const char*)m_pCurrentInfo->strFileName;
}
const char* CDirManager::LastFile()
{
if (m_pCurrentInfo == NULL) return NULL;
if (m_pInfoHeader == NULL) return NULL;
if (m_uiTotalFilesInDir == 0) return NULL;
m_pCurrentInfo = m_pInfoHeader + m_uiTotalFilesInDir - 1;
return (const char*)m_pCurrentInfo->strFileName;
}
void CDirManager::Resort()
{
if (m_uiTotalFilesInDir)
qsort((void*)m_pInfoHeader, m_uiTotalFilesInDir, sizeof(FILE_INFO), Compare);
}
void CDirManager::AllocateFile(const char* pszPath)
{
if (m_pInfoHeader == NULL)
{
m_pCurrentInfo = NULL;
return;
}
if (pszPath == NULL)
return;
if (*pszPath == '\0')
return;
FILE_INFO temp;
temp.strFileName = pszPath;
m_pCurrentInfo = (FILE_INFO*)FindEle((void*)m_pInfoHeader, (void*)&temp,
m_uiTotalFilesInDir, sizeof(FILE_INFO),Compare);
}
// ����������ıȽϺ���
int CDirManager::Compare(const void *arg1, const void *arg2)
{
int iRet = 0;
FILE_INFO* p1 = (FILE_INFO*)arg1;
FILE_INFO* p2 = (FILE_INFO*)arg2;
return strcmpi(p1->strFileName, p2->strFileName);
}
int CDirManager::GetCurrentFileIndex()
{
if (m_uiTotalFilesInDir == 0)
return 0;
return (m_pCurrentInfo - m_pInfoHeader + 1);
}
UINT CDirManager::TotalFilesInDir()
{
return m_uiTotalFilesInDir;
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.