Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #4995 from IntelOrca/refactor/track-design-reposit…
Browse files Browse the repository at this point in the history
…ory-stdstring

Refactor track design repository to use std::string
  • Loading branch information
IntelOrca committed Jan 6, 2017
2 parents e7a7704 + 60cb84f commit 44d9464
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 153 deletions.
14 changes: 7 additions & 7 deletions src/openrct2/core/File.cpp
Expand Up @@ -26,22 +26,22 @@ extern "C"

namespace File
{
bool Copy(const utf8 * srcPath, const utf8 * dstPath, bool overwrite)
bool Copy(const std::string &srcPath, const std::string &dstPath, bool overwrite)
{
return platform_file_copy(srcPath, dstPath, overwrite);
return platform_file_copy(srcPath.c_str(), dstPath.c_str(), overwrite);
}

bool Delete(const utf8 * path)
bool Delete(const std::string &path)
{
return platform_file_delete(path);
return platform_file_delete(path.c_str());
}

bool Move(const utf8 * srcPath, const utf8 * dstPath)
bool Move(const std::string &srcPath, const std::string &dstPath)
{
return platform_file_move(srcPath, dstPath);
return platform_file_move(srcPath.c_str(), dstPath.c_str());
}

void * ReadAllBytes(const utf8 * path, size_t * length)
void * ReadAllBytes(const std::string &path, size_t * length)
{
void * result = nullptr;

Expand Down
9 changes: 5 additions & 4 deletions src/openrct2/core/File.h
Expand Up @@ -16,12 +16,13 @@

#pragma once

#include <string>
#include "../common.h"

namespace File
{
bool Copy(const utf8 * srcPath, const utf8 * dstPath, bool overwrite);
bool Delete(const utf8 * path);
bool Move(const utf8 * srcPath, const utf8 * dstPath);
void * ReadAllBytes(const utf8 * path, size_t * length);
bool Copy(const std::string &srcPath, const std::string &dstPath, bool overwrite);
bool Delete(const std::string &path);
bool Move(const std::string &srcPath, const std::string &dstPath);
void * ReadAllBytes(const std::string &path, size_t * length);
}
93 changes: 39 additions & 54 deletions src/openrct2/core/FileScanner.cpp
Expand Up @@ -34,6 +34,7 @@
#endif

#include <stack>
#include <string>
#include <vector>
#include "FileScanner.h"
#include "Memory.hpp"
Expand All @@ -45,10 +46,10 @@ extern "C"
#include "../platform/platform.h"
}

enum DIRECTORY_CHILD_TYPE
enum class DIRECTORY_CHILD_TYPE
{
DCT_DIRECTORY,
DCT_FILE,
DIRECTORY,
FILE,
};

struct DirectoryChild
Expand All @@ -75,10 +76,9 @@ class FileScannerBase : public IFileScanner
};

// Options
utf8 * _rootPath;
utf8 * * _patterns;
size_t _numPatterns;
bool _recurse;
std::string _rootPath;
std::vector<std::string> _patterns;
bool _recurse;

// State
bool _started;
Expand All @@ -89,12 +89,11 @@ class FileScannerBase : public IFileScanner
utf8 * _currentPath;

