reorganisation of fonctions and file

This commit is contained in:
2025-04-11 12:12:13 +02:00
parent 16f1cd067b
commit 7d0f215f26
3 changed files with 88 additions and 76 deletions

53
src/read_and_write.rs Normal file
View File

@@ -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<u8> {
let mut n = 0;
let mut out: Vec<u8> = 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<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() {
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, String>) -> 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
}