init_config_options.cpp

This example demonstrates the use of run-time options. This code is used for other examples.

00001 /*<std-header orig-src='shore'>
00002 
00003  $Id: init_config_options.cpp,v 1.4 2010/06/24 17:08:14 nhall Exp $
00004 
00005 SHORE -- Scalable Heterogeneous Object REpository
00006 
00007 Copyright (c) 1994-99 Computer Sciences Department, University of
00008                       Wisconsin -- Madison
00009 All Rights Reserved.
00010 
00011 Permission to use, copy, modify and distribute this software and its
00012 documentation is hereby granted, provided that both the copyright
00013 notice and this permission notice appear in all copies of the
00014 software, derivative works or modified versions, and any portions
00015 thereof, and that both notices appear in supporting documentation.
00016 
00017 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
00018 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
00019 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
00020 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00021 
00022 This software was developed with support by the Advanced Research
00023 Project Agency, ARPA order number 018 (formerly 8230), monitored by
00024 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
00025 Further funding for this work was provided by DARPA through
00026 Rome Research Laboratory Contract No. F30602-97-2-0247.
00027 
00028 */
00029 
00030 #include "w_defines.h"
00031 
00032 /*  -- do not edit anything above this line --   </std-header>*/
00033 
00034 /**\anchor init_config_options_example */
00035 /*
00036  * This file implements configuration option processing for
00037  * both the client and the server.
00038  */
00039 
00040 // since this file only deals with the SSM option package,
00041 // rather than including sm_vas.h, just include what's needed for
00042 // options:
00043 #include <w_stream.h>
00044 #include <cstring>
00045 #include "w.h"
00046 #include "option.h"
00047 #include <w_strstream.h>
00048 
00049 /*
00050  * init_config_options intializes configuration options 
00051  *
00052  * The options parameter is the option group holding all the options.
00053  * It is assumed that all SSM options have been added if called
00054  * by the server.
00055  *
00056  * The prog_type parameter is should be either "client" or "server".
00057  *
00058  * The argc and argv parameters should be argc and argv from main().
00059  * Recognized options will be located in argv and removed.  argc
00060  * is changed to reflect the removal.
00061  *
00062  */
00063 
00064 w_rc_t
00065 init_config_options(option_group_t& options,
00066             const char* prog_type,
00067             int& argc, char** argv)
00068 {
00069 
00070     w_rc_t rc;    // return code
00071 
00072     // set prog_name to the file name of the program without the path
00073     char* prog_name = strrchr(argv[0], '/');
00074     if (prog_name == NULL) {
00075         prog_name = argv[0];
00076     } else {
00077         prog_name += 1; /* skip the '/' */
00078         if (prog_name[0] == '\0')  {
00079             prog_name = argv[0];
00080         }
00081     }
00082  
00083     W_DO(options.add_class_level("example")); 
00084     W_DO(options.add_class_level(prog_type));    // server or client
00085     W_DO(options.add_class_level(prog_name));    // program name
00086 
00087     // read the example config file to set options
00088     {
00089         w_ostrstream      err_stream;
00090         const char* opt_file = "EXAMPLE_SHORECONFIG";     // option config file
00091         option_file_scan_t opt_scan(opt_file, &options);
00092 
00093         // scan the file and override any current option settings
00094         // options names must be spelled correctly
00095         rc = opt_scan.scan(true /*override*/, err_stream, true);
00096         if (rc.is_error()) {
00097             cerr << "Error in reading option file: " << opt_file << endl;
00098             cerr << "\t" << err_stream.c_str() << endl;
00099             return rc;
00100         }
00101     }
00102 
00103     // parse argv for options
00104     if (!rc.is_error()) {
00105         // parse command line
00106         w_ostrstream      err_stream;
00107         rc = options.parse_command_line((const char **)argv, 
00108                 argc, 2, &err_stream);
00109         err_stream << ends;
00110         if (rc.is_error()) {
00111             cerr << "Error on Command line " << endl;
00112             cerr << "\t" << w_error_t::error_string(rc.err_num()) << endl;
00113             cerr << "\t" << err_stream.c_str() << endl;
00114         return rc;
00115         }
00116     }
00117  
00118     // check required options
00119     {
00120         w_ostrstream      err_stream;
00121         rc = options.check_required(&err_stream);
00122         if (rc.is_error()) {
00123             cerr << "These required options are not set:" << endl;
00124             cerr << err_stream.c_str() << endl;
00125             return rc;
00126         }
00127     } 
00128 
00129     return RCOK;
00130 }

Generated on Wed Jul 7 17:22:31 2010 for Shore Storage Manager by  doxygen 1.4.7