Skip to main content

rs_wikibzip2pages/
lib.rs

1use std::io;
2
3use io::Read;
4use io::Seek;
5use io::SeekFrom;
6
7use io::BufReader;
8
9use std::fs::File;
10
11use bzip2::bufread::BzDecoder;
12
13/// Gets page tags from the specified bzip2 data.
14///
15/// # Arguments
16/// - `bzseek`: The data to be read(concatenated bzip2 files).
17/// - `offset`: The number of bytes to seek(the address of the bzip2 data).
18/// - `size`: The size of the bzip2 data to be read.
19/// - `pages_buf`: The destination buffer.
20pub fn bzseek2pages<S>(
21    bzseek: &mut S,
22    offset: u64,
23    size: u64,
24    pages_buf: &mut String,
25) -> Result<(), io::Error>
26where
27    S: Seek + Read,
28{
29    bzseek.seek(SeekFrom::Start(offset))?;
30    let taken = bzseek.take(size);
31    let brf = BufReader::new(taken);
32    let br = BzDecoder::new(brf);
33    let mut brb = BufReader::new(br);
34    pages_buf.clear();
35    brb.read_to_string(pages_buf)?;
36    Ok(())
37}
38
39/// Gets page tags from the specified bzip2 "files".
40pub fn file2pages(
41    bzfile: &mut File,
42    offset: u64,
43    size: u64,
44    pages_buf: &mut String,
45) -> Result<(), io::Error> {
46    bzseek2pages(bzfile, offset, size, pages_buf)
47}
48
49/// Gets page tags from the specified file(concatenated bzip2 files).
50pub fn filepath2pages(
51    bzfilename: &str,
52    offset: u64,
53    size: u64,
54    pages_buf: &mut String,
55) -> Result<(), io::Error> {
56    let mut f = File::open(bzfilename)?;
57    file2pages(&mut f, offset, size, pages_buf)
58}
59
60/// Gets page tags using the computed size.
61pub fn filepath2pages_compute_size(
62    bzfilename: &str,
63    offset: u64,
64    offset_next: u64,
65    pages_buf: &mut String,
66) -> Result<(), io::Error> {
67    let size: u64 = offset_next.saturating_sub(offset);
68    filepath2pages(bzfilename, offset, size, pages_buf)
69}