Go to the documentation of this file.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
00055 #if !defined(ALIZE_ConfigChecker_cpp)
00056 #define ALIZE_ConfigChecker_cpp
00057
00058 #include "ConfigChecker.h"
00059 #include "Config.h"
00060 #include "Exception.h"
00061 #include <new>
00062
00063 using namespace alize;
00064 typedef ConfigChecker CC;
00065
00066
00067 CC::ConfigChecker()
00068 :Object() {}
00069
00070 void CC::check(const Config& config)
00071 {
00072 unsigned long i;
00073 for (i=0; i<_vect.size(); i++)
00074 {
00075 const Param& c = _vect.getObject(i);
00076 if (!config.existsParam(c.name))
00077 {
00078 if (c.mandatory)
00079 err("Parameter '" + c.name + "' mandatory in the config");
00080 }
00081 else
00082 {
00083 const String& content = config.getParam(c.name);
00084 if (c.type != PARAMTYPE_BOOLEAN && c.argIsRequired
00085 && content.getToken(0) == "")
00086 err("Parameter '" + c.name + "' argument required");
00087
00088 if (c.type == PARAMTYPE_INTEGER)
00089 {
00090 try { content.toLong(); }
00091 catch (Exception&)
00092 {
00093 err("Value '" + content + "' of parameter '"
00094 + c.name + "' is not an integer");
00095 }
00096 }
00097 else if (c.type == PARAMTYPE_FLOAT)
00098 {
00099 try { content.toDouble(); }
00100 catch (Exception&)
00101 {
00102 err("Value '" + content + "' of parameter '"
00103 + c.name + "' is not a float");
00104 }
00105 }
00106 else if (c.type == PARAMTYPE_BOOLEAN)
00107 {
00108 try
00109 {
00110 String l = content;
00111 if (l != "")
00112 l.toBool();
00113 }
00114 catch (Exception&)
00115 {
00116 err("Value '" + content + "' of parameter '"
00117 + c.name + "' is not '' nor 'true' nor 'false'");
00118 }
00119 }
00120 else if (c.type == PARAMTYPE_STRING)
00121 {
00122
00123 }
00124 }
00125 }
00126 }
00127
00128 void CC::err(const String& e) const
00129 { throw ConfigCheckException(e, __FILE__, __LINE__); }
00130
00131 void CC::checkName(const String& name) const
00132 {
00133 if (name.getToken(0) == "")
00134 throw Exception("parameter name cannot be empty", __FILE__, __LINE__);
00135 }
00136
00137 void CC::addIntegerParam(const String& name, bool mandatory,
00138 bool argIsRequired, const String& desc)
00139 {
00140 checkName(name);
00141 Param* p = new (std::nothrow) Param();
00142 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00143 p->name = name;
00144 p->type = PARAMTYPE_INTEGER;
00145 p->mandatory = mandatory;
00146 p->argIsRequired = argIsRequired;
00147 p->description = desc;
00148 _vect.addObject(*p);
00149 }
00150
00151 void CC::addFloatParam(const String& name, bool mandatory,
00152 bool argIsRequired, const String& desc)
00153 {
00154 checkName(name);
00155 Param* p = new (std::nothrow) Param();
00156 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00157 p->name = name;
00158 p->type = PARAMTYPE_FLOAT;
00159 p->mandatory = mandatory;
00160 p->argIsRequired = argIsRequired;
00161 p->description = desc;
00162 _vect.addObject(*p);
00163 }
00164
00165 void CC::addBooleanParam(const String& name, bool mandatory,
00166 bool argIsRequired, const String& desc)
00167 {
00168 checkName(name);
00169 Param* p = new (std::nothrow) Param();
00170 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00171 p->name = name;
00172 p->type = PARAMTYPE_BOOLEAN;
00173 p->mandatory = mandatory;
00174 p->argIsRequired = argIsRequired;
00175 p->description = desc;
00176 _vect.addObject(*p);
00177 }
00178
00179 void CC::addStringParam(const String& name, bool mandatory,
00180 bool argIsRequired, const String& desc)
00181 {
00182 checkName(name);
00183 Param* p = new (std::nothrow) Param();
00184 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00185 p->name = name;
00186 p->type = PARAMTYPE_STRING;
00187 p->mandatory = mandatory;
00188 p->argIsRequired = argIsRequired;
00189 p->description = desc;
00190 _vect.addObject(*p);
00191 }
00192
00193 String CC::getParamList()
00194 {
00195 unsigned long i;
00196 unsigned long length = 9;
00197 for (i=0; i<_vect.size(); i++)
00198 {
00199 unsigned long l = 2+_vect.getObject(i).name.length();
00200 if (l > length)
00201 length = l;
00202 }
00203 String s, n;
00204 n = "--help";
00205 while (n.length() < length) n += " ";
00206 s += n + " Show this help\n";
00207 n = "--version";
00208 while (n.length() < length) n += " ";
00209 s += n + " Show version information\n";
00210
00211 for (i=0; i<_vect.size(); i++)
00212 {
00213 const Param& c = _vect.getObject(i);
00214 n = "--" + c.name;
00215 while (n.length() < length) n += " ";
00216 s += n + " "
00217
00218 + c.description + " "
00219 + "<" + getParamTypeName(c.type) + ">"
00220 + "\n";
00221 }
00222 return s;
00223 }
00224
00225
00226
00227 String CC::getClassName() const { return "ConfigChecker";}
00228
00229 CC::~ConfigChecker() { _vect.deleteAllObjects(); }
00230
00231 String CC::Param::getClassName() const { return "Param"; }
00232
00233 #endif // !defined(ALIZE_ConfigChecker_cpp)