Replace dc_array_t with Vec in context.rs

This commit is contained in:
Alexander Krotov
2019-08-17 16:54:52 +03:00
committed by holger krekel
parent 799d362654
commit 6b2fe03d08
4 changed files with 24 additions and 47 deletions

View File

@@ -425,7 +425,7 @@ pub unsafe extern "C" fn dc_get_chat_msgs(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
chat::get_chat_msgs(context, chat_id, flags, marker1before) dc_array_t::from(chat::get_chat_msgs(context, chat_id, flags, marker1before)).into_raw()
} }
#[no_mangle] #[no_mangle]
@@ -454,7 +454,7 @@ pub unsafe extern "C" fn dc_get_fresh_msgs(
assert!(!context.is_null()); assert!(!context.is_null());
let context = &*context; let context = &*context;
context::dc_get_fresh_msgs(context) dc_array_t::from(context::dc_get_fresh_msgs(context)).into_raw()
} }
#[no_mangle] #[no_mangle]
@@ -571,7 +571,7 @@ pub unsafe extern "C" fn dc_search_msgs(
assert!(!query.is_null()); assert!(!query.is_null());
let context = &*context; let context = &*context;
context::dc_search_msgs(context, chat_id, query) dc_array_t::from(context::dc_search_msgs(context, chat_id, query)).into_raw()
} }
#[no_mangle] #[no_mangle]

View File

