Skip to content

Commit

Permalink
Added extra method to ffi to create metafits_context without passing …
Browse files Browse the repository at this point in the history
…mwa_version
  • Loading branch information
gsleap committed Aug 11, 2021
1 parent e303152 commit 973fbf1
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Changes in each release are listed below.

## 0.9.1 09-Aug-2021 (Pre-release)
* Added alternative version of mwalib_metafits_context_new (mwalib_metafits_context_new2) to FFI interface which does not require an MWAVersion and will determine it via the MODE keyword.
* Fixed errors, ommissions in comment/documentation for FFI function mwalib_metafits_get_expected_volt_filename().

## 0.9.0 09-Aug-2021 (Pre-release)
* Added mwa_version <Option<MWAVersion>> to MetafitsContext struct.
* When working only with a MetafitsContext, a None can be passed in lieu of an MWAVersion, and mwalib will attempt to determine the correct MWAVersion based on the MODE keyword from the metafits file.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mwalib"
version = "0.9.0"
version = "0.9.1"
homepage = "https://github.com/MWATelescope/mwalib"
repository = "https://github.com/MWATelescope/mwalib"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion examples/mwalib-print-context.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int main(int argc, char *argv[])
if (file_count == 1)
{
// Metafits only
if (mwalib_metafits_context_new(argv[1], CorrLegacy, &metafits_context, error_message, ERROR_MESSAGE_LEN) != EXIT_SUCCESS)
if (mwalib_metafits_context_new2(argv[1], &metafits_context, error_message, ERROR_MESSAGE_LEN) != EXIT_SUCCESS)
{
printf("Error getting metafits context: %s\n", error_message);
exit(-1);
Expand Down
64 changes: 53 additions & 11 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,13 @@ fn ffi_array_to_boxed_slice<T>(v: Vec<T>) -> *mut T {
}
}

/// Create and return a pointer to an `MetafitsContext` struct given only a metafits file
/// Create and return a pointer to an `MetafitsContext` struct given only a metafits file and MWAVersion.
///
/// # Arguments
///
/// * `metafits_filename` - pointer to char* buffer containing the full path and filename of a metafits file.
///
/// * `mwa_version` - enum providing mwalib with the intended mwa version which the metafits should be interpreted.
/// Pass 0 to get mwalib to detect the version from the MODE metafits keyword.
///
/// * `out_metafits_context_ptr` - A Rust-owned populated `MetafitsContext` pointer. Free with `mwalib_metafits_context_free'.
///
Expand Down Expand Up @@ -222,16 +221,59 @@ pub unsafe extern "C" fn mwalib_metafits_context_new(
.unwrap()
.to_string();

// In C/FFI any value can be passed in, even 0
let int_mwa_version = mwa_version as u8;
let context = match MetafitsContext::new(&m, Some(mwa_version)) {
Ok(c) => c,
Err(e) => {
set_c_string(
&format!("{}", e),
error_message as *mut u8,
error_message_length,
);
// Return failure
return MWALIB_FAILURE;
}
};

let context = match MetafitsContext::new(
&m,
match int_mwa_version {
0 => None,
_ => Some(mwa_version),
},
) {
*out_metafits_context_ptr = Box::into_raw(Box::new(context));

// Return success
MWALIB_SUCCESS
}

/// Create and return a pointer to an `MetafitsContext` struct given only a metafits file. Same as mwalib_metafits_context_new, but mwalib will guess the MWAVersion.
///
/// # Arguments
///
/// * `metafits_filename` - pointer to char* buffer containing the full path and filename of a metafits file.
///
/// * `out_metafits_context_ptr` - A Rust-owned populated `MetafitsContext` pointer. Free with `mwalib_metafits_context_free'.
///
/// * `error_message` - pointer to already allocated buffer for any error messages to be returned to the caller.
///
/// * `error_message_length` - length of error_message char* buffer.
///
///
/// # Returns
///
/// * return MWALIB_SUCCESS on success, non-zero on failure
///
///
/// # Safety
/// * `error_message` *must* point to an already allocated `char*` buffer for any error messages.
/// * Caller *must* call the `mwalib_metafits_context_free` function to release the rust memory.
#[no_mangle]
pub unsafe extern "C" fn mwalib_metafits_context_new2(
metafits_filename: *const c_char,
out_metafits_context_ptr: &mut *mut MetafitsContext,
error_message: *const c_char,
error_message_length: size_t,
) -> i32 {
let m = CStr::from_ptr(metafits_filename)
.to_str()
.unwrap()
.to_string();

let context = match MetafitsContext::new(&m, None) {
Ok(c) => c,
Err(e) => {
set_c_string(
Expand Down
36 changes: 36 additions & 0 deletions src/ffi/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,42 @@ fn test_mwalib_metafits_context_display() {
}
}

#[test]
fn test_mwalib_metafits_context_new_guess_mwa_version() {
let error_len: size_t = 128;
let error_message = CString::new(" ".repeat(error_len)).unwrap();
let error_message_ptr = error_message.as_ptr() as *const c_char;

let metafits_file =
CString::new("test_files/1101503312_1_timestep/1101503312.metafits").unwrap();
let metafits_file_ptr = metafits_file.as_ptr();

unsafe {
// Create a MetafitsContext
let mut metafits_context_ptr: *mut MetafitsContext = std::ptr::null_mut();
let retval = mwalib_metafits_context_new2(
metafits_file_ptr,
&mut metafits_context_ptr,
error_message_ptr,
error_len,
);

// Check return value of mwalib_metafits_context_new
assert_eq!(retval, 0, "mwalib_metafits_context_new failure");

// Check we got valid MetafitsContext pointer
let context_ptr = metafits_context_ptr.as_mut();
assert!(context_ptr.is_some());

let metafits_context = context_ptr.unwrap();

assert_eq!(
metafits_context.mwa_version.unwrap(),
MWAVersion::CorrLegacy
);
}
}

#[test]
fn test_mwalib_metafits_context_display_null_ptr() {
let metafits_context_ptr: *mut MetafitsContext = std::ptr::null_mut();
Expand Down
13 changes: 13 additions & 0 deletions src/metafits_context/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,19 @@ fn test_populate_expected_coarse_channels_corr_mwaxv2() {
}
}

#[test]
fn test_metafits_context_new_guess_version() {
// Open the test metafits file
let metafits_filename = "test_files/1101503312_1_timestep/1101503312.metafits";

// Open a context and load in a test metafits
let result = MetafitsContext::new(&metafits_filename, None);
assert!(result.is_ok());

let context = result.unwrap();
assert_eq!(context.mwa_version.unwrap(), MWAVersion::CorrLegacy);
}

#[test]
fn test_generate_expected_volt_filename_legacy_vcs() {
// Open the test metafits file
Expand Down

0 comments on commit 973fbf1

Please sign in to comment.