diff src/th06/pbg3.rs @ 746:0ebf6467e4ff

examples: Add a menu example.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 18 Jan 2020 19:19:51 +0100
parents 7bde50132735
children
line wrap: on
line diff
--- a/src/th06/pbg3.rs
+++ b/src/th06/pbg3.rs
@@ -8,8 +8,10 @@
 
 use crate::util::bitstream::BitStream;
 use crate::util::lzss;
+use std::fs::File;
 use std::io;
 use std::collections::hash_map::{self, HashMap};
+use std::path::Path;
 
 /// Helper struct to handle strings and integers in PBG3 bitstreams.
 pub struct PBG3BitStream<R: io::Read + io::Seek> {
@@ -132,9 +134,9 @@ impl<R: io::Read + io::Seek> PBG3<R> {
     }
 
     /// Read a single file from this PBG3 archive.
-    pub fn get_file(&mut self, filename: String, check: bool) -> io::Result<Vec<u8>> {
+    pub fn get_file(&mut self, filename: &str, check: bool) -> io::Result<Vec<u8>> {
         // XXX: no unwrap!
-        let (_unknown_1, _unknown_2, checksum, offset, size) = self.entries.get(&filename).unwrap();
+        let (_unknown_1, _unknown_2, checksum, offset, size) = self.entries.get(filename).unwrap();
         self.bitstream.seek(io::SeekFrom::Start(*offset as u64))?;
         let data = lzss::decompress(&mut self.bitstream.bitstream, *size as usize, 0x2000, 13, 4, 3)?;
         if check {
@@ -154,6 +156,13 @@ impl<R: io::Read + io::Seek> PBG3<R> {
     }
 }
 
+/// Open a PBG3 archive from its path.
+pub fn from_path_buffered<P: AsRef<Path>>(path: P) -> io::Result<PBG3<io::BufReader<File>>> {
+    let file = File::open(path)?;
+    let buf_file = io::BufReader::new(file);
+    PBG3::from_file(buf_file)
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;