use crate::{QCoreApplication, QString};
use cpp_utils::{MutPtr, MutRef};
use std::iter::once;
use std::os::raw::{c_char, c_int};
use std::process;
pub struct QCoreApplicationArgs {
_values: Vec<Vec<u8>>,
argc: Box<c_int>,
argv: Vec<*mut c_char>,
}
impl QCoreApplicationArgs {
pub fn new() -> QCoreApplicationArgs {
let mut args = std::env::args()
.map(|arg| unsafe {
QString::from_std_str(&arg)
.to_local8_bit()
.as_slice()
.iter()
.map(|&c| c as u8)
.chain(once(0))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
QCoreApplicationArgs {
argc: Box::new(args.len() as c_int),
argv: args
.iter_mut()
.map(|x| x.as_mut_ptr() as *mut c_char)
.collect(),
_values: args,
}
}
pub fn get(&mut self) -> (*mut c_int, *mut *mut c_char) {
(self.argc.as_mut(), self.argv.as_mut_ptr())
}
}
impl QCoreApplication {
pub fn init<F: FnOnce(::cpp_utils::MutPtr<QCoreApplication>) -> i32>(f: F) -> ! {
let exit_code = {
unsafe {
let mut args = QCoreApplicationArgs::new();
let (argc, argv) = args.get();
let mut app = QCoreApplication::new_2a(
MutRef::from_raw(argc).unwrap(),
MutPtr::from_raw(argv),
);
f(app.as_mut_ptr())
}
}; process::exit(exit_code)
}
}