diff --git a/src/Tools/readmap/src/lib.rs b/src/Tools/readmap/src/lib.rs index 03810f6d..543bd42a 100644 --- a/src/Tools/readmap/src/lib.rs +++ b/src/Tools/readmap/src/lib.rs @@ -1,8 +1,46 @@ pub mod types; use tool_helper::Error; -use types::MemoryMap; +use types::{MemoryMap, Section}; -pub fn scan Option>(_next_line: F) -> Result { +pub fn scan Option>(mut next_line: F) -> Result { + const SECTION_IDENTIFIER:&'static str = "Sections:"; + const SYMBOL_TABLE_IDENTIFIER:&'static str = "SYMBOL TABLE:"; + + while let Some(line) = next_line() { + if line == SECTION_IDENTIFIER { + scan_sections(&mut next_line)?; + } + + if line == SYMBOL_TABLE_IDENTIFIER {} + } Err(Error::not_implemented("scan")) +} + +fn scan_sections Option>(next_line: &mut F) -> Result, Error> { + while let Some(_) = next_line() { + // We read every other line + let line = next_line().ok_or(Error::from_str("Failed skipping section line"))?; + let mut split_line = line.split_whitespace(); + if let Ok(_) = split_line_radix(&mut split_line, 10, "") { + let name = split_line.next().ok_or(Error::from_str("Failed reading Section Name"))?; + let size = split_line_radix(&mut split_line, 16, "Section Size")?; + let adr = split_line_radix(&mut split_line, 16, "Section Address")?; + + println!("\"{}\" @0x{:X} -> 0x{:X}", name, adr, (adr + size)); + } + + else { + return Err(Error::from_str("")); + } + } + + Err(Error::not_implemented("scan")) +} + +fn split_line_radix<'a>(split_line: &mut std::str::SplitWhitespace<'a>, radix: u32, value_name: &str) -> Result { + match u64::from_str_radix(split_line.next().ok_or(Error::from_text(format!("Failed reading: {}", value_name)))?, radix) { + Ok(value) => Ok(value), + Err(error) => Err(Error::from_text(format!("Converting value for {} failed with: {}", value_name, error))) + } } \ No newline at end of file