Files
OpenRCT2-Web/src/core/FileEnumerator.cpp
T

196 lines
6.0 KiB
C++
Raw Normal View History

2016-06-28 00:16:35 +01:00
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
2016-10-22 19:10:16 +01:00
#include <stack>
2016-06-28 00:16:35 +01:00
#include "FileEnumerator.h"
#include "Memory.hpp"
#include "Path.hpp"
#include "String.hpp"
extern "C"
{
#include "../platform/platform.h"
}
2016-10-22 19:10:16 +01:00
class FileScanner : public IFileScanner
2016-06-28 00:16:35 +01:00
{
2016-10-22 19:10:16 +01:00
private:
struct DirectoryState
2016-06-28 00:16:35 +01:00
{
2016-10-22 19:10:16 +01:00
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);
_pattern = String::Duplicate(Path::GetFileName(pattern));
_recurse = recurse;
_fileInfo = Memory::Allocate<file_info>();
Memory::Set(_fileInfo, 0, sizeof(file_info));
_path = Memory::Allocate<utf8>(MAX_PATH);
_fileHandle = INVALID_HANDLE;
Reset();
}
~FileScanner() override
{
CloseHandles();
Memory::Free(_path);
Memory::Free(_fileInfo);
Memory::Free(_pattern);
Memory::Free(_rootPath);
}
const file_info * GetFileInfo() const override
{
return _fileInfo;
}
const utf8 * GetPath() const override
{
return _path;
}
void Reset() override
{
CloseHandles();
DirectoryState directoryState;
directoryState.Directory = String::Duplicate(_rootPath);
directoryState.Handle = INVALID_HANDLE;
_directoryStack.push(directoryState);
}
bool Next() override
{
while (true)
2016-06-28 00:16:35 +01:00
{
2016-10-22 19:10:16 +01:00
while (_fileHandle == INVALID_HANDLE)
2016-06-28 00:16:35 +01:00
{
2016-10-22 19:10:16 +01:00
if (_directoryStack.size() == 0)
2016-06-28 00:16:35 +01:00
{
2016-10-22 19:10:16 +01:00
return false;
2016-06-28 00:16:35 +01:00
}
2016-10-22 19:10:16 +01:00
DirectoryState directoryState = _directoryStack.top();
if (directoryState.Handle == INVALID_HANDLE)
2016-06-28 00:16:35 +01:00
{
2016-10-22 19:10:16 +01:00
// Start enumerating files for this directory
utf8 pattern[MAX_PATH];
String::Set(pattern, sizeof(pattern), directoryState.Directory);
Path::Append(pattern, sizeof(pattern), _pattern);
_fileHandle = platform_enumerate_files_begin(pattern);
break;
} else
{
// Next directory
utf8 name[MAX_PATH];
if (platform_enumerate_directories_next(directoryState.Handle, name))
{
DirectoryState newDirectoryState;
newDirectoryState.Handle = INVALID_HANDLE;
newDirectoryState.Directory = Memory::Allocate<utf8>(MAX_PATH);
String::Set(newDirectoryState.Directory, MAX_PATH, directoryState.Directory);
Path::Append(newDirectoryState.Directory, MAX_PATH, name);
_directoryStack.push(newDirectoryState);
} else
{
platform_enumerate_directories_end(directoryState.Handle);
Memory::Free(directoryState.Directory);
_directoryStack.pop();
}
}
}
// Next file
if (_fileHandle != INVALID_HANDLE)
{
if (platform_enumerate_files_next(_fileHandle, _fileInfo))
{
String::Set(_path, MAX_PATH, _directoryStack.top().Directory);
Path::Append(_path, MAX_PATH, _fileInfo->path);
return true;
}
platform_enumerate_files_end(_fileHandle);
_fileHandle = INVALID_HANDLE;
}
if (_recurse)
{
// Start enumerating sub-directories
DirectoryState * directoryState = &_directoryStack.top();
directoryState->Handle = platform_enumerate_directories_begin(directoryState->Directory);
if (directoryState->Handle == INVALID_HANDLE)
{
Memory::Free(directoryState->Directory);
2016-06-28 00:16:35 +01:00
_directoryStack.pop();
}
2016-10-22 19:10:16 +01:00
} else
{
Memory::Free(_directoryStack.top().Directory);
_directoryStack.pop();
return false;
2016-06-28 00:16:35 +01:00
}
}
2016-10-22 19:10:16 +01:00
}
2016-06-28 00:16:35 +01:00
2016-10-22 19:10:16 +01:00
private:
void CloseHandles()
{
2016-06-28 22:30:00 +01:00
if (_fileHandle != INVALID_HANDLE)
2016-06-28 00:16:35 +01:00
{
2016-06-28 22:30:00 +01:00
platform_enumerate_files_end(_fileHandle);
_fileHandle = INVALID_HANDLE;
2016-06-28 00:16:35 +01:00
}
2016-10-22 19:10:16 +01:00
while (_directoryStack.size() > 0)
2016-06-28 00:16:35 +01:00
{
2016-10-22 19:10:16 +01:00
DirectoryState directoryState = _directoryStack.top();
if (directoryState.Handle != INVALID_HANDLE)
2016-06-28 00:16:35 +01:00
{
2016-10-22 19:10:16 +01:00
platform_enumerate_directories_end(directoryState.Handle);
2016-06-28 00:16:35 +01:00
}
2016-10-22 19:10:16 +01:00
Memory::Free(directoryState.Directory);
2016-06-28 00:16:35 +01:00
_directoryStack.pop();
}
}
2016-10-22 19:10:16 +01:00
};
2016-06-28 00:16:35 +01:00
2016-10-22 19:10:16 +01:00
IFileScanner * Path::ScanDirectory(const utf8 * pattern, bool recurse)
2016-06-28 00:16:35 +01:00
{
2016-10-22 19:10:16 +01:00
return new FileScanner(pattern, recurse);
2016-06-28 00:16:35 +01:00
}