mdflib 0.2.1

Rust bindings for mdflib
Documentation
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Header wrapper for mdflib IHeader
//!
//! The header is the main entry point for accessing data in an [`crate::MdfFile`]. It
//! contains information about the measurement, such as the start time, author,
//! and project. It also provides access to the file's data groups, attachments,
//! and file history.

use mdflib_sys as ffi;
use std::ffi::{CStr, CString};
use std::ops::Deref;
use std::os::raw::c_char;

use crate::attachment::{Attachment, AttachmentRef};
use crate::datagroup::{DataGroup, DataGroupRef};
use crate::event::{Event, EventRef};
use crate::filehistory::{FileHistory, FileHistoryRef};
use crate::metadata::{MetaData, MetaDataRef};

/// Represents an immutable reference to the header of an MDF file.
#[derive(Debug, Clone, Copy)]
pub struct MdfHeaderRef {
    pub(crate) inner: *const ffi::IHeader,
}

impl std::fmt::Display for MdfHeaderRef {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "MdfHeaderRef {{ data_groups: {}, measurement_id: {}, recorder_id: {}, recorder_index: {}, start_angle: {:?}, start_distance: {:?}, author: {}, department: {}, project: {}, subject: {}, description: {}, start_time: {} }}",
            self.get_data_group_count(),
            self.get_measurement_id(),
            self.get_recorder_id(),
            self.get_recorder_index(),
            self.get_start_angle(),
            self.get_start_distance(),
            self.get_author(),
            self.get_department(),
            self.get_project(),
            self.get_subject(),
            self.get_description(),
            self.get_start_time()
        )
    }
}

impl MdfHeaderRef {
    pub(crate) fn new(inner: *const ffi::IHeader) -> Self {
        Self { inner }
    }

    /// Gets the measurement ID.
    pub fn get_measurement_id(&self) -> String {
        unsafe {
            let mut len = ffi::IHeaderGetMeasurementId(self.inner, std::ptr::null_mut(), 0);
            if len == 0 {
                return String::new();
            }
            len += 1; // For null terminator
            let mut buf = vec![0 as c_char; len as usize];
            ffi::IHeaderGetMeasurementId(self.inner, buf.as_mut_ptr(), len);
            CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned()
        }
    }

    /// Gets the recorder ID.
    pub fn get_recorder_id(&self) -> String {
        unsafe {
            let mut len = ffi::IHeaderGetRecorderId(self.inner, std::ptr::null_mut(), 0);
            if len == 0 {
                return String::new();
            }
            len += 1; // For null terminator
            let mut buf = vec![0 as c_char; len as usize];
            ffi::IHeaderGetRecorderId(self.inner, buf.as_mut_ptr(), len);
            CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned()
        }
    }

    /// Gets the recorder index.
    pub fn get_recorder_index(&self) -> i64 {
        unsafe { ffi::IHeaderGetRecorderIndex(self.inner) }
    }

    /// Gets the start angle.
    pub fn get_start_angle(&self) -> Option<f64> {
        unsafe {
            let mut angle = 0.0;
            if ffi::IHeaderGetStartAngle(self.inner, &mut angle) {
                Some(angle)
            } else {
                None
            }
        }
    }

    /// Gets the start distance.
    pub fn get_start_distance(&self) -> Option<f64> {
        unsafe {
            let mut distance = 0.0;
            if ffi::IHeaderGetStartDistance(self.inner, &mut distance) {
                Some(distance)
            } else {
                None
            }
        }
    }

    /// Gets the author.
    pub fn get_author(&self) -> String {
        unsafe {
            let mut len = ffi::IHeaderGetAuthor(self.inner, std::ptr::null_mut(), 0);
            if len == 0 {
                return String::new();
            }
            len += 1; // For null terminator
            let mut buf = vec![0 as c_char; len as usize];
            ffi::IHeaderGetAuthor(self.inner, buf.as_mut_ptr(), len);
            CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned()
        }
    }

    /// Gets the department.
    pub fn get_department(&self) -> String {
        unsafe {
            let mut len = ffi::IHeaderGetDepartment(self.inner, std::ptr::null_mut(), 0);
            if len == 0 {
                return String::new();
            }
            len += 1; // For null terminator
            let mut buf = vec![0 as c_char; len as usize];
            ffi::IHeaderGetDepartment(self.inner, buf.as_mut_ptr(), len);
            CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned()
        }
    }

