ffsco.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // Construction/Destruction
  21. //////////////////////////////////////////////////////////////////////
  22. ffsco::ffsco()
  23. : _limit(def_limit), _dirs(0)
  24. {
  25. }
  26. ffsco::~ffsco()
  27. {
  28. }
  29. //////////////////////////////////////////////////////////////////////
  30. string ffsco::_uppercase_(string s)
  31. {
  32. const char aazz = 'z' - 'Z';
  33. string rs;
  34. for (string::iterator it = s.begin();
  35. s.end() != it; it++)
  36. {
  37. if ('a' <= *it && *it <= 'z') rs.append(1, *it - aazz);
  38. else rs.append(1, *it);
  39. }
  40. return rs;
  41. }
  42. //////////////////////////////////////////////////////////////////////
  43. int ffsco::match(string fext_, 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) file = file.substr(pos);
  49. return (string::npos != fext.find(file));
  50. }
  51. void ffsco::limit(int limit_)
  52. {
  53. if (limit_ < 1) _limit = def_limit;
  54. if (limit_ < max_limit) _limit = limit_;
  55. }
  56. int ffsco::dir(string s)
  57. {
  58. return (FILE_ATTRIBUTE_DIRECTORY == GetFileAttributes(s.c_str()));
  59. }
  60. int ffsco::find(string path_, string fext_)
  61. {
  62. _co_file.clear();
  63. _co_dir.clear();
  64. _co.clear();
  65. string path = path_;
  66. if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
  67. path.append(s_pathSeparator);
  68. _co_dir.push_back( path );
  69. string fext = fext_;
  70. if (0 == fext.compare("*")
  71. || 0 == fext.compare("*.*")
  72. ) fext = "";
  73. //string file = fext;
  74. string file = "*";
  75. string s = path + file;
  76. WIN32_FIND_DATA fileinfo = {0};
  77. HANDLE handle = FindFirstFile( s.c_str(), &fileinfo );
  78. if (NULL != handle && INVALID_HANDLE_VALUE != handle)
  79. {
  80. do {
  81. if (_limit_()) break; //--limit test
  82. if ('.' != fileinfo.cFileName[0]) //--skip./..
  83. if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes)
  84. == FILE_ATTRIBUTE_DIRECTORY) //--目录
  85. {
  86. if (!_limit_() //--limit test
  87. //&& 0 == fext.size()
  88. ) _co_dir.push_back( path + fileinfo.cFileName + s_pathSeparator );
  89. if (0 != dirs())
  90. {
  91. ffsco o;
  92. o.dirs(dirs());
  93. o.find( path + fileinfo.cFileName, fext);
  94. //--dir
  95. typeT o_dir = o.co_dir();
  96. for (typeT::iterator it_dir = o_dir.begin();
  97. o_dir.end() != it_dir && !_limit_(); //--limit test
  98. it_dir ++) _co_dir.push_back(*it_dir);
  99. //--file
  100. typeT o_file = o.co_file();
  101. for (typeT::iterator it_file = o_file.begin();
  102. o_file.end() != it_file && !_limit_(); //--limit test
  103. it_file ++) _co_file.push_back(*it_file);
  104. }
  105. }
  106. else
  107. {
  108. if (!_limit_() //--limit test
  109. && (0 == fext.size() || match(fext, fileinfo.cFileName))
  110. ) _co_file.push_back( path + fileinfo.cFileName );
  111. }
  112. } while (FindNextFile( handle, &fileinfo ));
  113. FindClose(handle);
  114. }
  115. //--dir
  116. for (typeT::iterator it_dir = _co_dir.begin();
  117. _co_dir.end() != it_dir;
  118. it_dir ++) _co.push_back(*it_dir);
  119. //--file
  120. for (typeT::iterator it_file = _co_file.begin();
  121. _co_file.end() != it_file;
  122. it_file ++) _co.push_back(*it_file);
  123. return count();
  124. }
  125. //--example
  126. void ffsco::test()
  127. {
  128. char win32sys_path[MAX_PATH] = {0};
  129. ::GetSystemDirectory(win32sys_path, MAX_PATH);
  130. string path = win32sys_path;
  131. string fext = "*.dll; *.exe; *.ini; *.sys";
  132. //string fext;
  133. ffsco o;
  134. //o.dirs(1); //--查找子目录
  135. o.limit(100); //--最多查找100个
  136. int count = o.find(path, fext);
  137. //int count = o.find(path);
  138. int count_dir = o.co_dir().size();
  139. int count_file = o.co_file().size();
  140. //--first
  141. ::MessageBox(NULL, o.get().data(), "ffsco.test.path", MB_OK);
  142. ffsco::typeT coo;
  143. ffsco::typeT::iterator it;
  144. string s;
  145. stringstream ss;
  146. //--all
  147. coo = o.co();
  148. s.erase();
  149. ss.str("");
  150. ss << coo.size();
  151. s.append("list = " + ss.str() + "\r\n");
  152. for (it = coo.begin(); coo.end() != it; it ++)
  153. {
  154. s.append(*it);
  155. s.append("\r\n");
  156. }
  157. ::MessageBox(NULL, s.c_str(), "ffsco.test.all", MB_OK);
  158. //--dir
  159. coo = o.co_dir();
  160. s.erase();
  161. ss.str("");
  162. ss << coo.size();
  163. s.append("list = " + ss.str() + "\r\n");
  164. for (it = coo.begin(); coo.end() != it; it ++)
  165. {
  166. s.append(*it);
  167. s.append("\r\n");
  168. }
  169. ::MessageBox(NULL, s.c_str(), "ffsco.test.dir", MB_OK);
  170. //--file
  171. coo = o.co_file();
  172. s.erase();
  173. ss.str("");
  174. ss << coo.size();
  175. s.append("list = " + ss.str() + "\r\n");
  176. for (it = coo.begin(); coo.end() != it; it ++)
  177. {
  178. s.append(*it);
  179. s.append("\r\n");
  180. }
  181. ::MessageBox(NULL, s.c_str(), "ffsco.test.file", MB_OK);
  182. }
  183. }; //--namespace helper_coffs