forked from M-Labs/zynq-rs
fix libconfig errors
This commit is contained in:
parent
5523d22e43
commit
050ea011eb
|
@ -56,9 +56,9 @@ impl<'a> From<FromUtf8Error> for Error<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn read_to_string<'a>(file: &mut File<'a>, read_str: &mut String) -> Result<'a, ()> {
|
||||
let mut buffer = Vec::new();
|
||||
let mut temp_buffer = [0; 500];
|
||||
// Simplified replacements to `read_to_end` and `read_to_string` from core_io
|
||||
fn read_to_end<'a>(file: &mut File<'a>, buffer: &mut Vec<u8>) -> Result<'a, ()> {
|
||||
let mut temp_buffer = [0; 1024];
|
||||
loop {
|
||||
let read_bytes = file.read(&mut temp_buffer)?;
|
||||
if read_bytes != buffer.len() {
|
||||
|
@ -69,18 +69,22 @@ fn read_to_string<'a>(file: &mut File<'a>, read_str: &mut String) -> Result<'a,
|
|||
buffer.extend_from_slice(&temp_buffer);
|
||||
}
|
||||
}
|
||||
read_str.extend(String::from_utf8(buffer));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_to_string<'a>(file: &mut File<'a>) -> Result<'a, String> {
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
read_to_end(file, &mut buffer);
|
||||
Ok(String::from_utf8(buffer)?)
|
||||
}
|
||||
|
||||
fn parse_config<'a>(
|
||||
key: &'a str,
|
||||
buffer: &mut Vec<u8>,
|
||||
mut file: File<'a>,
|
||||
) -> Result<'a, ()> {
|
||||
let prefix = [key, "="].concat().to_ascii_lowercase();
|
||||
let mut read_buffer = String::new();
|
||||
read_to_string(&mut file, &mut read_buffer)?;
|
||||
let read_buffer = read_to_string(&mut file)?;
|
||||
for line in read_buffer.lines() {
|
||||
let line = line.to_ascii_lowercase();
|
||||
if line.starts_with(&prefix) {
|
||||
|
@ -125,7 +129,7 @@ impl Config {
|
|||
let root_dir = fs.root_dir();
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
match root_dir.open_file(&["/CONFIG/", key, ".BIN"].concat()) {
|
||||
Ok(mut f) => f.read(&mut buffer).map(|_| ())?,
|
||||
Ok(mut f) => read_to_end( &mut f, &mut buffer)?,
|
||||
Err(_) => match root_dir.open_file("/CONFIG.TXT") {
|
||||
Ok(f) => parse_config(key, &mut buffer, f)?,
|
||||
Err(_) => return Err(Error::KeyNotFoundError(key)),
|
||||
|
@ -150,8 +154,7 @@ impl Config {
|
|||
let prefix = [key, "="].concat().to_ascii_lowercase();
|
||||
match root_dir.create_file("/CONFIG.TXT") {
|
||||
Ok(mut f) => {
|
||||
let mut buffer = String::new();
|
||||
read_to_string(&mut f, &mut buffer)?;
|
||||
let buffer = read_to_string(&mut f)?;
|
||||
f.seek(SeekFrom::Start(0))?;
|
||||
f.truncate()?;
|
||||
for line in buffer.lines() {
|
||||
|
|
Loading…
Reference in New Issue