ffscoex.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 "ffscoex.h"
  17. #include <windows.h>
  18. #ifdef _UNICODE
  19. #include "CharacterConvert.h"
  20. #endif //#ifdef _UNICODE
  21. using helper_coffs::LPSFILEINFO;
  22. using helper_coffs::SFILEINFO;
  23. namespace helper_coffs
  24. {
  25. //////////////////////////////////////////////////////////////////////
  26. // Construction/Destruction
  27. //////////////////////////////////////////////////////////////////////
  28. ffscoex::ffscoex()
  29. : _limit(def_limit)
  30. , _dirs(0)
  31. , _strroot("")
  32. {
  33. }
  34. ffscoex::~ffscoex()
  35. {
  36. Clear();
  37. }
  38. //////////////////////////////////////////////////////////////////////
  39. string ffscoex::_uppercase_(IN string& s)
  40. {
  41. const char aazz = 'z' - 'Z';
  42. string rs;
  43. for (string::iterator it = s.begin(); s.end() != it; it++)
  44. {
  45. if ('a' <= *it && *it <= 'z')
  46. rs.append(1, *it - aazz);
  47. else
  48. rs.append(1, *it);
  49. }
  50. return rs;
  51. }
  52. //////////////////////////////////////////////////////////////////////
  53. /************************************************************************/
  54. /*
  55. 函数: match
  56. 描述: 文件匹配检测
  57. 参数:
  58. IN string fext_, 文件后叠
  59. IN string file_ 文件
  60. 返回:
  61. */
  62. /************************************************************************/
  63. int ffscoex::match(IN string fext_, IN string file_)
  64. {
  65. string fext = _uppercase_(fext_);
  66. string file = _uppercase_(file_);
  67. int pos = file.find_last_of('.');
  68. if (string::npos != pos)
  69. file = file.substr(pos);
  70. return (string::npos != fext.find(file));
  71. }
  72. /************************************************************************/
  73. /*
  74. 函数: limit
  75. 描述: 设置结果文件数目上限
  76. 参数:
  77. IN limit_ 文件数目上限
  78. 返回:
  79. */
  80. /************************************************************************/
  81. void ffscoex::limit(IN int limit_)
  82. {
  83. if (limit_ < 1)
  84. _limit = def_limit;
  85. if (limit_ < max_limit)
  86. _limit = limit_;
  87. }
  88. /************************************************************************/
  89. /*
  90. 函数: dir
  91. 描述: 判断s是否为目录
  92. 参数:
  93. IN string& s 目录
  94. 返回:
  95. */
  96. /************************************************************************/
  97. int ffscoex::dir(IN string& s)
  98. {
  99. #ifdef _UNICODE
  100. WCHAR wszFile[MAX_PATH] = {0};
  101. utf82unicode(s.c_str(), wszFile);
  102. return (FILE_ATTRIBUTE_DIRECTORY == GetFileAttributes(wszFile));
  103. #else
  104. return (FILE_ATTRIBUTE_DIRECTORY == GetFileAttributes(s.c_str()));
  105. #endif //#ifdef _UNICODE
  106. }
  107. /************************************************************************/
  108. /*
  109. 函数: find
  110. 描述: 查找文件
  111. 参数:
  112. IN string& path_, 目录路径
  113. IN string& fext_ 文件后叠
  114. 返回:
  115. */
  116. /************************************************************************/
  117. int ffscoex::find(IN string path_, IN string fext_)
  118. {
  119. recursivefind(path_, path_, fext_);
  120. //--dir
  121. for (typeT::iterator it_dir = _co_dir.begin(); _co_dir.end() != it_dir; it_dir++)
  122. _co.push_back(*it_dir);
  123. //--file
  124. for (typeT::iterator it_file = _co_file.begin(); _co_file.end() != it_file; it_file++)
  125. _co.push_back(*it_file);
  126. return count();
  127. }
  128. /************************************************************************/
  129. /*
  130. 函数: recursivefind
  131. 描述: 递归查找文件
  132. 参数:
  133. IN string root_, 相对要查找的目录作为根目录,注:不是磁盘的根目录
  134. IN string path_, 目录路径
  135. IN string fext_ 文件后叠
  136. 返回:
  137. */
  138. /************************************************************************/
  139. void ffscoex::recursivefind(IN const string& root_, IN string path_, IN string fext_)
  140. {
  141. string path = path_;
  142. if (path.size() > 0 && c_pathSeparator != path[path.size() - 1])
  143. path.append(s_pathSeparator);
  144. string fext = fext_;
  145. if (0 == fext.compare("*") || 0 == fext.compare("*.*"))
  146. fext = "";
  147. //string file = fext;
  148. string file = "*";
  149. string s = path + file;
  150. WIN32_FIND_DATA fileinfo;
  151. memset(&fileinfo, 0, sizeof(WIN32_FIND_DATA));
  152. #ifdef _UNICODE
  153. WCHAR wszFileName[MAX_PATH] = {0};
  154. ascii2unicode(s.c_str(), wszFileName);
  155. HANDLE handle = FindFirstFile( wszFileName, &fileinfo );
  156. #else
  157. HANDLE handle = FindFirstFile( s.c_str(), &fileinfo );
  158. #endif //#ifdef _UNICODE
  159. if (NULL != handle && INVALID_HANDLE_VALUE != handle)
  160. {
  161. //添加当前目录
  162. LPSFILEINFO lpCurrDir = new SFILEINFO;
  163. memcpy(lpCurrDir, &fileinfo, sizeof(WIN32_FIND_DATA));
  164. lpCurrDir->strRootDir = root_;
  165. lpCurrDir->strAllPath = path;
  166. _co_dir.push_back(lpCurrDir);
  167. do
  168. {
  169. if ('.' != fileinfo.cFileName[0])
  170. {
  171. //目录
  172. if(FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) //--目录
  173. {
  174. LPSFILEINFO lpSubDir = new SFILEINFO;
  175. memcpy(lpSubDir, &fileinfo, sizeof(WIN32_FIND_DATA));
  176. lpSubDir->strRootDir = root_;
  177. lpSubDir->strAllPath = path;
  178. #ifdef _UNICODE
  179. char szFileName[MAX_PATH] = {0};
  180. unicode2acsii(fileinfo.cFileName, szFileName);
  181. string strSubDir = path;
  182. strSubDir.append(szFileName);
  183. #else
  184. //子目录
  185. string strSubDir = lpSubDir->strAllPath + lpSubDir->cFileName;
  186. #endif //#ifdef _UNICODE
  187. //是否查找子目录
  188. if (1 == dirs())
  189. recursivefind(root_, strSubDir, fext);
  190. delete lpSubDir;
  191. }
  192. else
  193. {
  194. //文件
  195. LPSFILEINFO lpSFileInfo = new SFILEINFO;
  196. memcpy(lpSFileInfo, &fileinfo, sizeof(WIN32_FIND_DATA));
  197. lpSFileInfo->strRootDir = root_;
  198. #ifdef _UNICODE
  199. char szFileName[MAX_PATH] = {0};
  200. unicode2acsii(fileinfo.cFileName, szFileName);
  201. if (0 == fext.size() || match(fext, szFileName))
  202. {
  203. lpSFileInfo->strAllPath = path;
  204. lpSFileInfo->strAllPath.append(szFileName);
  205. _co_file.push_back(lpSFileInfo);
  206. }
  207. else
  208. {
  209. if(lpSFileInfo)
  210. delete lpSFileInfo;
  211. }
  212. #else
  213. if (0 == fext.size() || match(fext, fileinfo.cFileName))
  214. {
  215. lpSFileInfo->strAllPath = path + fileinfo.cFileName;
  216. _co_file.push_back(lpSFileInfo);
  217. }
  218. else
  219. {
  220. if(lpSFileInfo)
  221. delete lpSFileInfo;
  222. }
  223. #endif //#ifdef _UNICODE
  224. }
  225. }
  226. } while (FindNextFile( handle, &fileinfo ));
  227. FindClose(handle);
  228. }
  229. }
  230. /************************************************************************/
  231. /*
  232. 函数: Clear
  233. 描述: 清除
  234. 参数:
  235. 返回:
  236. */
  237. /************************************************************************/
  238. void ffscoex::Clear()
  239. {
  240. typeT::iterator it = _co_file.begin();
  241. for(;it!=_co_file.end();)
  242. {
  243. LPSFILEINFO lp = (*it);
  244. lp = NULL;
  245. ++it;
  246. }
  247. _co_file.clear();
  248. it = _co_dir.begin();
  249. for(;it!=_co_dir.end();)
  250. {
  251. LPSFILEINFO lp = (*it);
  252. lp = NULL;
  253. ++it;
  254. }
  255. _co_dir.clear();
  256. it = _co.begin();
  257. for(;it!=_co.end();)
  258. {
  259. LPSFILEINFO lp = (*it);
  260. if(lp != NULL)
  261. delete lp;
  262. lp = NULL;
  263. ++it;
  264. }
  265. _co.clear();
  266. }
  267. /************************************************************************/
  268. /*
  269. 函数: deldir
  270. 描述: 删除目录
  271. 参数:
  272. IN LPCTSTR pDir 目录
  273. 返回:
  274. */
  275. /************************************************************************/
  276. int ffscoex::deldir(IN LPCTSTR lpDir)
  277. {
  278. #ifdef _UNICODE
  279. if(lpDir == NULL || wcscmp(lpDir, _T("")) == 0)
  280. return 0;
  281. #else
  282. if(lpDir == NULL || strcmp(lpDir, "") == 0)
  283. return 0;
  284. #endif
  285. if( !PathFileExists(lpDir))
  286. return 0;
  287. TCHAR szPath[MAX_PATH] = {0};
  288. #ifdef _UNICODE
  289. wcscpy(szPath, lpDir);
  290. #else
  291. strcpy(szPath, lpDir);
  292. #endif
  293. lstrcat(szPath, _T("\\*"));
  294. WIN32_FIND_DATA fileinfo = { 0 };
  295. HANDLE handle = FindFirstFile(szPath, &fileinfo);
  296. if (NULL == handle && INVALID_HANDLE_VALUE == handle)
  297. return 0;
  298. do
  299. {
  300. // '.'和 '..'的系统文件去除;
  301. if (_T('.') != fileinfo.cFileName[0])
  302. {
  303. TCHAR szCurrPath[MAX_PATH] = {0};
  304. #ifdef _UNICODE
  305. wcscpy(szCurrPath, lpDir);
  306. #else
  307. strcpy(szCurrPath, lpDir);
  308. #endif
  309. lstrcat(szCurrPath, _T("\\"));
  310. lstrcat(szCurrPath, fileinfo.cFileName);
  311. SetFileAttributes(szCurrPath, FILE_ATTRIBUTE_NORMAL);//去掉所有属性;
  312. if ((FILE_ATTRIBUTE_DIRECTORY & fileinfo.dwFileAttributes) == FILE_ATTRIBUTE_DIRECTORY) // 目录;
  313. {
  314. deldir(szCurrPath);
  315. }
  316. else
  317. {
  318. if(!DeleteFile(szCurrPath))
  319. {
  320. DWORD dwError = GetLastError();
  321. //LOG4C_NO_FILENUM((LOG_NOTICE,"删除文件出错:%d",dwError));
  322. }
  323. }
  324. }
  325. } while (FindNextFile(handle, &fileinfo));
  326. FindClose(handle);
  327. //删除目录
  328. if(!RemoveDirectory(lpDir))
  329. {
  330. DWORD dwError = GetLastError();
  331. //LOG4C_NO_FILENUM((LOG_NOTICE,"删除文件夹出错:%d",dwError));
  332. //OutputDebugString(_T("**************删除目录失败**************\n"));
  333. return 0;
  334. }
  335. return 1;
  336. }
  337. }; //--namespace helper_coffs