|
@@ -1,4 +1,5 @@
|
|
-use std::{fs,io,iter,slice,vec};
|
|
|
|
|
|
+use std::{fs,io,iter,mem,slice,vec};
|
|
|
|
+use bzip2::bufread::BzDecoder;
|
|
|
|
|
|
/// A `ByteReader` is just a tiny wrapper over a mutable byte iterator, so we
|
|
/// A `ByteReader` is just a tiny wrapper over a mutable byte iterator, so we
|
|
/// can parse things more easily.
|
|
/// can parse things more easily.
|
|
@@ -6,9 +7,7 @@ pub struct ByteReader<Rd> {
|
|
bytes: Rd,
|
|
bytes: Rd,
|
|
}
|
|
}
|
|
|
|
|
|
-impl<R: io::Read>
|
|
|
|
- ByteReader<iter::FilterMap<io::Bytes<R>,
|
|
|
|
- &'static Fn(io::Result<u8>) -> Option<u8>>>
|
|
|
|
|
|
+impl<R: io::Read> ByteReader<iter::FilterMap<io::Bytes<R>, &'static Fn(io::Result<u8>) -> Option<u8>>>
|
|
{
|
|
{
|
|
/// Create a ByteReader from any type that implement Read
|
|
/// Create a ByteReader from any type that implement Read
|
|
pub fn from_reader(r: R) -> Self {
|
|
pub fn from_reader(r: R) -> Self {
|
|
@@ -35,11 +34,23 @@ impl ByteReader<vec::IntoIter<u8>> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-impl<'a> ByteReader<iter::Map<slice::Iter<'a, u8>, &'static Fn(&u8) -> u8>> {
|
|
|
|
|
|
+impl<'a> ByteReader<iter::Cloned<slice::Iter<'a, u8>>> {
|
|
/// Create a reader from a borrowed slice, with a copy on each access
|
|
/// Create a reader from a borrowed slice, with a copy on each access
|
|
pub fn from_slice(lst: &'a [u8]) -> Self {
|
|
pub fn from_slice(lst: &'a [u8]) -> Self {
|
|
- const DEREF: &'static Fn(&u8) -> u8 = &|s| *s;
|
|
|
|
- ByteReader { bytes: lst.iter().map(DEREF) }
|
|
|
|
|
|
+ ByteReader { bytes: lst.iter().cloned() }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl<R: io::BufRead> ByteReader<iter::FilterMap<io::Bytes<BzDecoder<R>>, &'static Fn(io::Result<u8>) -> Option<u8>>> {
|
|
|
|
+ pub fn from_compressed_reader(r: R) -> Self {
|
|
|
|
+ ByteReader::from_reader(BzDecoder::new(r))
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl ByteReader<iter::FilterMap<io::Bytes<BzDecoder<io::BufReader<fs::File>>>, &'static Fn(io::Result<u8>) -> Option<u8>>> {
|
|
|
|
+ pub fn from_compressed_file(path: &str) -> io::Result<Self> {
|
|
|
|
+ let f = try!(fs::File::open(path));
|
|
|
|
+ Ok(ByteReader::from_compressed_reader(io::BufReader::new(f)))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -81,6 +92,22 @@ impl<T> ByteReader<T> where T: Iterator<Item=u8> {
|
|
Ok(b as f32 / 255.0)
|
|
Ok(b as f32 / 255.0)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ pub fn read_u32be(&mut self) -> Result<u32, String> {
|
|
|
|
+ let a = try!(self.next());
|
|
|
|
+ let b = try!(self.next());
|
|
|
|
+ let c = try!(self.next());
|
|
|
|
+ let d = try!(self.next());
|
|
|
|
+ let rs = [ d, c, b, a ];
|
|
|
|
+ unsafe { Ok(mem::transmute::<[u8;4],u32>(rs)) }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub fn read_u16be(&mut self) -> Result<u16, String> {
|
|
|
|
+ let a = try!(self.next());
|
|
|
|
+ let b = try!(self.next());
|
|
|
|
+ let rs = [ b, a ];
|
|
|
|
+ unsafe { Ok(mem::transmute::<[u8;2],u16>(rs)) }
|
|
|
|
+ }
|
|
|
|
+
|
|
/// This reads a 64-bit int with a packed PrefixInteger representation.
|
|
/// This reads a 64-bit int with a packed PrefixInteger representation.
|
|
/// The shorter the int, the shorter the representation.
|
|
/// The shorter the int, the shorter the representation.
|
|
pub fn read_prefix_int(&mut self) -> Result<u64, String> {
|
|
pub fn read_prefix_int(&mut self) -> Result<u64, String> {
|