Rick Minerich
Rick Minerich

Reputation: 3088

Methods for parsing C++ defines?

I'm building a driver in C++ which relies heavily on another dll (surprise!). This dll comes with a header which contains a huge list of defines. Each define represents different return, message and error codes.

Each of these sets of defines has a prefix which differentiates it from the others. Something like:

MSG_GOOD... MSG_BAD... MSG_INDIFFERENT...

RETURN_OK... RETURN_OMG.. RETURN_WHAT...

But hundreds of them. This makes things rather hard to debug.

Ideally, I'd like to find a way to get some kind of reasonable log output with plain text. I was thinking it might be possible to parse the H file at run time, but a less complex compile time option would be much better.

Upvotes: 0

Views: 130

Answers (1)

tdammers
tdammers

Reputation: 20721

I think you'll have to parse the .h file at one point or another, because once compiled, the #defines won't be anywhere in the code anymore. Parsing .h files just for the #defines isn't too hard though - just read in full lines (mind the backslash at the end), ltrim them, and if they begin with "#define " then split the remainder at the first whitespace, trim both parts, stuff them in a key/value container and you're essentially done. Shouldn't be too hard even in C++.

Upvotes: 1

Related Questions