    /// Gets the project.
    pub fn get_project(&self) -> String {
        unsafe {
            let mut len = ffi::IHeaderGetProject(self.inner, std::ptr::null_mut(), 0);
            if len == 0 {
                return String::new();
            }
            len += 1; // For null terminator
            let mut buf = vec![0 as c_char; len as usize];
            ffi::IHeaderGetProject(self.inner, buf.as_mut_ptr(), len);
            CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned()
        }
    }

    /// Gets the subject.
    pub fn get_subject(&self) -> String {
        unsafe {
            let mut len = ffi::IHeaderGetSubject(self.inner, std::ptr::null_mut(), 0);
            if len == 0 {
                return String::new();
            }
            len += 1; // For null terminator
            let mut buf = vec![0 as c_char; len as usize];
            ffi::IHeaderGetSubject(self.inner, buf.as_mut_ptr(), len);
            CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned()
        }
    }

    /// Gets the description.
    pub fn get_description(&self) -> String {
        unsafe {
            let mut len = ffi::IHeaderGetDescription(self.inner, std::ptr::null_mut(), 0);
            if len == 0 {
                return String::new();
            }
            len += 1; // For null terminator
            let mut buf = vec![0 as c_char; len as usize];
            ffi::IHeaderGetDescription(self.inner, buf.as_mut_ptr(), len);
            CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned()
        }
    }

    /// Gets the start time.
    pub fn get_start_time(&self) -> u64 {
        unsafe { ffi::IHeaderGetStartTime(self.inner) }
    }

    /// Gets the metadata of the header.
    pub fn get_metadata(&self) -> Option<MetaDataRef<'_>> {
        unsafe {
            let metadata = ffi::IHeaderGetMetaData(self.inner);
            if metadata.is_null() {
                None
            } else {
                Some(MetaDataRef::new(metadata))
            }
        }
    }

    /// Gets the attachments of the header.
    pub fn get_attachments(&self) -> Vec<AttachmentRef<'_>> {
        const MAX_ATTACHMENTS: usize = 1000;
        let mut attachments: Vec<*const ffi::IAttachment> = vec![std::ptr::null(); MAX_ATTACHMENTS];
        let count = unsafe {
            ffi::IHeaderGetAttachments(self.inner, attachments.as_mut_ptr(), MAX_ATTACHMENTS)
        };

        attachments.truncate(count);
        attachments
            .into_iter()
            .filter(|&ptr| !ptr.is_null())
            .map(AttachmentRef::new)
            .collect()
    }

    /// Gets the file histories of the header.
    pub fn get_file_histories(&self) -> Vec<FileHistoryRef<'_>> {
        const MAX_HISTORIES: usize = 1000;
        let mut histories: Vec<*const ffi::IFileHistory> = vec![std::ptr::null(); MAX_HISTORIES];
        let count = unsafe {
            ffi::IHeaderGetFileHistories(self.inner, histories.as_mut_ptr(), MAX_HISTORIES)
        };

        histories.truncate(count);
        histories
            .into_iter()
            .filter(|&ptr| !ptr.is_null())
            .map(FileHistoryRef::new)
            .collect()
    }

    /// Gets the events of the header.
    pub fn get_events(&self) -> Vec<EventRef<'_>> {
        const MAX_EVENTS: usize = 1000;
        let mut events: Vec<*const ffi::IEvent> = vec![std::ptr::null(); MAX_EVENTS];
        let count = unsafe { ffi::IHeaderGetEvents(self.inner, events.as_mut_ptr(), MAX_EVENTS) };

        events.truncate(count);
        events
            .into_iter()
            .filter(|&ptr| !ptr.is_null())
            .map(EventRef::new)
            .collect()
    }

    /// Gets the data group count.
    pub fn get_data_group_count(&self) -> usize {
        unsafe { ffi::IHeaderGetDataGroupCount(self.inner) as usize }
    }
}

/// Represents a mutable reference to the header of an MDF file.
#[derive(Debug)]
pub struct MdfHeader {
    pub(crate) inner: *mut ffi::IHeader,
    inner_ref: MdfHeaderRef,
}

impl MdfHeader {
    pub(crate) fn new(inner: *mut ffi::IHeader) -> Self {
        Self {
            inner,
            inner_ref: MdfHeaderRef::new(inner),
        }
    }

    /// Sets the measurement ID.
    pub fn set_measurement_id(&mut self, id: &str) {
        let c_id = CString::new(id).unwrap();
        unsafe {
            ffi::IHeaderSetMeasurementId(self.inner, c_id.as_ptr());
        }
    }

