I/O library#
Importing#
|#| 'stdio.nest' = io
Functions#
@can_read#
Synopsis:
[file: IOFile] @can_read -> Bool
Returns:
true if file can be read and false otherwise.
@can_seek#
Synopsis:
[file: IOFile] @can_seek -> Bool
Returns:
true if file can be sought and false otherwise.
@can_write#
Synopsis:
[file: IOFile] @can_write -> Bool
Returns:
true if file can be written and false otherwise.
@close#
Synopsis:
[file: IOFile] @close -> null
Description:
Closes a file. After a file is closed, you will not be able to do any other operation with it. If the file was already closed an error is thrown. When the program ends any file that is still open gets closed.
Arguments:
file: the file to close
@descriptor#
Synopsis:
[file: IOFile] @descriptor -> Int
Returns:
The file descriptor of a given file. If the file is closed or does not have a
descriptor -1 is returned and no error is thrown.
@encoding#
Synopsis:
[file: IOFile] @encoding -> Str
Returns:
The encoding of the file as a string. If the file is closed or in binary mode this function fails.
@file_size#
Synopsis:
[file: IOFile] @file_size -> Int
Returns:
The size of the file in bytes. Throws an error if the file cannot be sought or is closed.
@flush#
Synopsis:
[file: IOFile] @flush -> null
Description:
Flushes the output buffer of a file. An error is thrown if the file is closed or cannot be written.
@get_flags#
Synopsis:
[file: IOFile] @get_flags -> Str
Returns:
A 5-character string where each character represents a flag as follows:
rif the file is readablewif the file is writablebif the file is binarysif the file can be soughttif the file is a TTY
If file does not have a flag, that character is replaced by a hyphen (-).
Example:
|#| 'stdio.nest' = io
'a.txt' 'w' @io.open = f1
>>> (f1 @io.get_flags '\n' ><) --> '-w-s-'
f1 @io.close
'a.txt' 'r+' @io.open = f2
>>> (f2 @io.get_flags '\n' ><) --> 'rw-s-'
f2 @io.close
'a.txt' 'rb' @io.open = f3
>>> (f3 @io.get_flags '\n' ><) --> 'r-bs-'
f3 @io.close
@is_a_tty#
Synopsis:
[file: IOFile] @is_a_tty -> Bool
Returns:
true if file is a TTY and false otherwise.
@is_bin#
Synopsis:
[file: IOFile] @is_bin -> Bool
Returns:
true if file was opened in binary mode and false otherwise.
@open#
Synopsis:
[path: Str, mode: Str?, encoding: Str?, buf_size: Int?] @open -> IOFile
Description:
Opens a file. If the mode is null it is set to r. If encoding is null it
is set to utf8. If buf_size is null it is set to 512 bytes.
The file modes are:
| Mode | Description |
|---|---|
w |
write, destroying the contents |
wb |
write bytes, destroying the contents |
r |
read |
rb |
read bytes |
a |
append |
ab |
append bytes |
r+ |
read and write, keeping the contents |
rb+ or r+b |
read and write bytes, keeping the contents |
w+ |
read and write, destroying the contents |
wb+ or w+b |
read and write bytes, destroying the contents |
a+ |
read and append, keeping the contents |
ab+ or a+b |
read and append bytes, keeping the contents |
To see all available encodings check this page.
Arguments:
path: the path of the file to openmode: the mode in which it should be openedencoding: the encoding used to open the file, ifmodeis binary this argument must benullbuf_size: the size of the buffer of the file
Returns:
An IOFile object or null if the file was not found.
@println#
Synopsis:
[object: Any, flush: Bool?, file: IOFile?] @println -> null
Description:
Will print object like >>> followed by a newline. flush, if null,
defaults to false. file, if null, is stdout by default. If file is
closed or cannot be written an error is thrown.
Arguments:
object: the object to be printedflush: whether the file should be flushedfile: the file to write to
@read#
Synopsis:
[file: IOFile, size: Int?] @read -> Str
Description:
Reads a number of characters from a file opened in r, r+, w+ or a+ and
returns a Str object. If size is negative or null the whole file is read.
Warning
The file cannot be read entirely if it cannot be sought. If you try to
read a file that cannot be sought while omitting the size or giving it a
negative value an error is thrown.
Arguments:
file: the file to be readsize: the number or characters to read
Returns:
The content that it read as a string.
@read_bytes#
Synopsis:
[file: IOFile, size: Int?] @read_bytes -> Array.Byte
Description:
Reads a number of bytes from a file opened in rb, rb+, wb+ or ab+ and
returns an Array object. To convert the array to a string, use the
decode function in
stdsutil.nest. If size is negative or null the whole file is read.
Warning
The file cannot be read entirely if it cannot be sought. If you try to
read a file that cannot be sought while omitting the size or giving it a
negative value an error is thrown.
Arguments:
file: the file to be readsize: the number of bytes to read
Returns:
The content that it read as an array of Byte objects.
@seek#
Synopsis:
[file: IOFile, starting_position: Int?, offset: Int?] @seek -> Int
Description:
Moves the file position indicator from starting_position by offset bytes.
An error is thrown if the file is closed, cannot be sought or if the file
position indicator would go outside the file.
If starting_position is null, FROM_CUR will be used.
If offset is null, 0 will be used.
Arguments:
file: the file of which the file position indicator should be movedstarting_position: the position from which the offset is applied, this can be eitherFROM_START,FROM_CURorFROM_ENDoffset: the offset in bytes from the starting position, it can be negative
Returns:
The end position of the indicator in bytes from the start of the file. Calling
this function without starting_position and offset will simply return the
current position (like ftell in C).
@virtual_file#
Synopsis:
[binary: Bool?, buffer_size: Int?] @virtual_file -> IOFile
Creates a virtual IOFile object that works like a normal file but is not an
actual file.
Arguments:
binary: specifies if the file should usewriteandreadorwrite_bytesandread_bytes. If set tonullit is interpreted as false.buffer_sizespecifies the initial size of the file in bytes
Returns:
The newly created file.
@write#
Synopsis:
[file: IOFile, content: Any] @write -> Int
Description:
Writes to a file opened in w, a, r+, w+ or a+. content is casted
to a string before being written. If the file is closed an error is thrown.
Arguments:
file: the file to be writtencontent: the contents to be written
Returns:
The number of characters written.
Example:
|#| 'stdio.nest' = io
'example.txt' 'w' @io.open = f
f {1, 2, 3} @io.write
f @io.close
'example.txt' @io.open = f
f @io.read @io.println --> '{1, 2, 3}'
f @io.close
@write_bytes#
Synopsis:
[file: IOFile, content: Array|Vector.Byte] @write_bytes -> Int
Description:
Writes to a binary file opened in wb, ab, rb+, wb+ or ab+.
The second argument is an array or vector containing only Byte objects.
To create such vector from a string, use the
encode
function in stdsutil.nest.
Arguments:
file: the file to be writtencontent: the sequence of bytes to write
Returns:
The number of bytes written.
@_get_stdin#
Synopsis:
[] @_get_stdin -> IOFile
Returns:
The current input stream. If _set_stdin is never called this will return the
same object that STDIN points to.
@_get_stderr#
Synopsis:
[] @_get_stdin -> IOFile
Returns:
The current error stream. If _set_stderr is never called this will return the
same object that STDERR points to.
@_get_stdout#
Synopsis:
[] @_get_stdin -> IOFile
Returns:
The current output stream. If _set_stdout is never called this will return
the same object that STDOUT points to.
@_set_stdin#
Synopsis:
[file: IOFile] @_set_stdin -> null
Description:
Changes the standard input stream to file.
file must support reading and must not be already closed.
The STDIN constant will not reflect any changes and will always
point to the original input stream unless changed manually.
@_set_stderr#
Synopsis:
[file: IOFile] @_set_stderr -> null
Description:
Changes the standard error stream to file.
file must support writing and must not be already closed.
The STDERR constant will not reflect any changes and will
always point to the original error stream unless changed manually.
@_set_stdout#
Synopsis:
[file: IOFile] @_set_stdout -> null
Description:
Changes the standard output stream to file. file must support writing and
must not be already closed.
The STDOUT constant will not reflect any changes and will
always point to the original output stream unless changed manually.
Constants#
FROM_START#
Used for move_fptr, puts the file pointer at the start of the file. The same
as SEEK_SET in C.
FROM_SET#
The same as FROM_START with a more familiar name.
FROM_CUR#
Used for move_fptr, does not move the file pointer. The same as SEEK_CUR in
C.
FROM_END#
Used for move_fptr, puts the file pointer at the end of the file. The same as
SEEK_SET in C.
STDIN#
File object of the standard input stream, changing this constant does not change
the stream used by <<<, use _set_stdin instead.
STDERR#
File object of the standard error stream, changing this constant does not change
the actual error stream, use _set_stderr instead.
STDOUT#
File object of the standard output stream, changing this constant does not
change the stream used by >>>, use _set_stdout
instead.