1 
2 ///////////////////////////////////////////////////////////
3 //                                                       //
4 //                         SAGA                          //
5 //                                                       //
6 //      System for Automated Geoscientific Analyses      //
7 //                                                       //
8 //           Application Programming Interface           //
9 //                                                       //
10 //                  Library: SAGA_API                    //
11 //                                                       //
12 //-------------------------------------------------------//
13 //                                                       //
14 //                    tool_library.h                     //
15 //                                                       //
16 //          Copyright (C) 2006 by Olaf Conrad            //
17 //                                                       //
18 //-------------------------------------------------------//
19 //                                                       //
20 // This file is part of 'SAGA - System for Automated     //
21 // Geoscientific Analyses'.                              //
22 //                                                       //
23 // This library is free software; you can redistribute   //
24 // it and/or modify it under the terms of the GNU Lesser //
25 // General Public License as published by the Free       //
26 // Software Foundation, either version 2.1 of the        //
27 // License, or (at your option) any later version.       //
28 //                                                       //
29 // This library is distributed in the hope that it will  //
30 // be useful, but WITHOUT ANY WARRANTY; without even the //
31 // implied warranty of MERCHANTABILITY or FITNESS FOR A  //
32 // PARTICULAR PURPOSE. See the GNU Lesser General Public //
33 // License for more details.                             //
34 //                                                       //
35 // You should have received a copy of the GNU Lesser     //
36 // General Public License along with this program; if    //
37 // not, see <http://www.gnu.org/licenses/>.              //
38 //                                                       //
39 //-------------------------------------------------------//
40 //                                                       //
41 //    contact:    Olaf Conrad                            //
42 //                Institute of Geography                 //
43 //                University of Goettingen               //
44 //                Goldschmidtstr. 5                      //
45 //                37077 Goettingen                       //
46 //                Germany                                //
47 //                                                       //
48 //    e-mail:     oconrad@saga-gis.org                   //
49 //                                                       //
50 ///////////////////////////////////////////////////////////
51 
52 //---------------------------------------------------------
53 #ifndef HEADER_INCLUDED__SAGA_API__tool_library_H
54 #define HEADER_INCLUDED__SAGA_API__tool_library_H
55 
56 
57 ///////////////////////////////////////////////////////////
58 //														 //
59 //														 //
60 //														 //
61 ///////////////////////////////////////////////////////////
62 
63 //---------------------------------------------------------
64 /** \file tool_library.h
65 * The definitions needed for any SAGA tool library and the
66 * tool library management, including the SAGA API's default
67 * tool library manager.
68 * @see SG_Get_Tool_Library_Manager
69 * @see CSG_Tool_Library_Manager
70 * @see CSG_Tool_Library
71 */
72 
73 
74 ///////////////////////////////////////////////////////////
75 //														 //
76 //														 //
77 //														 //
78 ///////////////////////////////////////////////////////////
79 
80 //---------------------------------------------------------
81 #include "tool.h"
82 
83 
84 ///////////////////////////////////////////////////////////
85 //														 //
86 //														 //
87 //														 //
88 ///////////////////////////////////////////////////////////
89 
90 //---------------------------------------------------------
91 typedef enum ESG_Tool_Library_Type
92 {
93 	TOOL_LIBRARY		= 0,
94 	TOOL_CHAINS
95 }
96 TSG_Tool_Library_Type;
97 
98 
99 ///////////////////////////////////////////////////////////
100 //														 //
101 //														 //
102 //														 //
103 ///////////////////////////////////////////////////////////
104 
105 //---------------------------------------------------------
106 class SAGA_API_DLL_EXPORT CSG_Tool_Library
107 {
108 	friend class CSG_Tool_Library_Manager;
109 
110 public:
111 
Get_Type(void)112 	virtual TSG_Tool_Library_Type	Get_Type			(void)	const	{	return( TOOL_LIBRARY );	}
113 
is_Valid(void)114 	bool							is_Valid			(void)	const	{	return( Get_Count() > 0 );	}
115 
Get_File_Name(void)116 	const CSG_String &				Get_File_Name		(void)	const	{	return( m_File_Name    );	}
Get_Library_Name(void)117 	const CSG_String &				Get_Library_Name	(void)	const	{	return( m_Library_Name );	}
118 
119 	virtual CSG_String				Get_Info			(int Type)	const;
Get_Name(void)120 	CSG_String						Get_Name			(void)	const	{	return( Get_Info(TLB_INFO_Name       ) );	}
Get_Description(void)121 	CSG_String						Get_Description		(void)	const	{	return( Get_Info(TLB_INFO_Description) );	}
Get_Author(void)122 	CSG_String						Get_Author			(void)	const	{	return( Get_Info(TLB_INFO_Author     ) );	}
Get_Version(void)123 	CSG_String						Get_Version			(void)	const	{	return( Get_Info(TLB_INFO_Version    ) );	}
Get_Menu(void)124 	CSG_String						Get_Menu			(void)	const	{	return( Get_Info(TLB_INFO_Menu_Path  ) );	}
Get_Category(void)125 	CSG_String						Get_Category		(void)	const	{	return( Get_Info(TLB_INFO_Category   ) );	}
126 
127 	CSG_String						Get_Summary			(int Format = SG_SUMMARY_FMT_HTML, bool bInteractive = true)	const;
128 	bool							Get_Summary			(const CSG_String &Path)	const;
129 
Get_Count(void)130 	virtual int						Get_Count			(void)	const	{	return( m_pInterface ? m_pInterface->Get_Count() : 0 );	}
131 
132 	virtual CSG_Tool *				Get_Tool			(int              Index, TSG_Tool_Type Type = TOOL_TYPE_Base)	const;
133 	virtual CSG_Tool *				Get_Tool			(const CSG_String &Name, TSG_Tool_Type Type = TOOL_TYPE_Base)	const;
134 	virtual CSG_Tool *				Get_Tool			(const char       *Name, TSG_Tool_Type Type = TOOL_TYPE_Base)	const;
135 	virtual CSG_Tool *				Get_Tool			(const wchar_t    *Name, TSG_Tool_Type Type = TOOL_TYPE_Base)	const;
136 
137 	virtual CSG_Tool *				Create_Tool			(int              Index, bool bWithGUI = false);
138 	virtual CSG_Tool *				Create_Tool			(const CSG_String &Name, bool bWithGUI = false);
139 	virtual CSG_Tool *				Create_Tool			(const char       *Name, bool bWithGUI = false);
140 	virtual CSG_Tool *				Create_Tool			(const wchar_t    *Name, bool bWithGUI = false);
141 
142 	virtual bool					Delete_Tool			(CSG_Tool *pTool);
143 	virtual bool					Delete_Tools		(void);
144 
Get_File_Name(int i)145 	virtual CSG_String				Get_File_Name		(int i)	const	{	return( "" );	}
146 	virtual CSG_String				Get_Menu			(int i)	const;
147 
148 	void							Add_Reference			(const CSG_String &Authors, const CSG_String &Year, const CSG_String &Title, const CSG_String &Where, const SG_Char *Link = NULL, const SG_Char *Link_Text = NULL);
149 	void							Add_Reference			(const CSG_String &Link, const SG_Char *Link_Text = NULL);
150 	void							Del_References			(void);
Get_References(void)151 	const CSG_Strings &				Get_References			(void)	const	{	return( m_References );		}
152 
153 
154 protected:
155 
156 	CSG_Tool_Library(void);
157 	CSG_Tool_Library(const CSG_String &File);
158 	virtual ~CSG_Tool_Library(void);
159 
160 
161 	CSG_String						m_File_Name, m_Library_Name;
162 
163 
164 private:
165 
166 	bool							_Destroy			(void);
167 
168 
169 	CSG_Strings						m_References;
170 
171 	CSG_Tool_Library_Interface		*m_pInterface;
172 
173 	class wxDynamicLibrary			*m_pLibrary;
174 
175 };
176 
177 
178 ///////////////////////////////////////////////////////////
179 //														 //
180 //														 //
181 //														 //
182 ///////////////////////////////////////////////////////////
183 
184 //---------------------------------------------------------
185 class SAGA_API_DLL_EXPORT CSG_Tool_Library_Manager
186 {
187 public:
188 	CSG_Tool_Library_Manager(void);
189 	virtual ~CSG_Tool_Library_Manager(void);
190 
191 	bool						Destroy				(void);
192 
Get_Count(void)193 	int							Get_Count			(void)	const	{	return( m_nLibraries );	}
194 	int							Get_Tool_Count		(void)	const;
195 
196 	CSG_Tool_Library *			Add_Library			(const CSG_String &File);
197 	CSG_Tool_Library *			Add_Library			(const char       *File);
198 	CSG_Tool_Library *			Add_Library			(const wchar_t    *File);
199 
200 	int							Add_Directory		(const CSG_String &Directory, bool bOnlySubDirectories = false);
201 	int							Add_Directory		(const char       *Directory, bool bOnlySubDirectories = false);
202 	int							Add_Directory		(const wchar_t    *Directory, bool bOnlySubDirectories = false);
203 
204 	bool						Del_Library			(int i);
205 	bool						Del_Library			(CSG_Tool_Library *pLibrary);
206 
Get_Library(int i)207 	CSG_Tool_Library *			Get_Library			(int i)	const	{	return( i >= 0 && i < Get_Count() ? m_pLibraries[i] : NULL );	}
208 	CSG_Tool_Library *			Get_Library			(const CSG_String &Name, bool bLibrary)	const;
209 	CSG_Tool_Library *			Get_Library			(const char       *Name, bool bLibrary)	const;
210 	CSG_Tool_Library *			Get_Library			(const wchar_t    *Name, bool bLibrary)	const;
211 
212 	bool						is_Loaded			(CSG_Tool_Library *pLibrary)	const;
213 
214 	CSG_Tool *					Get_Tool			(const CSG_String &Library, int              Index)	const;
215 	CSG_Tool *					Get_Tool			(const char       *Library, int              Index)	const;
216 	CSG_Tool *					Get_Tool			(const wchar_t    *Library, int              Index)	const;
217 	CSG_Tool *					Get_Tool			(const CSG_String &Library, const CSG_String &Name)	const;
218 	CSG_Tool *					Get_Tool			(const char       *Library, const char       *Name)	const;
219 	CSG_Tool *					Get_Tool			(const wchar_t    *Library, const wchar_t    *Name)	const;
220 
221 	CSG_Tool *					Create_Tool			(const CSG_String &Library, int              Index, bool bWithGUI = false)	const;
222 	CSG_Tool *					Create_Tool			(const char       *Library, int              Index, bool bWithGUI = false)	const;
223 	CSG_Tool *					Create_Tool			(const wchar_t    *Library, int              Index, bool bWithGUI = false)	const;
224 	CSG_Tool *					Create_Tool			(const CSG_String &Library, const CSG_String &Name, bool bWithGUI = false)	const;
225 	CSG_Tool *					Create_Tool			(const char       *Library, const char       *Name, bool bWithGUI = false)	const;
226 	CSG_Tool *					Create_Tool			(const wchar_t    *Library, const wchar_t    *Name, bool bWithGUI = false)	const;
227 	bool						Delete_Tool			(CSG_Tool *pTool)	const;
228 
229 	CSG_String					Get_Summary			(int Format = SG_SUMMARY_FMT_HTML)	const;
230 	bool						Get_Summary			(const CSG_String &Path)			const;
231 
232 
233 private:
234 
235 	int							m_nLibraries;
236 
237 	CSG_Tool_Library			**m_pLibraries;
238 
239 
240 	CSG_Tool_Library *			_Add_Tool_Chain		(const CSG_String &File);
241 
242 };
243 
244 //---------------------------------------------------------
245 SAGA_API_DLL_EXPORT CSG_Tool_Library_Manager &	SG_Get_Tool_Library_Manager	(void);
246 
247 
248 ///////////////////////////////////////////////////////////
249 //														 //
250 //														 //
251 //														 //
252 ///////////////////////////////////////////////////////////
253 
254 //---------------------------------------------------------
255 #define SG_RUN_TOOL(bRetVal, LIBRARY, TOOL, CONDITION)	{\
256 	\
257 	bRetVal	= false;\
258 	\
259 	CSG_Tool	*pTool	= SG_Get_Tool_Library_Manager().Create_Tool(SG_T(LIBRARY), TOOL);\
260 	\
261 	if(	pTool == NULL )\
262 	{\
263 		SG_UI_Msg_Add_Error(CSG_String::Format("%s [%s]", _TL("could not find tool"), SG_T(LIBRARY)));\
264 	}\
265 	else\
266 	{\
267 		SG_UI_Process_Set_Text(pTool->Get_Name());\
268 		\
269 		pTool->Settings_Push();\
270 		\
271 		if( !pTool->On_Before_Execution() || !(CONDITION) )\
272 		{\
273 			SG_UI_Msg_Add_Error(CSG_String::Format("%s [%s].[%s]", _TL("could not initialize tool"), SG_T(LIBRARY), pTool->Get_Name().c_str()));\
274 		}\
275 		else if( !pTool->Execute(false) )\
276 		{\
277 			SG_UI_Msg_Add_Error(CSG_String::Format("%s [%s].[%s]", _TL("could not execute tool"   ), SG_T(LIBRARY), pTool->Get_Name().c_str()));\
278 		}\
279 		else\
280 		{\
281 			bRetVal	= true;\
282 		}\
283 		\
284 		SG_Get_Tool_Library_Manager().Delete_Tool(pTool);\
285 	}\
286 }
287 
288 #define SG_RUN_TOOL_ExitOnError(LIBRARY, TOOL, CONDITION)	{\
289 	\
290 	bool	bResult;\
291 	\
292 	SG_RUN_TOOL(bResult, LIBRARY, TOOL, CONDITION)\
293 	\
294 	if( !bResult )\
295 	{\
296 		return( false );\
297 	}\
298 }
299 
300 //---------------------------------------------------------
301 #define SG_RUN_TOOL_KEEP_PARMS(bRetVal, LIBRARY, TOOL, PARMS, CONDITION)	{\
302 	\
303 	bRetVal	= false;\
304 	\
305 	CSG_Tool	*pTool	= SG_Get_Tool_Library_Manager().Create_Tool(SG_T(LIBRARY), TOOL);\
306 	\
307 	if(	pTool == NULL )\
308 	{\
309 		SG_UI_Msg_Add_Error(CSG_String::Format("%s [%s]", _TL("could not find tool"), SG_T(LIBRARY)));\
310 	}\
311 	else\
312 	{\
313 		SG_UI_Process_Set_Text(pTool->Get_Name());\
314 		\
315 		pTool->Settings_Push();\
316 		\
317 		if( !pTool->On_Before_Execution() || !(CONDITION) )\
318 		{\
319 			SG_UI_Msg_Add_Error(CSG_String::Format("%s [%s].[%s]", _TL("could not initialize tool"), SG_T(LIBRARY), pTool->Get_Name().c_str()));\
320 		}\
321 		else if( !pTool->Execute(false) )\
322 		{\
323 			SG_UI_Msg_Add_Error(CSG_String::Format("%s [%s].[%s]", _TL("could not execute tool"   ), SG_T(LIBRARY), pTool->Get_Name().c_str()));\
324 		}\
325 		else\
326 		{\
327 			bRetVal	= true;\
328 		}\
329 		\
330 		PARMS.Assign(pTool->Get_Parameters());\
331 		\
332 		SG_Get_Tool_Library_Manager().Delete_Tool(pTool);\
333 	}\
334 }
335 
336 #define SG_RUN_TOOL_KEEP_PARMS_ExitOnError(LIBRARY, TOOL, PARMS, CONDITION)	{\
337 	\
338 	bool	bResult;\
339 	\
340 	SG_RUN_TOOL_KEEP_PARMS(bResult, LIBRARY, TOOL, PARMS, CONDITION)\
341 	\
342 	if( !bResult )\
343 	{\
344 		return( false );\
345 	}\
346 }
347 
348 //---------------------------------------------------------
349 #define SG_TOOL_PARAMETER_SET(IDENTIFIER, VALUE)	pTool->Get_Parameters()->Set_Parameter(IDENTIFIER, VALUE)
350 
351 #define SG_TOOL_PARAMLIST_ADD(IDENTIFIER, VALUE)	(\
352 		pTool->Get_Parameters()->Get_Parameter(IDENTIFIER)\
353 	&&	pTool->Get_Parameters()->Get_Parameter(IDENTIFIER)->asList()\
354 	&&	pTool->Get_Parameters()->Get_Parameter(IDENTIFIER)->asList()->Add_Item(VALUE)\
355 )
356 
357 #define SG_TOOL_SET_DATAOBJECT_LIST(IDENTIFIER, VALUE)	(\
358 		pTool->Get_Parameters()->Get_Parameter(IDENTIFIER)\
359 	&&	pTool->Get_Parameters()->Get_Parameter(IDENTIFIER)->asList()\
360 	&&	pTool->Get_Parameters()->Get_Parameter(IDENTIFIER)->asList()->Assign(VALUE)\
361 )
362 
363 
364 ///////////////////////////////////////////////////////////
365 //														 //
366 //														 //
367 //														 //
368 ///////////////////////////////////////////////////////////
369 
370 //---------------------------------------------------------
371 #endif // #ifndef HEADER_INCLUDED__SAGA_API__tool_library_H
372