Utilities: Rewrote file/folder utility functions to use the new filesystem api (works in both VC & GCC)

This commit is contained in:
Souryo 2016-12-11 20:42:59 -05:00
parent 15f105962d
commit 8439290896
4 changed files with 32 additions and 75 deletions

View File

@ -42,7 +42,7 @@ bool RomLoader::LoadFromArchive(istream &zipFile, ArchiveReader& reader, int32_t
reader.LoadArchive(buffer, fileSize);
vector<string> fileList = reader.GetFileList({ ".nes", ".fds", ".nsf", ".nsfe", "*.unf" });
vector<string> 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<string> validExtensions = { { "*.nes", "*.zip", "*.7z", "*.fds" } };
vector<string> validExtensions = { { ".nes", ".zip", ".7z", ".fds" } };
vector<string> romFiles;
for(string extension : validExtensions) {

View File

@ -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();
}

View File

@ -106,7 +106,7 @@ int main(int argc, char* argv[])
}
vector<std::thread*> testThreads;
testFilenames = FolderUtilities::GetFilesInFolder(testFolder, "*.mtp", true);
testFilenames = FolderUtilities::GetFilesInFolder(testFolder, ".mtp", true);
testIndex = 0;
timer.Reset();

View File

@ -1,7 +1,8 @@
#include "stdafx.h"
#ifdef WIN32
#include <shlobj.h>
#endif
//TODO: Use non-experimental namespace (once it is officially supported by VC & GCC)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#include <algorithm>
#include "FolderUtilities.h"
@ -47,94 +48,68 @@ vector<string> 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<string> FolderUtilities::GetFolders(string rootFolder)
{
vector<string> 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<string> FolderUtilities::GetFilesInFolder(string rootFolder, string mask, bool recursive)
{
vector<string> files;
#ifdef WIN32
HANDLE hFind;
WIN32_FIND_DATA data;
vector<string> folders;
if(rootFolder[rootFolder.size() - 1] != '/' && rootFolder[rootFolder.size() - 1] != '\\') {
rootFolder += "/";
}
folders.push_back(rootFolder);
vector<string> folders = { { rootFolder } };
if(recursive) {
for(string subFolder : GetFolders(rootFolder)) {
folders.push_back(subFolder);
@ -142,56 +117,38 @@ vector<string> 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);
}