use std::{
ffi::{CStr, CString},
os::raw::{c_char, c_uint},
slice, str,
};
use libiio_sys::{self as ffi};
use nix::errno;
pub use crate::buffer::*;
pub use crate::channel::*;
pub use crate::context::*;
pub use crate::device::*;
pub use crate::errors::*;
mod macros;
pub mod buffer;
pub mod channel;
pub mod context;
pub mod device;
pub mod errors;
fn cstring_opt(pstr: *const c_char) -> Option<String> {
if pstr.is_null() {
None
}
else {
let name = unsafe { CStr::from_ptr(pstr) };
Some(name.to_str().unwrap_or_default().to_string())
}
}
pub(crate) fn sys_result<T>(ret: i32, result: T) -> Result<T> {
if ret < 0 {
Err(errno::from_i32(-ret).into())
}
else {
Ok(result)
}
}
pub fn library_version() -> (u32, u32, String) {
let mut major: c_uint = 0;
let mut minor: c_uint = 0;
const BUF_SZ: usize = 8;
let mut buf = vec![' ' as c_char; BUF_SZ];
let pbuf = buf.as_mut_ptr();
unsafe { ffi::iio_library_get_version(&mut major, &mut minor, pbuf) };
let sgit = unsafe {
if buf.contains(&0) {
CStr::from_ptr(pbuf).to_owned()
}
else {
let slc = str::from_utf8(slice::from_raw_parts(pbuf as *mut u8, BUF_SZ)).unwrap();
CString::new(slc).unwrap()
}
};
(
major as u32,
minor as u32,
sgit.to_string_lossy().into_owned(),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn version() {
let v1 = library_version();
let v2 = library_version();
assert!(v1 == v2);
}
}