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/DataSource.cpp | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) (limited to 'StarsEx/DataSource.cpp') 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) -- cgit v1.1