4 ms·
Yeah, C is really missing a way to serialize/deserialize data from raw memory/sockets into structs usable by your code. The least insane way, libpack [1] requir
by ge0rg 15y ago
Yeah, C is really missing a way to serialize/deserialize data from raw memory/sockets into structs usable by your code. The least insane way, libpack [1] requires replicating the data format definition three times:
* define the struct with all elements
* define a string for the binary representation
* call fpack/funpack with the string and all the struct elements as parameters...
Unfortunately, fixing this either requires some kind of black X-macro [2] magic or another template language used to write the specification and to generate the three above-mentioned representations from it...
[1] http://www.leonerd.org.uk/code/libpack/intro.html http://www.leonerd.org.uk/code/libpack/intro.html
[2] http://drdobbs.com/184401387 http://drdobbs.com/184401387
- masklinn 15y ago> or another template language Surely this could be handled via simple syntactic extensions to the struct specification (with everything wrapped into an ungodly macro from hell) in order to define the mapping between the struct itself and libpack's format string, no?
- ge0rg 15y agoThe problem is that you need to replicate the struct entries in the pack/unpack calls as well, which is only possible in plain C by using X-Macros. It might be possible to construct a macro that creates both the struct and the format string, though.
- masklinn 15y ago> The problem is that you need to replicate the struct entries in the pack/unpack calls as well Don't you only need the (generated) format string? Ideally, the macro could generate some wrapper function of some sort as well, which would unpack, fill and return an instance of the struct.
- signa11 15y agojust took a brief look at it. doesn't seem to support arbitrary bit-fields e.g. how would extract next 13 bits from a binary stream ?