reorganisation of fonctions and file
This commit is contained in:
83
src/main.rs
83
src/main.rs
@@ -1,4 +1,3 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
@@ -6,9 +5,11 @@ use std::io::{Seek, SeekFrom};
|
|||||||
|
|
||||||
mod byte_writer;
|
mod byte_writer;
|
||||||
mod node;
|
mod node;
|
||||||
|
mod read_and_write;
|
||||||
|
|
||||||
use byte_writer::{binary_to_string, string_to_binary};
|
use byte_writer::{binary_to_string, string_to_binary};
|
||||||
use node::{Node, create_tree, read_tree, tree_to_map, tree_to_string};
|
use node::{Node, create_tree, nodes_at_vec, read_tree, tree_to_map, tree_to_string};
|
||||||
|
use read_and_write::{body_to_bin, get_map_file, is_valid_file, read_n_bytes};
|
||||||
|
|
||||||
const MAGIC_NUMBER: u8 = 128;
|
const MAGIC_NUMBER: u8 = 128;
|
||||||
|
|
||||||
@@ -45,7 +46,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
after_compression.write_all(string.as_bytes())?;
|
after_compression.write_all(string.as_bytes())?;
|
||||||
to_compress.seek(SeekFrom::Start(0))?;
|
to_compress.seek(SeekFrom::Start(0))?;
|
||||||
let map = tree_to_map(&mut tree, true);
|
let map = tree_to_map(&mut tree, true);
|
||||||
let trad = translate_char_bin(&mut to_compress, map);
|
let trad = body_to_bin(&mut to_compress, map);
|
||||||
let trad_bin = string_to_binary(&trad);
|
let trad_bin = string_to_binary(&trad);
|
||||||
let len_body = trad.len() as u32;
|
let len_body = trad.len() as u32;
|
||||||
after_compression.write_all(&len_body.to_be_bytes())?;
|
after_compression.write_all(&len_body.to_be_bytes())?;
|
||||||
@@ -61,18 +62,13 @@ fn main() -> std::io::Result<()> {
|
|||||||
match file_name {
|
match file_name {
|
||||||
Some(f) => {
|
Some(f) => {
|
||||||
let mut to_print = File::open(f)?;
|
let mut to_print = File::open(f)?;
|
||||||
let mut mag_buf = [0u8];
|
if is_valid_file(&mut to_print) {
|
||||||
let _ = to_print.read_exact(&mut mag_buf);
|
|
||||||
match u8::from_be_bytes(mag_buf) {
|
|
||||||
128 => {
|
|
||||||
let untranslate = string_translate(&mut to_print);
|
let untranslate = string_translate(&mut to_print);
|
||||||
eprintln!("untranslate = {}", untranslate);
|
eprintln!("untranslate = {}", untranslate);
|
||||||
}
|
} else {
|
||||||
_ => {
|
|
||||||
println!("Error oppening file, check type file !")
|
println!("Error oppening file, check type file !")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
None => {
|
None => {
|
||||||
println!("No file name!");
|
println!("No file name!");
|
||||||
display_options();
|
display_options();
|
||||||
@@ -85,6 +81,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
Some(f) => {
|
Some(f) => {
|
||||||
let mut to_uncomp = File::open(&f)?;
|
let mut to_uncomp = File::open(&f)?;
|
||||||
let mut uncomp: File;
|
let mut uncomp: File;
|
||||||
|
if is_valid_file(&mut to_uncomp) {
|
||||||
if let Some(str) = file_to_un {
|
if let Some(str) = file_to_un {
|
||||||
uncomp = File::create(str)?;
|
uncomp = File::create(str)?;
|
||||||
} else {
|
} else {
|
||||||
@@ -92,12 +89,18 @@ fn main() -> std::io::Result<()> {
|
|||||||
}
|
}
|
||||||
let txt = string_translate(&mut to_uncomp);
|
let txt = string_translate(&mut to_uncomp);
|
||||||
uncomp.write_all(txt.as_bytes())?;
|
uncomp.write_all(txt.as_bytes())?;
|
||||||
|
} else {
|
||||||
|
println!("Error oppening file, check type file !")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
println!("No File name !");
|
println!("No File name !");
|
||||||
display_options();
|
display_options();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
println!("Illegal arguments !");
|
||||||
|
display_options();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
@@ -115,17 +118,6 @@ fn display_options() {
|
|||||||
println!("--print ,-p <input> To print a file compressed.");
|
println!("--print ,-p <input> To print a file compressed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_map_file(mut file: &File) -> Result<HashMap<char, i32>, std::io::Error> {
|
|
||||||
let mut hashmap: HashMap<char, i32> = HashMap::new();
|
|
||||||
let mut contents = String::new();
|
|
||||||
file.read_to_string(&mut contents)?;
|
|
||||||
for c in contents.chars() {
|
|
||||||
let counter = hashmap.entry(c).or_insert(0);
|
|
||||||
*counter += 1;
|
|
||||||
}
|
|
||||||
Ok(hashmap)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn string_translate(file: &mut File) -> String {
|
fn string_translate(file: &mut File) -> String {
|
||||||
let mut buffer = [0u8; 4];
|
let mut buffer = [0u8; 4];
|
||||||
let _ = file.read_exact(&mut buffer);
|
let _ = file.read_exact(&mut buffer);
|
||||||
@@ -144,52 +136,6 @@ fn string_translate(file: &mut File) -> String {
|
|||||||
translate_bin_char(file, root, len_body)
|
translate_bin_char(file, root, len_body)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn nodes_at_vec(map: HashMap<char, i32>) -> Vec<Node> {
|
|
||||||
let mut nodes = Vec::new();
|
|
||||||
|
|
||||||
for (key, value) in &map {
|
|
||||||
let mut node = Node::new();
|
|
||||||
node.set_char(*key);
|
|
||||||
node.set_counter(*value);
|
|
||||||
nodes.push(node);
|
|
||||||
}
|
|
||||||
nodes.sort();
|
|
||||||
//nodes.sort_by(|a, b| a.val.cmp(&b.val));
|
|
||||||
nodes
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_n_bytes(file: &mut File, len: u32) -> Vec<u8> {
|
|
||||||
let mut n = 0;
|
|
||||||
let mut out: Vec<u8> = Vec::new();
|
|
||||||
let mut buffer = vec![0u8; 1];
|
|
||||||
while n < len {
|
|
||||||
let read = file.read_exact(&mut buffer);
|
|
||||||
match read {
|
|
||||||
Ok(_c) => {
|
|
||||||
out.push(buffer[0]);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
println!("Problem {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
n += 8;
|
|
||||||
}
|
|
||||||
out
|
|
||||||
}
|
|
||||||
|
|
||||||
fn translate_char_bin(file: &mut File, map: HashMap<String, String>) -> String {
|
|
||||||
let mut txt = String::new();
|
|
||||||
let mut translate = String::new();
|
|
||||||
|
|
||||||
let _ = file.read_to_string(&mut txt);
|
|
||||||
for c in txt.chars() {
|
|
||||||
if let Some(code) = map.get(&c.to_string()) {
|
|
||||||
translate.push_str(code);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
translate
|
|
||||||
}
|
|
||||||
|
|
||||||
fn translate_bin_char(file: &mut File, root: Node, len: u32) -> String {
|
fn translate_bin_char(file: &mut File, root: Node, len: u32) -> String {
|
||||||
let mut txt = Vec::new();
|
let mut txt = Vec::new();
|
||||||
let mut translate = String::new();
|
let mut translate = String::new();
|
||||||
@@ -210,7 +156,8 @@ fn translate_bin_char(file: &mut File, root: Node, len: u32) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("I see you, you try to tinker with things but it breaks everything");
|
println!("Error !");
|
||||||
|
return String::new();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(v) = current.val {
|
if let Some(v) = current.val {
|
||||||
|
|||||||
12
src/node.rs
12
src/node.rs
@@ -165,3 +165,15 @@ pub fn read_tree(binary: &str, chars: &Vec<char>) -> Node {
|
|||||||
}
|
}
|
||||||
root
|
root
|
||||||
}
|
}
|
||||||
|
pub fn nodes_at_vec(map: HashMap<char, i32>) -> Vec<Node> {
|
||||||
|
let mut nodes = Vec::new();
|
||||||
|
|
||||||
|
for (key, value) in &map {
|
||||||
|
let mut node = Node::new();
|
||||||
|
node.set_char(*key);
|
||||||
|
node.set_counter(*value);
|
||||||
|
nodes.push(node);
|
||||||
|
}
|
||||||
|
nodes.sort();
|
||||||
|
nodes
|
||||||
|
}
|
||||||
|
|||||||
53
src/read_and_write.rs
Normal file
53
src/read_and_write.rs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
|
||||||
|
pub fn read_n_bytes(file: &mut File, len: u32) -> Vec<u8> {
|
||||||
|
let mut n = 0;
|
||||||
|
let mut out: Vec<u8> = Vec::new();
|
||||||
|
let mut buffer = vec![0u8; 1];
|
||||||
|
while n < len {
|
||||||
|
let read = file.read_exact(&mut buffer);
|
||||||
|
match read {
|
||||||
|
Ok(_c) => {
|
||||||
|
out.push(buffer[0]);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
println!("Problem {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
n += 8;
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_map_file(mut file: &File) -> Result<HashMap<char, i32>, std::io::Error> {
|
||||||
|
let mut hashmap: HashMap<char, i32> = HashMap::new();
|
||||||
|
let mut contents = String::new();
|
||||||
|
file.read_to_string(&mut contents)?;
|
||||||
|
for c in contents.chars() {
|
||||||
|
let counter = hashmap.entry(c).or_insert(0);
|
||||||
|
*counter += 1;
|
||||||
|
}
|
||||||
|
Ok(hashmap)
|
||||||
|
}
|
||||||
|
pub fn is_valid_file(file: &mut File) -> bool {
|
||||||
|
let mut mag_buf = [0u8];
|
||||||
|
let _ = file.read_exact(&mut mag_buf);
|
||||||
|
match u8::from_be_bytes(mag_buf) {
|
||||||
|
128 => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn body_to_bin(file: &mut File, map: HashMap<String, String>) -> String {
|
||||||
|
let mut txt = String::new();
|
||||||
|
let mut translate = String::new();
|
||||||
|
|
||||||
|
let _ = file.read_to_string(&mut txt);
|
||||||
|
for c in txt.chars() {
|
||||||
|
if let Some(code) = map.get(&c.to_string()) {
|
||||||
|
translate.push_str(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
translate
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user