Download: http://pastebin.com/fZGySPzh
Description: BinIO is a light solution of binary file output/input for Lua, it's (stupidly) base on string.char(), so it's moderately slow, if you're looking for massive binary file access, ignore this and use C or C++.
Interfaces: It's similar to file standard function in Lua, here's a simple list of all function of BinIO, check the source code for detailed usage.
BinIO.File
file handle when BinIO.Open invoked.
BinIO.Open(object fileNameOrHandle, string mode)
similar to io.open
BinIO.Close(file handle fileHandle)
similar to io.close
BinIO.ToByteList(object val, number length)
convert string or number to byte list (byte[] in C)
BinIO.Write(object val, number length, file handle fileHandle)
write binary data to file. val is string or number or bool or byte list (byte[])
BinIO.Read(number length, string readType, file handle fileHandle)
read binary data, read type can be number ("n"), string("s"), bool("bool") or byte list( byte[], "b")
BinIO.Seek(string whence, number offset, file handle fileHandle)
similar to file:seek
BinIO.SetBuffer(string mode, number size)
similar to file:setvbuf
Performance Note:
It's slow.
(native means use file:read directly. It can not read binary data of course, just a speed compare. Block means long string or large array, because others only write one byte per loop)
Write:
Read:
Write string and byte[] is acceptable fast, but others especially numbers are slow, because it costs much time to convert decimal number to hex for large numbers.
Read is somehow faster.
BinIO will try to use proper length when write number without set length, 127 in one byte, 128 in two bytes, etc. It's roughly 12% faster if you set the length explicitly.
Sample code:
BinIO.Open("scripts/test.out", "w")
BinIO.Write(127)
BinIO.Write(128)
BinIO.Write(-128, 16)
BinIO.Write(-129, 16)
BinIO.Write(0xAAFFFFFFFFBB, 16)
BinIO.Write("AAA", 8)
BinIO.Write("0123456789ABCDEF0123456", 16)
BinIO.Write("01234567")
BinIO.Write(true , 4)
BinIO.Write(false, 4)
local tbl = {0xAA, 0xBB, 0xCC}
BinIO.Write(tbl, 16)
BinIO.Write(tbl)
BinIO.Write(tbl, 2)
BinIO.Close()
BinIO.Open("scripts/test.out", "r")
tpt.log(tostring(BinIO.Read(1, "n" )))
tpt.log(tostring(BinIO.Read(2, "n" )))
tpt.log(tostring(BinIO.Read(16, "n" )))
tpt.log(tostring(BinIO.Read(16, "n" )))
tpt.log(tostring(BinIO.Read(16, "n" )))
tpt.log(tostring(BinIO.Read(8 , "s" )))
tpt.log(tostring(BinIO.Read(16, "s" )))
tpt.log(tostring(BinIO.Read(8 , "s" )))
tpt.log(tostring(BinIO.Read(4 , "bool")))
tpt.log(tostring(BinIO.Read(4 , "bool")))
local str = ""
hextable = BinIO.Read(16, "b" )
for i, hex in ipairs(hextable) do
str = str.." 0x"..string.format("%X", hex)
end
tpt.log(str)
local str = ""
hextable = BinIO.Read(3 , "b" )
for i, hex in ipairs(hextable) do
str = str.." 0x"..string.format("%X", hex)
end
tpt.log(str)
local str = ""
hextable = BinIO.Read(2 , "b" )
for i, hex in ipairs(hextable) do
str = str.." 0x"..string.format("%X", hex)
end
tpt.log(str)
BinIO.Close()
Result file:
Bad-styled? In what way? Constructive criticism, please. I actually prefer the style of byzods's code to the style of your code - it contains a multitude of comments, and uses reasonably descriptive variable names...
It‘s useless, but it‘s not bad-styled :)
if var==true then
return true
else if var==false false
return false
end
end
BinIO.File = nil
1)wut?if( foo )then
you dont need the parentheses in lualocal filehandle = nil
why not just local filehandle
if( mode:sub(-1) ~= "b" ) then mode = mode.."b" end
this aint gonna always work, use string.find/matchio.close(fileHandle)
Ever heard of pseudo-metatables?fileHandle:close()
if foo~=nil then
...ever heard of true values?if foo then
ought to work Thanks for your advice and looking through all those 'bad-styled' code. Frankly speaking I don't know much about Lua, I'll try to correct my code.
Personally suggest you write something else but not script, to find out what is programming 'style', such as initialize every variable before you use it.
Writing code without indent is not style but disaster.