Commit 041a7ae9 authored by Nagy Tibor's avatar Nagy Tibor
Browse files

Implement io::Seek for FILE, require it for scanf

parent d1f267af
Pipeline #8690 failed with stages
in 9 seconds
......@@ -24,7 +24,7 @@ use crate::{
string::{self, strlen},
unistd,
},
io::{self, BufRead, BufReader, Cursor, LineWriter, Read, Write},
io::{self, BufRead, BufReader, Cursor, LineWriter, Read, Seek, Write},
platform::{self, errno, types::*, Pal, Sys, WriteByte},
sync::Mutex,
};
......@@ -50,6 +50,7 @@ enum Buffer<'a> {
Borrowed(&'a mut [u8]),
Owned(Vec<u8>),
}
impl<'a> Deref for Buffer<'a> {
type Target = [u8];
......@@ -60,6 +61,7 @@ impl<'a> Deref for Buffer<'a> {
}
}
}
impl<'a> DerefMut for Buffer<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
......@@ -111,6 +113,7 @@ impl Read for FILE {
Ok(len)
}
}
impl BufRead for FILE {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
if self.read_pos == self.read_size {
......@@ -129,10 +132,12 @@ impl BufRead for FILE {
}
Ok(&self.read_buf[self.read_pos..self.read_size])
}
fn consume(&mut self, i: usize) {
self.read_pos = (self.read_pos + i).min(self.read_size);
}
}
impl Write for FILE {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match self.writer.write(buf) {
......@@ -143,6 +148,7 @@ impl Write for FILE {
}
}
}
fn flush(&mut self) -> io::Result<()> {
match self.writer.flush() {
Ok(()) => Ok(()),
......@@ -153,6 +159,21 @@ impl Write for FILE {
}
}
}
impl Seek for FILE {
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
let (offset, whence) = match pos {
io::SeekFrom::Start(start) => (start as off_t, SEEK_SET),
io::SeekFrom::Current(current) => (current as off_t, SEEK_CUR),
io::SeekFrom::End(end) => (end as off_t, SEEK_END),
};
match unsafe { fseek_locked(self, offset, whence) } {
-1 => Err(io::last_os_error()),
ok => Ok(ok as u64),
}
}
}
impl WriteFmt for FILE {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_all(s.as_bytes())
......@@ -160,11 +181,13 @@ impl WriteFmt for FILE {
.map_err(|_| fmt::Error)
}
}
impl WriteByte for FILE {
fn write_u8(&mut self, c: u8) -> fmt::Result {
self.write_all(&[c]).map(|_| ()).map_err(|_| fmt::Error)
}
}
impl FILE {
pub fn lock(&mut self) -> LockGuard {
unsafe {
......@@ -205,11 +228,13 @@ impl<'a> Deref for LockGuard<'a> {
&self.0
}
}
impl<'a> DerefMut for LockGuard<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.0
}
}
impl<'a> Drop for LockGuard<'a> {
fn drop(&mut self) {
unsafe {
......
use crate::io::{self, Read};
use crate::io::{self, Read, Seek};
use crate::platform::types::*;
use alloc::{string::String, vec::Vec};
use core::ffi::VaList as va_list;
......@@ -248,7 +248,7 @@ impl<'a> Iterator for FormatStringParser<'a> {
}
fn inner_scanf(
mut r: impl Read,
mut r: impl Read + Seek,
mut format: &str,
mut ap: va_list,
) -> Result<c_int, c_int> {
......@@ -266,7 +266,7 @@ fn inner_scanf(
Err(-1)
}
pub fn scanf(r: impl Read, format: &str, ap: va_list) -> c_int {
pub fn scanf(r: impl Read + Seek, format: &str, ap: va_list) -> c_int {
match inner_scanf(r, format, ap) {
Ok(n) => n,
Err(n) => n,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment