Skip to content

BinFile

Neo Mind edited this page Jan 7, 2021 · 11 revisions

BinFile

BinFile class is used for accessing & modifying an existing binary file or creating a new one in the file system from QJS scripts.

Constructor

Its constructor takes 2 forms.

  • new BinFile()

    Creates the object with empty values.

  • new BinFile(path, [mode])

    Creates the object and opens the specified path with the specified mode

    mode can be one of

    • r = read only
    • w = write only
    • rw = read/write
    • a = append

Once the object is created, the following functions become available.

Open & Close

  • <obj>.Open(path, mode)

    Opens a new file path in the specified mode. If any file is already open using this object, it is automatically closed first.

    Returns true if successfully opened.

  • <obj>.Close()

    Closes currently open file if any.

Position related

  • <obj>.AtEnd()

    Check whether End of File has reached.

  • <obj>.Seek(pos)

    Seeks to specified position i.e. Change the file position offset to pos.

    Returns true if successful.

Readers

  • <obj>.ReadText([size], [from], [enc])

    Read size bytes from the specified position as a text string with the specified Encoding. All arguments are optional.

    size and from defaults to -1 and enc defaults to ASCII.

    If size is negative then all remaining bytes are read.

    If from is negative, bytes are read from the current position, and then the position gets incremented.

  • <obj>.ReadHex([size], [from])

    Read size bytes from the specified position as a hex string. Both arguments are optional and defaults to -1.

    If size is negative then all remaining bytes are read.

    If from is negative, bytes are read from the current position, and then the position gets incremented.

  • <obj>.ReadInt8([from])
    <obj>.ReadInt16([from])
    <obj>.ReadInt32([from])

    Read 1/2/4 bytes from the specified position as a signed integer respectively. from defaults to -1.

    If from is negative, bytes are retrieved from the current position, and then the position gets incremented.

  • <obj>.ReadUint8([from])
    <obj>.ReadUint16([from])
    <obj>.ReadUint32([from])

    Read 1/2/4 bytes from the specified position as an unsigned integer respectively. from defaults to -1.

    If from is negative, bytes are retrieved from the current position, and then the position gets incremented.

Writers

  • <obj>.WriteText(str, [to], [enc])

    Write the text string of specified Encoding to the specified position. Both to & enc are optional. to defaults to -1 and enc defaults to ASCII.

    If to is negative, bytes are written at current position, and then the position gets incremented.

    Returns true if successful else false.

  • <obj>.WriteHex(str, [to])

    Write the hex string to the specified position. to defaults to -1.

    If to is negative, bytes are written at current position, and then the position gets incremented.

    Returns true if successful else false.

  • <obj>.WriteInt8(value, [to])
    <obj>.WriteInt16(value, [to])
    <obj>.WriteInt32(value, [to])

    Writes a signed 8/16/32 bit integer to the specified position respectively. to defaults to -1.

    If to is negative, bytes are written at current position, and then the position gets incremented.

    Returns true if successful else false.

  • <obj>.WriteUint8(value, [to])
    <obj>.WriteUint16(value, [to])
    <obj>.WriteUint32(value, [to])

    Writes an unsigned 8/16/32 bit integer to the specified position respectively. to defaults to -1.

    If to is negative, bytes are written at current position, and then the position gets incremented.

    Returns true if successful else false.

Clone this wiki locally