Associate argument with entry in header file

  • Thread starter Chandrashekar Tippur
  • Start date
C

Chandrashekar Tippur

All,

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.

For example,
________________________________________________________________________________
myheader.h
-----------

#define VARIABLE 10
________________________________________________________________________________

Checkheader.c
________________________________________________________________________________
#include myheader.h

int main(int argv, char * argc){
?????????
}
________________________________________________________________________________

We should be able to run Checkheader VARIABLE and it should return 10.

Thanks in advance,
Shekar
 
M

Mike Wahler

Chandrashekar Tippur said:
All,

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.

For example,
____________________________________________________________________________
____
myheader.h
-----------

#define VARIABLE 10
____________________________________________________________________________
____
____________________________________________________________________________
____
#include myheader.h

int main(int argv, char * argc){
?????????
}
____________________________________________________________________________
____

We should be able to run Checkheader VARIABLE and it should return 10.

#include <string.h>

#define VARIABLE 10

int main(int argc, char **argv)
{
return VARIABLE * ((argc > 1) && !strcmp(argv[1], "VARIABLE"));
}


-Mike
 
C

CBFalconer

Chandrashekar said:
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.

For example,
_________________________________________________________________
myheader.h
-----------

#define VARIABLE 10
_________________________________________________________________

Checkheader.c
_________________________________________________________________
#include myheader.h

int main(int argv, char * argc){
?????????
}
_________________________________________________________________

We should be able to run Checkheader VARIABLE and it should
return 10.

---- File checkheader.c ----
#include <stdio.h>
#include "myheader.h"

int main(void)
{
printf("%d\n", VARIABLE);
return 0;
}

Time to read your C book.
 
R

Richard Tobin

We should be able to run Checkheader VARIABLE and it should
return 10.
[...]
printf("%d\n", VARIABLE);[/QUOTE]

Note that he specifies the #defined variable name as an argument.

I take it he wants to be able to map from strings to preprocessor
variables at run time, which can't be done. He would need a table
of variable names and values.

-- Richard
 
C

Chandrashekar Tippur

Mike,

Thanks for replying back.
The VARIABLE can be anything. The bigger picture is that we need to be
able to get system configuration variables using sysconf() function.
This takes long as the argument and returns long. We want to be able
to pass a variable as argument to this function and get the
corresponding value.
Is there a way to use preprocessor directives like:

#ifdef <Variable name>
return sysconf(<variable name>)

I appreciate the help.

Shekar
 
C

Cedric LEMAIRE

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
 
C

Cedric LEMAIRE

The VARIABLE can be anything. The bigger picture is that we need to be
able to get system configuration variables using sysconf() function.
This takes long as the argument and returns long. We want to be able
to pass a variable as argument to this function and get the
corresponding value.

OK, so replace '#readNumeric:dValue' by '#readInteger:dValue' in the
BNF script, to accept long only.
Then, in the template-based script, replace 'printf("\"@i.key()@\" =
@i@");' by 'printf("sysconf(\"@i.key()@\") = %ld",
sysconf(@i.key()@));'.
You will need to include the C header for 'sysconf()'.

On your example, it will generate:
printf("sysconf(\"VARIABLE@\") = %ld", sysconf(VARIABLE));


Regards,

Cedric Lemaire
--
 
B

bowsayge

Chandrashekar Tippur said to us:
Mike,

Thanks for replying back.
The VARIABLE can be anything. The bigger picture is that we need to be
able to get system configuration variables using sysconf() function.
This takes long as the argument and returns long. We want to be able
to pass a variable as argument to this function and get the
corresponding value.
Is there a way to use preprocessor directives like:

#ifdef <Variable name>
return sysconf(<variable name>)

I appreciate the help.

Shekar

Yes, but it would require an outside program such as a perl script.

Without creating an outside program, you could create a table of
configuration variable names and their values:

#define MAX_ARRAY_SIZE 1000
#define TIMEOUT 600
#define ANOTHER_VALUE 17
struct sysconf_value {
const char * name;
long value;
};
struct sysconf_value values [] = {
"max_array_size", MAX_ARRAY_SIZE,
"timeout", TIMEOUT,
"another_value", ANOTHER_VALUE,
0, 0,
};

Define sysconf like so:
long sysconf (const char * str) {
int ii;
for (ii = 0; values[ii].name; ii++) {
if (0 == strcmp (str,values[ii].name)) return values[ii].value;
}
fprintf (stderr, "Can't find sysconf: %s\n", str);
exit(1);
}

In main:
int ii = 0;
const char * tests [] = { "max_array_size", "timeout", "another_value", 0 };

for (ii = 0; tests[ii]; ii++) {
printf ("name: %s value: %ld\n", tests[ii], sysconf(tests[ii]));
}

HTH
 
C

Chandrashekar Tippur

All,

Thanks for all your replies. I will certainly try the code worker
approach. I will update the thread with the results.
Thanks again.

Shekar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top