Struct ChmFile

Source
pub struct ChmFile { /* private fields */ }

Implementations§

Source§

impl ChmFile

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<ChmFile, OpenError>

Open a ChmFile from the file system.

Examples found in repository?
examples/extract.rs (line 17)
10fn main() {
11    let args: Vec<_> = env::args().skip(1).collect();
12    if args.len() != 2 || args.iter().any(|arg| arg.contains("-h")) {
13        println!("Usage: extract <chm-file> <out-dir>");
14        return;
15    }
16
17    let mut file = ChmFile::open(&args[0]).expect("Unable to open the file");
18
19    let out_dir = PathBuf::from(&args[1]);
20
21    file.for_each(Filter::all(), |file, item| extract(&out_dir, file, &item))
22        .unwrap();
23}
More examples
Hide additional examples
examples/enumerate-items.rs (line 10)
5fn main() {
6    let filename = env::args()
7        .nth(1)
8        .unwrap_or_else(|| panic!("Usage: enumerate-items <filename>"));
9
10    let mut file = ChmFile::open(&filename).expect("Unable to open the file");
11
12    println!("{}:", filename);
13    println!(" spc    start   length   type\t\t\tname");
14    println!(" ===    =====   ======   ====\t\t\t====");
15
16    file.for_each(Filter::all(), |_file, item| {
17        describe_item(item);
18        Continuation::Continue
19    })
20    .unwrap();
21}
Source

pub fn find<P: AsRef<Path>>(&mut self, path: P) -> Option<UnitInfo>

Find a particular object in the archive.

Source

pub fn for_each<F, C>( &mut self, filter: Filter, cb: F, ) -> Result<(), EnumerationError>
where F: FnMut(&mut ChmFile, UnitInfo) -> C, C: Into<Continuation>,

Inspect each item within the ChmFile.

Examples found in repository?
examples/extract.rs (line 21)
10fn main() {
11    let args: Vec<_> = env::args().skip(1).collect();
12    if args.len() != 2 || args.iter().any(|arg| arg.contains("-h")) {
13        println!("Usage: extract <chm-file> <out-dir>");
14        return;
15    }
16
17    let mut file = ChmFile::open(&args[0]).expect("Unable to open the file");
18
19    let out_dir = PathBuf::from(&args[1]);
20
21    file.for_each(Filter::all(), |file, item| extract(&out_dir, file, &item))
22        .unwrap();
23}
More examples
Hide additional examples
examples/enumerate-items.rs (lines 16-19)
5fn main() {
6    let filename = env::args()
7        .nth(1)
8        .unwrap_or_else(|| panic!("Usage: enumerate-items <filename>"));
9
10    let mut file = ChmFile::open(&filename).expect("Unable to open the file");
11
12    println!("{}:", filename);
13    println!(" spc    start   length   type\t\t\tname");
14    println!(" ===    =====   ======   ====\t\t\t====");
15
16    file.for_each(Filter::all(), |_file, item| {
17        describe_item(item);
18        Continuation::Continue
19    })
20    .unwrap();
21}
Source

pub fn for_each_item_in_dir<F, C, P>( &mut self, filter: Filter, prefix: P, cb: F, ) -> Result<(), EnumerationError>
where P: AsRef<Path>, F: FnMut(&mut ChmFile, UnitInfo) -> C, C: Into<Continuation>,

Inspect each item within the ChmFile inside a specified directory.

Source

pub fn read( &mut self, unit: &UnitInfo, offset: u64, buffer: &mut [u8], ) -> Result<usize, ReadError>

Examples found in repository?
examples/extract.rs (line 58)
25fn extract(
26    root_dir: &Path,
27    file: &mut ChmFile,
28    item: &UnitInfo,
29) -> Result<(), Box<dyn Error>> {
30    if !item.is_file() || !item.is_normal() {
31        // we only care about normal files
32        return Ok(());
33    }
34    let path = match item.path() {
35        Some(p) => p,
36        // if we can't get the path, ignore it and continue
37        None => return Ok(()),
38    };
39
40    let mut dest = root_dir.to_path_buf();
41    // Note: by design, the path for a normal file is absolute (starts with "/")
42    // so when joining it with the root_dir we need to drop the initial "/".
43    dest.extend(path.components().skip(1));
44
45    // make sure the parent directory exists
46    if let Some(parent) = dest.parent() {
47        fs::create_dir_all(parent)?;
48    }
49
50    let mut f = File::create(dest)?;
51    let mut start_offset = 0;
52    // CHMLib doesn't give us a &[u8] with the file contents directly (e.g.
53    // because it may be compressed) so we need to copy chunks to an
54    // intermediate buffer
55    let mut buffer = vec![0; 1 << 16];
56
57    loop {
58        let bytes_read = file.read(item, start_offset, &mut buffer)?;
59        if bytes_read == 0 {
60            // we've reached the end of the file
61            break;
62        } else {
63            // write this chunk to the file and continue
64            start_offset += bytes_read as u64;
65            f.write_all(&buffer)?;
66        }
67    }
68
69    Ok(())
70}

Trait Implementations§

Source§

impl Debug for ChmFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for ChmFile

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.