From 94e927f967c1d31b4ec379445104aef2d4c90e91 Mon Sep 17 00:00:00 2001 From: panicbit Date: Sat, 20 Aug 2016 04:35:56 +0200 Subject: [PATCH 1/2] Add read method to fs File --- src/services/fs.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/services/fs.rs b/src/services/fs.rs index 688341e..247a5b1 100644 --- a/src/services/fs.rs +++ b/src/services/fs.rs @@ -53,6 +53,7 @@ pub struct Archive { pub struct File { handle: u32, + offset: u64, } pub struct OpenOptions { @@ -111,6 +112,29 @@ impl Archive { } } +impl File { + pub fn read(&mut self, buf: &mut [u8]) -> Result { + unsafe { + let mut n_read = 0; + let r = FSFILE_Read( + self.handle, + &mut n_read, + self.offset, + buf.as_mut_ptr() as _, + buf.len() as u32 + ); + + self.offset += n_read as u64; + + if r < 0 { + Err(r) + } else { + Ok(n_read) + } + } + } +} + impl OpenOptions { pub fn read(&mut self, read: bool) -> &mut OpenOptions { self.read = read; @@ -140,7 +164,10 @@ impl OpenOptions { if ret < 0 { Err(ret) } else { - Ok(File { handle: file_handle }) + Ok(File { + handle: file_handle, + offset: 0, + }) } } } From d604db441014c2270c9fc63a08337996f0a92a2d Mon Sep 17 00:00:00 2001 From: panicbit Date: Sat, 20 Aug 2016 04:36:14 +0200 Subject: [PATCH 2/2] Add len method to fs File --- src/services/fs.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/services/fs.rs b/src/services/fs.rs index 247a5b1..ac84667 100644 --- a/src/services/fs.rs +++ b/src/services/fs.rs @@ -113,6 +113,18 @@ impl Archive { } impl File { + pub fn len(&self) -> Result { + unsafe { + let mut len = 0; + let r = FSFILE_GetSize(self.handle, &mut len); + if r < 0 { + Err(r) + } else { + Ok(len) + } + } + } + pub fn read(&mut self, buf: &mut [u8]) -> Result { unsafe { let mut n_read = 0;