mirror of https://github.com/OpenRCT2/OpenRCT2.git
Merge pull request #4995 from IntelOrca/refactor/track-design-repository-stdstring
Refactor track design repository to use std::string
This commit is contained in:
commit
44d9464d6b
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#endif
|
||||
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "FileScanner.h"
|
||||
#include "Memory.hpp"
|
||||
|
@ -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
|
||||
|
@ -75,9 +76,8 @@ private:
|
|||
};
|
||||
|
||||
// Options
|
||||
utf8 * _rootPath;
|
||||
utf8 * * _patterns;
|
||||
size_t _numPatterns;
|
||||
std::string _rootPath;
|
||||
std::vector<std::string> _patterns;
|
||||
bool _recurse;
|
||||
|
||||
// State
|
||||
|
@ -89,12 +89,11 @@ private:
|
|||
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>();
|
||||
|
@ -104,12 +103,6 @@ public:
|
|||
|
||||
~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);
|
||||
}
|
||||
|
@ -127,7 +120,7 @@ public:
|
|||
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
|
||||
|
@ -156,7 +149,7 @@ public:
|
|||
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());
|
||||
|
@ -164,7 +157,7 @@ public:
|
|||
|
||||
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());
|
||||
|
@ -180,20 +173,20 @@ public:
|
|||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -201,11 +194,11 @@ private:
|
|||
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
|
||||
|
@ -216,9 +209,7 @@ private:
|
|||
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;
|
||||
|
@ -227,12 +218,12 @@ private:
|
|||
}
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
|
@ -241,21 +232,16 @@ protected:
|
|||
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);
|
||||
|
@ -274,7 +260,6 @@ protected:
|
|||
FindClose(hFile);
|
||||
}
|
||||
|
||||
Memory::Free(pattern);
|
||||
Memory::Free(wPattern);
|
||||
}
|
||||
|
||||
|
@ -289,11 +274,11 @@ private:
|
|||
|
||||
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;
|
||||
}
|
||||
|
@ -308,16 +293,16 @@ private:
|
|||
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++)
|
||||
|
@ -326,7 +311,7 @@ protected:
|
|||
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]);
|
||||
|
@ -347,11 +332,11 @@ private:
|
|||
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;
|
||||
|
@ -375,7 +360,7 @@ private:
|
|||
|
||||
#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);
|
||||
|
@ -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())
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "../common.h"
|
||||
|
||||
struct FileInfo
|
||||
|
@ -54,7 +55,7 @@ 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
|
||||
|
@ -62,5 +63,5 @@ namespace Path
|
|||
* @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);
|
||||
}
|
||||
|
|
|
@ -34,6 +34,19 @@ namespace Path
|
|||
return safe_strcat_path(buffer, src, bufferSize);
|
||||
}
|
||||
|
||||
std::string Combine(const std::string &a, const std::string &b)
|
||||
{
|
||||
utf8 buffer[MAX_PATH];
|
||||
String::Set(buffer, sizeof(buffer), a.c_str());
|
||||
Path::Append(buffer, sizeof(buffer), b.c_str());
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
std::string GetDirectory(const std::string &path)
|
||||
{
|
||||
return GetDirectory(path.c_str());
|
||||
}
|
||||
|
||||
utf8 * GetDirectory(const utf8 * path)
|
||||
{
|
||||
size_t maxSize = String::SizeOf(path) + 1;
|
||||
|
@ -58,6 +71,11 @@ namespace Path
|
|||
return buffer;
|
||||
}
|
||||
|
||||
std::string GetFileName(const std::string &path)
|
||||
{
|
||||
return GetFileName(path.c_str());
|
||||
}
|
||||
|
||||
const utf8 * GetFileName(const utf8 * path)
|
||||
{
|
||||
const utf8 * lastPathSeperator = nullptr;
|
||||
|
@ -81,6 +99,14 @@ namespace Path
|
|||
lastPathSeperator + 1;
|
||||
}
|
||||
|
||||
std::string GetFileNameWithoutExtension(const std::string &path)
|
||||
{
|
||||
utf8 * cstr = GetFileNameWithoutExtension(path.c_str());
|
||||
std::string result = String::ToStd(cstr);
|
||||
Memory::Free(cstr);
|
||||
return result;
|
||||
}
|
||||
|
||||
utf8 * GetFileNameWithoutExtension(const utf8 * path)
|
||||
{
|
||||
size_t maxSize = String::SizeOf(path) + 1;
|
||||
|
@ -116,6 +142,11 @@ namespace Path
|
|||
return buffer;
|
||||
}
|
||||
|
||||
const std::string GetExtension(const std::string &path)
|
||||
{
|
||||
return GetExtension(path.c_str());
|
||||
}
|
||||
|
||||
const utf8 * GetExtension(const utf8 * path)
|
||||
{
|
||||
const utf8 * lastDot = nullptr;
|
||||
|
@ -171,6 +202,11 @@ namespace Path
|
|||
#endif
|
||||
}
|
||||
|
||||
bool Equals(const std::string &a, const std::string &b)
|
||||
{
|
||||
return String::Equals(a.c_str(), b.c_str());
|
||||
}
|
||||
|
||||
bool Equals(const utf8 * a, const utf8 * b)
|
||||
{
|
||||
bool ignoreCase = false;
|
||||
|
|
|
@ -16,20 +16,24 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "../common.h"
|
||||
}
|
||||
#include <string>
|
||||
#include "../common.h"
|
||||
|
||||
namespace Path
|
||||
{
|
||||
utf8 * Append(utf8 * buffer, size_t bufferSize, const utf8 * src);
|
||||
std::string Combine(const std::string &a, const std::string &b);
|
||||
std::string GetDirectory(const std::string &path);
|
||||
utf8 * GetDirectory(const utf8 * path);
|
||||
utf8 * GetDirectory(utf8 * buffer, size_t bufferSize, const utf8 * path);
|
||||
std::string GetFileName(const std::string &path);
|
||||
const utf8 * GetFileName(const utf8 * path);
|
||||
std::string GetFileNameWithoutExtension(const std::string &path);
|
||||
utf8 * GetFileNameWithoutExtension(const utf8 * path);
|
||||
utf8 * GetFileNameWithoutExtension(utf8 * buffer, size_t bufferSize, const utf8 * path);
|
||||
const std::string GetExtension(const std::string &path);
|
||||
const utf8 * GetExtension(const utf8 * path);
|
||||
utf8 * GetAbsolute(utf8 * buffer, size_t bufferSize, const utf8 * relativePath);
|
||||
bool Equals(const std::string &a, const std::string &b);
|
||||
bool Equals(const utf8 * a, const utf8 * b);
|
||||
}
|
||||
|
|
|
@ -50,6 +50,31 @@ namespace String
|
|||
return str == nullptr || str[0] == '\0';
|
||||
}
|
||||
|
||||
sint32 Compare(const std::string &a, const std::string &b, bool ignoreCase)
|
||||
{
|
||||
return Compare(a.c_str(), b.c_str(), ignoreCase);
|
||||
}
|
||||
|
||||
sint32 Compare(const utf8 * a, const utf8 * b, bool ignoreCase)
|
||||
{
|
||||
if (a == b) return true;
|
||||
if (a == nullptr || b == nullptr) return false;
|
||||
|
||||
if (ignoreCase)
|
||||
{
|
||||
return _stricmp(a, b);
|
||||
}
|
||||
else
|
||||
{
|
||||
return strcmp(a, b);
|
||||
}
|
||||
}
|
||||
|
||||
bool Equals(const std::string &a, const std::string &b, bool ignoreCase)
|
||||
{
|
||||
return Equals(a.c_str(), b.c_str(), ignoreCase);
|
||||
}
|
||||
|
||||
bool Equals(const utf8 * a, const utf8 * b, bool ignoreCase)
|
||||
{
|
||||
if (a == b) return true;
|
||||
|
@ -247,6 +272,11 @@ namespace String
|
|||
return buffer;
|
||||
}
|
||||
|
||||
utf8 * Duplicate(const std::string &src)
|
||||
{
|
||||
return String::Duplicate(src.c_str());
|
||||
}
|
||||
|
||||
utf8 * Duplicate(const utf8 * src)
|
||||
{
|
||||
utf8 * result = nullptr;
|
||||
|
|
|
@ -27,6 +27,9 @@ namespace String
|
|||
std::string StdFormat(const utf8 * format, ...);
|
||||
|
||||
bool IsNullOrEmpty(const utf8 * str);
|
||||
sint32 Compare(const std::string &a, const std::string &b, bool ignoreCase = false);
|
||||
sint32 Compare(const utf8 * a, const utf8 * b, bool ignoreCase = false);
|
||||
bool Equals(const std::string &a, const std::string &b, bool ignoreCase = false);
|
||||
bool Equals(const utf8 * a, const utf8 * b, bool ignoreCase = false);
|
||||
bool StartsWith(const utf8 * str, const utf8 * match, bool ignoreCase = false);
|
||||
size_t LastIndexOf(const utf8 * str, utf8 match);
|
||||
|
@ -48,6 +51,7 @@ namespace String
|
|||
utf8 * Format(const utf8 * format, ...);
|
||||
utf8 * Format_VA(const utf8 * format, va_list args);
|
||||
utf8 * AppendFormat(utf8 * buffer, size_t bufferSize, const utf8 * format, ...);
|
||||
utf8 * Duplicate(const std::string &src);
|
||||
utf8 * Duplicate(const utf8 * src);
|
||||
|
||||
/**
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "../core/Collections.hpp"
|
||||
#include "../core/Console.hpp"
|
||||
#include "../core/File.h"
|
||||
#include "../core/FileScanner.h"
|
||||
|
@ -65,6 +66,8 @@ enum TRACK_REPO_ITEM_FLAGS
|
|||
class TrackDesignRepository : public ITrackDesignRepository
|
||||
{
|
||||
private:
|
||||
static constexpr const utf8 * TD_FILE_PATTERN = "*.td4;*.td6";
|
||||
|
||||
IPlatformEnvironment * _env;
|
||||
|
||||
std::vector<TrackRepositoryItem> _items;
|
||||
|
@ -87,13 +90,13 @@ public:
|
|||
return _items.size();
|
||||
}
|
||||
|
||||
size_t GetCountForObjectEntry(uint8 rideType, const utf8 * entry) const override
|
||||
size_t GetCountForObjectEntry(uint8 rideType, const std::string &entry) const override
|
||||
{
|
||||
size_t count = 0;
|
||||
for (const auto item : _items)
|
||||
{
|
||||
if (item.RideType == rideType &&
|
||||
(entry == nullptr || String::Equals(item.ObjectEntry.c_str(), entry, true)))
|
||||
(entry.empty() || String::Equals(item.ObjectEntry, entry, true)))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
@ -101,26 +104,22 @@ public:
|
|||
return count;
|
||||
}
|
||||
|
||||
size_t GetItemsForObjectEntry(track_design_file_ref * * outRefs, uint8 rideType, const utf8 * entry) const override
|
||||
size_t GetItemsForObjectEntry(track_design_file_ref * * outRefs, uint8 rideType, const std::string &entry) const override
|
||||
{
|
||||
std::vector<track_design_file_ref> refs;
|
||||
for (const auto item : _items)
|
||||
{
|
||||
if (item.RideType == rideType &&
|
||||
(entry == nullptr || String::Equals(item.ObjectEntry.c_str(), entry, true)))
|
||||
(entry.empty() || String::Equals(item.ObjectEntry, entry, true)))
|
||||
{
|
||||
track_design_file_ref ref;
|
||||
ref.name = GetNameFromTrackPath(item.Path.c_str());
|
||||
ref.path = String::Duplicate(item.Path.c_str());
|
||||
ref.name = String::Duplicate(GetNameFromTrackPath(item.Path));
|
||||
ref.path = String::Duplicate(item.Path);
|
||||
refs.push_back(ref);
|
||||
}
|
||||
}
|
||||
|
||||
*outRefs = nullptr;
|
||||
if (refs.size() != 0)
|
||||
{
|
||||
*outRefs = Memory::DuplicateArray(refs.data(), refs.size());
|
||||
}
|
||||
*outRefs = Collections::ToArray(refs);
|
||||
return refs.size();
|
||||
}
|
||||
|
||||
|
@ -143,7 +142,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
bool Delete(const utf8 * path) override
|
||||
bool Delete(const std::string &path) override
|
||||
{
|
||||
bool result = false;
|
||||
size_t index = GetTrackIndex(path);
|
||||
|
@ -162,57 +161,41 @@ public:
|
|||
return result;
|
||||
}
|
||||
|
||||
const utf8 * Rename(const utf8 * path, const utf8 * newName) override
|
||||
std::string Rename(const std::string &path, const std::string &newName) override
|
||||
{
|
||||
const utf8 * result = nullptr;
|
||||
std::string result;
|
||||
size_t index = GetTrackIndex(path);
|
||||
if (index != SIZE_MAX)
|
||||
{
|
||||
TrackRepositoryItem * item = &_items[index];
|
||||
if (!(item->Flags & TRIF_READ_ONLY))
|
||||
{
|
||||
utf8 newPath[MAX_PATH];
|
||||
Path::GetDirectory(newPath, sizeof(newPath), path);
|
||||
Path::Append(newPath, sizeof(newPath), newName);
|
||||
String::Append(newPath, sizeof(newPath), Path::GetExtension(path));
|
||||
|
||||
std::string directory = Path::GetDirectory(path);
|
||||
std::string newPath = Path::Combine(directory, newName + Path::GetExtension(path));
|
||||
if (File::Move(path, newPath))
|
||||
{
|
||||
item->Name = std::string(newName);
|
||||
item->Path = std::string(newPath);
|
||||
item->Name = newName;
|
||||
item->Path = newPath;
|
||||
SortItems();
|
||||
|
||||
item = GetTrackItem(newPath);
|
||||
if (item != nullptr)
|
||||
{
|
||||
result = item->Path.c_str();
|
||||
}
|
||||
result = newPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const utf8 * Install(const utf8 * path) override
|
||||
std::string Install(const std::string &path) override
|
||||
{
|
||||
const utf8 * result = nullptr;
|
||||
const utf8 * fileName = Path::GetFileName(path);
|
||||
std::string result;
|
||||
std::string fileName = Path::GetFileName(path);
|
||||
std::string installDir = _env->GetDirectoryPath(DIRBASE::USER, DIRID::TRACK);
|
||||
|
||||
utf8 newPath[MAX_PATH];
|
||||
String::Set(newPath, sizeof(newPath), installDir.c_str());
|
||||
Path::Append(newPath, sizeof(newPath), fileName);
|
||||
|
||||
std::string newPath = Path::Combine(installDir, fileName);
|
||||
if (File::Copy(path, newPath, false))
|
||||
{
|
||||
AddTrack(path);
|
||||
SortItems();
|
||||
|
||||
const TrackRepositoryItem * item = GetTrackItem(path);
|
||||
if (item != nullptr)
|
||||
{
|
||||
result = item->Path.c_str();
|
||||
}
|
||||
result = path;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -220,18 +203,13 @@ public:
|
|||
private:
|
||||
void Query(const std::string &directory)
|
||||
{
|
||||
utf8 pattern[MAX_PATH];
|
||||
String::Set(pattern, sizeof(pattern), directory.c_str());
|
||||
Path::Append(pattern, sizeof(pattern), "*.td4;*.td6");
|
||||
std::string pattern = Path::Combine(directory, TD_FILE_PATTERN);
|
||||
Path::QueryDirectory(&_directoryQueryResult, pattern);
|
||||
}
|
||||
|
||||
void Scan(const std::string &directory, uint32 flags = 0)
|
||||
{
|
||||
utf8 pattern[MAX_PATH];
|
||||
String::Set(pattern, sizeof(pattern), directory.c_str());
|
||||
Path::Append(pattern, sizeof(pattern), "*.td4;*.td6");
|
||||
|
||||
std::string pattern = Path::Combine(directory, TD_FILE_PATTERN);
|
||||
IFileScanner * scanner = Path::ScanDirectory(pattern, true);
|
||||
while (scanner->Next())
|
||||
{
|
||||
|
@ -241,14 +219,14 @@ private:
|
|||
delete scanner;
|
||||
}
|
||||
|
||||
void AddTrack(const utf8 * path, uint32 flags = 0)
|
||||
void AddTrack(const std::string path, uint32 flags = 0)
|
||||
{
|
||||
rct_track_td6 * td6 = track_design_open(path);
|
||||
rct_track_td6 * td6 = track_design_open(path.c_str());
|
||||
if (td6 != nullptr)
|
||||
{
|
||||
TrackRepositoryItem item;
|
||||
item.Name = std::string(GetNameFromTrackPath(path));
|
||||
item.Path = std::string(path);
|
||||
item.Name = GetNameFromTrackPath(path);
|
||||
item.Path = path;
|
||||
item.RideType = td6->type;
|
||||
item.ObjectEntry = std::string(td6->vehicle_object.name, 8);
|
||||
item.Flags = flags;
|
||||
|
@ -266,10 +244,7 @@ private:
|
|||
{
|
||||
return a.RideType < b.RideType;
|
||||
}
|
||||
|
||||
const utf8 * nameA = a.Name.c_str();
|
||||
const utf8 * nameB = b.Name.c_str();
|
||||
return _stricmp(nameA, nameB) < 0;
|
||||
return String::Compare(a.Name, b.Name) < 0;
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -345,11 +320,11 @@ private:
|
|||
}
|
||||
}
|
||||
|
||||
size_t GetTrackIndex(const utf8 * path) const
|
||||
size_t GetTrackIndex(const std::string &path) const
|
||||
{
|
||||
for (size_t i = 0; i < _items.size(); i++)
|
||||
{
|
||||
if (Path::Equals(_items[i].Path.c_str(), path))
|
||||
if (Path::Equals(_items[i].Path, path))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
|
@ -357,7 +332,7 @@ private:
|
|||
return SIZE_MAX;
|
||||
}
|
||||
|
||||
TrackRepositoryItem * GetTrackItem(const utf8 * path)
|
||||
TrackRepositoryItem * GetTrackItem(const std::string &path)
|
||||
{
|
||||
TrackRepositoryItem * result = nullptr;
|
||||
size_t index = GetTrackIndex(path);
|
||||
|
@ -369,12 +344,9 @@ private:
|
|||
}
|
||||
|
||||
public:
|
||||
static utf8 * GetNameFromTrackPath(const utf8 * path)
|
||||
static std::string GetNameFromTrackPath(const std::string &path)
|
||||
{
|
||||
utf8 * name = Memory::Allocate<utf8>(MAX_PATH);
|
||||
Path::GetFileNameWithoutExtension(name, MAX_PATH, path);
|
||||
name = Memory::ReallocateArray(name, String::SizeOf(name) + 1);
|
||||
return name;
|
||||
return Path::GetFileNameWithoutExtension(path);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -402,13 +374,13 @@ extern "C"
|
|||
size_t track_repository_get_count_for_ride(uint8 rideType, const utf8 * entry)
|
||||
{
|
||||
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
||||
return repo->GetCountForObjectEntry(rideType, entry);
|
||||
return repo->GetCountForObjectEntry(rideType, String::ToStd(entry));
|
||||
}
|
||||
|
||||
size_t track_repository_get_items_for_ride(track_design_file_ref * * outRefs, uint8 rideType, const utf8 * entry)
|
||||
{
|
||||
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
||||
return repo->GetItemsForObjectEntry(outRefs, rideType, entry);
|
||||
return repo->GetItemsForObjectEntry(outRefs, rideType, String::ToStd(entry));
|
||||
}
|
||||
|
||||
bool track_repository_delete(const utf8 * path)
|
||||
|
@ -417,20 +389,22 @@ extern "C"
|
|||
return repo->Delete(path);
|
||||
}
|
||||
|
||||
const utf8 * track_repository_rename(const utf8 * path, const utf8 * newName)
|
||||
bool track_repository_rename(const utf8 * path, const utf8 * newName)
|
||||
{
|
||||
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
||||
return repo->Rename(path, newName);
|
||||
std::string newPath = repo->Rename(path, newName);
|
||||
return !newPath.empty();
|
||||
}
|
||||
|
||||
const utf8 * track_repository_install(const utf8 * srcPath)
|
||||
bool track_repository_install(const utf8 * srcPath)
|
||||
{
|
||||
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
||||
return repo->Install(srcPath);
|
||||
std::string newPath = repo->Install(srcPath);
|
||||
return !newPath.empty();
|
||||
}
|
||||
|
||||
utf8 * track_repository_get_name_from_path(const utf8 * path)
|
||||
{
|
||||
return TrackDesignRepository::GetNameFromTrackPath(path);
|
||||
return String::Duplicate(TrackDesignRepository::GetNameFromTrackPath(path));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,6 +26,8 @@ typedef struct track_design_file_ref
|
|||
|
||||
#ifdef __cplusplus
|
||||
|
||||
#include <string>
|
||||
|
||||
interface IPlatformEnvironment;
|
||||
|
||||
interface ITrackDesignRepository
|
||||
|
@ -33,15 +35,15 @@ interface ITrackDesignRepository
|
|||
virtual ~ITrackDesignRepository() = default;
|
||||
|
||||
virtual size_t GetCount() const abstract;
|
||||
virtual size_t GetCountForObjectEntry(uint8 rideType, const utf8 * entry) const abstract;
|
||||
virtual size_t GetCountForObjectEntry(uint8 rideType, const std::string &entry) const abstract;
|
||||
virtual size_t GetItemsForObjectEntry(track_design_file_ref * * outRefs,
|
||||
uint8 rideType,
|
||||
const utf8 * entry) const abstract;
|
||||
const std::string &entry) const abstract;
|
||||
|
||||
virtual void Scan() abstract;
|
||||
virtual bool Delete(const utf8 * path) abstract;
|
||||
virtual const utf8 * Rename(const utf8 * path, const utf8 * newName) abstract;
|
||||
virtual const utf8 * Install(const utf8 * path) abstract;
|
||||
virtual bool Delete(const std::string &path) abstract;
|
||||
virtual std::string Rename(const std::string &path, const std::string &newName) abstract;
|
||||
virtual std::string Install(const std::string &path) abstract;
|
||||
};
|
||||
|
||||
ITrackDesignRepository * CreateTrackDesignRepository(IPlatformEnvironment * env);
|
||||
|
@ -58,8 +60,8 @@ extern "C"
|
|||
size_t track_repository_get_items_for_ride(track_design_file_ref * * outRefs, uint8 rideType, const utf8 * entry);
|
||||
utf8 * track_repository_get_name_from_path(const utf8 *path);
|
||||
bool track_repository_delete(const utf8 *path);
|
||||
const utf8 * track_repository_rename(const utf8 *path, const utf8 *newName);
|
||||
const utf8 * track_repository_install(const utf8 *srcPath);
|
||||
bool track_repository_rename(const utf8 *path, const utf8 *newName);
|
||||
bool track_repository_install(const utf8 *srcPath);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue