open file bu bug with 8 chars

This commit is contained in:
2025-04-13 16:11:27 +02:00
parent c31c319021
commit 2c503c992d
4 changed files with 141 additions and 108 deletions

Binary file not shown.

View File

@@ -1,7 +1,8 @@
use eframe::egui::{self};
use eframe::epaint::text::{FontInsert, InsertFontFamily};
use std::fs::File;
use std::io::prelude::*;
use crate::read_and_write;
use read_and_write::{is_valid_file, save_action, string_translate};
pub struct Omelt {
file_name: String,
@@ -11,42 +12,43 @@ pub struct Omelt {
impl Omelt {
pub fn new(file_name: &str, file: File) -> Self {
Self {
let mut out = Self {
file_name: file_name.to_owned(),
text: "".to_owned(),
file: file,
}
};
out.update_txt();
out
}
pub fn update_txt(&mut self) {
let _ = self.file.read_to_string(&mut self.text);
if is_valid_file(&mut self.file) {
self.text = string_translate(&mut self.file);
} else {
println!("File is not valid");
}
}
}
fn add_font(ctx: &egui::Context) {
ctx.add_font(FontInsert::new(
"atwriter",
egui::FontData::from_static(include_bytes!("../fonts/atwriter.ttf")),
vec![
InsertFontFamily {
family: egui::FontFamily::Proportional,
priority: egui::epaint::text::FontPriority::Highest,
},
InsertFontFamily {
family: egui::FontFamily::Monospace,
priority: egui::epaint::text::FontPriority::Lowest,
},
],
))
}
impl eframe::App for Omelt {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
self.update_txt();
add_font(ctx);
let save = KeyboardShortcut::new(egui::Key::S, Modifiers::CTRL);
ctx.set_zoom_factor(2.0);
ctx.input(|i| {
//eprintln!("i = {:?}", i);
if save.matches(i) {
let _ = save_action(&self.text, &mut self.file);
println!("save");
}
});
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading(&self.file_name);
if ui.add(egui::Button::new("Click me")).clicked() {
let _ = self.file.set_len(0);
let _ = save_action(&self.text, &mut self.file);
eprintln!("self.text = {:?}", self.text);
}
egui::ScrollArea::vertical().show(ui, |ui| {
//let visible_text = self.text.lines().take(100).collect::<Vec<_>>().join("\n");
ui.add_sized(
ui.available_size(),
egui::TextEdit::multiline(&mut self.text),
@@ -55,3 +57,20 @@ impl eframe::App for Omelt {
});
}
}
use egui::Modifiers;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct KeyboardShortcut {
key: egui::Key,
modifiers: Modifiers,
}
impl KeyboardShortcut {
fn new(key: egui::Key, modifiers: Modifiers) -> Self {
Self { key, modifiers }
}
fn matches(&self, input: &egui::InputState) -> bool {
input.key_down(self.key) && input.modifiers == self.modifiers
}
}

View File

@@ -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) = &current.r {
current = r;
}
}
'0' => {
if let Some(l) = &current.l {
current = l;
}
}
_ => {
println!("Error !");
return String::new();
}
}
if let Some(v) = current.val {
translate.push(v);
current = &root;
}
}
translate
}

View File

@@ -1,7 +1,15 @@
use crate::byte_writer;
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::io::{Seek, SeekFrom};
use crate::node;
use byte_writer::*;
use eframe::Result;
use node::*;
const MAGIC_NUMBER: u8 = 128;
pub fn read_n_bytes(file: &mut File, len: u32) -> Vec<u8> {
let mut n = 0;
let mut out: Vec<u8> = Vec::new();
@@ -20,17 +28,39 @@ pub fn read_n_bytes(file: &mut File, len: u32) -> Vec<u8> {
}
out
}
pub fn save_action(txt: &str, file: &mut File) -> Result<(), std::io::Error> {
file.seek(SeekFrom::Start(0))?;
let map = get_map_string(txt)?;
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();
file.write_all(&MAGIC_NUMBER.to_be_bytes())?;
file.write_all(&len_tree.to_be_bytes())?;
file.write_all(&(string.len() as u32).to_be_bytes())?;
file.write_all(&binary)?;
file.write_all(string.as_bytes())?;
pub fn get_map_file(mut file: &File) -> Result<HashMap<char, i32>, std::io::Error> {
let map = tree_to_map(&mut tree, true);
let trad = body_to_bin(&txt, map);
let trad_bin = string_to_binary(&trad);
let len_body = trad.len() as u32;
file.write_all(&len_body.to_be_bytes())?;
file.write_all(&trad_bin)?;
Ok(())
}
pub fn get_map_string(txt: &str) -> 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() {
for c in txt.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);
@@ -39,11 +69,9 @@ pub fn is_valid_file(file: &mut File) -> bool {
_ => false,
}
}
pub fn body_to_bin(file: &mut File, map: HashMap<String, String>) -> String {
let mut txt = String::new();
pub fn body_to_bin(txt: &str, map: HashMap<String, String>) -> String {
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);
@@ -51,3 +79,51 @@ pub fn body_to_bin(file: &mut File, map: HashMap<String, String>) -> String {
}
translate
}
pub 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)
}
pub 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) = &current.r {
current = r;
}
}
'0' => {
if let Some(l) = &current.l {
current = l;
}
}
_ => {
println!("Error !");
return String::new();
}
}
if let Some(v) = current.val {
translate.push(v);
current = &root;
}
}
translate
}