Skip to main content

openbim_ifc_capi/
options.rs

1//! Reads under explicit STEP parse options (#244).
2//!
3//! The options cross as flag bits rather than a struct, so a later option
4//! is a new bit, not a new struct layout. An unknown bit is refused with
5//! `InvalidValue`, the same code the other hosts give a malformed options
6//! object, and the message reaches the optional error buffer.
7
8use std::path::Path;
9
10use openbim_ifc_binding_core::{BindingError, IfcModel, OnMalformed, ParseOptions};
11
12use crate::buffer::{bytes, text};
13use crate::model::{finish_load, OpenbimIfcModel};
14use crate::status::{boundary, OpenbimIfcStatus};
15
16/// Skip a data record that cannot be parsed and report it as a diagnostic,
17/// instead of failing the read.
18pub const OPENBIM_IFC_PARSE_SKIP_MALFORMED: u32 = 1;
19/// Report duplicate instance ids and references to undefined ids as
20/// diagnostics. Nothing is dropped.
21pub const OPENBIM_IFC_PARSE_CHECK_REFERENCES: u32 = 2;
22/// Read a real written without its decimal point (`1E-05`) as that real,
23/// with a diagnostic.
24pub const OPENBIM_IFC_PARSE_ACCEPT_REAL_WITHOUT_POINT: u32 = 4;
25/// The lenient preset: `SKIP_MALFORMED | ACCEPT_REAL_WITHOUT_POINT`.
26pub const OPENBIM_IFC_PARSE_LENIENT: u32 = 5;
27
28const KNOWN: u32 = OPENBIM_IFC_PARSE_SKIP_MALFORMED
29    | OPENBIM_IFC_PARSE_CHECK_REFERENCES
30    | OPENBIM_IFC_PARSE_ACCEPT_REAL_WITHOUT_POINT;
31
32/// Options from flag bits; an unknown bit is `invalid-value`.
33pub(crate) fn options(flags: u32) -> Result<ParseOptions, BindingError> {
34    if flags & !KNOWN != 0 {
35        return Err(BindingError::InvalidValue(format!(
36            "unknown parse flag bits {:#x}",
37            flags & !KNOWN
38        )));
39    }
40    Ok(ParseOptions {
41        on_malformed: if flags & OPENBIM_IFC_PARSE_SKIP_MALFORMED != 0 {
42            OnMalformed::Skip
43        } else {
44            OnMalformed::Abort
45        },
46        check_references: flags & OPENBIM_IFC_PARSE_CHECK_REFERENCES != 0,
47        accept_real_without_point: flags & OPENBIM_IFC_PARSE_ACCEPT_REAL_WITHOUT_POINT != 0,
48    })
49}
50
51/// As `openbim_ifc_v0_1_model_parse`, under the `OPENBIM_IFC_PARSE_*`
52/// `flags` (0 is the strict read). What a lenient read recovers from is
53/// listed by `openbim_ifc_v0_1_model_diagnostic`.
54///
55/// # Safety
56/// As `openbim_ifc_v0_1_model_parse`.
57#[no_mangle]
58pub unsafe extern "C" fn openbim_ifc_v0_1_model_parse_with_options(
59    data: *const u8,
60    len: usize,
61    flags: u32,
62    out_model: *mut OpenbimIfcModel,
63    error_buffer: *mut u8,
64    capacity: usize,
65) -> OpenbimIfcStatus {
66    boundary(|| {
67        if out_model.is_null() || (capacity != 0 && error_buffer.is_null()) {
68            return OpenbimIfcStatus::NullPointer;
69        }
70        // SAFETY: caller contract above.
71        let input = match unsafe { bytes(data, len) } {
72            Ok(input) => input,
73            Err(status) => return status,
74        };
75        let loaded = options(flags).and_then(|options| IfcModel::parse_with(input, options));
76        // SAFETY: checked above; caller contract above.
77        unsafe { finish_load(loaded, out_model, error_buffer, capacity) }
78    })
79}
80
81/// As `openbim_ifc_v0_1_model_open`, under the `OPENBIM_IFC_PARSE_*`
82/// `flags`.
83///
84/// # Safety
85/// As `openbim_ifc_v0_1_model_open`.
86#[no_mangle]
87pub unsafe extern "C" fn openbim_ifc_v0_1_model_open_with_options(
88    path: *const u8,
89    path_len: usize,
90    flags: u32,
91    out_model: *mut OpenbimIfcModel,
92    error_buffer: *mut u8,
93    capacity: usize,
94) -> OpenbimIfcStatus {
95    boundary(|| {
96        if out_model.is_null() || (capacity != 0 && error_buffer.is_null()) {
97            return OpenbimIfcStatus::NullPointer;
98        }
99        // SAFETY: caller contract above.
100        let path = match unsafe { text(path, path_len) } {
101            Ok(path) => path,
102            Err(status) => return status,
103        };
104        let loaded =
105            options(flags).and_then(|options| IfcModel::open_with(Path::new(path), options));
106        // SAFETY: checked above; caller contract above.
107        unsafe { finish_load(loaded, out_model, error_buffer, capacity) }
108    })
109}
110
111/// As `openbim_ifc_v0_1_model_open_mapped`, under the
112/// `OPENBIM_IFC_PARSE_*` `flags`.
113///
114/// # Safety
115/// As `openbim_ifc_v0_1_model_open_mapped`: the file must not be modified
116/// or truncated until the model is destroyed.
117#[no_mangle]
118pub unsafe extern "C" fn openbim_ifc_v0_1_model_open_mapped_with_options(
119    path: *const u8,
120    path_len: usize,
121    flags: u32,
122    out_model: *mut OpenbimIfcModel,
123    error_buffer: *mut u8,
124    capacity: usize,
125) -> OpenbimIfcStatus {
126    boundary(|| {
127        if out_model.is_null() || (capacity != 0 && error_buffer.is_null()) {
128            return OpenbimIfcStatus::NullPointer;
129        }
130        // SAFETY: caller contract above.
131        let path = match unsafe { text(path, path_len) } {
132            Ok(path) => path,
133            Err(status) => return status,
134        };
135        let loaded = options(flags).and_then(|options| {
136            // SAFETY: the caller guarantees the file stays unchanged while
137            // the model lives.
138            unsafe { IfcModel::open_mapped_with(Path::new(path), options) }
139        });
140        // SAFETY: checked above; caller contract above.
141        unsafe { finish_load(loaded, out_model, error_buffer, capacity) }
142    })
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148
149    #[test]
150    fn flag_bits_map_to_the_options_and_unknown_bits_are_refused() {
151        assert_eq!(options(0), Ok(ParseOptions::strict()));
152        assert_eq!(
153            options(OPENBIM_IFC_PARSE_LENIENT),
154            Ok(ParseOptions::lenient())
155        );
156        assert_eq!(
157            OPENBIM_IFC_PARSE_LENIENT,
158            OPENBIM_IFC_PARSE_SKIP_MALFORMED | OPENBIM_IFC_PARSE_ACCEPT_REAL_WITHOUT_POINT
159        );
160        assert!(
161            options(OPENBIM_IFC_PARSE_CHECK_REFERENCES)
162                .unwrap()
163                .check_references
164        );
165        assert!(matches!(options(8), Err(BindingError::InvalidValue(_))));
166    }
167}