Renamed CdRom to CdRomReader and moved it to it's own module.

This commit is contained in:
Filipe Rodrigues 2021-01-26 19:30:26 +00:00
parent beaab45aa4
commit dbb556f408
12 changed files with 106 additions and 99 deletions

View File

@ -58,79 +58,9 @@
// Modules
pub mod error;
pub mod iter;
pub mod reader;
pub mod sector;
// Exports
pub use error::{ReadNthSectorError, ReadSectorError, SeekSectorError};
pub use iter::SectorsRangeIter;
pub use reader::CdRomReader;
pub use sector::Sector;
// Imports
use dcb_bytes::Bytes;
use std::io::{Read, Seek, SeekFrom};
/// A CD-ROM/XA Mode 2 Form 1 wrapper
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct CdRom<R> {
/// Underlying reader
reader: R,
}
// Constants
impl<R> CdRom<R> {
/// Sector size
pub const SECTOR_SIZE: u64 = 2352;
}
// Constructors
impl<R> CdRom<R> {
/// Creates a new CD-ROM reader
#[must_use]
pub const fn new(reader: R) -> Self {
Self { reader }
}
}
// Read
impl<R: Read> CdRom<R> {
/// Reads the next sector
pub fn read_sector(&mut self) -> Result<Sector, ReadSectorError> {
// Read it
let mut bytes = [0; 2352];
self.reader.read_exact(&mut bytes).map_err(ReadSectorError::Read)?;
// And parse it
Sector::from_bytes(&bytes).map_err(ReadSectorError::Parse)
}
/// Returns an iterator over the next sectors
pub fn read_sectors(&mut self) -> SectorsRangeIter<R> {
SectorsRangeIter::new(self)
}
}
// Seek
impl<R: Seek> CdRom<R> {
/// Seeks to the `n`th sector
pub fn seek_sector(&mut self, n: u64) -> Result<(), SeekSectorError> {
// Seek to the sector.
match self.reader.seek(SeekFrom::Start(Self::SECTOR_SIZE * n)) {
Ok(_) => Ok(()),
Err(err) => Err(SeekSectorError { sector: n, err }),
}
}
}
// Seek + Read
impl<R: Read + Seek> CdRom<R> {
/// Reads the `n`th sector
pub fn read_nth_sector(&mut self, n: u64) -> Result<Sector, ReadNthSectorError> {
// Seek to the sector.
self.seek_sector(n).map_err(ReadNthSectorError::Seek)?;
// Then read it.
self.read_sector().map_err(ReadNthSectorError::ReadNext)
}
}

View File

@ -0,0 +1,78 @@
//! Reader
// Modules
pub mod error;
pub mod iter;
// Exports
pub use error::{ReadNthSectorError, ReadSectorError, SeekSectorError};
pub use iter::SectorsRangeIter;
// Imports
use crate::Sector;
use dcb_bytes::Bytes;
use std::io::{Read, Seek, SeekFrom};
/// A CD-ROM/XA Mode 2 Form 1 reader.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct CdRomReader<R> {
/// Underlying reader
reader: R,
}
// Constants
impl<R> CdRomReader<R> {
/// Sector size
pub const SECTOR_SIZE: u64 = 2352;
}
// Constructors
impl<R> CdRomReader<R> {
/// Creates a new CD-ROM reader
#[must_use]
pub const fn new(reader: R) -> Self {
Self { reader }
}
}
// Read
impl<R: Read> CdRomReader<R> {
/// Reads the next sector
pub fn read_sector(&mut self) -> Result<Sector, ReadSectorError> {
// Read it
let mut bytes = [0; 2352];
self.reader.read_exact(&mut bytes).map_err(ReadSectorError::Read)?;
// And parse it
Sector::from_bytes(&bytes).map_err(ReadSectorError::Parse)
}
/// Returns an iterator over the next sectors
pub fn read_sectors(&mut self) -> SectorsRangeIter<R> {
SectorsRangeIter::new(self)
}
}
// Seek
impl<R: Seek> CdRomReader<R> {
/// Seeks to the `n`th sector
pub fn seek_sector(&mut self, n: u64) -> Result<(), SeekSectorError> {
// Seek to the sector.
match self.reader.seek(SeekFrom::Start(Self::SECTOR_SIZE * n)) {
Ok(_) => Ok(()),
Err(err) => Err(SeekSectorError { sector: n, err }),
}
}
}
// Seek + Read
impl<R: Read + Seek> CdRomReader<R> {
/// Reads the `n`th sector
pub fn read_nth_sector(&mut self, n: u64) -> Result<Sector, ReadNthSectorError> {
// Seek to the sector.
self.seek_sector(n).map_err(ReadNthSectorError::Seek)?;
// Then read it.
self.read_sector().map_err(ReadNthSectorError::ReadNext)
}
}

