diff --git a/src/sdl2/filesystem.rs b/src/sdl2/filesystem.rs new file mode 100644 index 00000000..d1d398ef --- /dev/null +++ b/src/sdl2/filesystem.rs @@ -0,0 +1,44 @@ +use std::str; +use SdlResult; +use get_error; + +#[allow(non_camel_case_types)] +pub mod ll { + use libc::{c_char}; + + extern "C" { + pub fn SDL_GetBasePath() -> *c_char; + pub fn SDL_GetPrefPath(arg: *c_char, app: *c_char) -> *c_char; + } +} + +pub fn get_base_path() -> SdlResult { + let result = unsafe { + let cstr = ll::SDL_GetBasePath(); + str::raw::from_c_str(cstr) + }; + + if result.len() == 0 { + Err(get_error()) + } else { + Ok(result) + } +} + +pub fn get_pref_path(org: &str, app: &str) -> SdlResult { + let result = unsafe { + let cstr = + org.with_c_str(|org_cstr| { + app.with_c_str(|app_cstr| { + ll::SDL_GetPrefPath(org_cstr, app_cstr) + })}); + str::raw::from_c_str(cstr) + }; + + if result.len() == 0 { + Err(get_error()) + } else { + Ok(result) + } +} + diff --git a/src/sdl2/lib.rs b/src/sdl2/lib.rs index 867f42c3..5d4b44e3 100644 --- a/src/sdl2/lib.rs +++ b/src/sdl2/lib.rs @@ -20,6 +20,7 @@ pub mod scancode; pub mod cpuinfo; pub mod macros; pub mod event; +pub mod filesystem; pub mod gesture; pub mod touch; pub mod joystick;