ffsco.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #include "stdafx.h" //--i want remove it
  2. #include "ffsco.h"
  3. #include <windows.h>
  4. namespace helper_coffs
  5. {
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. ffsco::ffsco()
  10. : _limit(def_limit)
  11. , _dirs(0)
  12. {
  13. }
  14. ffsco::~ffsco()
  15. {
  16. Clear();
  17. }
  18. //////////////////////////////////////////////////////////////////////
  19. string ffsco::_uppercase_(IN string& s)
  20. {
  21. const char aazz = 'z' - 'Z';
  22. string rs;
  23. for (string::iterator it = s.begin(); s.end() != it; it++)
  24. {
  25. if ('a' <= *it && *it <= 'z')
  26. rs.append(1, *it - aazz);
  27. else
  28. rs.append(1, *it);
  29. }
  30. return rs;
  31. }
  32. //////////////////////////////////////////////////////////////////////
  33. /************************************************************************/
  34. /*
  35. 函数: match
  36. 描述: 文件匹配检测
  37. 参数:
  38. IN string fext_, 文件后叠
  39. IN string file_ 文件
  40. 返回:
  41. */
  42. /************************************************************************/
  43. int ffsco::match(IN string fext_, IN string file_)
  44. {
  45. string fext = _uppercase_(fext_);
  46. string file = _uppercase_(file_);
  47. int pos = file.find_last_of('.');
  48. if (string::npos != pos)
  49. file = file.substr(pos);
  50. return (string::npos != fext.find(file));
  51. }
  52. /************************************************************************/
  53. /*
  54. 函数: limit
  55. 描述: 设置结果文件数目上限
  56. 参数:
  57. IN limit_ 文件数目上限
  58. 返回:
  59. */
  60. /************************************************************************/
  61. void ffsco::limit(IN int limit_)
  62. {
  63. if (limit_ < 1)
  64. _limit = def_limit;
  65. if (limit_ < max_limit)
  66. _limit = limit_;
  67. }
  68. /************************************************************************/
  69. /*
  70. 函数: dir
  71. 描述: 判断s是否为目录
  72. 参数:
  73. IN string& s 目录
  74. 返回:
  75. */
  76. /************************************************************************/
  77. int ffsco::dir(IN string& s)
  78. {
  79. return (FILE_ATTRIBUTE_DIRECTORY == GetFileAttributes(s.c_str()));
  80. }
  81. /*
  82. int ffsco::find(string path_, string fext_)
  83. {
  84. _co_file.clear();
  85. _co_dir.clear();
  86. _co.clear();
  87. string path = path_;
  88. if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
  89. path.append(s_pathSeparator);
  90. _co_dir.push_back( path );
  91. string fext = fext_;
  92. if (0 == fext.compare("*") || 0 == fext.compare("*.*"))
  93. fext = "";
  94. //string file = fext;
  95. string file = "*";
  96. string s = path + file;
  97. WIN32_FIND_DATA fileinfo = {0};
  98. HANDLE handle = FindFirstFile( s.c_str(), &fileinfo );
  99. if (NULL != handle && INVALID_HANDLE_VALUE != handle)
  100. {
  101. do
  102. {
  103. if ('.' != fileinfo.cFileName[0])
  104. {
  105. //目录
  106. if (FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) //--目录
  107. {
  108. _co_dir.push_back( path + fileinfo.cFileName + s_pathSeparator );
  109. if (0 != dirs())
  110. {
  111. ffsco o;
  112. o.dirs(dirs());
  113. o.find( path + fileinfo.cFileName, fext);
  114. //--dir
  115. typeT o_dir = o.co_dir();
  116. for (typeT::iterator it_dir = o_dir.begin(); o_dir.end() != it_dir; it_dir ++)
  117. _co_dir.push_back(*it_dir);
  118. //--file
  119. typeT o_file = o.co_file();
  120. for (typeT::iterator it_file = o_file.begin(); o_file.end() != it_file; it_file ++)
  121. _co_file.push_back(*it_file);
  122. }
  123. }
  124. else
  125. {
  126. //文件
  127. if (0 == fext.size() || match(fext, fileinfo.cFileName))
  128. _co_file.push_back( path + fileinfo.cFileName );
  129. }
  130. }
  131. } while (FindNextFile( handle, &fileinfo ));
  132. FindClose(handle);
  133. }
  134. //--dir
  135. for (typeT::iterator it_dir = _co_dir.begin(); _co_dir.end() != it_dir; it_dir ++)
  136. _co.push_back(*it_dir);
  137. //--file
  138. for (typeT::iterator it_file = _co_file.begin(); _co_file.end() != it_file; it_file ++)
  139. _co.push_back(*it_file);
  140. return count();
  141. }
  142. */
  143. /************************************************************************/
  144. /*
  145. 函数: find
  146. 描述: 查找文件
  147. 参数:
  148. IN string& path_, 目录路径
  149. IN string& fext_ 文件后叠
  150. 返回:
  151. */
  152. /************************************************************************/
  153. int ffsco::find(IN string path_, IN string fext_)
  154. {
  155. findex(path_, fext_);
  156. //--dir
  157. for (typeT::iterator it_dir = _co_dir.begin(); _co_dir.end() != it_dir; it_dir++)
  158. _co.push_back(*it_dir);
  159. //--file
  160. for (typeT::iterator it_file = _co_file.begin(); _co_file.end() != it_file; it_file++)
  161. _co.push_back(*it_file);
  162. return count();
  163. }
  164. /************************************************************************/
  165. /*
  166. 函数: findex
  167. 描述: 查找文件
  168. 参数:
  169. IN string& path_, 目录路径
  170. IN string& fext_ 文件后叠
  171. 返回:
  172. */
  173. /************************************************************************/
  174. void ffsco::findex(IN string path_, IN string fext_)
  175. {
  176. string path = path_;
  177. if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
  178. path.append(s_pathSeparator);
  179. _co_dir.push_back( path );
  180. string fext = fext_;
  181. if (0 == fext.compare("*") || 0 == fext.compare("*.*"))
  182. fext = "";
  183. //string file = fext;
  184. string file = "*";
  185. string s = path + file;
  186. WIN32_FIND_DATA fileinfo = {0};
  187. HANDLE handle = FindFirstFile( s.c_str(), &fileinfo );
  188. if (NULL != handle && INVALID_HANDLE_VALUE != handle)
  189. {
  190. do
  191. {
  192. if ('.' != fileinfo.cFileName[0])
  193. {
  194. //目录
  195. if (FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) //--目录
  196. {
  197. //查找子目录
  198. if (1 == dirs())
  199. findex(path + fileinfo.cFileName, fext);
  200. }
  201. else
  202. {
  203. //文件
  204. if (0 == fext.size() || match(fext, fileinfo.cFileName))
  205. _co_file.push_back( path + fileinfo.cFileName );
  206. }
  207. }
  208. } while (FindNextFile( handle, &fileinfo ));
  209. FindClose(handle);
  210. }
  211. }
  212. /************************************************************************/
  213. /*
  214. 函数: Clear
  215. 描述: 清除
  216. 参数:
  217. 返回:
  218. */
  219. /************************************************************************/
  220. void ffsco::Clear()
  221. {
  222. _co_file.clear();
  223. _co_dir.clear();
  224. _co.clear();
  225. }
  226. //--example
  227. void ffsco::test()
  228. {
  229. char win32sys_path[MAX_PATH] = {0};
  230. ::GetSystemDirectory(win32sys_path, MAX_PATH);
  231. string path = win32sys_path;
  232. string fext = "*.dll; *.exe; *.ini; *.sys";
  233. //string fext;
  234. ffsco o;
  235. //o.dirs(1); //--查找子目录
  236. o.limit(100); //--最多查找100个
  237. int count = o.find(path, fext);
  238. int count_dir = o.co_dir().size();
  239. int count_file = o.co_file().size();
  240. //--first
  241. ::MessageBox(NULL, o.get().data(), "ffsco.test.path", MB_OK);
  242. ffsco::typeT coo;
  243. ffsco::typeT::iterator it;
  244. string s;
  245. stringstream ss;
  246. //--all
  247. coo = o.co();
  248. s.erase();
  249. ss.str("");
  250. ss << coo.size();
  251. s.append("list = " + ss.str() + "\r\n");
  252. for (it = coo.begin(); coo.end() != it; it ++)
  253. {
  254. s.append(*it);
  255. s.append("\r\n");
  256. }
  257. ::MessageBox(NULL, s.c_str(), "ffsco.test.all", MB_OK);
  258. //--dir
  259. coo = o.co_dir();
  260. s.erase();
  261. ss.str("");
  262. ss << coo.size();
  263. s.append("list = " + ss.str() + "\r\n");
  264. for (it = coo.begin(); coo.end() != it; it ++)
  265. {
  266. s.append(*it);
  267. s.append("\r\n");
  268. }
  269. ::MessageBox(NULL, s.c_str(), "ffsco.test.dir", MB_OK);
  270. //--file
  271. coo = o.co_file();
  272. s.erase();
  273. ss.str("");
  274. ss << coo.size();
  275. s.append("list = " + ss.str() + "\r\n");
  276. for (it = coo.begin(); coo.end() != it; it ++)
  277. {
  278. s.append(*it);
  279. s.append("\r\n");
  280. }
  281. ::MessageBox(NULL, s.c_str(), "ffsco.test.file", MB_OK);
  282. }
  283. }; //--namespace helper_coffs