Skip to content

Commit

Permalink
Merge #364
Browse files Browse the repository at this point in the history
364: Add CslStringList::add_string r=lnicola a=jshrake

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/gdal/blob/master/CODE_OF_CONDUCT.md).
- [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---

This method wraps gdal_sys::CSLAddString.

This method is useful when constructing argument lists for option structs for the various utility functions found in gdal_utils, such as GDALWarp and GDALTranslate.


Co-authored-by: Justin Shrake <justinshrake@gmail.com>
  • Loading branch information
bors[bot] and jshrake committed Feb 9, 2023
2 parents c7f88b5 + 57985f1 commit 24b7c12
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 24b7c12

Please sign in to comment.