Moved ScanDirectoryTree() to FileUtil: this is a generic directory scanning function which might be useful in other places as well.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@650 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Maarten ter Huurne 2008-09-23 23:27:38 +00:00
parent c688f95374
commit 35d830924d
4 changed files with 92 additions and 96 deletions

View File

@ -23,6 +23,7 @@
#include <shlobj.h> // for SHGetFolderPath
#include <shellapi.h>
#include <commdlg.h> // for GetSaveFileName
#include <io.h>
#else
#include <sys/stat.h>
@ -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

View File

@ -19,12 +19,22 @@
#define _FILEUTIL_H
#include <string>
#include <vector>
#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<FSTEntry> 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

View File

@ -16,16 +16,8 @@
// http://code.google.com/p/dolphin-emu/
#include "stdafx.h"
#ifdef _WIN32
#include <io.h>
#include <windows.h>
#else
#include <unistd.h>
#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<FSTEntry>::iterator iter = rootEntry.children.begin(); iter != rootEntry.children.end(); ++iter)
for(std::vector<File::FSTEntry>::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<FSTEntry>::const_iterator iter = entry.children.begin(); iter != entry.children.end(); ++iter)
for(std::vector<File::FSTEntry>::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<FSTEntry>& children = parentEntry.children;
for (std::vector<FSTEntry>::const_iterator it = children.begin();
const std::vector<File::FSTEntry>& children = parentEntry.children;
for (std::vector<File::FSTEntry>::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);

View File

@ -19,7 +19,7 @@
#include "Volume.h"
#include "Common.h"
#include <vector>
#include "FileUtil.h"
#include <string>
#include <map>
@ -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<FSTEntry> 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