Impl Clone for ExifKey

This commit is contained in:
2022-07-30 04:31:56 +00:00
committed by GitHub
parent 55bd0d3754
commit 5accb5e33a
3 changed files with 26 additions and 0 deletions

View File

@@ -35,6 +35,7 @@ EXIF_API int exif_image_write_metadata(ExifImage* image);
EXIF_API void free_exif_image(ExifImage* img);
EXIF_API ExifKey* exif_create_key_by_key(const char* key);
EXIF_API ExifKey* exif_create_key_by_id(uint16_t id, const char* group_name);
EXIF_API ExifKey* exif_create_key_by_another(ExifKey* key);
EXIF_API char* exif_get_key_key(ExifKey* key);
EXIF_API char* exif_get_key_family_name(ExifKey* key);
EXIF_API char* exif_get_key_group_name(ExifKey* key);

View File

@@ -95,6 +95,19 @@ end:
return nullptr;
}
ExifKey* exif_create_key_by_another(ExifKey* key) {
if (!key) return nullptr;
ExifKey* k = new ExifKey;
if (!k) return nullptr;
Exiv2::ExifKey* ek = new Exiv2::ExifKey(*(key->key));
if (!ek) {
delete k;
return nullptr;
}
k->key = ek;
return k;
}
char* exif_get_key_key(ExifKey* key) {
if (!key || !key->key) return nullptr;
auto s = key->key->key();

View File

@@ -130,6 +130,16 @@ impl Drop for ExifKey {
}
}
impl Clone for ExifKey {
fn clone(&self) -> Self {
let key = unsafe { _exif::exif_create_key_by_another(self.key) };
if key.is_null() {
panic!("Out of memory.");
}
Self { key }
}
}
impl ToRawHandle<_exif::ExifKey> for ExifKey {
unsafe fn to_raw_handle(&self) -> *mut _exif::ExifKey {
self.key
@@ -782,6 +792,8 @@ fn test_exif_key() {
assert!(k2.is_ok());
let k2 = k2.unwrap();
assert_eq!(Some(String::from("Exif.Image.XPTitle")), k2.key());
let k3 = k2.clone();
assert_eq!(Some(String::from("Exif.Image.XPTitle")), k3.key());
}
#[test]