Slice Tools
  • Home
  • SourceForge Page


  • libFoundation
  • Home
  • Class Hierarchy
  • Class List
  • Class Members
  • Examples


  • SourceForge.net Logo
     

    example-configfile.cc

    Demonstrates configuration file handling.

    Various methods are called to pull information out of the configuration file.
    // $Id: example-configfile.cc,v 1.1 2005/07/29 02:55:16 mschatz Exp $
    // Demonstrates different techniques for handling configuration files
    
    #include "Foundation.hh"
    using namespace std;
    
    // Prints a section from a configuration file
    void PrintSection(ConfigSection * section)
    {
      string variable;
      string fullName = section->getFullSectionName();
    
      while ((variable = section->getNextVariable()) != "")
      {
        cout << section->getFullVariableName(variable)
             << "=" << section->getValue(variable) << endl;
      }
    
      ConfigSection * nextSection;
    
      while ((nextSection = section->getNextSection()) != NULL)
      {
        PrintSection(nextSection);
      }
    }
    
    
    
    
    // Demonstrates configuration file operations
    void testConfigFile(ConfigFile *config)
    {
      cout << "--------------------------" << endl;
      cout << "Demonstating ConfigFile"    << endl;
      cout << "--------------------------" << endl;
    
      ConfigSection * section = config->getRootSection();
      PrintSection(section);
    
      cout << endl;
    
      string dir = "/tmp/work";
      cout << "dir is now " << dir << endl;
    
      section = config->findSection("TIGR");
      if (section && section->hasVariable("WORKDIR"))
      {
        dir = section->getValue("WORKDIR");
        cout << "TIGR dir is:" << dir << endl;
      }
    
      section = config->findSection("NIH");
      if (section && section->hasVariable("WORKDIR"))
      {
        dir = section->getValue("WORKDIR");
        cout << "NIH dir is:" << dir << endl;
      }
    
      cout << "dir is now " << dir << endl;
    }
    
    
    
    
    int main(int argc, char ** argv)
    {
      string helptext = "Foundation config file example";
      string version = "Foundation example-configfile 1.0";
      string dependencies = "Foundation";
    
      try
      {
        Foundation *app = new Foundation (version, 
                                                    helptext, 
                                                    dependencies,
                                                    argc, 
                                                    argv);
    
        // Try to parse options and a config file as necessary
        app->handleStandardOptions();
    
        // Main Program
        // Since --configfile is a standard option, one can getConfig
        // as soon as handleStandardOptions has been called to see if a configfile
        // was specified on the command line
        
        if (app->getConfig()) testConfigFile(app->getConfig());
    
        // Delete the Foundation object
        delete app;
      }
      catch (ExitProgramNormally e)
      {
        // non fatal error (help, version, or dependencies were requested)
      }
      catch (Exception e)
      {
        // fatal error
        
        cerr << e << endl;
        exit (100);
      }
    
      return 0;
    }