#include "StdAfx.h" #include ".\fileutil.h" #include "shellapi.h" #include "shlwapi.h" #include #include 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; }