From 7bc57558d057555ec18fb4768a7ce262c997ab84 Mon Sep 17 00:00:00 2001 From: Jaby Date: Tue, 18 Oct 2022 21:00:20 +0200 Subject: [PATCH] Prepare bin/cue writer --- src/Tools/psxcdgen_ex/planschi.bin | 0 src/Tools/psxcdgen_ex/src/encoder/mod.rs | 1 + src/Tools/psxcdgen_ex/src/encoder/psx.rs | 0 .../psxcdgen_ex/src/file_writer/bin_cue.rs | 25 +++++++++++++++++++ src/Tools/psxcdgen_ex/src/file_writer/mod.rs | 25 +++++++++++++++++++ src/Tools/psxcdgen_ex/src/lib.rs | 1 + src/Tools/psxcdgen_ex/src/main.rs | 5 ++-- src/Tools/tool_helper/src/lib.rs | 20 +++++++++++---- 8 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 src/Tools/psxcdgen_ex/planschi.bin create mode 100644 src/Tools/psxcdgen_ex/src/encoder/mod.rs create mode 100644 src/Tools/psxcdgen_ex/src/encoder/psx.rs create mode 100644 src/Tools/psxcdgen_ex/src/file_writer/bin_cue.rs create mode 100644 src/Tools/psxcdgen_ex/src/file_writer/mod.rs diff --git a/src/Tools/psxcdgen_ex/planschi.bin b/src/Tools/psxcdgen_ex/planschi.bin new file mode 100644 index 00000000..e69de29b diff --git a/src/Tools/psxcdgen_ex/src/encoder/mod.rs b/src/Tools/psxcdgen_ex/src/encoder/mod.rs new file mode 100644 index 00000000..2064fdc7 --- /dev/null +++ b/src/Tools/psxcdgen_ex/src/encoder/mod.rs @@ -0,0 +1 @@ +pub mod psx; \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/encoder/psx.rs b/src/Tools/psxcdgen_ex/src/encoder/psx.rs new file mode 100644 index 00000000..e69de29b diff --git a/src/Tools/psxcdgen_ex/src/file_writer/bin_cue.rs b/src/Tools/psxcdgen_ex/src/file_writer/bin_cue.rs new file mode 100644 index 00000000..c886f8f8 --- /dev/null +++ b/src/Tools/psxcdgen_ex/src/file_writer/bin_cue.rs @@ -0,0 +1,25 @@ +use std::io::Write; +use super::{Error, SectorWriter}; + +pub struct BinCueWriter { + _bin_out: std::boxed::Box, + sector_count: usize +} + +impl BinCueWriter { + pub fn new(writer: T) -> BinCueWriter { + BinCueWriter{_bin_out: std::boxed::Box::new(writer), sector_count: 0} + } +} + +impl SectorWriter for BinCueWriter { + fn write(&mut self) -> Result<(), Error> { + self.sector_count += 1; + + Err(Error::not_implemented("write for BinCueWriter")) + } + + fn sector_written_count(&self) -> usize { + self.sector_count + } +} \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/file_writer/mod.rs b/src/Tools/psxcdgen_ex/src/file_writer/mod.rs new file mode 100644 index 00000000..08a6f6fa --- /dev/null +++ b/src/Tools/psxcdgen_ex/src/file_writer/mod.rs @@ -0,0 +1,25 @@ +pub mod bin_cue; + +use bin_cue::BinCueWriter; +use super::types::CDDesc; +use std::path::PathBuf; +use tool_helper::{Error, open_output_file}; + +pub enum ImageType { + BinCue +} + +pub trait SectorWriter { + fn write(&mut self) -> Result<(), Error>; + fn sector_written_count(&self) -> usize; +} + +pub fn write_image(_cd_desc: CDDesc, image_type: ImageType, output_path: PathBuf) -> Result<(), Error> { + match image_type { + ImageType::BinCue => { + let mut _writer = BinCueWriter::new(open_output_file(output_path)?); + + Ok(()) + } + } +} \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/lib.rs b/src/Tools/psxcdgen_ex/src/lib.rs index dd198c6d..c05ae5b6 100644 --- a/src/Tools/psxcdgen_ex/src/lib.rs +++ b/src/Tools/psxcdgen_ex/src/lib.rs @@ -1 +1,2 @@ +pub mod file_writer; pub mod types; \ No newline at end of file diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index 9fe3cac2..1b82b6d8 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -1,4 +1,5 @@ -use psxcdgen_ex::types::{layout::Layout, CDDesc, File, Directory}; +use psxcdgen_ex::{file_writer::{ImageType, write_image}, types::{layout::Layout, CDDesc, File, Directory}}; +use std::{path::PathBuf, str::FromStr}; use tool_helper::Error; fn make_file(name: &str) -> Result { @@ -53,7 +54,7 @@ fn run_main() -> Result<(), Error> { println!("{}", rand_item.0); } - Ok(()) + write_image(desc, ImageType::BinCue, PathBuf::from_str("planschi.bin")?) } fn main() { diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 989adaf3..17261909 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -1,4 +1,4 @@ -use std::{boxed::Box, io::{Read, Write}, path::PathBuf}; +use std::{boxed::Box, io::{BufReader, BufWriter, Read, Write}, path::PathBuf}; pub mod bits; pub mod raw; @@ -80,17 +80,27 @@ impl std::convert::From for Error { } } +impl std::convert::From for Error { + fn from(error: std::convert::Infallible) -> Self { + Error::from_error(error) + } +} + +pub fn open_output_file(output_path: PathBuf) -> Result, Error> { + Ok(std::io::BufWriter::new(std::fs::File::create(output_path)?)) +} + pub fn open_output(output_file: Option) -> Result { match output_file { - Some(output_path) => Ok(Box::new(std::io::BufWriter::new(std::fs::File::create(output_path)?))), - None => Ok(Box::new(std::io::BufWriter::new(std::io::stdout()))), + Some(output_path) => Ok(Box::new(open_output_file(output_path)?)), + None => Ok(Box::new(BufWriter::new(std::io::stdout()))), } } pub fn open_input(input_file: Option) -> Result { match input_file { - Some(input_path) => Ok(Box::new(std::io::BufReader::new(std::fs::File::open(input_path)?))), - None => Ok(Box::new(std::io::BufReader::new(std::io::stdin()))), + Some(input_path) => Ok(Box::new(BufReader::new(std::fs::File::open(input_path)?))), + None => Ok(Box::new(BufReader::new(std::io::stdin()))), } }