00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
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
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;
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
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 }