From 8b25b2fadd085ab9aceba64135bcd9b015b11ce5 Mon Sep 17 00:00:00 2001 From: _ <> Date: Thu, 9 Jan 2020 00:18:12 +0000 Subject: [PATCH] Sending the IQM file into nom --- src/main.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 76138b6..8959838 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,11 +11,46 @@ use sdl2::keyboard::Keycode; use std::collections::HashMap; use std::convert::TryInto; use std::ffi::{c_void, CStr, CString}; +use std::fs::File; +use std::io::Read; +use std::path::Path; use std::time::Duration; -pub fn parse_iqm (input: &str) -> IResult<&str, ()> { - let (input, _) = tag("#")(input)?; - Ok((input, ())) +pub fn parse_iqm (input: &[u8]) -> IResult<&[u8], ()> { + let (input, _) = tag (b"INTERQUAKEMODEL\0")(input)?; + Ok ((input, ())) +} + +pub struct IqmBuffer { + data: Vec , + +} + +pub fn load_small_file

(name: P) -> Vec +where P: AsRef +{ + let mut f = File::open (name).unwrap (); + let len = f.metadata ().unwrap ().len (); + + if len > 1024 * 1024 { + panic! ("File is too big"); + } + + let mut data = vec! [0u8; len.try_into ().unwrap ()]; + + f.read (&mut data [..]).unwrap (); + + data +} + +impl IqmBuffer { + pub fn from_buffer (data: Vec ) -> IqmBuffer { + assert_eq! (parse_iqm (&data [..]), Ok ((&data [16..], ()))); + + IqmBuffer { + data, + } + } } const VERT_SHADER_SRC: &str = @@ -326,6 +361,8 @@ mod tests { #[test] pub fn iqm () { - assert_eq! (parse_iqm ("#"), Ok (("", ()))); + assert_eq! (parse_iqm (b"INTERQUAKEMODEL\0"), Ok ((&b"" [..], ()))); + + IqmBuffer::from_buffer (load_small_file ("pumpking.iqm")); } }