dllmain.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 file : $URL: http://svn.code.sf.net/p/frepple/code/trunk/src/dllmain.cpp $ 00003 version : $LastChangedRevision: 1713 $ $LastChangedBy: jdetaeye $ 00004 date : $LastChangedDate: 2012-07-18 11:46:01 +0200 (Wed, 18 Jul 2012) $ 00005 ***************************************************************************/ 00006 00007 /*************************************************************************** 00008 * * 00009 * Copyright (C) 2007-2012 by Johan De Taeye, frePPLe bvba * 00010 * * 00011 * This library is free software; you can redistribute it and/or modify it * 00012 * under the terms of the GNU Affero General Public License as published * 00013 * by the Free Software Foundation; either version 3 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 * This library is distributed in the hope that it will be useful, * 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00019 * GNU Affero General Public License for more details. * 00020 * * 00021 * You should have received a copy of the GNU Affero General Public * 00022 * License along with this program. * 00023 * If not, see <http://www.gnu.org/licenses/>. * 00024 * * 00025 ***************************************************************************/ 00026 00027 #define FREPPLE_CORE 00028 #include "frepple.h" 00029 #include "freppleinterface.h" 00030 using namespace frepple; 00031 #include <sys/stat.h> 00032 00033 00034 DECLARE_EXPORT(const char*) FreppleVersion() 00035 { 00036 return PACKAGE_VERSION; 00037 } 00038 00039 00040 DECLARE_EXPORT(void) FreppleInitialize(int argc, char *argv[]) 00041 { 00042 // Initialize only once 00043 static bool initialized = false; 00044 if (initialized) return; 00045 initialized = true; 00046 00047 // Initialize the libraries 00048 LibraryUtils::initialize(argc, argv); 00049 LibraryModel::initialize(); 00050 LibrarySolver::initialize(); 00051 00052 // Search for the initialization PY file 00053 string init = Environment::searchFile("init.py"); 00054 if (!init.empty()) 00055 { 00056 // Execute the commands in the file 00057 try 00058 { 00059 PythonInterpreter::executeFile(init); 00060 } 00061 catch (...) 00062 { 00063 logger << "Exception caught during execution of 'init.py'" << endl; 00064 throw; 00065 } 00066 } 00067 00068 // Search for the initialization XML file 00069 init = Environment::searchFile("init.xml"); 00070 if (!init.empty()) 00071 { 00072 // Execute the commands in the file 00073 try { XMLInputFile(init).parse(&Plan::instance(),true); } 00074 catch (...) 00075 { 00076 logger << "Exception caught during execution of 'init.xml'" << endl; 00077 throw; 00078 } 00079 } 00080 } 00081 00082 00083 DECLARE_EXPORT(void) FreppleReadXMLData (const char* x, bool validate, bool validateonly) 00084 { 00085 if (!x) return; 00086 if (validateonly) 00087 XMLInputString(x).parse(NULL, true); 00088 else 00089 XMLInputString(x).parse(&Plan::instance(), validate); 00090 } 00091 00092 00093 DECLARE_EXPORT(void) FreppleReadXMLFile (const char* filename, bool validate, bool validateonly) 00094 { 00095 if (!filename) 00096 { 00097 // Read from standard input 00098 xercesc::StdInInputSource in; 00099 if (validateonly) 00100 // When no root object is passed, only the input validation happens 00101 XMLInput().parse(in, NULL, true); 00102 else 00103 XMLInput().parse(in, &Plan::instance(), validate); 00104 } 00105 else if (validateonly) 00106 // Read and validate a file 00107 XMLInputFile(filename).parse(NULL, true); 00108 else 00109 // Read, execute and optionally validate a file 00110 XMLInputFile(filename).parse(&Plan::instance(),validate); 00111 } 00112 00113 00114 DECLARE_EXPORT(void) FreppleReadPythonFile(const char* filename) 00115 { 00116 if (!filename) 00117 throw DataException("No Python file passed to execute"); 00118 PythonInterpreter::executeFile(filename); 00119 } 00120 00121 00122 DECLARE_EXPORT(void) FreppleSaveFile(const char* x) 00123 { 00124 XMLOutputFile o(x); 00125 o.writeElementWithHeader(Tags::tag_plan, &Plan::instance()); 00126 } 00127 00128 00129 /** Closing any resources still used by frePPle.<br> 00130 * Allocated memory is not freed up with this call - for performance 00131 * reasons it is easier to "leak" the memory. The memory is freed when 00132 * the process exits. 00133 */ 00134 DECLARE_EXPORT(void) FreppleExit() 00135 { 00136 // Close the log file 00137 Environment::setLogFile(""); 00138 } 00139 00140 00141 DECLARE_EXPORT(void) FreppleLog(const string& msg) 00142 { 00143 logger << msg << endl; 00144 } 00145 00146 00147 extern "C" DECLARE_EXPORT(void) FreppleLog(const char* msg) 00148 { 00149 logger << msg << endl; 00150 } 00151 00152 00153 extern "C" DECLARE_EXPORT(int) FreppleWrapperInitialize(int argc, char* argv[]) 00154 { 00155 try {FreppleInitialize(argc, argv);} 00156 catch (...) {return EXIT_FAILURE;} 00157 return EXIT_SUCCESS; 00158 } 00159 00160 00161 extern "C" DECLARE_EXPORT(int) FreppleWrapperReadXMLData(char* d, bool v, bool c) 00162 { 00163 try {FreppleReadXMLData(d, v, c);} 00164 catch (...) {return EXIT_FAILURE;} 00165 return EXIT_SUCCESS; 00166 } 00167 00168 00169 extern "C" DECLARE_EXPORT(int) FreppleWrapperReadXMLFile(const char* f, bool v, bool c) 00170 { 00171 try {FreppleReadXMLFile(f, v, c);} 00172 catch (...) {return EXIT_FAILURE;} 00173 return EXIT_SUCCESS; 00174 } 00175 00176 00177 extern "C" DECLARE_EXPORT(int) FreppleWrapperReadPythonFile(const char* f) 00178 { 00179 try {FreppleReadPythonFile(f);} 00180 catch (...) {return EXIT_FAILURE;} 00181 return EXIT_SUCCESS; 00182 } 00183 00184 00185 extern "C" DECLARE_EXPORT(int) FreppleWrapperSaveFile(char* f) 00186 { 00187 try {FreppleSaveFile(f);} 00188 catch (...) {return EXIT_FAILURE;} 00189 return EXIT_SUCCESS; 00190 } 00191 00192 00193 extern "C" DECLARE_EXPORT(int) FreppleWrapperExit() 00194 { 00195 try {FreppleExit();} 00196 catch (...) {return EXIT_FAILURE;} 00197 return EXIT_SUCCESS; 00198 } 00199 00200 00201 /** Used to initialize frePPLe as a Python extension module. */ 00202 PyMODINIT_FUNC initfrepple(void) 00203 { 00204 try {FreppleInitialize(0, NULL);} 00205 catch(const exception& e) 00206 { 00207 logger << "Initialization failed: " << e.what() << endl; 00208 } 00209 catch (...) 00210 { 00211 logger << "Initialization failed: reason unknown" << endl; 00212 } 00213 }