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
use crate::unowned_ptr_to_str;
use bve_derive::c_interface;
use std::ffi::CString;
use std::os::raw::c_char;
use std::ptr::NonNull;

/// Reads the file at `filename` with [`bve::filesystem::read_convert_utf8`].
///
/// # Safety
///
/// - Pointer returned points to an **owned** string containing the contents of the file in utf8.
/// - Returned pointer must be deleted by [`crate::bve_delete_string`].
/// - If file loading fails, output is null.
#[must_use]
#[c_interface]
pub unsafe extern "C" fn bve_filesystem_read_convert_utf8(filename_ptr: *const c_char) -> Option<NonNull<c_char>> {
    let filename = unowned_ptr_to_str(&filename_ptr);

    let result = bve::filesystem::read_convert_utf8(filename.as_ref());

    result
        .ok()
        .and_then(|v| CString::new(v).ok())
        .map(CString::into_raw)
        .and_then(NonNull::new)
}