use super::bindings as ffi;
use super::util;
use crate::error::{CpdbError, Result};
use std::ffi::CString;
pub fn version() -> Result<String> {
let raw = unsafe { ffi::cpdbGetVersion() };
if raw.is_null() {
return Err(CpdbError::NullPointer);
}
unsafe { util::cstr_to_string(raw) }
}
pub fn init() {
unsafe { ffi::cpdbInit() };
}
pub fn user_config_dir() -> Result<String> {
unsafe { util::cstr_to_string_and_g_free(ffi::cpdbGetUserConfDir()) }
}
pub fn system_config_dir() -> Result<String> {
unsafe { util::cstr_to_string_and_g_free(ffi::cpdbGetSysConfDir()) }
}
pub fn absolute_path(path: &str) -> Result<String> {
let c_path = CString::new(path)?;
unsafe { util::cstr_to_string_and_g_free(ffi::cpdbGetAbsolutePath(c_path.as_ptr())) }
}
pub fn concat_sep(left: &str, right: &str) -> Result<String> {
let l = CString::new(left)?;
let r = CString::new(right)?;
unsafe { util::cstr_to_string_and_g_free(ffi::cpdbConcatSep(l.as_ptr(), r.as_ptr())) }
}
pub fn concat_path(parent: &str, child: &str) -> Result<String> {
let p = CString::new(parent)?;
let c = CString::new(child)?;
unsafe { util::cstr_to_string_and_g_free(ffi::cpdbConcatPath(p.as_ptr(), c.as_ptr())) }
}
pub fn option_group(option_name: &str) -> Result<Option<String>> {
let c_opt = CString::new(option_name)?;
let raw = unsafe { ffi::cpdbGetGroup(c_opt.as_ptr()) };
if raw.is_null() {
Ok(None)
} else {
unsafe { util::cstr_to_string_and_g_free(raw) }.map(Some)
}
}