summaryrefslogtreecommitdiff
path: root/Source/Core/Common/FileUtil.cpp
diff options
context:
space:
mode:
authorcomex <comexk@gmail.com>2014-11-15 15:46:40 -0500
committercomex <comexk@gmail.com>2015-05-28 19:14:42 -0400
commita225426510fc25316b3868e8764893dff284e77e (patch)
tree675c90e5057f2bbd5d2758d560845531e4dff303 /Source/Core/Common/FileUtil.cpp
parent6ff3fcee597663066c966a54ee51aab7fa166cd9 (diff)
Rewrite FileSearch and improve ScanDirectoryTree.
- FileSearch is now just one function, and it converts the original glob into a regex on all platforms rather than relying on native Windows pattern matching on there and a complete hack elsewhere. It now supports recursion out of the box rather than manually expanding into a full list of directories in multiple call sites. - This adds a GCC >= 4.9 dependency due to older versions having outright broken <regex>. MSVC is fine with it. - ScanDirectoryTree returns the parent entry rather than filling parts of it in via reference. The count is now stored in the entry like it was for subdirectories. - .glsl file search is now done with DoFileSearch. - IOCTLV_READ_DIR now uses ScanDirectoryTree directly and sorts the results after replacements for better determinism.
Diffstat (limited to 'Source/Core/Common/FileUtil.cpp')
-rw-r--r--Source/Core/Common/FileUtil.cpp75
1 files changed, 37 insertions, 38 deletions
diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp
index 6e721b9766..980821e010 100644
--- a/Source/Core/Common/FileUtil.cpp
+++ b/Source/Core/Common/FileUtil.cpp
@@ -453,11 +453,14 @@ bool CreateEmptyFile(const std::string &filename)
// Scans the directory tree gets, starting from _Directory and adds the
// results into parentEntry. Returns the number of files+directories found
-u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry)
+FSTEntry ScanDirectoryTree(const std::string &directory, bool recursive)
{
INFO_LOG(COMMON, "ScanDirectoryTree: directory %s", directory.c_str());
// How many files + directories we found
- u32 foundEntries = 0;
+ FSTEntry parent_entry;
+ parent_entry.physicalName = directory;
+ parent_entry.isDirectory = true;
+ parent_entry.size = 0;
#ifdef _WIN32
// Find the first file in the directory.
WIN32_FIND_DATA ffd;
@@ -466,59 +469,55 @@ u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry)
if (hFind == INVALID_HANDLE_VALUE)
{
FindClose(hFind);
- return foundEntries;
+ return parent_entry;
}
// Windows loop
do
{
- FSTEntry entry;
- const std::string virtualName(TStrToUTF8(ffd.cFileName));
+ const std::string virtual_name(TStrToUTF8(ffd.cFileName));
#else
struct dirent dirent, *result = nullptr;
DIR *dirp = opendir(directory.c_str());
if (!dirp)
- return 0;
-
- // non Windows loop
- while (!readdir_r(dirp, &dirent, &result) && result)
- {
- FSTEntry entry;
- const std::string virtualName(result->d_name);
-#endif
- // check for "." and ".."
- if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
- ((virtualName[0] == '.') && (virtualName[1] == '.') &&
- (virtualName[2] == '\0')))
- continue;
- entry.virtualName = virtualName;
- entry.physicalName = directory;
- entry.physicalName += DIR_SEP + entry.virtualName;
+ return parent_entry;
- if (IsDirectory(entry.physicalName))
+ // non Windows loop
+ while (!readdir_r(dirp, &dirent, &result) && result)
{
- entry.isDirectory = true;
- // is a directory, lets go inside
- entry.size = ScanDirectoryTree(entry.physicalName, entry);
- foundEntries += (u32)entry.size;
- }
- else
- { // is a file
- entry.isDirectory = false;
- entry.size = GetSize(entry.physicalName.c_str());
- }
- ++foundEntries;
- // Push into the tree
- parentEntry.children.push_back(entry);
-#ifdef _WIN32
- } while (FindNextFile(hFind, &ffd) != 0);
+ const std::string virtual_name(result->d_name);
+ #endif
+ if (virtual_name == "." || virtual_name == "..")
+ continue;
+ auto physical_name = directory + DIR_SEP + virtual_name;
+ FSTEntry entry;
+ entry.isDirectory = IsDirectory(physical_name);
+ if (entry.isDirectory)
+ {
+ if (recursive)
+ entry = ScanDirectoryTree(physical_name, true);
+ else
+ entry.size = 0;
+ }
+ else
+ {
+ entry.size = GetSize(physical_name);
+ }
+ entry.virtualName = virtual_name;
+ entry.physicalName = physical_name;
+
+ ++parent_entry.size;
+ // Push into the tree
+ parent_entry.children.push_back(entry);
+ #ifdef _WIN32
+ } while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
#else
}
closedir(dirp);
#endif
// Return number of entries found.
- return foundEntries;
+ return parent_entry;
}