| 1 |
#![forbid(unsafe_code)] |
| 2 |
use std::path::PathBuf; |
| 3 |
use std::{env, ffi, fs, io, process}; |
| 4 |
|
| 5 |
extern crate weezl; |
| 6 |
use weezl::{decode as delzw, encode as enlzw, BitOrder}; |
| 7 |
|
| 8 |
fn main() { |
| 9 |
let args = env::args_os().skip(1); |
| 10 |
let flags = Flags::from_args(args).unwrap_or_else(|ParamError| explain()); |
| 11 |
|
| 12 |
let out = io::stdout(); |
| 13 |
let out = out.lock(); |
| 14 |
|
| 15 |
let mut files = flags.files; |
| 16 |
let input = files.pop().unwrap_or_else(explain); |
| 17 |
if !files.is_empty() { |
| 18 |
return explain(); |
| 19 |
} |
| 20 |
let operation = flags.operation.unwrap_or_else(explain); |
| 21 |
let min_code = if flags.min_code < 2 || flags.min_code > 12 { |
| 22 |
return explain(); |
| 23 |
} else { |
| 24 |
flags.min_code |
| 25 |
}; |
| 26 |
let bit_order = flags.bit_order; |
| 27 |
|
| 28 |
let result = match (input, operation) { |
| 29 |
(Input::File(file), Operation::Encode) => (|| { |
| 30 |
let data = fs::File::open(file)?; |
| 31 |
let file = io::BufReader::with_capacity(1 << 26, data); |
| 32 |
|
| 33 |
let mut encoder = enlzw::Encoder::new(bit_order, min_code); |
| 34 |
encoder.into_stream(out).encode_all(file).status |
| 35 |
})(), |
| 36 |
(Input::Stdin, Operation::Encode) => { |
| 37 |
let input = io::BufReader::with_capacity(1 << 26, io::stdin()); |
| 38 |
let mut encoder = enlzw::Encoder::new(bit_order, min_code); |
| 39 |
encoder.into_stream(out).encode_all(input).status |
| 40 |
} |
| 41 |
(Input::File(file), Operation::Decode) => (|| { |
| 42 |
let data = fs::File::open(file)?; |
| 43 |
let file = io::BufReader::with_capacity(1 << 26, data); |
| 44 |
|
| 45 |
let mut decoder = delzw::Decoder::new(bit_order, min_code); |
| 46 |
decoder.into_stream(out).decode_all(file).status |
| 47 |
})(), |
| 48 |
(Input::Stdin, Operation::Decode) => { |
| 49 |
let input = io::BufReader::with_capacity(1 << 26, io::stdin()); |
| 50 |
let mut decoder = delzw::Decoder::new(bit_order, min_code); |
| 51 |
decoder.into_stream(out).decode_all(input).status |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
result.expect("Operation Failed: "); |
| 56 |
} |
| 57 |
|
| 58 |
struct Flags { |
| 59 |
files: Vec<Input>, |
| 60 |
operation: Option<Operation>, |
| 61 |
min_code: u8, |
| 62 |
bit_order: BitOrder, |
| 63 |
} |
| 64 |
|
| 65 |
struct ParamError; |
| 66 |
|
| 67 |
enum Input { |
| 68 |
File(PathBuf), |
| 69 |
Stdin, |
| 70 |
} |
| 71 |
|
| 72 |
enum Operation { |
| 73 |
Encode, |
| 74 |
Decode, |
| 75 |
} |
| 76 |
|
| 77 |
fn explain<T>() -> T { |
| 78 |
println!( |
| 79 |
"Usage: lzw [-e|-d] <file>\n\ |
| 80 |
Arguments:\n\ |
| 81 |
-e\t operation encode (default)\n\ |
| 82 |
-d\t operation decode\n\ |
| 83 |
<file>\tfilepath or '-' for stdin" |
| 84 |
); |
| 85 |
process::exit(1); |
| 86 |
} |
| 87 |
|
| 88 |
impl Default for Flags { |
| 89 |
fn default() -> Flags { |
| 90 |
Flags { |
| 91 |
files: vec![], |
| 92 |
operation: None, |
| 93 |
min_code: 8, |
| 94 |
bit_order: BitOrder::Msb, |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
impl Flags { |
| 100 |
fn from_args(mut args: impl Iterator<Item = ffi::OsString>) -> Result<Self, ParamError> { |
| 101 |
let mut flags = Flags::default(); |
| 102 |
let mut operation = None; |
| 103 |
loop { |
| 104 |
match args.next().as_ref().and_then(|s| s.to_str()) { |
| 105 |
Some("-d") | Some("--decode") => { |
| 106 |
if operation.is_some() { |
| 107 |
return Err(ParamError); |
| 108 |
} |
| 109 |
operation = Some(Operation::Decode); |
| 110 |
} |
| 111 |
Some("-e") | Some("--encode") => { |
| 112 |
if operation.is_some() { |
| 113 |
return Err(ParamError); |
| 114 |
} |
| 115 |
operation = Some(Operation::Encode); |
| 116 |
} |
| 117 |
Some("-w") | Some("--word-bits") => match args.next() { |
| 118 |
None => return Err(ParamError), |
| 119 |
Some(bits) => { |
| 120 |
let st = bits.to_str().ok_or(ParamError)?; |
| 121 |
flags.min_code = st.parse().ok().ok_or(ParamError)?; |
| 122 |
} |
| 123 |
}, |
| 124 |
Some("-le") | Some("--little-endian") => { |
| 125 |
flags.bit_order = BitOrder::Lsb; |
| 126 |
} |
| 127 |
Some("-be") | Some("--big-endian") | Some("-ne") | Some("--network-endian") => { |
| 128 |
flags.bit_order = BitOrder::Msb; |
| 129 |
} |
| 130 |
Some("-") => { |
| 131 |
flags.files.push(Input::Stdin); |
| 132 |
} |
| 133 |
Some(other) if other.starts_with('-') => { |
| 134 |
// Reserved for future use. |
| 135 |
// -a: self-describing archive format, similar to actual compress |
| 136 |
// -b: maximum bits |
| 137 |
// -v: verbosity |
| 138 |
// some compress compatibility mode? Probably through arg(0) though. |
| 139 |
return Err(ParamError); |
| 140 |
} |
| 141 |
Some(file) => { |
| 142 |
flags.files.push(Input::File(file.into())); |
| 143 |
} |
| 144 |
None => break, |
| 145 |
}; |
| 146 |
} |
| 147 |
|
| 148 |
flags.files.extend(args.map(|file| { |
| 149 |
if let Some("-") = file.to_str() { |
| 150 |
Input::Stdin |
| 151 |
} else { |
| 152 |
Input::File(file.into()) |
| 153 |
} |
| 154 |
})); |
| 155 |
|
| 156 |
flags.operation = operation; |
| 157 |
Ok(flags) |
| 158 |
} |
| 159 |
} |