From d4b069f4580b1cde512861cb3bfd3930c3a1e909 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Wed, 29 May 2019 08:16:12 +0200 Subject: [PATCH] DiscIO: Use std::string_view in FileSystem::FindFileInfo ...and in the functions that call it. --- Source/Core/Core/WiiUtils.cpp | 13 +++++++------ Source/Core/DiscIO/DiscExtractor.cpp | 4 ++-- Source/Core/DiscIO/DiscExtractor.h | 6 ++++-- Source/Core/DiscIO/FileSystemGCWii.cpp | 16 +++++++++++----- Source/Core/DiscIO/FileSystemGCWii.h | 5 +++-- Source/Core/DiscIO/Filesystem.h | 3 ++- Source/Core/DiscIO/VolumeFileBlobReader.cpp | 5 ++++- Source/Core/DiscIO/VolumeFileBlobReader.h | 4 ++-- 8 files changed, 35 insertions(+), 21 deletions(-) diff --git a/Source/Core/Core/WiiUtils.cpp b/Source/Core/Core/WiiUtils.cpp index 3986b6fb8c..1f9052ac02 100644 --- a/Source/Core/Core/WiiUtils.cpp +++ b/Source/Core/Core/WiiUtils.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -618,9 +619,9 @@ private: static_assert(sizeof(Entry) == 512, "Wrong size"); #pragma pack(pop) - UpdateResult UpdateFromManifest(const std::string& manifest_name); + UpdateResult UpdateFromManifest(std::string_view manifest_name); UpdateResult ProcessEntry(u32 type, std::bitset<32> attrs, const TitleInfo& title, - const std::string& path); + std::string_view path); UpdateCallback m_update_callback; std::unique_ptr m_volume; @@ -655,7 +656,7 @@ UpdateResult DiscSystemUpdater::DoDiscUpdate() return UpdateFromManifest("__update.inf"); } -UpdateResult DiscSystemUpdater::UpdateFromManifest(const std::string& manifest_name) +UpdateResult DiscSystemUpdater::UpdateFromManifest(std::string_view manifest_name) { const DiscIO::FileSystem* disc_fs = m_volume->GetFileSystem(m_partition); if (!disc_fs) @@ -693,7 +694,7 @@ UpdateResult DiscSystemUpdater::UpdateFromManifest(const std::string& manifest_n const u64 title_id = Common::swap64(entry.data() + offsetof(Entry, title_id)); const u16 title_version = Common::swap16(entry.data() + offsetof(Entry, title_version)); const char* path_pointer = reinterpret_cast(entry.data() + offsetof(Entry, path)); - const std::string path{path_pointer, strnlen(path_pointer, sizeof(Entry::path))}; + const std::string_view path{path_pointer, strnlen(path_pointer, sizeof(Entry::path))}; if (!m_update_callback(i, num_entries, title_id)) return UpdateResult::Cancelled; @@ -712,7 +713,7 @@ UpdateResult DiscSystemUpdater::UpdateFromManifest(const std::string& manifest_n } UpdateResult DiscSystemUpdater::ProcessEntry(u32 type, std::bitset<32> attrs, - const TitleInfo& title, const std::string& path) + const TitleInfo& title, std::string_view path) { // Skip any unknown type and boot2 updates (for now). if (type != 2 && type != 3 && type != 6 && type != 7) @@ -734,7 +735,7 @@ UpdateResult DiscSystemUpdater::ProcessEntry(u32 type, std::bitset<32> attrs, auto blob = DiscIO::VolumeFileBlobReader::Create(*m_volume, m_partition, path); if (!blob) { - ERROR_LOG(CORE, "Could not find %s", path.c_str()); + ERROR_LOG(CORE, "Could not find %s", std::string(path).c_str()); return UpdateResult::DiscReadFailed; } const DiscIO::WiiWAD wad{std::move(blob)}; diff --git a/Source/Core/DiscIO/DiscExtractor.cpp b/Source/Core/DiscIO/DiscExtractor.cpp index 0bc987a035..9ef3bd9d0a 100644 --- a/Source/Core/DiscIO/DiscExtractor.cpp +++ b/Source/Core/DiscIO/DiscExtractor.cpp @@ -69,7 +69,7 @@ u64 ReadFile(const Volume& volume, const Partition& partition, const FileInfo* f return read_length; } -u64 ReadFile(const Volume& volume, const Partition& partition, const std::string& path, u8* buffer, +u64 ReadFile(const Volume& volume, const Partition& partition, std::string_view path, u8* buffer, u64 max_buffer_size, u64 offset_in_file) { const FileSystem* file_system = volume.GetFileSystem(partition); @@ -117,7 +117,7 @@ bool ExportFile(const Volume& volume, const Partition& partition, const FileInfo export_filename); } -bool ExportFile(const Volume& volume, const Partition& partition, const std::string& path, +bool ExportFile(const Volume& volume, const Partition& partition, std::string_view path, const std::string& export_filename) { const FileSystem* file_system = volume.GetFileSystem(partition); diff --git a/Source/Core/DiscIO/DiscExtractor.h b/Source/Core/DiscIO/DiscExtractor.h index 16b8f932fd..0ca1954ee0 100644 --- a/Source/Core/DiscIO/DiscExtractor.h +++ b/Source/Core/DiscIO/DiscExtractor.h @@ -6,6 +6,8 @@ #include #include +#include +#include #include "Common/CommonTypes.h" @@ -24,13 +26,13 @@ std::string NameForPartitionType(u32 partition_type, bool include_prefix); u64 ReadFile(const Volume& volume, const Partition& partition, const FileInfo* file_info, u8* buffer, u64 max_buffer_size, u64 offset_in_file = 0); -u64 ReadFile(const Volume& volume, const Partition& partition, const std::string& path, u8* buffer, +u64 ReadFile(const Volume& volume, const Partition& partition, std::string_view path, u8* buffer, u64 max_buffer_size, u64 offset_in_file = 0); bool ExportData(const Volume& volume, const Partition& partition, u64 offset, u64 size, const std::string& export_filename); bool ExportFile(const Volume& volume, const Partition& partition, const FileInfo* file_info, const std::string& export_filename); -bool ExportFile(const Volume& volume, const Partition& partition, const std::string& path, +bool ExportFile(const Volume& volume, const Partition& partition, std::string_view path, const std::string& export_filename); // update_progress is called once for each child (file or directory). diff --git a/Source/Core/DiscIO/FileSystemGCWii.cpp b/Source/Core/DiscIO/FileSystemGCWii.cpp index a7826c9ab1..3954472465 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.cpp +++ b/Source/Core/DiscIO/FileSystemGCWii.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "Common/CommonFuncs.h" @@ -257,7 +258,7 @@ const FileInfo& FileSystemGCWii::GetRoot() const return m_root; } -std::unique_ptr FileSystemGCWii::FindFileInfo(const std::string& path) const +std::unique_ptr FileSystemGCWii::FindFileInfo(std::string_view path) const { if (!IsValid()) return nullptr; @@ -265,7 +266,7 @@ std::unique_ptr FileSystemGCWii::FindFileInfo(const std::string& path) return FindFileInfo(path, m_root); } -std::unique_ptr FileSystemGCWii::FindFileInfo(const std::string& path, +std::unique_ptr FileSystemGCWii::FindFileInfo(std::string_view path, const FileInfo& file_info) const { // Given a path like "directory1/directory2/fileA.bin", this function will @@ -276,12 +277,17 @@ std::unique_ptr FileSystemGCWii::FindFileInfo(const std::string& path, return file_info.clone(); // We're done const size_t name_end = path.find('/', name_start); - const std::string name = path.substr(name_start, name_end - name_start); - const std::string rest_of_path = (name_end != std::string::npos) ? path.substr(name_end + 1) : ""; + const std::string_view name = path.substr(name_start, name_end - name_start); + const std::string_view rest_of_path = + (name_end != std::string::npos) ? path.substr(name_end + 1) : ""; for (const FileInfo& child : file_info) { - if (!strcasecmp(child.GetName().c_str(), name.c_str())) + const std::string child_name = child.GetName(); + + // We need case insensitive comparison since some games have OPENING.BNR instead of opening.bnr + if (child_name.size() == name.size() && + !strncasecmp(child_name.data(), name.data(), name.size())) { // A match is found. The rest of the path is passed on to finish the search. std::unique_ptr result = FindFileInfo(rest_of_path, child); diff --git a/Source/Core/DiscIO/FileSystemGCWii.h b/Source/Core/DiscIO/FileSystemGCWii.h index 753d9ee7f0..26ff5d5444 100644 --- a/Source/Core/DiscIO/FileSystemGCWii.h +++ b/Source/Core/DiscIO/FileSystemGCWii.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "Common/CommonTypes.h" @@ -90,7 +91,7 @@ public: bool IsValid() const override { return m_valid; } const FileInfo& GetRoot() const override; - std::unique_ptr FindFileInfo(const std::string& path) const override; + std::unique_ptr FindFileInfo(std::string_view path) const override; std::unique_ptr FindFileInfo(u64 disc_offset) const override; private: @@ -100,7 +101,7 @@ private: // Maps the end offset of files to FST indexes mutable std::map m_offset_file_info_cache; - std::unique_ptr FindFileInfo(const std::string& path, const FileInfo& file_info) const; + std::unique_ptr FindFileInfo(std::string_view path, const FileInfo& file_info) const; }; } // namespace DiscIO diff --git a/Source/Core/DiscIO/Filesystem.h b/Source/Core/DiscIO/Filesystem.h index ad631b078b..b46da7c29b 100644 --- a/Source/Core/DiscIO/Filesystem.h +++ b/Source/Core/DiscIO/Filesystem.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "Common/CommonTypes.h" @@ -117,7 +118,7 @@ public: // are only valid for as long as the file system object is valid. virtual const FileInfo& GetRoot() const = 0; // Returns nullptr if not found - virtual std::unique_ptr FindFileInfo(const std::string& path) const = 0; + virtual std::unique_ptr FindFileInfo(std::string_view path) const = 0; // Returns nullptr if not found virtual std::unique_ptr FindFileInfo(u64 disc_offset) const = 0; }; diff --git a/Source/Core/DiscIO/VolumeFileBlobReader.cpp b/Source/Core/DiscIO/VolumeFileBlobReader.cpp index 0dd7ebf290..336fecad4c 100644 --- a/Source/Core/DiscIO/VolumeFileBlobReader.cpp +++ b/Source/Core/DiscIO/VolumeFileBlobReader.cpp @@ -4,6 +4,9 @@ #include "DiscIO/VolumeFileBlobReader.h" +#include +#include + #include "DiscIO/Filesystem.h" #include "DiscIO/Volume.h" @@ -11,7 +14,7 @@ namespace DiscIO { std::unique_ptr VolumeFileBlobReader::Create(const Volume& volume, const Partition& partition, - const std::string& file_path) + std::string_view file_path) { const FileSystem* file_system = volume.GetFileSystem(partition); if (!file_system) diff --git a/Source/Core/DiscIO/VolumeFileBlobReader.h b/Source/Core/DiscIO/VolumeFileBlobReader.h index 2696790829..bbc75d61b6 100644 --- a/Source/Core/DiscIO/VolumeFileBlobReader.h +++ b/Source/Core/DiscIO/VolumeFileBlobReader.h @@ -5,7 +5,7 @@ #pragma once #include -#include +#include #include "Common/CommonTypes.h" #include "DiscIO/Blob.h" @@ -20,7 +20,7 @@ class VolumeFileBlobReader final : public BlobReader { public: static std::unique_ptr - Create(const Volume& volume, const Partition& partition, const std::string& file_path); + Create(const Volume& volume, const Partition& partition, std::string_view file_path); BlobType GetBlobType() const override { return BlobType::PLAIN; } u64 GetRawSize() const override;