From 63e7217838a6f795bc2e40a475f2a61100f89dad Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 12 Apr 2022 19:18:05 +0200 Subject: Removed prefix from data sources This to avoid any potential issues due to use of mutable state across several objects that is in the end expected to be the same. This fits well as a function parameter. --- StarsEx/DataLoader.cpp | 12 +++--------- StarsEx/DataSource.cpp | 43 ++++++++++++++++--------------------------- StarsEx/DataSource.h | 20 +++++++++----------- 3 files changed, 28 insertions(+), 47 deletions(-) (limited to 'StarsEx') diff --git a/StarsEx/DataLoader.cpp b/StarsEx/DataLoader.cpp index 95e8444..2ef84c3 100644 --- a/StarsEx/DataLoader.cpp +++ b/StarsEx/DataLoader.cpp @@ -224,9 +224,7 @@ DataLoader::FindFile(const char* name) // first check current directory: if (work_directory_source) { - work_directory_source->SetPrefix(datapath); - bool found = work_directory_source->Find(name); - work_directory_source->SetPrefix(); + bool found = work_directory_source->Find(datapath, name); if (found) return true; } @@ -335,9 +333,7 @@ void DataLoader::ListFileSystem(const char* filter, List& list, Text base_path, bool recurse) { if (work_directory_source) { - work_directory_source->SetPrefix(base_path); - work_directory_source->ListFiles(filter, list, recurse); - work_directory_source->SetPrefix(); + work_directory_source->ListFiles(base_path, filter, list, recurse); } } @@ -354,9 +350,7 @@ DataLoader::LoadBuffer(const char* name, BYTE*& buf, bool null_terminate, bool o strcat_s(filename, name); if (work_directory_source) { - work_directory_source->SetPrefix(datapath); - int len = work_directory_source->Load(name, buf, null_terminate); - work_directory_source->SetPrefix(); + int len = work_directory_source->Load(datapath, name, buf, null_terminate); if (len > 0) return len; if (buf) diff --git a/StarsEx/DataSource.cpp b/StarsEx/DataSource.cpp index 1618171..ce458d3 100644 --- a/StarsEx/DataSource.cpp +++ b/StarsEx/DataSource.cpp @@ -22,7 +22,6 @@ int DataSource::s_next_id {0}; DataSource::DataSource(Group group) : - m_prefix {""}, m_group {group}, m_id {s_next_id++} { @@ -48,16 +47,6 @@ DataSource::GetGroup() const } -void -DataSource::SetPrefix(const char* prefix) -{ - if (prefix) - m_prefix = prefix; - else - m_prefix = ""; -} - - ArchiveDataSource::ArchiveDataSource(const char* name, Group group) : ArchiveDataSource(new DataArchive(name), group) { @@ -78,26 +67,26 @@ ArchiveDataSource::~ArchiveDataSource() bool -ArchiveDataSource::Find(const char* name) const +ArchiveDataSource::Find(const Text& prefix, const char* name) const { - const int index = m_archive->FindEntry(name); + const int index = m_archive->FindEntry(prefix.concat(name)); return index > -1; } int -ArchiveDataSource::ListFiles(Text filter, List& items, bool recurse) const +ArchiveDataSource::ListFiles(const Text& prefix, Text filter, List& items, bool recurse) const { (void) recurse; // Lookup in DataArchives was always recursive so far filter = filter.replace("*", ""); // Wildcards worked only on boundaries - const int prefix = m_prefix.length(); + const int first = prefix.length(); const int count = m_archive->NumFiles(); for (int i = 0; i < count; ++i) { const auto* entry = m_archive->GetFile(i); Text name = entry->name; name.setSensitive(false); - if (name.contains(m_prefix) && name.contains(filter)) { - const auto without_prefix = name.substring(prefix, name.length()); + if (name.contains(prefix) && name.contains(filter)) { + const auto without_prefix = name.substring(first, name.length()); if (!items.contains(&without_prefix)) items.append(new Text(without_prefix)); } @@ -107,9 +96,9 @@ ArchiveDataSource::ListFiles(Text filter, List& items, bool recurse) const int -ArchiveDataSource::Load(const char* name, std::uint8_t*& buf, bool null_terminate) const +ArchiveDataSource::Load(const Text& prefix, const char* name, std::uint8_t*& buf, bool null_terminate) const { - const int index = m_archive->FindEntry(name); + const int index = m_archive->FindEntry(prefix.concat(name)); if (index > -1) return m_archive->ExpandEntry(index, buf, null_terminate); return 0; // -1 would be preferable, but 0 is from legacy @@ -129,28 +118,28 @@ FileSystemDataSource::~FileSystemDataSource() bool -FileSystemDataSource::Find(const char* name) const +FileSystemDataSource::Find(const Text& prefix, const char* name) const { std::filesystem::path full_path {m_path}; - full_path.append(m_prefix.data()); + full_path.append(prefix.data()); full_path.append(name); return std::filesystem::is_regular_file(full_path); } int -FileSystemDataSource::ListFiles(Text filter, List& items, bool recurse) const +FileSystemDataSource::ListFiles(const Text& prefix, Text filter, List& items, bool recurse) const { std::filesystem::path full_path {m_path}; - full_path.append(m_prefix.data()); + full_path.append(prefix.data()); filter = filter.replace("*", ""); - const auto prefix = full_path.string().length(); + const auto first = full_path.string().length(); const auto check = [&](const std::filesystem::directory_entry& entry){ const auto filename = entry.path().filename().string(); // more of a reason to switch to string soon const auto index = filename.find(filter.data()); if (index != decltype(filename)::npos) { Text path = entry.path().string().c_str(); - items.append(new Text(path.substring(prefix, path.length()))); + items.append(new Text(path.substring(first, path.length()))); } }; try { @@ -171,10 +160,10 @@ FileSystemDataSource::ListFiles(Text filter, List& items, bool recurse) co int -FileSystemDataSource::Load(const char* name, std::uint8_t*& buf, bool null_terminate) const +FileSystemDataSource::Load(const Text& prefix, const char* name, std::uint8_t*& buf, bool null_terminate) const { std::filesystem::path full_path {m_path}; - full_path.append(m_prefix.data()); + full_path.append(prefix.data()); full_path.append(name); FILE* f = fopen(full_path.string().c_str(), "rb"); if (!f) diff --git a/StarsEx/DataSource.h b/StarsEx/DataSource.h index cc69870..86a496f 100644 --- a/StarsEx/DataSource.h +++ b/StarsEx/DataSource.h @@ -26,14 +26,12 @@ public: int Id() const; Group GetGroup() const; - void SetPrefix(const char* prefix=nullptr); - virtual bool Find(const char* name) const = 0; - virtual int ListFiles(Text filter, List& items, bool recurse=false) const = 0; - virtual int Load(const char* name, std::uint8_t*& buf, bool null_terminate=false) const = 0; + virtual bool Find(const Text& prefix, const char* name) const = 0; + virtual int ListFiles(const Text& prefix, Text filter, List& items, bool recurse=false) const = 0; + virtual int Load(const Text& prefix, const char* name, std::uint8_t*& buf, bool null_terminate=false) const = 0; protected: - Text m_prefix; Group m_group; const int m_id; static int s_next_id; @@ -47,9 +45,9 @@ public: explicit ArchiveDataSource(DataArchive* archive, Group group=Group::DEFAULT); ~ArchiveDataSource() override; - bool Find(const char* name) const override; - int ListFiles(Text filter, List& items, bool recurse=false) const override; - int Load(const char* name, std::uint8_t*& buf, bool null_terminate=false) const override; + bool Find(const Text& prefix, const char* name) const override; + int ListFiles(const Text& prefix, Text filter, List& items, bool recurse=false) const override; + int Load(const Text& prefix, const char* name, std::uint8_t*& buf, bool null_terminate=false) const override; protected: DataArchive* m_archive; @@ -62,9 +60,9 @@ public: explicit FileSystemDataSource(const char* path=".", Group group=Group::DEFAULT); ~FileSystemDataSource() override; - bool Find(const char* name) const override; - int ListFiles(Text filter, List& items, bool recurse=false) const override; - int Load(const char* name, std::uint8_t*& buf, bool null_terminate=false) const override; + bool Find(const Text& prefix, const char* name) const override; + int ListFiles(const Text& prefix, Text filter, List& items, bool recurse=false) const override; + int Load(const Text& prefix, const char* name, std::uint8_t*& buf, bool null_terminate=false) const override; protected: const char* m_path; -- cgit v1.1