1use crate::get_error;
2use libc::c_void;
3use std::error;
4use std::ffi::{CStr, CString, NulError};
5use std::fmt;
6
7use crate::sys;
8
9#[doc(alias = "SDL_GetBasePath")]
10pub fn base_path() -> Result<String, String> {
11 unsafe {
12 let buf = sys::SDL_GetBasePath();
13 if buf.is_null() {
14 Err(get_error())
15 } else {
16 let s = CStr::from_ptr(buf).to_str().unwrap().to_owned();
17 sys::SDL_free(buf as *mut c_void);
18 Ok(s)
19 }
20 }
21}
22
23#[derive(Debug, Clone)]
24pub enum PrefPathError {
25 InvalidOrganizationName(NulError),
26 InvalidApplicationName(NulError),
27 SdlError(String),
28}
29
30impl fmt::Display for PrefPathError {
31 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32 use self::PrefPathError::*;
33
34 match *self {
35 InvalidOrganizationName(ref e) => write!(f, "Invalid organization name: {}", e),
36 InvalidApplicationName(ref e) => write!(f, "Invalid application name: {}", e),
37 SdlError(ref e) => write!(f, "SDL error: {}", e),
38 }
39 }
40}
41
42impl error::Error for PrefPathError {
43 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
44 match self {
45 Self::InvalidOrganizationName(err) => Some(err),
46 Self::InvalidApplicationName(err) => Some(err),
47 Self::SdlError(_) => None,
48 }
49 }
50}
51
52#[doc(alias = "SDL_GetPrefPath")]
56pub fn pref_path(org_name: &str, app_name: &str) -> Result<String, PrefPathError> {
57 use self::PrefPathError::*;
58
59 let org = CString::new(org_name).map_err(InvalidOrganizationName)?;
60 let app = CString::new(app_name).map_err(InvalidApplicationName)?;
61
62 unsafe {
63 let buf = sys::SDL_GetPrefPath(org.as_ptr(), app.as_ptr());
64 if buf.is_null() {
65 Err(SdlError(get_error()))
66 } else {
67 let ret = CStr::from_ptr(buf).to_str().unwrap().to_owned();
68 sys::SDL_free(buf as *mut c_void);
69 Ok(ret)
70 }
71 }
72}