Store dc_kml_t::locations as Option<dc_array_t> instead of pointer

This commit is contained in:
Alexander Krotov
2019-07-28 15:50:41 +03:00
parent 14e42b48bd
commit 81a84620eb
4 changed files with 38 additions and 41 deletions

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<dc_array_t>,
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(),
} }
@@ -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<dc_array_t>,
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,31 @@ 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 i in 0..locations.len() {
let location = dc_array_get_ptr(locations, i as size_t) as *mut dc_location; let location = locations.get_location(i as size_t);
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 +500,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(dc_array_t::new_locations(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 +586,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()).add_location(location);
} }
(*kml).tag = 0 (*kml).tag = 0
}; };
@@ -636,11 +637,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

@@ -135,12 +135,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

@@ -771,7 +771,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 {
@@ -797,7 +797,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

@@ -840,24 +840,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.get_latitude(0) > 53.6f64);
assert!(dc_array_get_latitude(kml.locations, 0) < 53.8f64); assert!(locations_ref.get_latitude(0) < 53.8f64);
assert!(dc_array_get_longitude(kml.locations, 0) > 9.3f64); assert!(locations_ref.get_longitude(0) > 9.3f64);
assert!(dc_array_get_longitude(kml.locations, 0) < 9.5f64); assert!(locations_ref.get_longitude(0) < 9.5f64);
assert!(dc_array_get_accuracy(kml.locations, 0) > 31.9f64); assert!(locations_ref.get_accuracy(0) > 31.9f64);
assert!(dc_array_get_accuracy(kml.locations, 0) < 32.1f64); assert!(locations_ref.get_accuracy(0) < 32.1f64);
assert_eq!(dc_array_get_timestamp(kml.locations, 0), 1551906597); assert_eq!(locations_ref.get_timestamp(0), 1551906597);
assert!(dc_array_get_latitude(kml.locations, 1) > 63.6f64); assert!(locations_ref.get_latitude(1) > 63.6f64);
assert!(dc_array_get_latitude(kml.locations, 1) < 63.8f64); assert!(locations_ref.get_latitude(1) < 63.8f64);
assert!(dc_array_get_longitude(kml.locations, 1) > 19.3f64); assert!(locations_ref.get_longitude(1) > 19.3f64);
assert!(dc_array_get_longitude(kml.locations, 1) < 19.5f64); assert!(locations_ref.get_longitude(1) < 19.5f64);
assert!(dc_array_get_accuracy(kml.locations, 1) > 2.4f64); assert!(locations_ref.get_accuracy(1) > 2.4f64);
assert!(dc_array_get_accuracy(kml.locations, 1) < 2.6f64); assert!(locations_ref.get_accuracy(1) < 2.6f64);
assert_eq!(locations_ref.get_timestamp(1), 1544739072);
assert_eq!(dc_array_get_timestamp(kml.locations, 1), 1544739072);
dc_kml_unref(&mut kml); dc_kml_unref(&mut kml);
} }