Created
December 15, 2017 11:06
-
-
Save uenoku/7dd1ffb89a4fdf3e81be69ef7def5122 to your computer and use it in GitHub Desktop.
Rustでウェザコン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::{stdin, Read, Write}; | |
use std::str::FromStr; | |
#[allow(unused_imports)] | |
use std::{cmp, mem, process}; | |
#[allow(unused_imports)] | |
use std::collections::{HashSet, BTreeMap, BTreeSet}; | |
#[allow(unused_imports)] | |
use std::fs::File; | |
macro_rules! eprintln( | |
($($arg:tt)*) => { { | |
let r = writeln!(&mut ::std::io::stderr(), $($arg)*); | |
r.expect("failed printing to stderr"); | |
} } | |
); | |
#[allow(dead_code)] | |
struct Dat { | |
zip_file: String, | |
dir: String, | |
n: usize, | |
files: Vec<(String, usize)>, | |
} | |
fn input_decode() -> Dat { | |
let zip_file: String = read(); | |
let dir: String = read(); | |
Dat { | |
zip_file: zip_file, | |
dir: dir, | |
n: 0, | |
files: Vec::new(), | |
} | |
} | |
fn input_encode() -> Dat { | |
let zip_file: String = read(); | |
let dir: String = read(); | |
let n: usize = read(); | |
let mut files = Vec::<(String, usize)>::new(); | |
for _ in 0..n { | |
let tp: (String, usize) = (read(), read()); | |
files.push(tp); | |
} | |
Dat { | |
zip_file: zip_file, | |
dir: dir, | |
n: n, | |
files: files, | |
} | |
} | |
fn compress(buf: Vec<u8>) -> Vec<u8> { | |
buf | |
} | |
fn decompress(buf: Vec<u8>) -> Vec<u8> { | |
buf | |
} | |
fn encode() { | |
let dat: Dat = input_encode(); | |
if let Some(mut out) = File::create(dat.zip_file).ok() { | |
write_fs_8bytes(&mut out, dat.n); | |
for (file, size) in dat.files { | |
let full_path = format!("{}/{}", dat.dir, file); | |
if let Some(mut fs) = File::open(full_path).ok() { | |
let mut buf = Vec::<u8>::new(); | |
fs.read_to_end(&mut buf).ok(); | |
let bytes = file.into_bytes(); | |
write_fs_8bytes(&mut out, bytes.len()); | |
out.write(&bytes).ok(); | |
let buf = compress(buf); | |
write_fs_8bytes(&mut out, buf.len()); | |
out.write(&buf).ok(); | |
} | |
} | |
} | |
} | |
fn to_usize(buf: [u8; 8]) -> usize { | |
let mut v: usize = 0; | |
for i in 0..8 { | |
let j = buf[7 - i] as usize; | |
v = j + 256 * v; | |
} | |
v | |
} | |
fn write_fs_8bytes(out: &mut File, a: usize) { | |
let raw_bytes: [u8; 8] = unsafe { std::mem::transmute(a) }; | |
out.write(&raw_bytes).ok(); | |
} | |
fn read_fs_8bytes(zip_file: &mut File) -> usize { | |
let mut buf = [0; 8]; | |
zip_file.read(&mut buf).ok(); | |
to_usize(buf) | |
} | |
fn decode() { | |
let dat: Dat = input_decode(); | |
if let Some(mut zip_file) = File::open(dat.zip_file).ok() { | |
let n = read_fs_8bytes(&mut zip_file); | |
for _ in 0..n { | |
let name_len = read_fs_8bytes(&mut zip_file); | |
let mut buf = vec![0u8; name_len]; | |
zip_file.read(&mut buf).ok(); | |
let name = String::from_utf8(buf).ok().unwrap(); | |
let sz = read_fs_8bytes(&mut zip_file); | |
let mut buf = vec![0u8; sz]; | |
zip_file.read(&mut buf).ok(); | |
let file_path = format!("{}/{}", dat.dir, name); | |
if let Some(mut out) = File::create(file_path).ok() { | |
out.write(&buf).ok(); | |
} | |
} | |
} | |
} | |
fn main() { | |
let phase: String = read(); | |
if phase == "encode" { | |
encode(); | |
} else if phase == "decode" { | |
decode(); | |
} | |
} | |
#[allow(dead_code)] | |
fn read_vec<T: FromStr>(n: usize) -> Vec<T> { | |
let mut v = vec![]; | |
for _ in 0..n { | |
let a = read::<T>(); | |
v.push(a); | |
} | |
v | |
} | |
#[allow(dead_code)] | |
fn read_str() -> Vec<char> { | |
let s: String = read(); | |
s.chars().collect::<Vec<char>>() | |
} | |
#[allow(dead_code)] | |
fn read<T: FromStr>() -> T { | |
let stdin = stdin(); | |
let stdin = stdin.lock(); | |
let s = stdin | |
.bytes() | |
.map(|c| c.unwrap() as char) | |
.skip_while(|c| c.is_whitespace()) | |
.take_while(|c| !c.is_whitespace()) | |
.collect::<String>(); | |
s.parse::<T>().unwrap_or_else( | |
|_| panic!("Faild to parse {}", s), | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment