Skip to content

Commit

Permalink
Add CslStringList::add_string
Browse files Browse the repository at this point in the history
This method wraps gdal_sys::CSLAddString.
  • Loading branch information
jshrake committed Feb 9, 2023
1 parent bd8f877 commit 57985f1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

- Added `CslStringList::add_string`

- <https://github.com/georust/gdal/pull/364>

- **Possibly breaking**: Set MSRV to 1.58.

- <https://github.com/georust/gdal/pull/363>
Expand Down
28 changes: 27 additions & 1 deletion src/cpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -57,6 +59,17 @@ impl CslStringList {
Ok(())
}

/// Adds the string `value` to the list.
///
/// Returns `Ok<()>` on success, `Err<GdalError>` 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)
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit 57985f1

Please sign in to comment.