C structure analyzer ("Reflection" in C?)

19 Jul 1999 01:16:05 -0400

          From comp.compilers

Related articles
C structure analyzer ("Reflection" in C?) phollingsworth@sbsintl.com (1999-07-19)
Re: C structure analyzer ("Reflection" in C?) jsgray@acm.org.nospam (Jan Gray) (1999-07-20)
Re: C structure analyzer ("Reflection" in C?) pmai@acm.org (1999-07-21)
Re: C structure analyzer ("Reflection" in C?) kst@cts.com (Keith Thompson) (1999-07-23)
Re: C structure analyzer ("Reflection" in C?) jerry.pendergraft@endocardial.com (Jerry Pendergraft) (1999-07-28)
Re: C structure analyzer ("Reflection" in C?) norbert@dune.gia.rwth-aachen.de (Norbert Berzen) (1999-07-30)
Re: C structure analyzer ("Reflection" in C?) kst@cts.com (Keith Thompson) (1999-07-30)
[3 later articles]
| List of all articles for this month |

From: phollingsworth@sbsintl.com
Newsgroups: comp.compilers
Date: 19 Jul 1999 01:16:05 -0400
Organization: Deja.com - Share what you know. Learn what you don't.
Keywords: C, analysis

        I'm dealing with a 3rd party API (The Eurex exchange "Values" API)
that uses a large number of C structures. The exchange supplies C
header files that describe structures which define the format of the
input and output messages. These header files provide structures that
define the message format of blocks of bytes that are sent to, and
received from, the exchange.

        Does anybody know of a tool that I could use that converts the C
structure definitions into some other source file that has each data
member name, byte offset, size etc so that I could make a program to
dynamically compose these "structures"?

        For example, if it had been done entirely in Java, then I could
use "Reflection" to analyse the methods and properties and create a
dynamic interface.

        The annoying thing is, I know that somewhere in VC++ it has all
of this and more because you can browse all of the data structures in
a program from within the debugger.

        Does anyone know of a way to get VC++ to dump this information
into some sort of "C source" file that can be included elsewhere?

        My ideal is to be able allow the user to edit the structures
dynamically and then have my program use the information to package up
the resultant data so that it can be sent to the exchange...

        Ideally, I give this hypothetical program this:

        #include <eucb_fieldsizes.h> // Defines PASSWORD_LEN, LOGIN_LEN,
        struct Authorization {
                char password[PASSWORD_LEN];
                char login[LOGIN_LEN];

        struct Message {
                char request_id[REQUEST_ID_LEN];
                Authorization authorization_data;

        And it generates something like this:

        struct datainfo {
                      const char *name;
                      const char *member;
                      const char *member_type;
                      int offset_in_bytes,
                      int size_in_bytes

        // P is parent structure type
        // F is the first member name
        // M is member name for the offset
        // T is the type of the member
        #define OFFSET(P,F,M) ((unsigned long)&((P*)0)->M) - ((unsigned

        #define SIZEOF(P,M) (unsigned long)sizeof(((P*)0)->M))

        #define FIELD(P,M,T,F) #P,#M,#T,OFFSET(P,F,M), SIZEOF(P,M)

        struct datainfo all_structures[] = {

        which after the preprocessor is done would look like:

        struct datainfo all_structures[] = {
                "Message", "request_id", "CHAR", 0, 10,
                "Message", "authorization_data", "Authorization", 10, 20,
                "Authorization", "password", "CHAR", 0, 10,
                "Authorization", "login", "CHAR", 10, 10

        The use of the macros etc. would mean that implicit alignments made
by the compiler would also have the desired effect.

        I could run the header files through this program, generate an
"all_structures.h" file that I could include, and then my program can
dynamically use the structures instead...

        OK, I hope you get the idea...

        The C Exploration Tools (CXT:
) comes close, and I could probably muck around about with perl to
take it's output with various options and generate what I want - but
maybe somebody knows of a better, more direct solution...

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.