2014-12-17 05:38:14 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
2013-09-05 01:17:46 +01:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
|
2014-04-09 01:15:08 +01:00
|
|
|
#include "common/common.h"
|
2014-09-09 01:16:32 +01:00
|
|
|
|
2013-09-05 01:17:46 +01:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <dirent.h>
|
|
|
|
#else
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2014-04-09 01:15:08 +01:00
|
|
|
#include "common/file_search.h"
|
|
|
|
#include "common/string_util.h"
|
2013-09-05 01:17:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
CFileSearch::CFileSearch(const CFileSearch::XStringVector& _rSearchStrings, const CFileSearch::XStringVector& _rDirectories)
|
|
|
|
{
|
2014-04-01 23:20:08 +01:00
|
|
|
// Reverse the loop order for speed?
|
|
|
|
for (size_t j = 0; j < _rSearchStrings.size(); j++)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < _rDirectories.size(); i++)
|
|
|
|
{
|
|
|
|
FindFiles(_rSearchStrings[j], _rDirectories[i]);
|
|
|
|
}
|
|
|
|
}
|
2013-09-05 01:17:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CFileSearch::FindFiles(const std::string& _searchString, const std::string& _strPath)
|
|
|
|
{
|
2014-04-01 23:20:08 +01:00
|
|
|
std::string GCMSearchPath;
|
2014-09-07 19:50:43 +01:00
|
|
|
Common::BuildCompleteFilename(GCMSearchPath, _strPath, _searchString);
|
2013-09-05 01:17:46 +01:00
|
|
|
#ifdef _WIN32
|
2014-04-01 23:20:08 +01:00
|
|
|
WIN32_FIND_DATA findData;
|
2014-09-07 19:50:43 +01:00
|
|
|
HANDLE FindFirst = FindFirstFile(Common::UTF8ToTStr(GCMSearchPath).c_str(), &findData);
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
if (FindFirst != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
bool bkeepLooping = true;
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
while (bkeepLooping)
|
2014-11-19 08:49:13 +00:00
|
|
|
{
|
2014-04-01 23:20:08 +01:00
|
|
|
if (findData.cFileName[0] != '.')
|
|
|
|
{
|
|
|
|
std::string strFilename;
|
2014-09-07 19:50:43 +01:00
|
|
|
Common::BuildCompleteFilename(strFilename, _strPath, Common::TStrToUTF8(findData.cFileName));
|
2014-04-01 23:20:08 +01:00
|
|
|
m_FileNames.push_back(strFilename);
|
|
|
|
}
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
bkeepLooping = FindNextFile(FindFirst, &findData) ? true : false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FindClose(FindFirst);
|
2013-09-05 01:17:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
#else
|
2014-04-01 23:20:08 +01:00
|
|
|
// TODO: super lame/broken
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
auto end_match(_searchString);
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
// assuming we have a "*.blah"-like pattern
|
|
|
|
if (!end_match.empty() && end_match[0] == '*')
|
|
|
|
end_match.erase(0, 1);
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
// ugly
|
|
|
|
if (end_match == ".*")
|
|
|
|
end_match.clear();
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
DIR* dir = opendir(_strPath.c_str());
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
if (!dir)
|
|
|
|
return;
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
while (auto const dp = readdir(dir))
|
|
|
|
{
|
|
|
|
std::string found(dp->d_name);
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
if ((found != ".") && (found != "..")
|
|
|
|
&& (found.size() >= end_match.size())
|
|
|
|
&& std::equal(end_match.rbegin(), end_match.rend(), found.rbegin()))
|
|
|
|
{
|
|
|
|
std::string full_name;
|
|
|
|
if (_strPath.c_str()[_strPath.size()-1] == DIR_SEP_CHR)
|
|
|
|
full_name = _strPath + found;
|
|
|
|
else
|
|
|
|
full_name = _strPath + DIR_SEP + found;
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
m_FileNames.push_back(full_name);
|
|
|
|
}
|
|
|
|
}
|
2013-09-05 01:17:46 +01:00
|
|
|
|
2014-04-01 23:20:08 +01:00
|
|
|
closedir(dir);
|
2013-09-05 01:17:46 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
const CFileSearch::XStringVector& CFileSearch::GetFileNames() const
|
|
|
|
{
|
2014-04-01 23:20:08 +01:00
|
|
|
return m_FileNames;
|
2013-09-05 01:17:46 +01:00
|
|
|
}
|