MexModelManager.C
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 #include "Matlab/MexModelManager.H"
00039
00040 #include "Matlab/mexLog.H"
00041 #include "Neuro/NeuroOpts.H"
00042 #include "Util/fpe.H"
00043 #include "Util/sformat.H"
00044
00045 namespace
00046 {
00047 void mexPrintUsageAndExit(const char *usage)
00048 {
00049 mexFatal(sformat("USAGE: %s\nTry: %s('--help'); "
00050 "for additional information.",
00051 usage,mexFunctionName()));
00052 }
00053 }
00054
00055
00056 MexModelManager::MexModelManager(const std::string& descrName,
00057 const std::string& tagName)
00058 : ModelManager(descrName,tagName,false,false,false)
00059 {
00060 this->setFPE(false);
00061 this->unRequestTestMode();
00062 this->unRequestUsingFPE();
00063 fpExceptionsUnlock();
00064 fpExceptionsOff();
00065 fpExceptionsLock();
00066 }
00067
00068 MexModelManager::~MexModelManager()
00069 {}
00070
00071
00072 bool MexModelManager::parseMexCommandLine(const int nrhs, const mxArray *prhs[],
00073 const char *usage, const int minarg,
00074 const int maxarg)
00075 {
00076
00077
00078
00079 exportOptions(MC_RECURSE);
00080
00081
00082 clearExtraArgs();
00083 extraMexArgs.clear();
00084
00085
00086 std::vector<char*> buffers;
00087 std::vector<const char*> args;
00088 args.push_back(mexFunctionName());
00089
00090
00091 for (int n = 0; n < nrhs; n++)
00092 {
00093
00094 if (!mxIsChar(prhs[n])) {
00095 extraMexArgs.push_back(prhs[n]); continue; }
00096
00097
00098 int numelem = mxGetNumberOfElements(prhs[n]);
00099 if (numelem == 0) {
00100 extraMexArgs.push_back(prhs[n]); continue; }
00101
00102
00103 int buflen = numelem * sizeof(mxChar) + 1;
00104 char *buf = new char[buflen];
00105 mxGetString(prhs[n], buf, buflen);
00106 buffers.push_back(buf);
00107
00108
00109 if (buf[0] != '-') {
00110 extraMexArgs.push_back(prhs[n]); continue; }
00111
00112
00113 for (char *ptr = buf; ptr < (buf+numelem); ++ptr)
00114 {
00115 args.push_back(ptr);
00116
00117 while(!isspace(*ptr) && (ptr < (buf+numelem))) ++ptr;
00118 *ptr = '\0';
00119 }
00120 }
00121
00122
00123 bool ret = parseCommandLineCore(args.size(), &args[0]);
00124
00125
00126 for (uint i = 0; i < buffers.size(); ++i)
00127 delete [] buffers[i];
00128
00129
00130 if (ret == false) return false;
00131
00132
00133 if ((int(extraMexArgs.size()) < minarg) ||
00134 (maxarg >= 0 && int(extraMexArgs.size()) > maxarg))
00135 {
00136 if (maxarg == -1)
00137 mexError(sformat("Incorrect number of (non-opt) arg: %"ZU" [%d..Inf]",
00138 extraMexArgs.size(), minarg));
00139 else
00140 mexError(sformat("Incorrect number of (non-opt) arg: %"ZU" [%d..%d]",
00141 extraMexArgs.size(), minarg, maxarg));
00142 mexPrintUsageAndExit(usage);
00143 }
00144
00145
00146 if (this->numExtraArgs() > 0)
00147 {
00148 for (uint i = 0; i < this->numExtraArgs(); ++i)
00149 mexError(sformat("Unknown command line option: %s",
00150 this->getExtraArg(i).c_str()));
00151
00152 mexError("There were unknown command line options.");
00153 mexPrintUsageAndExit(usage);
00154 }
00155
00156
00157 return true;
00158 }
00159
00160
00161 uint MexModelManager::numExtraMexArgs() const
00162 { return extraMexArgs.size(); }
00163
00164
00165 const mxArray* MexModelManager::getExtraMexArg(const uint num) const
00166 {
00167 if (num >= extraMexArgs.size()) {
00168 mexFatal(sformat("Invalid arg number %d (0..%"ZU") -- IGNORED",
00169 num, extraMexArgs.size()));
00170 return 0;
00171 }
00172 return extraMexArgs[num];
00173 }
00174
00175
00176 void MexModelManager::exitMexFunction(int return_code)
00177 {
00178 if (return_code == 0)
00179 mexFatal(sformat("%s finished.",mexFunctionName()));
00180 else
00181 mexFatal(sformat("%s exited with error code %d.",
00182 mexFunctionName(),return_code));
00183 }
00184
00185
00186
00187
00188
00189