From faad71edf4333318559476776dda5a0cf8ad0ce6 Mon Sep 17 00:00:00 2001 From: josh Date: Fri, 20 Jun 2014 06:41:13 +0900 Subject: [PATCH] complete filesystem api --- src/sdl2/filesystem.rs | 44 ++++++++++++++++++++++++++++++++++++++++++ src/sdl2/lib.rs | 1 + 2 files changed, 45 insertions(+) create mode 100644 src/sdl2/filesystem.rs 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;