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