Browse Source

Added ByteReader abstraction for file types

Getty Ritter 7 years ago
parent
commit
a813f16f44
2 changed files with 128 additions and 0 deletions
  1. 2 0
      therm_util/src/lib.rs
  2. 126 0
      therm_util/src/reader.rs

+ 2 - 0
therm_util/src/lib.rs

@@ -1,3 +1,5 @@
+pub mod reader;
+
 #[cfg(test)]
 mod tests {
     #[test]

+ 126 - 0
therm_util/src/reader.rs

@@ -0,0 +1,126 @@
+use std::{fs,io,iter,slice,vec};
+
+/// A `ByteReader` is just a tiny wrapper over a mutable byte iterator, so we
+/// can parse things more easily.
+pub struct ByteReader<Rd> {
+    bytes: Rd,
+}
+
+pub type ByteReaderT = ByteReader<Iterator<Item=u8>>;
+
+const MK_OK: &'static Fn(io::Result<u8>) -> Option<u8> = &|s| s.ok();
+
+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
+    pub fn from_reader(r: R) -> Self {
+        let bytes = r.bytes().filter_map(MK_OK);
+        ByteReader { bytes: bytes }
+    }
+}
+
+impl ByteReader<iter::FilterMap<io::Bytes<fs::File>,
+                                &'static Fn(io::Result<u8>) -> Option<u8>>>
+{
+    /// Create a reader by opening a named file for reading
+    pub fn from_file(path: &str) -> io::Result<Self> {
+        let f = try!(fs::File::open(path));
+        Ok(ByteReader::from_reader(f))
+    }
+}
+
+impl ByteReader<vec::IntoIter<u8>> {
+    /// Create a reader from a vector of u8s
+    pub fn from_vec(lst: Vec<u8>) -> Self {
+        ByteReader { bytes: lst.into_iter() }
+    }
+}
+
+impl<'a> ByteReader<slice::Iter<'a, u8>> {
+    pub fn from_slice(lst: &'a [u8]) -> Self {
+        ByteReader { bytes: lst.iter() }
+    }
+}
+
+impl<T> ByteReader<T> where T: Iterator<Item=u8> {
+    /// This gets the next byte, or fails if it's out of input.
+    pub fn next(&mut self) -> Result<u8, String> {
+        Ok(try!(self.bytes.next().ok_or("out of input")))
+    }
+
+    /// This reads a one-byte or two-byte float in the rough range of
+    /// (192 .. -128). We're going to treat all models as if they're
+    /// supposed to be centered in a 64x64x64 square, so this gives
+    /// us 128 units on either side of the central square, as well.
+    pub fn read_twip(&mut self) -> Result<f32, String> {
+        let b1 = try!(self.next());
+        if (b1 & 0x80) != 0 {
+            let b2 = try!(self.next());
+            let val = ((b1 as u16 & 0x7f) << 8) | b2 as u16;
+            Ok((val as f32 / 102.0) - 128.0)
+        } else {
+            Ok((b1 as f32) - 32.0)
+        }
+    }
+
+    /// This reads a single byte and treats it as a ratio.
+    pub fn read_ratio(&mut self) -> Result<f32, String> {
+        let b = try!(self.next());
+        Ok(b as f32 / 255.0)
+    }
+
+    /// This reads a 64-bit int with a packed PrefixInteger representation.
+    /// The shorter the int, the shorter the representation.
+    pub fn read_prefix_int(&mut self) -> Result<u64, String> {
+        fn match_bits(n: u8, mask: u8) -> bool {
+            n & mask == mask
+        }
+        let b = try!(self.next());
+        if match_bits(b, 0xff)      { self.continue_prefix_int(8, 0) }
+        else if match_bits(b, 0xfe) { self.continue_prefix_int(7, 0) }
+        else if match_bits(b, 0xfc) { self.continue_prefix_int(6, b & 0x01) }
+        else if match_bits(b, 0xf8) { self.continue_prefix_int(5, b & 0x03) }
+        else if match_bits(b, 0xf0) { self.continue_prefix_int(4, b & 0x07) }
+        else if match_bits(b, 0xe0) { self.continue_prefix_int(3, b & 0x0f) }
+        else if match_bits(b, 0xc0) { self.continue_prefix_int(2, b & 0x1f) }
+        else if match_bits(b, 0x80) { self.continue_prefix_int(1, b & 0x3f) }
+        else {                        self.continue_prefix_int(0, b) }
+    }
+
+    /// This is a helper function for parsing prefix ints, too.
+    fn continue_prefix_int(&mut self, mut left: u8, upper: u8) -> Result<u64, String> {
+        let mut ret = upper as u64;
+        while left > 0 {
+            left -= 1;
+            ret = (ret << 8) | try!(self.next()) as u64;
+        }
+        Ok(ret)
+    }
+
+    /// This reads a PrefixInteger to find out how many other things to read,
+    /// and then reads that number of things.
+    pub fn read_several<F, R>(&mut self, reader: F) -> Result<Vec<R>, String>
+        where F: Fn(&mut ByteReader<T>) -> Result<R, String>
+    {
+        let ct = try!(self.read_prefix_int());
+        let mut ret = Vec::with_capacity(ct as usize);
+        for _ in 0..ct {
+            ret.push(try!(reader(self)))
+        }
+        Ok(ret)
+    }
+
+    /// This reads a PrefixInteger number of bytes, and then parses those
+    /// bytes as a UTF-8 string. This means, importantly, that we cannot
+    /// naïvely produce values intended to be parsed with this using a
+    /// basic string length.
+    pub fn read_string(&mut self) -> Result<String, String> {
+        let raw_bytes = try!(self.read_several(|r| r.next()));
+        match String::from_utf8(raw_bytes) {
+            Ok(s) => Ok(s),
+            Err(e) => Err(format!("Exception when parsing UTF-8: {:?}", e)),
+        }
+    }
+}