No matching definitions.

tur/io

stdlib/io.tur

FileSystem capability implementation using libc functions.

Since: Phase 16

defn

file-size

(file-size [f])

return the byte size of an already-open file.

fa FILE* cast to int64_t (as returned by fopen).

The file size in bytes as an int.

(file-size my-file-ptr)  ; => 1024
defn

Real-FileSystem

(Real-FileSystem)

create a FileSystem capability backed by libc.

A heap-allocated FileSystem struct whose function pointers use fopen/fread/fwrite/remove/opendir/readdir for all operations. Free with Real-FileSystem-free when done.

(let [fs (Real-FileSystem)] ...)

Since: Phase 16

defn

Real-FileSystem-free

(Real-FileSystem-free [fs])

release a FileSystem capability created by Real-FileSystem.

fsthe FileSystem pointer returned by Real-FileSystem.

Since: Phase 16

defn

read-file

(read-file [path])

read an entire file into a newly allocated buffer.

paththe filesystem path to open (:cstr).

A malloc'd buffer containing the file contents with a '\0' terminator, or NULL on any error. The caller is responsible for calling free.

(let [buf (read-file "/etc/hostname")] ...)

Since: Phase 16

defn

write-file

(write-file [path data len])

write a byte buffer to a file, overwriting any existing content.

paththe destination filesystem path (:cstr).
datapointer to the data to write.
lennumber of bytes to write.

0 on success, -1 on error.

(write-file "/tmp/out.bin" buf 256)  ; => 0

Since: Phase 16

defn

file-exists?

(file-exists? [path])

test whether a file is accessible for reading.

paththe filesystem path to check (:cstr).

1 if the file can be opened for reading, 0 otherwise.

(file-exists? "/etc/hosts")  ; => 1

Since: Phase 16

defn

list-dir

(list-dir [path])

list all non-hidden entries in a directory.

paththe directory path to list (:cstr).

A NULL-terminated array of strdup'd entry name strings, or NULL on error. The caller must free each string and then the array itself (or use free-dir-listing).

(let [entries (list-dir "/tmp")] ...)

Since: Phase 16

defn

free-dir-listing

(free-dir-listing [entries])

free a directory listing returned by list-dir.

entriesthe NULL-terminated string array returned by list-dir.

Since: Phase 16

defstruct linear

FileHandle

(defstruct FileHandle :linear [ptr :ptr<void>])

a linear (exactly-once) wrapper around a FILE* pointer.

Since: LT4

defn

file-open

(file-open [path mode] :FileHandle)

open a file and return a move-only FileHandle.

pathfilesystem path to open (:cstr).
modefopen mode string, e.g. "rb" or "wb" (:cstr).

A FileHandle wrapping the opened FILE*, or a FileHandle with a NULL ptr on error. Check with file-handle-ok? before using.

(let [fh (file-open "/etc/hostname" "rb")] ...)

Since: LT4

defn

file-handle-ok?

(file-handle-ok? [fh : FileHandle] :bool)

return true if the FileHandle is valid (non-NULL).

fha FileHandle (by value).

true if the underlying FILE* is non-NULL, false on open failure.

(file-handle-ok? (file-open "/etc/hostname" "rb"))  ; => true

Since: LT4

defn

file-read

(file-read [fh : FileHandle buf len] :int)

read up to len bytes from fh into buf.

fhan open FileHandle (by value; does not consume the handle).
bufdestination buffer as ptr<void>.
lenmaximum number of bytes to read.

Number of bytes actually read (int).

(file-read fh buf 1024)  ; => 512

Since: LT4

defn

file-close

(file-close [fh : FileHandle] :int)

close a FileHandle and release the underlying FILE*.

fha FileHandle to close (by value; handle is consumed).

0 on success, non-zero on error (mirrors fclose return value).

(file-close fh)  ; => 0

Since: LT4