Skip to content

Commit

Permalink
Do some refactoring. Update to recent rust.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Dmytrenko committed Jun 17, 2014
1 parent 5fa2d39 commit 478ebab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Rust gpx reader
===============
****

###Reads GuitarPro 6 compressed gpx file with BCFZ compression.
****

Expand Down
32 changes: 17 additions & 15 deletions gpx_reader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate core;
use std::vec::Vec;
use std::io::{Reader, MemReader};
// use std::io::fs::File;

pub mod bitbuffer {
pub struct BitBuffer {
Expand All @@ -15,7 +14,8 @@ pub mod bitbuffer {
BitBuffer{buffer: data, bit_position: 8, byte: 0}
}

// Reads MSB first
// Reads bit one by one
#[inline]
pub fn read_bit(&mut self) -> Option<u8> {
if self.bit_position == 8 {
let byte = self.buffer.read_byte();
Expand All @@ -25,16 +25,13 @@ pub mod bitbuffer {
}
self.bit_position = 0;
}

// let bit = (self.byte & (1<<(self.bit_position)) ) >> (self.bit_position); // littleEndian LSB
let bit = (self.byte >> (8 - self.bit_position - 1)) & 0x1; //MSB
self.bit_position += 1;
Some(bit)
}

// bigEndian MSB
pub fn read_bits(&mut self, count: uint) -> Option<uint> {
// println!("Reading bits, count: {}", count);
let mut word = 0u;
for idx in range(0, count) {
match self.read_bit() {
Expand All @@ -45,9 +42,7 @@ pub mod bitbuffer {
Some(word)
}

//littleEndian LSB
pub fn read_bits_reversed(&mut self, count: uint) -> Option<uint> {
// println!("Reading bits reversed, count: {}", count);
let mut word = 0u;
for idx in range(0, count) {
match self.read_bit() {
Expand All @@ -58,6 +53,7 @@ pub mod bitbuffer {
Some(word)
}


pub fn read_byte(&mut self) -> Option<u8> {
self.read_bits(8).map(|opt| opt as u8 )
}
Expand Down Expand Up @@ -105,7 +101,7 @@ pub fn decompress_bcfz(data: Vec<u8>) -> Vec<u8> {
let source_position = decomressed_data.len() - offset;
let to_read = core::cmp::min(len, offset);
let slice = decomressed_data.slice(source_position, source_position+to_read).to_owned();
decomressed_data.push_all(slice);
decomressed_data.push_all(slice.as_slice());
true
}

Expand All @@ -120,14 +116,15 @@ pub fn decompress_bcfz(data: Vec<u8>) -> Vec<u8> {
}
}
println!("Successfully decompressed data. Len: {}, Expected len: {}", decomressed_data.len(), decomressed_data_len);
return decomressed_data
decomressed_data
}


#[cfg(test)]
mod tests {
use std::io::MemReader;
use bitbuffer::BitBuffer;
#[allow(unreachable_code)]
#[test]
pub fn test_load_bcfz(){
return;
Expand Down Expand Up @@ -177,12 +174,17 @@ mod tests {
}

#[allow(unused_must_use)]
#[cfg(not(test))]
fn main(){
// let file_name = "/Users/andrii/Downloads/pink_floyd_hey_you.gpx";
// let data = Vec::from_slice( File::open(&Path::new(file_name)).read_to_end().unwrap().tailn(4) );
let mut stdout = std::io::stdio::stdout();
let mut stdin = std::io::stdio::stdin();
let data = Vec::from_slice(stdin.read_to_end().unwrap().tailn(4));
use std::io::fs::File;
let stream = if std::os::args().len() > 1{
File::open(&Path::new(std::os::args().get(1).as_slice())).read_to_end()
}else{
let mut stdin = std::io::stdio::stdin();
stdin.read_to_end()
};
let data = Vec::from_slice(stream.unwrap().tailn(4));
let content = decompress_bcfz(data);
let mut stdout = std::io::stdio::stdout();
stdout.write(content.as_slice());
}
}

0 comments on commit 478ebab

Please sign in to comment.