diff --git a/src/Tools/readmap/src/lib.rs b/src/Tools/readmap/src/lib.rs index fe547c15..aeca727d 100644 --- a/src/Tools/readmap/src/lib.rs +++ b/src/Tools/readmap/src/lib.rs @@ -11,27 +11,67 @@ pub fn scan(input: Input) -> Result, Error> { } fn process>(mut line_iter:F) -> Result<(), Error> { - while let Some(line) = line_iter.next() { - if line.starts_with(".") { - process_section(line, &mut line_iter)?; + while let Some(mut line) = line_iter.next() { + loop { + if line.starts_with(".") { + line = process_section(line, &mut line_iter)?; + } + + else { + break; + } } } Ok(()) } -fn process_section>(line: String, line_iter:&mut F) -> Result<(), Error> { - let (section, _line) = read_section(line, || { - line_iter.next().ok_or(Error::from_str("")) - })?; +fn process_section>(line: String, line_iter:&mut F) -> Result { + let mut next_line_closure = || { + line_iter.next().ok_or(Error::from_str("Unexpected end of file")) + }; + let (mut section, line) = read_section(line, &mut next_line_closure)?; println!("Section: {} @{:?} {:?}", section.name, section.adr, section.size); - Ok(()) + process_subsection(&mut section, line, &mut next_line_closure) } -fn read_section Result>(mut line: String, mut next_line: F) -> Result<(Section, Option), Error> { +fn process_subsection Result>(_section: &mut Section, line: Option, next_line: &mut F) -> Result { + let mut line = { + if let Some(line) = line { + line + } + + else { + next_line()? + } + }; + + loop { + if line.starts_with(" .") { + println!(">>> SubSection: {}", line); + } + + else if line.is_empty() { + return Ok(line); + } + + line = { + if let Ok(line) = next_line() { + line + } + + else { + // EOF + return Ok(String::from("")); + } + }; + } +} + +fn read_section Result>(mut line: String, next_line: &mut F) -> Result<(Section, Option), Error> { let mut split_line = line.split_whitespace(); - let name = split_line.next().ok_or(Error::from_str(""))?.to_string(); + let name = split_line.next().ok_or(Error::from_str("Couldn't locate section name"))?.to_string(); let need_new_line = name.chars().count() > 16; if need_new_line {