From 278ee844a0336f9c9e47877956889aab60aaabcf Mon Sep 17 00:00:00 2001 From: Jaby Date: Mon, 3 Jul 2023 22:17:44 +0200 Subject: [PATCH] Read sub sections --- src/Tools/readmap/src/lib.rs | 66 +++++++++++++++++++++--------- src/Tools/readmap/src/types/mod.rs | 5 ++- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/Tools/readmap/src/lib.rs b/src/Tools/readmap/src/lib.rs index aeca727d..f66a5dba 100644 --- a/src/Tools/readmap/src/lib.rs +++ b/src/Tools/readmap/src/lib.rs @@ -2,7 +2,7 @@ pub mod types; use tool_helper::{Error, Input}; use std::io::BufRead; -use types::{Section}; +use types::{Content, Section}; pub fn scan(input: Input) -> Result, Error> { process(input.lines().map(|l| l.unwrap()).into_iter())?; @@ -32,11 +32,16 @@ fn process_section>(line: String, line_iter: }; let (mut section, line) = read_section(line, &mut next_line_closure)?; - println!("Section: {} @{:?} {:?}", section.name, section.adr, section.size); + println!("Section: {} @{:?} {:?} \"{:?}\"", section.name, section.adr, section.size, section.file); process_subsection(&mut section, line, &mut next_line_closure) } -fn process_subsection Result>(_section: &mut Section, line: Option, next_line: &mut F) -> Result { +fn process_subsection Result>(section: &mut Section, line: Option, next_line: &mut F) -> Result { + fn push_sub_section(section: &mut Section, sub_section: Option
) { + if let Some(sub_section) = sub_section { + section.content.push(Content::Section(sub_section)); + } + } let mut line = { if let Some(line) = line { line @@ -47,25 +52,39 @@ fn process_subsection Result>(_section: &mut Section, } }; - loop { - if line.starts_with(" .") { - println!(">>> SubSection: {}", line); - } - - else if line.is_empty() { + let mut next_line_closure = || -> Result { + if let Ok(line) = next_line() { return Ok(line); } - line = { - if let Ok(line) = next_line() { - line - } + else { + // EOF + return Ok(String::from("")); + } + }; - else { - // EOF - return Ok(String::from("")); + let mut sub_section = None; + + loop { + if line.starts_with(" .") { + push_sub_section(section, sub_section); + let (section, new_line) = read_section(line, &mut next_line_closure)?; + + println!("\t>>>>> {:?} {:?} {:?} {:?}", section.name, section.adr, section.size, section.file); + sub_section = Some(section); + + if let Some(new_line) = new_line { + line = new_line; + continue; } - }; + } + + else if line.is_empty() { + push_sub_section(section, sub_section); + return Ok(line); + } + + line = next_line_closure()?; } } @@ -81,7 +100,7 @@ fn read_section Result>(mut line: String, next_line: let adr = read_as::(split_line.next()); if adr.is_none() { - return Ok((Section::new(name, None, None), { + return Ok((Section::new(name, None, None, None), { if need_new_line { Some(line) } @@ -93,7 +112,16 @@ fn read_section Result>(mut line: String, next_line: } let size = read_as::(split_line.next()); - Ok((Section::new(name, adr, size), None)) + let file = { + if let Some(file) = split_line.next() { + Some(file.to_string()) + } + + else { + None + } + }; + Ok((Section::new(name, adr, size, file), None)) } fn read_as(str: Option<&str>) -> Option { diff --git a/src/Tools/readmap/src/types/mod.rs b/src/Tools/readmap/src/types/mod.rs index e2976a2f..03b06a44 100644 --- a/src/Tools/readmap/src/types/mod.rs +++ b/src/Tools/readmap/src/types/mod.rs @@ -5,6 +5,7 @@ pub struct Section { pub name: String, pub adr: Option, pub size: Option, + pub file: Option, pub content: Vec } @@ -27,8 +28,8 @@ pub enum Content { } impl Section { - pub fn new(name: String, adr: Option, size: Option) -> Section { - Section{name, adr, size, content: Vec::new()} + pub fn new(name: String, adr: Option, size: Option, file: Option) -> Section { + Section{name, adr, size, file, content: Vec::new()} } }