Skip to content

Commit

Permalink
fixed parameter block offset
Browse files Browse the repository at this point in the history
  • Loading branch information
cvhammond committed Aug 21, 2023
1 parent 4c7f9a1 commit 089e4bc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "c3dio"
version = "0.1.0"
version = "0.2.0"
authors = ["Claire V. Hammond <claire@biomech.dev>"]
edition = "2021"
categories = ["encoding", "filesystem", "parser-implementation", "motion-capture", "biomechanics"]
Expand Down
10 changes: 4 additions & 6 deletions src/c3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ impl C3d {

/// Parses a C3D file from a byte slice.
pub fn from_bytes(bytes: &[u8]) -> Result<C3d, C3dParseError> {
let c3d = C3d::new()?
Ok(C3d::new()?
.parse_basic_info_from_bytes(bytes)?
.parse_header()?
.parse_parameters()?
.parse_data_from_bytes(bytes)?;

Ok(c3d)
.parse_data_from_bytes(bytes)?)
}

/// Parses a C3D file with just the header data.
Expand Down Expand Up @@ -148,7 +146,7 @@ impl C3d {
return Err(C3dParseError::InsufficientBlocks("data".to_string()));
}

self.bytes.parameter = bytes[512..(512 * (self.bytes.data_start_block_index))]
self.bytes.parameter = bytes[512..(512 * (self.bytes.data_start_block_index - 1))]
.try_into()
.unwrap();

Expand Down Expand Up @@ -203,7 +201,7 @@ impl C3d {
}

fn parse_data_from_bytes(mut self, bytes: &[u8]) -> Result<C3d, C3dParseError> {
let data_start_byte = 512 * (self.bytes.data_start_block_index);
let data_start_byte = 512 * (self.bytes.data_start_block_index - 1);
if bytes.len() < data_start_byte {
return Err(C3dParseError::InsufficientBlocks("data".to_string()));
}
Expand Down
19 changes: 19 additions & 0 deletions tests/test_byte_and_file_parity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use c3dio::C3d;
use std::fs::File;
use std::io::{BufReader, Read};

#[test]
fn compare_byte_to_file_load() {
let sample19_file = C3d::load("tests/data/short.c3d").unwrap();

let file = File::open("tests/data/short.c3d").unwrap();
let mut buffer = BufReader::new(file);
let mut bytes = Vec::new();
buffer.read_to_end(&mut bytes).unwrap();
let sample19_bytes = C3d::from_bytes(&bytes).unwrap();

assert_eq!(sample19_file.parameters, sample19_bytes.parameters);
assert_eq!(sample19_file.events, sample19_bytes.events);
assert_eq!(sample19_file.data, sample19_bytes.data);
}

0 comments on commit 089e4bc

Please sign in to comment.