MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
TclPlugIn Class Reference
Inheritance diagram for TclPlugIn:
Collaboration diagram for TclPlugIn:

Public Member Functions

 TclPlugIn (MathParser &mp)
 
 ~TclPlugIn (void)
 
const char * sName (void) const
 
int Read (int argc, char *argv[])
 
TypedValue::Type GetType (void) const
 
TypedValue GetVal (void) const
 
- Public Member Functions inherited from MathParser::PlugIn
 PlugIn (MathParser &mp)
 
virtual ~PlugIn ()
 

Protected Attributes

TypedValue::Type type
 
Tcl_Obj * cmd
 
- Protected Attributes inherited from MathParser::PlugIn
MathParsermp
 

Detailed Description

Definition at line 46 of file module-tclpgin.cc.

Constructor & Destructor Documentation

TclPlugIn::TclPlugIn ( MathParser mp)

Definition at line 60 of file module-tclpgin.cc.

References interp, and MBDYN_EXCEPT_ARGS.

62 cmd(0)
63 {
64  if (!::interp) {
65  ::interp = Tcl_CreateInterp();
66  if (!::interp) {
68  }
69  }
70 
71  interp_cnt++;
72 }
static int interp_cnt
Tcl_Obj * cmd
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
TypedValue::Type type
static Tcl_Interp * interp
TclPlugIn::~TclPlugIn ( void  )

Definition at line 74 of file module-tclpgin.cc.

References cmd, and interp.

75 {
76  Tcl_DecrRefCount(cmd);
77 
78  if (--interp_cnt == 0) {
79  if (::interp) {
80  Tcl_DeleteInterp(interp);
81  }
82  }
83 }
static int interp_cnt
Tcl_Obj * cmd
static Tcl_Interp * interp

Member Function Documentation

TypedValue::Type TclPlugIn::GetType ( void  ) const
virtual

Implements MathParser::PlugIn.

Definition at line 157 of file module-tclpgin.cc.

References type.

158 {
159  return type;
160 }
TypedValue::Type type
TypedValue TclPlugIn::GetVal ( void  ) const
virtual

Implements MathParser::PlugIn.

Definition at line 163 of file module-tclpgin.cc.

References cmd, interp, MBDYN_EXCEPT_ARGS, type, TypedValue::VAR_INT, and TypedValue::VAR_REAL.

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 }
Tcl_Obj * cmd
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
TypedValue::Type type
static Tcl_Interp * interp
int TclPlugIn::Read ( int  argc,
char *  argv[] 
)
virtual

Implements MathParser::PlugIn.

Definition at line 92 of file module-tclpgin.cc.

References buf, cmd, MBDYN_EXCEPT_ARGS, STRLENOF, type, TypedValue::VAR_BOOL, TypedValue::VAR_INT, and TypedValue::VAR_REAL.

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 }
Tcl_Obj * cmd
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
TypedValue::Type type
#define STRLENOF(s)
Definition: mbdyn.h:166
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
const char * TclPlugIn::sName ( void  ) const
virtual

Implements MathParser::PlugIn.

Definition at line 86 of file module-tclpgin.cc.

87 {
88  return 0;
89 }

Member Data Documentation

Tcl_Obj* TclPlugIn::cmd
protected

Definition at line 49 of file module-tclpgin.cc.

Referenced by GetVal(), Read(), and ~TclPlugIn().

TypedValue::Type TclPlugIn::type
protected

Definition at line 48 of file module-tclpgin.cc.

Referenced by GetType(), GetVal(), and Read().


The documentation for this class was generated from the following file: