diff --git a/src/dc_array.rs b/src/dc_array.rs index 2ca9164c2..5e0fb97fa 100644 --- a/src/dc_array.rs +++ b/src/dc_array.rs @@ -147,6 +147,12 @@ impl dc_array_t { } } +impl From> for dc_array_t { + fn from(array: Vec) -> Self { + dc_array_t::Locations(array) + } +} + pub unsafe fn dc_array_unref(array: *mut dc_array_t) { if array.is_null() { return; diff --git a/src/dc_location.rs b/src/dc_location.rs index bdef8671e..b16e3cb74 100644 --- a/src/dc_location.rs +++ b/src/dc_location.rs @@ -51,7 +51,7 @@ impl dc_location { #[allow(non_camel_case_types)] pub struct dc_kml_t { pub addr: *mut libc::c_char, - pub locations: *mut dc_array_t, + pub locations: Option>, pub tag: libc::c_int, pub curr: dc_location, } @@ -60,7 +60,7 @@ impl dc_kml_t { pub fn new() -> Self { dc_kml_t { addr: std::ptr::null_mut(), - locations: std::ptr::null_mut(), + locations: None, tag: 0, curr: dc_location::new(), } @@ -250,12 +250,12 @@ pub fn dc_get_locations( Ok(loc) }, |locations| { - let mut ret = dc_array_t::new_locations(500); + let mut ret = Vec::new(); for location in locations { - ret.add_location(location?); + ret.push(location?); } - Ok(ret.into_raw()) + Ok(dc_array_t::from(ret).into_raw()) }, ) .unwrap_or_else(|_| std::ptr::null_mut()) @@ -422,13 +422,14 @@ pub unsafe fn dc_save_locations( context: &Context, chat_id: u32, contact_id: u32, - locations: *const dc_array_t, + locations_opt: &Option>, independent: libc::c_int, ) -> u32 { - if chat_id <= 9 || locations.is_null() { + if chat_id <= 9 || locations_opt.is_none() { return 0; } + let locations = locations_opt.as_ref().unwrap(); context .sql .prepare2( @@ -440,31 +441,29 @@ pub unsafe fn dc_save_locations( let mut newest_timestamp = 0; let mut newest_location_id = 0; - for i in 0..dc_array_get_cnt(locations) { - let location = dc_array_get_ptr(locations, i as size_t) as *mut dc_location; - + for location in locations { let exists = - stmt_test.exists(params![(*location).timestamp, contact_id as i32])?; + stmt_test.exists(params![location.timestamp, contact_id as i32])?; if 0 != independent || !exists { stmt_insert.execute(params![ - (*location).timestamp, + location.timestamp, contact_id as i32, chat_id as i32, - (*location).latitude, - (*location).longitude, - (*location).accuracy, + location.latitude, + location.longitude, + location.accuracy, independent, ])?; - if (*location).timestamp > newest_timestamp { - newest_timestamp = (*location).timestamp; + if location.timestamp > newest_timestamp { + newest_timestamp = location.timestamp; newest_location_id = sql::get_rowid2_with_conn( context, conn, "locations", "timestamp", - (*location).timestamp, + location.timestamp, "from_id", contact_id as i32, ); @@ -499,7 +498,7 @@ pub unsafe fn dc_kml_parse( } else { content_nullterminated = dc_null_terminate(content, content_bytes as libc::c_int); if !content_nullterminated.is_null() { - kml.locations = dc_array_new_locations(100); + kml.locations = Some(Vec::with_capacity(100)); dc_saxparser_init( &mut saxparser, &mut kml as *mut dc_kml_t as *mut libc::c_void, @@ -585,7 +584,7 @@ unsafe fn kml_endtag_cb(userdata: *mut libc::c_void, tag: *const libc::c_char) { && 0. != (*kml).curr.longitude { let location = (*kml).curr.clone(); - (*(*kml).locations).add_location(location); + ((*kml).locations.as_mut().unwrap()).push(location); } (*kml).tag = 0 }; @@ -636,11 +635,7 @@ unsafe fn kml_starttag_cb( }; } -pub unsafe fn dc_kml_unref(kml: *mut dc_kml_t) { - if kml.is_null() { - return; - } - dc_array_unref((*kml).locations); +pub unsafe fn dc_kml_unref(kml: &mut dc_kml_t) { free((*kml).addr as *mut libc::c_void); } diff --git a/src/dc_mimeparser.rs b/src/dc_mimeparser.rs index f740d3232..b0b06cc0b 100644 --- a/src/dc_mimeparser.rs +++ b/src/dc_mimeparser.rs @@ -130,12 +130,12 @@ pub unsafe fn dc_mimeparser_empty(mimeparser: &mut dc_mimeparser_t) { dc_e2ee_thanks(&mut (*mimeparser).e2ee_helper); if let Some(location_kml) = (*mimeparser).location_kml.as_mut() { - dc_kml_unref(location_kml as *mut dc_kml_t); + dc_kml_unref(location_kml); } (*mimeparser).location_kml = None; if let Some(message_kml) = (*mimeparser).message_kml.as_mut() { - dc_kml_unref(message_kml as *mut dc_kml_t); + dc_kml_unref(message_kml); } (*mimeparser).message_kml = None; } diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index a01362b72..8821cab07 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -764,7 +764,7 @@ pub unsafe fn dc_receive_imf( context, chat_id, from_id, - mime_parser.message_kml.unwrap().locations, + &mime_parser.message_kml.unwrap().locations, 1, ); if 0 != newest_location_id && 0 == hidden { @@ -790,7 +790,7 @@ pub unsafe fn dc_receive_imf( context, chat_id, from_id, - mime_parser.location_kml.as_ref().unwrap().locations, + &mime_parser.location_kml.as_ref().unwrap().locations, 0, ); if newest_location_id != 0 && hidden == 0 && !location_id_written { diff --git a/tests/stress.rs b/tests/stress.rs index 4f363cb5e..c5a56f4f2 100644 --- a/tests/stress.rs +++ b/tests/stress.rs @@ -827,24 +827,24 @@ fn test_dc_kml_parse() { assert!(!kml.addr.is_null()); assert_eq!(as_str(kml.addr as *const libc::c_char), "user@example.org",); - assert_eq!(dc_array_get_cnt(kml.locations), 2); + let locations_ref = &kml.locations.as_ref().unwrap(); + assert_eq!(locations_ref.len(), 2); - assert!(dc_array_get_latitude(kml.locations, 0) > 53.6f64); - assert!(dc_array_get_latitude(kml.locations, 0) < 53.8f64); - assert!(dc_array_get_longitude(kml.locations, 0) > 9.3f64); - assert!(dc_array_get_longitude(kml.locations, 0) < 9.5f64); - assert!(dc_array_get_accuracy(kml.locations, 0) > 31.9f64); - assert!(dc_array_get_accuracy(kml.locations, 0) < 32.1f64); - assert_eq!(dc_array_get_timestamp(kml.locations, 0), 1551906597); + assert!(locations_ref[0].latitude > 53.6f64); + assert!(locations_ref[0].latitude < 53.8f64); + assert!(locations_ref[0].longitude > 9.3f64); + assert!(locations_ref[0].longitude < 9.5f64); + assert!(locations_ref[0].accuracy > 31.9f64); + assert!(locations_ref[0].accuracy < 32.1f64); + assert_eq!(locations_ref[0].timestamp, 1551906597); - assert!(dc_array_get_latitude(kml.locations, 1) > 63.6f64); - assert!(dc_array_get_latitude(kml.locations, 1) < 63.8f64); - assert!(dc_array_get_longitude(kml.locations, 1) > 19.3f64); - assert!(dc_array_get_longitude(kml.locations, 1) < 19.5f64); - assert!(dc_array_get_accuracy(kml.locations, 1) > 2.4f64); - assert!(dc_array_get_accuracy(kml.locations, 1) < 2.6f64); - - assert_eq!(dc_array_get_timestamp(kml.locations, 1), 1544739072); + assert!(locations_ref[1].latitude > 63.6f64); + assert!(locations_ref[1].latitude < 63.8f64); + assert!(locations_ref[1].longitude > 19.3f64); + assert!(locations_ref[1].longitude < 19.5f64); + assert!(locations_ref[1].accuracy > 2.4f64); + assert!(locations_ref[1].accuracy < 2.6f64); + assert_eq!(locations_ref[1].timestamp, 1544739072); dc_kml_unref(&mut kml); }