123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- #include "stdafx.h" //--i want remove it
- #include "ffsco.h"
- #include <windows.h>
- namespace helper_coffs
- {
-
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
-
- ffsco::ffsco()
- : _limit(def_limit)
- , _dirs(0)
- {
-
- }
-
- ffsco::~ffsco()
- {
- Clear();
- }
-
- //////////////////////////////////////////////////////////////////////
- string ffsco::_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 ffsco::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 ffsco::limit(IN int limit_)
- {
- if (limit_ < 1)
- _limit = def_limit;
- if (limit_ < max_limit)
- _limit = limit_;
- }
-
- /************************************************************************/
- /*
- 函数: dir
- 描述: 判断s是否为目录
- 参数:
- IN string& s 目录
- 返回:
- */
- /************************************************************************/
- int ffsco::dir(IN string& s)
- {
- return (FILE_ATTRIBUTE_DIRECTORY == GetFileAttributes(s.c_str()));
- }
- /*
- int ffsco::find(string path_, string fext_)
- {
- _co_file.clear();
- _co_dir.clear();
- _co.clear();
-
- string path = path_;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
-
- _co_dir.push_back( path );
-
- string fext = fext_;
- if (0 == fext.compare("*") || 0 == fext.compare("*.*"))
- fext = "";
- //string file = fext;
- string file = "*";
- string s = path + file;
- WIN32_FIND_DATA fileinfo = {0};
- HANDLE handle = FindFirstFile( s.c_str(), &fileinfo );
-
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
- if ('.' != fileinfo.cFileName[0])
- {
- //目录
- if (FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) //--目录
- {
- _co_dir.push_back( path + fileinfo.cFileName + s_pathSeparator );
-
- if (0 != dirs())
- {
- ffsco o;
- o.dirs(dirs());
- o.find( path + fileinfo.cFileName, fext);
- //--dir
- typeT o_dir = o.co_dir();
- for (typeT::iterator it_dir = o_dir.begin(); o_dir.end() != it_dir; it_dir ++)
- _co_dir.push_back(*it_dir);
- //--file
- typeT o_file = o.co_file();
- for (typeT::iterator it_file = o_file.begin(); o_file.end() != it_file; it_file ++)
- _co_file.push_back(*it_file);
- }
- }
- else
- {
- //文件
- if (0 == fext.size() || match(fext, fileinfo.cFileName))
- _co_file.push_back( path + fileinfo.cFileName );
- }
- }
-
- } while (FindNextFile( handle, &fileinfo ));
-
- FindClose(handle);
- }
-
- //--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();
- }
- */
- /************************************************************************/
- /*
- 函数: find
- 描述: 查找文件
- 参数:
- IN string& path_, 目录路径
- IN string& fext_ 文件后叠
- 返回:
- */
- /************************************************************************/
- int ffsco::find(IN string path_, IN string fext_)
- {
- findex(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();
- }
- /************************************************************************/
- /*
- 函数: findex
- 描述: 查找文件
- 参数:
- IN string& path_, 目录路径
- IN string& fext_ 文件后叠
- 返回:
- */
- /************************************************************************/
- void ffsco::findex(IN string path_, IN string fext_)
- {
- string path = path_;
- if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
- path.append(s_pathSeparator);
-
- _co_dir.push_back( path );
-
- string fext = fext_;
- if (0 == fext.compare("*") || 0 == fext.compare("*.*"))
- fext = "";
-
- //string file = fext;
- string file = "*";
- string s = path + file;
-
- WIN32_FIND_DATA fileinfo = {0};
- HANDLE handle = FindFirstFile( s.c_str(), &fileinfo );
-
- if (NULL != handle && INVALID_HANDLE_VALUE != handle)
- {
- do
- {
- if ('.' != fileinfo.cFileName[0])
- {
- //目录
- if (FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) //--目录
- {
- //查找子目录
- if (1 == dirs())
- findex(path + fileinfo.cFileName, fext);
- }
- else
- {
- //文件
- if (0 == fext.size() || match(fext, fileinfo.cFileName))
- _co_file.push_back( path + fileinfo.cFileName );
- }
- }
-
- } while (FindNextFile( handle, &fileinfo ));
-
- FindClose(handle);
- }
- }
- /************************************************************************/
- /*
- 函数: Clear
- 描述: 清除
- 参数:
- 返回:
- */
- /************************************************************************/
- void ffsco::Clear()
- {
- _co_file.clear();
- _co_dir.clear();
- _co.clear();
- }
- //--example
- void ffsco::test()
- {
- char win32sys_path[MAX_PATH] = {0};
- ::GetSystemDirectory(win32sys_path, MAX_PATH);
-
- string path = win32sys_path;
- string fext = "*.dll; *.exe; *.ini; *.sys";
- //string fext;
-
- ffsco o;
- //o.dirs(1); //--查找子目录
- o.limit(100); //--最多查找100个
- int count = o.find(path, fext);
- int count_dir = o.co_dir().size();
- int count_file = o.co_file().size();
-
- //--first
- ::MessageBox(NULL, o.get().data(), "ffsco.test.path", MB_OK);
-
- ffsco::typeT coo;
- ffsco::typeT::iterator it;
- string s;
- stringstream ss;
-
- //--all
- coo = o.co();
- s.erase();
- ss.str("");
- ss << coo.size();
- s.append("list = " + ss.str() + "\r\n");
- for (it = coo.begin(); coo.end() != it; it ++)
- {
- s.append(*it);
- s.append("\r\n");
- }
- ::MessageBox(NULL, s.c_str(), "ffsco.test.all", MB_OK);
-
- //--dir
- coo = o.co_dir();
- s.erase();
- ss.str("");
- ss << coo.size();
- s.append("list = " + ss.str() + "\r\n");
- for (it = coo.begin(); coo.end() != it; it ++)
- {
- s.append(*it);
- s.append("\r\n");
- }
- ::MessageBox(NULL, s.c_str(), "ffsco.test.dir", MB_OK);
-
- //--file
- coo = o.co_file();
- s.erase();
- ss.str("");
- ss << coo.size();
- s.append("list = " + ss.str() + "\r\n");
- for (it = coo.begin(); coo.end() != it; it ++)
- {
- s.append(*it);
- s.append("\r\n");
- }
- ::MessageBox(NULL, s.c_str(), "ffsco.test.file", MB_OK);
- }
-
- }; //--namespace helper_coffs
|