1
0
Fork 0

libconfig fix parse_config

This commit is contained in:
Simon Renblad 2024-07-30 14:03:26 +08:00
parent 87cfff9070
commit 84c45ec235
1 changed files with 11 additions and 22 deletions

View File

@ -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 {