This will come in handy when working with files, especially for save systems.
--[[ Features ]]--
Successfully Write and Read Data:
Byte or Char
Short
Integer
String
Work with Bits (Booleans) in a byte or other format
More Info
--[[ System Data Types ]]--
08 bit (1 byte ) Byte - Max Value: 255
16 bit (2 bytes) Short - Max Value: 65535
24 bit (3 bytes) LongShort - Max Value: 16777215
32 bit (4 bytes) Int - Max Value: 4294967295
--[[ Constants Used In Examples ]]--
CONST_BYTE = 8
CONST_SHORT = 16
CONST_LONGSHORT = 24
CONST_INT = 32
--[[ File Utility Sytem Functions List ]]--
--[[ Bits Table System Functions List ]]--
08 bit (1 byte ) Byte - Max Value: 255
16 bit (2 bytes) Short - Max Value: 65535
24 bit (3 bytes) LongShort - Max Value: 16777215
32 bit (4 bytes) Int - Max Value: 4294967295
--[[ Constants Used In Examples ]]--
CONST_BYTE = 8
CONST_SHORT = 16
CONST_LONGSHORT = 24
CONST_INT = 32
--[[ File Utility Sytem Functions List ]]--
WriteStream( url )
Description: Open a stream for writing
Returns: A stream object
Information: All streams created by ReadStream or WriteStream should eventually be closed using CloseStream.
Code Example:
Returns: A stream object
Information: All streams created by ReadStream or WriteStream should eventually be closed using CloseStream.
Code Example:
1
2
3
4
2
3
4
dofile("sys/lua/FileSystemLibrary.lua") File = WriteStream("test.txt") WriteLine(File, "hello world") CloseStream(File)
ReadStream( url )
Description: Open a stream for reading
Returns: A stream object
Information: All streams created by ReadStream or WriteStream should eventually be closed using CloseStream.
Code Example:
Returns: A stream object
Information: All streams created by ReadStream or WriteStream should eventually be closed using CloseStream.
Code Example:
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
dofile("sys/lua/FileSystemLibrary.lua") -- "server.lua" -- the following prints the contents of this lua file File = ReadStream("sys/lua/server.lua") while true do 	local Line = ReadLine(File) 	if (Line == nil) then break end 	print(Line) end CloseStream(File)
CloseStream( stream )
Description: Close a file stream
Information: All streams should be closed when they are no longer required.
Code Example:
Information: All streams should be closed when they are no longer required.
Code Example:
1
2
3
4
2
3
4
dofile("sys/lua/FileSystemLibrary.lua") File = WriteFile("test.txt") WriteLine(File, "hello world") CloseFile(File)
WriteChar( stream, char )
Description: Write a Char of a string to a stream
Information: WriteChar writes a single Char to stream.
Code Example:
Information: WriteChar writes a single Char to stream.
Code Example:
1
2
3
4
2
3
4
dofile("sys/lua/FileSystemLibrary.lua") File = WriteStream("test.txt") WriteChar(File, "hello world") --> writes only "h" CloseStream(File)
WriteByte( stream, value )
Description: Write a Byte to a stream
Information: WriteByte writes a single Byte to stream.
Code Example:
Information: WriteByte writes a single Byte to stream.
Code Example:
1
2
3
4
2
3
4
dofile("sys/lua/FileSystemLibrary.lua") File = WriteStream("test.txt") WriteByte(File, 255) CloseStream(File)
WriteShort( stream, value )
Description: Write a Short to a stream
Information: WriteShort writes 2 bytes to stream.
Code Example:
Information: WriteShort writes 2 bytes to stream.
Code Example:
1
2
3
4
2
3
4
dofile("sys/lua/FileSystemLibrary.lua") File = WriteStream("test.txt") WriteShort(File, 12257) CloseStream(File)
WriteLongShort( stream, value )
Description: Write a LongShort to a stream
Information: WriteLongShort writes 3 bytes to stream.
Code Example:
Information: WriteLongShort writes 3 bytes to stream.
Code Example:
1
2
3
4
2
3
4
dofile("sys/lua/FileSystemLibrary.lua") File = WriteStream("test.txt") WriteLongShort(File, 123451) CloseStream(File)
WriteInt( stream, value )
Description: Write a Int to a stream
Information: WriteInt writes 4 bytes to stream.
Code Example:
Information: WriteInt writes 4 bytes to stream.
Code Example:
1
2
3
4
2
3
4
dofile("sys/lua/FileSystemLibrary.lua") File = WriteStream("test.txt") WriteInt(File, 123456789) CloseStream(File)
WriteString( stream, string )
Description: Write a String to a stream
Information: Max string length is 255. if (auto_length = nil) then auto_length is true.
Code Example:
Information: Max string length is 255. if (auto_length = nil) then auto_length is true.
Code Example:
1
2
3
4
2
3
4
dofile("sys/lua/FileSystemLibrary.lua") File = WriteStream("test.txt") WriteString(File, "my name is simon") CloseStream(File)
ReadString( stream, length )
Description: Read a String from a stream
Returns: A String
Code Example:
Returns: A String
Code Example:
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
dofile("sys/lua/FileSystemLibrary.lua") File = WriteStream("test.txt") WriteString(File, "my name is simon") CloseStream(File) File = ReadStream("test.txt") Print(ReadString(File)) CloseStream(File)
--[[ Bits Table System Functions List ]]--
StateToNumber( state )
Description: Converts boolean to number
Returns: A number
Information: false = 0; true = 1.
Code Example:
Returns: A number
Information: false = 0; true = 1.
Code Example:
1
2
2
bool = StateToNumber(false) print(bool) --> 1
NumberToState( value )
Description: Converts number to boolean
Returns: A boolean
Information: false = 0; true = 1.
Code Example:
Returns: A boolean
Information: false = 0; true = 1.
Code Example:
1
bool = NumberToState(1) --> bool = true
PrintBits( bits_table, bits_count )
Description: prints bits line
Information: bits_count sets with how many bits function will work
Code Example:
Information: bits_count sets with how many bits function will work
Code Example:
1
2
3
2
3
Bits = NumberToBits(255, CONST_BYTE) InvertBits(Bits, CONST_BYTE) PrintBits(Bits,CONST_BYTE) --> 1111 1111
ArgumentsTobits( ... )
Description: Converts arguments to bits
Returns: A bits table
Information: Bits count is equal arguments count
Code Example:
Returns: A bits table
Information: Bits count is equal arguments count
Code Example:
1
2
2
byte_bits = ArgumentsToBits(1,0,1,0,1,0,1,0) PrintBits(byte_bits,CONST_BYTE) --> 1010 1010
NumberToBits ( value, bit_count )
Description: Converts number to bits
Returns: A bits table
Information: bit_count sets with how many bits function is going to work
Code Example:
Returns: A bits table
Information: bit_count sets with how many bits function is going to work
Code Example:
1
2
2
byte_bits = NumberToBits(129,CONST_BYTE) PrintBits(byte_bits,CONST_BYTE) --> 1000 0001
BitsToNumber( bits_table, bit_count )
Description: Converts bits to number
Returns: A number
Information: bit_count sets with how many bits function is going to work
Code Example:
Returns: A number
Information: bit_count sets with how many bits function is going to work
Code Example:
1
2
3
2
3
byte_bits = ArgumentsToBits(1,0,0,0,0,0,0,1) byte_number = BitsToNumber(byte_bits,CONST_BYTE) print(byte_number) --> 129
SumBits( bits_table_a, bits_table_b, bit_count )
Description: converts bits to number
Returns: A number
Information: bits_table_a will be also changed as result
Code Example:
Returns: A number
Information: bits_table_a will be also changed as result
Code Example:
1
2
3
4
2
3
4
BitsA = ArgumentsToBits(1,0,0,0,0,0,0,1) BitsB = ArgumentsToBits(0,0,1,0,0,1,0,0) SumBits(BitsA, BitsB, CONST_BYTE) PrintBits(byte_bits,CONST_BYTE) --> 1010 0101
PushBits( bits_table, push_direction, bit_count )
Description: Pushes bits to the right or left
Returns: A table
Information: bits_table will be also changed as result. push_direction (1 = right, -1 = left)
Code Example:
Returns: A table
Information: bits_table will be also changed as result. push_direction (1 = right, -1 = left)
Code Example:
1
2
3
2
3
Bits = ArgumentsToBits(1,1,0,0,0,0,1,1) PushBits(Bits, 2, CONST_BYTE) PrintBits(Bits,CONST_BYTE) --> 0011 0000
InvertBits( bits_table, bit_count )
Description: inverts bits
Returns: A table
Information: bits_table will be also changed as result.
Code Example:
Returns: A table
Information: bits_table will be also changed as result.
Code Example:
1
2
3
2
3
Bits = ArgumentsToBits(1,1,0,1,0,0,0,0) InvertBits(Bits, CONST_BYTE) PrintBits(Bits,CONST_BYTE) --> 0000 1011
--[[ Download ]]--
File does not exist (12623)
Sample 1 - Write/Read String From a File
1
2
3
4
5
6
7
2
3
4
5
6
7
File = WriteStream("text.txt") WriteString(File, "This is a string") CloseStream(File) File = ReadStream("text.txt") Print(ReadString(File)) --> prints "this is a string" CloseStream(File)
Sample 2 - Write/Read Multiple Strings From a File
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
File = WriteStream("test.txt") WriteString(File, "My name is Simon") WriteString(File, "I am student") WriteString(File, "I am a good student!") CloseStream(File) File = ReadStream("test.txt") Print(ReadString(File)) --> My name is Simon Print(ReadString(File)) --> I am student Print(ReadString(File)) --> I am a good student! CloseStream(File)
Sample 3 - Write/Read Byte From a File
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
File = WriteStream("test.txt") MyByte = 132 WriteByte(File, MyByte) CloseStream(File) File = ReadStream("test.txt") Print(ReadByte(File)) --> prints 132 value CloseStream(File)
Sample 4 - Write/Read Short and Integer From a File
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
File = WriteStream("test.txt") MyInteger = 123456789 MyShort = 32124 WriteInt(File, MyInteger) WriteShort(File, MyShort) CloseStream(File) File = ReadStream("test.txt") Print(ReadInt(File)) --> prints 123456789 value Print(ReadShort(File)) --> prints 32124 value CloseStream(File)
edited 3×, last 24.10.15 11:17:02 pm