From 8439290896480b3657d929b4a53c61a1154c08f4 Mon Sep 17 00:00:00 2001 From: Souryo Date: Sun, 11 Dec 2016 20:42:59 -0500 Subject: [PATCH] Utilities: Rewrote file/folder utility functions to use the new filesystem api (works in both VC & GCC) --- Core/RomLoader.cpp | 4 +- GUI.NET/Forms/frmMain.cs | 2 +- TestHelper/TestHelper.cpp | 2 +- Utilities/FolderUtilities.cpp | 99 ++++++++++------------------------- 4 files changed, 32 insertions(+), 75 deletions(-) diff --git a/Core/RomLoader.cpp b/Core/RomLoader.cpp index 72e3ac74..0cf7a37a 100644 --- a/Core/RomLoader.cpp +++ b/Core/RomLoader.cpp @@ -42,7 +42,7 @@ bool RomLoader::LoadFromArchive(istream &zipFile, ArchiveReader& reader, int32_t reader.LoadArchive(buffer, fileSize); - vector fileList = reader.GetFileList({ ".nes", ".fds", ".nsf", ".nsfe", "*.unf" }); + vector fileList = reader.GetFileList({ ".nes", ".fds", ".nsf", ".nsfe", ".unf" }); int32_t currentIndex = 0; if(archiveFileIndex > (int32_t)fileList.size()) { return false; @@ -207,7 +207,7 @@ int32_t RomLoader::FindMatchingRomInFile(string filename, uint32_t crc32Hash) string RomLoader::FindMatchingRomInFolder(string folder, string romFilename, uint32_t crc32Hash, bool useFastSearch, int32_t &archiveFileIndex) { std::transform(romFilename.begin(), romFilename.end(), romFilename.begin(), ::tolower); - vector validExtensions = { { "*.nes", "*.zip", "*.7z", "*.fds" } }; + vector validExtensions = { { ".nes", ".zip", ".7z", ".fds" } }; vector romFiles; for(string extension : validExtensions) { diff --git a/GUI.NET/Forms/frmMain.cs b/GUI.NET/Forms/frmMain.cs index 5ca57fb0..0bf217d0 100644 --- a/GUI.NET/Forms/frmMain.cs +++ b/GUI.NET/Forms/frmMain.cs @@ -763,7 +763,7 @@ namespace Mesen.GUI.Forms if(fileTime == 0) { label = i.ToString() + ". " + ResourceHelper.GetMessage("EmptyState"); } else { - DateTime dateTime = DateTime.FromFileTime(fileTime); + DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(fileTime).ToLocalTime(); label = i.ToString() + ". " + dateTime.ToShortDateString() + " " + dateTime.ToShortTimeString(); } diff --git a/TestHelper/TestHelper.cpp b/TestHelper/TestHelper.cpp index f213a51d..19f04233 100644 --- a/TestHelper/TestHelper.cpp +++ b/TestHelper/TestHelper.cpp @@ -106,7 +106,7 @@ int main(int argc, char* argv[]) } vector testThreads; - testFilenames = FolderUtilities::GetFilesInFolder(testFolder, "*.mtp", true); + testFilenames = FolderUtilities::GetFilesInFolder(testFolder, ".mtp", true); testIndex = 0; timer.Reset(); diff --git a/Utilities/FolderUtilities.cpp b/Utilities/FolderUtilities.cpp index a31fa64d..8e69e5be 100644 --- a/Utilities/FolderUtilities.cpp +++ b/Utilities/FolderUtilities.cpp @@ -1,7 +1,8 @@ #include "stdafx.h" -#ifdef WIN32 - #include -#endif + +//TODO: Use non-experimental namespace (once it is officially supported by VC & GCC) +#include +namespace fs = std::experimental::filesystem; #include #include "FolderUtilities.h" @@ -47,94 +48,68 @@ vector FolderUtilities::GetKnownGameFolders() string FolderUtilities::GetSaveFolder() { - string folder = CombinePath(GetHomeFolder(), "Saves\\"); + string folder = CombinePath(GetHomeFolder(), "Saves"); CreateFolder(folder); return folder; } string FolderUtilities::GetHdPackFolder() { - string folder = CombinePath(GetHomeFolder(), "HdPacks\\"); + string folder = CombinePath(GetHomeFolder(), "HdPacks"); CreateFolder(folder); return folder; } string FolderUtilities::GetDebuggerFolder() { - string folder = CombinePath(GetHomeFolder(), "Debugger\\"); + string folder = CombinePath(GetHomeFolder(), "Debugger"); CreateFolder(folder); return folder; } string FolderUtilities::GetSaveStateFolder() { - string folder = CombinePath(GetHomeFolder(), "SaveStates\\"); + string folder = CombinePath(GetHomeFolder(), "SaveStates"); CreateFolder(folder); return folder; } string FolderUtilities::GetMovieFolder() { - string folder = CombinePath(GetHomeFolder(), + "Movies\\"); + string folder = CombinePath(GetHomeFolder(), + "Movies"); CreateFolder(folder); return folder; } string FolderUtilities::GetScreenshotFolder() { - string folder = CombinePath(GetHomeFolder(), "Screenshots\\"); + string folder = CombinePath(GetHomeFolder(), "Screenshots"); CreateFolder(folder); return folder; } void FolderUtilities::CreateFolder(string folder) { -#ifdef WIN32 - CreateDirectory(utf8::utf8::decode(folder).c_str(), nullptr); -#endif + fs::create_directory(folder); } vector FolderUtilities::GetFolders(string rootFolder) { vector folders; -#ifdef WIN32 - HANDLE hFind; - WIN32_FIND_DATA data; - hFind = FindFirstFile(utf8::utf8::decode(rootFolder + "*").c_str(), &data); - if(hFind != INVALID_HANDLE_VALUE) { - do { - string filename = utf8::utf8::encode(data.cFileName); - if(data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && filename.compare(".") != 0 && filename.compare("..") != 0) { - string subfolder = rootFolder + filename + "\\"; - folders.push_back(subfolder); - for(string folderName : GetFolders(subfolder.c_str())) { - folders.push_back(folderName); - } - } + for(fs::recursive_directory_iterator i(rootFolder), end; i != end; i++) { + if(fs::is_directory(i->path())) { + folders.push_back(i->path().string()); } - while(FindNextFile(hFind, &data)); - FindClose(hFind); } -#endif + return folders; } vector FolderUtilities::GetFilesInFolder(string rootFolder, string mask, bool recursive) { vector files; - -#ifdef WIN32 - HANDLE hFind; - WIN32_FIND_DATA data; - - vector folders; - if(rootFolder[rootFolder.size() - 1] != '/' && rootFolder[rootFolder.size() - 1] != '\\') { - rootFolder += "/"; - } - - folders.push_back(rootFolder); - + vector folders = { { rootFolder } }; if(recursive) { for(string subFolder : GetFolders(rootFolder)) { folders.push_back(subFolder); @@ -142,56 +117,38 @@ vector FolderUtilities::GetFilesInFolder(string rootFolder, string mask, } for(string folder : folders) { - hFind = FindFirstFile(utf8::utf8::decode(folder + mask).c_str(), &data); - if(hFind != INVALID_HANDLE_VALUE) { - do { - files.push_back(folder + utf8::utf8::encode(data.cFileName)); - } while(FindNextFile(hFind, &data)); - FindClose(hFind); + for(fs::directory_iterator i(fs::path(folder.c_str())), end; i != end; i++) { + string extension = i->path().extension().string(); + std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower); + if(extension == mask) { + files.push_back(i->path().string()); + } } } -#endif return files; } string FolderUtilities::GetFilename(string filepath, bool includeExtension) { - size_t index = filepath.find_last_of("/\\"); - string filename = (index == std::string::basic_string::npos) ? filepath : filepath.substr(index + 1); + fs::path filename = fs::path(filepath).filename(); if(!includeExtension) { - filename = filename.substr(0, filename.find_last_of(".")); + filename.replace_extension(""); } - return filename; + return filename.string(); } string FolderUtilities::GetFolderName(string filepath) { - size_t index = filepath.find_last_of("/\\"); - return filepath.substr(0, index); + return fs::path(filepath).remove_filename().string(); } string FolderUtilities::CombinePath(string folder, string filename) { -#ifdef WIN32 - string separator = "\\"; -#else - string separator = "/"; -#endif - - if(folder.find_last_of(separator) != folder.length() - 1) { - folder += separator; - } - - return folder + filename; + return fs::path(folder).append(filename).string(); } int64_t FolderUtilities::GetFileModificationTime(string filepath) { -#ifdef WIN32 - WIN32_FILE_ATTRIBUTE_DATA fileAttrData = {0}; - GetFileAttributesEx(utf8::utf8::decode(filepath).c_str(), GetFileExInfoStandard, &fileAttrData); - return ((int64_t)fileAttrData.ftLastWriteTime.dwHighDateTime << 32) | (int64_t)fileAttrData.ftLastWriteTime.dwLowDateTime; -#endif - return 0; + return fs::last_write_time(fs::path(filepath)).time_since_epoch() / std::chrono::seconds(1); } \ No newline at end of file