[Tool] BinIO: Binary I/O for Lua, anyway

  • byzod
    1st Feb 2013 Member 1 Permalink

    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:

  • mniip
    1st Feb 2013 Developer 0 Permalink
    Eh?
    EDIT: honestly, what I see is a whole bunch of useless bad-styled code...
  • jacksonmj
    1st Feb 2013 Developer 0 Permalink

    @mniip (View Post)

    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...

  • boxmein
    1st Feb 2013 Former Staff 1 Permalink
    @mniip (View Post)
    Yeah, I'd probably say your code is far more bad-styled. Lots of variables have one letter names, you don't use whitespace enough, it's like you write minified code. If you want people to "gain speed" via minification, distribute a second source file or something.
    He has well written doc comments, his coding style is consistent (ifs etc get parentheses) and variable names are legible.
  • byzod
    2nd Feb 2013 Member 1 Permalink

    @mniip (View Post)

     It‘s useless, but it‘s not bad-styled :)

  • mniip
    5th Feb 2013 Developer 0 Permalink
    @jacksonmj (View Post)
    by bad style i usually mean things like

    if var==true then
    return true
    else if var==false false
    return false
    end
    end


    EDIT:
    back to your code, @byzod

    BinIO.File = nil1)wut?
    2)is your system not capable of handling multiple files?

    Everywhere:
    if( foo )thenyou dont need the parentheses in lua

    local filehandle = nilwhy not just local filehandle


    if( mode:sub(-1) ~= "b" ) then mode = mode.."b" endthis aint gonna always work, use string.find/match

    io.close(fileHandle)Ever heard of pseudo-metatables?
    you can fileHandle:close()

    Lines 48-52:
    what the?

    if foo~=nil then...ever heard of true values?
    if foo then ought to work
  • byzod
    7th Feb 2013 Member 4 Permalink

    @mniip (View Post)

     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.

  • mniip
    8th Feb 2013 Developer 0 Permalink
    @byzod (View Post)
    I have programmed in many compiled and/or fixed typing languages such as C and Pascal.