forked from M-Labs/zynq-rs
libconfig rewrites
This commit is contained in:
parent
a11d94f959
commit
83959b2691
@ -3,7 +3,6 @@ extern crate alloc;
|
||||
|
||||
use core::fmt;
|
||||
use alloc::{string::FromUtf8Error, string::String, vec::Vec, rc::Rc};
|
||||
use core_io::{self as io, BufRead, BufReader, Read, Write, Seek, SeekFrom};
|
||||
use libboard_zynq::sdio;
|
||||
|
||||
pub mod sd_reader;
|
||||
@ -13,7 +12,7 @@ pub mod bootgen;
|
||||
#[derive(Debug)]
|
||||
pub enum Error<'a> {
|
||||
SdError(sdio::sd_card::CardInitializationError),
|
||||
IoError(io::Error),
|
||||
IoError(sd_reader::SdReader::Error),
|
||||
Utf8Error(FromUtf8Error),
|
||||
KeyNotFoundError(&'a str),
|
||||
NoConfig,
|
||||
@ -39,8 +38,8 @@ impl<'a> From<sdio::sd_card::CardInitializationError> for Error<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<io::Error> for Error<'a> {
|
||||
fn from(error: io::Error) -> Self {
|
||||
impl<'a> From<sd_reader::SdReader::Error> for Error<'a> {
|
||||
fn from(error: sd_reader::SdReader::Error) -> Self {
|
||||
Error::IoError(error)
|
||||
}
|
||||
}
|
||||
@ -57,14 +56,29 @@ fn parse_config<'a>(
|
||||
file: fatfs::File<sd_reader::SdReader>,
|
||||
) -> Result<'a, ()> {
|
||||
let prefix = [key, "="].concat().to_ascii_lowercase();
|
||||
for line in BufReader::new(file).lines() {
|
||||
let line = line?.to_ascii_lowercase();
|
||||
if line.starts_with(&prefix) {
|
||||
buffer.extend(line[prefix.len()..].as_bytes());
|
||||
return Ok(());
|
||||
let mut file_buf: Vec<u8> = Vec::new();
|
||||
loop {
|
||||
match memchr::memchr(b'\n', &file_buf) {
|
||||
Some(i) => {
|
||||
let s = String::from_utf8(&file_buf[..=i])
|
||||
if s.starts_with(&prefix) {
|
||||
buffer.extend_from_slice(&file_buf[..=i]);
|
||||
return Ok(())
|
||||
} else {
|
||||
file.consume(i + 1)
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let s = String::from_utf8(file_buf)
|
||||
if s.starts_with(&prefix) {
|
||||
buffer.extend_from_slice(file_buf);
|
||||
return Ok(())
|
||||
} else {
|
||||
return Err(Error::KeyNotFoundError(key))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(Error::KeyNotFoundError(key))
|
||||
}
|
||||
|
||||
pub struct Config {
|
||||
|
@ -188,6 +188,25 @@ impl SdReader {
|
||||
fn cmd_error_to_io_error(_: CmdTransferError) -> Self::Error {
|
||||
Self::Error::Other("Command transfer error")
|
||||
}
|
||||
fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
|
||||
if self.index == BLOCK_SIZE {
|
||||
// flush the buffer if it is dirty before overwriting it with new data
|
||||
if self.dirty {
|
||||
self.flush()?;
|
||||
}
|
||||
// reload buffer
|
||||
self.sd
|
||||
.read_block(self.byte_addr / (BLOCK_SIZE as u32), 1, &mut self.buffer)
|
||||
.map_err(cmd_error_to_io_error)?;
|
||||
self.index = (self.byte_addr as usize) % BLOCK_SIZE;
|
||||
}
|
||||
Ok(&self.buffer[self.index..])
|
||||
}
|
||||
|
||||
fn consume(&mut self, amt: usize) {
|
||||
self.index += amt;
|
||||
self.byte_addr += amt as u32;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -217,28 +236,6 @@ impl Read for SdReader {
|
||||
}
|
||||
}
|
||||
|
||||
// impl BufRead for SdReader {
|
||||
// fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
|
||||
// if self.index == BLOCK_SIZE {
|
||||
// // flush the buffer if it is dirty before overwriting it with new data
|
||||
// if self.dirty {
|
||||
// self.flush()?;
|
||||
// }
|
||||
// // reload buffer
|
||||
// self.sd
|
||||
// .read_block(self.byte_addr / (BLOCK_SIZE as u32), 1, &mut self.buffer)
|
||||
// .map_err(cmd_error_to_io_error)?;
|
||||
// self.index = (self.byte_addr as usize) % BLOCK_SIZE;
|
||||
// }
|
||||
// Ok(&self.buffer[self.index..])
|
||||
// }
|
||||
//
|
||||
// fn consume(&mut self, amt: usize) {
|
||||
// self.index += amt;
|
||||
// self.byte_addr += amt as u32;
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Write for SdReader {
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
||||
let (a, b, c) = self.block_align(buf);
|
||||
|
Loading…
Reference in New Issue
Block a user