User:Rapidgame7/iodocs

From SRB2 Wiki
Jump to navigation Jump to search

Feel free to edit this page

static const luaL_Reg iolib[] = {
  {"close", io_close},
  {"open", io_open},
  {"openlocal", io_openlocal},
  {"tmpfile", io_tmpfile},
  {"type", io_type},
  {NULL, NULL}
};


static const luaL_Reg flib[] = {
  {"close", io_close},
  {"flush", f_flush},
  {"lines", f_lines},
  {"read", f_read},
  {"seek", f_seek},
  {"setvbuf", f_setvbuf},
  {"write", f_write},
  {"__gc", io_gc},
  {"__tostring", io_tostring},
  {NULL, NULL}
};

For reference ^

FOR /USERDATA

file*

This userdata type represents a file handle, obtained by calling io.openlocal.

General
Accessibility Read-only
Allows custom variables No
file* does not have any attributes.

In the below examples, file refers to a file handle.

file* methods
Method Return value(s) Description
file:close() nil Closes the file.
file:flush() nil Saves any written data to file.
file:lines() function Returns an iterator function that, each time it is called, returns a new line from the file as a string. Therefore, the construction
for line in file:lines() do
    -- body
end

will iterate over all lines of the file.

file:read([* format]) various Reads the file file, according to the given formats, which specify what to read. For each format, the function returns a string (or a number) with the characters read, or nil if it cannot read data with the specified format. When called without formats, it uses a default format that reads the entire next line (see below).

The available formats are:

  • "*n": reads a number; this is the only format that returns a number instead of a string.
  • "*a": reads the whole file, starting at the current position. On end of file, it returns the empty string.
  • "*l": reads the next line (skipping the end of line), returning nil on end of file. This is the default format.
  • number: reads a string with up to this number of characters, returning nil on end of file. If number is zero, it reads nothing and returns an empty string, or nil on end of file.
file:seek([string whence], [int offset]) string or nil Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string whence, as follows:
  • "set": base is position 0 (beginning of the file);
  • "cur": base is current position;
  • "end": base is end of file;

In case of success, function seek returns the final file position, measured in bytes from the beginning of the file. If this function fails, it returns nil, plus a string describing the error.

The default value for whence is "cur", and for offset is 0. Therefore, the call file:seek() returns the current file position, without changing it; the call file:seek("set") sets the position to the beginning of the file (and returns 0); and the call file:seek("end") sets the position to the end of the file, and returns its size.

file:setvbuf(string mode, [int size]) TODO Sets the buffering mode for an output file. There are three available modes:
  • "no": no buffering; the result of any output operation appears immediately.
  • "full": full buffering; output operation is performed only when the buffer is full (or when you explicitly flush the file (see io.flush)).
  • "line": line buffering; output is buffered until a newline is output.

For the last two cases, size specifies the size of the buffer, in bytes. The default is an appropriate size.

file:write(...) ? Writes the value of each of its arguments to the file. The arguments must be strings or numbers. To write other values, use tostring or string.format before write.

If the file size surpasses 1 megabyte after a write operation, BLua will raise an error and discard any pending changes.

FOR /FUNCTIONS

I/O library

(lua.org documentation)

Function Return value(s) Description
io.openlocal(string filename, [string mode]) file or nil, string or nil This function opens a file, in the mode specified in the string mode. It returns a new file handle, or, in case of errors, nil plus an error message.

The mode string can be any of the following:

  • "r": read mode (the default);
  • "w": write mode;
  • "a": append mode;
  • "r+": update mode, all previous data is preserved;
  • "w+": update mode, all previous data is erased;
  • "a+": append update mode, previous data is preserved, writing is only allowed at the end of file.

The mode string can also have a 'b' at the end, which is needed in some systems to open the file in binary mode.

This function enforces the following:

  • Files must be located within the luafiles folder in the SRB2 root directory.
  • Files must have one of the following extensions:
    • .bmp
    • .cfg
    • .csv
    • .dat
    • .png
    • .sav2
    • .txt
  • Files must weigh 1 megabyte or lower, if they exist.

Trying to open or create a file that doesn't follow these restrictions will raise an error.

io.open(string filename, [string mode], function callback) nil * Callback function format: function(file file, string filename)

This function is similar to io.openlocal, except:

  • This function sends the specified file to all players in a netgame.
  • It takes a callback function, which is executed immediately as soon as all players have downloaded the file.
  • This function does not return a value.
  • Follows the same restrictions, except the path is now constricted to the luafiles/client folder.

This function is non-blocking, which means that BLua will not wait for all players to download the file and the callback function to be run. Instead, the script will continue to run as normal. Keep this in mind when setting variables with values found in the file, as those variables will not be updated until an indeterminate amount of time in the future.

io.close(file file) TODO Closes the file handle file. Equivalent to file:close()
To do
io.close can be called with no arguments. What does it do?
io.tmpfile() file Returns a handle for a temporary file. This file is opened in update mode and it is automatically removed when the program ends.
io.type(* obj) string or nil Checks whether obj is a valid file handle.

Returns the string "file" if obj is an open file handle, "closed file" if obj is a closed file handle, or nil if obj is not a file handle.