View File

@ -1,7 +1,7 @@
//! Errors
// Imports
use super::sector;
use crate::sector;
/// Error type for [`CdRom::seek_sector`](super::CdRom::seek_sector)
#[derive(Debug, thiserror::Error)]

View File

@ -1,26 +1,25 @@
//! Iterators
// Imports
use super::{ReadSectorError, Sector};
use crate::CdRom;
use crate::{reader, CdRomReader, Sector};
use std::io;
/// Iterator over sectors
pub struct SectorsRangeIter<'a, R: io::Read> {
/// The cdrom
cdrom: &'a mut CdRom<R>,
cdrom: &'a mut CdRomReader<R>,
}
impl<'a, R: io::Read> SectorsRangeIter<'a, R> {
/// Creates a new sector range iterator
pub(crate) fn new(cdrom: &'a mut CdRom<R>) -> Self {
pub(crate) fn new(cdrom: &'a mut CdRomReader<R>) -> Self {
Self { cdrom }
}
}
impl<'a, R: io::Read> Iterator for SectorsRangeIter<'a, R> {
type Item = Result<Sector, ReadSectorError>;
type Item = Result<Sector, reader::ReadSectorError>;
fn next(&mut self) -> Option<Self::Item> {
// Read the next sector

View File

@ -10,14 +10,14 @@ use dcb_iso9660::entry::FileReader;
pub use error::{NewError, ReadFileError};
// Imports
use dcb_cdrom_xa::CdRom;
use dcb_cdrom_xa::CdRomReader;
use std::io;
/// Game file reader.
#[derive(PartialEq, Eq, Debug)]
pub struct GameFile<'a, R> {
/// CD-Rom
cdrom: &'a mut CdRom<R>,
cdrom: &'a mut CdRomReader<R>,
/// Iso9660 filesystem
filesystem: dcb_iso9660::Filesystem,
@ -26,7 +26,7 @@ pub struct GameFile<'a, R> {
// Constructors
impl<'a, R: io::Read + io::Seek> GameFile<'a, R> {
/// Creates a new game file from the cd reader
pub fn new(cdrom: &'a mut CdRom<R>) -> Result<Self, NewError> {
pub fn new(cdrom: &'a mut CdRomReader<R>) -> Result<Self, NewError> {
// Read the filesystem
let filesystem = dcb_iso9660::Filesystem::new(cdrom).map_err(NewError::ParseFilesystem)?;
@ -36,7 +36,7 @@ impl<'a, R: io::Read + io::Seek> GameFile<'a, R> {
impl<'a, R> GameFile<'a, R> {
/// Returns the cdrom associated with this game file
pub fn cdrom(&mut self) -> &mut CdRom<R> {
pub fn cdrom(&mut self) -> &mut CdRomReader<R> {
self.cdrom
}
}

View File

@ -12,7 +12,7 @@ pub use file::FileReader;
use super::string::FileString;
use crate::Dir;
use byteorder::{ByteOrder, LittleEndian};
use dcb_cdrom_xa::CdRom;
use dcb_cdrom_xa::CdRomReader;
use dcb_util::array_split;
use std::{convert::TryFrom, io};
@ -57,7 +57,7 @@ impl DirEntry {
}
/// Reads a file from this entry
pub fn read_file<'a, R: io::Read + io::Seek>(&self, cdrom: &'a mut CdRom<R>) -> Result<FileReader<'a, R>, ReadFileError> {
pub fn read_file<'a, R: io::Read + io::Seek>(&self, cdrom: &'a mut CdRomReader<R>) -> Result<FileReader<'a, R>, ReadFileError> {
// If this isn't a file, return Err
if !self.is_file() {
return Err(ReadFileError::NotAFile);
@ -73,7 +73,7 @@ impl DirEntry {
}
/// Reads a directory from this entry.
pub fn read_dir<R: io::Read + io::Seek>(&self, cdrom: &mut CdRom<R>) -> Result<Dir, ReadDirError> {
pub fn read_dir<R: io::Read + io::Seek>(&self, cdrom: &mut CdRomReader<R>) -> Result<Dir, ReadDirError> {
// If this isn't a directory, return Err
if !self.is_dir() {
return Err(ReadDirError::NotADirectory);

View File

@ -2,7 +2,7 @@
// Imports
use crate::string;
use dcb_cdrom_xa::{ReadNthSectorError, SeekSectorError};
use dcb_cdrom_xa::reader::{ReadNthSectorError, SeekSectorError};
use std::io;
/// Error type for [`Bytes::from_bytes`](dcb_bytes::Bytes::from_bytes)
@ -23,7 +23,7 @@ pub enum FromReaderError {
/// Unable to read header
#[error("Unable to read header")]
ReadName(#[source] io::Error),
/// Unable to read remaining
#[error("Unable to read remaining")]
ReadRemaining(#[source] io::Error),

View File

@ -1,14 +1,14 @@
//! File reader
// Imports
use dcb_cdrom_xa::{CdRom, Sector};
use dcb_cdrom_xa::{CdRomReader, Sector, reader::ReadSectorError};
use std::{convert::TryFrom, io};
/// A file reader
#[derive(PartialEq, Eq, Debug)]
pub struct FileReader<'a, R> {
/// Cd-rom reader
cdrom: &'a mut CdRom<R>,
cdrom: &'a mut CdRomReader<R>,
/// File sector
sector_pos: u64,
@ -30,7 +30,7 @@ impl<'a, R> FileReader<'a, R> {
///
/// # Note
/// Expects `cdrom` to be seeked to the start of the file.
pub(super) fn new(cdrom: &'a mut CdRom<R>, sector_pos: u64, size: u64) -> Self {
pub(super) fn new(cdrom: &'a mut CdRomReader<R>, sector_pos: u64, size: u64) -> Self {
Self {
cdrom,
sector_pos,
@ -57,8 +57,8 @@ impl<'a, R: io::Read> FileReader<'a, R> {
// Grab the next sector
let sector = self.cdrom.read_sector().map_err(|err| match err {
dcb_cdrom_xa::ReadSectorError::Read(err) => err,
dcb_cdrom_xa::ReadSectorError::Parse(err) => io::Error::new(io::ErrorKind::InvalidData, err),
ReadSectorError::Read(err) => err,
ReadSectorError::Parse(err) => io::Error::new(io::ErrorKind::InvalidData, err),
})?;
Ok(cached.get_or_insert(sector))

View File

@ -2,7 +2,7 @@
// Imports
use super::volume_descriptor::{self};
use dcb_cdrom_xa::{ReadSectorError, SeekSectorError};
use dcb_cdrom_xa::reader::{ReadSectorError, SeekSectorError};
/// Error type for [`Filesystem::new`](super::Filesystem::new)
#[derive(Debug, thiserror::Error)]

View File

@ -75,7 +75,7 @@ pub use volume_descriptor::VolumeDescriptor;
// Imports
use self::volume_descriptor::PrimaryVolumeDescriptor;
use dcb_bytes::Bytes;
use dcb_cdrom_xa::CdRom;
use dcb_cdrom_xa::CdRomReader;
use std::io;
/// The filesystem
@ -89,7 +89,7 @@ pub struct Filesystem {
impl Filesystem {
/// Reads the filesystem from the cd rom.
pub fn new<R: io::Read + io::Seek>(cdrom: &mut CdRom<R>) -> Result<Self, NewError> {
pub fn new<R: io::Read + io::Seek>(cdrom: &mut CdRomReader<R>) -> Result<Self, NewError> {
// Start reading volume descriptors from sector `0x10` until we hit the primary one
// Note: First `32 kiB` (= 16 sectors) are reserved for arbitrary data.
cdrom.seek_sector(0x10).map_err(NewError::SeekVolumeDescriptorSet)?;

View File

@ -79,7 +79,7 @@ mod logger;
use std::fmt;
use anyhow::Context;
use dcb_cdrom_xa::CdRom;
use dcb_cdrom_xa::CdRomReader;
use dcb_exe::{
exe::{
inst::{basic, pseudo, Directive, Inst, InstFmt, InstTarget, InstTargetFmt},
@ -99,7 +99,7 @@ fn main() -> Result<(), anyhow::Error> {
// Open the game file
let input_file = std::fs::File::open(&cli.game_file_path).context("Unable to open input file")?;
let mut cdrom = CdRom::new(input_file);
let mut cdrom = CdRomReader::new(input_file);
let mut game_file = GameFile::new(&mut cdrom).context("Unable to read game file")?;
// Read the executable

View File

@ -79,7 +79,7 @@ mod logger;
// Imports
use anyhow::Context;
use dcb_cdrom_xa::CdRom;
use dcb_cdrom_xa::CdRomReader;
use dcb_io::GameFile;
fn main() -> Result<(), anyhow::Error> {
@ -91,7 +91,7 @@ fn main() -> Result<(), anyhow::Error> {
// Open the game file
let input_file = std::fs::File::open(&game_file_path).context("Unable to open input file")?;
let mut cdrom = CdRom::new(input_file);
let mut cdrom = CdRomReader::new(input_file);
let _game_file = GameFile::new(&mut cdrom).context("Unable to read filesystem")?;
Ok(())