Print top-level sections

This commit is contained in:
Jaby 2023-06-29 20:18:02 +02:00
parent 2b3ee8ea51
commit d92c2f5a56
2 changed files with 49 additions and 13 deletions

View File

@ -2,16 +2,52 @@ pub mod types;
use tool_helper::{Error, Input};
use std::io::BufRead;
use types::Section;
use types::{Section};
pub fn scan(input: Input) -> Result<Vec<Section>, Error> {
let lines = input.lines().map(|l| l.unwrap());
for line in lines {
if line.starts_with('.') {
println!("Found section \"{}\"", line);
}
}
process(input.lines().map(|l| l.unwrap()).into_iter());
Err(Error::not_implemented("readmap::scan"))
}
fn process<F: std::iter::Iterator<Item=String>>(mut line_iter:F) {
while let Some(line) = line_iter.next() {
if line.starts_with(".") {
process_section(line, &mut line_iter);
}
}
}
macro_rules! force_next {
($split_line:ident, $line_iter:ident, $line:ident) => {
{
if let Some(value) = $split_line.next() {
value
}
else {
$line = $line_iter.next().unwrap();
$split_line = $line.split_whitespace();
$split_line.next().unwrap()
}
}
};
}
fn process_section<F: std::iter::Iterator<Item=String>>(mut line: String, line_iter:&mut F) {
let mut split_line = line.split_whitespace();
let name = split_line.next().unwrap().to_string();
let adr = u64::from_str_radix(force_next!(split_line, line_iter, line).trim_start_matches("0x"), 16).ok();
let size = { // If adr is already empty we do not need to check for a size
if adr.is_some() {
usize::from_str_radix(force_next!(split_line, line_iter, line).trim_start_matches("0x"), 16).ok()
}
else {
None
}
};
println!("Section: {} @{:?} {:?}", name, adr, size);
}

View File

@ -2,10 +2,10 @@ use std::default::Default;
#[derive(Default)]
pub struct Section {
name: String,
adr: u64,
size: usize,
content: Vec<Content>
pub name: String,
pub adr: Option<u64>,
pub size: Option<usize>,
pub content: Vec<Content>
}
#[derive(Default)]
@ -27,7 +27,7 @@ pub enum Content {
}
impl Section {
pub fn new(name: String, adr: u64, size: usize) -> Section {
pub fn new(name: String, adr: Option<u64>, size: Option<usize>) -> Section {
Section{name, adr, size, content: Vec::new()}
}
}