mirror of
https://github.com/OpenRCT2/OpenRCT2.git
synced 2025-01-22 18:31:59 -05:00
Convert FileEnumerator to IFileScanner
This commit is contained in:
parent
a17748eb41
commit
16626eabb8
5 changed files with 174 additions and 152 deletions
|
@ -246,13 +246,14 @@ private:
|
|||
String::Set(pattern, sizeof(pattern), directory);
|
||||
Path::Append(pattern, sizeof(pattern), "*.sc6");
|
||||
|
||||
auto fileEnumerator = FileEnumerator(pattern, true);
|
||||
while (fileEnumerator.Next())
|
||||
IFileScanner * scanner = Path::ScanDirectory(pattern, true);
|
||||
while (scanner->Next())
|
||||
{
|
||||
auto path = fileEnumerator.GetPath();
|
||||
auto fileInfo = fileEnumerator.GetFileInfo();
|
||||
auto path = scanner->GetPath();
|
||||
auto fileInfo = scanner->GetFileInfo();
|
||||
AddScenario(path, fileInfo->last_modified);
|
||||
}
|
||||
delete scanner;
|
||||
}
|
||||
|
||||
void AddScenario(const utf8 * path, uint64 timestamp)
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#include <stack>
|
||||
#include "FileEnumerator.h"
|
||||
#include "Memory.hpp"
|
||||
#include "Path.hpp"
|
||||
|
@ -24,7 +25,30 @@ extern "C"
|
|||
#include "../platform/platform.h"
|
||||
}
|
||||
|
||||
FileEnumerator::FileEnumerator(const utf8 * pattern, bool recurse)
|
||||
class FileScanner : public IFileScanner
|
||||
{
|
||||
private:
|
||||
struct DirectoryState
|
||||
{
|
||||
utf8 * Directory;
|
||||
int Handle;
|
||||
};
|
||||
|
||||
// Enumeration options
|
||||
utf8 * _rootPath;
|
||||
utf8 * _pattern;
|
||||
bool _recurse;
|
||||
|
||||
// Enumeration state
|
||||
int _fileHandle;
|
||||
std::stack<DirectoryState> _directoryStack;
|
||||
|
||||
// Current enumeration
|
||||
file_info * _fileInfo;
|
||||
utf8 * _path;
|
||||
|
||||
public:
|
||||
FileScanner(const utf8 * pattern, bool recurse)
|
||||
{
|
||||
_rootPath = Memory::Allocate<utf8>(MAX_PATH);
|
||||
Path::GetDirectory(_rootPath, MAX_PATH, pattern);
|
||||
|
@ -40,7 +64,7 @@ FileEnumerator::FileEnumerator(const utf8 * pattern, bool recurse)
|
|||
Reset();
|
||||
}
|
||||
|
||||
FileEnumerator::~FileEnumerator()
|
||||
~FileScanner() override
|
||||
{
|
||||
CloseHandles();
|
||||
Memory::Free(_path);
|
||||
|
@ -49,7 +73,17 @@ FileEnumerator::~FileEnumerator()
|
|||
Memory::Free(_rootPath);
|
||||
}
|
||||
|
||||
void FileEnumerator::Reset()
|
||||
const file_info * GetFileInfo() const override
|
||||
{
|
||||
return _fileInfo;
|
||||
}
|
||||
|
||||
const utf8 * GetPath() const override
|
||||
{
|
||||
return _path;
|
||||
}
|
||||
|
||||
void Reset() override
|
||||
{
|
||||
CloseHandles();
|
||||
|
||||
|
@ -59,7 +93,7 @@ void FileEnumerator::Reset()
|
|||
_directoryStack.push(directoryState);
|
||||
}
|
||||
|
||||
bool FileEnumerator::Next()
|
||||
bool Next() override
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
|
@ -80,8 +114,7 @@ bool FileEnumerator::Next()
|
|||
|
||||
_fileHandle = platform_enumerate_files_begin(pattern);
|
||||
break;
|
||||
}
|
||||
else
|
||||
} else
|
||||
{
|
||||
// Next directory
|
||||
utf8 name[MAX_PATH];
|
||||
|
@ -95,8 +128,7 @@ bool FileEnumerator::Next()
|
|||
Path::Append(newDirectoryState.Directory, MAX_PATH, name);
|
||||
|
||||
_directoryStack.push(newDirectoryState);
|
||||
}
|
||||
else
|
||||
} else
|
||||
{
|
||||
platform_enumerate_directories_end(directoryState.Handle);
|
||||
Memory::Free(directoryState.Directory);
|
||||
|
@ -128,8 +160,7 @@ bool FileEnumerator::Next()
|
|||
Memory::Free(directoryState->Directory);
|
||||
_directoryStack.pop();
|
||||
}
|
||||
}
|
||||
else
|
||||
} else
|
||||
{
|
||||
Memory::Free(_directoryStack.top().Directory);
|
||||
_directoryStack.pop();
|
||||
|
@ -138,7 +169,8 @@ bool FileEnumerator::Next()
|
|||
}
|
||||
}
|
||||
|
||||
void FileEnumerator::CloseHandles()
|
||||
private:
|
||||
void CloseHandles()
|
||||
{
|
||||
if (_fileHandle != INVALID_HANDLE)
|
||||
{
|
||||
|
@ -156,3 +188,9 @@ void FileEnumerator::CloseHandles()
|
|||
_directoryStack.pop();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
IFileScanner * Path::ScanDirectory(const utf8 * pattern, bool recurse)
|
||||
{
|
||||
return new FileScanner(pattern, recurse);
|
||||
}
|
||||
|
|
|
@ -16,42 +16,22 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <stack>
|
||||
#include "../common.h"
|
||||
|
||||
struct file_info;
|
||||
class FileEnumerator final
|
||||
|
||||
interface IFileScanner
|
||||
{
|
||||
private:
|
||||
struct DirectoryState
|
||||
{
|
||||
utf8 * Directory;
|
||||
int Handle;
|
||||
virtual ~IFileScanner() = default;
|
||||
|
||||
virtual const file_info * GetFileInfo() const abstract;
|
||||
virtual const utf8 * GetPath() const abstract;
|
||||
|
||||
virtual void Reset() abstract;
|
||||
virtual bool Next() abstract;
|
||||
};
|
||||
|
||||
// Enumeration options
|
||||
utf8 * _rootPath;
|
||||
utf8 * _pattern;
|
||||
bool _recurse;
|
||||
|
||||
// Enumeration state
|
||||
int _fileHandle;
|
||||
std::stack<DirectoryState> _directoryStack;
|
||||
|
||||
// Current enumeration
|
||||
file_info * _fileInfo;
|
||||
utf8 * _path;
|
||||
|
||||
public:
|
||||
FileEnumerator(const utf8 * pattern, bool recurse);
|
||||
~FileEnumerator();
|
||||
|
||||
const file_info * GetFileInfo() const { return _fileInfo; }
|
||||
const utf8 * GetPath() const { return _path; }
|
||||
|
||||
void Reset();
|
||||
bool Next();
|
||||
|
||||
private:
|
||||
void CloseHandles();
|
||||
};
|
||||
namespace Path
|
||||
{
|
||||
IFileScanner * ScanDirectory(const utf8 * pattern, bool recurse);
|
||||
}
|
||||
|
|
|
@ -240,11 +240,11 @@ private:
|
|||
String::Set(pattern, sizeof(pattern), directory);
|
||||
Path::Append(pattern, sizeof(pattern), "*.dat");
|
||||
|
||||
auto fileEnumerator = FileEnumerator(pattern, true);
|
||||
while (fileEnumerator.Next())
|
||||
IFileScanner * scanner = Path::ScanDirectory(pattern, true);
|
||||
while (scanner->Next())
|
||||
{
|
||||
const file_info * enumFileInfo = fileEnumerator.GetFileInfo();
|
||||
const utf8 * enumPath = fileEnumerator.GetPath();
|
||||
const file_info * enumFileInfo = scanner->GetFileInfo();
|
||||
const utf8 * enumPath = scanner->GetPath();
|
||||
|
||||
result->TotalFiles++;
|
||||
result->TotalFileSize += enumFileInfo->size;
|
||||
|
@ -254,6 +254,7 @@ private:
|
|||
result->FileDateModifiedChecksum = ror32(result->FileDateModifiedChecksum, 5);
|
||||
result->PathChecksum += GetPathChecksum(enumPath);
|
||||
}
|
||||
delete scanner;
|
||||
}
|
||||
|
||||
void Construct()
|
||||
|
@ -282,12 +283,13 @@ private:
|
|||
String::Set(pattern, sizeof(pattern), directory);
|
||||
Path::Append(pattern, sizeof(pattern), "*.dat");
|
||||
|
||||
auto fileEnumerator = FileEnumerator(pattern, true);
|
||||
while (fileEnumerator.Next())
|
||||
IFileScanner * scanner = Path::ScanDirectory(pattern, true);
|
||||
while (scanner->Next())
|
||||
{
|
||||
const utf8 * enumPath = fileEnumerator.GetPath();
|
||||
const utf8 * enumPath = scanner->GetPath();
|
||||
ScanObject(enumPath);
|
||||
}
|
||||
delete scanner;
|
||||
}
|
||||
|
||||
void ScanObject(const utf8 * path)
|
||||
|
|
|
@ -202,12 +202,13 @@ private:
|
|||
String::Set(pattern, sizeof(pattern), directory);
|
||||
Path::Append(pattern, sizeof(pattern), "*.td6");
|
||||
|
||||
auto fileEnumerator = FileEnumerator(pattern, true);
|
||||
while (fileEnumerator.Next())
|
||||
IFileScanner * scanner = Path::ScanDirectory(pattern, true);
|
||||
while (scanner->Next())
|
||||
{
|
||||
const utf8 * path = fileEnumerator.GetPath();
|
||||
const utf8 * path = scanner->GetPath();
|
||||
AddTrack(path, flags);
|
||||
}
|
||||
delete scanner;
|
||||
}
|
||||
|
||||
void AddTrack(const utf8 * path, uint32 flags = 0)
|
||||
|
|
Loading…
Reference in a new issue