AutoGen: 7.7.5 Config file only example

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 7.7.5 Config file only example

If for some reason it is difficult or unworkable to integrate configuration file processing with command line option parsing, the `libopts` (see section libopts External Procedures) library can still be used to process configuration files. Below is a "`Hello, World!`" greeting program that tries to load a configuration file ``hello.conf'` to see if it should use an alternate greeting or to personalize the salutation.

    #include <sys/types.h>
    #include <pwd.h>
    #include <string.h>
    #include <unistd.h>
    #include <autoopts/options.h>
    int main( int argc, char** argv ) {
      char* greeting = "Hello";
      char* greeted  = "World";
      const tOptionValue* pOV = configFileLoad( "hello.conf" );

      if (pOV != NULL) {
        const tOptionValue* pGetV = optionGetValue( pOV, "greeting" );

        if (  (pGetV != NULL)
           && (pGetV->valType == OPARG_TYPE_STRING))
          greeting = strdup( pGetV->v.strVal );

        pGetV = optionGetValue( pOV, "personalize" );
        if (pGetV != NULL) {
          struct passwd* pwe = getpwuid( getuid() );
          if (pwe != NULL)
            greeted = strdup( pwe->pw_gecos );
        }

        optionUnloadNested( pOV ); /* deallocate config data */
      }
      printf( "%s, %s!\n", greeting, greeted );
      return 0;
    }

With that text in a file named "hello.c", this short script:

    cc -o hello hello.c `autoopts-config cflags ldflags`
    ./hello
    echo 'greeting Buzz off' > hello.conf
    ./hello
    echo personalize > hello.conf
    ./hello

will produce the following output (for me):

    Hello, World!
    Buzz off, World!
    Hello, Bruce Korb!

This document was generated by Bruce Korb on July, 28 2005 using texi2html 1.76.