2024-04-16 21:03:26 -04:00
|
|
|
// SPDX-FileCopyrightText: 2024 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2024-04-30 16:17:04 -04:00
|
|
|
use std::io::SeekFrom;
|
|
|
|
use binrw::{BinReaderExt, BinResult};
|
|
|
|
|
2024-04-16 21:03:26 -04:00
|
|
|
pub(crate) fn read_bool_from<T: std::convert::From<u8> + std::cmp::PartialEq>(x: T) -> bool {
|
|
|
|
x == T::from(1u8)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn write_bool_as<T: std::convert::From<u8>>(x: &bool) -> T {
|
2024-04-20 13:18:03 -04:00
|
|
|
if *x {
|
|
|
|
T::from(1u8)
|
|
|
|
} else {
|
|
|
|
T::from(0u8)
|
|
|
|
}
|
2024-04-16 21:03:26 -04:00
|
|
|
}
|
|
|
|
|
2024-04-30 16:17:04 -04:00
|
|
|
#[binrw::parser(reader, endian)]
|
|
|
|
pub(crate) fn strings_parser(base_offset: u64, strings_offset: &Vec<u16>) -> BinResult<Vec<String>> {
|
|
|
|
let mut strings: Vec<String> =
|
|
|
|
vec![];
|
|
|
|
|
|
|
|
for offset in strings_offset {
|
|
|
|
let string_offset = base_offset + *offset as u64;
|
|
|
|
|
|
|
|
let mut string = String::new();
|
|
|
|
|
|
|
|
reader.seek(SeekFrom::Start(string_offset as u64))?;
|
|
|
|
let mut next_char = reader.read_le::<u8>().unwrap() as char;
|
|
|
|
while next_char != '\0' {
|
|
|
|
string.push(next_char);
|
|
|
|
next_char = reader.read_le::<u8>().unwrap() as char;
|
|
|
|
}
|
|
|
|
|
|
|
|
strings.push(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(strings)
|
|
|
|
}
|
|
|
|
|
2024-04-16 21:03:26 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-04-16 21:19:08 -04:00
|
|
|
use super::*;
|
2024-04-16 21:03:26 -04:00
|
|
|
|
|
|
|
const DATA: [u8; 2] = [0u8, 1u8];
|
|
|
|
|
|
|
|
// TODO: add tests for u16
|
2024-04-16 21:19:08 -04:00
|
|
|
|
2024-04-16 21:03:26 -04:00
|
|
|
#[test]
|
|
|
|
fn read_bool_u8() {
|
|
|
|
assert!(!read_bool_from::<u8>(DATA[0]));
|
|
|
|
assert!(read_bool_from::<u8>(DATA[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn write_bool_u8() {
|
|
|
|
assert_eq!(write_bool_as::<u8>(&false), DATA[0]);
|
|
|
|
assert_eq!(write_bool_as::<u8>(&true), DATA[1]);
|
|
|
|
}
|
2024-04-20 13:18:03 -04:00
|
|
|
}
|