1pub struct BinaryReader {
2 pub data: Vec<u8>,
3 pub position: usize
4}
5
6impl BinaryReader {
7 pub fn new(path: &str) -> Result<BinaryReader, std::io::Error> {
8 let data = std::fs::read(path);
9 let data = match data {
10 Ok(dat) => dat,
11 Err(error) => return Err(error),
12 };
13
14 Ok(BinaryReader {
15 data: data,
16 position: 0
17 })
18 }
19
20 pub fn read_u8(&mut self) -> u8 {
21 let data = self.data[self.position];
22 self.position += 1;
23 data
24 }
25
26 pub fn read_i16(&mut self) -> i16 {
27 let b1 = self.read_u8() as i16;
28 let b2 = self.read_u8() as i16;
29
30 b1 | (b2 << 8)
31 }
32
33 pub fn read_i32(&mut self) -> i32 {
34 let b1 = self.read_u8() as i32;
35 let b2 = self.read_u8() as i32;
36 let b3 = self.read_u8() as i32;
37 let b4 = self.read_u8() as i32;
38
39 b1 | (b2 << 8) | (b3 << 16) | (b4 << 24)
40 }
41
42 pub fn read_string(&mut self, num_chars: i32) -> String {
43 let mut text = String::new();
44
45 for _ in 0..num_chars {
46 text.push(self.data[self.position] as char);
47 self.position += 1;
48 }
49
50 text
51 }
52
53 pub fn read_bytes(&mut self, num_bytes: usize) -> Vec<u8> {
54 let data = self.data[self.position..(self.position + num_bytes)].to_vec();
55 self.position += num_bytes;
56 data
57 }
58}