1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
* Copyright (c) 2002-2006 Samit Basu
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "Array.hpp"
#include "Interpreter.hpp"
#include <QtCore>
#include "Algorithms.hpp"
#include "Struct.hpp"
//@@Signature
//function p_end EndFunction
//inputs x dim subindexes
//outputs y
//DOCBLOCK inspection_end
ArrayVector EndFunction(int nargout, const ArrayVector& arg) {
if (arg.size() != 3)
throw Exception("End function requires 3 arguments, the array, the end index, and the number of subindexes");
NTuple t(arg[0].dimensions());
index_t enddim(arg[1].asDouble());
index_t totalndxs(arg[2].asDouble());
if (totalndxs == 1)
return ArrayVector(Array(index_t(arg[0].length())));
return ArrayVector(Array(index_t(t[int(enddim-1)])));
}
//@@Signature
//sfunction who WhoFunction
//inputs varargin
//outputs none
//DOCBLOCK inspection_who
ArrayVector WhoFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
StringVector names;
Context *context = eval->getContext();
// Bypass our context (the who(who) one)
ParentScopeLocker lock(context);
// Search upwards until we find an active scope
int bypasscount = 0;
while (!context->isScopeActive()) {
bypasscount++;
context->bypassScope(1);
}
if (arg.size() == 0)
names = eval->getContext()->listAllVariables();
else
for (int i=0;i<arg.size();i++)
names.push_back(arg[i].asString());
qSort(names.begin(),names.end());
eval->outputMessage(" Variable Name Type Flags Size\n");
for (int i=0;i<names.size();i++) {
Array lookup;
ArrayReference ptr;
eval->outputMessage(names[i].rightJustified(15,' ',false));
ptr = eval->getContext()->lookupVariable(names[i]);
if (!ptr.valid())
eval->outputMessage(" <undefined>");
else {
lookup = *ptr;
eval->outputMessage(lookup.className().rightJustified(10,' ',false));
if (lookup.isSparse())
eval->outputMessage(" sparse");
else
eval->outputMessage(" ");
if (eval->getContext()->isVariableGlobal(names[i])) {
eval->outputMessage(" global ");
} else if (eval->getContext()->isVariablePersistent(names[i])) {
eval->outputMessage(" persist ");
} else {
eval->outputMessage(" ");
}
eval->outputMessage(QString(" [") +
lookup.dimensions().toString() +
QString("]"));
}
eval->outputMessage("\n");
}
context->restoreScope(bypasscount);
return ArrayVector();
}
//@@Signature
//sfunction whos WhosFunction
//inputs varargin
//outputs none
//DOCBLOCK inspection_whos
ArrayVector WhosFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
StringVector names;
Context *context = eval->getContext();
// Bypass our context (the who(who) one)
ParentScopeLocker lock(context);
// Search upwards until we find an active scope
int bypasscount = 0;
while (!context->isScopeActive()) {
bypasscount++;
context->bypassScope(1);
}
if (arg.size() == 0)
names = eval->getContext()->listAllVariables();
else
for (int i=0;i<arg.size();i++)
names.push_back(arg[i].asString());
qSort(names.begin(),names.end());
eval->outputMessage(" Variable Name Type Flags Size Bytes\n");
for (int i=0;i<names.size();i++) {
Array lookup;
ArrayReference ptr;
eval->outputMessage(names[i].rightJustified(15,' ',false));
ptr = eval->getContext()->lookupVariable(names[i]);
if (!ptr.valid())
eval->outputMessage(" <undefined>");
else {
lookup = *ptr;
eval->outputMessage(lookup.className().rightJustified(10,' ',false));
if (lookup.isSparse())
eval->outputMessage(" sparse");
else
eval->outputMessage(" ");
if (eval->getContext()->isVariableGlobal(names[i])) {
eval->outputMessage(" global ");
} else if (eval->getContext()->isVariablePersistent(names[i])) {
eval->outputMessage(" persist ");
} else {
eval->outputMessage(" ");
}
QString txt(QString(" [") +
lookup.dimensions().toString() +
QString("]"));
eval->outputMessage(txt.leftJustified(15,' ',false));
eval->outputMessage(QString(" %1").arg(lookup.bytes()));
}
eval->outputMessage("\n");
}
context->restoreScope(bypasscount);
return ArrayVector();
}
//@@Signature
//function fieldnames FieldNamesFunction
//inputs y
//outputs x
//DOCBLOCK inspection_fieldnames
ArrayVector FieldNamesFunction(int nargout, const ArrayVector& arg) {
if (arg.size() < 1)
throw Exception("fieldnames function requires at least one argument");
if (arg[0].dataClass() != Struct)
return ArrayVector(Array(CellArray,NTuple(0,0)));
StringVector names(FieldNames(arg[0]));
ArrayMatrix m;
for (int i=0;i<names.size();i++)
m.push_back(ArrayVector(Array(names.at(i))));
return ArrayVector(CellConstructor(m));
}
//@@Signature
//sfunction where WhereFunction
//inputs none
//outputs none
//DOCBLOCK inspection_where
ArrayVector WhereFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
eval->stackTrace(1);
return ArrayVector();
}
ArrayVector AddrFunction(int nargout, const ArrayVector& arg) {
return ArrayVector(Array(arg[0].address()));
}
#define LOOKUP(x,field) x.constStructPtr()[field].get(1)
//@@Signature
//sfunction nargin NarginFunction
//inputs funcname
//outputs count
//DOCBLOCK functions_nargin
ArrayVector NarginFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
if (arg.size() == 0)
{
Context *ctxt = eval->getContext();
ParentScopeLocker lock(ctxt);
int nargin = ctxt->scopeNargin();
return ArrayVector() << Array(double(nargin));
}
Array a = arg[0];
QString txt;
if (a.className() == "functionpointer")
txt = LOOKUP(a,"name").asString();
else
txt = arg[0].asString();
FuncPtr val;
if (!eval->lookupFunction(txt,val))
throw Exception("Unable to resolve " + txt + " to a function call");
return Array(double(val->inputArgCount()));
}
//@@Signature
//sfunction nargout NargoutFunction
//inputs func
//outputs count
//DOCBLOCK functions_nargout
ArrayVector NargoutFunction(int, const ArrayVector&arg, Interpreter* eval) {
if (arg.size() == 0)
{
Context *ctxt = eval->getContext();
ParentScopeLocker lock(ctxt);
int nargout = ctxt->scopeNargout();
return ArrayVector() << Array(double(nargout));
}
Array a = arg[0];
QString txt;
if (a.className() == "functionpointer")
txt = LOOKUP(a,"name").asString();
else
txt = arg[0].asString();
FuncPtr val;
if (!eval->lookupFunction(txt,val))
throw Exception("Unable to resolve " + txt + " to a function call");
return Array(double(val->outputArgCount()));
}
//@@Signature
//sfunction which WhichFunction
//inputs functionname
//outputs location
//DOCBLOCK inspection_which
ArrayVector WhichFunction(int nargout, const ArrayVector& arg,
Interpreter* eval) {
if (arg.size() != 1)
throw Exception("which function takes one string argument (the name of the function to look up)");
QString fname = arg[0].asString();
bool isFun;
FuncPtr val;
isFun = eval->lookupFunction(fname,val);
Array ret(Double,NTuple(0,0));
if (isFun) {
if (val->type() == FM_M_FUNCTION) {
MFunctionDef *mptr;
mptr = (MFunctionDef *) val;
try {
mptr->updateCode(eval);
} catch (Exception &e) {}
if (mptr->pcodeFunction) {
if (mptr->scriptFlag) {
if (nargout == 0) {
eval->outputMessage("Function "+fname+", P-code script\n");
}
} else {
if (nargout == 0) {
eval->outputMessage("Function "+fname+", P-code function\n");
}
}
} else {
if (mptr->scriptFlag) {
if (nargout == 0) {
eval->outputMessage("Function "+fname+", M-File script in file '"+mptr->fileName+"'\n");
} else
ret = Array(mptr->fileName);
} else {
if (nargout == 0) {
eval->outputMessage("Function "+fname+", M-File function in file '"+mptr->fileName+"'\n");
} else
ret = Array(mptr->fileName);
}
}
} else if ((val->type() == FM_BUILT_IN_FUNCTION) ||
(val->type() == FM_SPECIAL_FUNCTION) ) {
if (nargout == 0) {
eval->outputMessage("Function "+fname+" is a built in function\n");
}
} else {
if (nargout == 0) {
eval->outputMessage("Function "+fname+" is an imported function\n");
}
}
} else {
if (nargout == 0) {
eval->outputMessage("Function "+fname+" is unknown!\n");
}
}
if (nargout > 0)
return ArrayVector(ret);
else
return ArrayVector();
}
//@@Signature
//sfunction mfilename MFilenameFunction
//inputs none
//outputs filename
//DOCBLOCK freemat_mfilename
ArrayVector MFilenameFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
return ArrayVector(Array(QFileInfo(eval->getMFileName()).fileName()));
}
//@@Signature
//function computer ComputerFunction
//inputs none
//outputs str
//DOCBLOCK freemat_computer
ArrayVector ComputerFunction(int nargout, const ArrayVector& arg) {
#ifdef WIN32
return ArrayVector(Array(QString("PCWIN")));
#elif defined(__APPLE__)
return ArrayVector(Array(QString("MAC")));
#else
return ArrayVector(Array(QString("UNIX")));
#endif
}
|