summaryrefslogtreecommitdiff
path: root/src/common/file_search.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/file_search.cpp')
-rw-r--r--src/common/file_search.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/common/file_search.cpp b/src/common/file_search.cpp
new file mode 100644
index 000000000..59f640109
--- /dev/null
+++ b/src/common/file_search.cpp
@@ -0,0 +1,106 @@
1// Copyright 2013 Dolphin Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5
6#include "common.h"
7#include "common_paths.h"
8#ifndef _WIN32
9#include <sys/types.h>
10#include <dirent.h>
11#else
12#include <windows.h>
13#endif
14
15#include <string>
16#include <algorithm>
17
18#include "file_search.h"
19
20#include "string_util.h"
21
22
23CFileSearch::CFileSearch(const CFileSearch::XStringVector& _rSearchStrings, const CFileSearch::XStringVector& _rDirectories)
24{
25 // Reverse the loop order for speed?
26 for (size_t j = 0; j < _rSearchStrings.size(); j++)
27 {
28 for (size_t i = 0; i < _rDirectories.size(); i++)
29 {
30 FindFiles(_rSearchStrings[j], _rDirectories[i]);
31 }
32 }
33}
34
35
36void CFileSearch::FindFiles(const std::string& _searchString, const std::string& _strPath)
37{
38 std::string GCMSearchPath;
39 BuildCompleteFilename(GCMSearchPath, _strPath, _searchString);
40#ifdef _WIN32
41 WIN32_FIND_DATA findData;
42 HANDLE FindFirst = FindFirstFile(UTF8ToTStr(GCMSearchPath).c_str(), &findData);
43
44 if (FindFirst != INVALID_HANDLE_VALUE)
45 {
46 bool bkeepLooping = true;
47
48 while (bkeepLooping)
49 {
50 if (findData.cFileName[0] != '.')
51 {
52 std::string strFilename;
53 BuildCompleteFilename(strFilename, _strPath, TStrToUTF8(findData.cFileName));
54 m_FileNames.push_back(strFilename);
55 }
56
57 bkeepLooping = FindNextFile(FindFirst, &findData) ? true : false;
58 }
59 }
60 FindClose(FindFirst);
61
62
63#else
64 // TODO: super lame/broken
65
66 auto end_match(_searchString);
67
68 // assuming we have a "*.blah"-like pattern
69 if (!end_match.empty() && end_match[0] == '*')
70 end_match.erase(0, 1);
71
72 // ugly
73 if (end_match == ".*")
74 end_match.clear();
75
76 DIR* dir = opendir(_strPath.c_str());
77
78 if (!dir)
79 return;
80
81 while (auto const dp = readdir(dir))
82 {
83 std::string found(dp->d_name);
84
85 if ((found != ".") && (found != "..")
86 && (found.size() >= end_match.size())
87 && std::equal(end_match.rbegin(), end_match.rend(), found.rbegin()))
88 {
89 std::string full_name;
90 if (_strPath.c_str()[_strPath.size()-1] == DIR_SEP_CHR)
91 full_name = _strPath + found;
92 else
93 full_name = _strPath + DIR_SEP + found;
94
95 m_FileNames.push_back(full_name);
96 }
97 }
98
99 closedir(dir);
100#endif
101}
102
103const CFileSearch::XStringVector& CFileSearch::GetFileNames() const
104{
105 return m_FileNames;
106}