rust/physfs-rs/src/physfs/mod.rs
author alfadur
Tue, 23 Jun 2020 02:04:00 +0300
changeset 15628 e63748a3aa52
parent 14435 a1613788130d
permissions -rw-r--r--
workaround for world edge selector on MacOS
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14435
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
     1
use std::ffi::{ CString, CStr, OsStr };
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
     2
use std::io::Result;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
     3
use std::sync::{ Mutex };
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
     4
use libc::{ c_int, c_char };
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
     5
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
     6
/// Keep track of the number of global contexts.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
     7
static mut NUM_CONTEXTS: usize = 0;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
     8
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
     9
/// Utility
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    10
mod util;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    11
/// File operations
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    12
pub mod file;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    13
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    14
#[link(name = "physfs")]
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    15
extern {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    16
    // nonzero on success, zero on error.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    17
    fn PHYSFS_init(arg0: *const c_char) -> c_int;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    18
    // nonzero if initialized, zero if not.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    19
    fn PHYSFS_isInit() -> c_int;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    20
    // nonzero if success, zero if error.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    21
    fn PHYSFS_deinit() -> c_int;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    22
    // string if success, NULL if error.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    23
    fn PHYSFS_getLastError() -> *const c_char;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    24
    // nonzero if success, zero if error
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    25
    fn PHYSFS_mount(new_dir: *const c_char, mount_point: *const c_char, append_to_path: c_int) -> c_int;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    26
    // nonzero if success, zero if error.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    27
    fn PHYSFS_setWriteDir(write_dir: *const c_char) -> c_int;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    28
    // nonzero on success, zero on error.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    29
    fn PHYSFS_mkdir(dir_name: *const c_char) -> c_int;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    30
    // Checks if a given path exists; returns nonzero if true
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    31
    fn PHYSFS_exists(path: *const c_char) -> c_int;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    32
    // Checks if a given path is a directory; returns nonzero if true
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    33
    fn PHYSFS_isDirectory(path: *const c_char) -> c_int;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    34
}
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    35
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    36
/// The access point for PhysFS function calls.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    37
pub struct PhysFSContext;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    38
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    39
unsafe impl Send for PhysFSContext {}
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    40
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    41
impl PhysFSContext {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    42
    /// Creates a new PhysFS context.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    43
    pub fn new() -> Result<Self> {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    44
        let con = PhysFSContext;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    45
        match PhysFSContext::init() {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    46
            Err(e) => Err(e),
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    47
            _ => {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    48
                // Everything's gone right so far
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    49
                // now, increment the instance counter
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    50
                println!("Inc");
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    51
                unsafe {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    52
                    NUM_CONTEXTS += 1;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    53
                }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    54
                // and return the newly created context
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    55
                Ok(con)
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    56
            }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    57
        }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    58
    }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    59
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    60
    /// initializes the PhysFS library.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    61
    fn init() -> Result<()> {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    62
        // Initializing multiple times throws an error. So let's not!
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    63
        if PhysFSContext::is_init() { return Ok(()); }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    64
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    65
        let mut args = ::std::env::args();
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    66
        let default_arg0 = "".to_string();
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    67
        let arg0 = args.next().unwrap_or(default_arg0);
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    68
        let c_arg0 = try!(CString::new(arg0));
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    69
        let ret = unsafe { PHYSFS_init(c_arg0.as_ptr()) };
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    70
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    71
        match ret {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    72
            0 => Err(util::physfs_error_as_io_error()),
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    73
            _ => Ok(())
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    74
        }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    75
    }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    76
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    77
    /// Checks if PhysFS is initialized
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    78
    pub fn is_init() -> bool {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    79
        unsafe { PHYSFS_isInit() != 0 }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    80
    }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    81
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    82
    /// De-initializes PhysFS. It is recommended to close
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    83
    /// all file handles manually before calling this.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    84
    fn de_init() {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    85
        // de_init'ing more than once can cause a double-free -- do not want.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    86
        if !PhysFSContext::is_init() { return }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    87
        unsafe {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    88
            PHYSFS_deinit();
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    89
        }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    90
    }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    91
    /// Adds an archive or directory to the search path.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    92
    /// mount_point is the location in the tree to mount it to.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    93
    pub fn mount<P>(&self, new_dir: P, mount_point: String, append_to_path: bool) -> Result<()>
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    94
        where P: AsRef<OsStr>
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    95
    {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    96
        let c_new_dir = CString::new(new_dir.as_ref().to_string_lossy().as_bytes()).unwrap();
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    97
        let c_mount_point = try!(CString::new(mount_point));
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    98
        match unsafe {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
    99
            PHYSFS_mount(
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   100
                c_new_dir.as_c_str().as_ptr(),
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   101
                c_mount_point.as_ptr(),
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   102
                append_to_path as c_int
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   103
            )
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   104
        } {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   105
            0 => Err(util::physfs_error_as_io_error()),
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   106
            _ => Ok(())
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   107
        }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   108
    }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   109
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   110
    /// Gets the last error message in a human-readable format
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   111
    /// This message may be localized, so do not expect it to
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   112
    /// match a specific string of characters.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   113
    pub fn get_last_error() -> String {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   114
        let ptr: *const c_char = unsafe {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   115
            PHYSFS_getLastError()
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   116
        };
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   117
        if ptr.is_null() {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   118
            return "".to_string()
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   119
        }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   120
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   121
        let buf = unsafe { CStr::from_ptr(ptr).to_bytes().to_vec() };
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   122
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   123
        String::from_utf8(buf).unwrap()
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   124
    }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   125
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   126
    /// Sets a new write directory.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   127
    /// This method will fail if the current write dir
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   128
    /// still has open files in it.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   129
    pub fn set_write_dir<P>(&self, write_dir: P) -> Result<()>
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   130
        where P: AsRef<OsStr>
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   131
    {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   132
        let write_dir = CStr::from_bytes_with_nul(write_dir.as_ref().to_str().unwrap().as_bytes()).unwrap();
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   133
        let ret = unsafe {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   134
            PHYSFS_setWriteDir(write_dir.as_ptr())
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   135
        };
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   136
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   137
        match ret {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   138
            0 => Err(util::physfs_error_as_io_error()),
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   139
            _ => Ok(())
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   140
        }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   141
    }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   142
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   143
    /// Creates a new dir relative to the write_dir.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   144
    pub fn mkdir(&self, dir_name: &str) -> Result<()> {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   145
        let c_dir_name = try!(CString::new(dir_name));
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   146
        let ret = unsafe {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   147
            PHYSFS_mkdir(c_dir_name.as_ptr())
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   148
        };
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   149
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   150
        match ret {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   151
            0 => Err(util::physfs_error_as_io_error()),
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   152
            _ => Ok(())
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   153
        }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   154
    }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   155
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   156
    /// Checks if given path exists
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   157
    pub fn exists(&self, path: &str) -> Result<()> {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   158
        let c_path = try!(CString::new(path));
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   159
        let ret = unsafe { PHYSFS_exists(c_path.as_ptr()) };
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   160
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   161
        match ret {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   162
            0 => Err(util::physfs_error_as_io_error()),
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   163
            _ => Ok(())
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   164
        }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   165
    }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   166
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   167
    /// Checks if given path is a directory
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   168
    pub fn is_directory(&self, path: &str) -> Result<()> {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   169
        let c_path = try!(CString::new(path));
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   170
        let ret = unsafe { PHYSFS_isDirectory(c_path.as_ptr()) };
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   171
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   172
        match ret {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   173
            0 => Err(util::physfs_error_as_io_error()),
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   174
            _ => Ok(())
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   175
        }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   176
    }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   177
}
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   178
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   179
impl Drop for PhysFSContext {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   180
    fn drop(&mut self) {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   181
        // decrement NUM_CONTEXTS
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   182
        unsafe {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   183
            NUM_CONTEXTS -= 1;
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   184
        }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   185
        // and de_init if there aren't any contexts left.
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   186
        if unsafe { NUM_CONTEXTS == 0 } {
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   187
            PhysFSContext::de_init();
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   188
        }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   189
    }
a1613788130d Adopt physfs-rs lib and adapt it to recent std library, remove global mutex
unc0rr
parents:
diff changeset
   190
}