123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877 |
- #include "StdAfx.h"
- #include "findfile.h"
- #include <algorithm>
- CONST size_t nMaxLimit = 0xFFFFFFFF;
- CONST size_t nDefLimit = 0xFFFFF;
- CONST TCHAR c_pathSeparator = _T('\\');
- CONST TString s_pathSeparator = _T("\\");
- CONST TString g_sVertical = _T("|");
- CONST TString g_sCorrectExt = _T("*.jpg|*.jpeg|*.png|*.bmp|*.cr2|*.nef|*.raw");
- TString lowercase(IN const TString &Str)
- {
- #if 1
- TString sResult;
- for ( TString::const_iterator it = Str.begin(); it != Str.end(); it++ )
- {
- if ( *it < 0 )
- {
- sResult.append(1, *(it++));
- sResult.append(1, *it);
- }
- else
- {
- if (_T('A') <= *it && *it <= _T('Z'))
- sResult.append(1, *it + 32);
- else
- sResult.append(1, *it);
- }
- }
- return sResult;
- #else
- TString sResult;
- std::transform(Str.begin(), Str.end(), sResult.begin(), ::toupper);
-
- static CString str = _T("");
- str = Str.c_str();
- str.MakeLower();
- sResult = str.GetString();
- return sResult;
- #endif
- }
- TString uppercase(IN const TString &Str)
- {
- #if 1
- TString sResult;
- for ( TString::const_iterator it = Str.begin(); it != Str.end(); it++ )
- {
- if ( *it < 0 )
- {
- sResult.append(1, *(it++));
- sResult.append(1, *it);
- }
- else
- {
- if (_T('A') <= *it && *it <= _T('Z'))
- sResult.append(1, *it - 32);
- else
- sResult.append(1, *it);
- }
- }
- return sResult;
- #else
- TString sResult;
- static CString strTemp = _T("");
- strTemp = Str.c_str();
- strTemp.MakeUpper();
- sResult = strTemp.GetString();
- return sResult;
- #endif
- }
- TString getfilename(IN CONST TString &file)
- {
- TString name;
- TString strfile = file;
- int nIndex = strfile.find_last_of(_T('\\'));
- if (nIndex == TString::npos)
- {
- nIndex = strfile.find_last_of(_T('.'));
- if ( nIndex == TString::npos )
- return _T("");
- return strfile.substr(0, nIndex);
- }
- name = strfile.substr(nIndex+1);
- nIndex = name.find_last_of(_T('.'));
- if (nIndex == TString::npos)
- return _T("");
- return name.substr(0, nIndex);
- }
- BOOL IsStringExistNoCase(IN CONST TString& str, IN STR_VEC &tagVt)
- {
- int nSize = tagVt.size();
- if (nSize == 0) return FALSE;
- BOOL bExist = FALSE;
- TString stmp1 = uppercase(str);
- TString stmp2;
- for (STR_VEC::iterator it = tagVt.begin(); it != tagVt.end(); it++)
- {
- stmp2 = uppercase(*it);
- if (stmp1.compare(stmp2) == 0)
- {
- bExist = TRUE;
- break;
- }
- }
- return bExist;
- }
- findfile::findfile(void):
- m_nlimit(nDefLimit),
- m_pvtfiles(NULL),
- m_pvtnames(NULL),
- m_pvtfolders(NULL),
- m_pvtfiles_sth(NULL),
- m_pvtfiles_mth(NULL)
- {
- groupExt();
- }
- findfile::~findfile(void)
- {
- }
- void findfile::groupExt()
- {
-
- INT nIndex = 0;
- TString strtmp;
- TString strEffctExt = g_sCorrectExt;
- strEffctExt.append(_T("|"));
- do
- {
- nIndex = strEffctExt.find(_T('|'));
- if (nIndex != TString::npos)
- {
- strtmp = strEffctExt.substr(0, nIndex);
- strEffctExt = strEffctExt.substr(nIndex + 1);
- if (strtmp.compare(_T("*.*")) && strtmp.size())
- {
- if ( !IsStringExistNoCase(strtmp,m_vtEffctExt) )
- m_vtEffctExt.push_back(strtmp);
- }
- }
- } while (strEffctExt.find(_T('|')) != TString::npos);
- }
- void findfile::groupExt( IN CONST TString &exts, IN STR_VEC &vtExts )
- {
-
- INT nIndex = 0;
- TString strtmp;
- TString strEffctExt = exts;
- strEffctExt.append(_T("|"));
- do
- {
- nIndex = strEffctExt.find(_T('|'));
- if (nIndex != TString::npos)
- {
- strtmp = strEffctExt.substr(0, nIndex);
- strEffctExt = strEffctExt.substr(nIndex + 1);
- if (strtmp.compare(_T("*.*")) && strtmp.size())
- {
- if ( !IsStringExistNoCase(strtmp,vtExts) )
- vtExts.push_back(strtmp);
- }
- }
- } while (strEffctExt.find(_T('|')) != TString::npos);
- }
- BOOL findfile::iscorrectext(IN const TString &fext, OUT TString* lpMistakenExt )
- {
- if (fext.size() == 0) return FALSE;
- if (fext.find(_T("*.*")) != TString::npos) return TRUE;
- TString ext = lowercase(fext);
- if (ext[ext.length() - 1] != _T('|'))
- ext.append(g_sVertical);
- BOOL bret = TRUE;
- int nIndex = 0;
- do
- {
- nIndex = ext.find(_T('|'));
- if (nIndex != TString::npos)
- {
- if (g_sCorrectExt.find(ext.substr(0, nIndex)) == TString::npos)
- {
- if (lpMistakenExt)
- *lpMistakenExt = ext.substr(0, nIndex);
- bret = FALSE;
- break;
- }
- ext = ext.substr(nIndex + 1);
- }
- } while (ext.find(_T('|')) != TString::npos);
- return bret;
- }
- TString findfile::lowercase(IN const TString &Str)
- {
- TString sResult;
- for ( TString::const_iterator it = Str.begin(); it != Str.end(); it++ )
- {
- if ( *it < 0 )
- {
- sResult.append(1, *(it++));
- sResult.append(1, *it);
- }
- else
- {
- if (_T('A') <= *it && *it <= _T('Z'))
- sResult.append(1, *it + 32);
- else
- sResult.append(1, *it);
- }
- }
- return sResult;
- }
- void findfile::lowercase(IN TString& Str)
- {
- for ( TString::iterator it = Str.begin(); it != Str.end(); it++ )
- {
- if ( *it < 0 )
- {
- it++;
- }
- else
- {
- if (_T('A') <= *it && *it <= _T('Z'))
- *it += 32;
- }
- }
- }
- TString findfile::uppercase(IN const TString &Str)
- {
- TString sResult;
- for ( TString::const_iterator it = Str.begin(); it != Str.end(); it++ )
- {
- if ( *it < 0 )
- {
- sResult.append(1, *(it++));
- sResult.append(1, *it);
- }
- else
- {
- if (_T('a') <= *it && *it <= _T('z'))
- sResult.append(1, *it - 32);
- else
- sResult.append(1, *it);
- }
- }
- return sResult;
- }
- void findfile::uppercase(IN TString& Str)
- {
- for ( TString::iterator it = Str.begin(); it != Str.end(); it++ )
- {
- if ( *it < 0 )
- {
- it++;
- }
- else
- {
- if (_T('a') <= *it && *it <= _T('z'))
- *it -= 32;
- }
- }
- }
- void findfile::setlimit(IN CONST INT &nLimit)
- {
- if (nLimit < 1)
- m_nlimit = nDefLimit;
- if (nLimit < nMaxLimit)
- m_nlimit = nLimit;
- }
- TString findfile::getfilename(IN CONST TString &file)
- {
- TString name;
- TString strfile = file;
- int nIndex = strfile.find_last_of(_T('\\'));
- if (nIndex == TString::npos)
- {
- nIndex = strfile.find_last_of(_T('.'));
- if ( nIndex == TString::npos )
- return _T("");
- return strfile.substr(0, nIndex);
- }
- name = strfile.substr(nIndex+1);
- nIndex = name.find_last_of(_T('.'));
- if (nIndex == TString::npos)
- return _T("");
- return name.substr(0, nIndex);
- }
- void findfile::findall(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
-
- if (checklimit()) break;
-
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)
- {
- m_pvtfolders->push_back(path + fileinfo.cFileName);
- findall(path + fileinfo.cFileName);
- }
- else
- {
- if (!checklimit())
- {
- m_pvtnames->push_back(fileinfo.cFileName);
- m_pvtfiles->push_back(path + fileinfo.cFileName);
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
- void findfile::findsubfolder(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)
- {
- m_pvtfolders->push_back(path + fileinfo.cFileName);
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
- void findfile::findallsubfolder(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)
- {
- m_pvtfolders->push_back(path + fileinfo.cFileName);
- findallsubfolder(path + fileinfo.cFileName);
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
- void findfile::findfiles_findin_subfolder(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
-
- if (checklimit()) break;
-
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)
- {
- findfiles_findin_subfolder(path + fileinfo.cFileName);
- }
- else
- {
- if (!checklimit())
- {
- m_pvtfiles->push_back(path + fileinfo.cFileName);
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
- void findfile::findfiles_findout_subfolder(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
-
- if (checklimit()) break;
-
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) != FILE_ATTRIBUTE_DIRECTORY)
- {
- if (!checklimit())
- {
- m_pvtfiles->push_back(path + fileinfo.cFileName);
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
- void findfile::findfiles_within_subfolder(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
-
- if (checklimit()) break;
-
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)
- {
- m_pvtfolders->push_back(path + fileinfo.cFileName);
- findfiles_within_subfolder(path + fileinfo.cFileName);
- }
- else
- {
- if (!checklimit())
- {
- m_pvtfiles->push_back(path + fileinfo.cFileName);
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
- void findfile::findnames_findin_subfolder(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
-
- if (checklimit()) break;
-
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)
- {
- findnames_findin_subfolder(path + fileinfo.cFileName);
- }
- else
- {
- if (!checklimit() )
- {
- m_pvtnames->push_back(fileinfo.cFileName);
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
- void findfile::findnames_findout_subfolder(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
-
- if (checklimit()) break;
-
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) != FILE_ATTRIBUTE_DIRECTORY)
- {
- if (!checklimit())
- {
- m_pvtnames->push_back(fileinfo.cFileName);
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
- void findfile::findnames_within_subfolder(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
-
- if (checklimit()) break;
-
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)
- {
- m_pvtfolders->push_back(path + fileinfo.cFileName);
- findnames_within_subfolder(path + fileinfo.cFileName);
- }
- else
- {
- if (!checklimit())
- {
- m_pvtnames->push_back(fileinfo.cFileName);
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
- void findfile::findfilesnames_findin_subfolder(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
-
- if (checklimit()) break;
-
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)
- {
- findfilesnames_findin_subfolder(path + fileinfo.cFileName);
- }
- else
- {
- if (!checklimit() )
- {
- m_pvtnames->push_back(fileinfo.cFileName);
- m_pvtfiles->push_back(path + fileinfo.cFileName);
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
- void findfile::findfilesnames_findout_subfolder(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
-
- if (checklimit()) break;
-
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) != FILE_ATTRIBUTE_DIRECTORY)
- {
- if (!checklimit() )
- {
- m_pvtnames->push_back(fileinfo.cFileName);
- m_pvtfiles->push_back(path + fileinfo.cFileName);
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
- void findfile::findfilesnames_within_subfolder(IN CONST TString& folder)
- {
- TString path = folder;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
- TString file = _T("*");
- TString s = path + file;
- WIN32_FIND_DATA fileinfo = { 0 };
- HANDLE handle = FindFirstFile(s.c_str(), &fileinfo);
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
-
- if (checklimit()) break;
-
- if (_T('.') != fileinfo.cFileName[0])
- {
- if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY)
- {
- m_pvtfolders->push_back(path + fileinfo.cFileName);
- findfilesnames_within_subfolder(path + fileinfo.cFileName);
- }
- else
- {
- if (!checklimit() )
- {
- m_pvtnames->push_back(fileinfo.cFileName);
- m_pvtfiles->push_back(path + fileinfo.cFileName);
- }
- }
- }
- } while (FindNextFile(handle, &fileinfo));
- FindClose(handle);
- }
- }
|