From 42d49fa637445a9a8d7679a90fe99d2b97b064e1 Mon Sep 17 00:00:00 2001 From: Simon Walker Date: Sat, 20 Jul 2024 23:16:31 +0100 Subject: [PATCH] Read comments --- fitsio/src/headers.rs | 44 +++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/fitsio/src/headers.rs b/fitsio/src/headers.rs index ed2042d7..0a610f55 100644 --- a/fitsio/src/headers.rs +++ b/fitsio/src/headers.rs @@ -7,7 +7,10 @@ use std::ffi; use std::fmt::Debug; use std::ptr; +// FLEN_VALUE const MAX_VALUE_LENGTH: usize = 71; +// FLEN_COMMENT +const MAX_COMMENT_LENGTH: usize = 73; /// Struct representing a FITS header value pub struct HeaderValue { @@ -93,20 +96,27 @@ macro_rules! reads_key_impl { let c_name = ffi::CString::new(name)?; let mut status = 0; let mut value: Self = Self::default(); + let mut comment: Vec = vec![0; MAX_COMMENT_LENGTH]; unsafe { $func( f.fptr.as_mut() as *mut _, c_name.as_ptr(), &mut value, - ptr::null_mut(), + comment.as_mut_ptr(), &mut status, ); } + let comment: Vec = comment + .iter() + .map(|&x| x as u8) + .filter(|&x| x != 0) + .collect(); + check_status(status).map(|_| HeaderValue { value, - comment: None, + comment: String::from_utf8(comment).ok(), }) } } @@ -135,22 +145,28 @@ impl ReadsKey for String { let c_name = ffi::CString::new(name)?; let mut status = 0; let mut value: Vec = vec![0; MAX_VALUE_LENGTH]; + let mut comment: Vec = vec![0; MAX_COMMENT_LENGTH]; unsafe { fits_read_key_str( f.fptr.as_mut() as *mut _, c_name.as_ptr(), value.as_mut_ptr(), - ptr::null_mut(), + comment.as_mut_ptr(), &mut status, ); } check_status(status).and_then(|_| { let value: Vec = value.iter().map(|&x| x as u8).filter(|&x| x != 0).collect(); + let comment: Vec = comment + .iter() + .map(|&x| x as u8) + .filter(|&x| x != 0) + .collect(); Ok(HeaderValue { value: String::from_utf8(value)?, - comment: None, + comment: String::from_utf8(comment).ok(), }) }) } @@ -429,22 +445,10 @@ mod tests { FitsFile::open(filename) .map(|mut f| { - assert_eq!( - f.hdu(0) - .unwrap() - .read_key::(&mut f, "foo") - .unwrap() - .value, - 1 - ); - assert_eq!( - f.hdu(0) - .unwrap() - .read_key::(&mut f, "bar") - .unwrap() - .value, - "baz".to_string() - ); + let foo_header_value = + f.hdu(0).unwrap().read_key::(&mut f, "foo").unwrap(); + assert_eq!(foo_header_value.value, 1); + assert_eq!(foo_header_value.comment, Some("Foo value".to_string())); }) .unwrap(); });