Tightened the source code and README documentations in the library.

This commit is contained in:
2026-09-18 12:45:39 +02:00
parent c9f887bacb
commit 9ae10590cc
97 changed files with 854 additions and 1142 deletions
+11 -14
View File
@@ -1,24 +1,22 @@
internal import CPlaydate
/// The cached `playdate->file` C API table.
/// Cached `playdate->file` table.
var fileAPI: UnsafePointer<playdate_file> { Playdate.fileAPI.unsafelyUnwrapped }
/// The most recent file system error as a thrown error.
/// The most recent file error, with the OS's description.
func lastFileError() -> PlaydateError {
PlaydateError(cString: fileAPI.pointee.geterr.unsafelyUnwrapped())
}
/// The file API: access to the game's Data directory and pdx contents.
///
/// Paths are relative to the game's Data directory (read/write) or the
/// game's pdx (read-only), depending on the mode used to open them.
/// The file API. Paths are relative to the Data directory (writable) or the pdx (read-only).
/// Every throwing API throws `PlaydateError` with the OS's description on failure.
public enum File {}
extension File {
// MARK: - Directory operations
/// Calls `each` with the name of every file in `path`. Subdirectory names
/// end in a slash. Throws if the directory does not exist.
/// Calls `each` with each entry name in `path`, non-recursively; directories end in `/`.
/// Skips `.`-prefixed names unless `showHidden`. Throws if `path` can't be opened.
public static func listFiles(at path: String, showHidden: Bool = false,
_ each: (String) -> Void) throws(PlaydateError) {
let result = withoutActuallyEscaping(each) { each in
@@ -36,7 +34,7 @@ extension File {
if result != 0 { throw lastFileError() }
}
/// Information about the file or directory at `path`.
/// Information about the file or directory at `path`; throws if it is missing.
public static func stat(_ path: String) throws(PlaydateError) -> Stat {
var stat = FileStat()
let result = path.withCString { fileAPI.pointee.stat.unsafelyUnwrapped($0, &stat) }
@@ -49,14 +47,13 @@ extension File {
hour: UInt8(stat.m_hour), minute: UInt8(stat.m_minute), second: UInt8(stat.m_second)))
}
/// Creates a directory (and intermediate directories) in the Data directory.
/// Creates directory `path` in the Data directory; does not create intermediate ones.
public static func mkdir(_ path: String) throws(PlaydateError) {
let result = path.withCString { fileAPI.pointee.mkdir.unsafelyUnwrapped($0) }
if result != 0 { throw lastFileError() }
}
/// Deletes the file or directory at `path`. Directories require
/// `recursive` to be deleted with their contents.
/// Deletes the file at `path`; with `recursive`, a directory and its contents.
public static func unlink(_ path: String, recursive: Bool = false) throws(PlaydateError) {
let result = path.withCString {
fileAPI.pointee.unlink.unsafelyUnwrapped($0, recursive ? 1 : 0)
@@ -64,8 +61,8 @@ extension File {
if result != 0 { throw lastFileError() }
}
/// Renames (moves) a file in the Data directory, overwriting any existing
/// file at the destination.
/// Moves `from` to `to` in the Data directory, overwriting `to`; does not create
/// intermediate directories.
public static func rename(from: String, to: String) throws(PlaydateError) {
let result = from.withCString { cFrom in
to.withCString { cTo in