forked from M-Labs/zynq-rs
libconfig fix parse_config
This commit is contained in:
parent
87cfff9070
commit
84c45ec235
|
@ -3,6 +3,7 @@ extern crate alloc;
|
|||
|
||||
use core::fmt;
|
||||
use alloc::{string::FromUtf8Error, string::String, vec::Vec, rc::Rc};
|
||||
use fatfs::{self as fatfs, SeekFrom, Write, Read, Seek};
|
||||
use libboard_zynq::sdio;
|
||||
|
||||
pub mod sd_reader;
|
||||
|
@ -53,32 +54,20 @@ impl<'a> From<FromUtf8Error> for Error<'a> {
|
|||
fn parse_config<'a>(
|
||||
key: &'a str,
|
||||
buffer: &mut Vec<u8>,
|
||||
file: fatfs::File<sd_reader::SdReader>,
|
||||
file: fatfs::File<sd_reader::SdReader, fatfs::DefaultTimeProvider, fatfs::LossyOemCpConverter>,
|
||||
) -> Result<'a, ()> {
|
||||
let prefix = [key, "="].concat().to_ascii_lowercase();
|
||||
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))
|
||||
}
|
||||
}
|
||||
let file_buf = &mut [];
|
||||
let length = file.read(file_buf)?;
|
||||
let s = core::str::from_utf8(file_buf)?;
|
||||
for line in s.lines() {
|
||||
let line = line.make_ascii_lowercase()?;
|
||||
if line.starts_with(&prefix) {
|
||||
buffer.extend(line[prefix.len()..].as_bytes());
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Err(Error::KeyNotFoundError(key))
|
||||
}
|
||||
|
||||
pub struct Config {
|
||||
|
|
Loading…
Reference in New Issue