Introduction:
I've been using CC for a while, and it bothered me that there are is no seek function in the FS API.
In other words, you either overwrite the whole entire file, or append to it; you can't place text inside of previously existing text.
However, with this API you can.
This API makes file handles that are similar in use to the file handles produced by the io API, but with added functions: seek, update, and flush. In addition, all you need to open a file with this API is a filename; there are no modes. Unfortunately, this means that binary mode is not supported yet. I'm working on it!
How to Use:
API Functions:
- open(filename) - Opens a file in text mode.
- openBinary(filename) - Opens a file in binary mode.
myfile.write("data") -- wrong, don't use periods
myfile:write("data") -- right, do use colons
Text-Mode File Handle Functions:
- writeToBuffer(data) - Internal, used by write().
- update() - Updates the internal buffer with what's on the hard drive.
- seek(whence, offset) - Sets where characters will be read/written.
- flush() - Writes the buffer to disk.
- write(…) - Writes all passed arguments to the buffer.
- clear() - Sets the interal buffer to the empty string.
- writeLine(…) - Writes all passed arguments to the buffer with newlines appended.
- readLine() - Reads until it finds a newline.
- readAll() - Reads all characters from the current position to the end of the file
- read(chars) - Reads chars characters from the file.
- lines() - Iterates over every line in the file.
- autoSeek(mode) - sets the autoseek mode.
- readAll() - Returns a table with every byte in the file
- read(bytes) - Returns a table with bytes bytes from the file.
- write(…) - Writes all passed arguments to the buffer.
- flush() - Writes the buffer to disk.
- clear() - Sets the buffer to the empty table.
- update() - Sets the buffer to what's on the hard drive.
- bytes() - Returns an iterator that loops over every byte in the buffer.
- autoSeek(mode) - Sets the autoseek mode.
seek() takes two arguments: whence and offset.
whence is where to seek from.
offset is how many bytes/characters to move the position up/down.
When seek() is used, it will return the new position of the seek pointer.
Example:
handle:seek("cur", 5) -- moves the seek pointer 5 characters/bytes up
handle:seek("cur", -5) -- moves the seek pointer 5 characters/bytes back
handle:seel("set") -- returns the seek pointer to the start of the file
handle:seek("cur") -- returns where the seek pointer is
handle:seek("end") -- returns the length of the file
How to use autoSeek():
autoSeek() takes one argument: mode.
The mode determines when the API will use "auto-seeking":
- Passing no mode at all, or passing "n", will disable "auto-seeking". This is the default behavior.
- Passing "r" will activate automatic seeking for reads only.
- Passing "w" will activate automatic seeking for writes only.
- Passing "rw" will activate automatic seeking for both reads and writes.
With autoseek off:
handle:write("a", "b", "c")
handle:flush()
produces this:
cba
However, with autoseek on:
handle:write("a", "b", "c")
handle:flush()
produces the expected:
abc
As for reading from files, what happens is obvious.
If you read with autoseek off, you will read the text from the same position every time:
-- Assuming the handle points to a file with "Test File" inside of it:
handle:read(4) -- returns "Test"
handle:read(9) -- returns "Test File"
handle:read(3) -- returns "Tes"
handle:readAll() -- returns "Test File"
handle:readAll() -- returns "Test File" again
Download:
File API w/ Seek on Pastebin
or alternatively, download the API in-game with this:
pastebin get a2yes8pR [APIName]