MTA:Eir/FileSystem/file/readShort: Difference between revisions
Jump to navigation
Jump to search
(Created page with "__NOTOC__ This function attempts to read a short (native type) from a file and returns it. The amount of bytes read should be two. ==Syntax== <syntaxhighlight lang="lua"> short file.readShort ...") |
mNo edit summary |
||
Line 4: | Line 4: | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
short file | short file:readShort () | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 16: | Line 16: | ||
local function writeBuffers( outFile, buffers ) | local function writeBuffers( outFile, buffers ) | ||
-- Write header information. | -- Write header information. | ||
outFile | outFile:writeInt( #buffers ); -- number of buffers | ||
-- Write the buffers. | -- Write the buffers. | ||
Line 23: | Line 23: | ||
-- Write header of buffer. | -- Write header of buffer. | ||
outFile | outFile:writeShort( #bufferContent ); -- length of buffer | ||
-- Write the content | -- Write the content | ||
outFile | outFile:write( bufferContent ); | ||
end | end | ||
end | end | ||
Line 34: | Line 34: | ||
-- Read header information. | -- Read header information. | ||
local numBuffers = inFile | local numBuffers = inFile:readInt(); | ||
-- Make sure we read the int correctly. | -- Make sure we read the int correctly. | ||
Line 43: | Line 43: | ||
while ( n <= numBuffers ) do | while ( n <= numBuffers ) do | ||
-- Read buffer header. | -- Read buffer header. | ||
local bufferSize = inFile | local bufferSize = inFile:readShort(); | ||
-- We always have to add the amount of buffers specified by the header. | -- We always have to add the amount of buffers specified by the header. | ||
Line 51: | Line 51: | ||
if ( bufferSize ) then | if ( bufferSize ) then | ||
-- Read the buffer. | -- Read the buffer. | ||
buffer = inFile | buffer = inFile:read( bufferSize ); | ||
end | end | ||
Line 73: | Line 73: | ||
-- Now retrieve them again. | -- Now retrieve them again. | ||
theFile | theFile:seek( 0, "set" ); | ||
myBuffers = readBuffers( theFile ); | myBuffers = readBuffers( theFile ); |
Latest revision as of 23:35, 16 January 2022
This function attempts to read a short (native type) from a file and returns it. The amount of bytes read should be two.
Syntax
short file:readShort ()
Returns
Returns the short integer if it was successfully read from the file, false otherwise.
Example
Click to collapse [-]
ClientThis snippet reads and writes string buffers into a custom file format.
local function writeBuffers( outFile, buffers ) -- Write header information. outFile:writeInt( #buffers ); -- number of buffers -- Write the buffers. for m,n in ipairs( buffers ) do local bufferContent = tostring( n ); -- Write header of buffer. outFile:writeShort( #bufferContent ); -- length of buffer -- Write the content outFile:write( bufferContent ); end end local function readBuffers( inFile ) local buffers = {}; -- Read header information. local numBuffers = inFile:readInt(); -- Make sure we read the int correctly. -- Fails if the file has less than 4 bytes to read. if ( numBuffers ) then local n = 1; while ( n <= numBuffers ) do -- Read buffer header. local bufferSize = inFile:readShort(); -- We always have to add the amount of buffers specified by the header. -- Hence we always add a buffer to the result array. local buffer = ""; if ( bufferSize ) then -- Read the buffer. buffer = inFile:read( bufferSize ); end -- Append the result buffer. buffers[n] = buffer; n = n + 1; end end return buffers; end -- Manager some buffers in a file. local myBuffers = { "Hello World!", "Cooking eggs on a frying pan", "The American Pizzas taste awesome", "There is no bathroom." }; local theFile = fileCreate( "buffers.dat" ); -- First write the buffers into the file. writeBuffers( theFile, myBuffers ); -- Now retrieve them again. theFile:seek( 0, "set" ); myBuffers = readBuffers( theFile ); -- Output the buffers. for m,n in ipairs( myBuffers ) do outputChatBox( n ); end