Add scanf

This commit is contained in:
jD91mZM2
2018-06-21 16:45:10 +02:00
parent 6f8fff4b1c
commit 5936c7a76e
10 changed files with 677 additions and 2 deletions
+49
View File
@@ -82,6 +82,16 @@ impl<'a, W: Write> Write for &'a mut W {
}
}
pub trait Read {
fn read_u8(&mut self, byte: &mut u8) -> bool;
}
impl<'a, R: Read> Read for &'a mut R {
fn read_u8(&mut self, byte: &mut u8) -> bool {
(**self).read_u8(byte)
}
}
pub struct FileWriter(pub c_int);
impl FileWriter {
@@ -112,6 +122,15 @@ impl FileReader {
}
}
impl Read for FileReader {
fn read_u8(&mut self, byte: &mut u8) -> bool {
let mut buf = [*byte];
let n = self.read(&mut buf);
*byte = buf[0];
n > 0
}
}
pub struct StringWriter(pub *mut u8, pub usize);
impl StringWriter {
@@ -174,3 +193,33 @@ impl Write for UnsafeStringWriter {
Ok(())
}
}
pub struct StringReader<'a>(pub &'a [u8]);
impl<'a> Read for StringReader<'a> {
fn read_u8(&mut self, byte: &mut u8) -> bool {
if self.0.is_empty() {
false
} else {
*byte = self.0[0];
self.0 = &self.0[1..];
true
}
}
}
pub struct UnsafeStringReader(pub *const u8);
impl Read for UnsafeStringReader {
fn read_u8(&mut self, byte: &mut u8) -> bool {
unsafe {
if *self.0 == 0 {
false
} else {
*byte = *self.0;
self.0 = self.0.offset(1);
true
}
}
}
}