Results 1 to 6 of 6

Thread: dynamic structures class

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,055

    dynamic structures class

    class to load structures from memory or disk using a basic text definition language at runtime.

    todo: make a loader for kaitai def language which already has many many common and complex
    structures defined. https://kaitai.io/

    any updates here:
    https://github.com/dzzie/libs/tree/master/CMemStruct

    Example:
    Code:
        Dim ms As New CMemStruct, errMsg As String, b() as byte
       
        If Not ms.AddFields("byte1*b,int1*i,lng1*l,cur1*c,blob1*15", errMsg) Then
            Text1 = errMsg
            Exit Sub
        End If
    
        ms.LoadFromFile , App.Path & "\test.bin"
        b() = ms.toBytes()
            
        addText ms.dump(True)
    
        If Not ms.field("blob1").SetValue("new blob2!", errMsg) Then
            addText "Error setting new blob1 in test2.bin: " & errMsg
        End If
    
         ms.field("cur1").SetValue 3.14
         ms.SaveToFile , App.Path & "\test3.bin"

    Interface:
    Code:
    'Variables
    Public filePath As String
    Public name As String      'in case instances get held in a collection or want to be named
    
    'Properties
    Property size() As Long
    Property fieldCount() As Long
    
    'Methods
    Function AddFields(ByVal formatString As String, Optional errMsg, Optional reset As Boolean = True, Optional ByVal name As String) As Boolean
    Sub AddField(ByVal name As String, ft As ms_fieldType, Optional size As Long = 0)
    Function dump(Optional asHex As Boolean = True) As String
    Function fromBytes(buf() As Byte) As Boolean
    Function toBytes() As Byte()
    Function fromHexString(str, Optional errMsg) As Boolean
    Function toHexString() As String
    Function SaveToFile(Optional ByVal offset As Long, Optional ByVal filePath As String)
    Function LoadFromFile(Optional ByVal offset As Long, Optional ByVal filePath As String)
    Function offsetOf(fieldIndexOrName) As Long
    Sub CloseFile()
    Sub reset()
    Function CurOffset() As Long
    Function field(fieldIndexOrName) As CStructField
    Another similar class that may be of interest is CMemBuf that lets you do read and writes to a memory buffer in a very similar way tha tyou can use get/put to write different types to disk. buffer grows dynamically:

    https://github.com/dzzie/libs/tree/master/CMemBuf


    Code:
        Dim mb As New CMemBuf
        Dim i As Integer
        Dim l As Long
        Dim ii(4) As Integer
        dim ss() as string
    
        i = &H1122
        l = &H66554433
        
        ii(0) = &H1122
        ii(1) = &H3344
        ii(2) = &H5566
        ii(3) = &H7788
        ii(4) = &H99AA
        
        ss = Split("this is my string", " ")
        
        mb.write_ i
        mb.write_ l
        mb.write_ ii
        
        strStart = mb.curOffset
        mb.writeStr "testing!"
        mb.writeStr ss, , mb_bstr
        debug.print mb.HexDump
        
        mb.read l, 2
        debug.print Hex(l)
        
        debug.print mb.readStr(strStart)
        debug.print mb.readStr(, mb_bstr)
    Interface:
    Code:
    'Variables
    Public lastErr As String
    
    'Properties
    Property curOffset() As Long
    Property Buffer() As Byte()
    
    'Methods
    Function AryIsEmpty(ary) As Boolean
    Sub push(ary, Value) 'this modifies parent ary object
    Function rc4(ByVal password As Variant, Optional overWriteBuf As Boolean = True) As Byte()
    Function WriteLong(ByVal X As Long, Optional offset As Long = -1) As Boolean
    Function ReadByte(ByRef X As Byte, Optional offset As Long = -1) As Boolean
    Function ReadLong(ByRef X As Long, Optional offset As Long = -1) As Boolean
    Function read(ByRef data As Variant, Optional offset As Long = -1) As Boolean
    Function write_(data As Variant, Optional offset As Long = -1) As Boolean
    Function fromFile(ByVal fPath As String, Optional fileOffset As Long = 1, Optional ByVal dataSize As Long = -1) As Boolean
    Function toFile(ByVal fPath As String, Optional fileOffset As Long = 1) As Boolean
    Function saveToMem(memAddress As Long, Optional size As Long, Optional offset As Long = -1) As Boolean
    Function writeFromMem(memAddress As Long, size As Long, Optional offset As Long = -1) As Boolean
    Function writeStr(data As Variant, Optional offset As Long = -1, Optional writeType As mb_StrTypes = mb_cstr) As Boolean
    Function readStr(Optional offset As Long = -1, Optional readType As mb_StrTypes = mb_cstr, Optional fixedWidth As Long = 0) As String
    Function HexDump(Optional ByVal Length As Long = -1, Optional ByVal startAt As Long = 1, Optional hexFormat As hexOutFormats = hoDump) As String
    Function sizeInBounds(size As Long, Optional offset As Long = -1) As Boolean
    Function ensureSize(ByVal size As Long, Optional offset As Long = -1) As Boolean
    Sub clear()
    Attached Files Attached Files
    Last edited by dz32; Feb 15th, 2020 at 08:10 PM.

  2. #2
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: dynamic structures class

    How tough would it be to add support for bit fields?

  3. #3

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,055

    Re: dynamic structures class

    [...]
    Last edited by dz32; Apr 26th, 2019 at 11:19 AM.

  5. #5
    PowerPoster
    Join Date
    Sep 2012
    Posts
    2,083

    Re: dynamic structures class

    Quote Originally Posted by The trick View Post
    What's about IRecordInfo? The advantages are the assignment that dynamic structure to a variant variable. Then you can pass it into the methods of the interfaces, proper marshaling etc.
    Hi The trick, I want to use VB6 to simulate the dynamic structure classes of JavaScript. Could you give some suggestions? Thanks!

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: dynamic structures class

    Considering how worthless the rest of this thread has become due to the editing by the OP, you really ought to start a new thread on the subject.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width