Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

vgetconfig.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 //
00020 // vgetconfig.C
00021 //
00022 
00023 // Read from the vesta.cfg file; for use in scripts.  See VestaConfig.H.
00024 
00025 // Usage:
00026 //  vgetconfig [-l location] [-L] [-i] [-b] [-f] [section name]
00027 
00028 // If section and name are provided, and -i is not specified, print
00029 // VestaConfig::get_Text(section, name).  If section and name are
00030 // provided, and -i is specified, print VestaConfig::get_int(section,
00031 // name).
00032 
00033 // Other options:
00034 // -l location 
00035 //   Use the given location for the config file, overriding the default.
00036 // -L 
00037 //   Print the config file location.
00038 
00039 // If there is nothing to do, a usage message is printed.
00040 
00041 #include <Basics.H>
00042 #include <Text.H>
00043 #include <VestaConfig.H>
00044 
00045 #if !defined(__sun__)
00046 extern "C" {
00047 #include <getopt.h>
00048 }
00049 #endif
00050 
00051 using std::cerr;
00052 using std::cout;
00053 using std::endl;
00054 
00055 Text program_name;
00056 extern const char *Version;
00057 
00058 void
00059 Usage()
00060 {
00061     cerr << "Usage: " << program_name << endl <<
00062       "        [-l location] [-L] [-V]" << endl <<
00063       "        [-i] [-b] [-f]" << endl <<
00064       "        [section name [section name ...]]" << endl << endl;
00065     exit(1);
00066 }
00067 
00068 int
00069 main(int argc, char* argv[])
00070 {
00071   typedef enum { get_text, get_int, get_bool, get_float } get_t;
00072     try {
00073         Text location, section, name;
00074         bool default_location = true, print_location = false, print_value = true;
00075         bool print_version = false, print_sect = false;
00076         int c;
00077         get_t get_type = get_text;
00078         // 
00079         // Parse command line
00080         //
00081         program_name = argv[0];
00082         opterr = 0;
00083         for (;;) {
00084             c = getopt(argc, argv, "l:ibfLVs");
00085             if (c == EOF) break;
00086             switch (c) {
00087               case 'l':
00088                 location = optarg;
00089                 default_location = false;
00090                 break;
00091               case 'i':
00092                 get_type = get_int;
00093                 break;
00094               case 'b':
00095                 get_type = get_bool;
00096                 break;
00097               case 'f':
00098                 get_type = get_float;
00099                 break;
00100               case 'L':
00101                 print_location = true;
00102                 break;
00103               case 'V':
00104                 print_version = true;
00105                 break;
00106               case 's':
00107                 print_sect = true;
00108                 break;
00109               case '?':
00110               default:
00111                 Usage();
00112             }
00113         }
00114         
00115         if (optind == argc) {
00116             if (!print_location && !print_version && !print_sect) {
00117                 cerr << program_name << ": nothing to do\n" << endl;
00118                 Usage();
00119             }
00120             print_value = false;
00121         } else if ((argc - optind) == 1) {
00122           if(!print_sect)
00123             Usage();
00124           print_value = false;
00125         } else if ((argc - optind) & 0x1) {
00126             Usage();
00127         }
00128 
00129         if (!default_location) {
00130             VestaConfig::set_location(location);
00131         }           
00132 
00133         if (print_version) {
00134             cout << program_name << ": version " << Version << endl;
00135         }
00136 
00137         if (print_location) {
00138             cout << VestaConfig::get_location() << endl;
00139         }
00140 
00141         if(print_sect)
00142           {
00143             TextSeq result;
00144             if(argc > optind)
00145               result = VestaConfig::section_vars(argv[optind]);
00146             else
00147               result = VestaConfig::sections();
00148             while(result.size())
00149               {
00150                 cout << result.remlo() << endl;
00151               }
00152           }
00153 
00154         if(print_value)
00155           {
00156             for(;optind < argc;optind += 2) {
00157                 section = argv[optind];
00158                 name = argv[optind + 1];
00159                 switch(get_type)
00160                   {
00161                   case get_text:
00162                     cout << VestaConfig::get_Text(section, name) << endl;
00163                     break;
00164                   case get_int:
00165                     cout << VestaConfig::get_int(section, name) << endl;
00166                     break;
00167                   case get_bool:
00168                     cout << (VestaConfig::get_bool(section, name)
00169                          ? "true" : "false")
00170                          << endl;
00171                     break;
00172                   case get_float:
00173                     cout << VestaConfig::get_float(section, name) << endl;
00174                     break;
00175                   }
00176              }
00177           }
00178 
00179     } catch (VestaConfig::failure f) {
00180         cerr << program_name << ": " << f.msg << endl;
00181         exit(2);
00182     }
00183     
00184     return 0;
00185 }
00186 
00187 

Generated on Mon May 8 00:48:37 2006 for Vesta by  doxygen 1.4.2