diff --git a/src/bit_buffer.rs b/src/bit_buffer.rs index a7ab2ac..c0eb5e0 100644 --- a/src/bit_buffer.rs +++ b/src/bit_buffer.rs @@ -76,7 +76,6 @@ impl BitReader { } else if self.repeat_bits <= 0 { return Err(io::Error::other("No more bits")); } else { - println!("{}", self.repeat_bits); self.repeat_bits -= 8; } self.next = 1 << 7; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..bd83bbc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,51 @@ +use std::{ + env, + fs::File, + io::{self, BufReader, Read}, + path::Path, +}; + +use sac::modelA::ModelA; + +enum Mode { + Compress, + Decompress, +} +type CodeValue = u32; + +fn get_file>(filepath: FilePath) -> io::Result> { + let f = File::open(&filepath)?; + Ok(Box::new(BufReader::new(f))) +} + +fn main() -> io::Result<()> { + let args: Vec = env::args().collect(); + let name = &args[0]; + + let (input, mode): (Box, Mode) = match args.len() { + 2 if args[1].to_lowercase() == "compress" => (Box::new(io::stdin()), Mode::Compress), + 2 if args[1].to_lowercase() == "decompress" => (Box::new(io::stdin()), Mode::Decompress), + + 3 if args[1].to_lowercase() == "compress" => (get_file(&args[2])?, Mode::Compress), + 3 if args[1].to_lowercase() == "decompress" => (get_file(&args[2])?, Mode::Decompress), + + _ => { + eprintln!("Usage:"); + eprintln!("{name} compress file > output # compress file and save to output"); + eprintln!("{name} decompress file > output # decompress file and save to output"); + return Err(io::Error::other(format!( + "Invalid command `{}`", + args.join(" ") + ))); + } + }; + + let model: ModelA = ModelA::default(); + let mut output = io::stdout().lock(); + match mode { + Mode::Compress => model.compress(input, &mut output)?, + Mode::Decompress => model.decompress(input, &mut output)?, + }; + + Ok(()) +}