open file bu bug with 8 chars
This commit is contained in:
92
src/main.rs
92
src/main.rs
@@ -3,20 +3,17 @@
|
||||
use eframe::egui;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::io::prelude::*;
|
||||
use std::io::{Seek, SeekFrom};
|
||||
|
||||
mod byte_writer;
|
||||
mod gui;
|
||||
mod node;
|
||||
mod read_and_write;
|
||||
|
||||
use byte_writer::{binary_to_string, string_to_binary};
|
||||
use gui::Omelt;
|
||||
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;
|
||||
use read_and_write::{is_valid_file, save_action, string_translate};
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
@@ -37,25 +34,11 @@ fn main() -> std::io::Result<()> {
|
||||
} else {
|
||||
after_compression = File::create(f + ".oeuf")?;
|
||||
}
|
||||
let map = get_map_file(&to_compress)?;
|
||||
let nodes = nodes_at_vec(map);
|
||||
let mut tree = create_tree(nodes);
|
||||
let (str_bin, chars) = tree_to_string(&mut tree);
|
||||
let binary = string_to_binary(&str_bin);
|
||||
let len_tree: u32 = str_bin.len() as u32;
|
||||
let string: String = chars.iter().collect();
|
||||
after_compression.write_all(&MAGIC_NUMBER.to_be_bytes())?;
|
||||
after_compression.write_all(&len_tree.to_be_bytes())?;
|
||||
after_compression.write_all(&(string.len() as u32).to_be_bytes())?;
|
||||
after_compression.write_all(&binary)?;
|
||||
after_compression.write_all(string.as_bytes())?;
|
||||
to_compress.seek(SeekFrom::Start(0))?;
|
||||
let map = tree_to_map(&mut tree, true);
|
||||
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())?;
|
||||
after_compression.write_all(&trad_bin)?;
|
||||
let mut txt = "".to_owned();
|
||||
to_compress.read_to_string(&mut txt)?;
|
||||
eprintln!("txt = {:?}", txt);
|
||||
save_action(&txt, &mut after_compression)?;
|
||||
println!("Done.")
|
||||
}
|
||||
None => {
|
||||
println!("No file name!");
|
||||
@@ -111,7 +94,12 @@ fn main() -> std::io::Result<()> {
|
||||
let _ = eframe::run_native(
|
||||
"Omelt",
|
||||
options,
|
||||
Box::new(|_| Ok(Box::new(Omelt::new(&r, File::open(&r)?)))),
|
||||
Box::new(|_| {
|
||||
Ok(Box::new(Omelt::new(
|
||||
&r,
|
||||
OpenOptions::new().read(true).write(true).open(&r)?,
|
||||
)))
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -127,7 +115,7 @@ fn main() -> std::io::Result<()> {
|
||||
Box::new(|_| {
|
||||
Ok(Box::new(Omelt::new(
|
||||
"new_file.oeuf",
|
||||
File::create("new_file.oeuf")?,
|
||||
File::create_new("new_file.oeuf")?,
|
||||
)))
|
||||
}),
|
||||
);
|
||||
@@ -142,53 +130,3 @@ fn display_options() {
|
||||
println!("--uncompress, -u <intput> [<output>] To uncompress a file.");
|
||||
println!("--print ,-p <input> To print a file compressed.");
|
||||
}
|
||||
|
||||
fn string_translate(file: &mut File) -> String {
|
||||
let mut buffer = [0u8; 4];
|
||||
let _ = file.read_exact(&mut buffer);
|
||||
let len_tree = u32::from_be_bytes(buffer);
|
||||
let _ = file.read_exact(&mut buffer);
|
||||
let len_chars = u32::from_be_bytes(buffer) * 8;
|
||||
|
||||
let tree_bin = read_n_bytes(file, len_tree);
|
||||
let tree_str = binary_to_string(tree_bin, len_tree);
|
||||
|
||||
let chars_bin = read_n_bytes(file, len_chars);
|
||||
let chars_str = String::from_utf8(chars_bin).unwrap();
|
||||
let root = read_tree(&tree_str, &chars_str.chars().collect());
|
||||
let _ = file.read_exact(&mut buffer);
|
||||
let len_body = u32::from_be_bytes(buffer);
|
||||
translate_bin_char(file, root, len_body)
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
let _ = file.read_to_end(&mut txt);
|
||||
let txt_str = binary_to_string(txt, len);
|
||||
for b in txt_str.chars() {
|
||||
match b {
|
||||
'1' => {
|
||||
if let Some(r) = ¤t.r {
|
||||
current = r;
|
||||
}
|
||||
}
|
||||
'0' => {
|
||||
if let Some(l) = ¤t.l {
|
||||
current = l;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
println!("Error !");
|
||||
return String::new();
|
||||
}
|
||||
}
|
||||
if let Some(v) = current.val {
|
||||
translate.push(v);
|
||||
current = &root;
|
||||
}
|
||||
}
|
||||
translate
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user