@@ -261,11 +261,9 @@ unsafe fn log_msg(context: &Context, prefix: impl AsRef<str>, msg: *mut dc_msg_t
free(msgtext as *mut libc::c_void); free(msgtext as *mut libc::c_void);
} }
unsafe fn log_msglist(context: &Context, msglist: *mut dc_array_t) { unsafe fn log_msglist(context: &Context, msglist: &Vec<u32>) {
let cnt = dc_array_get_cnt(msglist) as usize;
let mut lines_out = 0; let mut lines_out = 0;
for i in 0..cnt { for &msg_id in msglist {
let msg_id = dc_array_get_id(msglist, i as size_t);
if msg_id == 9 as libc::c_uint { if msg_id == 9 as libc::c_uint {
info!( info!(
context, context,
@@ -274,7 +272,7 @@ unsafe fn log_msglist(context: &Context, msglist: *mut dc_array_t) {
); );
lines_out += 1 lines_out += 1
} else if msg_id > 0 as libc::c_uint { } else if msg_id > 0 {
if lines_out == 0 { if lines_out == 0 {
info!( info!(
context, 0, context, 0,
@@ -694,10 +692,7 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
"" ""
}, },
); );
if !msglist.is_null() { log_msglist(context, &msglist);
log_msglist(context, msglist);
dc_array_unref(msglist);
}
let draft = chat::get_draft(context, sel_chat.get_id()); let draft = chat::get_draft(context, sel_chat.get_id());
if !draft.is_null() { if !draft.is_null() {
log_msg(context, "Draft", draft); log_msg(context, "Draft", draft);
@@ -892,13 +887,10 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
0 as libc::c_uint 0 as libc::c_uint
}; };
let msglist_0 = dc_search_msgs(context, chat, arg1_c); let msglist = dc_search_msgs(context, chat, arg1_c);
if !msglist_0.is_null() { log_msglist(context, &msglist);
log_msglist(context, msglist_0); println!("{} messages.", msglist.len());
println!("{} messages.", dc_array_get_cnt(msglist_0));
dc_array_unref(msglist_0);
}
} }
"draft" => { "draft" => {
ensure!(sel_chat.is_some(), "No chat selected."); ensure!(sel_chat.is_some(), "No chat selected.");
@@ -963,11 +955,9 @@ pub unsafe fn dc_cmdline(context: &Context, line: &str) -> Result<(), failure::E
} }
"listfresh" => { "listfresh" => {
let msglist = dc_get_fresh_msgs(context); let msglist = dc_get_fresh_msgs(context);
ensure!(!msglist.is_null(), "Failed to retrieve messages");
log_msglist(context, msglist); log_msglist(context, &msglist);
print!("{} fresh messages.", dc_array_get_cnt(msglist)); print!("{} fresh messages.", msglist.len());
dc_array_unref(msglist);
} }
"forward" => { "forward" => {
ensure!( ensure!(

View File

@@ -1049,12 +1049,7 @@ pub unsafe fn get_draft(context: &Context, chat_id: u32) -> *mut dc_msg_t {
draft_msg draft_msg
} }
pub fn get_chat_msgs( pub fn get_chat_msgs(context: &Context, chat_id: u32, flags: u32, marker1before: u32) -> Vec<u32> {
context: &Context,
chat_id: u32,
flags: u32,
marker1before: u32,
) -> *mut dc_array_t {
let mut ret = Vec::new(); let mut ret = Vec::new();
let mut last_day = 0; let mut last_day = 0;
@@ -1123,9 +1118,9 @@ pub fn get_chat_msgs(
}; };
if success.is_ok() { if success.is_ok() {
dc_array_t::from(ret).into_raw() ret
} else { } else {
0 as *mut dc_array_t Vec::new()
} }
} }

View File

@@ -3,7 +3,6 @@ use std::sync::{Arc, Condvar, Mutex, RwLock};
use crate::chat::*; use crate::chat::*;
use crate::constants::*; use crate::constants::*;
use crate::contact::*; use crate::contact::*;
use crate::dc_array::*;
use crate::dc_job::*; use crate::dc_job::*;
use crate::dc_jobthread::*; use crate::dc_jobthread::*;
use crate::dc_loginparam::*; use crate::dc_loginparam::*;
@@ -484,7 +483,7 @@ pub unsafe fn dc_get_version_str() -> *mut libc::c_char {
(&*DC_VERSION_STR).strdup() (&*DC_VERSION_STR).strdup()
} }
pub fn dc_get_fresh_msgs(context: &Context) -> *mut dc_array_t { pub fn dc_get_fresh_msgs(context: &Context) -> Vec<u32> {
let show_deaddrop = 0; let show_deaddrop = 0;
context context
@@ -504,7 +503,7 @@ pub fn dc_get_fresh_msgs(context: &Context) -> *mut dc_array_t {
let id: u32 = row?; let id: u32 = row?;
ret.push(id); ret.push(id);
} }
Ok(dc_array_t::from(ret).into_raw()) Ok(ret)
}, },
) )
.unwrap() .unwrap()
@@ -515,14 +514,14 @@ pub fn dc_search_msgs(
context: &Context, context: &Context,
chat_id: uint32_t, chat_id: uint32_t,
query: *const libc::c_char, query: *const libc::c_char,
) -> *mut dc_array_t { ) -> Vec<u32> {
if query.is_null() { if query.is_null() {
return std::ptr::null_mut(); return Vec::new();
} }
let real_query = to_string(query).trim().to_string(); let real_query = to_string(query).trim().to_string();
if real_query.is_empty() { if real_query.is_empty() {
return std::ptr::null_mut(); return Vec::new();
} }
let strLikeInText = format!("%{}%", &real_query); let strLikeInText = format!("%{}%", &real_query);
let strLikeBeg = format!("{}%", &real_query); let strLikeBeg = format!("{}%", &real_query);
@@ -538,28 +537,21 @@ pub fn dc_search_msgs(
AND ct.blocked=0 AND (m.txt LIKE ? OR ct.name LIKE ?) ORDER BY m.timestamp DESC,m.id DESC;" AND ct.blocked=0 AND (m.txt LIKE ? OR ct.name LIKE ?) ORDER BY m.timestamp DESC,m.id DESC;"
}; };
let mut ret = Vec::new(); context
let success = context
.sql .sql
.query_map( .query_map(
query, query,
params![chat_id as libc::c_int, &strLikeInText, &strLikeBeg], params![chat_id as libc::c_int, &strLikeInText, &strLikeBeg],
|row| row.get::<_, i32>(0), |row| row.get::<_, i32>(0),
|rows| { |rows| {
let mut ret = Vec::new();
for id in rows { for id in rows {
ret.push(id? as u32); ret.push(id? as u32);
} }
Ok(()) Ok(ret)
}, },
) )
.is_ok(); .unwrap_or_default()
if success {
return dc_array_t::from(ret).into_raw();
}
std::ptr::null_mut()
} }
pub fn dc_is_inbox(_context: &Context, folder_name: impl AsRef<str>) -> bool { pub fn dc_is_inbox(_context: &Context, folder_name: impl AsRef<str>) -> bool {