make CStringVec null terminated

gui-branch
an 2019-03-22 23:05:38 -04:00
parent 135e9c688a
commit 74fdfcaa43
1 changed files with 11 additions and 8 deletions

View File

@ -6,7 +6,7 @@ pub use std::{ffi::*, os::raw::*, ptr::{null, null_mut}};
/// Creates a C string from a literal. /// Creates a C string from a literal.
#[macro_export] #[macro_export]
macro_rules! c_str { macro_rules! c_str {
($s:expr) => {concat!($s, "\0").as_ptr() as *const c_char}; ($s:expr) => {concat!($s, "\0").as_ptr() as $crate::durandal::ffi::NT};
} }
impl CStringVec impl CStringVec
@ -14,7 +14,7 @@ impl CStringVec
/// Creates a new empty CStringVec. /// Creates a new empty CStringVec.
pub fn new() -> Self pub fn new() -> Self
{ {
Self::default() Self{sv: Vec::new(), cv: vec![null()]}
} }
/// Creates a new `CStringVec` from an iterator. /// Creates a new `CStringVec` from an iterator.
@ -33,29 +33,32 @@ impl CStringVec
/// Pushes a new `CString`. /// Pushes a new `CString`.
pub fn push(&mut self, st: CString) pub fn push(&mut self, st: CString)
{ {
self.cv.push(st.as_c_str().as_ptr()); self.cv.insert(self.cv.len() - 1, st.as_c_str().as_ptr());
self.sv.push(st); self.sv.push(st);
} }
/// Returns the FFI pointer. /// Returns the FFI pointer.
pub fn as_ptr(&self) -> *const *const c_char pub fn as_ptr(&self) -> *const NT
{ {
self.cv.as_ptr() self.cv.as_ptr()
} }
/// Returns the FFI pointer mutably. /// Returns the FFI pointer mutably.
pub fn as_mut_ptr(&mut self) -> *mut *const c_char pub fn as_mut_ptr(&mut self) -> *mut NT
{ {
self.cv.as_mut_ptr() self.cv.as_mut_ptr()
} }
} }
/// An owned FFI-compatible string vector. /// An owned null-terminated string vector.
#[derive(Default)] #[derive(Debug)]
pub struct CStringVec pub struct CStringVec
{ {
sv: Vec<CString>, sv: Vec<CString>,
cv: Vec<*const c_char>, cv: Vec<NT>,
} }
/// A null-terminated byte string pointer.
pub type NT = *const c_char;
// EOF // EOF