My purpose is to build a config file. So I basically have to represent command packet structures in some way.
When the actual data(binary) comes I want to compare the packet with this config file and then process it (convert the data into CSV format).
So there are different command types.
So each time when a packet comes, I have to take its opcode and check it with the config file and return the appropriate command format that represents the format of that packet.
The command format may look like below:
opcode - 1 bytes - integer
command - 4 byte - string
All the commands doesn't have the same number of fields or the same format.
I want to retrieve all these details. I can represent it in XML and parse it using some library like libxml2.
A sample XML format is given below:
<cmd type="Multiplication"> <field name="opcode" type="string" bytes="4"/> <field name="Multiplicand" type="number" bytes="2"/> <field name="Multiplier" type="number" bytes="2"/> </cmd>
But this approach is rather slow.
My thinking is to somehow represent the command packet format in structures. But since C/C++ is not a reflective language, the structure members cannot be known and you will need one function per structure (command) to parse it.
Please suggest some way to store the formats such that one generic function can parse the binary data just by looking at this format.
- The languages can be C or C++.
- Performance is top priority so XML and similar types are discouraged.
- In memory data structures are preferred.
Any help is highly appreciated.