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 core::fmt;
use alloc::{string::FromUtf8Error, string::String, vec::Vec, rc::Rc}; use alloc::{string::FromUtf8Error, string::String, vec::Vec, rc::Rc};
use fatfs::{self as fatfs, SeekFrom, Write, Read, Seek};
use libboard_zynq::sdio; use libboard_zynq::sdio;
pub mod sd_reader; pub mod sd_reader;
@ -53,32 +54,20 @@ impl<'a> From<FromUtf8Error> for Error<'a> {
fn parse_config<'a>( fn parse_config<'a>(
key: &'a str, key: &'a str,
buffer: &mut Vec<u8>, buffer: &mut Vec<u8>,
file: fatfs::File<sd_reader::SdReader>, file: fatfs::File<sd_reader::SdReader, fatfs::DefaultTimeProvider, fatfs::LossyOemCpConverter>,
) -> Result<'a, ()> { ) -> Result<'a, ()> {
let prefix = [key, "="].concat().to_ascii_lowercase(); let prefix = [key, "="].concat().to_ascii_lowercase();
let mut file_buf: Vec<u8> = Vec::new(); let file_buf = &mut [];
loop { let length = file.read(file_buf)?;
match memchr::memchr(b'\n', &file_buf) { let s = core::str::from_utf8(file_buf)?;
Some(i) => { for line in s.lines() {
let s = String::from_utf8(&file_buf[..=i]) let line = line.make_ascii_lowercase()?;
if s.starts_with(&prefix) { if line.starts_with(&prefix) {
buffer.extend_from_slice(&file_buf[..=i]); buffer.extend(line[prefix.len()..].as_bytes());
return Ok(()) 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 { pub struct Config {