Collect highest address

This commit is contained in:
jaby 2023-07-11 19:00:59 +02:00
parent 5db0b74720
commit 9f20537352
3 changed files with 24 additions and 13 deletions

View File

@ -2,20 +2,22 @@ pub mod types;
use tool_helper::{Error, Input}; use tool_helper::{Error, Input};
use std::io::BufRead; use std::io::BufRead;
use types::{Content, Section}; use types::{Content, MemoryMapInfo, Section};
pub fn scan(input: Input) -> Result<Vec<Section>, Error> { pub fn scan(input: Input) -> Result<MemoryMapInfo, Error> {
let sections = process(input.lines().map(|l| l.unwrap()).into_iter())?; let (sections, highest_address) = process(input.lines().map(|l| l.unwrap()).into_iter())?;
Ok(sections) Ok(MemoryMapInfo{sections, highest_address: highest_address})
} }
fn process<F: std::iter::Iterator<Item=String>>(mut line_iter:F) -> Result<Vec<Section>, Error> { fn process<F: std::iter::Iterator<Item=String>>(mut line_iter:F) -> Result<(Vec<Section>, u64), Error> {
let mut sections = Vec::new(); let mut sections = Vec::new();
let mut highest_address = 0u64;
while let Some(mut line) = line_iter.next() { while let Some(mut line) = line_iter.next() {
loop { loop {
if line.starts_with(".") { if line.starts_with(".") {
let (new_line, new_section) = process_section(line, &mut line_iter)?; let (new_line, new_section) = process_section(&mut highest_address, line, &mut line_iter)?;
sections.push(new_section); sections.push(new_section);
line = new_line; line = new_line;
@ -30,19 +32,19 @@ fn process<F: std::iter::Iterator<Item=String>>(mut line_iter:F) -> Result<Vec<S
sections.sort_by(|left, right| { sections.sort_by(|left, right| {
left.adr.cmp(&right.adr) left.adr.cmp(&right.adr)
}); });
Ok(sections) Ok((sections, highest_address))
} }
fn process_section<F: std::iter::Iterator<Item=String>>(line: String, line_iter:&mut F) -> Result<(String, Section), Error> { fn process_section<F: std::iter::Iterator<Item=String>>(highest_address: &mut u64, line: String, line_iter:&mut F) -> Result<(String, Section), Error> {
let mut next_line_closure = || { let mut next_line_closure = || {
line_iter.next().ok_or(Error::from_str("Unexpected end of file")) line_iter.next().ok_or(Error::from_str("Unexpected end of file"))
}; };
let (mut section, line) = read_section(line, &mut next_line_closure)?; let (mut section, line) = read_section(line, &mut next_line_closure)?;
Ok((process_subsection(&mut section, line, &mut next_line_closure)?, section)) Ok((process_subsection(&mut section, highest_address, line, &mut next_line_closure)?, section))
} }
fn process_subsection<F:FnMut()-> Result<String, Error>>(section: &mut Section, line: Option<String>, next_line: &mut F) -> Result<String, Error> { fn process_subsection<F:FnMut()-> Result<String, Error>>(section: &mut Section, highest_address: &mut u64, line: Option<String>, next_line: &mut F) -> Result<String, Error> {
fn push_sub_section(section: &mut Section, sub_section: Option<Section>) { fn push_sub_section(section: &mut Section, sub_section: Option<Section>) {
if let Some(sub_section) = sub_section { if let Some(sub_section) = sub_section {
section.content.push(Content::Section(sub_section)); section.content.push(Content::Section(sub_section));
@ -105,6 +107,10 @@ fn process_subsection<F:FnMut()-> Result<String, Error>>(section: &mut Section,
else if line.starts_with(" 0x") { else if line.starts_with(" 0x") {
if let Some(symbol) = parse_symbol(read_symbol(line)?) { if let Some(symbol) = parse_symbol(read_symbol(line)?) {
if symbol.adr > *highest_address {
*highest_address = symbol.adr;
}
sub_section = add_element(sub_section, section, symbol, |section, symbol| { sub_section = add_element(sub_section, section, symbol, |section, symbol| {
section.content.push(Content::Symbol(symbol)); section.content.push(Content::Symbol(symbol));
}); });

View File

@ -1,5 +1,10 @@
use std::default::Default; use std::default::Default;
pub struct MemoryMapInfo {
pub sections: Vec<Section>,
pub highest_address: u64,
}
#[derive(Default)] #[derive(Default)]
pub struct Section { pub struct Section {
pub name: String, pub name: String,

View File

@ -1,5 +1,5 @@
use crossterm::event::KeyCode; use crossterm::event::KeyCode;
use readmap::types::Section; use readmap::types::MemoryMapInfo;
use std::path::PathBuf; use std::path::PathBuf;
use tool_helper::Error; use tool_helper::Error;
use tui::{ use tui::{
@ -211,6 +211,6 @@ impl ConsoleUI {
} }
} }
pub fn load_memory_map(file_path: Option<PathBuf>) -> Result<Vec<Section>, Error> { pub fn load_memory_map(file_path: Option<PathBuf>) -> Result<MemoryMapInfo, Error> {
readmap::scan(tool_helper::open_input(file_path)?) readmap::scan(tool_helper::open_input(file_path)?)
} }