From 0c0125937f8877651b79c8cd99a230747e527dd2 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 12 Nov 2024 20:25:19 +0100 Subject: [PATCH] Show filename as title if image has no title --- floppy/src/loaders/a2r2.rs | 7 +++++-- floppy/src/loaders/a2r3.rs | 7 +++++-- floppy/src/loaders/auto.rs | 16 ++++++++-------- floppy/src/loaders/bitfile.rs | 4 ++-- floppy/src/loaders/diskcopy42.rs | 11 ++++++++--- floppy/src/loaders/mod.rs | 9 +++++++-- floppy/src/loaders/moof.rs | 7 +++++-- floppy/src/loaders/pfi.rs | 6 ++---- floppy/src/loaders/raw.rs | 8 ++++---- 9 files changed, 46 insertions(+), 29 deletions(-) diff --git a/floppy/src/loaders/a2r2.rs b/floppy/src/loaders/a2r2.rs index 0ea210d..a8d6b08 100644 --- a/floppy/src/loaders/a2r2.rs +++ b/floppy/src/loaders/a2r2.rs @@ -116,7 +116,7 @@ impl A2Rv2 { } impl FloppyImageLoader for A2Rv2 { - fn load(data: &[u8]) -> Result { + fn load(data: &[u8], filename: Option<&str>) -> Result { let mut cursor = Cursor::new(data); let _header = A2RHeader::read(&mut cursor)?; @@ -163,7 +163,10 @@ impl FloppyImageLoader for A2Rv2 { bail!("Image is not of a 3.5 inch disk"); } let metadata = Self::parse_meta(&meta); - let title = metadata.get("title").copied().unwrap_or("?"); + let title = metadata + .get("title") + .copied() + .unwrap_or_else(|| filename.unwrap_or_default()); let mut img = FloppyImage::new_empty( if captures.iter().any(|c| c.get_side() > 0) { diff --git a/floppy/src/loaders/a2r3.rs b/floppy/src/loaders/a2r3.rs index 2fe7f92..5f2fe80 100644 --- a/floppy/src/loaders/a2r3.rs +++ b/floppy/src/loaders/a2r3.rs @@ -147,7 +147,7 @@ impl A2Rv3 { } impl FloppyImageLoader for A2Rv3 { - fn load(data: &[u8]) -> Result { + fn load(data: &[u8], filename: Option<&str>) -> Result { let mut cursor = Cursor::new(data); let _header = A2RHeader::read(&mut cursor)?; @@ -198,7 +198,10 @@ impl FloppyImageLoader for A2Rv3 { bail!("Image is not of a Mac 3.5 inch CLV disk"); } let metadata = Self::parse_meta(&meta); - let title = metadata.get("title").copied().unwrap_or("?"); + let title = metadata + .get("title") + .copied() + .unwrap_or_else(|| filename.unwrap_or_default()); let mut img = FloppyImage::new_empty( if captures.iter().any(|c| c.get_side() > 0) { diff --git a/floppy/src/loaders/auto.rs b/floppy/src/loaders/auto.rs index eea0ccb..35d0010 100644 --- a/floppy/src/loaders/auto.rs +++ b/floppy/src/loaders/auto.rs @@ -79,15 +79,15 @@ impl Autodetect { } impl FloppyImageLoader for Autodetect { - fn load(data: &[u8]) -> Result { + fn load(data: &[u8], filename: Option<&str>) -> Result { match Self::detect(data)? { - ImageType::A2R2 => A2Rv2::load(data), - ImageType::A2R3 => A2Rv3::load(data), - ImageType::MOOF => Moof::load(data), - ImageType::Bitfile => Bitfile::load(data), - ImageType::DC42 => Diskcopy42::load(data), - ImageType::PFI => PFI::load(data), - ImageType::Raw => RawImage::load(data), + ImageType::A2R2 => A2Rv2::load(data, filename), + ImageType::A2R3 => A2Rv3::load(data, filename), + ImageType::MOOF => Moof::load(data, filename), + ImageType::Bitfile => Bitfile::load(data, filename), + ImageType::DC42 => Diskcopy42::load(data, filename), + ImageType::PFI => PFI::load(data, filename), + ImageType::Raw => RawImage::load(data, filename), } } } diff --git a/floppy/src/loaders/bitfile.rs b/floppy/src/loaders/bitfile.rs index 042fc9a..475a228 100644 --- a/floppy/src/loaders/bitfile.rs +++ b/floppy/src/loaders/bitfile.rs @@ -39,7 +39,7 @@ impl Bitfile { } impl FloppyImageLoader for Bitfile { - fn load(data: &[u8]) -> Result { + fn load(data: &[u8], filename: Option<&str>) -> Result { let tracks = Self::count_tracks(data)?; let mut image = FloppyImage::new_empty( match tracks { @@ -47,7 +47,7 @@ impl FloppyImageLoader for Bitfile { 160 => FloppyType::Mac800K, _ => bail!("Invalid amount of tracks: {}", tracks), }, - "", + filename.unwrap_or_default(), ); let mut offset = 0; diff --git a/floppy/src/loaders/diskcopy42.rs b/floppy/src/loaders/diskcopy42.rs index fa09bcc..9645f5e 100644 --- a/floppy/src/loaders/diskcopy42.rs +++ b/floppy/src/loaders/diskcopy42.rs @@ -4,7 +4,7 @@ use super::FloppyImageLoader; use crate::macformat::MacFormatEncoder; -use crate::FloppyType; +use crate::{FloppyImage, FloppyType}; use anyhow::{bail, Result}; use binrw::io::Cursor; @@ -89,9 +89,14 @@ impl Dc42Raw { pub struct Diskcopy42 {} impl FloppyImageLoader for Diskcopy42 { - fn load(data: &[u8]) -> anyhow::Result { + fn load(data: &[u8], filename: Option<&str>) -> Result { let mut cursor = Cursor::new(data); let raw = Dc42Raw::read(&mut cursor)?; + let title = if raw.name.is_empty() { + filename.unwrap_or_default() + } else { + &raw.name + }; let floppytype = raw.get_type()?; if raw.data.len() != floppytype.get_logical_size() { @@ -102,6 +107,6 @@ impl FloppyImageLoader for Diskcopy42 { ); } - MacFormatEncoder::encode(floppytype, &raw.data, Some(&raw.tags), &raw.name) + MacFormatEncoder::encode(floppytype, &raw.data, Some(&raw.tags), title) } } diff --git a/floppy/src/loaders/mod.rs b/floppy/src/loaders/mod.rs index 75baf78..804f6f5 100644 --- a/floppy/src/loaders/mod.rs +++ b/floppy/src/loaders/mod.rs @@ -7,6 +7,8 @@ mod moof; mod pfi; mod raw; +use std::path::Path; + pub use a2r2::A2Rv2; pub use a2r3::A2Rv3; pub use auto::Autodetect; @@ -23,10 +25,13 @@ use anyhow::Result; /// A loader to read a specific format and transform it into a usable FloppyImage pub trait FloppyImageLoader { - fn load(data: &[u8]) -> Result; + fn load(data: &[u8], filename: Option<&str>) -> Result; fn load_file(filename: &str) -> Result { - Self::load(&std::fs::read(filename)?) + Self::load( + &std::fs::read(filename)?, + Path::new(filename).file_name().and_then(|s| s.to_str()), + ) } } diff --git a/floppy/src/loaders/moof.rs b/floppy/src/loaders/moof.rs index 7093e2f..5732187 100644 --- a/floppy/src/loaders/moof.rs +++ b/floppy/src/loaders/moof.rs @@ -151,7 +151,7 @@ impl Moof { } impl FloppyImageLoader for Moof { - fn load(data: &[u8]) -> Result { + fn load(data: &[u8], filename: Option<&str>) -> Result { let mut cursor = Cursor::new(data); let header = MoofHeader::read(&mut cursor)?; let checksum = crc::Crc::::new(&crc::CRC_32_ISO_HDLC).checksum(&data[12..]); @@ -198,7 +198,10 @@ impl FloppyImageLoader for Moof { let info = info.context("No INFO chunk in file")?; let trks = trks.context("No TRKS chunk in file")?; let metadata = Self::parse_meta(&meta); - let title = metadata.get("title").copied().unwrap_or("?"); + let title = metadata + .get("title") + .copied() + .unwrap_or_else(|| filename.unwrap_or_default()); let mut img = FloppyImage::new_empty( info.disktype diff --git a/floppy/src/loaders/pfi.rs b/floppy/src/loaders/pfi.rs index e7632af..18815a9 100644 --- a/floppy/src/loaders/pfi.rs +++ b/floppy/src/loaders/pfi.rs @@ -66,7 +66,7 @@ struct PayloadTrack { pub struct PFI {} impl FloppyImageLoader for PFI { - fn load(data: &[u8]) -> Result { + fn load(data: &[u8], filename: Option<&str>) -> Result { let mut cursor = Cursor::new(data); let mut tracks: HashMap<(usize, usize), &[u8]> = HashMap::new(); @@ -139,15 +139,13 @@ impl FloppyImageLoader for PFI { cursor.seek(SeekFrom::Start(startpos + u64::from(chunk.size) + 4))?; } - let title = ""; - let mut img = FloppyImage::new_empty( if tracks.keys().any(|&(s, _t)| s > 0) { FloppyType::Mac800K } else { FloppyType::Mac400K }, - title, + filename.unwrap_or_default(), ); // Fill tracks diff --git a/floppy/src/loaders/raw.rs b/floppy/src/loaders/raw.rs index f12671e..9cfe19b 100644 --- a/floppy/src/loaders/raw.rs +++ b/floppy/src/loaders/raw.rs @@ -1,22 +1,22 @@ //! Raw, sector-based image format use super::FloppyImageLoader; -use crate::macformat::MacFormatEncoder; use crate::FloppyType; +use crate::{macformat::MacFormatEncoder, FloppyImage}; -use anyhow::bail; +use anyhow::{bail, Result}; use strum::IntoEnumIterator; /// Raw image loader pub struct RawImage {} impl FloppyImageLoader for RawImage { - fn load(data: &[u8]) -> anyhow::Result { + fn load(data: &[u8], filename: Option<&str>) -> Result { let Some(floppytype) = FloppyType::iter().find(|t| t.get_logical_size() == data.len()) else { bail!("Invalid raw image length: {}", data.len()) }; - MacFormatEncoder::encode(floppytype, data, None, "") + MacFormatEncoder::encode(floppytype, data, None, filename.unwrap_or_default()) } }