From d92c2f5a56ab787df3686b65a6b754074f70068c Mon Sep 17 00:00:00 2001 From: Jaby Date: Thu, 29 Jun 2023 20:18:02 +0200 Subject: [PATCH] Print top-level sections --- src/Tools/readmap/src/lib.rs | 52 +++++++++++++++++++++++++----- src/Tools/readmap/src/types/mod.rs | 10 +++--- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/Tools/readmap/src/lib.rs b/src/Tools/readmap/src/lib.rs index e1bf80e3..396edc04 100644 --- a/src/Tools/readmap/src/lib.rs +++ b/src/Tools/readmap/src/lib.rs @@ -2,16 +2,52 @@ pub mod types; use tool_helper::{Error, Input}; use std::io::BufRead; -use types::Section; +use types::{Section}; pub fn scan(input: Input) -> Result, Error> { - let lines = input.lines().map(|l| l.unwrap()); - - for line in lines { - if line.starts_with('.') { - println!("Found section \"{}\"", line); - } - } + process(input.lines().map(|l| l.unwrap()).into_iter()); Err(Error::not_implemented("readmap::scan")) } + +fn process>(mut line_iter:F) { + while let Some(line) = line_iter.next() { + if line.starts_with(".") { + process_section(line, &mut line_iter); + } + } +} + +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>(mut line: String, line_iter:&mut F) { + 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() + } + + else { + None + } + }; + + println!("Section: {} @{:?} {:?}", name, adr, size); +} \ No newline at end of file diff --git a/src/Tools/readmap/src/types/mod.rs b/src/Tools/readmap/src/types/mod.rs index d599e196..e2976a2f 100644 --- a/src/Tools/readmap/src/types/mod.rs +++ b/src/Tools/readmap/src/types/mod.rs @@ -2,10 +2,10 @@ use std::default::Default; #[derive(Default)] pub struct Section { - name: String, - adr: u64, - size: usize, - content: Vec + pub name: String, + pub adr: Option, + pub size: Option, + pub content: Vec } #[derive(Default)] @@ -27,7 +27,7 @@ pub enum Content { } impl Section { - pub fn new(name: String, adr: u64, size: usize) -> Section { + pub fn new(name: String, adr: Option, size: Option) -> Section { Section{name, adr, size, content: Vec::new()} } }