Add more scanf tests

This commit is contained in:
oddcoder
2020-06-03 23:19:08 +02:00
parent 8973535fdc
commit 92d6735e3f
12 changed files with 66 additions and 35 deletions
+11 -16
View File
@@ -1,4 +1,4 @@
use super::{fseek_locked, FILE, SEEK_SET, ftell_locked};
use super::{fseek_locked, ftell_locked, FILE, SEEK_SET};
use crate::core_io::Read;
struct LookAheadBuffer {
buf: *const u8,
@@ -27,23 +27,22 @@ impl From<*const u8> for LookAheadBuffer {
}
}
struct LookAheadFile<'a> {
f: &'a mut FILE,
look_ahead: i64,
}
impl <'a> LookAheadFile<'a> {
impl<'a> LookAheadFile<'a> {
fn look_ahead(&mut self) -> Result<Option<u8>, i32> {
let buf = &mut [0];
let seek = unsafe{ ftell_locked(self.f)};
unsafe{fseek_locked(self.f, self.look_ahead, SEEK_SET)};
let seek = unsafe { ftell_locked(self.f) };
unsafe { fseek_locked(self.f, self.look_ahead, SEEK_SET) };
let ret = match self.f.read(buf) {
Ok(0) => Ok(None),
Ok(_) => Ok(Some(buf[0])),
Err(_) => Err(-1),
};
unsafe{fseek_locked(self.f, seek, SEEK_SET)};
unsafe { fseek_locked(self.f, seek, SEEK_SET) };
self.look_ahead += 1;
ret
}
@@ -53,17 +52,13 @@ impl <'a> LookAheadFile<'a> {
}
}
impl <'a> From<&'a mut FILE> for LookAheadFile<'a> {
impl<'a> From<&'a mut FILE> for LookAheadFile<'a> {
fn from(f: &'a mut FILE) -> LookAheadFile<'a> {
let look_ahead = unsafe{ftell_locked(f)} as i64;
LookAheadFile{
f,
look_ahead,
}
let look_ahead = unsafe { ftell_locked(f) } as i64;
LookAheadFile { f, look_ahead }
}
}
enum LookAheadReaderEnum<'a> {
FILE(LookAheadFile<'a>),
// (buffer, location)
@@ -72,7 +67,7 @@ enum LookAheadReaderEnum<'a> {
pub struct LookAheadReader<'a>(LookAheadReaderEnum<'a>);
impl <'a> LookAheadReader<'a> {
impl<'a> LookAheadReader<'a> {
pub fn lookahead1(&mut self) -> Result<Option<u8>, i32> {
match &mut self.0 {
LookAheadReaderEnum::FILE(f) => f.look_ahead(),
@@ -87,13 +82,13 @@ impl <'a> LookAheadReader<'a> {
}
}
impl <'a> From<&'a mut FILE> for LookAheadReader<'a> {
impl<'a> From<&'a mut FILE> for LookAheadReader<'a> {
fn from(f: &'a mut FILE) -> LookAheadReader {
LookAheadReader(LookAheadReaderEnum::FILE(f.into()))
}
}
impl <'a> From<*const u8> for LookAheadReader<'a > {
impl<'a> From<*const u8> for LookAheadReader<'a> {
fn from(buff: *const u8) -> LookAheadReader<'a> {
LookAheadReader(LookAheadReaderEnum::BUFFER(buff.into()))
}
+1 -2
View File
@@ -516,7 +516,6 @@ pub unsafe extern "C" fn fseek(stream: *mut FILE, offset: c_long, whence: c_int)
/// Seek to an offset `offset` from `whence`
#[no_mangle]
pub unsafe extern "C" fn fseeko(stream: *mut FILE, off: off_t, whence: c_int) -> c_int {
let mut stream = (*stream).lock();
fseek_locked(&mut *stream, off, whence)
}
@@ -1051,7 +1050,7 @@ pub unsafe extern "C" fn vsprintf(s: *mut c_char, format: *const c_char, ap: va_
pub unsafe extern "C" fn vfscanf(file: *mut FILE, format: *const c_char, ap: va_list) -> c_int {
let ret = {
let mut file = (*file).lock();
let f :&mut FILE = &mut *file;
let f: &mut FILE = &mut *file;
let reader: LookAheadReader = f.into();
scanf::scanf(reader, format, ap)
};