?? dirmanager.cpp
字號:
//////////////////////////////////////
// CDirManager
#include "stdafx.h"
#include "io.h"
#include "sys\stat.h"
#include <shlobj.h >
#include "DirManager.h"
#include "language.h"
//Midified by LU Baifang for character problem.
//all string are from string table instead of hard code.
//1999-6-23
//Here I define three strings only for this file. because I do not know
//what is the best way to resolve this problem.
#ifdef ENGLISH_US
static char dirCanNotCreate[] = "Can not create specificed directory: %s";
static char needCreate[] = " not exit. Create or not?";
static char fileSystemError[] = "File system error!";
static char memoryError[] = "Menory is too low, can not create file.";
#else
static char dirCanNotCreate[] = "不能創建指定的目錄: %s";
static char needCreate[] = " 不存在,要創建嗎?";
static char fileSystemError[] = "文件系統錯誤!";
static char memoryError[] = "內存不足,不能打開文件";
#endif
CDirManager::CDirManager()
{
m_currentWorkPath.Empty();
m_prompt = FALSE;
}
CDirManager::~CDirManager()
{
}
CDirManager::CheckPath( CString & path )
{
CString msg;
if( m_prompt )
{
//modified by Lu Baifang for uniform string management
//AfxFormatString1(msg,IDS_DIRMANAGER_NOTCREATEDIR, path);
msg.Format(dirCanNotCreate, path );
}
if( !InitPath( path ) )
{//不能初始化目錄
if( m_prompt )
{
AfxMessageBox( msg );
}
return FALSE;
}
if( IsExist( path ) )
{//要檢驗的目錄已存在
return TRUE;
}
//m_prompt為TRUE時才提示是否創建,否則自動創建
if( m_prompt && AfxMessageBox( path+needCreate , MB_YESNO ) != IDYES )
{//用戶放棄創建此目錄
return FALSE;
}
CString buf;
m_path.Empty();
while( GetNextSubdir( buf ) )
{
m_path += buf;
if( !IsExist( m_path ) )
{
if( !CreateDirectory( m_path ) )
{
if( m_prompt )
{
AfxMessageBox( msg );
}
return FALSE;
}
}
}
//目錄創建成功
path = m_path;
if( path[path.GetLength()-1] != '\\' )
{
path += '\\';
}
return TRUE;
}
CDirManager::IsExist( char const * path )
{
if( path == NULL )
{
return FALSE;
}
CString buf = path;
if( buf[buf.GetLength()-1] == '\\' )
{
buf = buf.Left( buf.GetLength()-1 );
}
if( buf.IsEmpty() )
{//目錄為空,表明其為當前目錄,所以是存在的 1999-04-05 ChK
return TRUE;
}
//修改以兼容網絡驅動器 chk: 2000-09-06
CFileStatus fs;
fs.m_szFullName[0] = '\0';
if( CFile::GetStatus( buf, fs ) )
{
return TRUE;
}
if( buf.GetLength() == 1 )
{
return FALSE;
}
if( buf[buf.GetLength()-1] != '\\' )
{
buf += '\\';
}
UINT type = GetDriveType( buf );
if( type == DRIVE_UNKNOWN )
{//類型未知
return FALSE;
}
if( type == DRIVE_NO_ROOT_DIR )
{//不是有效的驅動器
return FALSE;
}
return TRUE;
}
CDirManager::InitPath( CString path )
{
char buf[512];
if( !::GetCurrentDirectory( 511, buf ) )
{
AfxMessageBox( fileSystemError );
return FALSE;
}
//使m_tailString成為x:\xxx\xxx或\XXX\XXX的格式
m_tailString.Empty();
switch( path.GetLength() )
{
case 0: m_tailString = buf;
break;
case 1: if( path[0] != '\\' )
{
m_tailString = buf;
m_tailString += ('\\' + path);
}
else
{
m_tailString = '\\';
}
break;
default: if( path[1] != ':'
&& path[0] != '\\' )
{
m_tailString = buf;
}
m_tailString += path;
}
return TRUE;
}
BOOL CDirManager::GetNextSubdir( CString & subdir )
{
int pos;
if( m_tailString.Find ( ':' ) == 1 )
{//帶驅動器號的根路徑
subdir = m_tailString.Left( 2 ) + '\\';
m_tailString = m_tailString.Right( m_tailString.GetLength() - 3 );
}
else if( m_tailString.Find ( "\\\\" ) == 0 )
{//網絡驅動器 chk:2000-09-06 兼容網絡驅動器
subdir = m_tailString.Left( 2 );
m_tailString = m_tailString.Right( m_tailString.GetLength() - 2 );
pos = m_tailString.Find( '\\' );
if( pos < 0 )
{
subdir += m_tailString;
m_tailString.Empty();
}
else
{
subdir += m_tailString.Left( pos+1 );
m_tailString = m_tailString.Right( m_tailString.GetLength() - pos - 1 );
}
}
else
{
subdir = '\\';
}
begin:
if( m_tailString.IsEmpty() )
{
return FALSE;
}
pos = m_tailString.Find( '\\' );
if( pos == 0 )
{
m_tailString = m_tailString.Right( m_tailString.GetLength() - 1 );
goto begin;
}
else if( pos > 0 )
{
subdir += m_tailString.Left( pos );
m_tailString = m_tailString.Right( m_tailString.GetLength() - pos );
}
else
{
subdir += m_tailString;
m_tailString.Empty();
}
return TRUE;
}
BOOL CDirManager::CreateDirectory( char const * path )
{
SECURITY_ATTRIBUTES attrib;
attrib.nLength = sizeof(SECURITY_ATTRIBUTES);
attrib.bInheritHandle = FALSE;
attrib.lpSecurityDescriptor = NULL;//目錄權限,win95不使用這個參數
return ::CreateDirectory( path, &attrib );
}
BOOL CDirManager::CheckPathHaveFile(CString & pathFile)
{
int pos;
CString buf;
if( ( pos = pathFile.ReverseFind( '\\' ) ) >= 0 )
{
buf = pathFile.Left( pos );
if( !CheckPath( buf ) )
{
return FALSE;
}
pathFile = buf + pathFile.Right( pathFile.GetLength() - pos );
return TRUE;
}
return TRUE;
}
BOOL CDirManager::GetSystemPath(CString & sysPath)
{
char buf[512];
if( ::GetSystemDirectory( buf, 511 ) )
{
sysPath = buf;
if( !sysPath.IsEmpty() )
{
if( sysPath[ sysPath.GetLength()-1 ] != '\\' )
{
sysPath += '\\';
}
}
return TRUE;
}
else
{
return FALSE;
}
}
BOOL CDirManager::GetTempPath( CString & tempPath )
{
char buf[512];
if( ::GetTempPath( 511, buf ) )
{
tempPath = buf;
if( !tempPath.IsEmpty() )
{
if( tempPath[ tempPath.GetLength()-1 ] != '\\' )
{
tempPath += '\\';
}
}
return TRUE;
}
else
{
return FALSE;
}
}
BOOL CDirManager::GetMultiOpenFile( char const * initPathName, char const * type, CStringArray & openFileList, long fBufSize )
{
CFileDialog openFile( TRUE, NULL,
initPathName,
OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT,
type
);
char * pFileNameBuf = new char[fBufSize];
if( pFileNameBuf == NULL )
{
::MessageBox( NULL, memoryError, "Error Open File", MB_OK );
return FALSE;
}
*pFileNameBuf = NULL;
openFile.m_ofn.lpstrFile = pFileNameBuf;
openFile.m_ofn.nMaxFile = fBufSize - 1;
if( openFile.DoModal() != IDOK )
{//用戶放棄打開文件的操作
delete pFileNameBuf;
return FALSE;
}
POSITION pos;
openFileList.SetSize( 0, 20 );
pos = openFile.GetStartPosition();
while( pos != NULL )
{
openFileList.Add( openFile.GetNextPathName( pos ) );
}
delete pFileNameBuf;
return TRUE;
}
BOOL PASCAL CDirManager::MakePathName( const char * path, const char * name, CString & pathName )
{
pathName = path;
if( pathName.GetLength() >= 1 )
{
if( pathName[pathName.GetLength() - 1] != '\\' )
{
pathName += '\\';
}
}
if( name[0] == '\\' )
{
CString buf = name;
pathName += buf.Right( buf.GetLength() - 1 );
}
else
{
pathName += name;
}
return TRUE;
}
BOOL CDirManager::MakePathName( char const * name, CString & pathName)
{
return MakePathName( m_currentWorkPath, name, pathName );
}
BOOL CDirManager::DeleteTree(char const * dirName)
{
struct _finddata_t fileInfo;
long hFind;
CString findName;
BOOL re = TRUE;
CString subDir;
CString file;
subDir = dirName;
switch( subDir.GetLength() )
{//保證根目錄不被刪除
case 0: return FALSE;
case 1: if( subDir[0] == '\\' )
{
return FALSE;
}
break;
case 2: if( subDir[1] == ':' )
{
return FALSE;
}
break;
case 3: if( subDir.Right( 2 ) == ":\\" )
{
return FALSE;
}
break;
}
MakePathName( dirName, "*.*", findName );
if( (hFind = _findfirst( findName, &fileInfo )) == -1L )
{//本目錄已空,可以刪除
return RemoveDirectory( dirName );
}
if( fileInfo.name[0] != '.' )
{//第一個文件名如果不是 . 或 .. ,則該目錄很可能是根目錄,所以不刪除
return FALSE;
}
while( TRUE )
{
if( strcmp( fileInfo.name, "." ) == 0
|| strcmp( fileInfo.name, ".." ) == 0 )
{
//do nothing
}
else if( fileInfo.attrib & _A_SUBDIR )
{//發現子目錄
CString subDir;
MakePathName( dirName, fileInfo.name, subDir );
if( !DeleteTree( subDir ) )
{
re = FALSE;
}
}
else
{
MakePathName( dirName, fileInfo.name, file );
if( (fileInfo.attrib & _A_RDONLY )
|| (fileInfo.attrib & _A_SYSTEM )
|| (fileInfo.attrib & _A_HIDDEN ) )
{//去掉文件的保護屬性
_chmod( file, _S_IREAD | _S_IWRITE );
}
if( remove( file ) )
{//成功時remove 返回 0
re = FALSE;
}
}
if( _findnext( hFind, &fileInfo ) == -1L )
{
_findclose( hFind );
break;
}
}
if( RemoveDirectory( dirName ) )
{
return re;
}
else
{
return FALSE;
}
}
long CDirManager::DeleteFile( const char * fileName )
{
struct _finddata_t fileInfo;
long hFind;
long counter = 0;
CString path;
CString file;
GetPathFromPathName( fileName, path );
if( (hFind = _findfirst( fileName, &fileInfo )) == -1L )
{//未找到指定的文件
return 0;
}
while( TRUE )
{
if( strcmp( fileInfo.name, "." ) == 0
|| strcmp( fileInfo.name, ".." ) == 0 )
{//子目錄中的專用文件,不刪除
}
else if( fileInfo.attrib & _A_SUBDIR )
{//子目錄,不刪除
}
else
{
MakePathName( path, fileInfo.name, file );
if( (fileInfo.attrib & _A_RDONLY )
|| (fileInfo.attrib & _A_SYSTEM )
|| (fileInfo.attrib & _A_HIDDEN ) )
{//去掉文件的保護屬性
_chmod( file, _S_IREAD | _S_IWRITE );
}
if( remove( file ) == 0 )
{//成功時remove 返回 0
counter++;
}
}
if( _findnext( hFind, &fileInfo ) == -1L )
{
_findclose( hFind );
break;
}
}
return counter;
}
BOOL CDirManager::ListFile(const char * filter, CStringArray & result)
{
CString findName;
MakePathName( filter, findName );
return ListFile( findName, result, TRUE );//TRUE表示要搜索文件
}
BOOL CDirManager::ListDir(const char * filter, CStringArray & result)
{
CString findName;
MakePathName( filter, findName );
return ListFile( findName, result, FALSE );//FALSE表示要搜索文件
}
BOOL CDirManager::ListFile(const char * pathName, CStringArray & result, BOOL isFindFile)
{
struct _finddata_t fileInfo;
long hFind;
if( (hFind = _findfirst( pathName, &fileInfo )) == -1L )
{//本目錄中沒匹配的項目
return FALSE;
}
while( TRUE )
{
if( strcmp( fileInfo.name, "." ) == 0
|| strcmp( fileInfo.name, ".." ) == 0 )
{
//do nothing
}
else if( fileInfo.attrib & _A_SUBDIR )
{//發現子目錄
if( !isFindFile )
{
result.Add( fileInfo.name );
}
}
else
{//發現文件
if( isFindFile )
{
result.Add( fileInfo.name );
}
}
if( _findnext( hFind, &fileInfo ) == -1L )
{
_findclose( hFind );
break;
}
}
return TRUE;
}
BOOL CDirManager::GetPathByBrowse( CString & path, const char * prompt/*=NULL*/ )
{
LPITEMIDLIST pidl = NULL;
LPMALLOC pMalloc;
char buf[MAX_PATH];
BROWSEINFO bi;
if( !SUCCEEDED( ::SHGetMalloc( &pMalloc ) ) )
{
return FALSE;
}
/*
// Get the Init PIDL
if (!SUCCEEDED( ::SHGetSpecialFolderLocation(
(AfxGetMainWnd()->m_hWnd), CSIDL_DRIVES, &pidlInit ) ) )
{
pMalloc->Free( pidlInit );
return FALSE;
}
*/
// Fill in the BROWSEINFO structure.
bi.hwndOwner = AfxGetMainWnd()->m_hWnd;
/* bi.pidlRoot = pidlInit; */
bi.pidlRoot = NULL;
bi.pszDisplayName = buf;
bi.lpszTitle = prompt;
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
// Browse for a folder and return its PIDL.
if( (pidl = ::SHBrowseForFolder(&bi)) == NULL )
{
return FALSE;
}
::SHGetPathFromIDList( pidl, buf );
pMalloc->Free( pidl );
path = buf;
return TRUE;
}
BOOL CDirManager::SplitPathName( const char * pathName, CString & drv, CString & path, CString & name, CString & extName )
{
char pathBuf[MAX_PATH], drvBuf[10], nameBuf[256], extNameBuf[256];
_splitpath( pathName, drvBuf, pathBuf, nameBuf, extNameBuf );
drv = drvBuf;
path = pathBuf;
name = nameBuf;
extName = extNameBuf;
return TRUE;
}
BOOL CDirManager::GetPathFromPathName( char const * pathName, CString & path )
{
CString drv, pathBuf, nameBuf;
SplitPathName( pathName, drv, pathBuf, nameBuf, nameBuf );
if( pathBuf.Find ( "\\\\" ) == 0 )
{//網絡路徑 ChK 2004-03-20
path = pathBuf;
}
else if( drv.IsEmpty() || pathBuf.IsEmpty() )
// if( drv.IsEmpty() || pathBuf.IsEmpty() )
{//下傳的不是全路徑
char curDir[MAX_PATH];
DWORD reLength = ::GetCurrentDirectory( MAX_PATH, curDir );
if( reLength == MAX_PATH || reLength <= 0 )
{
path.Empty();
return FALSE;
}
path = curDir + pathBuf;
}
else
{
path = drv + pathBuf;
}
return TRUE;
}
BOOL CDirManager::GetNameFromPathName( const char * pathName, CString & name )
{
CString strBuf, extName;
if( !SplitPathName( pathName, strBuf, strBuf, name, extName ) )
{
return FALSE;
}
name += extName;
return TRUE;
}
BOOL CDirManager::GetMajorNameFromPathName( const char * pathName, CString & name )
{
CString strBuf;
return SplitPathName( pathName, strBuf, strBuf, name, strBuf );
}
BOOL CDirManager::GetExtNameFromPathName( const char * pathName, CString & extName )
{
CString strBuf;
if( !SplitPathName( pathName, strBuf, strBuf, strBuf, extName ) )
{
return FALSE;
}
if( extName.IsEmpty() )
{
return FALSE;
}
return TRUE;
}
BOOL CDirManager::SetExtName( CString & fileName, const char * extName )
{
char path[MAX_PATH], drive[10], name[256], extNameBuf[256];
_splitpath( fileName, drive, path, name, extNameBuf );
fileName.Format( "%s%s%s%s", drive, path, name, extName );
return TRUE;
}
BOOL CDirManager::GetRootPath(CString &rootPath, const char *debugPath /*=NULL*/)
{
char pathBuf[_MAX_PATH];
pathBuf[0] = '\0';
if( ::GetModuleFileName( AfxGetApp()->m_hInstance, pathBuf, sizeof(pathBuf) ) > 0 )
{
char * pStr = strrchr( pathBuf, '\\' );
if( pStr != NULL )
{
*( pStr + 1 ) = '\0';
}
else
{
pathBuf[0] = '\0';
}
}
rootPath = pathBuf;
#ifdef _DEBUG
if( debugPath != NULL )
{
rootPath = debugPath;
if( !rootPath.IsEmpty() )
{
if( rootPath[rootPath.GetLength()-1] != '\\' )
{
rootPath += '\\';
}
}
return FALSE;
}
#endif
return TRUE;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -