MTA:Eir/FileSystem/file/isReadable: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ This function returns whether a stream is readable. If a stream is not readable, then all read operations should result in nil operations (they will return zero bytes r...")
 
No edit summary
 
Line 4: Line 4:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
boolean file.isReadable ()
boolean file:isReadable ()
</syntaxhighlight>
</syntaxhighlight>


Line 16: Line 16:
local function readHeader( theFile )
local function readHeader( theFile )
     -- Check whether we can read anything from the file.
     -- Check whether we can read anything from the file.
     if not ( theFile.isReadable() ) then
     if not ( theFile:isReadable() ) then
         error( "fatal error: file cannot be read from" );
         error( "fatal error: file cannot be read from" );
     end
     end
Line 22: Line 22:
     -- Read the generic header back into the engine.
     -- Read the generic header back into the engine.
     local headerInfo = {
     local headerInfo = {
         chunkSize = theFile.readUInt(),
         chunkSize = theFile:readUInt(),
         version = theFile.readFloat(),
         version = theFile:readFloat(),
         isRaw = theFile.readBoolean()
         isRaw = theFile:readBoolean()
     };
     };



Latest revision as of 23:50, 16 January 2022

This function returns whether a stream is readable. If a stream is not readable, then all read operations should result in nil operations (they will return zero bytes read or false if value reading). This state should be immutable across the lifetime of a file/stream class.

Syntax

boolean file:isReadable ()

Returns

Returns true if the file/stream is readable, false otherwise.

Example

Click to collapse [-]
Client

This snippet is the counterpart of the file.isWritable example. It attempts to read the header from the file, but throws an exception if it cannot be read from.

local function readHeader( theFile )
    -- Check whether we can read anything from the file.
    if not ( theFile:isReadable() ) then
        error( "fatal error: file cannot be read from" );
    end

    -- Read the generic header back into the engine.
    local headerInfo = {
        chunkSize = theFile:readUInt(),
        version = theFile:readFloat(),
        isRaw = theFile:readBoolean()
    };

    -- Return the header information.
    return headerInfo;
end

FileSystem File Functions