#include "StdAfx.h" #include "findfile.h" #include <algorithm> // �����ļ����������������nMaxLimit; CONST size_t nMaxLimit = 0xFFFFFFFF; //--4294967295; // �����ļ���Ĭ������������nDefLimit; CONST size_t nDefLimit = 0xFFFFF; //--1048575 // ·���ָ���; CONST TCHAR c_pathSeparator = _T('\\'); CONST TString s_pathSeparator = _T("\\"); CONST TString g_sVertical = _T("|"); // ��ȷ����չ������ʽ����; // ���ֵ��ÿ��Դ������ļ�ini��xml�л�ȡ,�����ڴ�����д��; 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 ) {// С��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 ) {// С��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('\\')); // ���file������ '\\' ,�ò����ļ���; 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) { } /************************************************************************/ /* ������groupExt[2/19/2017 Jeff]; /* ���������ַ���������"|"���ŷָ�������; /* ������; /* [GBL] g_sCorrectExt��ȫ���ַ���; /* ���أ�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); } /************************************************************************/ /* ������[2/19/2017 Jeff]; /* ������; /* ������; /* [IN] ��; /* [OUT] ��; /* [IN/OUT] ��; /* ���أ�void; /* ע�⣺; /* ʾ����; /* /* �ģ�; /* ���ڣ�; /* ���ݣ�; /************************************************************************/ 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); } /************************************************************************/ /* ������iscorrectext[2/18/2017 Jeff]; /* �������ж�ָ���ĺ����Ƿ���Ч; /* ������; /* [IN] fext���������չ��,fext�ĸ�ʽ������:_T("*.jpg|*.jpeg|*.png|*.bmp"); /* [IN] lpMistakenExt��������Ҫ�����չ��; /* [IN/OUT] ��; /* ���أ����ָ������չ������Ҫ��,��TRUE,����FALSE; /* ע�⣺ /* 1.����������չ������_T("*.*"),��TRUE;��lpMistakenExtָ����Ч,��¼��һ�η��ز�����Ҫ�����չ��; /* 2.fext�ĸ�ʽ������:_T("*.jpg|*.jpeg|*.png|*.bmp"); /* ʾ����; /* /* �ģ�; /* ���ڣ�; /* ���ݣ�; /************************************************************************/ BOOL findfile::iscorrectext(IN const TString &fext, OUT TString* lpMistakenExt /*= NULL*/) { 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; } /************************************************************************/ /* ������lowercase[2/18/2017 Jeff]; /* ��������ָ���ַ���Сд��; /* ������; /* [IN] Str��ҪתΪСд���ַ���; /* [OUT] ��; /* [IN/OUT] ��; /* ���أ�����תСд����ַ���; /* ע�⣺; /* ʾ����; /* /* �ģ�; /* ���ڣ�; /* ���ݣ�; /************************************************************************/ TString findfile::lowercase(IN const TString &Str) { TString sResult; for ( TString::const_iterator it = Str.begin(); it != Str.end(); it++ ) { if ( *it < 0 ) {// С��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 ) {// С��0,������,ʲô������; it++; } else { if (_T('A') <= *it && *it <= _T('Z')) *it += 32; } } } /************************************************************************/ /* ������uppercase[2/18/2017 Jeff]; /* ��������ָ���ַ�����д��; /* ������; /* [IN] Str��ҪתΪ��д���ַ���; /* [OUT] ��; /* [IN/OUT] ��; /* ���أ�����ת��д����ַ���; /* ע�⣺; /* ʾ����; /* /* �ģ�; /* ���ڣ�; /* ���ݣ�; /************************************************************************/ TString findfile::uppercase(IN const TString &Str) { TString sResult; for ( TString::const_iterator it = Str.begin(); it != Str.end(); it++ ) { if ( *it < 0 ) {// С��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 ) {// С��0,������,ʲô������; it++; } else { if (_T('a') <= *it && *it <= _T('z')) *it -= 32; } } } /************************************************************************/ /* ����:match ����:�ж�ָ�����ļ������Ƿ�ƥ��ָ������չ��; ����: IN sFileExt ��չ��; IN sFile �ļ���; ����:ƥ�䷵�ط�0ֵ,����0; ע��: */ /************************************************************************/ 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('\\')); // ���file������ '\\' ,�ò����ļ���; 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); } }