Files
dolphin/Source/Core/Common/FileSearch.cpp
T

103 lines
2.2 KiB
C++
Raw Normal View History

// 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 05:18:15 -05:00
#include <algorithm>
#include <cstring>
2014-02-17 05:18:15 -05: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?
for (auto& _rSearchString : _rSearchStrings)
2008-12-08 05:30:24 +00:00
{
for (auto& _rDirectory : _rDirectories)
2008-12-08 05:30:24 +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;
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 18:58:02 -04:00
{
2008-12-08 05:30:24 +00:00
if (findData.cFileName[0] != '.')
{
std::string strFilename;
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
// TODO: super lame/broken
2008-12-08 05:30:24 +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;
while (auto const dp = readdir(dir))
2008-12-08 05:30:24 +00:00
{
std::string found(dp->d_name);
2008-12-08 05:30:24 +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)
full_name = _strPath + found;
2010-04-08 16:59:35 +00:00
else
full_name = _strPath + DIR_SEP + found;
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
}