ffsco.cpp 5.2 KB

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