Files
2022-10-26 12:25:11 +08:00

252 lines
5.7 KiB
C++

#include "StdAfx.h"
#include ".\fileutil.h"
#include "shellapi.h"
#include "shlwapi.h"
#include <string>
#include <stdio.h>
FileUtil::FileUtil(void)
{
}
FileUtil::~FileUtil(void)
{
}
//파일경로를 디렉토리와 파일이름으로 분리한다.(파일이름에는 확장자가 있다.)
BOOL FileUtil::GetSplitFilePath( const TCHAR* pszFilePath, OUT TCHAR* pszDir, WORD wDirSize, OUT TCHAR* pszFile, WORD wFileSize )
{
ZeroMemory( pszDir, MAX_PATH );
ZeroMemory( pszFile, MAX_PATH );
if( pszFilePath == NULL )
return FALSE;
TCHAR szDir[MAX_PATH];
TCHAR szFileName[MAX_PATH];
TCHAR szFileExt[MAX_PATH];
//분리..
_splitpath( pszFilePath, NULL, szDir, szFileName, szFileExt );
//디렉토리
if( szDir == NULL || (strcmp( szDir, "") == 0) )
_snprintf( pszDir, wDirSize, "\\" );
else
_snprintf( pszDir, wDirSize, "%s", szDir );
pszDir[wDirSize-1] = '0';
//파일이름
_snprintf( pszFile, wFileSize, "%s%s", szFileName, szFileExt);
pszFile[wFileSize-1] = '0';
return TRUE;
}
//자식의 경로에서 부모의 경로를 구한다.
VOID FileUtil::GetParentDirectory( const TCHAR* pszChildPath, OUT TCHAR* pszParentPath, int nSize )
{
std::string strTemp = pszChildPath;
int idx = (int)strTemp.rfind( "\\" );
if( idx != std::string::npos )
{
std::string strParent = strTemp.substr( idx+1, strTemp.length() - idx );
strncpy( pszParentPath, strParent.c_str(), nSize );
}
else //루트다.
{
ZeroMemory( pszParentPath, nSize );
}
}
//자식의 경로에서 부모의 경로를 구한다.
VOID FileUtil::FTPGetParentDirectory( const TCHAR* pszChildPath, OUT TCHAR* pszParentPath, int nSize )
{
std::string strTemp = pszChildPath;
//FTP는 "/"로 루트를 구분한다.
int idx = (int)strTemp.rfind( "/" );
if( idx != std::string::npos )
{
std::string strParent;
if( idx == 0 )
strParent = strTemp.substr( 0, idx+1 );
else
strParent = strTemp.substr( 0, idx );
strncpy( pszParentPath, strParent.c_str(), nSize );
}
else //못 찾으면 루트로 간주...
{
_snprintf( pszParentPath, MAX_PATH, "/" );
}
}
#ifdef __20080312__SOLARAUTH_SERVER_PATCH
VOID FileUtil::FTPGetParentDirectoryEx(const TCHAR* pszChildPath, OUT TCHAR* pszParentPath, int nSize)
{
std::string strTemp = pszChildPath;
remove_name:
int idx = (int)strTemp.rfind( "/" );
if( idx != std::string::npos )
{
std::string strParent;
if( idx == 0 )
strParent = strTemp.substr( 0, idx+1 );
else if( idx+1 == strTemp.size() )
{
strTemp = strTemp.substr( 0, idx );
goto remove_name;
}
else
strParent = strTemp.substr( 0, idx );
strncpy( pszParentPath, strParent.c_str(), nSize );
}
else //못 찾으면 루트로 간주...
{
_snprintf( pszParentPath, MAX_PATH, "/" );
}
}
#endif
//경로중 제일 마지막 디렉토리 이름을 얻는다.
//들어오는 경로 형태는 C:\\AAA\BBB 이런식이어야 한다.
BOOL FileUtil::GetLastDirectoryName( const TCHAR* pszFilePath, OUT TCHAR* pszLastDirName, WORD wDirSize )
{
std::string strTemp = pszFilePath;
// 제일 끝이 파일인지 디렉토리인지 구분..
WIN32_FIND_DATA FindData;
HANDLE hFindFile = FindFirstFile( pszFilePath, &FindData );
if( hFindFile == INVALID_HANDLE_VALUE )
return FALSE;
if( FindData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY ) //파일이면..
{
int idx = (int)strTemp.rfind( "\\" );
if( idx != std::string::npos )
{
std::string strSubDir = strTemp.substr( 0, idx );
int nSubIdx = (int)strSubDir.rfind( "\\" );
if( nSubIdx != std::string::npos )
{
std::string strDir = strSubDir.substr( nSubIdx+1, strSubDir.length() - nSubIdx );
strncpy( pszLastDirName, strDir.c_str(), wDirSize );
}
else //C:\\FileName.exe와 같은 형태이다.
{
std::string strDir = strTemp.substr( 0, strTemp.length() - idx );
strncpy( pszLastDirName, strDir.c_str(), wDirSize );
}
}
else //루트다.
{
ZeroMemory( pszLastDirName, wDirSize );
}
}
else //끝이 디렉토리 이다.
{
int idx = (int)strTemp.rfind( "\\" );
if( idx != std::string::npos )
{
std::string strDir = strTemp.substr( idx+1, strTemp.length() - idx );
strncpy( pszLastDirName, strDir.c_str(), wDirSize );
}
else //루트다.
{
ZeroMemory( pszLastDirName, wDirSize );
}
}
FindClose( hFindFile );
return TRUE;
}
BOOL FileUtil::CopyFoler( const TCHAR* pszFrom, const TCHAR* pszTo )
{
//먼저 pszTo 폴더의 디렉토리를 생성한다.
CreteFolder( pszTo );
TCHAR fromPath[MAX_PATH+2];
memset(fromPath, 0, sizeof(fromPath));
strcpy(fromPath, pszFrom);
TCHAR toPath[MAX_PATH+2];
memset(toPath, 0, sizeof(toPath));
strcpy(toPath, pszTo);
SHFILEOPSTRUCT shfo = {0};
ZeroMemory(&shfo, sizeof shfo);
shfo.hwnd = NULL;
shfo.wFunc = FO_COPY;
shfo.fFlags = FOF_SILENT | FOF_NOERRORUI | FOF_NOCONFIRMATION;
shfo.lpszProgressTitle = "폴더 복사";
shfo.fAnyOperationsAborted = false;
shfo.pTo = toPath;
shfo.pFrom = fromPath;
int nResult = SHFileOperation(&shfo);
if( nResult == 0 ) return TRUE;
return FALSE;
}
BOOL FileUtil::DeleteFoler( const TCHAR* pszPath )
{
BOOL bRet = TRUE;
SHFILEOPSTRUCT *SHELL = new SHFILEOPSTRUCT;
memset( (void*)SHELL,0,sizeof(SHFILEOPSTRUCT) );
char buff[255];
memset(buff,'\0',sizeof(buff));
sprintf(buff, "%s\0\0", pszPath ); //2개 넣는게 중요..
SHELL->wFunc = FO_DELETE;
SHELL->pFrom = buff;
SHELL->fFlags = FOF_NOERRORUI | FOF_NOCONFIRMATION;
if( strstr( pszPath, "*") ) //wild card
{
SHELL->fFlags |= FOF_FILESONLY;
SHELL->fFlags |= FOF_MULTIDESTFILES;
}
if( SHFileOperation(SHELL) != 0) bRet = FALSE;
delete SHELL;
return bRet;
}
void FileUtil::CreteFolder( const TCHAR* pszFoletPath )
{
int len = (int)strlen(pszFoletPath) + 1;
char *buf = new char[len];
strcpy(buf, pszFoletPath);
for(int idx = 0; idx < len; idx++)
{
if(buf[idx] && buf[idx] != '\\')
continue;
buf[idx] = '\0';
if( PathIsDirectory(buf) == FALSE )
CreateDirectory(buf, NULL);
buf[idx] = '\\';
}
delete[] buf;
}