File: Misc.cpp

package info (click to toggle)
freemat 4.2%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 142,116 kB
  • sloc: ansic: 126,788; cpp: 62,015; python: 2,080; perl: 1,255; sh: 1,146; yacc: 1,019; lex: 239; makefile: 107
file content (229 lines) | stat: -rw-r--r-- 7,131 bytes parent folder | download | duplicates (2)
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
/*
 * 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 "Scanner.hpp"
#include "Parser.hpp"
#include "System.hpp"
#include <QtCore>
#include "Algorithms.hpp"
#include "PathSearch.hpp"

//@@Signature
//sfunction simkeys SimKeysFunction
//inputs itext
//outputs otext
//DOCBLOCK freemat_simkeys
ArrayVector SimKeysFunction(int nargout, const ArrayVector& arg,
			    Interpreter* eval) {
  if (arg.size() == 0)
    throw Exception("simkeys requires at least one argument (the cell array of strings to simulate)");
  ParentScopeLocker lock(eval->getContext());
  eval->clearCaptureString();
  eval->setCaptureState(true);
  if (arg[0].dataClass() != CellArray)
    throw Exception("simkeys requires a cell array of strings");
  const BasicArray<Array> &dp(arg[0].constReal<Array>());
  for (index_t i=1;i<=dp.length();i++) {
    QString txt(dp[i].asString());
    if ((txt.size() > 0) && (!txt.endsWith('\n')))
      txt.push_back('\n');
    eval->ExecuteLine(txt);
  }
  eval->ExecuteLine("quit\n");
  try {
    while(1) 
      eval->evalCLI();
  } catch (InterpreterContinueException& e) {
  } catch (InterpreterBreakException& e) {
  } catch (InterpreterReturnException& e) {
  } catch (InterpreterRetallException& e) {
  } catch (InterpreterQuitException& e) {
  }
  eval->setCaptureState(false);
  return ArrayVector(Array(eval->getCaptureString()));
}

//@@Signature
//sfunction diary DiaryFunction
//inputs x
//outputs state
//DOCBLOCK freemat_diary
ArrayVector DiaryFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  if (nargout == 1) {
    if (arg.size() > 0)
      throw Exception("diary function with an assigned return value (i.e, 'x=diary') does not support any arguments");
    return ArrayVector(Array(bool(eval->getDiaryState())));
  }
  if (arg.size() == 0) {
    eval->setDiaryState(!eval->getDiaryState());
    return ArrayVector();
  }
  QString diaryString(arg[0].asString());
  if (diaryString.toLower() == "on")
    eval->setDiaryState(true);
  else if (diaryString.toLower() == "off")
    eval->setDiaryState(false);
  else {
    eval->setDiaryFilename(diaryString);
    eval->setDiaryState(true);
  }
  return ArrayVector();
}


//@@Signature
//sfunction quiet QuietFunction
//inputs mode
//outputs mode
//DOCBLOCK freemat_quiet
ArrayVector QuietFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  if (arg.size() > 0) {
    QString qtype(arg[0].asString().toUpper());
    if (qtype == "NORMAL")
      eval->setQuietLevel(0);
    else if (qtype == "QUIET")
      eval->setQuietLevel(1);
    else if (qtype == "SILENT")
      eval->setQuietLevel(2);
    else
      throw Exception("quiet function takes one argument - the quiet level (normal, quiet, or silent) as a string");
  }
  QString rtype;
  if (eval->getQuietLevel() == 0)
    rtype = "normal";
  else if (eval->getQuietLevel() == 1)
    rtype = "quiet";
  else if (eval->getQuietLevel() == 2)
    rtype = "silent";
  return ArrayVector(Array(rtype));
}

//@@Signature
//sfunction source SourceFunction
//inputs filename
//outputs none
//DOCBLOCK freemat_source
ArrayVector SourceFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  if (arg.size() != 1)
    throw Exception("source function takes exactly one argument - the filename of the script to execute");
  QString filename = arg[0].asString();
  QFile fp(filename);
  if (!fp.open(QFile::ReadOnly))
    throw Exception("unable to open file " + filename + " for reading");
  QTextStream fstr(&fp);
  QString scriptText(fstr.readAll());
  if (!scriptText.endsWith("\n")) scriptText += "\n";
  Scanner S(scriptText,filename);
  Parser P(S);
  Tree pcode(P.process());
  if (pcode.is(TOK_FUNCTION_DEFS))
    throw Exception("only scripts can be source-ed, not functions");
  Tree code = pcode.first();
  ParentScopeLocker lock(eval->getContext());
  eval->block(code);
  return ArrayVector();
}

//@@Signature
//sfunction builtin BuiltinFunction
//inputs fname varargin
//outputs varargout
//DOCBLOCK freemat_builtin
ArrayVector BuiltinFunction(int nargout, const ArrayVector& arg,Interpreter* eval){
  if (arg.size() == 0)
    throw Exception("builtin function requires at least one argument");
  if (!(arg[0].isString()))
    throw Exception("first argument to builtin must be the name of a function (i.e., a string)");
  FuncPtr funcDef;
  QString fname = arg[0].asString();
  Context *context = eval->getContext();
  if (!context->lookupFunction(fname,funcDef))
    throw Exception("function " + fname + " undefined!");
  funcDef->updateCode(eval);
  if (funcDef->scriptFlag)
    throw Exception("cannot use feval on a script");
  ArrayVector newarg(arg);
  newarg.pop_front();
  bool flagsave = eval->getStopOverload();
  eval->setStopOverload(true);
  ArrayVector tmp(eval->doFunction(funcDef,newarg,nargout));
  eval->setStopOverload(flagsave);
  return tmp;
}

//DOCBLOCK freemat_startup

//@@Signature
//sfunction docli DoCLIFunction
//inputs none
//outputs none
//DOCBLOCK freemat_docli
ArrayVector DoCLIFunction(int nargout, const ArrayVector& arg, Interpreter* eval) {
  Context *context = eval->getContext();
  context->deactivateCurrentScope();
  FuncPtr funcDef;
  if (eval->lookupFunction("startup",funcDef)) {
    funcDef->updateCode(eval);
    if (funcDef->scriptFlag) {
      try {
	eval->block(((MFunctionDef*)funcDef)->code);
      } catch (Exception& e) {
	eval->errorMessage("Startup script error:\n" + e.msg());
      }
    } else {
      eval->outputMessage(QString("startup.m must be a script"));
    }
  }
  eval->doCLI();
  return ArrayVector();
}

//@@Signature
//function getenv GetEnvFunction
//inputs var
//outputs value
//DOCBLOCK os_getenv
ArrayVector GetEnvFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) throw Exception("getenv requires one string argument");
  QString name(arg[0].asString());
  QByteArray ret = qgetenv(qPrintable(name));
  return Array(QString(ret));
}

//@@Signature
//function system SystemFunction
//inputs cmd
//outputs results
//DOCBLOCK os_system
ArrayVector SystemFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() != 1) 
    throw Exception("System function takes one string argument");
  QString systemArg(arg[0].asString());
  if (systemArg.size() == 0) 
    return ArrayVector();
  StringVector cp(DoSystemCallCaptured(systemArg));
  return CellArrayFromStringVector(cp);
}