diff --git a/CHANGES.md b/CHANGES.md index 5e55113da..d32f1de24 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## Unreleased +- Added `CslStringList::add_string` + + - + - **Possibly breaking**: Set MSRV to 1.58. - diff --git a/src/cpl.rs b/src/cpl.rs index 8f0afca00..0fa779059 100644 --- a/src/cpl.rs +++ b/src/cpl.rs @@ -7,7 +7,9 @@ use std::ffi::CString; use std::fmt::{Debug, Formatter}; use std::ptr; -use gdal_sys::{CSLCount, CSLDestroy, CSLDuplicate, CSLFetchNameValue, CSLSetNameValue}; +use gdal_sys::{ + CSLAddString, CSLCount, CSLDestroy, CSLDuplicate, CSLFetchNameValue, CSLSetNameValue, +}; use libc::c_char; use crate::errors::{GdalError, Result}; @@ -57,6 +59,17 @@ impl CslStringList { Ok(()) } + /// Adds the string `value` to the list. + /// + /// Returns `Ok<()>` on success, `Err` if `value` cannot be converted to a C string. + /// + /// See: [`CSLAddString`](https://gdal.org/api/cpl.html#_CPPv412CSLAddStringPPcPKc) + pub fn add_string(&mut self, value: &str) -> Result<()> { + let v = CString::new(value)?; + self.list_ptr = unsafe { CSLAddString(self.list_ptr, v.as_ptr()) }; + Ok(()) + } + /// Looks up the value corresponding to `key`. /// /// See [`CSLFetchNameValue`](https://gdal.org/doxygen/cpl__string_8h.html#a4f23675f8b6f015ed23d9928048361a1) @@ -269,4 +282,17 @@ mod tests { Ok(()) } + + #[test] + fn can_add_strings() -> Result<()> { + let mut l = CslStringList::new(); + assert!(l.is_empty()); + l.add_string("-abc")?; + l.add_string("-d_ef")?; + l.add_string("A")?; + l.add_string("B")?; + assert_eq!(l.len(), 4); + + Ok(()) + } }