123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- #include "stdafx.h"
- #include "ffscoex.h"
- #include <windows.h>
- #ifdef _UNICODE
- #include "CharacterConvert.h"
- #endif
- using helper_coffs::LPSFILEINFO;
- using helper_coffs::SFILEINFO;
- namespace helper_coffs
- {
-
-
-
-
-
- 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;
- }
-
-
-
-
-
- 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));
- }
-
-
-
-
- void ffscoex::limit(IN int limit_)
- {
- if (limit_ < 1)
- _limit = def_limit;
- if (limit_ < max_limit)
- _limit = limit_;
- }
-
-
-
-
- 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
- }
-
-
-
- int ffscoex::find(IN string path_, IN string fext_)
- {
- recursivefind(path_, path_, fext_);
-
-
- for (typeT::iterator it_dir = _co_dir.begin(); _co_dir.end() != it_dir; it_dir++)
- _co.push_back(*it_dir);
-
-
- for (typeT::iterator it_file = _co_file.begin(); _co_file.end() != it_file; it_file++)
- _co.push_back(*it_file);
-
- return count();
- }
-
-
-
- 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 = "*";
- 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
- 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
-
- 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
-
- }
- }
-
- } while (FindNextFile( handle, &fileinfo ));
-
- FindClose(handle);
- }
- }
-
-
-
- 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();
- }
-
-
-
- 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();
-
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
-
- if(!RemoveDirectory(lpDir))
- {
- DWORD dwError = GetLastError();
-
-
- return 0;
- }
- return 1;
- }
-
- };
|