ffsco.cpp 5.3 KB

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