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 #include "examples/support.hh"
00040
00041 #include <iostream>
00042 #include <iomanip>
00043
00044 #include <cstdlib>
00045 #include <cstring>
00046
00047 void
00048 StringOption::add(int v, const char* o, const char* h) {
00049 Value* n = new Value;
00050 n->val = v;
00051 n->opt = o;
00052 n->help = h;
00053 n->next = NULL;
00054 if (fst == NULL) {
00055 fst = n;
00056 } else {
00057 lst->next = n;
00058 }
00059 lst = n;
00060 }
00061
00062 bool
00063 StringOption::parse(int& argc, char* argv[]) {
00064 if ((argc < 2) || strcmp(argv[1],opt))
00065 return false;
00066 if (argc == 2) {
00067 std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
00068 exit(EXIT_FAILURE);
00069 }
00070 for (Value* v = fst; v != NULL; v = v->next)
00071 if (!strcmp(argv[2],v->opt)) {
00072 cur = v->val;
00073
00074 argc -= 2;
00075 for (int i=1; i<argc; i++)
00076 argv[i] = argv[i+2];
00077 return true;
00078 }
00079 std::cerr << "Wrong argument \"" << argv[2]
00080 << "\" for option \"" << opt << "\""
00081 << std::endl;
00082 exit(EXIT_FAILURE);
00083 }
00084
00085 void
00086 StringOption::help(void) {
00087 if (fst == NULL)
00088 return;
00089 std::cerr << '\t' << opt << " (";
00090 const char* d = NULL;
00091 for (Value* v = fst; v != NULL; v = v->next) {
00092 std::cerr << v->opt << ((v->next != NULL) ? ", " : "");
00093 if (v->val == cur)
00094 d = v->opt;
00095 }
00096 std::cerr << ")";
00097 if (d != NULL)
00098 std::cerr << " default: " << d;
00099 std::cerr << std::endl << "\t\t" << exp << std::endl;
00100 for (Value* v = fst; v != NULL; v = v->next)
00101 if (v->help != NULL)
00102 std::cerr << "\t\t " << v->opt << ": " << v->help << std::endl;
00103 }
00104
00105 StringOption::~StringOption(void) {
00106 Value* v = fst;
00107 while (v != NULL) {
00108 Value* n = v->next;
00109 delete v;
00110 v = n;
00111 }
00112 }
00113
00114
00115 bool
00116 UnsignedIntOption::parse(int& argc, char* argv[]) {
00117 if ((argc < 2) || strcmp(argv[1],opt))
00118 return false;
00119 if (argc == 2) {
00120 std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
00121 exit(EXIT_FAILURE);
00122 }
00123 cur = atoi(argv[2]);
00124
00125 argc -= 2;
00126 for (int i=1; i<argc; i++)
00127 argv[i] = argv[i+2];
00128 return true;
00129 }
00130
00131 void
00132 UnsignedIntOption::help(void) {
00133 using namespace std;
00134 cerr << '\t' << opt << " (unsigned int) default: " << cur << endl
00135 << "\t\t" << exp << endl;
00136 }
00137
00138
00139
00140 Options::Options(const char* n)
00141 : fst(NULL), lst(NULL),
00142
00143 _name(n),
00144
00145 _model("-model","model variants"),
00146 _propagation("-propagation","propagation variants"),
00147 _pk("-pk","propagation kind",PK_DEF),
00148 _icl("-icl","integer consistency level",ICL_DEF),
00149 _branching("-branching","branching variants"),
00150
00151 _search("-search","search engine variants"),
00152 _solutions("-solutions","number of solutions (0 = all)",1),
00153 _c_d("-c-d","recomputation copy distance",Search::Config::c_d),
00154 _a_d("-a-d","recomputation adaption distance",Search::Config::a_d),
00155 _fail("-fail","failure cutoff (0 = none, solution mode)"),
00156 _time("-time","time (in ms) cutoff (0 = none, solution mode)"),
00157
00158 _mode("-mode","how to execute example",EM_SOLUTION),
00159 _samples("-samples","how many samples (time mode)",1),
00160 _iterations("-iterations","iterations per sample (time mode)",1)
00161 {
00162
00163 _pk.add(PK_DEF, "def"); _pk.add(PK_SPEED, "speed");
00164 _pk.add(PK_MEMORY, "memory");
00165
00166 _icl.add(ICL_DEF, "def"); _icl.add(ICL_VAL, "val");
00167 _icl.add(ICL_BND, "bnd"); _icl.add(ICL_DOM, "dom");
00168
00169 _mode.add(EM_SOLUTION, "solution");
00170 _mode.add(EM_TIME, "time");
00171 _mode.add(EM_STAT, "stat");
00172 #ifdef GECODE_HAS_GIST
00173 _mode.add(EM_GIST, "gist");
00174 #endif
00175
00176 add(_model); add(_propagation); add(_pk); add(_icl); add(_branching);
00177 add(_search); add(_solutions); add(_c_d); add(_a_d); add(_fail); add(_time);
00178 add(_mode); add(_iterations); add(_samples);
00179 }
00180
00181 void
00182 Options::help(void) {
00183 std::cerr << "Options for " << name() << ":" << std::endl
00184 << "\t-help, --help, -?" << std::endl
00185 << "\t\tprint this help message" << std::endl;
00186 for (BaseOption* o = fst; o != NULL; o = o->next)
00187 o->help();
00188 }
00189
00190 void
00191 Options::parse(int& argc, char* argv[]) {
00192 next:
00193 for (BaseOption* o = fst; o != NULL; o = o->next)
00194 if (o->parse(argc,argv))
00195 goto next;
00196 if (argc < 2)
00197 return;
00198 if (!strcmp(argv[1],"-help") || !strcmp(argv[1],"--help") ||
00199 !strcmp(argv[1],"-?")) {
00200 help();
00201 exit(EXIT_SUCCESS);
00202 }
00203 return;
00204 }
00205
00206
00207 SizeOptions::SizeOptions(const char* e)
00208 : Options(e), _size(0) {}
00209
00210 void
00211 SizeOptions::help(void) {
00212 Options::help();
00213 std::cerr << "\t(unsigned int) default: " << size() << std::endl
00214 << "\t\twhich version/size for example" << std::endl;
00215 }
00216
00217 void
00218 SizeOptions::parse(int& argc, char* argv[]) {
00219 Options::parse(argc,argv);
00220 if (argc < 2)
00221 return;
00222 size(atoi(argv[1]));
00223 }
00224
00225
00226