sdl3/
filesystem.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
use libc::{c_char, c_void};
use std::error;
use std::ffi::{CStr, CString, NulError};
use std::fmt;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use std::ptr;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use sys::filesystem::SDL_PathInfo;

use crate::get_error;
use crate::sys;
use crate::Error;

#[derive(Debug, Clone)]
pub enum FileSystemError {
    InvalidPathError(PathBuf),
    NulError(NulError),
    SdlError(Error),
}

/// Turn a AsRef<Path> into a CString so it can be passed to C
macro_rules! path_cstring {
    ($pathref:ident) => {
        let Some($pathref) = $pathref.as_ref().to_str() else {
            return Err(FileSystemError::InvalidPathError(
                $pathref.as_ref().to_owned(),
            ));
        };

        let Ok($pathref) = CString::new($pathref) else {
            return Err(FileSystemError::InvalidPathError(PathBuf::from($pathref)));
        };
    };
}

// Turn a CString into a Path for ease of use
macro_rules! cstring_path {
    ($path:ident, $error:expr) => {
        let Ok($path) = CStr::from_ptr($path).to_str() else {
            $error
        };
        let $path = Path::new($path);
    };
}

#[doc(alias = "SDL_CopyFile")]
pub fn copy_file(
    old_path: impl AsRef<Path>,
    new_path: impl AsRef<Path>,
) -> Result<(), FileSystemError> {
    path_cstring!(old_path);
    path_cstring!(new_path);
    unsafe {
        if !sys::filesystem::SDL_CopyFile(old_path.as_ptr(), new_path.as_ptr()) {
            return Err(FileSystemError::SdlError(get_error()));
        }
    }
    Ok(())
}

#[doc(alias = "SDL_CreateDirectory")]
pub fn create_directory(path: impl AsRef<Path>) -> Result<(), FileSystemError> {
    path_cstring!(path);
    unsafe {
        if !sys::filesystem::SDL_CreateDirectory(path.as_ptr()) {
            return Err(FileSystemError::SdlError(get_error()));
        }
    }
    Ok(())
}

pub use sys::filesystem::SDL_EnumerationResult as EnumerationResult;

pub type EnumerateCallback = fn(&Path, &Path) -> EnumerationResult;

unsafe extern "C" fn c_enumerate_directory(
    userdata: *mut c_void,
    dirname: *const c_char,
    fname: *const c_char,
) -> EnumerationResult {
    let callback: EnumerateCallback = std::mem::transmute(userdata);

    cstring_path!(dirname, return EnumerationResult::FAILURE);
    cstring_path!(fname, return EnumerationResult::FAILURE);

    callback(dirname, fname)
}

#[doc(alias = "SDL_EnumerateDirectory")]
pub fn enumerate_directory(
    path: impl AsRef<Path>,
    callback: EnumerateCallback,
) -> Result<(), FileSystemError> {
    path_cstring!(path);
    unsafe {
        if !sys::filesystem::SDL_EnumerateDirectory(
            path.as_ptr(),
            Some(c_enumerate_directory),
            callback as *mut c_void,
        ) {
            return Err(FileSystemError::SdlError(get_error()));
        }
    }
    Ok(())
}

#[doc(alias = "SDL_GetBasePath")]
pub fn get_base_path() -> Result<&'static Path, FileSystemError> {
    unsafe {
        let path = sys::filesystem::SDL_GetBasePath();
        cstring_path!(path, return Err(FileSystemError::SdlError(get_error())));
        Ok(path)
    }
}

//TODO: Implement SDL_GetCurrentDirectory when sdl3-sys is updated to SDL 3.2.0.

pub use sys::filesystem::SDL_PathType as PathType;

pub struct PathInfo {
    internal: SDL_PathInfo,
}

impl PathInfo {
    fn path_type(&self) -> PathType {
        self.internal.r#type as PathType
    }

    fn size(&self) -> usize {
        self.internal.size as usize
    }

    fn create_time(&self) -> SystemTime {
        UNIX_EPOCH + Duration::from_nanos(self.internal.create_time as u64)
    }

    fn modify_time(&self) -> SystemTime {
        UNIX_EPOCH + Duration::from_nanos(self.internal.modify_time as u64)
    }

    fn access_time(&self) -> SystemTime {
        UNIX_EPOCH + Duration::from_nanos(self.internal.access_time as u64)
    }
}

impl fmt::Debug for PathInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PathInfo")
            .field(
                "path_type",
                match self.path_type() {
                    PathType::DIRECTORY => &"Directory",
                    PathType::FILE => &"File",
                    PathType::NONE => &"None",
                    _ => &"Other",
                },
            )
            .field("size", &self.size())
            .field("create_time", &self.create_time())
            .field("modify_time", &self.modify_time())
            .field("access_time", &self.access_time())
            .finish()
    }
}

#[doc(alias = "SDL_GetPathInfo")]
pub fn get_path_info(path: impl AsRef<Path>) -> Result<PathInfo, FileSystemError> {
    let mut info = SDL_PathInfo {
        r#type: PathType::NONE,
        size: 0,
        create_time: 0,
        modify_time: 0,
        access_time: 0,
    };
    path_cstring!(path);

    unsafe {
        if !sys::filesystem::SDL_GetPathInfo(path.as_ptr(), &mut info as *mut SDL_PathInfo) {
            return Err(FileSystemError::SdlError(get_error()));
        }
    }

    Ok(PathInfo { internal: info })
}

#[derive(Debug, Clone)]
pub enum PrefPathError {
    InvalidOrganizationName(NulError),
    InvalidApplicationName(NulError),
    SdlError(Error),
}

