From 21b167d3823e3dcbf0f9f6f65f4d55f28e1612a7 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sun, 19 Jan 2025 20:52:18 +1000 Subject: [PATCH] CDImage: Tidy up Open() method --- src/util/cd_image.cpp | 68 +++++++++++++++++------------------- src/util/cd_image.h | 16 ++++----- src/util/cd_image_chd.cpp | 4 +-- src/util/cd_image_device.cpp | 26 +++++++------- src/util/cd_image_m3u.cpp | 4 +-- src/util/cd_image_mds.cpp | 4 +-- src/util/cd_image_pbp.cpp | 4 +-- src/util/cd_image_ppf.cpp | 7 ++-- 8 files changed, 64 insertions(+), 69 deletions(-) diff --git a/src/util/cd_image.cpp b/src/util/cd_image.cpp index 3532f6d24..93648ab9a 100644 --- a/src/util/cd_image.cpp +++ b/src/util/cd_image.cpp @@ -50,83 +50,79 @@ void CDImage::DeinterleaveSubcode(const u8* subcode_in, u8* subcode_out) } } -std::unique_ptr CDImage::Open(const char* filename, bool allow_patches, Error* error) +std::unique_ptr CDImage::Open(const char* path, bool allow_patches, Error* error) { - const char* extension; - + // Annoying handling because of storage access framework. #ifdef __ANDROID__ - std::string filename_display_name(FileSystem::GetDisplayNameFromPath(filename)); - if (filename_display_name.empty()) - filename_display_name = filename; - - extension = std::strrchr(filename_display_name.c_str(), '.'); + const std::string path_display_name = FileSystem::GetDisplayNameFromPath(path); + const std::string_view extension = Path::GetExtension(path_display_name); #else - extension = std::strrchr(filename, '.'); + const std::string_view extension = Path::GetExtension(path); #endif std::unique_ptr image; - if (!extension) + if (extension.empty()) { // Device filenames on Linux don't have extensions. - if (IsDeviceName(filename)) + if (IsDeviceName(path)) { - image = OpenDeviceImage(filename, error); + image = OpenDeviceImage(path, error); } else { - Error::SetStringFmt(error, "Invalid filename: '{}'", Path::GetFileName(filename)); + Error::SetStringFmt(error, "Invalid filename: '{}'", Path::GetFileName(path)); return nullptr; } } - else if (StringUtil::Strcasecmp(extension, ".cue") == 0) + else if (StringUtil::EqualNoCase(extension, "cue")) { - image = OpenCueSheetImage(filename, error); + image = OpenCueSheetImage(path, error); } - else if (StringUtil::Strcasecmp(extension, ".bin") == 0 || StringUtil::Strcasecmp(extension, ".img") == 0 || - StringUtil::Strcasecmp(extension, ".iso") == 0 || StringUtil::Strcasecmp(extension, ".ecm") == 0) + else if (StringUtil::EqualNoCase(extension, "bin") || StringUtil::EqualNoCase(extension, "img") || + StringUtil::EqualNoCase(extension, "iso") || StringUtil::EqualNoCase(extension, "ecm")) { - image = OpenBinImage(filename, error); + image = OpenBinImage(path, error); } - else if (StringUtil::Strcasecmp(extension, ".chd") == 0) + else if (StringUtil::EqualNoCase(extension, "chd")) { - image = OpenCHDImage(filename, error); + image = OpenCHDImage(path, error); } - else if (StringUtil::Strcasecmp(extension, ".mds") == 0) + else if (StringUtil::EqualNoCase(extension, "mds")) { - image = OpenMdsImage(filename, error); + image = OpenMdsImage(path, error); } - else if (StringUtil::Strcasecmp(extension, ".pbp") == 0) + else if (StringUtil::EqualNoCase(extension, "pbp")) { - image = OpenPBPImage(filename, error); + image = OpenPBPImage(path, error); } - else if (StringUtil::Strcasecmp(extension, ".m3u") == 0) + else if (StringUtil::EqualNoCase(extension, "m3u")) { - image = OpenM3uImage(filename, allow_patches, error); + // skip applying patches to the main path, which isn't a real disc + image = OpenM3uImage(path, allow_patches, error); + allow_patches = false; } - else if (IsDeviceName(filename)) + else if (IsDeviceName(path)) { - image = OpenDeviceImage(filename, error); + image = OpenDeviceImage(path, error); } else { - Error::SetStringFmt(error, "Unknown extension '{}' from filename '{}'", extension, Path::GetFileName(filename)); + Error::SetStringFmt(error, "Unknown extension '{}' from filename '{}'", extension, Path::GetFileName(path)); return nullptr; } if (allow_patches) { #ifdef __ANDROID__ - const std::string ppf_filename( - Path::BuildRelativePath(filename, Path::ReplaceExtension(filename_display_name, "ppf"))); + const std::string ppf_path = Path::BuildRelativePath(path, Path::ReplaceExtension(path_display_name, "ppf")); #else - const std::string ppf_filename( - Path::BuildRelativePath(filename, Path::ReplaceExtension(Path::GetFileName(filename), "ppf"))); + const std::string ppf_path = Path::BuildRelativePath(path, Path::ReplaceExtension(Path::GetFileName(path), "ppf")); #endif - if (FileSystem::FileExists(ppf_filename.c_str())) + if (FileSystem::FileExists(ppf_path.c_str())) { - image = CDImage::OverlayPPFPatch(ppf_filename.c_str(), std::move(image)); + image = CDImage::OverlayPPFPatch(ppf_path.c_str(), std::move(image)); if (!image) - Error::SetStringFmt(error, "Failed to apply ppf patch from '{}'.", ppf_filename); + Error::SetStringFmt(error, "Failed to apply ppf patch from '{}'.", ppf_path); } } diff --git a/src/util/cd_image.h b/src/util/cd_image.h index 295c44492..85de1bdb5 100644 --- a/src/util/cd_image.h +++ b/src/util/cd_image.h @@ -33,7 +33,7 @@ public: MODE1_HEADER_SIZE = 4, MODE2_HEADER_SIZE = 12, MODE2_DATA_SECTOR_SIZE = 2336, // header + edc - FRAMES_PER_SECOND = 75, // "sectors", or "timecode frames" (not "channel frames") + FRAMES_PER_SECOND = 75, // "sectors", or "timecode frames" (not "channel frames") SECONDS_PER_MINUTE = 60, FRAMES_PER_MINUTE = FRAMES_PER_SECOND * SECONDS_PER_MINUTE, SUBCHANNEL_BYTES_PER_FRAME = 12, @@ -240,17 +240,17 @@ public: static bool IsDeviceName(const char* filename); // Opening disc image. - static std::unique_ptr Open(const char* filename, bool allow_patches, Error* error); + static std::unique_ptr Open(const char* path, bool allow_patches, Error* error); static std::unique_ptr OpenBinImage(const char* path, Error* error); static std::unique_ptr OpenCueSheetImage(const char* path, Error* error); - static std::unique_ptr OpenCHDImage(const char* filename, Error* error); - static std::unique_ptr OpenMdsImage(const char* filename, Error* error); - static std::unique_ptr OpenPBPImage(const char* filename, Error* error); - static std::unique_ptr OpenM3uImage(const char* filename, bool apply_patches, Error* error); - static std::unique_ptr OpenDeviceImage(const char* filename, Error* error); + static std::unique_ptr OpenCHDImage(const char* path, Error* error); + static std::unique_ptr OpenMdsImage(const char* path, Error* error); + static std::unique_ptr OpenPBPImage(const char* path, Error* error); + static std::unique_ptr OpenM3uImage(const char* path, bool apply_patches, Error* error); + static std::unique_ptr OpenDeviceImage(const char* path, Error* error); static std::unique_ptr CreateMemoryImage(CDImage* image, ProgressCallback* progress = ProgressCallback::NullProgressCallback); - static std::unique_ptr OverlayPPFPatch(const char* filename, std::unique_ptr parent_image, + static std::unique_ptr OverlayPPFPatch(const char* path, std::unique_ptr parent_image, ProgressCallback* progress = ProgressCallback::NullProgressCallback); // Accessors. diff --git a/src/util/cd_image_chd.cpp b/src/util/cd_image_chd.cpp index 94e3cbfce..84735afc8 100644 --- a/src/util/cd_image_chd.cpp +++ b/src/util/cd_image_chd.cpp @@ -533,10 +533,10 @@ s64 CDImageCHD::GetSizeOnDisk() const return static_cast(chd_get_compressed_size(m_chd)); } -std::unique_ptr CDImage::OpenCHDImage(const char* filename, Error* error) +std::unique_ptr CDImage::OpenCHDImage(const char* path, Error* error) { std::unique_ptr image = std::make_unique(); - if (!image->Open(filename, error)) + if (!image->Open(path, error)) return {}; return image; diff --git a/src/util/cd_image_device.cpp b/src/util/cd_image_device.cpp index 18fd8e032..70e25be1e 100644 --- a/src/util/cd_image_device.cpp +++ b/src/util/cd_image_device.cpp @@ -634,10 +634,10 @@ bool CDImageDeviceWin32::DetermineReadMode(bool try_sptd) return false; } -std::unique_ptr CDImage::OpenDeviceImage(const char* filename, Error* error) +std::unique_ptr CDImage::OpenDeviceImage(const char* path, Error* error) { std::unique_ptr image = std::make_unique(); - if (!image->Open(filename, error)) + if (!image->Open(path, error)) return {}; return image; @@ -679,9 +679,9 @@ std::vector> CDImage::GetDeviceList() return ret; } -bool CDImage::IsDeviceName(const char* filename) +bool CDImage::IsDeviceName(const char* path) { - return std::string_view(filename).starts_with("\\\\.\\"); + return std::string_view(path).starts_with("\\\\.\\"); } #elif defined(__linux__) && !defined(__ANDROID__) @@ -1083,10 +1083,10 @@ bool CDImageDeviceLinux::DetermineReadMode(Error* error) return false; } -std::unique_ptr CDImage::OpenDeviceImage(const char* filename, Error* error) +std::unique_ptr CDImage::OpenDeviceImage(const char* path, Error* error) { std::unique_ptr image = std::make_unique(); - if (!image->Open(filename, error)) + if (!image->Open(path, error)) return {}; return image; @@ -1550,10 +1550,10 @@ bool CDImageDeviceMacOS::DetermineReadMode(Error* error) return false; } -std::unique_ptr CDImage::OpenDeviceImage(const char* filename, Error* error) +std::unique_ptr CDImage::OpenDeviceImage(const char* path, Error* error) { std::unique_ptr image = std::make_unique(); - if (!image->Open(filename, error)) + if (!image->Open(path, error)) return {}; return image; @@ -1605,12 +1605,12 @@ std::vector> CDImage::GetDeviceList() return ret; } -bool CDImage::IsDeviceName(const char* filename) +bool CDImage::IsDeviceName(const char* path) { - if (!std::string_view(filename).starts_with("/dev")) + if (!std::string_view(path).starts_with("/dev")) return false; - io_service_t service = GetDeviceMediaService(filename); + io_service_t service = GetDeviceMediaService(path); const bool valid = (service != 0); if (valid) IOObjectRelease(service); @@ -1620,7 +1620,7 @@ bool CDImage::IsDeviceName(const char* filename) #else -std::unique_ptr CDImage::OpenDeviceImage(const char* filename, Error* error) +std::unique_ptr CDImage::OpenDeviceImage(const char* path, Error* error) { return {}; } @@ -1630,7 +1630,7 @@ std::vector> CDImage::GetDeviceList() return {}; } -bool CDImage::IsDeviceName(const char* filename) +bool CDImage::IsDeviceName(const char* path) { return false; } diff --git a/src/util/cd_image_m3u.cpp b/src/util/cd_image_m3u.cpp index 1d0d12177..9f9f4b07e 100644 --- a/src/util/cd_image_m3u.cpp +++ b/src/util/cd_image_m3u.cpp @@ -181,10 +181,10 @@ bool CDImageM3u::ReadSubChannelQ(SubChannelQ* subq, const Index& index, LBA lba_ return m_current_image->ReadSubChannelQ(subq, index, lba_in_index); } -std::unique_ptr CDImage::OpenM3uImage(const char* filename, bool apply_patches, Error* error) +std::unique_ptr CDImage::OpenM3uImage(const char* path, bool apply_patches, Error* error) { std::unique_ptr image = std::make_unique(); - if (!image->Open(filename, apply_patches, error)) + if (!image->Open(path, apply_patches, error)) return {}; return image; diff --git a/src/util/cd_image_mds.cpp b/src/util/cd_image_mds.cpp index 96146feb8..c9d99629f 100644 --- a/src/util/cd_image_mds.cpp +++ b/src/util/cd_image_mds.cpp @@ -270,10 +270,10 @@ s64 CDImageMds::GetSizeOnDisk() const return FileSystem::FSize64(m_mdf_file); } -std::unique_ptr CDImage::OpenMdsImage(const char* filename, Error* error) +std::unique_ptr CDImage::OpenMdsImage(const char* path, Error* error) { std::unique_ptr image = std::make_unique(); - if (!image->OpenAndParse(filename, error)) + if (!image->OpenAndParse(path, error)) return {}; return image; diff --git a/src/util/cd_image_pbp.cpp b/src/util/cd_image_pbp.cpp index d011bc52b..0ba7f6b06 100644 --- a/src/util/cd_image_pbp.cpp +++ b/src/util/cd_image_pbp.cpp @@ -935,10 +935,10 @@ s64 CDImagePBP::GetSizeOnDisk() const return FileSystem::FSize64(m_file); } -std::unique_ptr CDImage::OpenPBPImage(const char* filename, Error* error) +std::unique_ptr CDImage::OpenPBPImage(const char* path, Error* error) { std::unique_ptr image = std::make_unique(); - if (!image->Open(filename, error)) + if (!image->Open(path, error)) return {}; return image; diff --git a/src/util/cd_image_ppf.cpp b/src/util/cd_image_ppf.cpp index b57296b22..f5a18679e 100644 --- a/src/util/cd_image_ppf.cpp +++ b/src/util/cd_image_ppf.cpp @@ -458,12 +458,11 @@ s64 CDImagePPF::GetSizeOnDisk() const return m_patch_size + m_parent_image->GetSizeOnDisk(); } -std::unique_ptr -CDImage::OverlayPPFPatch(const char* filename, std::unique_ptr parent_image, - ProgressCallback* progress /* = ProgressCallback::NullProgressCallback */) +std::unique_ptr CDImage::OverlayPPFPatch(const char* path, std::unique_ptr parent_image, + ProgressCallback* progress) { std::unique_ptr ppf_image = std::make_unique(); - if (!ppf_image->Open(filename, std::move(parent_image))) + if (!ppf_image->Open(path, std::move(parent_image))) return {}; return ppf_image;