use crate::{QCoreApplication, QString};
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: Box<[*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::<Vec<_>>()
.into_boxed_slice(),
_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_core::Ptr<QCoreApplication>) -> i32>(f: F) -> ! {
let exit_code = {
unsafe {
let mut args = QCoreApplicationArgs::new();
let (argc, argv) = args.get();
let app = QCoreApplication::new_2a(argc, argv);
f(app.as_ptr())
}
}; process::exit(exit_code)
}
}