summaryrefslogtreecommitdiff
path: root/plugins/warp/src/plugin/ffi/file.rs
blob: 951b5eb28da21a927d150b430de8978a380c49b1 (plain)
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
use std::ffi::c_char;
use std::sync::Arc;
use warp::WarpFile;

pub type BNWARPFile = WarpFile<'static>;

// TODO: At some point we may want to expose chunks directly. For now we will just enumerate all of them.
// pub type BNWARPChunk = warp::chunk::Chunk<'static>;

// TODO: From bytes as well.
#[no_mangle]
pub unsafe extern "C" fn BNWARPNewFileFromPath(path: *mut c_char) -> *mut BNWARPFile {
    let path_cstr = unsafe { std::ffi::CStr::from_ptr(path) };
    let Ok(path) = path_cstr.to_str() else {
        return std::ptr::null_mut();
    };
    let Ok(bytes) = std::fs::read(path) else {
        return std::ptr::null_mut();
    };
    let Some(file) = WarpFile::from_owned_bytes(bytes) else {
        return std::ptr::null_mut();
    };
    Arc::into_raw(Arc::new(file)) as *mut BNWARPFile
}

#[no_mangle]
pub unsafe extern "C" fn BNWARPNewFileReference(file: *mut BNWARPFile) -> *mut BNWARPFile {
    Arc::increment_strong_count(file);
    file
}

#[no_mangle]
pub unsafe extern "C" fn BNWARPFreeFileReference(file: *mut BNWARPFile) {
    if file.is_null() {
        return;
    }
    Arc::decrement_strong_count(file);
}