From 7d0f215f26e79b9dc867be521b05c7933e713a9f Mon Sep 17 00:00:00 2001 From: Dukantic Date: Fri, 11 Apr 2025 12:12:13 +0200 Subject: [PATCH] reorganisation of fonctions and file --- src/main.rs | 99 ++++++++++--------------------------------- src/node.rs | 12 ++++++ src/read_and_write.rs | 53 +++++++++++++++++++++++ 3 files changed, 88 insertions(+), 76 deletions(-) create mode 100644 src/read_and_write.rs diff --git a/src/main.rs b/src/main.rs index 61e0a76..c7bcb86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use std::collections::HashMap; use std::env; use std::fs::File; use std::io::prelude::*; @@ -6,9 +5,11 @@ use std::io::{Seek, SeekFrom}; mod byte_writer; mod node; +mod read_and_write; 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; @@ -45,7 +46,7 @@ fn main() -> std::io::Result<()> { after_compression.write_all(string.as_bytes())?; to_compress.seek(SeekFrom::Start(0))?; 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 len_body = trad.len() as u32; after_compression.write_all(&len_body.to_be_bytes())?; @@ -61,16 +62,11 @@ fn main() -> std::io::Result<()> { match file_name { Some(f) => { let mut to_print = File::open(f)?; - let mut mag_buf = [0u8]; - let _ = to_print.read_exact(&mut mag_buf); - match u8::from_be_bytes(mag_buf) { - 128 => { - let untranslate = string_translate(&mut to_print); - eprintln!("untranslate = {}", untranslate); - } - _ => { - println!("Error oppening file, check type file !") - } + if is_valid_file(&mut to_print) { + let untranslate = string_translate(&mut to_print); + eprintln!("untranslate = {}", untranslate); + } else { + println!("Error oppening file, check type file !") } } None => { @@ -85,19 +81,26 @@ fn main() -> std::io::Result<()> { Some(f) => { let mut to_uncomp = File::open(&f)?; let mut uncomp: File; - if let Some(str) = file_to_un { - uncomp = File::create(str)?; + if is_valid_file(&mut to_uncomp) { + if let Some(str) = file_to_un { + uncomp = File::create(str)?; + } else { + uncomp = File::create(f + ".txt")?; + } + let txt = string_translate(&mut to_uncomp); + uncomp.write_all(txt.as_bytes())?; } else { - uncomp = File::create(f + ".txt")?; + println!("Error oppening file, check type file !") } - let txt = string_translate(&mut to_uncomp); - uncomp.write_all(txt.as_bytes())?; } None => { println!("No File name !"); display_options(); } } + } else { + println!("Illegal arguments !"); + display_options(); } } None => { @@ -115,17 +118,6 @@ fn display_options() { println!("--print ,-p To print a file compressed."); } -fn get_map_file(mut file: &File) -> Result, std::io::Error> { - let mut hashmap: HashMap = 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 { let mut buffer = [0u8; 4]; let _ = file.read_exact(&mut buffer); @@ -144,52 +136,6 @@ fn string_translate(file: &mut File) -> String { translate_bin_char(file, root, len_body) } -fn nodes_at_vec(map: HashMap) -> Vec { - 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 { - let mut n = 0; - let mut out: Vec = 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 { - 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 { let mut txt = Vec::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 { diff --git a/src/node.rs b/src/node.rs index a38a3ed..3b447eb 100644 --- a/src/node.rs +++ b/src/node.rs @@ -165,3 +165,15 @@ pub fn read_tree(binary: &str, chars: &Vec) -> Node { } root } +pub fn nodes_at_vec(map: HashMap) -> Vec { + 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 +} diff --git a/src/read_and_write.rs b/src/read_and_write.rs new file mode 100644 index 0000000..9a740b3 --- /dev/null +++ b/src/read_and_write.rs @@ -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 { + let mut n = 0; + let mut out: Vec = 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, std::io::Error> { + let mut hashmap: HashMap = 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 { + 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 +}