Skip to main content

openbim_ifc_capi/
xml.rs

1//! The ifcXML codec (#244).
2//!
3//! A null `profile` with length 0 selects the library's own lossless
4//! layout; `IFC4` or `IFC4X3_ADD2` selects the buildingSMART XSD
5//! configuration of that release.
6
7use openbim_ifc_binding_core::IfcModel;
8
9use crate::buffer::{bytes, fill, text};
10use crate::model::{done, finish_load, with_model, OpenbimIfcModel};
11use crate::status::{boundary, OpenbimIfcStatus};
12
13/// The profile argument: `None` for the native layout.
14///
15/// # Safety
16/// As `buffer::text`.
17pub(crate) unsafe fn profile<'a>(
18    profile: *const u8,
19    profile_len: usize,
20) -> Result<Option<&'a str>, OpenbimIfcStatus> {
21    if profile.is_null() && profile_len == 0 {
22        return Ok(None);
23    }
24    // SAFETY: forwarded caller contract.
25    unsafe { text(profile, profile_len) }.map(Some)
26}
27
28/// Parse `len` bytes of ifcXML and write the new model's handle.
29///
30/// `profile` (UTF-8, `profile_len` bytes) is null with length 0 for the
31/// native layout, else `IFC4` or `IFC4X3_ADD2` (any case) for that
32/// release's XSD configuration. `UnsupportedProfile` for any other name,
33/// `UnsupportedSchema` when the release is not bundled, `Parse` when the
34/// document does not read, `FeatureDisabled` without the `ifcxml` feature.
35/// The message goes to the optional `error_buffer` as for
36/// `openbim_ifc_v0_1_model_parse`.
37///
38/// # Safety
39/// As `openbim_ifc_v0_1_model_parse`; `profile`, if non-null, valid for
40/// `profile_len` reads.
41#[no_mangle]
42pub unsafe extern "C" fn openbim_ifc_v0_1_model_parse_ifcxml(
43    data: *const u8,
44    len: usize,
45    profile: *const u8,
46    profile_len: usize,
47    out_model: *mut OpenbimIfcModel,
48    error_buffer: *mut u8,
49    capacity: usize,
50) -> OpenbimIfcStatus {
51    boundary(|| {
52        if out_model.is_null() || (capacity != 0 && error_buffer.is_null()) {
53            return OpenbimIfcStatus::NullPointer;
54        }
55        // SAFETY: caller contract above.
56        let inputs = unsafe { (bytes(data, len), self::profile(profile, profile_len)) };
57        let (input, profile) = match inputs {
58            (Ok(input), Ok(profile)) => (input, profile),
59            (Err(status), _) | (_, Err(status)) => return status,
60        };
61        let loaded = IfcModel::parse_ifcxml(input, profile);
62        // SAFETY: checked above; caller contract above.
63        unsafe { finish_load(loaded, out_model, error_buffer, capacity) }
64    })
65}
66
67/// Serialize as ifcXML into a caller buffer; `out_required` gets the size.
68/// `profile` as for `openbim_ifc_v0_1_model_parse_ifcxml`. An XSD-layout
69/// write needs the header to declare the profile's schema, and refuses
70/// with `Write` what the configuration cannot carry.
71///
72/// # Safety
73/// As `openbim_ifc_v0_1_model_write`; `profile`, if non-null, valid for
74/// `profile_len` reads.
75#[no_mangle]
76pub unsafe extern "C" fn openbim_ifc_v0_1_model_write_ifcxml(
77    model: OpenbimIfcModel,
78    profile: *const u8,
79    profile_len: usize,
80    buffer: *mut u8,
81    capacity: usize,
82    out_required: *mut usize,
83) -> OpenbimIfcStatus {
84    boundary(|| {
85        // SAFETY: caller contract above.
86        let profile = match unsafe { self::profile(profile, profile_len) } {
87            Ok(profile) => profile,
88            Err(status) => return status,
89        };
90        with_model(model, |m| {
91            let bytes = m.write_ifcxml(profile)?;
92            // SAFETY: caller contract above.
93            done(unsafe { fill(&bytes, buffer, capacity, out_required) })
94        })
95    })
96}