file_search.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/common.h"
  5. #ifndef _WIN32
  6. #include <dirent.h>
  7. #else
  8. #include <windows.h>
  9. #endif
  10. #include <algorithm>
  11. #include "common/file_search.h"
  12. #include "common/string_util.h"
  13. CFileSearch::CFileSearch(const CFileSearch::XStringVector& _rSearchStrings, const CFileSearch::XStringVector& _rDirectories)
  14. {
  15. // Reverse the loop order for speed?
  16. for (size_t j = 0; j < _rSearchStrings.size(); j++)
  17. {
  18. for (size_t i = 0; i < _rDirectories.size(); i++)
  19. {
  20. FindFiles(_rSearchStrings[j], _rDirectories[i]);
  21. }
  22. }
  23. }
  24. void CFileSearch::FindFiles(const std::string& _searchString, const std::string& _strPath)
  25. {
  26. std::string GCMSearchPath;
  27. Common::BuildCompleteFilename(GCMSearchPath, _strPath, _searchString);
  28. #ifdef _WIN32
  29. WIN32_FIND_DATA findData;
  30. HANDLE FindFirst = FindFirstFile(Common::UTF8ToTStr(GCMSearchPath).c_str(), &findData);
  31. if (FindFirst != INVALID_HANDLE_VALUE)
  32. {
  33. bool bkeepLooping = true;
  34. while (bkeepLooping)
  35. {
  36. if (findData.cFileName[0] != '.')
  37. {
  38. std::string strFilename;
  39. Common::BuildCompleteFilename(strFilename, _strPath, Common::TStrToUTF8(findData.cFileName));
  40. m_FileNames.push_back(strFilename);
  41. }
  42. bkeepLooping = FindNextFile(FindFirst, &findData) ? true : false;
  43. }
  44. }
  45. FindClose(FindFirst);
  46. #else
  47. // TODO: super lame/broken
  48. auto end_match(_searchString);
  49. // assuming we have a "*.blah"-like pattern
  50. if (!end_match.empty() && end_match[0] == '*')
  51. end_match.erase(0, 1);
  52. // ugly
  53. if (end_match == ".*")
  54. end_match.clear();
  55. DIR* dir = opendir(_strPath.c_str());
  56. if (!dir)
  57. return;
  58. while (auto const dp = readdir(dir))
  59. {
  60. std::string found(dp->d_name);
  61. if ((found != ".") && (found != "..")
  62. && (found.size() >= end_match.size())
  63. && std::equal(end_match.rbegin(), end_match.rend(), found.rbegin()))
  64. {
  65. std::string full_name;
  66. if (_strPath.c_str()[_strPath.size()-1] == DIR_SEP_CHR)
  67. full_name = _strPath + found;
  68. else
  69. full_name = _strPath + DIR_SEP + found;
  70. m_FileNames.push_back(full_name);
  71. }
  72. }
  73. closedir(dir);
  74. #endif
  75. }
  76. const CFileSearch::XStringVector& CFileSearch::GetFileNames() const
  77. {
  78. return m_FileNames;
  79. }