Getting "tree" of a Struct

60 views
Skip to first unread message

Lou King

unread,
Aug 16, 2015, 10:18:43 AM8/16/15
to Construct
Is there any easy way to get the tree which is defined by a struct?

I have a project which needs to translate a binary file into xml, and if I had a method which returned the tree, it would be helpful to construct my translation table.

E.g., 

Struct('A', Struct('B', String('B1'), String('B2')), Struct('C', ULInt32('C1')))

would return something like

['A', ['B', ['B1', 'B2']], ['C', ['C1']]]

Of course Arrays, Unions, etc., would need to be represented as well somehow.

I was looking at writing a recursive function using subcon.subcons, and subcon.name which might be private. This gets complicated, though, when dealing with arrays and other substructs.

Frank Carney

unread,
Sep 25, 2015, 3:32:13 PM9/25/15
to Construct
There is a way to print out the entire construct structure at any point.  So you should be able to print out the parsed structure at the end of construction.  I know I have done this, but cannot remember the exact construct function to do this.  It is great for debugging.  I found how to do this in the construct documentation.

Arek Bulski

unread,
Sep 26, 2016, 10:48:08 AM9/26/16
to Construct
print(complexstruct.parse(...)) will print nested containers no problem. 

As for processing the tree, that is something you have to do yourself. Each Struct has a subcon and each construct has a name member. That's it.
Reply all
Reply to author
Forward
0 new messages