2013-04-18 03:09:55 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <algorithm>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstring>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileSearch.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <dirent.h>
|
2009-01-16 02:58:34 +00:00
|
|
|
#else
|
|
|
|
#include <windows.h>
|
2008-12-08 05:30:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
CFileSearch::CFileSearch(const CFileSearch::XStringVector& _rSearchStrings, const CFileSearch::XStringVector& _rDirectories)
|
|
|
|
{
|
|
|
|
// Reverse the loop order for speed?
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& _rSearchString : _rSearchStrings)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& _rDirectory : _rDirectories)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
FindFiles(_rSearchString, _rDirectory);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CFileSearch::FindFiles(const std::string& _searchString, const std::string& _strPath)
|
|
|
|
{
|
|
|
|
std::string GCMSearchPath;
|
|
|
|
BuildCompleteFilename(GCMSearchPath, _strPath, _searchString);
|
|
|
|
#ifdef _WIN32
|
|
|
|
WIN32_FIND_DATA findData;
|
2013-02-28 00:51:02 +00:00
|
|
|
HANDLE FindFirst = FindFirstFile(UTF8ToTStr(GCMSearchPath).c_str(), &findData);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
if (FindFirst != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
bool bkeepLooping = true;
|
|
|
|
|
|
|
|
while (bkeepLooping)
|
2013-10-19 22:58:02 +00:00
|
|
|
{
|
2008-12-08 05:30:24 +00:00
|
|
|
if (findData.cFileName[0] != '.')
|
|
|
|
{
|
|
|
|
std::string strFilename;
|
2013-02-28 00:51:02 +00:00
|
|
|
BuildCompleteFilename(strFilename, _strPath, TStrToUTF8(findData.cFileName));
|
2008-12-08 05:30:24 +00:00
|
|
|
m_FileNames.push_back(strFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
bkeepLooping = FindNextFile(FindFirst, &findData) ? true : false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FindClose(FindFirst);
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
2013-01-13 21:32:26 +00:00
|
|
|
// TODO: super lame/broken
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2013-01-13 21:32:26 +00:00
|
|
|
auto end_match(_searchString);
|
|
|
|
|
|
|
|
// assuming we have a "*.blah"-like pattern
|
|
|
|
if (!end_match.empty() && end_match[0] == '*')
|
|
|
|
end_match.erase(0, 1);
|
|
|
|
|
|
|
|
// ugly
|
|
|
|
if (end_match == ".*")
|
|
|
|
end_match.clear();
|
2008-12-08 05:30:24 +00:00
|
|
|
|
|
|
|
DIR* dir = opendir(_strPath.c_str());
|
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
return;
|
|
|
|
|
2013-01-13 21:32:26 +00:00
|
|
|
while (auto const dp = readdir(dir))
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2013-01-13 21:32:26 +00:00
|
|
|
std::string found(dp->d_name);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-03-10 11:30:55 +00:00
|
|
|
if ((found != ".") && (found != "..") &&
|
|
|
|
(found.size() >= end_match.size()) &&
|
|
|
|
std::equal(end_match.rbegin(), end_match.rend(), found.rbegin()))
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2010-04-08 16:59:35 +00:00
|
|
|
std::string full_name;
|
|
|
|
if (_strPath.c_str()[_strPath.size()-1] == DIR_SEP_CHR)
|
2013-01-13 21:32:26 +00:00
|
|
|
full_name = _strPath + found;
|
2010-04-08 16:59:35 +00:00
|
|
|
else
|
2013-01-13 21:32:26 +00:00
|
|
|
full_name = _strPath + DIR_SEP + found;
|
2010-01-13 21:09:31 +00:00
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
m_FileNames.push_back(full_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
const CFileSearch::XStringVector& CFileSearch::GetFileNames() const
|
|
|
|
{
|
2010-04-08 16:59:35 +00:00
|
|
|
return m_FileNames;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|