ffsco.cpp 7.9 KB

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