123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- // ffsco.cpp: implementation of the helper_coffs::ffsco class.
- //
- //////////////////////////////////////////////////////////////////////
- /*
- * Copyright 2004, ??? (www.???.com). All rights reserved.
- * Copyright 1990-2004, ???.
- *
- * See the COPYING file for the terms of usage and distribution.
- */
- //////////////////////////////////////////////////////////////////////
- // ...
- // create@2004-03-17
- // by ???
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h" //--i want remove it
- #include "ffscoex.h"
- #include <windows.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #ifdef _UNICODE
- #include "CharacterConvert.h"
- #endif //#ifdef _UNICODE
- using helper_coffs::LPSFILEINFO;
- using helper_coffs::SFILEINFO;
- namespace helper_coffs
- {
-
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
-
- ffscoex::ffscoex()
- : _limit(def_limit)
- , _dirs(0)
- , _strroot("")
- {
-
- }
-
- ffscoex::~ffscoex()
- {
- Clear();
- }
-
- //////////////////////////////////////////////////////////////////////
- string ffscoex::_uppercase_(IN string& s)
- {
- const char aazz = 'z' - 'Z';
- string rs;
- for (string::iterator it = s.begin(); s.end() != it; it++)
- {
- if ('a' <= *it && *it <= 'z')
- rs.append(1, *it - aazz);
- else
- rs.append(1, *it);
- }
- return rs;
- }
- //////////////////////////////////////////////////////////////////////
-
- /************************************************************************/
- /*
- 函数: match
- 描述: 文件匹配检测
- 参数:
- IN string fext_, 文件后叠
- IN string file_ 文件
- 返回:
- */
- /************************************************************************/
- int ffscoex::match(IN string fext_, IN string file_)
- {
- string fext = _uppercase_(fext_);
- string file = _uppercase_(file_);
-
- int pos = file.find_last_of('.');
- if (string::npos != pos)
- file = file.substr(pos);
-
- return (string::npos != fext.find(file));
- }
-
- /************************************************************************/
- /*
- 函数: limit
- 描述: 设置结果文件数目上限
- 参数:
- IN limit_ 文件数目上限
- 返回:
- */
- /************************************************************************/
- void ffscoex::limit(IN int limit_)
- {
- if (limit_ < 1)
- _limit = def_limit;
- if (limit_ < max_limit)
- _limit = limit_;
- }
-
- /************************************************************************/
- /*
- 函数: dir
- 描述: 判断s是否为目录
- 参数:
- IN string& s 目录
- 返回:
- */
- /************************************************************************/
- int ffscoex::dir(IN string& s)
- {
- #ifdef _UNICODE
- WCHAR wszFile[MAX_PATH] = {0};
- utf82unicode(s.c_str(), wszFile);
- return (FILE_ATTRIBUTE_DIRECTORY == GetFileAttributes(wszFile));
- #else
- return (FILE_ATTRIBUTE_DIRECTORY == GetFileAttributes(s.c_str()));
- #endif //#ifdef _UNICODE
- }
- /************************************************************************/
- /*
- 函数: find
- 描述: 查找文件
- 参数:
- IN string& path_, 目录路径
- IN string& fext_ 文件后叠
- 返回:
- */
- /************************************************************************/
- int ffscoex::find(IN string path_, IN string fext_)
- {
- recursivefind(path_, path_, fext_);
-
- //--dir
- for (typeT::iterator it_dir = _co_dir.begin(); _co_dir.end() != it_dir; it_dir++)
- _co.push_back(*it_dir);
-
- //--file
- for (typeT::iterator it_file = _co_file.begin(); _co_file.end() != it_file; it_file++)
- _co.push_back(*it_file);
-
- return count();
- }
- /************************************************************************/
- /*
- 函数: recursivefind
- 描述: 递归查找文件
- 参数:
- IN string root_, 相对要查找的目录作为根目录,注:不是磁盘的根目录
- IN string path_, 目录路径
- IN string fext_ 文件后叠
- 返回:
- */
- /************************************************************************/
- void ffscoex::recursivefind(IN const string& root_, IN string path_, IN string fext_)
- {
- string path = path_;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
-
- string fext = fext_;
- if (0 == fext.compare("*") || 0 == fext.compare("*.*"))
- fext = "";
-
- //string file = fext;
- string file = "*";
- string s = path + file;
-
- WIN32_FIND_DATA fileinfo;
- memset(&fileinfo, 0, sizeof(WIN32_FIND_DATA));
- #ifdef _UNICODE
- WCHAR wszFileName[MAX_PATH] = {0};
- ascii2unicode(s.c_str(), wszFileName);
- HANDLE handle = FindFirstFile( wszFileName, &fileinfo );
- #else
- HANDLE handle = FindFirstFile( s.c_str(), &fileinfo );
- #endif //#ifdef _UNICODE
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- //添加当前目录
- LPSFILEINFO lpCurrDir = new SFILEINFO;
- memcpy(lpCurrDir, &fileinfo, sizeof(WIN32_FIND_DATA));
- lpCurrDir->strRootDir = root_;
- lpCurrDir->strAllPath = path;
- _co_dir.push_back(lpCurrDir);
- do
- {
- if ('.' != fileinfo.cFileName[0])
- {
- //目录
- if(FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) //--目录
- {
- LPSFILEINFO lpSubDir = new SFILEINFO;
- memcpy(lpSubDir, &fileinfo, sizeof(WIN32_FIND_DATA));
- lpSubDir->strRootDir = root_;
- lpSubDir->strAllPath = path;
- #ifdef _UNICODE
- char szFileName[MAX_PATH] = {0};
- unicode2acsii(fileinfo.cFileName, szFileName);
- string strSubDir = path;
- strSubDir.append(szFileName);
- #else
- //子目录
- string strSubDir = lpSubDir->strAllPath + lpSubDir->cFileName;
- #endif //#ifdef _UNICODE
- //是否查找子目录
- if (1 == dirs())
- recursivefind(root_, strSubDir, fext);
- delete lpSubDir;
- }
- else
- {
- //文件
- LPSFILEINFO lpSFileInfo = new SFILEINFO;
- memcpy(lpSFileInfo, &fileinfo, sizeof(WIN32_FIND_DATA));
- lpSFileInfo->strRootDir = root_;
- #ifdef _UNICODE
- char szFileName[MAX_PATH] = {0};
- unicode2acsii(fileinfo.cFileName, szFileName);
- if (0 == fext.size() || match(fext, szFileName))
- {
- lpSFileInfo->strAllPath = path;
- lpSFileInfo->strAllPath.append(szFileName);
- _co_file.push_back(lpSFileInfo);
- }
- else
- {
- if(lpSFileInfo)
- delete lpSFileInfo;
- }
- #else
- if (0 == fext.size() || match(fext, fileinfo.cFileName))
- {
- lpSFileInfo->strAllPath = path + fileinfo.cFileName;
- _co_file.push_back(lpSFileInfo);
- }
- else
- {
- if(lpSFileInfo)
- delete lpSFileInfo;
- }
- #endif //#ifdef _UNICODE
-
- }
- }
-
- } while (FindNextFile( handle, &fileinfo ));
-
- FindClose(handle);
- }
- }
- /************************************************************************/
- /*
- 函数: Clear
- 描述: 清除
- 参数:
- 返回:
- */
- /************************************************************************/
- void ffscoex::Clear()
- {
- typeT::iterator it = _co_file.begin();
- for(;it!=_co_file.end();)
- {
- LPSFILEINFO lp = (*it);
- lp = NULL;
- ++it;
- }
- _co_file.clear();
- it = _co_dir.begin();
- for(;it!=_co_dir.end();)
- {
- LPSFILEINFO lp = (*it);
- lp = NULL;
- ++it;
- }
- _co_dir.clear();
- it = _co.begin();
- for(;it!=_co.end();)
- {
- LPSFILEINFO lp = (*it);
- if(lp != NULL)
- delete lp;
- lp = NULL;
- ++it;
- }
- _co.clear();
- }
- /************************************************************************/
- /*
- 函数: deldir
- 描述: 删除目录
- 参数:
- IN LPCTSTR pDir 目录
- 返回:
- */
- /************************************************************************/
- int ffscoex::deldir(IN LPCTSTR lpDir)
- {
- #ifdef _UNICODE
- if(lpDir == NULL || wcscmp(lpDir, _T("")) == 0)
- return 0;
- #else
- if(lpDir == NULL || strcmp(lpDir, "") == 0)
- return 0;
- #endif
- if( !PathFileExists(lpDir))
- return 0;
- TCHAR szPath[MAX_PATH] = {0};
- #ifdef _UNICODE
- wcscpy(szPath, lpDir);
- #else
- strcpy(szPath, lpDir);
- #endif
- lstrcat(szPath, _T("\\*"));
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(szPath, &fileinfo);
- if (NULL == handle && INVALID_HANDLE_VALUE == handle)
- return 0;
- do
- {
- // '.'和 '..'的系统文件去除;
- if (_T('.') != fileinfo.cFileName[0])
- {
- TCHAR szCurrPath[MAX_PATH] = {0};
- #ifdef _UNICODE
- wcscpy(szCurrPath, lpDir);
- #else
- strcpy(szCurrPath, lpDir);
- #endif
- lstrcat(szCurrPath, _T("\\"));
- lstrcat(szCurrPath, fileinfo.cFileName);
- SetFileAttributes(szCurrPath, FILE_ATTRIBUTE_NORMAL);//去掉所有属性;
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY) // 目录;
- {
- deldir(szCurrPath);
- }
- else
- {
- if(!DeleteFile(szCurrPath))
- {
- DWORD dwError = GetLastError();
- //LOG4C_NO_FILENUM((LOG_NOTICE,"删除文件出错:%d",dwError));
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- //删除目录
- if(!RemoveDirectory(lpDir))
- {
- DWORD dwError = GetLastError();
- //LOG4C_NO_FILENUM((LOG_NOTICE,"删除文件夹出错:%d",dwError));
- //OutputDebugString(_T("**************删除目录失败**************\n"));
- return 0;
- }
- return 1;
- }
-
- }; //--namespace helper_coffs
|