Merge pull request #244 from link2xt/dc_location_vec

Replace some dc_array_t with Vec<dc_location>
This commit is contained in:
Friedel Ziegelmayer
2019-07-31 22:15:35 +02:00
committed by GitHub
5 changed files with 46 additions and 45 deletions

View File

@@ -147,6 +147,12 @@ impl dc_array_t {
} }
} }
impl From<Vec<dc_location>> for dc_array_t {
fn from(array: Vec<dc_location>) -> Self {
dc_array_t::Locations(array)
}
}
pub unsafe fn dc_array_unref(array: *mut dc_array_t) { pub unsafe fn dc_array_unref(array: *mut dc_array_t) {
if array.is_null() { if array.is_null() {
return; return;

View File

@@ -51,7 +51,7 @@ impl dc_location {
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub struct dc_kml_t { pub struct dc_kml_t {
pub addr: *mut libc::c_char, pub addr: *mut libc::c_char,
pub locations: *mut dc_array_t, pub locations: Option<Vec<dc_location>>,
pub tag: libc::c_int, pub tag: libc::c_int,
pub curr: dc_location, pub curr: dc_location,
} }
@@ -60,7 +60,7 @@ impl dc_kml_t {
pub fn new() -> Self { pub fn new() -> Self {
dc_kml_t { dc_kml_t {
addr: std::ptr::null_mut(), addr: std::ptr::null_mut(),
locations: std::ptr::null_mut(), locations: None,
tag: 0, tag: 0,
curr: dc_location::new(), curr: dc_location::new(),
} }
@@ -250,12 +250,12 @@ pub fn dc_get_locations(
Ok(loc) Ok(loc)
}, },
|locations| { |locations| {
let mut ret = dc_array_t::new_locations(500); let mut ret = Vec::new();
for location in locations { 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()) .unwrap_or_else(|_| std::ptr::null_mut())
@@ -422,13 +422,14 @@ pub unsafe fn dc_save_locations(
context: &Context, context: &Context,
chat_id: u32, chat_id: u32,
contact_id: u32, contact_id: u32,
locations: *const dc_array_t, locations_opt: &Option<Vec<dc_location>>,
independent: libc::c_int, independent: libc::c_int,
) -> u32 { ) -> u32 {
if chat_id <= 9 || locations.is_null() { if chat_id <= 9 || locations_opt.is_none() {
return 0; return 0;
} }
let locations = locations_opt.as_ref().unwrap();
context context
.sql .sql
.prepare2( .prepare2(
@@ -440,31 +441,29 @@ pub unsafe fn dc_save_locations(
let mut newest_timestamp = 0; let mut newest_timestamp = 0;
let mut newest_location_id = 0; let mut newest_location_id = 0;
for i in 0..dc_array_get_cnt(locations) { for location in locations {
let location = dc_array_get_ptr(locations, i as size_t) as *mut dc_location;
let exists = 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 { if 0 != independent || !exists {
stmt_insert.execute(params![ stmt_insert.execute(params![
(*location).timestamp, location.timestamp,
contact_id as i32, contact_id as i32,
chat_id as i32, chat_id as i32,
(*location).latitude, location.latitude,
(*location).longitude, location.longitude,
(*location).accuracy, location.accuracy,
independent, independent,
])?; ])?;
if (*location).timestamp > newest_timestamp { if location.timestamp > newest_timestamp {
newest_timestamp = (*location).timestamp; newest_timestamp = location.timestamp;
newest_location_id = sql::get_rowid2_with_conn( newest_location_id = sql::get_rowid2_with_conn(
context, context,
conn, conn,
"locations", "locations",
"timestamp", "timestamp",
(*location).timestamp, location.timestamp,
"from_id", "from_id",
contact_id as i32, contact_id as i32,
); );
@@ -499,7 +498,7 @@ pub unsafe fn dc_kml_parse(
} else { } else {
content_nullterminated = dc_null_terminate(content, content_bytes as libc::c_int); content_nullterminated = dc_null_terminate(content, content_bytes as libc::c_int);
if !content_nullterminated.is_null() { if !content_nullterminated.is_null() {
kml.locations = dc_array_new_locations(100); kml.locations = Some(Vec::with_capacity(100));
dc_saxparser_init( dc_saxparser_init(
&mut saxparser, &mut saxparser,
&mut kml as *mut dc_kml_t as *mut libc::c_void, &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 && 0. != (*kml).curr.longitude
{ {
let location = (*kml).curr.clone(); let location = (*kml).curr.clone();
(*(*kml).locations).add_location(location); ((*kml).locations.as_mut().unwrap()).push(location);
} }
(*kml).tag = 0 (*kml).tag = 0
}; };
@@ -636,11 +635,7 @@ unsafe fn kml_starttag_cb(
}; };
} }
pub unsafe fn dc_kml_unref(kml: *mut dc_kml_t) { pub unsafe fn dc_kml_unref(kml: &mut dc_kml_t) {
if kml.is_null() {
return;
}
dc_array_unref((*kml).locations);
free((*kml).addr as *mut libc::c_void); free((*kml).addr as *mut libc::c_void);
} }

View File

@@ -130,12 +130,12 @@ pub unsafe fn dc_mimeparser_empty(mimeparser: &mut dc_mimeparser_t) {
dc_e2ee_thanks(&mut (*mimeparser).e2ee_helper); dc_e2ee_thanks(&mut (*mimeparser).e2ee_helper);
if let Some(location_kml) = (*mimeparser).location_kml.as_mut() { 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; (*mimeparser).location_kml = None;
if let Some(message_kml) = (*mimeparser).message_kml.as_mut() { 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; (*mimeparser).message_kml = None;
} }

View File

@@ -764,7 +764,7 @@ pub unsafe fn dc_receive_imf(
context, context,
chat_id, chat_id,
from_id, from_id,
mime_parser.message_kml.unwrap().locations, &mime_parser.message_kml.unwrap().locations,
1, 1,
); );
if 0 != newest_location_id && 0 == hidden { if 0 != newest_location_id && 0 == hidden {
@@ -790,7 +790,7 @@ pub unsafe fn dc_receive_imf(
context, context,
chat_id, chat_id,
from_id, from_id,
mime_parser.location_kml.as_ref().unwrap().locations, &mime_parser.location_kml.as_ref().unwrap().locations,
0, 0,
); );
if newest_location_id != 0 && hidden == 0 && !location_id_written { if newest_location_id != 0 && hidden == 0 && !location_id_written {

View File

@@ -827,24 +827,24 @@ fn test_dc_kml_parse() {
assert!(!kml.addr.is_null()); assert!(!kml.addr.is_null());
assert_eq!(as_str(kml.addr as *const libc::c_char), "user@example.org",); 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!(locations_ref[0].latitude > 53.6f64);
assert!(dc_array_get_latitude(kml.locations, 0) < 53.8f64); assert!(locations_ref[0].latitude < 53.8f64);
assert!(dc_array_get_longitude(kml.locations, 0) > 9.3f64); assert!(locations_ref[0].longitude > 9.3f64);
assert!(dc_array_get_longitude(kml.locations, 0) < 9.5f64); assert!(locations_ref[0].longitude < 9.5f64);
assert!(dc_array_get_accuracy(kml.locations, 0) > 31.9f64); assert!(locations_ref[0].accuracy > 31.9f64);
assert!(dc_array_get_accuracy(kml.locations, 0) < 32.1f64); assert!(locations_ref[0].accuracy < 32.1f64);
assert_eq!(dc_array_get_timestamp(kml.locations, 0), 1551906597); assert_eq!(locations_ref[0].timestamp, 1551906597);
assert!(dc_array_get_latitude(kml.locations, 1) > 63.6f64); assert!(locations_ref[1].latitude > 63.6f64);
assert!(dc_array_get_latitude(kml.locations, 1) < 63.8f64); assert!(locations_ref[1].latitude < 63.8f64);
assert!(dc_array_get_longitude(kml.locations, 1) > 19.3f64); assert!(locations_ref[1].longitude > 19.3f64);
assert!(dc_array_get_longitude(kml.locations, 1) < 19.5f64); assert!(locations_ref[1].longitude < 19.5f64);
assert!(dc_array_get_accuracy(kml.locations, 1) > 2.4f64); assert!(locations_ref[1].accuracy > 2.4f64);
assert!(dc_array_get_accuracy(kml.locations, 1) < 2.6f64); assert!(locations_ref[1].accuracy < 2.6f64);
assert_eq!(locations_ref[1].timestamp, 1544739072);
assert_eq!(dc_array_get_timestamp(kml.locations, 1), 1544739072);
dc_kml_unref(&mut kml); dc_kml_unref(&mut kml);
} }