Print top-level sections
This commit is contained in:
parent
2b3ee8ea51
commit
d92c2f5a56
|
@ -2,16 +2,52 @@ pub mod types;
|
||||||
|
|
||||||
use tool_helper::{Error, Input};
|
use tool_helper::{Error, Input};
|
||||||
use std::io::BufRead;
|
use std::io::BufRead;
|
||||||
use types::Section;
|
use types::{Section};
|
||||||
|
|
||||||
pub fn scan(input: Input) -> Result<Vec<Section>, Error> {
|
pub fn scan(input: Input) -> Result<Vec<Section>, Error> {
|
||||||
let lines = input.lines().map(|l| l.unwrap());
|
process(input.lines().map(|l| l.unwrap()).into_iter());
|
||||||
|
|
||||||
for line in lines {
|
|
||||||
if line.starts_with('.') {
|
|
||||||
println!("Found section \"{}\"", line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(Error::not_implemented("readmap::scan"))
|
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);
|
||||||
|
}
|
|
@ -2,10 +2,10 @@ use std::default::Default;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Section {
|
pub struct Section {
|
||||||
name: String,
|
pub name: String,
|
||||||
adr: u64,
|
pub adr: Option<u64>,
|
||||||
size: usize,
|
pub size: Option<usize>,
|
||||||
content: Vec<Content>
|
pub content: Vec<Content>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -27,7 +27,7 @@ pub enum Content {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Section {
|
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()}
|
Section{name, adr, size, content: Vec::new()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue