diff --git a/src/Tools/readmap/src/lib.rs b/src/Tools/readmap/src/lib.rs index 83c7c243..0151c99d 100644 --- a/src/Tools/readmap/src/lib.rs +++ b/src/Tools/readmap/src/lib.rs @@ -101,10 +101,11 @@ fn process_subsection Result>(section: &mut Section, } else if line.starts_with(" 0x") { - let symbol = read_symbol(line)?; - sub_section = add_element(sub_section, section, symbol, |section, symbol| { - section.content.push(Content::Symbol(symbol)); - }); + if let Some(symbol) = parse_symbol(read_symbol(line)?) { + sub_section = add_element(sub_section, section, symbol, |section, symbol| { + section.content.push(Content::Symbol(symbol)); + }); + } } else if line.is_empty() { @@ -161,13 +162,26 @@ fn read_fill(line: String) -> Result { } fn read_symbol(line: String) -> Result { - let mut split_line = line.split_whitespace(); - let adr = read_as::(split_line.next()).ok_or_else(|| {return Error::from_str("Symbol statement requires an address")})?; - let name = split_line.next().ok_or_else(|| {return Error::from_str("Symbol statement requires a symbol name")})?.to_string(); + let mut split_line = line.split_whitespace(); + let adr = read_as::(split_line.next()).ok_or_else(|| {return Error::from_str("Symbol statement requires an address")})?; + let mut name = split_line.next().ok_or_else(|| {return Error::from_str("Symbol statement requires a symbol name")})?.to_string(); + + for fragment in split_line { + name += fragment; + } Ok(types::Symbol::new(name, adr)) } +fn parse_symbol(mut symbol: types::Symbol) -> Option { + if symbol.name.starts_with(".") { + return None; + } + + symbol.name = symbol.name.split('=').next().unwrap().to_owned(); + Some(symbol) +} + fn read_as(str: Option<&str>) -> Option { if let Some(str) = str { F::from_str_radix(str.trim_start_matches("0x"), 16).ok() diff --git a/src/Tools/readmap/src/main.rs b/src/Tools/readmap/src/main.rs index 99045765..91445a6f 100644 --- a/src/Tools/readmap/src/main.rs +++ b/src/Tools/readmap/src/main.rs @@ -1,6 +1,7 @@ use clap::Parser; +use readmap::types::{Content::*, Section}; use tool_helper::{Error, exit_with_error}; -use std::path::PathBuf; +use std::{fs::File, io::{BufWriter, Write}, path::PathBuf}; #[derive(Parser)] #[clap(about = "Opens and scans a MAP file to print extended information", long_about = None)] @@ -10,9 +11,46 @@ struct CommandLine { } fn run_main(cmd: CommandLine) -> Result<(), Error> { - let _sections = readmap::scan(tool_helper::open_input(cmd.input)?)?; + fn value_to_hex(value: T) -> String { + return format!("0x{:X}", value); + } - println!("Found sections!"); + fn option_to_hex(value: Option) -> String { + if let Some(value) = value { + return value_to_hex(value); + } + + else { + return String::from(""); + } + } + + let sections = readmap::scan(tool_helper::open_input(cmd.input)?)?; + let mut file = tool_helper::open_output_file(&PathBuf::from("./planschi.txt"))?; + + for section in sections { + fn print_content(tab_level: usize, file: &mut BufWriter, section: Section) -> Result<(), Error> { + for content in section.content { + match content { + Fill(fill) => { + writeln!(file, "{:>tab_level$}*fill* @{}, {}", ' ', value_to_hex(fill.adr), value_to_hex(fill.size), tab_level=tab_level*4)?; + }, + Section(section) => { + writeln!(file, "{:>tab_level$} {} @{}, {}", ' ', section.name, option_to_hex(section.adr), option_to_hex(section.size), tab_level=tab_level*4)?; + print_content(tab_level + 1, file, section)?; + }, + Symbol(symbol) => { + writeln!(file, "{:>tab_level$}{} @{}", ' ', symbol.name, value_to_hex(symbol.adr), tab_level=tab_level*4)?; + } + } + } + + Ok(()) + } + + writeln!(file, "{}: @{}, {}", section.name, option_to_hex(section.adr), option_to_hex(section.size))?; + print_content(1, &mut file, section)?; + } Ok(()) }