From 96602d7a8382055d1acc2bf8c351bf2ee6165487 Mon Sep 17 00:00:00 2001 From: Jaby Date: Thu, 29 Jun 2023 21:32:49 +0200 Subject: [PATCH] Improve reading sections in --- src/Tools/readmap/Cargo.toml | 1 + src/Tools/readmap/src/lib.rs | 74 +++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/src/Tools/readmap/Cargo.toml b/src/Tools/readmap/Cargo.toml index 2dc9112b..be1a86cc 100644 --- a/src/Tools/readmap/Cargo.toml +++ b/src/Tools/readmap/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] clap = {version = "*", features = ["derive"]} +num-traits = "*" tool_helper = {path = "../tool_helper"} \ No newline at end of file diff --git a/src/Tools/readmap/src/lib.rs b/src/Tools/readmap/src/lib.rs index 396edc04..fe547c15 100644 --- a/src/Tools/readmap/src/lib.rs +++ b/src/Tools/readmap/src/lib.rs @@ -5,49 +5,63 @@ use std::io::BufRead; use types::{Section}; pub fn scan(input: Input) -> Result, Error> { - process(input.lines().map(|l| l.unwrap()).into_iter()); + process(input.lines().map(|l| l.unwrap()).into_iter())?; Err(Error::not_implemented("readmap::scan")) } -fn process>(mut line_iter:F) { +fn process>(mut line_iter:F) -> Result<(), Error> { while let Some(line) = line_iter.next() { if line.starts_with(".") { - process_section(line, &mut line_iter); + process_section(line, &mut line_iter)?; } } + + Ok(()) } -macro_rules! force_next { - ($split_line:ident, $line_iter:ident, $line:ident) => { - { - if let Some(value) = $split_line.next() { - value - } - - else { - $line = $line_iter.next().unwrap(); - $split_line = $line.split_whitespace(); - - $split_line.next().unwrap() - } - } - }; +fn process_section>(line: String, line_iter:&mut F) -> Result<(), Error> { + let (section, _line) = read_section(line, || { + line_iter.next().ok_or(Error::from_str("")) + })?; + + println!("Section: {} @{:?} {:?}", section.name, section.adr, section.size); + Ok(()) } -fn process_section>(mut line: String, line_iter:&mut F) { +fn read_section Result>(mut line: String, mut next_line: F) -> Result<(Section, Option), Error> { let mut split_line = line.split_whitespace(); - let name = split_line.next().unwrap().to_string(); - let adr = u64::from_str_radix(force_next!(split_line, line_iter, line).trim_start_matches("0x"), 16).ok(); - let size = { // If adr is already empty we do not need to check for a size - if adr.is_some() { - usize::from_str_radix(force_next!(split_line, line_iter, line).trim_start_matches("0x"), 16).ok() - } + let name = split_line.next().ok_or(Error::from_str(""))?.to_string(); + let need_new_line = name.chars().count() > 16; - else { - None - } - }; + if need_new_line { + line = next_line()?; + split_line = line.split_whitespace(); + } - println!("Section: {} @{:?} {:?}", name, adr, size); + let adr = read_as::(split_line.next()); + if adr.is_none() { + return Ok((Section::new(name, None, None), { + if need_new_line { + Some(line) + } + + else { + None + } + })); + } + + let size = read_as::(split_line.next()); + Ok((Section::new(name, adr, size), None)) +} + +fn read_as(str: Option<&str>) -> Option { + if let Some(str) = str { + F::from_str_radix(str.trim_start_matches("0x"), 16).ok() + } + + else { + None + } } \ No newline at end of file