Parse map and write to file

This commit is contained in:
Jaby 2023-07-05 21:36:12 +02:00
parent b43f029107
commit d0e0c6f16b
2 changed files with 62 additions and 10 deletions

View File

@ -101,11 +101,12 @@ fn process_subsection<F:FnMut()-> Result<String, Error>>(section: &mut Section,
}
else if line.starts_with(" 0x") {
let symbol = read_symbol(line)?;
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() {
push_sub_section(section, sub_section);
@ -163,11 +164,24 @@ fn read_fill(line: String) -> Result<types::Fill, Error> {
fn read_symbol(line: String) -> Result<types::Symbol, Error> {
let mut split_line = line.split_whitespace();
let adr = read_as::<u64>(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 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<types::Symbol> {
if symbol.name.starts_with(".") {
return None;
}
symbol.name = symbol.name.split('=').next().unwrap().to_owned();
Some(symbol)
}
fn read_as<F: num_traits::Num>(str: Option<&str>) -> Option<F> {
if let Some(str) = str {
F::from_str_radix(str.trim_start_matches("0x"), 16).ok()

View File

@ -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<T: std::fmt::UpperHex>(value: T) -> String {
return format!("0x{:X}", value);
}
println!("Found sections!");
fn option_to_hex<T: std::fmt::UpperHex>(value: Option<T>) -> String {
if let Some(value) = value {
return value_to_hex(value);
}
else {
return String::from("<None>");
}
}
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<File>, 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(())
}