impl fmt::Display for PrefPathError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use self::PrefPathError::*;

        match *self {
            InvalidOrganizationName(ref e) => write!(f, "Invalid organization name: {}", e),
            InvalidApplicationName(ref e) => write!(f, "Invalid application name: {}", e),
            SdlError(ref e) => write!(f, "SDL error: {}", e),
        }
    }
}

impl error::Error for PrefPathError {
    fn description(&self) -> &str {
        use self::PrefPathError::*;

        match *self {
            InvalidOrganizationName(_) => "invalid organization name",
            InvalidApplicationName(_) => "invalid application name",
            SdlError(ref e) => &e.0,
        }
    }
}

/// Return the preferred directory for the application to write files on this
/// system, based on the given organization and application name.
#[doc(alias = "SDL_GetPrefPath")]
pub fn get_pref_path(org_name: &str, app_name: &str) -> Result<PathBuf, PrefPathError> {
    let org = match CString::new(org_name) {
        Ok(s) => s,
        Err(err) => return Err(PrefPathError::InvalidOrganizationName(err)),
    };
    let app = match CString::new(app_name) {
        Ok(s) => s,
        Err(err) => return Err(PrefPathError::InvalidApplicationName(err)),
    };

    let path = unsafe {
        let buf = sys::filesystem::SDL_GetPrefPath(
            org.as_ptr() as *const c_char,
            app.as_ptr() as *const c_char,
        );
        let path = PathBuf::from(CStr::from_ptr(buf).to_str().unwrap());
        sys::stdinc::SDL_free(buf as *mut c_void);
        path
    };

    if path.as_os_str().is_empty() {
        Err(PrefPathError::SdlError(get_error()))
    } else {
        Ok(path)
    }
}

pub use sys::filesystem::SDL_Folder as Folder;

#[doc(alias = "SDL_GetUserFolder")]
pub fn get_user_folder(folder: Folder) -> Result<&'static Path, FileSystemError> {
    unsafe {
        let path = sys::filesystem::SDL_GetUserFolder(folder);
        cstring_path!(path, return Err(FileSystemError::SdlError(get_error())));
        Ok(path)
    }
}

bitflags! {
    pub struct GlobFlags: sys::filesystem::SDL_GlobFlags {
        const NONE = 0;
        const CASEINSENSITIVE = sys::filesystem::SDL_GLOB_CASEINSENSITIVE;
    }
}

pub struct GlobResultsIter<'a> {
    results: &'a GlobResults<'a>,
    index: isize,
}

impl<'a> Iterator for GlobResultsIter<'a> {
    type Item = &'a Path;
    fn next(&mut self) -> Option<Self::Item> {
        let current = self.results.get(self.index);
        self.index += 1;
        current
    }
}

pub struct GlobResults<'a> {
    internal: *mut *mut c_char,
    count: isize,
    phantom: PhantomData<&'a *mut *mut c_char>,
}

impl GlobResults<'_> {
    fn new(internal: *mut *mut c_char, count: isize) -> Self {
        Self {
            internal,
            count,
            phantom: PhantomData,
        }
    }

    fn len(&self) -> usize {
        self.count as usize
    }

    fn get<I>(&self, index: I) -> Option<&Path>
    where
        I: Into<isize>,
    {
        let index = index.into();
        if index >= self.count {
            return None;
        }
        unsafe {
            let path = *self.internal.offset(index);
            cstring_path!(path, return None);
            Some(path)
        }
    }
}

impl<'a> IntoIterator for &'a GlobResults<'a> {
    type Item = &'a Path;
    type IntoIter = GlobResultsIter<'a>;
    fn into_iter(self) -> Self::IntoIter {
        Self::IntoIter {
            results: self,
            index: 0,
        }
    }
}

impl Drop for GlobResults<'_> {
    fn drop(&mut self) {
        unsafe {
            sys::stdinc::SDL_free(self.internal as *mut c_void);
        }
    }
}

#[doc(alias = "SDL_GlobDirectory")]
pub fn glob_directory(
    path: impl AsRef<Path>,
    pattern: Option<&str>,
    flags: GlobFlags,
) -> Result<GlobResults, FileSystemError> {
    path_cstring!(path);
    let pattern = match pattern {
        Some(pattern) => match CString::new(pattern) {
            Ok(pattern) => Some(pattern),
            Err(error) => return Err(FileSystemError::NulError(error)),
        },
        None => None,
    };
    let pattern_ptr = pattern.as_ref().map_or(ptr::null(), |pat| pat.as_ptr());
    let mut count = 0;

    let results = unsafe {
        let paths = sys::filesystem::SDL_GlobDirectory(
            path.as_ptr(),
            pattern_ptr,
            flags.bits(),
            &mut count as *mut i32,
        );
        if paths.is_null() {
            return Err(FileSystemError::SdlError(get_error()));
        }
        GlobResults::new(paths, count as isize)
    };
    Ok(results)
}

#[doc(alias = "SDL_RemovePath")]
pub fn remove_path(path: impl AsRef<Path>) -> Result<(), FileSystemError> {
    path_cstring!(path);
    unsafe {
        if !sys::filesystem::SDL_RemovePath(path.as_ptr()) {
            return Err(FileSystemError::SdlError(get_error()));
        }
    }
    Ok(())
}

#[doc(alias = "SDL_RenamePath")]
pub fn rename_path(
    old_path: impl AsRef<Path>,
    new_path: impl AsRef<Path>,
) -> Result<(), FileSystemError> {
    path_cstring!(old_path);
    path_cstring!(new_path);

    unsafe {
        if !sys::filesystem::SDL_RenamePath(old_path.as_ptr(), new_path.as_ptr()) {
            return Err(FileSystemError::SdlError(get_error()));
        }
    }

    Ok(())
}