Skip to content

stdfiles

Anssi Halmeaho edited this page May 2, 2020 · 1 revision

stdfiles

Provides file handling services.

Opaque type: file

Opaque type representing file instance.

Opaque type: fileinfo

Opaque type containing file information.

File open modes

Following values represent file open modes:

  • stdfiles.w: write mode
  • stdfiles.r: read mode
  • stdfiles.a: append mode

These can be summed:

  • plus(stdfiles.w stdfiles.r) : read-write mode
  • stdfiles.r : read-only mode
  • stdfiles.w : write only mode

Procedures

create

Creates file. File name (and path) is given as argument.

type: procedure

Format:

call(stdfiles.create <filename-string>) -> <opaque:file> or <error-string>

Return value:

  • if success, opaque:file
  • if failure, error string

open

Opens file.

Arguments:

  1. file name (and path)
  2. open mode
    • stdfiles.w (write-only)
    • stdfiles.r (read-only)
    • plus(stdfiles.w stdfiles.r) (read-write)
    • stdfiles.a (append -> writes are appending to file end instead of overwriting)

type: procedure

Format:

call(stdfiles.open <filename-string> <open-mode-int>) -> <opaque:file> or <error-string>

Return value:

  • if success, opaque:file
  • if failure, error string

write

Writes bytearray (defined in stdbytes) to file.

type: procedure

Format:

call(stdfiles.write <opaque:file> <data-to-write-bytearray>) -> <number-of-bytes-written-int> or <error-string>

Return value:

  • if success, number of bytes written (int)
  • if failure, error string

write-at

Writes bytearray (defined in stdbytes) to file starting from given offset (int) in file.

type: procedure

Format:

call(stdfiles.write-at <opaque:file> <data-to-write-bytearray> <offset-int>) -> <number-of-bytes-written-int> or <error-string>

Return value:

  • if success, number of bytes written (int)
  • if failure, error string

writeln

Writes string to file and puts newline in the end.

type: procedure

Format:

call(stdfiles.writeln <opaque:file> <data-to-write-string>) -> <number-of-bytes-written-int> or <error-string>

Return value:

  • if success, number of bytes written (int)
  • if failure, error string

read

Reads file. Maximum count of bytes to be read is given as argument (int).

type: procedure

Format:

call(stdfiles.read <opaque:file> <max-bytes-int>) -> <list>

Return value: list, conatins following values

  1. bool value which is true if EOF was reached, false otherwise
  2. bytearray containing data read

read-at

Reads file. Maximum count of bytes to be read is given as argument (int). Read starts from given offset (int) in file.

type: procedure

Format:

call(stdfiles.read <opaque:file> <max-bytes-int> <offset-int>) -> <list>

Return value: list, conatins following values

  1. bool value which is true if EOF was reached, false otherwise
  2. bytearray containing data read

read-all

Reads whole file content. Content returned as bytearray.

type: procedure

Format:

call(stdfiles.read-all <opaque:file>) -> bytearray (file content)

Return value: bytearray (whole file content)

readlines

Reads whole file content (as text). Content returned as list of strings (splitted by newlines).

type: procedure

Format:

call(stdfiles.readlines <opaque:file>) -> list (of strings) or <error-string>

Return value:

  • if success: list of strings (splitted by newlines in file)
  • if failure: error string

seek

Sets the offset (2nd argument) for the next read or write on file to offset, interpreted according to whence (3rd argument). Whence 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end.

type: procedure

Arguments:

  1. opaque: file
  2. offset (int)
  3. whence (int)

Format:

call(stdfiles.seek <opaque:file> <offset> <whence>) -> <new-offset-int> or <error-string>

Return value:

  • if success: new offset (int)
  • if failure: error string

remove

Removes file. Name (and path) is given as arguemnt (string).

type: procedure

Format:

call(stdfiles.remove <filename-string>) -> <string>

Return value: string

  • if success: ''
  • if failure: error description (string)

rename

Renames file. Old name is 1st argument (string), new name is 2nd argument (string).

type: procedure

Format:

call(stdfiles.rename <old-name-string> <new-name-string>) -> string

Return value: string

  • if success: ''
  • if failure: error description (string)

close

Closes file.

type: procedure

Format:

call(stdfiles.close <opaque:file>) -> true or <error-string>

Return value: true (bool) or string (describing error)

  • if success: true
  • if failure: error description (string)

read-dir

Returns file informations of certain directory (as map: filename -> file-info).

Format:

call(stdfiles.read-dir <directory-string>) -> map or <error-string>

Return value: map or string (describing error)

Map contains file names as keys and file infos as values (opaque:fileinfo)

finfo-map

Creates file information from opaque:fileinfo (given as argument) to map.

type: procedure

Format:

call(stdfiles.finfo-map <opaque:fileinfo>) -> map or <error-string>

Return value: map or string (describing error)

Map contains follwoign information:

Key Value
'name' file name (string)
'size' length in bytes (int)
'mode' file mode bits (string), example: '-rw-rw-rw-'
'modtime' modification time (string)
'is-dir' bool, true if is directory, false otherwise

cwd

Returns current working directory as string.

type: procedure

Format:

call(stdfiles.cwd) -> string

Return value: string

chdir

Change current working directory.

type: procedure

Format:

call(stdfiles.chdir <new-directory-string>) -> string

Return value: string

  • if success: ''
  • if failure: error description (string)

mkdir

Create new directory. Directory name (and path) is given as argument.

type: procedure

Format:

call(stdfiles.mkdir) -> <string>

Return value: string

  • if success: ''
  • if failure: error description (string)
Clone this wiki locally