MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
module-tclpgin.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/modules/module-tclpgin/module-tclpgin.cc,v 1.9 2017/01/12 14:57:50 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include <cmath>
35 #include <cfloat>
36 
37 #include "dataman.h"
38 #include "constltp.h"
39 
40 //#include <mathp.h>
41 #include <tcl.h>
42 
43 static Tcl_Interp *interp;
44 static int interp_cnt;
45 
46 class TclPlugIn : public MathParser::PlugIn {
47 protected:
49  Tcl_Obj *cmd;
50 
51 public:
53  ~TclPlugIn(void);
54  const char *sName(void) const;
55  int Read(int argc, char *argv[]);
56  TypedValue::Type GetType(void) const;
57  TypedValue GetVal(void) const;
58 };
59 
61 : MathParser::PlugIn(mp), type(TypedValue::VAR_UNKNOWN),
62 cmd(0)
63 {
64  if (!::interp) {
65  ::interp = Tcl_CreateInterp();
66  if (!::interp) {
68  }
69  }
70 
71  interp_cnt++;
72 }
73 
75 {
76  Tcl_DecrRefCount(cmd);
77 
78  if (--interp_cnt == 0) {
79  if (::interp) {
80  Tcl_DeleteInterp(interp);
81  }
82  }
83 }
84 
85 const char *
86 TclPlugIn::sName(void) const
87 {
88  return 0;
89 }
90 
91 int
92 TclPlugIn::Read(int argc, char *argv[])
93 {
94  char *s_type = argv[0];
95  if (strcasecmp(s_type, "real") == 0) {
97 
98  } else if (strcasecmp(s_type, "integer") == 0) {
100 
101  } else if (strcasecmp(s_type, "bool") == 0) {
103 
104  } else {
105  silent_cerr("unknown or unhandled type \"" << s_type << "\"" << std::endl);
107  }
108 
109  char *s_tcl = argv[1];
110  if (strncasecmp(s_tcl, "file://", STRLENOF("file://")) == 0) {
111  char *fname = &s_tcl[STRLENOF("file://")];
112  FILE *fin;
113  std::string s;
114  char buf[1024];
115  int cmdlen;
116 
117  fin = fopen(fname, "r");
118  if (fin == 0) {
119  silent_cerr("TclPlugIn::Read: unable to open file \"" << fname << "\"" << std::endl);
121  }
122 
123  if (!fgets(buf, sizeof(buf), fin)) {
124  silent_cerr("TclPlugIn::Read: unable to read from file \"" << fname << "\"" << std::endl);
125  fclose(fin);
127  }
128 
129  s += buf;
130 
131  while (fgets(buf, sizeof(buf), fin)) {
132  s += buf;
133  }
134  fclose(fin);
135 
136  cmd = Tcl_NewStringObj(s.c_str(), s.length());
137 
138  } else {
139 
140  /*
141  * check / escape string ?
142  */
143  cmd = Tcl_NewStringObj(s_tcl, strlen(s_tcl));
144  }
145 
146  if (cmd == 0) {
147  silent_cerr("TclPlugIn::Read: Tcl_NewStringObj failed" << std::endl);
149  }
150 
151  Tcl_IncrRefCount(cmd);
152 
153  return 0;
154 }
155 
158 {
159  return type;
160 }
161 
162 TypedValue
163 TclPlugIn::GetVal(void) const
164 {
165  Tcl_Obj *res;
166 
167  if (Tcl_EvalObjEx(interp, cmd, 0) != TCL_OK) {
168  silent_cerr("TclPlugIn::GetVal: Tcl_EvalObjEx failed"
169  << std::endl);
171  }
172 
173  res = Tcl_GetObjResult(interp);
174  if (res == 0) {
175  silent_cerr("TclPlugIn::GetVal: Tcl_GetObjResult failed"
176  << std::endl);
178  }
179 
180  switch (type) {
181  case TypedValue::VAR_INT: {
182  int i;
183  if (Tcl_GetIntFromObj(0, res, &i) != TCL_OK) {
184  silent_cerr("TclPlugIn::GetVal: Tcl_GetIntFromObj failed"
185  << std::endl);
187  }
188  return TypedValue(i);
189  }
190 
191  case TypedValue::VAR_REAL: {
192  double d;
193  if (Tcl_GetDoubleFromObj(0, res, &d) != TCL_OK) {
194  silent_cerr("TclPlugIn::GetVal: Tcl_GetDoubleFromObj failed"
195  << std::endl);
197  }
198  return TypedValue(d);
199  }
200 
201  default:
203  }
204 
205  Tcl_ResetResult(interp);
206 }
207 
208 static MathParser::PlugIn *
209 tcl_plugin(MathParser& mp, void *arg)
210 {
211  MathParser::PlugIn *p = 0;
212 
214 
215  return p;
216 }
217 
218 extern "C" int
219 module_init(const char *module_name, void *pdm, void *php)
220 {
221 #if 0
222  DataManager *pDM = (DataManager *)pdm;
223 #endif
224  MBDynParser *pHP = (MBDynParser *)php;
225 
226  pHP->GetMathParser().RegisterPlugIn("tcl", tcl_plugin, 0);
227 
228  return 0;
229 }
230 
static int interp_cnt
TypedValue GetVal(void) const
Tcl_Obj * cmd
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
const char * sName(void) const
int module_init(const char *module_name, void *pdm, void *php)
This function registers our user defined element for the math parser.
TypedValue::Type type
TclPlugIn(MathParser &mp)
int Read(int argc, char *argv[])
virtual MathParser & GetMathParser(void)
Definition: parser.cc:668
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
~TclPlugIn(void)
#define STRLENOF(s)
Definition: mbdyn.h:166
static MathParser::PlugIn * tcl_plugin(MathParser &mp, void *arg)
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
int RegisterPlugIn(const char *name, MathParser::PlugIn *(*)(MathParser &, void *), void *arg)
Definition: mathp.cc:4584
static std::stack< const HighParser * > pHP
Definition: parser.cc:598
TypedValue::Type GetType(void) const
static Tcl_Interp * interp
MathParser & mp
Definition: mathp.h:224