⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dirmanager.cpp

📁 通过手机数据线连接手机
💻 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 + -