Associate argument with entry in header file

Discussion in 'C Programming' started by Chandrashekar Tippur, Aug 20, 2004.

  1. 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
    Chandrashekar Tippur, Aug 20, 2004
    #1
    1. Advertising

  2. Chandrashekar Tippur

    Mike Wahler Guest

    "Chandrashekar Tippur" <> wrote in message
    news:...
    > 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
    Mike Wahler, Aug 21, 2004
    #2
    1. Advertising

  3. Chandrashekar Tippur

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Aug 21, 2004
    #3
  4. In article <>,
    CBFalconer <> 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
    Richard Tobin, Aug 21, 2004
    #4
  5. 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
    Chandrashekar Tippur, Aug 21, 2004
    #5
  6. (Chandrashekar Tippur) wrote in message news:<>...
    > 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
    Cedric LEMAIRE, Aug 21, 2004
    #6
  7. (Chandrashekar Tippur) wrote in message news:<>...
    > 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
    --
    Cedric LEMAIRE, Aug 22, 2004
    #7
  8. Chandrashekar Tippur

    bowsayge Guest

    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
    bowsayge, Aug 22, 2004
    #8
  9. 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
    Chandrashekar Tippur, Aug 22, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?ZGF2aWQ=?=

    setup associate application with file type

    =?Utf-8?B?ZGF2aWQ=?=, Apr 25, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    375
    =?Utf-8?B?ZGF2aWQ=?=
    Apr 25, 2005
  2. senges
    Replies:
    3
    Views:
    393
    senges
    Apr 12, 2006
  3. Replies:
    1
    Views:
    358
    Alessandro Bottoni
    Aug 22, 2005
  4. mlt
    Replies:
    2
    Views:
    820
    Jean-Marc Bourguet
    Jan 31, 2009
  5. Stef Mientki

    OS independent file associate ?

    Stef Mientki, May 14, 2009, in forum: Python
    Replies:
    0
    Views:
    249
    Stef Mientki
    May 14, 2009
Loading...

Share This Page