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

📄 publicfunction.cpp

📁 多线程下载的
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	ASSERT ( lpszFilePath );
	char chDirPart = '\\';
	if ( hwStrChr ( lpszFilePath, '/' ) )
		chDirPart = '/';

	if ( szOnlyFileName )
	{
		memset ( szOnlyFileName, 0, nFileNameSize );
	}
	if ( szOnlyPath )
	{
		memset ( szOnlyPath, 0, nPathSize );
	}
	
	WIN32_FILE_ATTRIBUTE_DATA FileAttrData;
	if ( GetFileAttributesEx ( lpszFilePath, GetFileExInfoStandard, (LPVOID)&FileAttrData ) &&
		( FileAttrData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY
		&& FileAttrData.dwFileAttributes != 0xffffffff ) //	本身就是目录
	{
		if ( szOnlyPath )
		{
			STRNCPY ( szOnlyPath, lpszFilePath, nPathSize );
			StandardizationPathBuffer ( szOnlyPath, nPathSize, chDirPart );
		}
		return TRUE;
	}

	char *p = hwStrrChr ( lpszFilePath, chDirPart );
	if ( !p )
	{
		STRNCPY ( szOnlyFileName, lpszFilePath, nFileNameSize );
		return TRUE;
	}

	if ( szOnlyFileName )
		STRNCPY ( szOnlyFileName, p+1, nFileNameSize );
	if ( szOnlyPath )
	{
		STRNCPY ( szOnlyPath, lpszFilePath, nPathSize );
		int nLen = p-lpszFilePath+1;
		if ( nPathSize-1 < nLen ) return FALSE;
		szOnlyPath [ nLen ] = '\0';
	}
	return TRUE;
}

BOOL PartPathAndFileAndExtensionName (
		IN LPCTSTR lpszFilePath,			// 全路径名(包含文件名)
		OUT CString *pcsOnlyPath,			// 输出光路径(没有文件名)
		OUT CString *pcsOnlyFileName,		// 输出光文件名(没有路径)
		OUT CString *pcsExtensionName		// 输出扩展名
	)
{
	char szOnlyPath[MAX_PATH] = {0};
	char szOnlyFileName[MAX_PATH] = {0};
	char szExtensionName[MAX_PATH] = {0};
	if ( !PartFileAndPathByFullPath ( lpszFilePath, szOnlyFileName, MAX_PATH, szOnlyPath, MAX_PATH ) )
		return FALSE;
	if ( !PartFileAndExtensionName ( szOnlyFileName, szOnlyFileName, MAX_PATH, szExtensionName, MAX_PATH ) )
		return FALSE;

	if ( pcsOnlyPath ) *pcsOnlyPath = szOnlyPath;
	if ( pcsOnlyFileName ) *pcsOnlyFileName = szOnlyFileName;
	if ( pcsExtensionName ) *pcsExtensionName = szExtensionName;

	return TRUE;
}

char *hwStrrChr ( const char *string, int c )
{
	if ( !string ) return NULL;
	CString csString = string;
	ReplaceChineseStrToEnglish ( csString.GetBuffer(0), (c>=127) ? (c-1) : (c+1) );
	csString.ReleaseBuffer ();
	int nPos = csString.ReverseFind ( c );
	if ( nPos < 0 ) return NULL;
	return ( (char*)string + nPos );
}

char *hwStrChr ( const char *string, int c )
{
	if ( !string ) return NULL;
	CString csString = string;
	ReplaceChineseStrToEnglish ( csString.GetBuffer(0), (c>=127) ? (c-1) : (c+1) );
	csString.ReleaseBuffer ();
	int nPos = csString.Find ( c );
	if ( nPos < 0 ) return NULL;
	return ( (char*)string + nPos );
}

