Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper cleanup #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/file/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ pub const EMPTY: [u8; 1] = [0; 1];
pub const OP_LEN: u8 = 4;

pub fn write_empty_byte(file: &mut File) {

let res = file.write(&EMPTY);

if let Err(e) = res {
panic!("Failed to write empty byte: {}", e);
}

if res.unwrap() != EMPTY.len() {
panic!("Failed to write empty byte: {}", "Write length mismatch.");
}
Expand All @@ -34,6 +37,7 @@ pub fn cut(file: &mut File) {
}

pub fn truncate(file: &mut File) {

let res = file.set_len(0);

if let Err(e) = res {
Expand All @@ -45,6 +49,7 @@ pub fn truncate(file: &mut File) {
}

pub fn jump_stream_start(file: &mut File) {

let res = file.seek(SeekFrom::Start(0));

if let Err(e) = res {
Expand All @@ -53,6 +58,7 @@ pub fn jump_stream_start(file: &mut File) {
}

pub fn jump_stream_end(file: &mut File) {

let res = file.seek(SeekFrom::End(-1));

if let Err(e) = res {
Expand All @@ -61,6 +67,7 @@ pub fn jump_stream_end(file: &mut File) {
}

pub fn catch_stream_read(file: &mut File, read: usize, predict: usize, fix_fn: fn(file: &mut File)) -> Result<(), Error> {

if read != predict {
fix_fn(file);
return Err(
Expand All @@ -70,6 +77,7 @@ pub fn catch_stream_read(file: &mut File, read: usize, predict: usize, fix_fn: f
)
);
}

Ok(())
}

Expand Down Expand Up @@ -170,6 +178,7 @@ pub fn read(file: &mut File) -> Result<Vec<u8>, Error> {
}

pub fn seek_forward(file: &mut File) -> Result<(), Error> {

let mut len_buf: [u8; OP_LEN as usize] = [0; OP_LEN as usize];

let res = file.read(&mut len_buf);
Expand Down Expand Up @@ -203,6 +212,7 @@ pub fn seek_forward(file: &mut File) -> Result<(), Error> {
}

pub fn seek_backward(file: &mut File) -> Result<(), Error> {

let mut len_buf: [u8; OP_LEN as usize] = [0; OP_LEN as usize];

let res = file.seek(SeekFrom::Current(-(OP_LEN as i64)));
Expand Down Expand Up @@ -251,20 +261,23 @@ pub fn seek_backward(file: &mut File) -> Result<(), Error> {
}

pub fn seek_forward_n(file: &mut File, n: u64) -> Result<(), Error> {

for _ in 0..n {
seek_forward(file)?;
}
Ok(())
}

pub fn seek_backward_n(file: &mut File, n: u64) -> Result<(), Error> {

for _ in 0..n {
seek_backward(file)?;
}
Ok(())
}

pub fn inner_len(file: &mut File) -> Result<u32, Error> {

let mut len_buf: [u8; OP_LEN as usize] = [0; OP_LEN as usize];
let res = file.read(&mut len_buf);

Expand All @@ -291,6 +304,7 @@ pub fn inner_len(file: &mut File) -> Result<u32, Error> {
}

pub fn len_calc(inner_lens: Vec<u32>, op_sets: u8) -> i64 {

let mut len = 0;
for inner_len in inner_lens {
len += inner_len as u64 + 2*OP_LEN as u64;
Expand Down
Loading