00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifdef WIN32
00017 #pragma warning(disable:4786)
00018 #pragma warning(disable:4503)
00019 #pragma warning(disable:4996)
00020 #endif
00021
00022
00023 #include <stdarg.h>
00024
00025 #include "global.h"
00026 #include "ctexceptions.h"
00027 #include "stringUtils.h"
00028 #include "units.h"
00029 #include "xml.h"
00030 #include "ctml.h"
00031 #include "FactoryBase.h"
00032 #include "logger.h"
00033
00034
00035 #include <fstream>
00036 #include <memory>
00037
00038 #ifdef WIN32
00039 #include <algorithm>
00040 #include <functional>
00041 #include <new>
00042 #endif
00043
00044 using namespace std;
00045
00046
00047
00048
00049
00050
00051
00052 #ifdef THREAD_SAFE_CANTERA
00053
00054 #include <boost/shared_ptr.hpp>
00055 #include <boost/thread/mutex.hpp>
00056 static boost::mutex dir_mutex;
00057 static boost::mutex msg_mutex;
00058 static boost::mutex app_mutex;
00059
00060 static boost::mutex xml_mutex;
00061
00062
00063 #define DIR_LOCK() boost::mutex::scoped_lock d_lock(dir_mutex)
00064
00065
00066 #define MSG_LOCK() boost::mutex::scoped_lock m_lock(msg_mutex)
00067
00068
00069 #define APP_LOCK() boost::mutex::scoped_lock a_lock(app_mutex)
00070
00071
00072 #define XML_LOCK() boost::mutex::scoped_lock x_lock(xml_mutex)
00073
00074 #ifdef WITH_HTML_LOGS
00075
00076
00077 #endif
00078
00079 #if defined(BOOST_HAS_WINTHREADS)
00080 #include <windows.h>
00081 typedef unsigned int cthreadId_t ;
00082 class thread_equal
00083 {
00084 public:
00085 bool operator()(cthreadId_t L, cthreadId_t R)
00086 {
00087 return L == R ;
00088 }
00089 } ;
00090 cthreadId_t getThisThreadId()
00091 {
00092 return ::GetCurrentThreadId() ;
00093 }
00094 #elif defined(BOOST_HAS_PTHREADS)
00095
00096 typedef pthread_t cthreadId_t ;
00097 class thread_equal
00098 {
00099 public:
00100 bool operator()(cthreadId_t L, cthreadId_t R)
00101 {
00102 return pthread_equal(L, R) ;
00103 }
00104 } ;
00105 cthreadId_t getThisThreadId()
00106 {
00107 return pthread_self() ;
00108 }
00109 #elif defined(BOOST_HAS_MPTASKS)
00110 typedef MPTaskID cthreadId_t ;
00111 class thread_equal
00112 {
00113 public:
00114 bool operator()(cthreadId_t L, cthreadId_t R)
00115 {
00116 return L == R ;
00117 }
00118 } ;
00119 cthreadId_t getThisThreadId()
00120 {
00121 return MPCurrentTaskID() ;
00122 }
00123 #endif
00124
00125 #else
00126 #define DIR_LOCK()
00127 #define MSG_LOCK()
00128 #define APP_LOCK()
00129
00130 #define XML_LOCK()
00131
00132 #ifdef WITH_HTML_LOGS
00133
00134 #endif
00135
00136 #endif
00137
00138 namespace Cantera {
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 class Application {
00166
00167 protected:
00168
00169
00170
00171
00172 class Messages {
00173 public:
00174
00175
00176
00177
00178
00179 Messages() {
00180
00181
00182 logwriter = new Logger();
00183 #ifdef WITH_HTML_LOGS
00184 xmllog = 0;
00185 current = 0;
00186 loglevel = 0;
00187 #endif
00188 }
00189
00190
00191 ~Messages() {
00192 delete logwriter;
00193 #ifdef WITH_HTML_LOGS
00194 if (xmllog) {
00195 write_logfile("orphan");
00196 }
00197 #endif
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 void addError(std::string r, std::string msg) ;
00211
00212
00213
00214
00215
00216 int getErrorCount() ;
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 void popError() ;
00227
00228
00229
00230
00231
00232
00233
00234
00235 std::string lastErrorMessage() ;
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 void getErrors(std::ostream& f) ;
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 void logErrors();
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 void writelog(const std::string& msg);
00274
00275
00276 void writelogendl();
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 void writelog(const char* pszmsg) ;
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 void logerror(const std::string& msg) ;
00303
00304
00305 int getUserEnv() ;
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316 void setLogger(Logger* logwriter) ;
00317
00318 #ifdef WITH_HTML_LOGS
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 void beginLogGroup(std::string title, int loglevel) ;
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 void addLogEntry(std::string tag, std::string value) ;
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354 void addLogEntry(std::string tag, doublereal value) ;
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 void addLogEntry(std::string tag, int value) ;
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375 void addLogEntry(std::string msg) ;
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 void endLogGroup(std::string title) ;
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405 void write_logfile(std::string file);
00406 #endif
00407
00408 protected:
00409
00410 vector<string> errorMessage;
00411
00412
00413 vector<string> errorRoutine;
00414
00415
00416 Logger* logwriter;
00417 #ifdef WITH_HTML_LOGS
00418
00419 XML_Node *xmllog;
00420
00421
00422 XML_Node *current;
00423
00424
00425 int loglevel;
00426
00427
00428 vector<int> loglevels;
00429
00430
00431 vector<string> loggroups;
00432 #endif
00433 } ;
00434
00435 #ifdef THREAD_SAFE_CANTERA
00436
00437 typedef boost::shared_ptr< Messages > pMessages_t ;
00438
00439
00440 typedef std::map< cthreadId_t, pMessages_t > threadMsgMap_t ;
00441
00442
00443
00444 class ThreadMessages
00445 {
00446 public:
00447
00448 ThreadMessages()
00449 {
00450 }
00451
00452
00453
00454
00455
00456 Messages* operator->()
00457 {
00458 MSG_LOCK() ;
00459 cthreadId_t curId = getThisThreadId() ;
00460 threadMsgMap_t::iterator iter = m_threadMsgMap.find( curId ) ;
00461 if ( iter != m_threadMsgMap.end() )
00462 {
00463 return (iter->second.get()) ;
00464 }
00465 pMessages_t pMsgs( new Messages() ) ;
00466 m_threadMsgMap.insert( std::pair< cthreadId_t, pMessages_t >( curId, pMsgs ) ) ;
00467 return pMsgs.get() ;
00468 }
00469
00470
00471 void removeThreadMessages() {
00472 MSG_LOCK() ;
00473 cthreadId_t curId = getThisThreadId() ;
00474 threadMsgMap_t::iterator iter = m_threadMsgMap.find( curId ) ;
00475 if ( iter != m_threadMsgMap.end() ) {
00476 m_threadMsgMap.erase( iter ) ;
00477 }
00478 }
00479
00480 private:
00481
00482 threadMsgMap_t m_threadMsgMap ;
00483 } ;
00484 #endif
00485
00486
00487 protected:
00488
00489 Application() : stop_on_error(false),
00490 tmp_dir("."), m_sleep("1")
00491 {
00492 #if !defined( THREAD_SAFE_CANTERA )
00493 pMessenger = std::auto_ptr<Messages>(new Messages());
00494 #endif
00495
00496
00497 char* ctmpdir = getenv("CANTERA_TMPDIR");
00498 if (ctmpdir != 0) {
00499 tmp_dir = string(ctmpdir);
00500 } else {
00501 char* tmpdir = getenv("TMP");
00502 if (tmpdir == 0)
00503 tmpdir = getenv("TEMP");
00504 if (tmpdir != 0)
00505 tmp_dir = string(tmpdir);
00506 }
00507
00508
00509 char* sleepstr = getenv("SLEEP");
00510 if (sleepstr != 0) {
00511 m_sleep = string(sleepstr);
00512 }
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523 setDefaultDirectories();
00524 #if defined(THREAD_SAFE_CANTERA)
00525 Unit::units() ;
00526 #endif
00527 }
00528
00529 public:
00530
00531
00532
00533
00534 static Application* Instance() {
00535 APP_LOCK();
00536 if (Application::s_app == 0) {
00537 Application::s_app = new Application();
00538 }
00539 return s_app;
00540 }
00541
00542
00543
00544
00545
00546
00547 virtual ~Application() {
00548 std::map<std::string, XML_Node*>::iterator pos;
00549 for (pos = xmlfiles.begin(); pos != xmlfiles.end(); ++pos) {
00550 pos->second->unlock();
00551 delete pos->second;
00552 pos->second = 0;
00553 }
00554 }
00555
00556
00557 static void ApplicationDestroy() {
00558 APP_LOCK() ;
00559 if (Application::s_app != 0) {
00560 delete Application::s_app;
00561 Application::s_app = 0;
00562 }
00563 }
00564
00565
00566
00567
00568
00569 void setTmpDir(std::string tmp) ;
00570
00571
00572 std::string getTmpDir() ;
00573
00574
00575
00576
00577
00578 std::string sleep() ;
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590 void addError(std::string r, std::string msg) { pMessenger->addError(r, msg) ; }
00591
00592
00593
00594
00595
00596 int getErrorCount() { return pMessenger->getErrorCount() ; }
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606 void popError() { pMessenger->popError() ; }
00607
00608
00609
00610
00611
00612
00613
00614
00615 std::string lastErrorMessage() { return pMessenger->lastErrorMessage() ; }
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630 void getErrors( std::ostream& f) { pMessenger->getErrors(f) ; }
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642 void logErrors() { pMessenger->logErrors() ; }
00643
00644
00645
00646
00647
00648
00649
00650 void addDataDirectory( std::string dir ) ;
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679 std::string findInputFile(std::string name) ;
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691 XML_Node* get_XML_File(std::string file, int debug=0) ;
00692
00693
00694
00695
00696
00697
00698
00699 void close_XML_File(std::string file) ;
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710 void writelog(const std::string& msg) { pMessenger->writelog(msg); }
00711
00712
00713
00714
00715
00716
00717 void writelogendl() { pMessenger->writelogendl(); }
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728 void writelog(const char* pszmsg) { pMessenger->writelog(pszmsg); }
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743 void logerror(const std::string& msg) {pMessenger->logerror(msg); }
00744
00745
00746 int getUserEnv() { return pMessenger->getUserEnv() ; }
00747
00748
00749
00750
00751
00752
00753
00754
00755 void setLogger(Logger* logwriter) {pMessenger->setLogger(logwriter);}
00756
00757
00758 void thread_complete() ;
00759
00760 #ifdef WITH_HTML_LOGS
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773 void beginLogGroup(std::string title, int loglevel) { pMessenger->beginLogGroup(title,loglevel);}
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784 void addLogEntry(std::string tag, std::string value) { pMessenger->addLogEntry(tag, value);}
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795 void addLogEntry(std::string tag, doublereal value) { pMessenger->addLogEntry(tag, value);}
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806 void addLogEntry(std::string tag, int value) { pMessenger->addLogEntry(tag, value);}
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816 void addLogEntry(std::string msg) { pMessenger->addLogEntry(msg); }
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831 void endLogGroup(std::string title) { pMessenger->endLogGroup(title) ;}
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847 void write_logfile(std::string file) { pMessenger->write_logfile(file) ; }
00848 #endif
00849
00850 protected:
00851 void setDefaultDirectories();
00852
00853
00854 vector<string> inputDirs;
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866 bool stop_on_error;
00867
00868 map<string, string> options;
00869
00870 string tmp_dir;
00871
00872 map<string, XML_Node*> xmlfiles;
00873
00874 string m_sleep;
00875
00876
00877 #ifdef WITH_HTML_LOGS
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888 #endif
00889
00890 #if defined(THREAD_SAFE_CANTERA)
00891 ThreadMessages pMessenger ;
00892 #else
00893 auto_ptr< Messages > pMessenger ;
00894 #endif
00895
00896 private:
00897 static Application* s_app ;
00898 };
00899
00900
00901 inline Application* app() {
00902 return Application::Instance() ;
00903 }
00904
00905
00906
00907 Application* Application::s_app = 0;
00908
00909
00910
00911
00912 Unit* Unit::s_u = 0;
00913 #if defined(THREAD_SAFE_CANTERA)
00914 boost::mutex Unit::units_mutex ;
00915 #endif
00916
00917
00918
00919
00920
00921
00922 void appdelete() {
00923 Application::ApplicationDestroy() ;
00924 FactoryBase::deleteFactories();
00925 Unit::deleteUnit();
00926 }
00927
00928
00929
00930
00931 void thread_complete() {
00932 app()->thread_complete() ;
00933 }
00934
00935 void Application::thread_complete() {
00936 #if defined(THREAD_SAFE_CANTERA)
00937 pMessenger.removeThreadMessages() ;
00938 #endif
00939 }
00940
00941 XML_Node* get_XML_File(std::string file, int debug) {
00942 XML_Node* xtmp = app()->get_XML_File(file, debug) ;
00943
00944 return xtmp;
00945 }
00946
00947 XML_Node* Application::get_XML_File(std::string file, int debug) {
00948 XML_LOCK();
00949 std::string path = "";
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974 path = findInputFile(file);
00975
00976 #ifdef WIN32
00977
00978
00979 replace_if( path.begin(), path.end(), bind2nd( equal_to<char>(), '\\'), '/' ) ;
00980 #endif
00981
00982 string ff = path;
00983 if (xmlfiles.find(path)
00984 == xmlfiles.end()) {
00985
00986
00987
00988
00989
00990 string::size_type idot = path.rfind('.');
00991 string ext;
00992 if (idot != string::npos) {
00993 ext = path.substr(idot, path.size());
00994 } else {
00995 ext = "";
00996 idot = path.size();
00997 }
00998 if (ext != ".xml" && ext != ".ctml") {
00999
01000
01001
01002
01003
01004
01005
01006 string::size_type islash = path.rfind('/');
01007 if (islash != string::npos)
01008 ff = string("./")+path.substr(islash+1,idot-islash - 1) + ".xml";
01009 else {
01010 ff = string("./")+path.substr(0,idot) + ".xml";
01011 }
01012 #ifdef DEBUG_PATHS
01013 cout << "get_XML_File(): Expected location of xml file = "
01014 << ff << endl;
01015 #endif
01016
01017
01018
01019
01020
01021 if (xmlfiles.find(ff) != xmlfiles.end()) {
01022 #ifdef DEBUG_PATHS
01023 cout << "get_XML_File(): File, " << ff << ", was previously read."
01024 << " Retrieving the storred xml tree." << endl;
01025 #endif
01026 return xmlfiles[ff];
01027 }
01028
01029
01030
01031
01032 ctml::ct2ctml(path.c_str(),debug);
01033 }
01034 else {
01035 ff = path;
01036 }
01037
01038
01039
01040
01041
01042 ifstream s(ff.c_str());
01043
01044 XML_Node* x = new XML_Node("doc");
01045 if (s) {
01046 x->build(s);
01047 x->lock();
01048 xmlfiles[ff] = x;
01049 }
01050 else {
01051 string estring = "cannot open "+ff+" for reading.";
01052 estring += "Note, this error indicates a possible configuration problem.";
01053 throw CanteraError("get_XML_File", estring);
01054 }
01055 }
01056
01057
01058
01059
01060
01061
01062 return xmlfiles[ff];
01063 }
01064
01065
01066
01067
01068
01069
01070
01071 void close_XML_File(std::string file) {
01072 app()->close_XML_File(file) ;
01073 }
01074
01075 void Application::close_XML_File(std::string file) {
01076 XML_LOCK();
01077 if (file == "all") {
01078 map<string, XML_Node*>::iterator
01079 b = xmlfiles.begin(),
01080 e = xmlfiles.end();
01081 for(; b != e; ++b) {
01082 b->second->unlock();
01083 delete b->second;
01084 xmlfiles.erase(b->first);
01085 }
01086 }
01087 else if (xmlfiles.find(file) != xmlfiles.end()) {
01088 xmlfiles[file]->unlock();
01089 delete xmlfiles[file];
01090 xmlfiles.erase(file);
01091 }
01092 }
01093
01094 void setTmpDir(std::string tmp) { app()->setTmpDir(tmp); }
01095 void Application::setTmpDir(std::string tmp) {
01096 APP_LOCK();
01097 tmp_dir = tmp ;
01098 }
01099
01100 string tmpDir() { return app()->getTmpDir(); }
01101 std::string Application::getTmpDir() {
01102 APP_LOCK();
01103 return tmp_dir ;
01104 }
01105
01106 string sleep() { return app()->sleep(); }
01107 std::string Application::sleep() {
01108 APP_LOCK();
01109 return m_sleep ;
01110 }
01111
01112
01113
01114
01115
01116
01117 int nErrors() {
01118 return app()->getErrorCount();
01119 }
01120
01121
01122 int Application::Messages::getErrorCount() {
01123 return static_cast<int>(errorMessage.size()) ;
01124 }
01125
01126
01127
01128
01129
01130
01131
01132
01133 void popError() {
01134 app()->popError() ;
01135 }
01136
01137
01138 void Application::Messages::popError() {
01139 if ( static_cast<int>(errorMessage.size()) > 0) {
01140 errorRoutine.pop_back() ;
01141 errorMessage.pop_back() ;
01142 }
01143 }
01144
01145
01146
01147
01148
01149
01150
01151 string lastErrorMessage() {
01152 return app()->lastErrorMessage() ;
01153 }
01154
01155
01156 std::string Application::Messages::lastErrorMessage() {
01157 if ( static_cast<int>(errorMessage.size()) > 0) {
01158 string head =
01159 "\n\n************************************************\n"
01160 " Cantera Error! \n"
01161 "************************************************\n\n";
01162 return head+string("\nProcedure: ")+errorRoutine.back()
01163 +string("\nError: ")+errorMessage.back();
01164 } else {
01165 return "<no Cantera error>";
01166 }
01167 }
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178 void showErrors(std::ostream& f) {
01179 app()->getErrors(f) ;
01180 }
01181
01182
01183 void Application::Messages::getErrors( std::ostream& f) {
01184 int i = static_cast<int>(errorMessage.size());
01185 if (i == 0) return;
01186 f << endl << endl;
01187 f << "************************************************" << endl;
01188 f << " Cantera Error! " << endl;
01189 f << "************************************************" << endl
01190 << endl;
01191 int j;
01192 for (j = 0; j < i; j++) {
01193 f << endl;
01194 f << "Procedure: " << errorRoutine[j] << endl;
01195 f << "Error: " << errorMessage[j] << endl;
01196 }
01197 f << endl << endl;
01198 errorMessage.clear();
01199 errorRoutine.clear();
01200 }
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211 void showErrors() {
01212 app()->logErrors() ;
01213 }
01214
01215
01216 void Application::Messages::logErrors() {
01217 int i = static_cast<int>(errorMessage.size());
01218 if (i == 0) return;
01219 writelog("\n\n");
01220 writelog("************************************************\n");
01221 writelog(" Cantera Error! \n");
01222 writelog("************************************************\n\n");
01223 int j;
01224 for (j = 0; j < i; j++) {
01225 writelog("\n");
01226 writelog(string("Procedure: ")+ errorRoutine[j]+" \n");
01227 writelog(string("Error: ")+ errorMessage[j]+" \n");
01228 }
01229 writelog("\n\n");
01230 errorMessage.clear();
01231 errorRoutine.clear();
01232 }
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242 void setError(std::string r, std::string msg) {
01243 app()->addError(r, msg) ;
01244 }
01245
01246
01247 void Application::Messages::addError(std::string r, std::string msg) {
01248 errorMessage.push_back(msg);
01249 errorRoutine.push_back(r);
01250 }
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281 void Application::setDefaultDirectories() {
01282 vector<string>& dirs = inputDirs;
01283
01284
01285 dirs.push_back(".");
01286
01287
01288 #ifdef WIN32
01289
01290
01291
01292
01293
01294
01295
01296
01297 const char* comfiles = getenv("COMMONPROGRAMFILES");
01298 if (comfiles != 0) {
01299 string cfiles = string(comfiles);
01300
01301
01302 if (cfiles[0] == '\'')
01303 cfiles = cfiles.substr(1,1000);
01304 if (cfiles[cfiles.size()-1] == '\'') cfiles[cfiles.size()-1] = '\n';
01305
01306 string datadir = string(comfiles) + "/Cantera/data";
01307 string tmpldir = string(comfiles) + "/Cantera/templates";
01308 dirs.push_back(datadir);
01309 dirs.push_back(tmpldir);
01310 }
01311 #endif
01312
01313 #ifdef DARWIN
01314
01315
01316
01317 dirs.push_back("/Applications/Cantera/data");
01318 #endif
01319
01320
01321
01322
01323
01324 if (getenv("CANTERA_DATA") != 0) {
01325 string datadir = string(getenv("CANTERA_DATA"));
01326 dirs.push_back(datadir);
01327 }
01328
01329
01330
01331
01332
01333 #ifdef CANTERA_DATA
01334 string datadir = string(CANTERA_DATA);
01335 dirs.push_back(datadir);
01336 #endif
01337 }
01338
01339
01340
01341
01342 void addDirectory(std::string dir) {
01343 app()->addDataDirectory( dir ) ;
01344 }
01345 void Application::addDataDirectory( std::string dir ) {
01346 DIR_LOCK() ;
01347 if (inputDirs.size() == 0) setDefaultDirectories();
01348 string d = stripnonprint(dir);
01349 size_t m, n = inputDirs.size();
01350
01351
01352 for (m = 0; m < n; m++) {
01353 if (d == inputDirs[m]) return;
01354 }
01355
01356 inputDirs.push_back(d);
01357 }
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381 std::string findInputFile(std::string name) {
01382 return app()->findInputFile(name) ;
01383 }
01384
01385 std::string Application::findInputFile(std::string name) {
01386 DIR_LOCK() ;
01387 string::size_type islash = name.find('/');
01388 string::size_type ibslash = name.find('\\');
01389 string inname;
01390 vector<string>& dirs = inputDirs;
01391
01392 int nd;
01393 if (islash == string::npos && ibslash == string::npos) {
01394 nd = static_cast<int>(dirs.size());
01395 int i;
01396 inname = "";
01397 for (i = 0; i < nd; i++) {
01398 inname = dirs[i] + "/" + name;
01399 ifstream fin(inname.c_str());
01400 if (fin) {
01401 fin.close();
01402 return inname;
01403 }
01404 }
01405 string msg;
01406 msg = "\nInput file " + name
01407 + " not found in director";
01408 msg += (nd == 1 ? "y " : "ies ");
01409 for (i = 0; i < nd; i++) {
01410 msg += "\n'" + dirs[i] + "'";
01411 if (i < nd-1) msg += ", ";
01412 }
01413 msg += "\n\n";
01414 msg += "To fix this problem, either:\n";
01415 msg += " a) move the missing files into the local directory;\n";
01416 msg += " b) define environment variable CANTERA_DATA to\n";
01417 msg += " point to the directory containing the file.";
01418 throw CanteraError("findInputFile", msg);
01419 }
01420
01421 return name;
01422 }
01423
01424 doublereal toSI(std::string unit) {
01425 doublereal f = Unit::units()->toSI(unit);
01426 if (f) {
01427 return f;
01428 } else {
01429 throw CanteraError("toSI","unknown unit string: "+unit);
01430 }
01431 return 1.0;
01432 }
01433
01434 doublereal actEnergyToSI(std::string unit) {
01435 doublereal f = Unit::units()->actEnergyToSI(unit);
01436 if (f) {
01437 return f;
01438 }
01439 return 1.0;
01440 }
01441
01442 string canteraRoot() {
01443 char* ctroot = 0;
01444 ctroot = getenv("CANTERA_ROOT");
01445 if (ctroot != 0) {
01446 return string(ctroot);
01447 }
01448 #ifdef CANTERA_ROOT
01449 return string(CANTERA_ROOT);
01450 #else
01451 return "";
01452 #endif
01453
01454 }
01455
01456
01457
01458 CanteraError::CanteraError(std::string proc, std::string msg) {
01459 app()->addError(proc, msg);
01460 }
01461
01462 ArraySizeError::ArraySizeError(std::string proc, int sz, int reqd) :
01463 CanteraError(proc, "Array size ("+int2str(sz)+
01464 ") too small. Must be at least "+int2str(reqd)) {}
01465
01466 ElementRangeError::ElementRangeError(std::string func, int m, int mmax) :
01467 CanteraError(func, "Element index " + int2str(m) +
01468 " outside valid range of 0 to " + int2str(mmax-1)) {}
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480
01481
01482 void deprecatedMethod(std::string classnm, std::string oldnm, std::string newnm) {
01483 writelog(">>>> WARNING: method "+oldnm+" of class "+classnm
01484 +" is deprecated.\n");
01485 writelog(" Use method "+newnm+" instead.\n");
01486 writelog(" (If you want to rescue this method from deprecated\n");
01487 writelog(" status, see http://www.cantera.org/deprecated.html)");
01488 }
01489
01490
01491 void removeAtVersion(std::string func, std::string version) {
01492
01493 writelog("Removed procedure: "+func+"\n");
01494 writelog("Removed in version: "+version+"\n");
01495 throw CanteraError("removeAtVersion: "+ func,"procedure has been removed.");
01496
01497 }
01498
01499
01500
01501
01502
01503
01504
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525
01526 void writelog(const std::string& msg) {
01527 app()->writelog(msg);
01528 }
01529
01530
01531 void Application::Messages::writelog(const std::string& msg) {
01532 logwriter->write(msg);
01533 }
01534
01535
01536 void writelog(const char* msg) {
01537 app()->writelog(msg);
01538 }
01539
01540
01541 void Application::Messages::writelog(const char* pszmsg) {
01542 logwriter->write( pszmsg ) ;
01543 }
01544
01545
01546 void Application::Messages::writelogendl() {
01547 logwriter->writeendl();
01548 }
01549
01550
01551 void writelogf(const char* fmt,...) {
01552 enum { BUFSIZE = 2048 } ;
01553 char sbuf[BUFSIZE] ;
01554
01555 va_list args ;
01556
01557 va_start( args, fmt ) ;
01558
01559 #if defined(WIN32) && defined(MSC_VER)
01560 _vsnprintf( sbuf, BUFSIZE, fmt, args ) ;
01561 #else
01562 vsprintf( sbuf, fmt, args ) ;
01563 #endif
01564
01565 writelog( sbuf ) ;
01566
01567 va_end(args) ;
01568 }
01569
01570 void writelogendl() {
01571 app()->writelogendl();
01572 }
01573
01574
01575
01576 void error(const std::string& msg) {
01577 app()->logerror(msg);
01578 }
01579
01580
01581 void Application::Messages::logerror(const std::string& msg) {
01582 logwriter->error(msg) ;
01583 }
01584
01585
01586 int userInterface() {
01587 return app()->getUserEnv();
01588 }
01589
01590
01591 int Application::Messages::getUserEnv() {
01592 return logwriter->env() ;
01593 }
01594
01595
01596
01597
01598
01599 void setLogger(Logger* logwriter) {
01600 try {
01601 app()->setLogger(logwriter) ;
01602 }
01603 catch (std::bad_alloc) {
01604 logwriter->error("bad alloc thrown by app()");
01605 }
01606 }
01607
01608 void Application::Messages::setLogger(Logger* _logwriter) {
01609 if ( logwriter == _logwriter ) return ;
01610 if (logwriter != 0 ) {
01611 delete logwriter;
01612 logwriter = 0 ;
01613 }
01614 logwriter = _logwriter;
01615 }
01616
01617
01618 #ifdef WITH_HTML_LOGS
01619
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629
01630
01631
01632
01633
01634
01635
01636
01637
01638
01639
01640
01641
01642
01643
01644
01645
01646
01647
01648
01649
01650
01651 void beginLogGroup(std::string title, int loglevel) {
01652 app()->beginLogGroup(title, loglevel) ;
01653 }
01654
01655 void Application::Messages::beginLogGroup(std::string title, int _loglevel ) {
01656
01657 if (_loglevel != -99) {
01658 loglevel = _loglevel;
01659 } else {
01660 loglevel--;
01661 }
01662 if (loglevel <= 0) return;
01663
01664 loglevels.push_back(loglevel);
01665
01666 loggroups.push_back(title);
01667
01668 if (xmllog == 0) {
01669
01670 xmllog = new XML_Node("html");
01671 current = &xmllog->addChild("ul");
01672 }
01673
01674 current = ¤t->addChild("li","<b>"+title+"</b>");
01675 current = ¤t->addChild("ul");
01676 }
01677
01678
01679
01680
01681 void addLogEntry(std::string tag, std::string value) {
01682 app()->addLogEntry(tag, value) ;
01683 }
01684
01685 void Application::Messages::addLogEntry(std::string tag, std::string value) {
01686 if (loglevel > 0 && current)
01687 current->addChild("li",tag+": "+value);
01688 }
01689
01690
01691
01692
01693 void addLogEntry(std::string tag, doublereal value) {
01694 app()->addLogEntry( tag, value ) ;
01695 }
01696
01697 void Application::Messages::addLogEntry(std::string tag, doublereal value) {
01698 if (loglevel > 0 && current)
01699 current->addChild("li",tag+": "+fp2str(value));
01700 }
01701
01702
01703
01704
01705 void addLogEntry(std::string tag, int value) {
01706 app()->addLogEntry( tag, value ) ;
01707 }
01708
01709 void Application::Messages::addLogEntry(std::string tag, int value) {
01710 if (loglevel > 0 && current)
01711 current->addChild("li",tag+": "+int2str(value));
01712 }
01713
01714
01715
01716 void addLogEntry(std::string msg) {
01717 app()->addLogEntry(msg) ;
01718 }
01719
01720 void Application::Messages::addLogEntry(std::string msg) {
01721 if (loglevel > 0 && current)
01722 current->addChild("li",msg);
01723 }
01724
01725
01726
01727
01728
01729
01730
01731
01732 void endLogGroup(std::string title) {
01733 app()->endLogGroup(title) ;
01734 }
01735
01736 void Application::Messages::endLogGroup(std::string title) {
01737 if (loglevel <= 0) return;
01738 AssertThrowMsg(current, "Application::Messages::endLogGroup",
01739 "Error while ending a LogGroup. This is probably due to an unmatched"
01740 " beginnning and ending group");
01741 current = current->parent();
01742 AssertThrowMsg(current, "Application::Messages::endLogGroup",
01743 "Error while ending a LogGroup. This is probably due to an unmatched"
01744 " beginnning and ending group");
01745 current = current->parent();
01746
01747
01748 loglevel = loglevels.back();
01749 loglevels.pop_back();
01750 if (title != "" && title != loggroups.back()) {
01751 writelog("Logfile error."
01752 "\n beginLogGroup: "+ loggroups.back()+
01753 "\n endLogGroup: "+title+"\n");
01754 write_logfile("logerror");
01755 } else if (loggroups.size() == 1) {
01756 write_logfile(loggroups.back()+"_log");
01757 loggroups.clear();
01758 loglevels.clear();
01759 } else {
01760 loggroups.pop_back();
01761 }
01762 }
01763
01764
01765
01766
01767
01768
01769
01770
01771
01772
01773
01774
01775
01776
01777
01778
01779
01780 void write_logfile(std::string file) {
01781 app()->write_logfile(file) ;
01782 }
01783
01784
01785 void Application::Messages::write_logfile(std::string file) {
01786 if (!xmllog) {
01787 return;
01788 }
01789 string::size_type idot = file.rfind('.');
01790 string ext = "";
01791 string nm = file;
01792 if (idot != string::npos) {
01793 ext = file.substr(idot, file.size());
01794 nm = file.substr(0,idot);
01795 }
01796 else {
01797 ext = ".html";
01798 nm = file;
01799 }
01800
01801
01802
01803
01804 string fname = nm + ext;
01805 ifstream f(fname.c_str());
01806 if (f) {
01807 int n = 0;
01808 while (1 > 0) {
01809 n++;
01810 fname = nm + int2str(n) + ext;
01811 ifstream f(fname.c_str());
01812 if (!f) break;
01813 }
01814 }
01815
01816
01817
01818
01819
01820 if (xmllog) {
01821 ofstream f(fname.c_str());
01822
01823 xmllog->root().write(f);
01824 f.close();
01825 writelog("Log file " + fname + " written.\n");
01826 delete xmllog;
01827 xmllog = 0;
01828 current = 0;
01829 }
01830 }
01831
01832 #endif // WITH_HTML_LOGS
01833
01834
01835
01836
01837 static void split_at_pound(const std::string& src, std::string& file, std::string& id) {
01838 string::size_type ipound = src.find('#');
01839 if (ipound != string::npos) {
01840 id = src.substr(ipound+1,src.size());
01841 file = src.substr(0,ipound);
01842 }
01843 else {
01844 id = "";
01845 file = src;
01846 }
01847 }
01848
01849
01850
01851
01852
01853
01854
01855
01856
01857
01858
01859
01860
01861
01862
01863
01864
01865
01866
01867 XML_Node* get_XML_Node(const std::string& file_ID, XML_Node* root) {
01868 std::string fname, idstr;
01869 XML_Node *db, *doc;
01870 split_at_pound(file_ID, fname, idstr);
01871 if (fname == "") {
01872 if (!root) throw CanteraError("get_XML_Node",
01873 "no file name given. file_ID = "+file_ID);
01874 db = root->findID(idstr, 3);
01875 }
01876 else {
01877 doc = get_XML_File(fname);
01878 if (!doc) throw CanteraError("get_XML_Node",
01879 "get_XML_File failed trying to open "+fname);
01880 db = doc->findID(idstr, 3);
01881 }
01882 if (!db) {
01883 throw CanteraError("get_XML_Node",
01884 "id tag '"+idstr+"' not found.");
01885 }
01886 return db;
01887 }
01888
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905
01906
01907
01908
01909
01910
01911
01912
01913
01914 XML_Node* get_XML_NameID(const std::string& nameTarget,
01915 const std::string& file_ID,
01916 XML_Node* root) {
01917 string fname, idTarget;
01918 XML_Node *db, *doc;
01919 split_at_pound(file_ID, fname, idTarget);
01920 if (fname == "") {
01921 if (!root) return 0;
01922 db = root->findNameID(nameTarget, idTarget);
01923 } else {
01924 doc = get_XML_File(fname);
01925 if (!doc) return 0;
01926 db = doc->findNameID(nameTarget, idTarget);
01927 }
01928 return db;
01929 }
01930
01931
01932 std::vector<FactoryBase*> FactoryBase::s_vFactoryRegistry;
01933 }
01934
01935