473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Associate argument with entry in header file

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
Nov 14 '05 #1
8 1454

"Chandrashekar Tippur" <ct*****@msn.com> wrote in message
news:62**************************@posting.google.c om...
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.


#include <string.h>

#define VARIABLE 10

int main(int argc, char **argv)
{
return VARIABLE * ((argc > 1) && !strcmp(argv[1], "VARIABLE"));
}
-Mike
Nov 14 '05 #2
Chandrashekar Tippur wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3
In article <41***************@yahoo.com>,
CBFalconer <cb********@worldnet.att.net> wrote:
We should be able to run Checkheader VARIABLE and it should
return 10.
[...] printf("%d\n", VARIABLE);


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
Nov 14 '05 #4
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
#include <string.h>

#define VARIABLE 10

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

Nov 14 '05 #5
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
Nov 14 '05 #6
ct*****@msn.com (Chandrashekar Tippur) wrote in message news:<62**************************@posting.google. com>...
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
--
Nov 14 '05 #7
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

Nov 14 '05 #8
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
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Grzegorz Staniak | last post by:
Hello, I'm a newbie Python user, a systems administrator - I've been trying to switch from Perl to Python for administrative tasks - and one thing I cannot understand so far is why I need the...
1
by: knutsample | last post by:
Hello! I'm trying to associate a file extension to my wxPython script so that all I have to do is double click on the file and my python script will load up with the path of that file. For...
6
by: Ashok | last post by:
hi, i want to know how to make a specific type of file open in an application i developed in python when the user clicks on the file.(in windows) for eg. a .txt file when clicked opens in notepad,...
0
by: Scott Chang | last post by:
Hi all, I have Microsoft Visual C++ .NET (2002) program that is installed on my Windows XP Professional Operating System PC. I started my project (named HelloMCPP)in the Managed C++ Application...
2
by: S.Creek | last post by:
Does anybody know how to associate an icon with a file extension that windows is not familiar with? thanks
6
by: Prashant Bhuptani | last post by:
Hi Guys, I am trying to use a C++ dll in VB.NET code. I have imported the dll in the following manner: <code> Imports System.Runtime.InteropServices
14
by: AtomicBob | last post by:
I have been given an interesting task: Make a page with a single form field, which is to take a name (first and last), and regardless of case or whether there is a space between the first and...
3
by: zhang.yongpeng | last post by:
Hello, I met some problems when trying to sort a list that has shared_ptr in it. here is the non-compliable code. test.cpp: /********************* begin of code *************************/...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.