public:
FileScannerBase(const utf8 * pattern, bool recurse)
FileScannerBase(const std::string &pattern, bool recurse)
{
_rootPath = Memory::Allocate<utf8>(MAX_PATH);
Path::GetDirectory(_rootPath, MAX_PATH, pattern);
_rootPath = Path::GetDirectory(pattern);
_recurse = recurse;
_numPatterns = GetPatterns(&_patterns, Path::GetFileName(pattern));
_patterns = GetPatterns(Path::GetFileName(pattern));

_currentPath = Memory::Allocate<utf8>(MAX_PATH);
_currentFileInfo = Memory::Allocate<FileInfo>();
Expand All @@ -104,12 +103,6 @@ class FileScannerBase : public IFileScanner

~FileScannerBase() override
{
Memory::Free(_rootPath);
for (size_t i = 0; i < _numPatterns; i++)
{
Memory::Free(_patterns[i]);
}
Memory::Free(_patterns);
Memory::Free(_currentPath);
Memory::Free(_currentFileInfo);
}
Expand All @@ -127,7 +120,7 @@ class FileScannerBase : public IFileScanner
const utf8 * GetPathRelative() const override
{
// +1 to remove the path separator
return _currentPath + String::SizeOf(_rootPath) + 1;
return _currentPath + _rootPath.size() + 1;
}

void Reset() override
Expand Down Expand Up @@ -156,15 +149,15 @@ class FileScannerBase : public IFileScanner
else
{
const DirectoryChild * child = &state->Listing[state->Index];
if (child->Type == DCT_DIRECTORY)
if (child->Type == DIRECTORY_CHILD_TYPE::DIRECTORY)
{
utf8 childPath[MAX_PATH];
String::Set(childPath, sizeof(childPath), state->Path.c_str());
Path::Append(childPath, sizeof(childPath), child->Name.c_str());

PushState(childPath);
}
else if (PatternMatch(child->Name.c_str()))
else if (PatternMatch(child->Name))
{
String::Set(_currentPath, MAX_PATH, state->Path.c_str());
Path::Append(_currentPath, MAX_PATH, child->Name.c_str());
Expand All @@ -180,32 +173,32 @@ class FileScannerBase : public IFileScanner
}

private:
void PushState(const utf8 * directory)
void PushState(const std::string &directory)
{
DirectoryState newState;
newState.Path = std::string(directory);
newState.Path = directory;
newState.Index = -1;
GetDirectoryChildren(newState.Listing, directory);
_directoryStack.push(newState);
}

bool PatternMatch(const utf8 * fileName)
bool PatternMatch(const std::string &fileName)
{
for (size_t i = 0; i < _numPatterns; i++)
for (const auto &pattern : _patterns)
{
if (MatchWildcard(fileName, _patterns[i]))
if (MatchWildcard(fileName.c_str(), pattern.c_str()))
{
return true;
}
}
return false;
}

static size_t GetPatterns(utf8 * * * outPatterns, const utf8 * delimitedPatterns)
static std::vector<std::string> GetPatterns(const std::string &delimitedPatterns)
{
std::vector<utf8 *> patterns;
std::vector<std::string> patterns;

const utf8 * start = delimitedPatterns;
const utf8 * start = delimitedPatterns.c_str();
const utf8 * ch = start;
utf8 c;
do
Expand All @@ -216,9 +209,7 @@ class FileScannerBase : public IFileScanner
size_t length = (size_t)(ch - start);
if (length > 0)
{
utf8 * newPattern = Memory::Allocate<utf8>(length + 1);
Memory::Copy(newPattern, start, length);
newPattern[length] = '\0';
std::string newPattern = std::string(start, length);
patterns.push_back(newPattern);
}
start = ch + 1;
Expand All @@ -227,12 +218,12 @@ class FileScannerBase : public IFileScanner
}
while (c != '\0');

*outPatterns = Memory::DuplicateArray(patterns.data(), patterns.size());
return patterns.size();
patterns.shrink_to_fit();
return patterns;
}

protected:
virtual void GetDirectoryChildren(std::vector<DirectoryChild> &children, const utf8 * path) abstract;
virtual void GetDirectoryChildren(std::vector<DirectoryChild> &children, const std::string &path) abstract;

};

Expand All @@ -241,21 +232,16 @@ class FileScannerBase : public IFileScanner
class FileScannerWindows final : public FileScannerBase
{
public:
FileScannerWindows(const utf8 * pattern, bool recurse)
FileScannerWindows(const std::string &pattern, bool recurse)
: FileScannerBase(pattern, recurse)
{
}

protected:
void GetDirectoryChildren(std::vector<DirectoryChild> &children, const utf8 * path) override
void GetDirectoryChildren(std::vector<DirectoryChild> &children, const std::string &path) override
{
size_t pathLength = String::SizeOf(path);
utf8 * pattern = Memory::Duplicate(path, pathLength + 3);
pattern[pathLength + 0] = '\\';
pattern[pathLength + 1] = '*';
pattern[pathLength + 2] = '\0';

wchar_t * wPattern = utf8_to_widechar(pattern);
std::string pattern = path + "\\*";
wchar_t * wPattern = utf8_to_widechar(pattern.c_str());

WIN32_FIND_DATAW findData;
HANDLE hFile = FindFirstFileW(wPattern, &findData);
Expand All @@ -274,7 +260,6 @@ class FileScannerWindows final : public FileScannerBase
FindClose(hFile);
}

Memory::Free(pattern);
Memory::Free(wPattern);
}

