translate work

This commit is contained in:
2025-04-09 00:57:43 +02:00
parent ea563518fd
commit d286ef66c1
4 changed files with 797 additions and 7 deletions

View File

@@ -2,12 +2,13 @@ use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::io::{Seek, SeekFrom};
mod byte_writer;
mod node;
use byte_writer::{binary_to_string, string_to_binary};
use node::{Node, create_tree, read_tree, tree_to_string};
use node::{Node, create_tree, read_tree, tree_to_map, tree_to_string};
fn main() -> std::io::Result<()> {
let args: Vec<String> = env::args().collect();
@@ -25,7 +26,7 @@ fn main() -> std::io::Result<()> {
let file_name = args.get(2).map(|s| s.to_string());
match file_name {
Some(f) => {
let to_compress = File::open(&f)?;
let mut to_compress = File::open(&f)?;
let mut after_compression = File::create(f + ".oeuf")?;
let map = get_map_file(&to_compress)?;
let nodes = nodes_at_vec(map);
@@ -40,6 +41,14 @@ fn main() -> std::io::Result<()> {
after_compression.write_all(&len_tree.to_be_bytes())?;
after_compression.write_all(&binary)?;
after_compression.write_all(string.as_bytes())?;
to_compress.seek(SeekFrom::Start(0))?;
let mut map = tree_to_map(&mut tree, true);
let mut trad = translate_char_bin(&mut to_compress, map);
//eprintln!("trad = {:?}", trad);
let trad_bin = string_to_binary(&trad);
let len_body = trad.len() as u32;
after_compression.write_all(&len_body.to_be_bytes())?;
after_compression.write_all(&trad_bin)?;
}
None => {
println!("No file name!");
@@ -68,7 +77,10 @@ fn main() -> std::io::Result<()> {
let chars_str = String::from_utf8(chars_bin).unwrap();
let mut root = read_tree(&tree_str, &chars_str.chars().collect());
let txt = tree_to_string(&mut root);
eprintln!("txt = {:?}", txt);
to_print.read_exact(&mut buffer)?;
let len_body = u32::from_be_bytes(buffer);
let untranslate = translate_bin_char(&mut to_print, root, len_body);
eprintln!("untranslate = {:?}", untranslate);
}
None => {
println!("No file name!");
@@ -184,3 +196,47 @@ fn read_n_bytes(file: &mut File, len: u32) -> Vec<u8> {
}
out
}
fn translate_char_bin(file: &mut File, map: HashMap<String, String>) -> String {
let mut txt = String::new();
let mut translate = String::new();
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();
let mut current = &root;
file.read_to_end(&mut txt);
let mut txt_str = binary_to_string(txt, len);
for b in txt_str.chars() {
match b {
'1' => {
if let Some(r) = &current.r {
current = r;
}
}
'0' => {
if let Some(l) = &current.l {
current = l;
}
}
_ => {
println!("I see you, you try to tinker with things but it breaks everything");
}
}
if let Some(v) = current.val {
translate.push(v);
current = &root;
}
}
translate
}

View File

@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::collections::VecDeque;
pub struct Node {
@@ -5,6 +6,7 @@ pub struct Node {
pub l: Option<Box<Node>>,
pub r: Option<Box<Node>>,
pub n: i32,
pub path: String,
}
impl Node {
@@ -14,6 +16,7 @@ impl Node {
l: None,
r: None,
n: 0,
path: String::new(),
}
}
@@ -31,6 +34,35 @@ impl Node {
}
}
pub fn tree_to_map(root: &mut Node, char_to_bin: bool) -> HashMap<String, String> {
let mut map: HashMap<String, String> = HashMap::new();
let mut to_do: VecDeque<&mut Node> = VecDeque::new();
to_do.push_back(root);
while let Some(int_root) = to_do.pop_front() {
if let Some(v) = int_root.val {
// leaf
if char_to_bin {
map.insert(v.to_string(), int_root.path.to_string());
} else {
map.insert(int_root.path.to_string(), v.to_string());
}
} else {
// internal
if let Some(l) = int_root.l.as_mut() {
l.path = format!("{}0", int_root.path);
to_do.push_back(l);
}
if let Some(r) = int_root.r.as_mut() {
r.path = format!("{}1", int_root.path);
to_do.push_back(r);
}
}
}
map
}
pub fn create_tree(mut nodes: Vec<Node>) -> Node {
while nodes.len() > 1 {
nodes.sort_by_key(|node| node.n);
@@ -78,9 +110,11 @@ pub fn read_tree(binary: &str, chars: &Vec<char>) -> Node {
let mut root = Node::new();
if stack_bin.remove(0) == '1' {
root.path = String::from("");
to_do.push_back(&mut root);
} else {
root.val = Some(stack_chars.remove(0));
root.path = String::from("0");
return root;
}
@@ -88,15 +122,19 @@ pub fn read_tree(binary: &str, chars: &Vec<char>) -> Node {
int_root.insert(Node::new(), Node::new());
if let Some(left) = int_root.l.as_mut() {
if stack_bin.remove(0) == '1' {
left.path = String::from("1");
to_do.push_back(left);
} else {
left.path = String::from("0");
left.val = Some(stack_chars.remove(0));
}
}
if let Some(right) = int_root.r.as_mut() {
if stack_bin.remove(0) == '1' {
to_do.push_front(right);
right.path = String::from("1");
to_do.push_back(right);
} else {
right.path = String::from("0");
right.val = Some(stack_chars.remove(0));
}
}