MTA:Eir/FileSystem/file/isWritable: 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 writable. If a stream is not writable, then all write operations should result in nil operations (they will return zero bytes ...")
 
mNo edit summary
 
Line 4: Line 4:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
boolean file.isWritable ()
boolean file:isWritable ()
</syntaxhighlight>
</syntaxhighlight>


Line 16: Line 16:
local function writeHeader( theFile, headerInfo )
local function writeHeader( theFile, headerInfo )
     -- Check whether we can write things into the file.
     -- Check whether we can write things into the file.
     if not ( theFile.isWritable() ) then
     if not ( theFile:isWritable() ) then
         error( "fatal error: stream is not writable" );
         error( "fatal error: stream is not writable" );
     end
     end


     -- Write a generic header structure.
     -- Write a generic header structure.
     theFile.writeUInt( headerInfo.chunkSize );
     theFile:writeUInt( headerInfo.chunkSize );
     theFile.writeFloat( headerInfo.version );
     theFile:writeFloat( headerInfo.version );
     theFile.writeBoolean( headerInfo.isRaw );
     theFile:writeBoolean( headerInfo.isRaw );
end
end
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
{{:MTA:Eir/FileSystem/file/functions}}
{{:MTA:Eir/FileSystem/file/functions}}

Latest revision as of 23:50, 16 January 2022

This function returns whether a stream is writable. If a stream is not writable, then all write operations should result in nil operations (they will return zero bytes written). This state should be immutable across the lifetime of a file/stream class.

Syntax

boolean file:isWritable ()

Returns

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

Example

Click to collapse [-]
Client

This snippet implements a file function that makes sure the file it has been passed to for writing actually supports writing.

local function writeHeader( theFile, headerInfo )
    -- Check whether we can write things into the file.
    if not ( theFile:isWritable() ) then
        error( "fatal error: stream is not writable" );
    end

    -- Write a generic header structure.
    theFile:writeUInt( headerInfo.chunkSize );
    theFile:writeFloat( headerInfo.version );
    theFile:writeBoolean( headerInfo.isRaw );
end

FileSystem File Functions