openbim_ifc_capi/
options.rs1use 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
16pub const OPENBIM_IFC_PARSE_SKIP_MALFORMED: u32 = 1;
19pub const OPENBIM_IFC_PARSE_CHECK_REFERENCES: u32 = 2;
22pub const OPENBIM_IFC_PARSE_ACCEPT_REAL_WITHOUT_POINT: u32 = 4;
25pub 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
32pub(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#[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 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 unsafe { finish_load(loaded, out_model, error_buffer, capacity) }
78 })
79}
80
81#[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 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 unsafe { finish_load(loaded, out_model, error_buffer, capacity) }
108 })
109}
110
111#[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 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 unsafe { IfcModel::open_mapped_with(Path::new(path), options) }
139 });
140 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}