Skip to content

Commit

Permalink
Show filename as title if image has no title
Browse files Browse the repository at this point in the history
  • Loading branch information
twvd committed Nov 12, 2024
1 parent 711c8fe commit 0c01259
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 29 deletions.
7 changes: 5 additions & 2 deletions floppy/src/loaders/a2r2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl A2Rv2 {
}

impl FloppyImageLoader for A2Rv2 {
fn load(data: &[u8]) -> Result<FloppyImage> {
fn load(data: &[u8], filename: Option<&str>) -> Result<FloppyImage> {
let mut cursor = Cursor::new(data);
let _header = A2RHeader::read(&mut cursor)?;

Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions floppy/src/loaders/a2r3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl A2Rv3 {
}

impl FloppyImageLoader for A2Rv3 {
fn load(data: &[u8]) -> Result<FloppyImage> {
fn load(data: &[u8], filename: Option<&str>) -> Result<FloppyImage> {
let mut cursor = Cursor::new(data);
let _header = A2RHeader::read(&mut cursor)?;

Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions floppy/src/loaders/auto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ impl Autodetect {
}

impl FloppyImageLoader for Autodetect {
fn load(data: &[u8]) -> Result<FloppyImage> {
fn load(data: &[u8], filename: Option<&str>) -> Result<FloppyImage> {
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),
}
}
}
4 changes: 2 additions & 2 deletions floppy/src/loaders/bitfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ impl Bitfile {
}

impl FloppyImageLoader for Bitfile {
fn load(data: &[u8]) -> Result<FloppyImage> {
fn load(data: &[u8], filename: Option<&str>) -> Result<FloppyImage> {
let tracks = Self::count_tracks(data)?;
let mut image = FloppyImage::new_empty(
match tracks {
80 => FloppyType::Mac400K,
160 => FloppyType::Mac800K,
_ => bail!("Invalid amount of tracks: {}", tracks),
},
"",
filename.unwrap_or_default(),
);

let mut offset = 0;
Expand Down
11 changes: 8 additions & 3 deletions floppy/src/loaders/diskcopy42.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,9 +89,14 @@ impl Dc42Raw {
pub struct Diskcopy42 {}

impl FloppyImageLoader for Diskcopy42 {
fn load(data: &[u8]) -> anyhow::Result<crate::FloppyImage> {
fn load(data: &[u8], filename: Option<&str>) -> Result<FloppyImage> {
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() {
Expand All @@ -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)
}
}
9 changes: 7 additions & 2 deletions floppy/src/loaders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<FloppyImage>;
fn load(data: &[u8], filename: Option<&str>) -> Result<FloppyImage>;

fn load_file(filename: &str) -> Result<FloppyImage> {
Self::load(&std::fs::read(filename)?)
Self::load(
&std::fs::read(filename)?,
Path::new(filename).file_name().and_then(|s| s.to_str()),
)
}
}

Expand Down
7 changes: 5 additions & 2 deletions floppy/src/loaders/moof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Moof {
}

impl FloppyImageLoader for Moof {
fn load(data: &[u8]) -> Result<FloppyImage> {
fn load(data: &[u8], filename: Option<&str>) -> Result<FloppyImage> {
let mut cursor = Cursor::new(data);
let header = MoofHeader::read(&mut cursor)?;
let checksum = crc::Crc::<u32>::new(&crc::CRC_32_ISO_HDLC).checksum(&data[12..]);
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions floppy/src/loaders/pfi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct PayloadTrack {
pub struct PFI {}

impl FloppyImageLoader for PFI {
fn load(data: &[u8]) -> Result<FloppyImage> {
fn load(data: &[u8], filename: Option<&str>) -> Result<FloppyImage> {
let mut cursor = Cursor::new(data);

let mut tracks: HashMap<(usize, usize), &[u8]> = HashMap::new();
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions floppy/src/loaders/raw.rs
Original file line number Diff line number Diff line change
@@ -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<crate::FloppyImage> {
fn load(data: &[u8], filename: Option<&str>) -> Result<FloppyImage> {
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())
}
}

0 comments on commit 0c01259

Please sign in to comment.