Parsing a binary file. What is a modern way?

后端 未结 10 2042
悲哀的现实
悲哀的现实 2021-01-30 01:41

I have a binary file with some layout I know. For example let format be like this:

  • 2 bytes (unsigned short) - length of a string
  • 5 bytes (5 x chars) - the
10条回答
  •  时光取名叫无心
    2021-01-30 02:11

    I had to solve this problem once. The data files were packed FORTRAN output. Alignments were all wrong. I succeeded with preprocessor tricks that did automatically what you are doing manually: unpack the raw data from a byte buffer to a struct. The idea is to describe the data in an include file:

    BEGIN_STRUCT(foo)
        UNSIGNED_SHORT(length)
        STRING_FIELD(length, label)
        UNSIGNED_INT(stride)
        FLOAT_ARRAY(3 * stride)
    END_STRUCT(foo)
    

    Now you can define these macros to generate the code you need, say the struct declaration, include the above, undef and define the macros again to generate unpacking functions, followed by another include, etc.

    NB I first saw this technique used in gcc for abstract syntax tree-related code generation.

    If CPP is not powerful enough (or such preprocessor abuse is not for you), substitute a small lex/yacc program (or pick your favorite tool).

    It's amazing to me how often it pays to think in terms of generating code rather than writing it by hand, at least in low level foundation code like this.

提交回复
热议问题