//
// 根据文件名来找它的文件名和扩展名,如:完整路径为“E:\123\456.bmp”,那么 szFileName 等于“E:\123\456”
// szExtensionName 等于“bmp”
//
BOOL PartFileAndExtensionName (
		IN LPCTSTR lpszFileName,
		OUT char *szFileName,
		IN int nFileNameSize,
		OUT char *szExtensionName/*=NULL*/,
		IN int nExtensionNameSize/*=0*/ )
{
	ASSERT ( lpszFileName );
	char chDirPart = '\\';
	if ( hwStrChr ( lpszFileName, '/' ) )
		chDirPart = '/';

	if ( szFileName )
	{
		STRNCPY ( szFileName, lpszFileName, nFileNameSize );
	}
	if ( szExtensionName )
	{
		memset ( szExtensionName, 0, nExtensionNameSize );
	}

	char *p_Dot = hwStrrChr ( lpszFileName, '.' );
	if ( !p_Dot )
	{
		return TRUE;
	}
	char *p_Slash = hwStrrChr ( lpszFileName, chDirPart );
	if ( szFileName )
	{
		if ( p_Dot-lpszFileName >= nFileNameSize )
			return FALSE;
//		TRACE ( "%d, %d\n", p_Dot-lpszFileName, strlen(lpszFileName) );
		if ( int(p_Dot-lpszFileName) < (int)strlen(lpszFileName)-1 )
			szFileName [p_Dot-lpszFileName] = '\0';
	}
	if ( p_Slash > p_Dot )
	{
		return TRUE;
	}

	if ( szExtensionName )
	{
		STRNCPY ( szExtensionName, p_Dot+1, nExtensionNameSize );
	}
	return TRUE;
}

//
// 删除一个文件夹,不管里面有没有文件,都会被删除
//
BOOL hwDeleteFolder ( LPCTSTR lpszFolder )
{
	BOOL bRet = FALSE;
	CHwDir dir ( lpszFolder, TRUE, TRUE );
	if ( !dir.m_pStrAryResFile || !dir.m_pStrArySubDirectory )
		return FALSE;
	for ( int i=0; i<dir.m_pStrAryResFile->GetSize(); i++ )
	{
		CString csFileName = dir.m_pStrAryResFile->GetAt ( i );
		bRet = ::SetFileAttributes ( csFileName, FILE_ATTRIBUTE_NORMAL );
		bRet = ::DeleteFile ( csFileName );
		if ( !bRet )
		{
			DWORD dwLastError = ::GetLastError ();
		}
	}

	for ( i=dir.m_pStrArySubDirectory->GetSize()-1; i>=0; i-- )
	{
		CString csDirName = dir.m_pStrArySubDirectory->GetAt ( i );
		TRACE ( "Delete dir : %s\n", csDirName );
		bRet = ::SetFileAttributes ( csDirName, FILE_ATTRIBUTE_NORMAL );
		bRet = RemoveDirectory ( csDirName );
	}
	
	return RemoveDirectory ( lpszFolder );
}

//
// 将一个文件追加拷贝到另一个文件末尾
//
BOOL CopyFileAppend ( LPCTSTR lpszFileName_Src, LPCTSTR lpszFileName_Dst, int nOffset )
{
	if ( !lpszFileName_Src || !lpszFileName_Dst ) return FALSE;
	CFileStatus fileStatus;
	if ( !CFile::GetStatus(lpszFileName_Src,fileStatus) || fileStatus.m_size < 1 )
	{
		return TRUE;
	}

	const int nBufSize = 1024*1024;
	char *pTempBuf = new char[nBufSize];
	if ( !pTempBuf ) return FALSE;
	BOOL bRet = FALSE;
	CFile file_Src, file_Dst;
	TRY
	{
		if ( file_Src.Open ( lpszFileName_Src, CFile::modeRead|CFile::typeBinary ) &&
			file_Dst.Open ( lpszFileName_Dst, CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite|CFile::typeBinary ) )
		{
			if ( nOffset >= 0 )
			{
				file_Dst.Seek ( nOffset, CFile::begin );
			}
			else
			{
				file_Dst.SeekToEnd ();
			}
			int nReadSize = 0;
			while ( (nReadSize = file_Src.ReadHuge ( pTempBuf, nBufSize )) > 0 )
			{
				file_Dst.WriteHuge ( pTempBuf, nReadSize );
			}
			bRet = TRUE;
		}
	}
	CATCH( CFileException, e )
	{
		e->Delete ();
		bRet = FALSE;
	}
	END_CATCH

	if ( pTempBuf ) delete[] pTempBuf;
	if ( HANDLE_IS_VALID(file_Src.m_hFile) )
		file_Src.Close ();
	if ( HANDLE_IS_VALID(file_Dst.m_hFile) )
		file_Dst.Close ();

	return bRet;

}

