MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
env.cc File Reference
#include "mbconfig.h"
#include <sstream>
#include <unistd.h>
#include <cerrno>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "ac/f2c.h"
#include "mathp.h"
Include dependency graph for env.cc:

Go to the source code of this file.

Functions

void GetEnviron (MathParser &MP)
 

Variables

char ** environ
 
static const char MBDYNPREFIX [] = "MBDYN"
 

Function Documentation

void GetEnviron ( MathParser MP)

Definition at line 53 of file env.cc.

References DEBUGCOUT, environ, Table::Get(), MathParser::GetLastStmt(), MathParser::GetSymbolTable(), MBDYN_EXCEPT_ARGS, MBDYNPREFIX, NO_OP, Table::Put(), SAFEDELETEARR, SAFESTRDUP, and STRLENOF.

Referenced by mbdyn_program().

54 {
55  // cerca la variabile MBDYNVARS
56  std::string MBDYNVARS = std::string(MBDYNPREFIX) + "VARS";
57  char* e = getenv(MBDYNVARS.c_str());
58  if (e != NULL) {
59  DEBUGCOUT("GetEnv: reading variable <" << e << ">" << std::endl);
60  std::istringstream in(e);
61  InputStream In(in);
62  MP.GetLastStmt(In);
63  DEBUGCOUT("GetEnv: variable <" << e << "> read" << std::endl);
64  }
65 
66  /* cerca le variabili definite singolarmente */
67  Table& T = MP.GetSymbolTable();
68  char** env = environ;
69  while (*env) {
70  if (strncmp(*env, MBDYNPREFIX, STRLENOF(MBDYNPREFIX)) == 0) {
71  DEBUGCOUT("reading var <" << *env << ">" << std::endl);
72  char* p = NULL;
73  char* v = NULL;
74  char* n = NULL;
75 
76  SAFESTRDUP(p, *env);
77  v = std::strchr(p, '=');
78  if (v == NULL) {
79  silent_cerr("parse error in envvar <"
80  << p << ">" << std::endl);
81  SAFEDELETEARR(p);
83  }
84 
85  *v = '\0';
86  v++;
87 
88  if (strcmp(p, "MBDYNVARS") == 0) {
89  NO_OP;
90 
91  } else if (strncmp(p, "MBDYN_real_", STRLENOF("MBDYN_real_")) == 0) {
92  n = p + STRLENOF("MBDYN_real_");
93  char *endptr = NULL;
94  errno = 0;
95  doublereal d = strtod(v, &endptr);
96  int save_errno = errno;
97  if (endptr != NULL && endptr[0] != '\0') {
98  silent_cerr("SetEnv: unable to parse "
99  "real <" << v << "> "
100  "for var <" << p << ">"
101  << std::endl);
102  SAFEDELETEARR(p);
104 
105  } else if (save_errno == ERANGE) {
106  silent_cerr("SetEnv: real <" << v << "> "
107  "for var <" << p << "> overflows"
108  << std::endl);
109  SAFEDELETEARR(p);
111  }
112  DEBUGCOUT("setting real var <"
113  << n << "=" << d << ">" << std::endl);
114 
115  if ((T.Get(n)) == NULL) {
116  if (T.Put(n, Real(d)) == NULL) {
117  silent_cerr("SetEnv:"
118  " error in insertion"
119  " of real symbol <"
120  << n << ">" << std::endl);
121  SAFEDELETEARR(p);
123  }
124  }
125 
126  } else if (strncmp(p, "MBDYN_integer_", STRLENOF("MBDYN_integer_")) == 0) {
127  n = p + STRLENOF("MBDYN_integer_");
128 #ifdef HAVE_STRTOL
129  char *endptr = NULL;
130  errno = 0;
131  long i = strtol(v, &endptr, 10);
132  int save_errno = errno;
133  if (endptr != NULL && endptr[0] != '\0') {
134  silent_cerr("SetEnv: unable to parse "
135  "integer <" << v << "> "
136  "for var <" << p << ">"
137  << std::endl);
138  SAFEDELETEARR(p);
140 
141  } else if (save_errno == ERANGE) {
142  silent_cerr("SetEnv: integer <" << v << "> "
143  "for var <" << p << "> overflows"
144  << std::endl);
145  SAFEDELETEARR(p);
147  }
148 #else /* !HAVE_STRTOL */
149  i = atol(v);
150 #endif /* !HAVE_STRTOL */
151  DEBUGCOUT("setting integer var <"
152  << n << "=" << i << ">" << std::endl);
153 
154  if ((T.Get(n)) == NULL) {
155  if (T.Put(n, Int(i)) == NULL) {
156  silent_cerr("SetEnv:"
157  " error in insertion"
158  " of integer symbol <"
159  << n << ">" << std::endl);
160  SAFEDELETEARR(p);
162  }
163  }
164 
165  } else if (strncmp(p, "MBDYN_string_", STRLENOF("MBDYN_string_")) == 0) {
166  n = p + STRLENOF("MBDYN_string_");
167  if ((T.Get(n)) == NULL) {
168  if (T.Put(n, TypedValue(std::string(v))) == NULL) {
169  silent_cerr("SetEnv:"
170  " error in insertion"
171  " of string symbol <"
172  << n << ">" << std::endl);
173  SAFEDELETEARR(p);
175  }
176  }
177 
178  } else {
179  silent_cerr("unknown var type <"
180  << p << ">; skipping ..." << std::endl);
181  }
182 
183  SAFEDELETEARR(p);
184  }
185  env++;
186  }
187 
188  /* altro ... */
189 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
Var * Put(const std::string &name, const TypedValue &v)
Definition: table.cc:110
char ** environ
#define SAFEDELETEARR(pnt)
Definition: mynewmem.h:713
#define NO_OP
Definition: myassert.h:74
int Int
Definition: mathtyp.h:40
#define DEBUGCOUT(msg)
Definition: myassert.h:232
#define STRLENOF(s)
Definition: mbdyn.h:166
static const char MBDYNPREFIX[]
Definition: env.cc:48
NamedValue * Get(const std::string &name) const
Definition: table.cc:150
Real GetLastStmt(Real d=0., Token t=ARGSEP)
Definition: mathp.cc:4402
#define SAFESTRDUP(pnt, src)
Definition: mynewmem.h:707
Definition: table.h:43
double doublereal
Definition: colamd.c:52
double Real
Definition: mathtyp.h:39
Table & GetSymbolTable(void) const
Definition: mathp.cc:1931

Here is the call graph for this function:

Variable Documentation

char** environ

Referenced by GetEnviron(), and Solver::Prepare().

const char MBDYNPREFIX[] = "MBDYN"
static

Definition at line 48 of file env.cc.

Referenced by GetEnviron().