Cantera  2.0
global.cpp
1 #include "cantera/base/global.h"
2 
5 #include "cantera/base/xml.h"
6 #include "application.h"
7 #include "units.h"
8 
9 #include <cstdio>
10 #include <stdarg.h>
11 
12 using namespace std;
13 
14 namespace Cantera {
15 
16 //! Return a pointer to the application object
17 static Application* app()
18 {
19  return Application::Instance() ;
20 }
21 
22 // **************** Text Logging ****************
23 
24 void setLogger(Logger* logwriter)
25 {
26  try {
27  app()->setLogger(logwriter) ;
28  } catch (std::bad_alloc) {
29  logwriter->error("bad alloc thrown by app()");
30  }
31 }
32 
33 void writelog(const std::string& msg)
34 {
35  app()->writelog(msg);
36 }
37 
38 void writelog(const char* msg)
39 {
40  app()->writelog(msg);
41 }
42 
43 void writelogf(const char* fmt,...)
44 {
45  enum { BUFSIZE = 2048 } ;
46  char sbuf[BUFSIZE] ;
47 
48  va_list args ;
49  va_start(args, fmt) ;
50 
51 #ifdef _MSC_VER
52  _vsnprintf(sbuf, BUFSIZE, fmt, args) ;
53 #else
54  vsprintf(sbuf, fmt, args) ;
55 #endif
56 
57  writelog(sbuf) ;
58  va_end(args) ;
59 }
60 
62 {
63  app()->writelogendl();
64 }
65 
66 void error(const std::string& msg)
67 {
68  app()->logerror(msg);
69 }
70 
72 {
73  return app()->getUserEnv();
74 }
75 
76 // **************** HTML Logging ****************
77 
78 #ifdef WITH_HTML_LOGS
79 
80 void beginLogGroup(std::string title, int loglevel)
81 {
82  app()->beginLogGroup(title, loglevel) ;
83 }
84 
85 void addLogEntry(std::string tag, std::string value)
86 {
87  app()->addLogEntry(tag, value) ;
88 }
89 
90 void addLogEntry(std::string tag, doublereal value)
91 {
92  app()->addLogEntry(tag, value) ;
93 }
94 
95 void addLogEntry(std::string tag, int value)
96 {
97  app()->addLogEntry(tag, value) ;
98 }
99 
100 void addLogEntry(std::string msg)
101 {
102  app()->addLogEntry(msg) ;
103 }
104 
105 void endLogGroup(std::string title)
106 {
107  app()->endLogGroup(title) ;
108 }
109 
110 void write_logfile(std::string file)
111 {
112  app()->write_logfile(file) ;
113 }
114 
115 #endif // WITH_HTML_LOGS
116 
117 // **************** Global Data ****************
118 
119 Unit* Unit::s_u = 0;
120 mutex_t Unit::units_mutex;
121 
122 void appdelete()
123 {
124  Application::ApplicationDestroy() ;
125  FactoryBase::deleteFactories();
126  Unit::deleteUnit();
127 }
128 
130 {
131  app()->thread_complete() ;
132 }
133 
134 XML_Node* get_XML_File(std::string file, int debug)
135 {
136  XML_Node* xtmp = app()->get_XML_File(file, debug) ;
137  //writelog("get_XML_File: returned from app:get_XML_FILE " + int2str(xtmp) + "\n");
138  return xtmp;
139 }
140 
141 void close_XML_File(std::string file)
142 {
143  app()->close_XML_File(file) ;
144 }
145 
146 int nErrors()
147 {
148  return app()->getErrorCount();
149 }
150 
151 void popError()
152 {
153  app()->popError() ;
154 }
155 
157 {
158  return app()->lastErrorMessage() ;
159 }
160 
161 void showErrors(std::ostream& f)
162 {
163  app()->getErrors(f) ;
164 }
165 
167 {
168  app()->logErrors() ;
169 }
170 
171 void setError(std::string r, std::string msg)
172 {
173  app()->addError(r, msg) ;
174 }
175 
176 void addDirectory(std::string dir)
177 {
178  app()->addDataDirectory(dir) ;
179 }
180 
181 std::string findInputFile(std::string name)
182 {
183  return app()->findInputFile(name) ;
184 }
185 
186 doublereal toSI(std::string unit)
187 {
188  doublereal f = Unit::units()->toSI(unit);
189  if (f) {
190  return f;
191  } else {
192  throw CanteraError("toSI","unknown unit string: "+unit);
193  }
194  return 1.0;
195 }
196 
197 doublereal actEnergyToSI(std::string unit)
198 {
199  doublereal f = Unit::units()->actEnergyToSI(unit);
200  if (f) {
201  return f;
202  }
203  return 1.0;
204 }
205 
206 string canteraRoot()
207 {
208  char* ctroot = 0;
209  ctroot = getenv("CANTERA_ROOT");
210  if (ctroot != 0) {
211  return string(ctroot);
212  }
213 #ifdef CANTERA_ROOT
214  return string(CANTERA_ROOT);
215 #else
216  return "";
217 #endif
218 
219 }
220 
221 //! split a string at a '#' sign. Used to separate a file name from an id string.
222 /*!
223  * @param src Original string to be split up. This is unchanged.
224  * @param file Output string representing the first part of the string, which is the filename.
225  * @param id Output string representing the last part of the string, which is the id.
226  */
227 static void split_at_pound(const std::string& src, std::string& file, std::string& id)
228 {
229  string::size_type ipound = src.find('#');
230  if (ipound != string::npos) {
231  id = src.substr(ipound+1,src.size());
232  file = src.substr(0,ipound);
233  } else {
234  id = "";
235  file = src;
236  }
237 }
238 
239 XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root)
240 {
241  std::string fname, idstr;
242  XML_Node* db, *doc;
243  split_at_pound(file_ID, fname, idstr);
244  if (fname == "") {
245  if (!root) throw CanteraError("get_XML_Node",
246  "no file name given. file_ID = "+file_ID);
247  db = root->findID(idstr, 3);
248  } else {
249  doc = get_XML_File(fname);
250  if (!doc) throw CanteraError("get_XML_Node",
251  "get_XML_File failed trying to open "+fname);
252  db = doc->findID(idstr, 3);
253  }
254  if (!db) {
255  throw CanteraError("get_XML_Node",
256  "id tag '"+idstr+"' not found.");
257  }
258  return db;
259 }
260 
261 XML_Node* get_XML_NameID(const std::string& nameTarget,
262  const std::string& file_ID,
263  XML_Node* root)
264 {
265  string fname, idTarget;
266  XML_Node* db, *doc;
267  split_at_pound(file_ID, fname, idTarget);
268  if (fname == "") {
269  if (!root) {
270  return 0;
271  }
272  db = root->findNameID(nameTarget, idTarget);
273  } else {
274  doc = get_XML_File(fname);
275  if (!doc) {
276  return 0;
277  }
278  db = doc->findNameID(nameTarget, idTarget);
279  }
280  return db;
281 }
282 
283 std::vector<FactoryBase*> FactoryBase::s_vFactoryRegistry;
284 
285 }