//
// 创建一个空文件
//
BOOL CreateNullFile ( LPCTSTR lpszFileName, int nFileSize )
{
	if ( !lpszFileName ) return FALSE;
	DeleteFile ( lpszFileName );
	CFile file;
	BOOL bRet = TRUE;
	TRY
	{
		bRet = file.Open ( lpszFileName, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary );
		if ( bRet && nFileSize > 0 )
			file.SetLength ( nFileSize );
	}
	CATCH( CFileException, e )
	{
		e->Delete ();
		bRet = FALSE;
	}
	END_CATCH
	if ( !bRet )
	{
		Log ( L_WARNING, "Create file [%s] failed. %s", lpszFileName, hwFormatMessage ( GetLastError() ) );
	}
	if ( HANDLE_IS_VALID(file.m_hFile) )
		file.Close ();

	return bRet;
}

//
// 等待线程退出
//
BOOL WaitForThreadEnd ( HANDLE hThread, DWORD dwWaitTime /*=5000*/ )
{
	BOOL bRet = TRUE;
	if ( !HANDLE_IS_VALID(hThread) ) return TRUE;
	if ( ::WaitForSingleObject ( hThread, dwWaitTime ) == WAIT_TIMEOUT )
	{
		bRet = FALSE;
		::TerminateThread ( hThread, 0 );
	}
	return bRet;
}

/********************************************************************************
* Function Type	:	Global
* Parameter		:	lpszPathName			-	[out] 选择的路径名
* Return Value	:	路径字符数
* Description	:	从通用对话框中选择路径
* Note			:	缓冲lpszPathName申请时建议大小为“MAX_PATH”
*********************************************************************************/
DWORD SelectPathByCommonDlg(LPSTR lpszPathName,HWND hwndOwner/*=NULL*/)
{
	ASSERT_ADDRESS ( lpszPathName, MAX_PATH );
	DWORD dwLength = 0;
	LPMALLOC pMalloc;	//利用shell的扩展功能
	BROWSEINFO bi;
	if ( !SUCCEEDED(SHGetMalloc(&pMalloc)) )	//为生成目录选择对话框分配内存
		return 0;
	ZeroMemory( (char*)&bi, sizeof(bi) );
	LPITEMIDLIST pidl;
	bi.hwndOwner = hwndOwner;
	bi.pidlRoot = NULL;
	bi.pszDisplayName = (LPSTR)lpszPathName;
	bi.lpszTitle = _T("Select Directory");
	bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
	bi.lpfn = NULL;
	bi.lParam = 0;
	if ( (pidl = ::SHBrowseForFolder(&bi)) != NULL)//调用选择目录对话框
	{
		// 获得所选择的目录
		if ( ::SHGetPathFromIDList(pidl, (LPSTR)lpszPathName) )
		{
			dwLength = (DWORD)strlen((const char*)lpszPathName);
			if(lpszPathName[dwLength-1] != '\\')
			{
				strcat((char*)lpszPathName,"\\");
				dwLength ++;
			}
		}
		pMalloc->Free(pidl);//释放分配的资源
	}
	pMalloc->Release();
	return dwLength;
}

BOOL SelectPathByCommonDlg ( CWnd *pDlg, UINT nEditID )
{
	if ( !pDlg || nEditID < 1 ) return FALSE;
	char szPath[MAX_PATH] = {0};
	if ( SelectPathByCommonDlg ( szPath, pDlg->GetSafeHwnd() ) > 0 )
	{
		pDlg->SetDlgItemText ( nEditID, szPath );
		return TRUE;
	}
	return FALSE;
}

//
// 标准化路径缓冲,如果不是以“\”结尾,将自动加上
//
void StandardizationPathBuffer ( char *szPath, int nSize, char cFlagChar/*='\\'*/ )
{
	int nLen = strlen(szPath);
	if ( nLen < 1 ) return;
	ASSERT_ADDRESS ( szPath, nLen+1 );
	char szTemp[4] = {0};
	szTemp[0] = cFlagChar;
	if ( szPath[nLen-1] != cFlagChar )
		strncat ( szPath, szTemp, nSize );

	CString csPath = StandardizationFileForPathName ( szPath, FALSE );
	strncpy ( szPath, csPath, nSize );
}

