ct*****@msn.com (Chandrashekar Tippur) wrote in message news:<62**************************@posting.google. com>...
We want to associate a argument with a corresponding entry in header
file. How do we do this?
The argument is defined in a header file. We want to extract the value
of the #define variable.
[snip...]
You cannot accomplish it in C directly. However, I can propose you a
solution: insert a CodeWorker script into your main() function.
CodeWorker is a parsing tool and a source code generation, freeware
available at "http://www.codeworker.org".
Your file, "main.cpp" becomes:
#include <stdio.h>
#include <string.h>
#include "myHeader.h"
int main(int argc, char **argv) {
if (argc != 2) {
printf("Argument expected!");
return -1;
}
// this markup announces a CodeWorker script
/*##markup##"generate mapping"*//*##script##*/
/*
// parse the file "myHeader.h" with an extended-BNF script
parseAsBNF({
mapping ::=
#ignore(C++) // ignore C-like comments
// looking for every '#define <numeric> <EOL>
[
// jump to the next '#define'
->[
'#' #readIdentifier:"define"
// register the name of the preprocessor definition
#readIdentifier:sDefine
// numeric value ?
#readNumeric:dValue
#!ignore // stop to ignore whitespaces or comments
['\r']? '\n' // <EOL>
// store the 'define' and the associated value
=> insert this[sDefine] = dValue;
]
]*
;
}, project, "myHeader.h");
// code generation of a sequence of if/else on the preprocessor
// definitions: display their corresponding numeric value.
foreach i in project {
// the beginning isn't the same if it is the first definition
// or not
if i.first() {
@
@
} else {
@ else @
}
// the test on the value of the command-line argument:
// display it on the console if valid.
@if (strcmp(argv[1], "@i.key()@") == 0) {
printf("\"@i.key()@\" = @i@");
}@
}
@
@
*/
/*##script##*/
return 0;
}
Type:
codeworker -autoexpand main.cpp -commentbegin /* -commentend */
and it will add the following code just after the last
'/*##script##*/' and just before ' return 0;':
/*##begin##"generate mapping"*/
if (strcmp(argv[1], "VARIABLE") == 0) {
printf("\"VARIABLE\" = 10");
} else if (strcmp(argv[1], "OTHER") == 0) {
printf("\"OTHER\" = 25.5");
}
/*##end##"generate mapping"*/
with "myHeader.h" being worth:
#ifndef _myHeader_h_
// this one is refused: not a number
#define _myHeader_h_
// this one is retained
#define VARIABLE 10
// this one is refused, because not an integer
#define WRONG "constant string aren't accepted"
// this one is refused, because points to another definition
#define WRONG2 VARIABLE
// this one is accepted too
#define OTHER 25.5
#endif
Regards,
Cedric Lemaire