-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.rs
144 lines (131 loc) 路 4.65 KB
/
utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use anyhow::{Error, Ok, Result};
use log::*;
use std::{
fs::{File, OpenOptions},
io::{self, prelude::*, BufRead, BufReader, BufWriter, Write},
path::Path,
};
const GZ_MAGIC: [u8; 3] = [0x1f, 0x8b, 0x08];
const BZ_MAGIC: [u8; 3] = [0x42, 0x5a, 0x68];
const XZ_MAGIC: [u8; 6] = [0xfd, 0x37, 0x7a, 0x58, 0x5A, 0x00];
const MAGIC_MAX_LEN: usize = 6;
const BUFF_SIZE: usize = 1024 * 1024;
fn magic_num<P: AsRef<Path> + Copy>(file_name: P) -> Result<[u8; MAGIC_MAX_LEN], Error> {
let mut buffer: [u8; MAGIC_MAX_LEN] = [0; MAGIC_MAX_LEN];
let mut fp = File::open(file_name)?;
let _ = fp.read(&mut buffer)?;
Ok(buffer)
}
fn is_gzipped<P: AsRef<Path> + Copy>(file_name: P) -> Result<bool> {
let buffer = magic_num(file_name)?;
let gz_or_not =
buffer[0] == GZ_MAGIC[0] && buffer[1] == GZ_MAGIC[1] && buffer[2] == GZ_MAGIC[2];
Ok(gz_or_not || file_name.as_ref().ends_with(".gz"))
}
fn is_bzipped<P: AsRef<Path> + Copy>(file_name: P) -> Result<bool> {
let buffer = magic_num(file_name)?;
let bz_or_not =
buffer[0] == BZ_MAGIC[0] && buffer[1] == BZ_MAGIC[1] && buffer[2] == BZ_MAGIC[2];
Ok(bz_or_not || file_name.as_ref().ends_with(".bz2"))
}
fn is_xz<P: AsRef<Path> + Copy>(file_name: P) -> Result<bool> {
let buffer = magic_num(file_name)?;
let xz_or_not = buffer[0] == XZ_MAGIC[0]
&& buffer[1] == XZ_MAGIC[1]
&& buffer[2] == XZ_MAGIC[2]
&& buffer[3] == XZ_MAGIC[3]
&& buffer[4] == XZ_MAGIC[4]
&& buffer[5] == XZ_MAGIC[5];
Ok(xz_or_not || file_name.as_ref().ends_with(".xz"))
}
pub fn file_reader<P>(file_in: Option<P>) -> Result<Box<dyn BufRead>>
where
P: AsRef<Path> + Copy,
{
if let Some(file_name) = file_in {
let gz_flag = is_gzipped(file_name)?;
let bz_flag = is_bzipped(file_name)?;
let zx_flag = is_xz(file_name)?;
let fp = File::open(file_name)?;
if gz_flag {
Ok(Box::new(BufReader::with_capacity(
BUFF_SIZE,
flate2::read::MultiGzDecoder::new(fp),
)))
} else if bz_flag {
Ok(Box::new(BufReader::with_capacity(
BUFF_SIZE,
bzip2::read::MultiBzDecoder::new(fp),
)))
} else if zx_flag {
Ok(Box::new(BufReader::with_capacity(
BUFF_SIZE,
xz2::read::XzDecoder::new(fp),
)))
} else {
Ok(Box::new(BufReader::with_capacity(BUFF_SIZE, fp)))
}
} else {
if atty::is(atty::Stream::Stdin) {
error!("stdin not detected");
std::process::exit(1);
}
let fp = BufReader::new(io::stdin());
Ok(Box::new(fp))
}
}
pub fn file_writer<P>(file_out: Option<P>, compression_level: u32) -> Result<Box<dyn Write>>
where
P: AsRef<Path> + Copy,
{
if let Some(file_name) = file_out {
let fp = File::create(file_name)?;
if file_name.as_ref().ends_with(".gz") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
flate2::write::GzEncoder::new(fp, flate2::Compression::new(compression_level)),
)))
} else if file_name.as_ref().ends_with(".bz2") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
bzip2::write::BzEncoder::new(fp, bzip2::Compression::new(compression_level)),
)))
} else if file_name.as_ref().ends_with(".xz") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
xz2::write::XzEncoder::new(fp, compression_level),
)))
} else {
Ok(Box::new(BufWriter::with_capacity(BUFF_SIZE, fp)))
}
} else {
Ok(Box::new(BufWriter::new(io::stdout())))
}
}
pub fn file_writer_append<P>(file_out: P, compression_level: u32) -> Result<Box<dyn Write>>
where
P: AsRef<Path> + Copy,
{
let fp = OpenOptions::new()
.append(true)
.create(true)
.open(file_out)?;
if file_out.as_ref().ends_with(".gz") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
flate2::write::GzEncoder::new(fp, flate2::Compression::new(compression_level)),
)))
} else if file_out.as_ref().ends_with(".bz2") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
bzip2::write::BzEncoder::new(fp, bzip2::Compression::new(compression_level)),
)))
} else if file_out.as_ref().ends_with("xz") {
Ok(Box::new(BufWriter::with_capacity(
BUFF_SIZE,
xz2::write::XzEncoder::new(fp, compression_level),
)))
} else {
Ok(Box::new(BufWriter::with_capacity(BUFF_SIZE, fp)))
}
}