Finder.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include "Finder.h"
  2. #include "FileService.h"
  3. Finder::Finder(const char * fullPath, const char * findMask, dword flags, const char * _cppFileName, long _cppFileLine) : files(_FL_)
  4. {
  5. path = fullPath;
  6. if(path.Len() > 0 && path[path.Len() - 1] != '\\')
  7. {
  8. path += '\\';
  9. }
  10. mask = findMask;
  11. if(mask == "*")
  12. {
  13. mask = "*.*";
  14. }
  15. isRecursive = ((flags & find_no_recursive) == 0);
  16. isDots = ((flags & find_dots) != 0);
  17. isAddFolders = ((flags & find_folders) != 0);
  18. isFindInLoadedPacks = ((flags & find_no_files_from_packs) == 0);
  19. isFindInMirrors = ((flags & find_no_mirror_files) == 0);
  20. isInverseOrder = ((flags & find_inverse_order) != 0);
  21. #ifndef STOP_DEBUG
  22. cppFileName = _cppFileName;
  23. cppFileLine = _cppFileLine;
  24. #endif
  25. Reset();
  26. }
  27. Finder::~Finder()
  28. {
  29. }
  30. //Удалить сообщив об ошибке
  31. void Finder::ErrorRelease()
  32. {
  33. #ifndef STOP_DEBUG
  34. FileService::object->Error("FileService error: IFinder not release (file: %s, line %i)", cppFileName, cppFileLine);
  35. #endif
  36. delete this;
  37. }
  38. //Удалить объект
  39. void Finder::Release()
  40. {
  41. {
  42. SingleExClassThread(FileService::object)
  43. FileService::object->DeleteFinder(this);
  44. }
  45. delete this;
  46. }
  47. //Начать поиск заново, возвращает количество найденых файлов
  48. dword Finder::Reset()
  49. {
  50. SingleExClassThread(FileService::object)
  51. //Перебираем все файлы по реальному пути
  52. FindOnDisk(path, null);
  53. //Ищем в паках
  54. if(isFindInLoadedPacks)
  55. {
  56. FindInPacks();
  57. }
  58. //Просматриваем зеркальные пути
  59. if(isFindInMirrors)
  60. {
  61. dword count = FileService::object->filesTree.FindPaths(path);
  62. for(dword i = 0; i < count; i++)
  63. {
  64. //Зеркальный путь
  65. const char * mirrorPath = FileService::object->filesTree.GetPath(i);
  66. //Смотрим на диске
  67. FindOnDisk(mirrorPath, mirrorPath);
  68. }
  69. }
  70. for(dword i = 0; i < files.Size(); i++)
  71. {
  72. FindFile & fl = files[i];
  73. fl.fileNameIndex = string::GetFileName(fl.path.c_str()) - fl.path.c_str();
  74. fl.fileNameHash = string::HashNoCase(&fl.path[fl.fileNameIndex]);
  75. }
  76. //Замещяем одинаковые файлы файлами с большим приоритетом
  77. for(dword i = 0; i < files.Size(); i++)
  78. {
  79. if(!(files[i].flags & (f_mirror | f_pack))) continue;
  80. for(dword j = 0; j < files.Size(); j++)
  81. {
  82. //Пропускаем значения с разным хэшем
  83. FindFile & cur = files[i];
  84. FindFile & ff = files[j];
  85. if(ff.fileNameHash != cur.fileNameHash || (ff.flags & f_folder) != 0 || i == j) continue;
  86. if(!string::IsEqual(&ff.path[ff.fileNameIndex], &cur.path[cur.fileNameIndex])) continue;
  87. //Имена совпадают, решаем что делать
  88. if(cur.flags > ff.flags)
  89. {
  90. files.DelIndex(j);
  91. Assert(j != i);
  92. if(j < i) i--;
  93. j--;
  94. }
  95. }
  96. }
  97. /*
  98. api->Trace("-------------------------------------------------");
  99. api->Trace("Finder path: %s", path.c_str());
  100. api->Trace("Finder mask: %s", mask.c_str());
  101. for(long i = 0; i < files; i++)
  102. {
  103. api->Trace("%s", files[i].path.c_str());
  104. }
  105. api->Trace("-------------------------------------------------");
  106. */
  107. return files.Size();
  108. }
  109. //Получить количество найденых файлов
  110. dword Finder::Count() const
  111. {
  112. return files.Size();
  113. }
  114. //Получить полный путь с именем файла "С:\path\name.ext"
  115. const char * Finder::FilePath(dword index) const
  116. {
  117. if(index < files.Size())
  118. {
  119. return files[index].path.c_str();
  120. }
  121. return null;
  122. }
  123. //Получить полный путь до файла "С:\path\"
  124. const char * Finder::Path(dword index) const
  125. {
  126. if(index < files.Size())
  127. {
  128. if((files[index].flags & f_folder) == 0)
  129. {
  130. return buffer.GetFilePath(files[index].path);
  131. }else{
  132. return files[index].path.c_str();
  133. }
  134. }
  135. return null;
  136. }
  137. //Получить имя файла "name.ext"
  138. const char * Finder::Name(dword index) const
  139. {
  140. if(index < files.Size())
  141. {
  142. return buffer.GetFileName(files[index].path);
  143. }
  144. return null;
  145. }
  146. //Получить имя файла без расширения name
  147. const char * Finder::Title(dword index) const
  148. {
  149. if(index < files.Size())
  150. {
  151. return buffer.GetFileTitle(files[index].path);
  152. }
  153. return null;
  154. }
  155. //Получить расширение файла "ext"
  156. const char * Finder::Extension(dword index) const
  157. {
  158. if(index < files.Size())
  159. {
  160. return buffer.GetFileExt(files[index].path);
  161. }
  162. return null;
  163. }
  164. //true если найдены "." или ".."
  165. bool Finder::IsDot(dword index) const
  166. {
  167. if(index < files.Size())
  168. {
  169. buffer.GetFileName(files[index].path);
  170. return IsDots(buffer.c_str());
  171. }
  172. return false;
  173. }
  174. //true если найдена папка
  175. bool Finder::IsFolder(dword index) const
  176. {
  177. if(index < files.Size())
  178. {
  179. return (files[index].flags & f_folder) != 0;
  180. }
  181. return false;
  182. }
  183. //true если этот файл найден в зеркальной директории
  184. bool Finder::IsMirror(dword index) const
  185. {
  186. if(index < files.Size())
  187. {
  188. return (files[index].flags & f_mirror) != 0;
  189. }
  190. return false;
  191. }
  192. //Поиск файлов на диске
  193. void Finder::FindOnDisk(const char * findPath, const char * mirrorPath)
  194. {
  195. //Перебираем папки для рекурсивного поиска
  196. if(isInverseOrder && isRecursive)
  197. {
  198. FindOnDiskRecursive(findPath, mirrorPath);
  199. }
  200. WIN32_FIND_DATA wfd;
  201. //Ищем в текущей папке файлы
  202. buffer = findPath;
  203. if(buffer.Len() > 0)
  204. {
  205. Assert(buffer[buffer.Len() - 1] == '\\');
  206. }
  207. buffer += mask;
  208. HANDLE handle = ::FindFirstFile(buffer, &wfd);
  209. if(handle != INVALID_HANDLE_VALUE)
  210. {
  211. do
  212. {
  213. bool isFolder = ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
  214. if(isFolder)
  215. {
  216. if(!isAddFolders)
  217. {
  218. continue;
  219. }
  220. if(!isDots)
  221. {
  222. if(IsDots(wfd.cFileName))
  223. {
  224. continue;
  225. }
  226. }
  227. }
  228. FindFile & file = files[files.Add()];
  229. if(mirrorPath)
  230. {
  231. file.path = mirrorPath;
  232. buffer = findPath;
  233. buffer.GetRelativePath(file.path);
  234. file.path = path;
  235. file.path += buffer;
  236. }else{
  237. file.path = findPath;
  238. }
  239. file.path += wfd.cFileName;
  240. file.fileNameIndex = string::GetFileName(file.path.c_str()) - file.path.c_str();
  241. file.fileNameHash = string::HashNoCase(&file.path[file.fileNameIndex]);
  242. file.flags = (isFolder ? f_folder : 0) | ((mirrorPath != null) ? f_mirror : 0);
  243. }while(::FindNextFile(handle, &wfd));
  244. ::FindClose(handle);
  245. }
  246. //Перебираем папки для рекурсивного поиска
  247. if(!isInverseOrder && isRecursive)
  248. {
  249. FindOnDiskRecursive(findPath, mirrorPath);
  250. }
  251. }
  252. //Рекурсивный поиск на диске
  253. void Finder::FindOnDiskRecursive(const char * findPath, const char * mirrorPath)
  254. {
  255. WIN32_FIND_DATA wfd;
  256. buffer = findPath;
  257. if(buffer.Len() > 0)
  258. {
  259. Assert(buffer[buffer.Len() - 1] == '\\');
  260. }
  261. buffer += "*.*";
  262. string path;
  263. HANDLE handle = ::FindFirstFile(buffer, &wfd);
  264. if(handle != INVALID_HANDLE_VALUE)
  265. {
  266. do
  267. {
  268. bool isFolder = ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
  269. if(isFolder)
  270. {
  271. if(!IsDots(wfd.cFileName))
  272. {
  273. path = findPath;
  274. path += wfd.cFileName;
  275. path += '\\';
  276. FindOnDisk(path.c_str(), mirrorPath);
  277. }
  278. }
  279. }while(::FindNextFile(handle, &wfd));
  280. ::FindClose(handle);
  281. }
  282. }
  283. //Поиск файлов в загруженых пак-файлах
  284. void Finder::FindInPacks()
  285. {
  286. array<const char *> packsFiles(_FL_, 256);
  287. FileService::object->CollectFilesFromPacks(packsFiles);
  288. dword count = packsFiles.Size();
  289. for(dword i = 0; i < count; i++)
  290. {
  291. //Сравнивайм имя файла с маской
  292. buffer = packsFiles[i];
  293. if(buffer.IsFileMask(mask))
  294. {
  295. FindFile & file = files[files.Add()];
  296. file.path = packsFiles[i];
  297. file.fileNameIndex = string::GetFileName(file.path.c_str()) - file.path.c_str();
  298. file.fileNameHash = string::HashNoCase(&file.path[file.fileNameIndex]);
  299. file.flags = f_pack;
  300. }
  301. }
  302. buffer.Empty();
  303. }
  304. //Проверить точки ли это
  305. bool Finder::IsDots(const char * str)
  306. {
  307. if(!str) return false;
  308. for(const char * name = str; *str; str++)
  309. {
  310. if(*str == '\\' || *str == '/') name = str + 1;
  311. }
  312. if(name[0] == '.')
  313. {
  314. if(name[1] == 0)
  315. {
  316. return true;
  317. }
  318. if(name[1] == '.')
  319. {
  320. if(name[2] == 0)
  321. {
  322. return true;
  323. }
  324. }
  325. }
  326. return false;
  327. }