Skip to content

Instantly share code, notes, and snippets.

@cretz
Created December 5, 2012 03:33
Show Gist options
  • Save cretz/4211946 to your computer and use it in GitHub Desktop.
Save cretz/4211946 to your computer and use it in GitHub Desktop.
impl @ZipFile: io::Reader {
pub fn read(bytes: &[mut u8], len: uint) -> uint {
io::println(fmt!("Calling read for %? bytes", len));
if !self.open {
0
} else {
do vec::as_mut_buf(bytes) |buf_p, buf_len| {
assert buf_len >= len;
let count = zip_fread(self.file, buf_p as *c_void, len as uint64_t);
if count == EOF as int64_t { self.open = false; }
count as uint
}
}
}
pub fn read_byte() -> int {
io::println("Calling read for 1 byte");
let bytes: ~[mut u8] = ~[mut];
let count = self.read(bytes, 1);
if count != 1 {
EOF
} else {
bytes[0] as int
}
}
pub fn unread_byte(_byte: int) { fail ~"Unsupported" }
pub fn eof() -> bool { self.open }
pub fn seek(_offset: int, _whence: io::SeekStyle) { fail ~"Unsupported" }
pub fn tell() -> uint { fail ~"Unsupported" }
}
#[cfg(test)]
mod tests {
#[test]
fn simple_read() {
let archive_res = open_archive(~"test_resources/test.zip", 0);
let archive = result::get(&archive_res);
let file_res = archive.open_name(~"test", 0);
let file = file_res.get();
io::println(fmt!("Reader: %?", file));
let bytes = (file as io::ReaderUtil).read_whole_stream();
io::println(fmt!("Bytes: %?", bytes));
assert bytes.len() == 5;
assert str::from_bytes(bytes).trim() == ~"test";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment