dllmain.cpp
Go to the documentation of this file.00001 /*************************************************************************** 00002 file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/trunk/src/dllmain.cpp $ 00003 version : $LastChangedRevision: 652 $ $LastChangedBy: jdetaeye $ 00004 date : $LastChangedDate: 2008-01-09 17:48:42 +0100 (Wed, 09 Jan 2008) $ 00005 ***************************************************************************/ 00006 00007 /*************************************************************************** 00008 * * 00009 * Copyright (C) 2007 by Johan De Taeye * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA * 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 #if defined(WIN32) 00035 // This function is only applicable for the windows operating systems 00036 // and when it hasn't been explicitly disabled by setting the STATIC variable. 00037 #define WIN32_LEAN_AND_MEAN 00038 #include <windows.h> 00039 00040 BOOL APIENTRY DllMain(HANDLE hInst, DWORD ul_reason_for_call, LPVOID lpReserved) 00041 { 00042 switch (ul_reason_for_call) 00043 { 00044 case DLL_PROCESS_ATTACH: 00045 // Loading the library 00046 try { FreppleInitialize(NULL); } 00047 catch (exception& e) 00048 { 00049 logger << "Error: " << e.what() << endl; 00050 return FALSE; 00051 } 00052 catch (...) 00053 { 00054 logger << "Error: Unknown exception type" << endl; 00055 return FALSE; 00056 } 00057 return TRUE; 00058 00059 case DLL_PROCESS_DETACH: 00060 // Unloading the library 00061 FreppleWrapperExit(); 00062 return TRUE; 00063 } 00064 return TRUE; 00065 } 00066 #endif 00067 00068 00069 DECLARE_EXPORT(const char*) FreppleVersion() 00070 { 00071 return PACKAGE_VERSION; 00072 } 00073 00074 00075 DECLARE_EXPORT(void) FreppleInitialize(const char* h) 00076 { 00077 // Initialize only once 00078 static bool initialized = false; 00079 if (initialized) return; 00080 initialized = true; 00081 00082 // If a parameter is given we set the environment variable FREPPLE_HOME. 00083 // If the parameter is NULL, we pick up the existing value of that 00084 // variable. 00085 if (h) Environment::setHomeDirectory(h); 00086 else 00087 { 00088 const char *c = getenv("FREPPLE_HOME"); 00089 if (c) Environment::setHomeDirectory(c); 00090 else logger << "Warning: No valid home directory specified" << endl; 00091 } 00092 00093 // Initialize the libraries 00094 LibraryModel::initialize(); // also initializes the utils library 00095 LibrarySolver::initialize(); 00096 00097 // Search for the initialization file 00098 if (!Environment::getHomeDirectory().empty()) 00099 { 00100 string init(Environment::getHomeDirectory()); 00101 init += "init.xml"; 00102 struct stat stat_p; 00103 if (!stat(init.c_str(), &stat_p)) 00104 { 00105 // File exists 00106 if (!(stat_p.st_mode & S_IREAD)) 00107 // File exists but is not readable 00108 logger << "Warning: Initialization file 'init.xml'" 00109 << " exists but is not readable" << endl; 00110 else 00111 // Execute the commands in the file 00112 try{ CommandReadXMLFile(init).execute(); } 00113 catch (...) 00114 { 00115 logger << "Exception caught during execution of 'init.xml'" << endl; 00116 throw; 00117 } 00118 } 00119 } 00120 } 00121 00122 00123 DECLARE_EXPORT(void) FreppleReadXMLData (char* x, bool validate, bool validateonly) 00124 { 00125 if (x) CommandReadXMLString(string(x), validate, validateonly).execute(); 00126 } 00127 00128 00129 DECLARE_EXPORT(void) FreppleReadXMLFile (const char* x, bool validate, bool validateonly) 00130 { 00131 CommandReadXMLFile(x, validate, validateonly).execute(); 00132 } 00133 00134 00135 DECLARE_EXPORT(void) FreppleSaveFile(char* x) 00136 { 00137 CommandSave(x).execute(); 00138 } 00139 00140 00141 DECLARE_EXPORT(string) FreppleSaveString() 00142 { 00143 XMLOutputString x; 00144 x.writeElementWithHeader(Tags::tag_plan, &Plan::instance()); 00145 return x.getData(); 00146 } 00147 00148 00149 DECLARE_EXPORT(void) FreppleExit() 00150 { 00151 // Shut down the application that loaded frePPLe as a dynamic library 00152 Environment::setLogFile(""); // Close the log file 00153 std::exit(EXIT_SUCCESS); 00154 } 00155 00156 00157 DECLARE_EXPORT(void) FreppleLog(const string msg) 00158 { 00159 logger << msg << endl; 00160 } 00161 00162 00163 extern "C" DECLARE_EXPORT(void) FreppleLog(const char* msg) 00164 { 00165 logger << msg << endl; 00166 } 00167 00168 00169 extern "C" DECLARE_EXPORT(int) FreppleWrapperInitialize(const char* h) 00170 { 00171 try {FreppleInitialize(h);} 00172 catch (...) {return EXIT_FAILURE;} 00173 return EXIT_SUCCESS; 00174 } 00175 00176 00177 extern "C" DECLARE_EXPORT(int) FreppleWrapperReadXMLData(char* d, bool v, bool c) 00178 { 00179 try {FreppleReadXMLData(d, v, c);} 00180 catch (...) {return EXIT_FAILURE;} 00181 return EXIT_SUCCESS; 00182 } 00183 00184 00185 extern "C" DECLARE_EXPORT(int) FreppleWrapperReadXMLFile(const char* f, bool v, bool c) 00186 { 00187 try {FreppleReadXMLFile(f, v, c);} 00188 catch (...) {return EXIT_FAILURE;} 00189 return EXIT_SUCCESS; 00190 } 00191 00192 00193 extern "C" DECLARE_EXPORT(int) FreppleWrapperSaveFile(char* f) 00194 { 00195 try {FreppleSaveFile(f);} 00196 catch (...) {return EXIT_FAILURE;} 00197 return EXIT_SUCCESS; 00198 } 00199 00200 00201 extern "C" DECLARE_EXPORT(int) FreppleWrapperSaveString(char* buf, unsigned long sz) 00202 { 00203 try 00204 { 00205 // Get the result 00206 string result = FreppleSaveString(); 00207 // Copy into the reply buffer 00208 unsigned long l = result.size(); 00209 memcpy(buf, result.data(), l>sz ? sz : l); 00210 } 00211 catch (...) {return EXIT_FAILURE;} 00212 return EXIT_SUCCESS; 00213 } 00214 00215 00216 extern "C" DECLARE_EXPORT(int) FreppleWrapperExit() 00217 { 00218 try {FreppleExit();} 00219 catch (...) {return EXIT_FAILURE;} 00220 return EXIT_SUCCESS; 00221 } 00222
Documentation generated by
