From f8a7607ea8951d9166849393d93b29035a37fea0 Mon Sep 17 00:00:00 2001 From: Moysan Gabriel Date: Wed, 8 Feb 2023 01:17:46 +0100 Subject: [PATCH] conversion + creation MemChecker from a file path --- src/simulator/mem_cmp.rs | 105 +++++++++++++++++++++++++++++++++++---- test_file_section.txt | 8 +++ 2 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 test_file_section.txt diff --git a/src/simulator/mem_cmp.rs b/src/simulator/mem_cmp.rs index 859bfad..02a3602 100644 --- a/src/simulator/mem_cmp.rs +++ b/src/simulator/mem_cmp.rs @@ -1,6 +1,15 @@ +use std::fs; +use std::io; +use std::io::BufRead; + + + /* TRUCS MANQUANT * Verifier qu'il y a un nombre pair de caractere hexa dans la ligne correspondante d'une section du fichier source * Sinon on ne peut pas automatiquement remplir chaque octect car 2 hexa = 1 octet + + +* OUBLI CONVERSIONS ADDR ET LEN QUI SONT AUSSI EN CARACTÈRES HEXA DS LE FICHIER */ @@ -41,10 +50,12 @@ struct Section{ * Voir si instanciation d'une structure deplace les valeurs "locales" à la méthode from, je sais plus .... */ impl Section{ + fn from(section: &SectionFormat) -> Section { + let mut content: Vec = Vec::new(); - let addr: usize = section.addr.parse().unwrap(); - let len: usize = section.len.parse().unwrap(); + let addr: usize = string_hex_to_usize(§ion.addr); + let len: usize = string_hex_to_usize(§ion.len); let mut tmp_a: char = ' '; let mut tmp_b: char = ' '; @@ -60,7 +71,6 @@ impl Section{ } } - Section{addr:addr, len:len, content:content} } } @@ -76,12 +86,51 @@ struct Mem_Checker{ impl Mem_Checker{ - /*fn from(path: &String) -> Mem_Checker{ + fn from(path: &String) /*-> Mem_Checker*/{ - }*/ + let file = fs::File::open("test_file_section.txt").expect("Wrong filename"); + let reader = io::BufReader::new(file); + let mut pc: usize = 0; + let mut sp: usize = 0; + + for (i,line) in reader.lines().enumerate() { + + let current_line = line.unwrap(); + + if i == 0 { + //Lecture de PC + pc = string_hex_to_usize(¤t_line); + } + else if i == 1 { + //Lecture SP + sp = string_hex_to_usize(¤t_line); + } + else { + //Lecture des sections + } + } + } } + +fn string_hex_to_usize(s: &String) -> usize{ + + let max_pow = (s.len()-1) as u32; + let mut ret_value: usize = 0; + let base: usize = 16; + + for (i,c )in s.chars().enumerate(){ + print!("Current char :: {} :: Current pow :: {} ::", c, max_pow - (i as u32)); + let tmp: usize = (one_hex_to_dec(c) as usize); + ret_value += base.pow(max_pow - (i as u32))*tmp; + } + + return ret_value; +} + + + /* * c doit etre un caractère hexadécimale */ @@ -101,7 +150,7 @@ fn one_hex_to_dec(c: char) -> u8 { } } - + fn two_hex_to_u8(c1: char, c2: char) -> u8 { let a = one_hex_to_dec(c1); @@ -110,10 +159,49 @@ fn two_hex_to_u8(c1: char, c2: char) -> u8 { 16*a + b } + + +/* +* Juste pour voir si via BufReader les \n sont présent, apres test il s'avère que non +* De toute facon on limitera d'une section la lecture par len + */ +fn test_show_sections_file(){ + let file = fs::File::open("test_file_section.txt").expect("Wrong filename"); + let reader = io::BufReader::new(file); + + for line in reader.lines() { + //println!("Tailles de la ligne : {}", + let current = line.unwrap(); + //println!("Taille de la ligne : {}", current.len()); // En effet pas de \n dans chaque line, parfait + println!("{}", ¤t); + } + +} + + + + #[cfg(test)] mod tests { use super::*; + + #[test] + fn test_string_hex_to_usize(){ + let s = String::from("AE1F20"); + //println!("taille de string : {}", s.len()); + let expected: usize = 11411232; + let result = string_hex_to_usize(&s); + + assert_eq!(expected,result); + } + + #[test] + fn tmp_fct_read_file(){ + println!("Reading A file \n"); + test_show_sections_file(); + } + #[test] fn test_create_section_content(){ let section_format = SectionFormat{ @@ -136,11 +224,6 @@ mod tests { assert_eq!(section.content, expected_vec); } - - - - - #[test] fn test_mod(){ let cond = (0%2) == 0; diff --git a/test_file_section.txt b/test_file_section.txt new file mode 100644 index 0000000..7198c00 --- /dev/null +++ b/test_file_section.txt @@ -0,0 +1,8 @@ +0 +0 +0 0 +F4A12200 +0 0 +01022B +0 0 +FFACBC5CEF