    /// Sets the recorder ID.
    pub fn set_recorder_id(&mut self, id: &str) {
        let c_id = CString::new(id).unwrap();
        unsafe {
            ffi::IHeaderSetRecorderId(self.inner, c_id.as_ptr());
        }
    }

    /// Sets the recorder index.
    pub fn set_recorder_index(&mut self, index: i64) {
        unsafe {
            ffi::IHeaderSetRecorderIndex(self.inner, index);
        }
    }

    /// Sets the start angle.
    pub fn set_start_angle(&mut self, angle: f64) {
        unsafe {
            ffi::IHeaderSetStartAngle(self.inner, angle);
        }
    }

    /// Sets the start distance.
    pub fn set_start_distance(&mut self, distance: f64) {
        unsafe {
            ffi::IHeaderSetStartDistance(self.inner, distance);
        }
    }

    /// Sets the author.
    pub fn set_author(&mut self, author: &str) {
        let c_author = CString::new(author).unwrap();
        unsafe {
            ffi::IHeaderSetAuthor(self.inner, c_author.as_ptr());
        }
    }

    /// Sets the department.
    pub fn set_department(&mut self, department: &str) {
        let c_department = CString::new(department).unwrap();
        unsafe {
            ffi::IHeaderSetDepartment(self.inner, c_department.as_ptr());
        }
    }

    /// Sets the project.
    pub fn set_project(&mut self, project: &str) {
        let c_project = CString::new(project).unwrap();
        unsafe {
            ffi::IHeaderSetProject(self.inner, c_project.as_ptr());
        }
    }

    /// Sets the subject.
    pub fn set_subject(&mut self, subject: &str) {
        let c_subject = CString::new(subject).unwrap();
        unsafe {
            ffi::IHeaderSetSubject(self.inner, c_subject.as_ptr());
        }
    }

    /// Sets the description.
    pub fn set_description(&mut self, description: &str) {
        let c_description = CString::new(description).unwrap();
        unsafe {
            ffi::IHeaderSetDescription(self.inner, c_description.as_ptr());
        }
    }

    /// Sets the start time.
    pub fn set_start_time(&mut self, start_time: u64) {
        unsafe {
            ffi::IHeaderSetStartTime(self.inner, start_time);
        }
    }

    /// Creates metadata for the header.
    pub fn create_metadata(&mut self) -> Option<MetaData<'_>> {
        unsafe {
            let metadata = ffi::IHeaderCreateMetaData(self.inner);
            if metadata.is_null() {
                None
            } else {
                Some(MetaData::new(metadata))
            }
        }
    }

    /// Creates an attachment for the header.
    pub fn create_attachment(&mut self) -> Option<Attachment<'_>> {
        unsafe {
            let attachment = ffi::IHeaderCreateAttachment(self.inner);
            if attachment.is_null() {
                None
            } else {
                Some(Attachment::new(attachment))
            }
        }
    }

    /// Creates a file history for the header.
    pub fn create_file_history(&mut self) -> Option<FileHistory<'_>> {
        unsafe {
            let file_history = ffi::IHeaderCreateFileHistory(self.inner);
            if file_history.is_null() {
                None
            } else {
                Some(FileHistory::new(file_history))
            }
        }
    }

    /// Creates an event for the header.
    pub fn create_event(&mut self) -> Option<Event<'_>> {
        unsafe {
            let event = ffi::IHeaderCreateEvent(self.inner);
            if event.is_null() {
                None
            } else {
                Some(Event::new(event))
            }
        }
    }

    /// Gets all data groups from the header.
    pub fn get_data_groups(&self) -> Vec<DataGroupRef> {
        const MAX_DATA_GROUPS: usize = 1000;
        let mut data_groups: Vec<*const ffi::IDataGroup> = vec![std::ptr::null(); MAX_DATA_GROUPS];
        let count = unsafe {
            ffi::IHeaderGetDataGroups(self.inner, data_groups.as_mut_ptr(), MAX_DATA_GROUPS)
        };

        data_groups.truncate(count);
        data_groups
            .into_iter()
            .filter(|&ptr| !ptr.is_null())
            .map(DataGroupRef::new)
            .collect()
    }

    /// Gets the last data group from the header.
    pub fn get_last_data_group(&self) -> Option<DataGroup> {
        unsafe {
            let data_group = ffi::IHeaderLastDataGroup(self.inner);
            if data_group.is_null() {
                None
            } else {
                Some(DataGroup::new(data_group))
            }
        }
    }
}

impl Deref for MdfHeader {
    type Target = MdfHeaderRef;

    fn deref(&self) -> &Self::Target {
        &self.inner_ref
    }
}