openbim_ifc_capi/header.rs
1//! The STEP file header as a value tape (#244).
2//!
3//! The header crosses as one `LIST` of ten values, in the order a STEP
4//! header writes them: description (`LIST` of `TEXT`), implementation
5//! level, name, time stamp (`TEXT`), author, organization (`LIST` of
6//! `TEXT`), preprocessor version, originating system, authorization
7//! (`TEXT`), schema (`LIST` of `TEXT`). Reading and replacing use the same
8//! shape, so a host edits a field and writes the tape back.
9
10use openbim_ifc_binding_core::header;
11
12use crate::buffer::{bytes, slice};
13use crate::model::{done, fill_tape, with_model, OpenbimIfcModel};
14use crate::status::{boundary, OpenbimIfcStatus};
15use crate::tape::{OpenbimIfcValueNode, Reader, Tape};
16
17/// The file header as a value tape: one `LIST` of ten values, in STEP
18/// header order -- description (`LIST` of `TEXT`), implementation level,
19/// name, time stamp, author (`LIST`), organization (`LIST`), preprocessor
20/// version, originating system, authorization, schema (`LIST`).
21///
22/// # Safety
23/// As for `openbim_ifc_v0_1_entity_attribute`: each buffer null with
24/// capacity 0, or valid for its capacity; both `out_*_required` valid for
25/// one write.
26#[no_mangle]
27pub unsafe extern "C" fn openbim_ifc_v0_1_model_header(
28 model: OpenbimIfcModel,
29 nodes: *mut OpenbimIfcValueNode,
30 node_capacity: usize,
31 out_nodes_required: *mut usize,
32 strings: *mut u8,
33 string_capacity: usize,
34 out_strings_required: *mut usize,
35) -> OpenbimIfcStatus {
36 boundary(|| {
37 with_model(model, |m| {
38 let tape = Tape::encode(&header::to_tagged(&m.header()));
39 // SAFETY: caller contract above.
40 done(unsafe {
41 fill_tape(
42 &tape,
43 nodes,
44 node_capacity,
45 out_nodes_required,
46 strings,
47 string_capacity,
48 out_strings_required,
49 )
50 })
51 })
52 })
53}
54
55/// Replace the file header with the one on the tape. A tape of any other
56/// shape is `InvalidValue` and leaves the header unchanged.
57///
58/// # Safety
59/// As for `openbim_ifc_v0_1_entity_set_attribute`.
60#[no_mangle]
61pub unsafe extern "C" fn openbim_ifc_v0_1_model_set_header(
62 model: OpenbimIfcModel,
63 nodes: *const OpenbimIfcValueNode,
64 node_count: usize,
65 strings: *const u8,
66 string_len: usize,
67) -> OpenbimIfcStatus {
68 boundary(|| {
69 // SAFETY: caller contract above.
70 let (nodes, strings) =
71 match unsafe { (slice(nodes, node_count), bytes(strings, string_len)) } {
72 (Ok(n), Ok(s)) => (n, s),
73 (Err(status), _) | (_, Err(status)) => return status,
74 };
75 with_model(model, |m| {
76 let value = Reader::new(nodes, strings).single()?;
77 m.set_header(header::from_tagged(value)?);
78 Ok(OpenbimIfcStatus::Ok)
79 })
80 })
81}