dllmain.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/tags/0.9.1/src/dllmain.cpp $ 00003 version : $LastChangedRevision: 1656 $ $LastChangedBy: jdetaeye $ 00004 date : $LastChangedDate: 2012-03-27 19:05:34 +0200 (Tue, 27 Mar 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 Lesser General Public License as published * 00013 * by the Free Software Foundation; either version 2.1 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 GNU Lesser * 00019 * General Public License for more details. * 00020 * * 00021 * You should have received a copy of the GNU Lesser General Public * 00022 * License along with this library; if not, write to the Free Software * 00023 * Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * 00024 * USA * 00025 * * 00026 ***************************************************************************/ 00027 00028 #define FREPPLE_CORE 00029 #include "frepple.h" 00030 #include "freppleinterface.h" 00031 using namespace frepple; 00032 #include <sys/stat.h> 00033 00034 00035 DECLARE_EXPORT(const char*) FreppleVersion() 00036 { 00037 return PACKAGE_VERSION; 00038 } 00039 00040 00041 DECLARE_EXPORT(void) FreppleInitialize(int argc, char *argv[]) 00042 { 00043 // Initialize only once 00044 static bool initialized = false; 00045 if (initialized) return; 00046 initialized = true; 00047 00048 // Initialize the libraries 00049 LibraryUtils::initialize(argc, argv); 00050 LibraryModel::initialize(); 00051 LibrarySolver::initialize(); 00052 00053 // Search for the initialization PY file 00054 string init = Environment::searchFile("init.py"); 00055 if (!init.empty()) 00056 { 00057 // Execute the commands in the file 00058 try 00059 { 00060 PythonInterpreter::executeFile(init); 00061 } 00062 catch (...) 00063 { 00064 logger << "Exception caught during execution of 'init.py'" << endl; 00065 throw; 00066 } 00067 } 00068 00069 // Search for the initialization XML file 00070 init = Environment::searchFile("init.xml"); 00071 if (!init.empty()) 00072 { 00073 // Execute the commands in the file 00074 try { XMLInputFile(init).parse(&Plan::instance(),true); } 00075 catch (...) 00076 { 00077 logger << "Exception caught during execution of 'init.xml'" << endl; 00078 throw; 00079 } 00080 } 00081 } 00082 00083 00084 DECLARE_EXPORT(void) FreppleReadXMLData (const char* x, bool validate, bool validateonly) 00085 { 00086 if (!x) return; 00087 if (validateonly) 00088 XMLInputString(x).parse(NULL, true); 00089 else 00090 XMLInputString(x).parse(&Plan::instance(), validate); 00091 } 00092 00093 00094 DECLARE_EXPORT(void) FreppleReadXMLFile (const char* filename, bool validate, bool validateonly) 00095 { 00096 if (!filename) 00097 { 00098 // Read from standard input 00099 xercesc::StdInInputSource in; 00100 if (validateonly) 00101 // When no root object is passed, only the input validation happens 00102 XMLInput().parse(in, NULL, true); 00103 else 00104 XMLInput().parse(in, &Plan::instance(), validate); 00105 } 00106 else if (validateonly) 00107 // Read and validate a file 00108 XMLInputFile(filename).parse(NULL, true); 00109 else 00110 // Read, execute and optionally validate a file 00111 XMLInputFile(filename).parse(&Plan::instance(),validate); 00112 } 00113 00114 00115 DECLARE_EXPORT(void) FreppleReadPythonFile(const char* filename) 00116 { 00117 if (!filename) 00118 throw DataException("No Python file passed to execute"); 00119 PythonInterpreter::executeFile(filename); 00120 } 00121 00122 00123 DECLARE_EXPORT(void) FreppleSaveFile(const char* x) 00124 { 00125 XMLOutputFile o(x); 00126 o.writeElementWithHeader(Tags::tag_plan, &Plan::instance()); 00127 } 00128 00129 00130 /** Closing any resources still used by frePPle.<br> 00131 * Allocated memory is not freed up with this call - for performance 00132 * reasons it is easier to "leak" the memory. The memory is freed when 00133 * the process exits. 00134 */ 00135 DECLARE_EXPORT(void) FreppleExit() 00136 { 00137 // Close the log file 00138 Environment::setLogFile(""); 00139 } 00140 00141 00142 DECLARE_EXPORT(void) FreppleLog(const string& msg) 00143 { 00144 logger << msg << endl; 00145 } 00146 00147 00148 extern "C" DECLARE_EXPORT(void) FreppleLog(const char* msg) 00149 { 00150 logger << msg << endl; 00151 } 00152 00153 00154 extern "C" DECLARE_EXPORT(int) FreppleWrapperInitialize(int argc, char* argv[]) 00155 { 00156 try {FreppleInitialize(argc, argv);} 00157 catch (...) {return EXIT_FAILURE;} 00158 return EXIT_SUCCESS; 00159 } 00160 00161 00162 extern "C" DECLARE_EXPORT(int) FreppleWrapperReadXMLData(char* d, bool v, bool c) 00163 { 00164 try {FreppleReadXMLData(d, v, c);} 00165 catch (...) {return EXIT_FAILURE;} 00166 return EXIT_SUCCESS; 00167 } 00168 00169 00170 extern "C" DECLARE_EXPORT(int) FreppleWrapperReadXMLFile(const char* f, bool v, bool c) 00171 { 00172 try {FreppleReadXMLFile(f, v, c);} 00173 catch (...) {return EXIT_FAILURE;} 00174 return EXIT_SUCCESS; 00175 } 00176 00177 00178 extern "C" DECLARE_EXPORT(int) FreppleWrapperReadPythonFile(const char* f) 00179 { 00180 try {FreppleReadPythonFile(f);} 00181 catch (...) {return EXIT_FAILURE;} 00182 return EXIT_SUCCESS; 00183 } 00184 00185 00186 extern "C" DECLARE_EXPORT(int) FreppleWrapperSaveFile(char* f) 00187 { 00188 try {FreppleSaveFile(f);} 00189 catch (...) {return EXIT_FAILURE;} 00190 return EXIT_SUCCESS; 00191 } 00192 00193 00194 extern "C" DECLARE_EXPORT(int) FreppleWrapperExit() 00195 { 00196 try {FreppleExit();} 00197 catch (...) {return EXIT_FAILURE;} 00198 return EXIT_SUCCESS; 00199 } 00200 00201 00202 /** Used to initialize frePPLe as a Python extension module. */ 00203 PyMODINIT_FUNC initfrepple(void) 00204 { 00205 try {FreppleInitialize(0, NULL);} 00206 catch(const exception& e) 00207 { 00208 logger << "Initialization failed: " << e.what() << endl; 00209 } 00210 catch (...) 00211 { 00212 logger << "Initialization failed: reason unknown" << endl; 00213 } 00214 }