|
| 1 | +use mbinary::decode::RecordDecoder; |
| 2 | +use std::ffi::CStr; |
| 3 | +use std::fs::File; |
| 4 | +use std::io::{BufReader, Cursor, Read}; |
| 5 | +use std::ptr; |
| 6 | +use std::slice; |
| 7 | + |
| 8 | +use crate::records::CRecordEnum; |
| 9 | + |
| 10 | +/// C-compatible wrapper around RecordDecoder |
| 11 | +pub struct CRecordDecoder { |
| 12 | + decoder: RecordDecoder<Box<dyn Read>>, |
| 13 | +} |
| 14 | + |
| 15 | +/// Create a new `CRecordDecoder` with an in-memory buffer as the source. |
| 16 | +#[no_mangle] |
| 17 | +pub extern "C" fn create_buffer_decoder( |
| 18 | + source: *const u8, |
| 19 | + source_size: usize, |
| 20 | +) -> *mut CRecordDecoder { |
| 21 | + if source.is_null() || source_size == 0 { |
| 22 | + return ptr::null_mut(); |
| 23 | + } |
| 24 | + |
| 25 | + // Convert the raw pointer and size to a Vec<u8> |
| 26 | + let source_slice = unsafe { slice::from_raw_parts(source, source_size) }; |
| 27 | + let source_buffer = Cursor::new(source_slice.to_vec()); |
| 28 | + |
| 29 | + let decoder = CRecordDecoder { |
| 30 | + decoder: RecordDecoder::new(Box::new(source_buffer)), |
| 31 | + }; |
| 32 | + |
| 33 | + Box::into_raw(Box::new(decoder)) |
| 34 | +} |
| 35 | + |
| 36 | +/// Create a new `CRecordDecoder` with a file as the source. |
| 37 | +#[no_mangle] |
| 38 | +pub extern "C" fn create_file_decoder(file_path: *const libc::c_char) -> *mut CRecordDecoder { |
| 39 | + if file_path.is_null() { |
| 40 | + return ptr::null_mut(); |
| 41 | + } |
| 42 | + |
| 43 | + // Convert C string to Rust Path |
| 44 | + let c_str = unsafe { CStr::from_ptr(file_path) }; |
| 45 | + let path = match c_str.to_str() { |
| 46 | + Ok(path) => path, |
| 47 | + Err(_) => return ptr::null_mut(), |
| 48 | + }; |
| 49 | + |
| 50 | + let decoder = match create_decoder_from_file(path) { |
| 51 | + Ok(f) => f, |
| 52 | + Err(_) => return ptr::null_mut(), |
| 53 | + }; |
| 54 | + |
| 55 | + let c_decoder = CRecordDecoder { decoder }; |
| 56 | + |
| 57 | + Box::into_raw(Box::new(c_decoder)) |
| 58 | +} |
| 59 | + |
| 60 | +// Helper function, not exposed to C directly. |
| 61 | +fn create_decoder_from_file( |
| 62 | + file_path: &str, |
| 63 | +) -> Result<RecordDecoder<Box<dyn Read>>, std::io::Error> { |
| 64 | + let file = File::open(file_path)?; |
| 65 | + let buf_reader = BufReader::new(file); |
| 66 | + let boxed_reader: Box<dyn Read> = Box::new(buf_reader); |
| 67 | + Ok(RecordDecoder::new(boxed_reader)) |
| 68 | +} |
| 69 | + |
| 70 | +/// Iteratively decodes records, returning false when all records are decoded. |
| 71 | +#[no_mangle] |
| 72 | +pub extern "C" fn decoder_iter(decoder: *mut CRecordDecoder, output: *mut CRecordEnum) -> bool { |
| 73 | + if decoder.is_null() || output.is_null() { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + let decoder = unsafe { &mut *decoder }; |
| 78 | + let mut iterator = decoder.decoder.decode_iterator(); |
| 79 | + |
| 80 | + match iterator.next() { |
| 81 | + Some(Ok(record_enum)) => { |
| 82 | + let c_record: CRecordEnum = record_enum.into(); |
| 83 | + unsafe { ptr::write(output, c_record) }; |
| 84 | + true |
| 85 | + } |
| 86 | + _ => false, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Destroy the `CRecordDecoder` |
| 91 | +#[no_mangle] |
| 92 | +pub extern "C" fn destroy_record_decoder(decoder: *mut CRecordDecoder) { |
| 93 | + if decoder.is_null() { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + unsafe { |
| 98 | + let _ = Box::from_raw(decoder); |
| 99 | + } |
| 100 | +} |
0 commit comments