diff --git a/Source/Core/Common/Src/FileUtil.cpp b/Source/Core/Common/Src/FileUtil.cpp index ab8894ee75..742ec097ee 100644 --- a/Source/Core/Common/Src/FileUtil.cpp +++ b/Source/Core/Common/Src/FileUtil.cpp @@ -23,6 +23,7 @@ #include // for SHGetFolderPath #include #include // for GetSaveFileName +#include #else #include @@ -169,4 +170,70 @@ u64 GetSize(const char *filename) return pos; } +#ifdef _WIN32 +static bool ReadFoundFile(const WIN32_FIND_DATA& ffd, FSTEntry& entry) +{ + // ignore files starting with a . + if(strncmp(ffd.cFileName, ".", 1) == 0) + return false; + + entry.virtualName = ffd.cFileName; + + if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + entry.isDirectory = true; + } + else + { + entry.isDirectory = false; + entry.size = ffd.nFileSizeLow; + } + + return true; +} + +u32 ScanDirectoryTree(const std::string& _Directory, FSTEntry& parentEntry) +{ + // Find the first file in the directory. + WIN32_FIND_DATA ffd; + std::string searchName = _Directory + "\\*"; + HANDLE hFind = FindFirstFile(searchName.c_str(), &ffd); + + u32 foundEntries = 0; + + if (hFind != INVALID_HANDLE_VALUE) + { + do + { + FSTEntry entry; + + if(ReadFoundFile(ffd, entry)) + { + entry.physicalName = _Directory + "\\" + entry.virtualName; + if(entry.isDirectory) + { + u32 childEntries = ScanDirectoryTree(entry.physicalName, entry); + entry.size = childEntries; + foundEntries += childEntries; + } + + ++foundEntries; + + parentEntry.children.push_back(entry); + } + } while (FindNextFile(hFind, &ffd) != 0); + } + + FindClose(hFind); + + return foundEntries; +} +#else +u32 ScanDirectoryTree(const std::string& _Directory, FSTEntry& parentEntry) +{ + // TODO - Insert linux stuff here + return 0; +} +#endif + } // namespace diff --git a/Source/Core/Common/Src/FileUtil.h b/Source/Core/Common/Src/FileUtil.h index b0bebb5c35..fac0486251 100644 --- a/Source/Core/Common/Src/FileUtil.h +++ b/Source/Core/Common/Src/FileUtil.h @@ -19,12 +19,22 @@ #define _FILEUTIL_H #include +#include #include "Common.h" namespace File { +struct FSTEntry +{ + bool isDirectory; + u32 size; // file length or number of entries from children + std::string physicalName; // name on disk + std::string virtualName; // name in FST names table + std::vector children; +}; + bool Exists(const char *filename); void Launch(const char *filename); void Explore(const char *path); @@ -34,6 +44,8 @@ bool Delete(const char *filename); u64 GetSize(const char *filename); std::string GetUserDirectory(); +u32 ScanDirectoryTree(const std::string& _Directory, FSTEntry& parentEntry); + } // namespace #endif diff --git a/Source/Core/DiscIO/Src/VolumeDirectory.cpp b/Source/Core/DiscIO/Src/VolumeDirectory.cpp index 9ad87a1370..3bf641c519 100644 --- a/Source/Core/DiscIO/Src/VolumeDirectory.cpp +++ b/Source/Core/DiscIO/Src/VolumeDirectory.cpp @@ -16,16 +16,8 @@ // http://code.google.com/p/dolphin-emu/ #include "stdafx.h" -#ifdef _WIN32 -#include -#include -#else -#include -#endif - #include "VolumeDirectory.h" #include "FileBlob.h" -#include "FileUtil.h" namespace DiscIO { @@ -300,7 +292,7 @@ void CVolumeDirectory::BuildFST() delete m_FSTData; } - FSTEntry rootEntry; + File::FSTEntry rootEntry; // read data from physical disk to rootEntry u32 totalEntries = AddDirectoryEntries(m_rootDirectory, rootEntry) + 1; @@ -320,7 +312,7 @@ void CVolumeDirectory::BuildFST() // write root entry WriteEntryData(fstOffset, DIRECTORY_ENTRY, 0, 0, totalEntries); - for(std::vector::iterator iter = rootEntry.children.begin(); iter != rootEntry.children.end(); ++iter) + for(std::vector::iterator iter = rootEntry.children.begin(); iter != rootEntry.children.end(); ++iter) { WriteEntry(*iter, fstOffset, nameOffset, curDataAddress, rootOffset); } @@ -404,7 +396,7 @@ void CVolumeDirectory::WriteEntryName(u32& nameOffset, const std::string& name) nameOffset += (name.length() + 1); } -void CVolumeDirectory::WriteEntry(const FSTEntry& entry, u32& fstOffset, u32& nameOffset, u64& dataOffset, u32 parentEntryNum) +void CVolumeDirectory::WriteEntry(const File::FSTEntry& entry, u32& fstOffset, u32& nameOffset, u64& dataOffset, u32 parentEntryNum) { if(entry.isDirectory) { @@ -413,7 +405,7 @@ void CVolumeDirectory::WriteEntry(const FSTEntry& entry, u32& fstOffset, u32& na WriteEntryData(fstOffset, DIRECTORY_ENTRY, nameOffset, parentEntryNum, myEntryNum + entry.size + 1); WriteEntryName(nameOffset, entry.virtualName); - for(std::vector::const_iterator iter = entry.children.begin(); iter != entry.children.end(); ++iter) + for(std::vector::const_iterator iter = entry.children.begin(); iter != entry.children.end(); ++iter) { WriteEntry(*iter, fstOffset, nameOffset, dataOffset, myEntryNum); } @@ -433,80 +425,14 @@ void CVolumeDirectory::WriteEntry(const FSTEntry& entry, u32& fstOffset, u32& na } } -#ifdef _WIN32 -static bool ReadFoundFile(const WIN32_FIND_DATA& ffd, FSTEntry& entry) -{ - // ignore files starting with a . - if(strncmp(ffd.cFileName, ".", 1) == 0) - return false; - - entry.virtualName = ffd.cFileName; - - if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - { - entry.isDirectory = true; - } - else - { - entry.isDirectory = false; - entry.size = ffd.nFileSizeLow; - } - - return true; -} - -static u32 ScanDirectoryTree(const std::string& _Directory, FSTEntry& parentEntry) -{ - // Find the first file in the directory. - WIN32_FIND_DATA ffd; - std::string searchName = _Directory + "\\*"; - HANDLE hFind = FindFirstFile(searchName.c_str(), &ffd); - - u32 foundEntries = 0; - - if (hFind != INVALID_HANDLE_VALUE) - { - do - { - FSTEntry entry; - - if(ReadFoundFile(ffd, entry)) - { - entry.physicalName = _Directory + "\\" + entry.virtualName; - if(entry.isDirectory) - { - u32 childEntries = ScanDirectoryTree(entry.physicalName, entry); - entry.size = childEntries; - foundEntries += childEntries; - } - - ++foundEntries; - - parentEntry.children.push_back(entry); - } - } while (FindNextFile(hFind, &ffd) != 0); - } - - FindClose(hFind); - - return foundEntries; -} -#else -static u32 ScanDirectoryTree(const std::string& _Directory, FSTEntry& parentEntry) -{ - // TODO - Insert linux stuff here - return 0; -} -#endif - -static u32 ComputeNameSize(const FSTEntry& parentEntry) +static u32 ComputeNameSize(const File::FSTEntry& parentEntry) { u32 nameSize = 0; - const std::vector& children = parentEntry.children; - for (std::vector::const_iterator it = children.begin(); + const std::vector& children = parentEntry.children; + for (std::vector::const_iterator it = children.begin(); it != children.end(); ++it) { - const FSTEntry& entry = *it; + const File::FSTEntry& entry = *it; if (entry.isDirectory) { nameSize += ComputeNameSize(entry); @@ -516,7 +442,7 @@ static u32 ComputeNameSize(const FSTEntry& parentEntry) return nameSize; } -u32 CVolumeDirectory::AddDirectoryEntries(const std::string& _Directory, FSTEntry& parentEntry) +u32 CVolumeDirectory::AddDirectoryEntries(const std::string& _Directory, File::FSTEntry& parentEntry) { u32 foundEntries = ScanDirectoryTree(_Directory, parentEntry); m_totalNameSize += ComputeNameSize(parentEntry); diff --git a/Source/Core/DiscIO/Src/VolumeDirectory.h b/Source/Core/DiscIO/Src/VolumeDirectory.h index f88abbbb69..7642839632 100644 --- a/Source/Core/DiscIO/Src/VolumeDirectory.h +++ b/Source/Core/DiscIO/Src/VolumeDirectory.h @@ -19,7 +19,7 @@ #include "Volume.h" #include "Common.h" -#include +#include "FileUtil.h" #include #include @@ -30,15 +30,6 @@ namespace DiscIO { -struct FSTEntry -{ - bool isDirectory; - u32 size; // file length or number of entries from children - std::string physicalName; // name on disk - std::string virtualName; // name in FST names table - std::vector children; -}; - class CVolumeDirectory : public IVolume { @@ -81,10 +72,10 @@ class CVolumeDirectory // FST creation void WriteEntryData(u32& entryOffset, u8 type, u32 nameOffset, u64 dataOffset, u32 length); void WriteEntryName(u32& nameOffset, const std::string& name); - void WriteEntry(const FSTEntry& entry, u32& fstOffset, u32& nameOffset, u64& dataOffset, u32 parentEntryNum); + void WriteEntry(const File::FSTEntry& entry, u32& fstOffset, u32& nameOffset, u64& dataOffset, u32 parentEntryNum); // returns number of entries found in _Directory - u32 AddDirectoryEntries(const std::string& _Directory, FSTEntry& parentEntry); + u32 AddDirectoryEntries(const std::string& _Directory, File::FSTEntry& parentEntry); std::string m_rootDirectory; @@ -105,5 +96,5 @@ class CVolumeDirectory u8* m_FSTData; u8* m_diskHeader; }; -} // namespace +} // namespace