Expand All @@ -289,11 +274,11 @@ class FileScannerWindows final : public FileScannerBase

if (child->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
result.Type = DCT_DIRECTORY;
result.Type = DIRECTORY_CHILD_TYPE::DIRECTORY;
}
else
{
result.Type = DCT_FILE;
result.Type = DIRECTORY_CHILD_TYPE::FILE;
result.Size = ((uint64)child->nFileSizeHigh << 32ULL) | (uint64)child->nFileSizeLow;
result.LastModified = ((uint64)child->ftLastWriteTime.dwHighDateTime << 32ULL) | (uint64)child->ftLastWriteTime.dwLowDateTime;
}
Expand All @@ -308,16 +293,16 @@ class FileScannerWindows final : public FileScannerBase
class FileScannerUnix final : public FileScannerBase
{
public:
FileScannerUnix(const utf8 * pattern, bool recurse)
FileScannerUnix(const std::string &pattern, bool recurse)
: FileScannerBase(pattern, recurse)
{
}

protected:
void GetDirectoryChildren(std::vector<DirectoryChild> &children, const utf8 * path) override
void GetDirectoryChildren(std::vector<DirectoryChild> &children, const std::string &path) override
{
struct dirent * * namelist;
int count = scandir(path, &namelist, FilterFunc, alphasort);
int count = scandir(path.c_str(), &namelist, FilterFunc, alphasort);
if (count > 0)
{
for (int i = 0; i < count; i++)
Expand All @@ -326,7 +311,7 @@ class FileScannerUnix final : public FileScannerBase
if (!String::Equals(node->d_name, ".") &&
!String::Equals(node->d_name, ".."))
{
DirectoryChild child = CreateChild(path, node);
DirectoryChild child = CreateChild(path.c_str(), node);
children.push_back(child);
}
free(namelist[i]);
Expand All @@ -347,11 +332,11 @@ class FileScannerUnix final : public FileScannerBase
result.Name = std::string(node->d_name);
if (node->d_type & DT_DIR)
{
result.Type = DCT_DIRECTORY;
result.Type = DIRECTORY_CHILD_TYPE::DIRECTORY;
}
else
{
result.Type = DCT_FILE;
result.Type = DIRECTORY_CHILD_TYPE::FILE;

// Get the full path of the file
size_t pathSize = String::SizeOf(directory) + 1 + String::SizeOf(node->d_name) + 1;
Expand All @@ -375,7 +360,7 @@ class FileScannerUnix final : public FileScannerBase

#endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))

IFileScanner * Path::ScanDirectory(const utf8 * pattern, bool recurse)
IFileScanner * Path::ScanDirectory(const std::string &pattern, bool recurse)
{
#ifdef __WINDOWS__
return new FileScannerWindows(pattern, recurse);
Expand All @@ -384,7 +369,7 @@ IFileScanner * Path::ScanDirectory(const utf8 * pattern, bool recurse)
#endif
}

void Path::QueryDirectory(QueryDirectoryResult * result, const utf8 * pattern)
void Path::QueryDirectory(QueryDirectoryResult * result, const std::string &pattern)
{
IFileScanner * scanner = Path::ScanDirectory(pattern, true);
while (scanner->Next())
Expand Down
5 changes: 3 additions & 2 deletions src/openrct2/core/FileScanner.h
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <string>
#include "../common.h"

struct FileInfo
Expand Down Expand Up @@ -54,13 +55,13 @@ namespace Path
* @param recurse Whether to scan sub directories or not.
* @returns A new FileScanner, this must be deleted when no longer needed.
*/
IFileScanner * ScanDirectory(const utf8 * pattern, bool recurse);
IFileScanner * ScanDirectory(const std::string &pattern, bool recurse);

/**
* Scans a directory and all sub directories
* @param result The query result to modify.
* @param pattern The path followed by a semi-colon delimited list of wildcard patterns.
* @returns An aggregated result of all scanned files.
*/
void QueryDirectory(QueryDirectoryResult * result, const utf8 * pattern);
void QueryDirectory(QueryDirectoryResult * result, const std::string &pattern);
}

0 comments on commit 44d9464

Please sign in to comment.