//
// 标准化路径或文件名,把不符合文件名命名规则的字符替换成指定的字符。//u 在 StandardizationPathBuffer() 中
// 加上该函数
//
CString StandardizationFileForPathName ( LPCTSTR lpszFileOrPathName, BOOL bIsFileName, char cReplaceChar/*='_'*/ )
{
	CString csFileOrPathName = GET_SAFE_STRING(lpszFileOrPathName);
	CString csHead, csTail;
	// 路径名中最后一个'\\'是正常的。另外类似“c:\\”的字符也是正常的。所以先提取出来不参与后面的替换,等替换完以后再补回来
	if ( !bIsFileName )
	{
		if ( csFileOrPathName.GetLength() >= 1 && (csFileOrPathName[csFileOrPathName.GetLength()-1] == '\\' || csFileOrPathName[csFileOrPathName.GetLength()-1] == '/') )
		{
			csTail += csFileOrPathName[csFileOrPathName.GetLength()-1];
			csFileOrPathName = csFileOrPathName.Left ( csFileOrPathName.GetLength()-1 );
		}

		if ( csFileOrPathName.GetLength() >= 2 && isalpha(csFileOrPathName[0]) && csFileOrPathName[1]==':' )
		{
			csHead = csFileOrPathName.Left(2);
			csFileOrPathName = csFileOrPathName.Mid(2);
		}
		if ( csFileOrPathName.GetLength() >= 1 && (csFileOrPathName[0]=='\\' || csFileOrPathName[0]=='/') )
		{
			csHead += csFileOrPathName[0];
			csFileOrPathName = csFileOrPathName.Mid(1);
		}
	}
	else
	{
		csFileOrPathName.Replace ( "\\", "_" );
		csFileOrPathName.Replace ( "/", "_" );
	}
	csFileOrPathName.Replace ( ":", "_" );
	csFileOrPathName.Replace ( "*", "_" );
	csFileOrPathName.Replace ( "?", "_" );
	csFileOrPathName.Replace ( "\"", "_" );
	csFileOrPathName.Replace ( "<", "_" );
	csFileOrPathName.Replace ( ">", "_" );
	csFileOrPathName.Replace ( "|", "_" );
	csFileOrPathName.Insert ( 0, csHead );
	csFileOrPathName += csTail;

	return csFileOrPathName;
}

//
// 格式化表示文件大小的数字,如 2048 被格式化为“2.00 k”
//
CString FormatFileSize ( double fFileSize )
{
	CString csRet;
	if ( fFileSize < 1024.0 )
	{
		csRet.Format ( "%d b", fFileSize );
	}
	else if ( fFileSize >= 1024.0 && fFileSize < 1024.0*1024.0 )
	{
		csRet.Format ( "%.2f K", fFileSize/1024.0 );
	}
	else if ( fFileSize >= 1024.0*1024.0 && fFileSize < 1024.0*1024.0*1024.0 )
	{
		csRet.Format ( "%.2f M", fFileSize/(1024.0*1024.0) );
	}
	else
	{
		csRet.Format ( "%.2f G", fFileSize/(1024.0*1024.0*1024.0) );
	}
	return csRet;
}

/********************************************************************************
* Function Type	:	public
* Parameter		:	buf		-	输出缓冲
* Return Value	:	字符个数
* Description	:	获取当前时间的字符串,如:2003-10-01 12:00:00
*********************************************************************************/
DWORD GetCurTimeString ( char *buf, time_t tNow/*=0*/ )
{
	ASSERT_ADDRESS ( buf, DATETIME_TYPE_LENGTH );
	if ( tNow == 0 ) tNow = time(NULL);
	CTime cTime ( tNow );
	CString csNow = cTime.Format ( "%Y-%m-%d %H:%M:%S" );
	return (DWORD)_snprintf ( buf, DATETIME_TYPE_LENGTH, "%s", csNow );
}

CString GetCurTimeString ( time_t tNow/*=0*/ )
{
	char szTime[DATETIME_TYPE_LENGTH+1] = {0};
	GetCurTimeString ( szTime, tNow );
	return szTime;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -