dllmain.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * Copyright (C) 2007-2013 by Johan De Taeye, frePPLe bvba *
4  * *
5  * This library is free software; you can redistribute it and/or modify it *
6  * under the terms of the GNU Affero General Public License as published *
7  * by the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU Affero General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Affero General Public *
16  * License along with this program. *
17  * If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  ***************************************************************************/
20 
21 #define FREPPLE_CORE
22 #include "frepple.h"
23 #include "freppleinterface.h"
24 using namespace frepple;
25 #include <sys/stat.h>
26 
27 
29 {
30  return PACKAGE_VERSION;
31 }
32 
33 
35 {
36  // Initialize only once
37  static bool initialized = false;
38  if (initialized) return;
39  initialized = true;
40 
41  // Initialize the libraries
45 
46  // Search for the initialization PY file
47  string init = Environment::searchFile("init.py");
48  if (!init.empty())
49  {
50  // Execute the commands in the file
51  try
52  {
53  PythonInterpreter::executeFile(init);
54  }
55  catch (...)
56  {
57  logger << "Exception caught during execution of 'init.py'" << endl;
58  throw;
59  }
60  }
61 
62  // Search for the initialization XML file
63  init = Environment::searchFile("init.xml");
64  if (!init.empty())
65  {
66  // Execute the commands in the file
67  try { XMLInputFile(init).parse(&Plan::instance(),true); }
68  catch (...)
69  {
70  logger << "Exception caught during execution of 'init.xml'" << endl;
71  throw;
72  }
73  }
74 }
75 
76 
77 DECLARE_EXPORT(void) FreppleReadXMLData (const char* x, bool validate, bool validateonly)
78 {
79  if (!x) return;
80  if (validateonly)
81  XMLInputString(x).parse(NULL, true);
82  else
83  XMLInputString(x).parse(&Plan::instance(), validate);
84 }
85 
86 
87 DECLARE_EXPORT(void) FreppleReadXMLFile (const char* filename, bool validate, bool validateonly)
88 {
89  if (!filename)
90  {
91  // Read from standard input
92  xercesc::StdInInputSource in;
93  if (validateonly)
94  // When no root object is passed, only the input validation happens
95  XMLInput().parse(in, NULL, true);
96  else
97  XMLInput().parse(in, &Plan::instance(), validate);
98  }
99  else if (validateonly)
100  // Read and validate a file
101  XMLInputFile(filename).parse(NULL, true);
102  else
103  // Read, execute and optionally validate a file
104  XMLInputFile(filename).parse(&Plan::instance(),validate);
105 }
106 
107 
108 DECLARE_EXPORT(void) FreppleReadPythonFile(const char* filename)
109 {
110  if (!filename)
111  throw DataException("No Python file passed to execute");
112  PythonInterpreter::executeFile(filename);
113 }
114 
115 
116 DECLARE_EXPORT(void) FreppleSaveFile(const char* x)
117 {
118  XMLOutputFile o(x);
120 }
121 
122 
123 /** Closing any resources still used by frePPle.<br>
124  * Allocated memory is not freed up with this call - for performance
125  * reasons it is easier to "leak" the memory. The memory is freed when
126  * the process exits.
127  */
129 {
130  // Close the log file
131  Environment::setLogFile("");
132 }
133 
134 
135 DECLARE_EXPORT(void) FreppleLog(const string& msg)
136 {
137  logger << msg << endl;
138 }
139 
140 
141 extern "C" DECLARE_EXPORT(void) FreppleLog(const char* msg)
142 {
143  logger << msg << endl;
144 }
145 
146 
148 {
149  try {FreppleInitialize();}
150  catch (...) {return EXIT_FAILURE;}
151  return EXIT_SUCCESS;
152 }
153 
154 
155 extern "C" DECLARE_EXPORT(int) FreppleWrapperReadXMLData(char* d, bool v, bool c)
156 {
157  try {FreppleReadXMLData(d, v, c);}
158  catch (...) {return EXIT_FAILURE;}
159  return EXIT_SUCCESS;
160 }
161 
162 
163 extern "C" DECLARE_EXPORT(int) FreppleWrapperReadXMLFile(const char* f, bool v, bool c)
164 {
165  try {FreppleReadXMLFile(f, v, c);}
166  catch (...) {return EXIT_FAILURE;}
167  return EXIT_SUCCESS;
168 }
169 
170 
171 extern "C" DECLARE_EXPORT(int) FreppleWrapperReadPythonFile(const char* f)
172 {
173  try {FreppleReadPythonFile(f);}
174  catch (...) {return EXIT_FAILURE;}
175  return EXIT_SUCCESS;
176 }
177 
178 
179 extern "C" DECLARE_EXPORT(int) FreppleWrapperSaveFile(char* f)
180 {
181  try {FreppleSaveFile(f);}
182  catch (...) {return EXIT_FAILURE;}
183  return EXIT_SUCCESS;
184 }
185 
186 
188 {
189  try {FreppleExit();}
190  catch (...) {return EXIT_FAILURE;}
191  return EXIT_SUCCESS;
192 }
193 
194 
195 /** Used to initialize frePPLe as a Python extension module. */
196 #if PY_MAJOR_VERSION >= 3
197 PyMODINIT_FUNC PyInit_frepple(void)
198 #else
199 PyMODINIT_FUNC initfrepple(void)
200 #endif
201 {
202  try
203  {
205 #if PY_MAJOR_VERSION >= 3
206  return PythonInterpreter::getModule();
207 #endif
208  }
209  catch(const exception& e)
210  {
211  logger << "Initialization failed: " << e.what() << endl;
212 #if PY_MAJOR_VERSION >= 3
213  return NULL;
214 #endif
215  }
216  catch (...)
217  {
218  logger << "Initialization failed: reason unknown" << endl;
219 #if PY_MAJOR_VERSION >= 3
220  return NULL;
221 #endif
222  }
223 }