dc_location: store marker as Option<String> instead of C string

This commit is contained in:
Alexander Krotov
2019-07-25 14:26:33 +03:00
committed by Floris Bruynooghe
parent ec6cc5c355
commit 9f75a5049e
2 changed files with 41 additions and 39 deletions

View File

@@ -42,13 +42,7 @@ pub unsafe fn dc_array_free_ptr(array: *mut dc_array_t) {
} }
let mut i: size_t = 0i32 as size_t; let mut i: size_t = 0i32 as size_t;
while i < (*array).count { while i < (*array).count {
if (*array).type_0 == 1i32 { Box::from_raw(*(*array).array.offset(i as isize) as *mut dc_location);
free(
(*(*(*array).array.offset(i as isize) as *mut dc_location)).marker
as *mut libc::c_void,
);
}
free(*(*array).array.offset(i as isize) as *mut libc::c_void);
*(*array).array.offset(i as isize) = 0i32 as uintptr_t; *(*array).array.offset(i as isize) = 0i32 as uintptr_t;
i = i.wrapping_add(1) i = i.wrapping_add(1)
} }
@@ -203,7 +197,11 @@ pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *m
{ {
return 0 as *mut libc::c_char; return 0 as *mut libc::c_char;
} }
dc_strdup_keep_null((*(*(*array).array.offset(index as isize) as *mut dc_location)).marker) if let Some(s) = &(*(*(*array).array.offset(index as isize) as *mut dc_location)).marker {
to_cstring(s)
} else {
std::ptr::null_mut()
}
} }
/** /**

View File

@@ -13,8 +13,7 @@ use crate::types::*;
use crate::x::*; use crate::x::*;
// location handling // location handling
#[derive(Copy, Clone)] #[derive(Clone, Default)]
#[repr(C)]
pub struct dc_location { pub struct dc_location {
pub location_id: uint32_t, pub location_id: uint32_t,
pub latitude: libc::c_double, pub latitude: libc::c_double,
@@ -24,7 +23,7 @@ pub struct dc_location {
pub contact_id: uint32_t, pub contact_id: uint32_t,
pub msg_id: uint32_t, pub msg_id: uint32_t,
pub chat_id: uint32_t, pub chat_id: uint32_t,
pub marker: *mut libc::c_char, pub marker: Option<String>,
pub independent: uint32_t, pub independent: uint32_t,
} }
@@ -39,14 +38,13 @@ impl dc_location {
contact_id: 0, contact_id: 0,
msg_id: 0, msg_id: 0,
chat_id: 0, chat_id: 0,
marker: std::ptr::null_mut(), marker: None,
independent: 0, independent: 0,
} }
} }
} }
#[derive(Copy, Clone)] #[derive(Clone)]
#[repr(C)]
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: *mut dc_array_t,
@@ -226,34 +224,39 @@ pub fn dc_get_locations(
timestamp_from, timestamp_from,
timestamp_to, timestamp_to,
], ],
|row| unsafe { |row| {
let mut loc: *mut dc_location = let msg_id = row.get(6)?;
calloc(1, ::std::mem::size_of::<dc_location>()) as *mut dc_location; let txt: String = row.get(9)?;
assert!(!loc.is_null(), "allocation failed"); let marker = if msg_id != 0 && is_marker(&txt) {
Some(txt)
} else {
None
};
(*loc).location_id = row.get(0)?; let loc = dc_location {
(*loc).latitude = row.get(1)?; location_id: row.get(0)?,
(*loc).longitude = row.get(2)?; latitude: row.get(1)?,
(*loc).accuracy = row.get(3)?; longitude: row.get(2)?,
(*loc).timestamp = row.get(4)?; accuracy: row.get(3)?,
(*loc).independent = row.get(5)?; timestamp: row.get(4)?,
(*loc).msg_id = row.get(6)?; independent: row.get(5)?,
(*loc).contact_id = row.get(7)?; msg_id: msg_id,
(*loc).chat_id = row.get(8)?; contact_id: row.get(7)?,
chat_id: row.get(8)?,
if 0 != (*loc).msg_id { marker: marker,
let txt: String = row.get(9)?; };
if is_marker(&txt) {
(*loc).marker = to_cstring(txt);
}
}
Ok(loc) Ok(loc)
}, },
|locations| { |locations| {
let ret = unsafe { dc_array_new_typed(1, 500) }; let ret = unsafe { dc_array_new_typed(1, 500) };
for location in locations { for location in locations {
unsafe { dc_array_add_ptr(ret, location? as *mut libc::c_void) }; unsafe {
dc_array_add_ptr(
ret,
Box::into_raw(Box::new(location?)) as *mut libc::c_void,
)
};
} }
Ok(ret) Ok(ret)
}, },
@@ -584,10 +587,11 @@ unsafe fn kml_endtag_cb(userdata: *mut libc::c_void, tag: *const libc::c_char) {
&& 0. != (*kml).curr.latitude && 0. != (*kml).curr.latitude
&& 0. != (*kml).curr.longitude && 0. != (*kml).curr.longitude
{ {
let location: *mut dc_location = let location = (*kml).curr.clone();
calloc(1, ::std::mem::size_of::<dc_location>()) as *mut dc_location; dc_array_add_ptr(
*location = (*kml).curr; (*kml).locations,
dc_array_add_ptr((*kml).locations, location as *mut libc::c_void); Box::into_raw(Box::new(location)) as *mut libc::c_void,
);
} }
(*kml).tag = 0 (*kml).tag = 0
}; };