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

paramproc.cxx

Go to the documentation of this file.
00001 /**************************************************************************
00002 ***    
00003 *** Copyright (c) 1995-2000 Regents of the University of California,
00004 ***               Andrew E. Caldwell, Andrew B. Kahng and Igor L. Markov
00005 *** Copyright (c) 2000-2004 Regents of the University of Michigan,
00006 ***               Saurabh N. Adya, Jarrod A. Roy and Igor L. Markov
00007 ***
00008 ***  Contact author(s): abk@cs.ucsd.edu, imarkov@umich.edu
00009 ***  Original Affiliation:   UCLA, Computer Science Department,
00010 ***                          Los Angeles, CA 90095-1596 USA
00011 ***
00012 ***  Permission is hereby granted, free of charge, to any person obtaining 
00013 ***  a copy of this software and associated documentation files (the
00014 ***  "Software"), to deal in the Software without restriction, including
00015 ***  without limitation 
00016 ***  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
00017 ***  and/or sell copies of the Software, and to permit persons to whom the 
00018 ***  Software is furnished to do so, subject to the following conditions:
00019 ***
00020 ***  The above copyright notice and this permission notice shall be included
00021 ***  in all copies or substantial portions of the Software.
00022 ***
00023 *** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
00024 *** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00025 *** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
00026 *** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00027 *** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
00028 *** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
00029 *** THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00030 ***
00031 ***
00032 ***************************************************************************/
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 // Created:  June 15, 1997  Igor Markov
00042 
00043 // 970622 ilm    added NOPARAM
00044 // 970820 ilm    moved enum ParamType inside class Param
00045 //               added new ctor for use with NOPARAM
00046 //               allowed for +option as well as -option
00047 //               added member bool on() to tell if it was +option
00048 //               added abkfatal(found(),...) when reading param, value
00049 // 970824 ilm    changed the uninitialized value for int to MAX_INT
00050 // 980313 ilm    fixed const-correctness
00051 
00052 
00053 
00054 #ifdef _MSC_VER
00055 #pragma warning(disable:4786)
00056 #endif
00057 
00058 
00059 #include <stdlib.h>
00060 #include <limits.h>
00061 #include "abkstring.h"
00062 #include "paramproc.h"
00063 #include "abkassert.h"
00064 static char _uninitialized[]="Uninitialized";
00065 
00066 //using namespace std;
00067 
00068 Param::Param(Type pt,int argc, const char * const argv[])
00069 : _b(false), _on(false), _i(INT_MAX), _u(unsigned(-1)), _d(-1.29384756657), 
00070   _s(_uninitialized), _pt(pt), _key("")
00071 {
00072   abkfatal(pt==NOPARAM," This constructor can only work with Param:NOPARAM\n");
00073   (void)argv; // please compiler 
00074    
00075   _b=(argc<2?true:false);
00076   return;
00077 } 
00078 
00079 Param::Param (const char * key, Type pt,int argc, const char * const argv[])
00080 : _b(false), _on(false), _i(-1), _u(unsigned(-1)), _d(-1.23456), 
00081   _s(_uninitialized), _pt(pt), _key(key)
00082 {
00083   abkfatal(strlen(_key)>0," Zero length key for command line parameter");
00084   
00085   int n=0;
00086   if (_pt==NOPARAM)
00087   {
00088      if (argc<2)  _b=true; 
00089      else _b=false;
00090      return;
00091   }
00092   while ( ++n < argc && ! found())
00093   {
00094      if (argv[n][0]=='-' || argv[n][0]=='+')
00095      {
00096          const char * start=argv[n]+1;
00097          if (argv[n][0]=='-')
00098             {
00099                 if (argv[n][1]=='-') start++;
00100             }
00101          else
00102             _on=true;  
00103 
00104          if (strcasecmp(start,_key)==0)
00105          {
00106               _b=true;
00107               if (n+1 < argc)
00108               {
00109                  char tmp[80];
00110                  strncpy(tmp,argv[n+1],80);
00111                  switch (_pt)
00112                  {
00113                     case BOOL     : break;
00114                     case INT      : _i=atoi(argv[n+1]); break;
00115                     case UNSIGNED : _u=strtoul(argv[n+1],(char**)NULL,10);
00116                                                         break;
00117                     case DOUBLE   : _d=atof(argv[n+1]); break;
00118                     case STRING   : _s=argv[n+1];       break;
00119                     default : abkfatal(0," Unknown command line parameter");
00120                  }
00121               }
00122           }
00123       }
00124   }
00125 }
00126 
00127 bool      Param::found()       const
00128 { return _b; }
00129 
00130 bool      Param::on()          const // true for +option, false otherwise
00131 { 
00132   abkfatal(found()," Parameter not found: you need to check for this first\n");
00133   return _on;
00134 }
00135 
00136 int       Param::getInt()      const
00137 { 
00138    abkfatal(_pt==INT," Parameter is not INT ");
00139    abkfatal(found()," Parameter not found: you need to check for this first\n");
00140    return _i;
00141 }
00142 
00143 unsigned  Param::getUnsigned() const
00144 { 
00145    abkfatal(_pt==UNSIGNED," Parameter is not UNSIGNED "); 
00146    abkfatal(found(),
00147       " UNSIGNED Parameter not found: you need to check for this first\n");
00148    return _u;
00149 }
00150 
00151 double    Param::getDouble()   const
00152 { 
00153    abkfatal(_pt==DOUBLE," Parameter is not DOUBLE "); 
00154    abkfatal(found(),
00155       " DOUBLE parameter not found: you need to check for this first\n");
00156    return _d;
00157 }
00158 
00159 const char * Param::getString()   const
00160 { 
00161    abkfatal(_pt==STRING," Parameter is not STRING"); 
00162    abkfatal(found(),
00163       " STRING parameter not found: you need to check for this first\n");
00164    return _s;
00165 }

Generated on Mon Apr 25 01:09:24 2005 for Parquete by doxygen 1.3.2