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
}