UXFileIO
UXFileIO reads a whole file into a Data
and writes one back:
Data* bytes = UXFileIO.read(path); // null if it cannot be readbool ok = UXFileIO.write(path, bytes); // false if it did not happenIt is libc’s stdio underneath, which every native target has: macOS,
Linux, Windows, iOS and Android inside their sandboxes, and GEM through its
own libc. The standard library’s Files.xc exists only on the host
architectures, so a document app built on it could not save on a device or
on GEM. UXFileIO can.
Saving is atomic
Section titled “Saving is atomic”write puts the bytes in path + .uxtmp first. It renames that over the
original only when every byte is written and the file is closed.
A full disk, a missing folder or a failed write therefore leaves the previous document exactly as it was, instead of truncating it. The temporary file is removed on failure.
The web
Section titled “The web”A browser has no file system, so on a page UXKit keeps one: a store of
files by path, held where the app runs. write puts the file in the store
and hands it to the browser as a download, so saving a document downloads
it. read reads from the store, which is where a file the user opens with
UXOpenPanel is put. Nothing in the
store outlives the page.
Under node, which runs the wasm32 tests, the same calls read and write real files.
The phones
Section titled “The phones”On iOS and Android, UXSavePanel asks
where a document goes before anything is written, and hands back a staging
path in the app’s own space. A write to that path is written as above, then
copied on to the chosen document. write returns true only if the copy
arrives too.
Topics
Section titled “Topics”static Data* read(u8* path)The whole file, or null if it does not exist or cannot be read. An empty
file reads as an empty Data, not as null.
static bool write(u8* path, Data* d)Writes d to path, replacing it only if every byte made it to disk.
See also
Section titled “See also”UXSavePanelandUXOpenPanel, for asking the user which file