SAGA API v9.10
Loading...
Searching...
No Matches
tool_library.cpp
Go to the documentation of this file.
1
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.cpp //
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// //
51
52//---------------------------------------------------------
53#include <wx/dynlib.h>
54#include <wx/dir.h>
55#include <wx/filename.h>
56#include <wx/utils.h>
57
58#include "tool_chain.h"
59
60
62// //
63// //
64// //
66
67//---------------------------------------------------------
69{
70 m_pInterface = NULL;
71 m_pLibrary = NULL;
72}
73
74//---------------------------------------------------------
76{
77 m_pLibrary = new wxDynamicLibrary(SG_File_Get_Path_Absolute(File).c_str(), wxDL_DEFAULT|wxDL_QUIET);
78
79 if( m_pLibrary->IsLoaded()
80 && m_pLibrary->HasSymbol(SYMBOL_TLB_Get_Interface)
81 && m_pLibrary->HasSymbol(SYMBOL_TLB_Initialize)
82 && m_pLibrary->HasSymbol(SYMBOL_TLB_Finalize)
83 && ((TSG_PFNC_TLB_Initialize)m_pLibrary->GetSymbol(SYMBOL_TLB_Initialize))(File) )
84 {
85 m_pInterface = ((TSG_PFNC_TLB_Get_Interface)m_pLibrary->GetSymbol(SYMBOL_TLB_Get_Interface))();
86
87 if( m_pInterface->Get_Count() > 0 )
88 {
89 m_File_Name = m_pInterface->Get_Info(TLB_INFO_File );
90 m_Library_Name = m_pInterface->Get_Info(TLB_INFO_Library);
91
92 return; // success
93 }
94 }
95
96 _Destroy();
97}
98
99//---------------------------------------------------------
101{
102 _Destroy();
103}
104
105//---------------------------------------------------------
106bool CSG_Tool_Library::_Destroy(void)
107{
108 if( m_pLibrary )
109 {
110 if( m_pLibrary->IsLoaded() && m_pLibrary->HasSymbol(SYMBOL_TLB_Finalize) )
111 {
112 TSG_PFNC_TLB_Finalize TLB_Finalize = (TSG_PFNC_TLB_Finalize)m_pLibrary->GetSymbol(SYMBOL_TLB_Finalize);
113
114 TLB_Finalize();
115 }
116
117 delete(m_pLibrary);
118
119 m_pLibrary = NULL;
120 }
121
122 m_pInterface = NULL;
123
124 return( true );
125}
126
127
129// //
131
132//---------------------------------------------------------
134{
135 if( m_pInterface != NULL )
136 {
137 return( m_pInterface->Get_Info(Type) );
138 }
139
140 return( "" );
141}
142
143
145// //
147
148//---------------------------------------------------------
150{
151 CSG_Tool *pTool = m_pInterface && Index >= 0 && Index < Get_Count() ? m_pInterface->Get_Tool(Index) : NULL;
152
153 return( pTool && (Type == TOOL_TYPE_Base || Type == pTool->Get_Type()) ? pTool : NULL );
154}
155
156//---------------------------------------------------------
157CSG_Tool * CSG_Tool_Library::Get_Tool(const char *Name, TSG_Tool_Type Type) const { return( Get_Tool(CSG_String(Name), Type) ); }
158CSG_Tool * CSG_Tool_Library::Get_Tool(const wchar_t *Name, TSG_Tool_Type Type) const { return( Get_Tool(CSG_String(Name), Type) ); }
160{
161 for(int i=0; i<Get_Count(); i++)
162 {
163 CSG_Tool *pTool = Get_Tool(i, Type);
164
165 if( pTool && (!pTool->Get_ID().Cmp(Name) || !pTool->Get_Name().Cmp(Name)) )
166 {
167 return( pTool );
168 }
169 }
170
171 return( NULL );
172}
173
174
176// //
178
179//---------------------------------------------------------
189//---------------------------------------------------------
190CSG_Tool * CSG_Tool_Library::Create_Tool(int Index, bool bWithGUI, bool bWithCMD)
191{
192 return( m_pInterface ? m_pInterface->Create_Tool(Index, bWithGUI, bWithCMD) : NULL );
193}
194
195//---------------------------------------------------------
196CSG_Tool * CSG_Tool_Library::Create_Tool(const char *Name, bool bWithGUI, bool bWithCMD) { return( Create_Tool(CSG_String(Name), bWithGUI, bWithCMD) ); }
197CSG_Tool * CSG_Tool_Library::Create_Tool(const wchar_t *Name, bool bWithGUI, bool bWithCMD) { return( Create_Tool(CSG_String(Name), bWithGUI, bWithCMD) ); }
198CSG_Tool * CSG_Tool_Library::Create_Tool(const CSG_String &Name, bool bWithGUI, bool bWithCMD)
199{
200 int Index; return( Name.asInt(Index) ? Create_Tool(Index, bWithGUI, bWithCMD) : NULL );
201}
202
203//---------------------------------------------------------
208//---------------------------------------------------------
210{
211 return( m_pInterface ? m_pInterface->Delete_Tool(pTool) : false );
212}
213
214//---------------------------------------------------------
216{
217 return( m_pInterface ? m_pInterface->Delete_Tools() : false );
218}
219
220
222// //
224
225//---------------------------------------------------------
227{
228 if( Get_Tool(i) )
229 {
230 return( Get_Tool(i)->Get_MenuPath(true) );
231 }
232
233 return( "" );
234}
235
236
238// //
240
241//---------------------------------------------------------
242void CSG_Tool_Library::Add_Reference(const CSG_String &Authors, const CSG_String &Year, const CSG_String &Title, const CSG_String &Where, const SG_Char *Link, const SG_Char *Link_Text)
243{
244 CSG_String Reference = Authors;
245
246 Reference.Printf("<b>%s (%s):</b> %s. %s", Authors.c_str(), Year.c_str(), Title.c_str(), Where.c_str());
247
248 if( Link && *Link )
249 {
250 Reference += CSG_String::Format(" <a href=\"%s\">%s</a>.", Link, Link_Text && *Link_Text ? Link_Text : Link);
251 }
252
253 if( !Reference.is_Empty() )
254 {
255 m_References += Reference;
256 }
257
258 m_References.Sort();
259}
260
261//---------------------------------------------------------
262void CSG_Tool_Library::Add_Reference(const CSG_String &Link, const SG_Char *Link_Text)
263{
264 m_References += CSG_String::Format("<a href=\"%s\">%s</a>", Link.c_str(), Link_Text && *Link_Text ? Link_Text : Link.c_str());
265
266 m_References.Sort();
267}
268
269//---------------------------------------------------------
271{
272 m_References.Clear();
273}
274
275
277// //
278// //
279// //
281
282//---------------------------------------------------------
284
285//---------------------------------------------------------
290
291
293// //
295
296//---------------------------------------------------------
298{
299 m_pLibraries = NULL;
300 m_nLibraries = 0;
301
302 if( this == &g_Tool_Library_Manager )
303 {
304 CSG_Random::Initialize(); // initialize with current time on startup
305 }
306}
307
308//---------------------------------------------------------
313
314
316// //
318
319//---------------------------------------------------------
321{
322 int nTools = 0;
323
324 for(int i=0; i<m_nLibraries; i++)
325 {
326 nTools += m_pLibraries[i]->Get_Count();
327 }
328
329 return( nTools );
330}
331
332
334// //
336
337//---------------------------------------------------------
339{
340 if( bVerbose == false )
341 {
342 SG_UI_Msg_Lock(true);
343 }
344
345 //-----------------------------------------------------
346 #if defined(_SAGA_MSW)
347 {
349 }
350 #elif defined(__WXMAC__)
351 {
352 if( SG_Get_Tool_Library_Manager().Add_Directory(SG_UI_Get_Application_Path(true) + "/../Tools", false) < 1 )
353 {
354 #ifdef TOOLS_PATH
356 #endif
357
358 #ifdef SHARE_PATH
359 SG_Get_Tool_Library_Manager().Add_Directory(SG_File_Make_Path(SHARE_PATH, "toolchains")); // look for tool chains
360 #endif
361 }
362 }
363 #else // #if defined(_SAGA_LINUX)
364 {
365 #ifdef TOOLS_PATH
367 #endif
368
369 #ifdef SHARE_PATH
370 SG_Get_Tool_Library_Manager().Add_Directory(SG_File_Make_Path(SHARE_PATH, "toolchains")); // look for tool chains
371 #endif
372 }
373 #endif
374
375 //-----------------------------------------------------
376 CSG_String Paths;
377
378 if( SG_Get_Environment("SAGA_TLB", &Paths) )
379 {
380 #if defined(_SAGA_MSW)
381 CSG_Strings Path = SG_String_Tokenize(Paths, ";" ); // colon (':') would split drive from paths!
382 #else // #if defined(_SAGA_LINUX) || defined(__WXMAC__)
383 CSG_Strings Path = SG_String_Tokenize(Paths, ";:"); // colon (':') is more native to non-windows os than semi-colon (';'), we support both...
384 #endif
385
386 for(int i=0; i<Path.Get_Count(); i++)
387 {
389 }
390 }
391
392 //-----------------------------------------------------
393 if( bVerbose == false )
394 {
395 SG_UI_Msg_Lock(false);
396 }
397
398 return( Get_Count() > 0 );
399}
400
401
403// //
405
406//---------------------------------------------------------
410{
411 if( SG_File_Exists(File) == false )
412 {
413 return( NULL );
414 }
415
416 if( SG_File_Cmp_Extension(File, "xml") )
417 {
418 return( _Add_Tool_Chain(File) );
419 }
420
421 if( SG_File_Cmp_Extension(File, "dll" ) == false
422 && SG_File_Cmp_Extension(File, "dylib") == false
423 && SG_File_Cmp_Extension(File, "so" ) == false )
424 {
425 return( NULL );
426 }
427
428 //-----------------------------------------------------
429 wxFileName FileName(File.c_str());
430
431 SG_UI_Msg_Add(CSG_String::Format("%s: %s...", _TL("Loading library"), File.c_str()), true);
432
433 for(int i=0; i<Get_Count(); i++)
434 {
435 if( FileName == Get_Library(i)->Get_File_Name().c_str() )
436 {
437 SG_UI_Msg_Add(_TL("has already been loaded"), false);
438
439 return( Get_Library(i) );
440 }
441 }
442
443 //-----------------------------------------------------
444 CSG_Tool_Library *pLibrary = new CSG_Tool_Library(File);
445
446 if( pLibrary->is_Valid() )
447 {
448 m_pLibraries = (CSG_Tool_Library **)SG_Realloc(m_pLibraries, (m_nLibraries + 1) * sizeof(CSG_Tool_Library *));
449 m_pLibraries[m_nLibraries++] = pLibrary;
450
452
453 return( pLibrary );
454 }
455
456 delete(pLibrary);
457
458 SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE);
459
460 return( NULL );
461}
462
463//---------------------------------------------------------
464int CSG_Tool_Library_Manager::Add_Directory(const char *Directory, bool bOnlySubDirectories) { return( Add_Directory(CSG_String(Directory), bOnlySubDirectories) ); }
465int CSG_Tool_Library_Manager::Add_Directory(const wchar_t *Directory, bool bOnlySubDirectories) { return( Add_Directory(CSG_String(Directory), bOnlySubDirectories) ); }
466int CSG_Tool_Library_Manager::Add_Directory(const CSG_String &Directory, bool bOnlySubDirectories)
467{
468 int nOpened = 0; wxDir Dir;
469
470 if( Dir.Open(Directory.c_str()) )
471 {
472 wxString FileName, DirName(Dir.GetName());
473
474 if( !bOnlySubDirectories && Dir.GetFirst(&FileName, wxEmptyString, wxDIR_FILES) )
475 {
476 do
477 { if( FileName.Find("saga_") < 0 && FileName.Find("wx") < 0 )
478 if( Add_Library(SG_File_Make_Path(&DirName, &FileName)) )
479 {
480 nOpened++;
481 }
482 }
483 while( Dir.GetNext(&FileName) );
484 }
485
486 if( Dir.GetFirst(&FileName, wxEmptyString, wxDIR_DIRS) )
487 {
488 do
489 {
490 if( FileName.CmpNoCase("dll") )
491 {
492 nOpened += Add_Directory(SG_File_Make_Path(&DirName, &FileName), false);
493 }
494 }
495 while( Dir.GetNext(&FileName) );
496 }
497 }
498
499 return( nOpened );
500}
501
502
504// //
506
507//---------------------------------------------------------
508CSG_Tool_Library * CSG_Tool_Library_Manager::_Add_Tool_Chain(const CSG_String &File, bool bReload)
509{
510 if( !SG_File_Cmp_Extension(File, "xml") )
511 {
512 return( NULL );
513 }
514
515 //-----------------------------------------------------
516 CSG_Tool_Chains *pLibrary = NULL;
517 CSG_Tool_Chain *pTool = NULL;
518
519 //-----------------------------------------------------
520 wxFileName FileName(File.c_str()); // check if tool chain is already loaded ?
521
522 for(int iLibrary=0; !pTool && iLibrary<Get_Count(); iLibrary++)
523 {
524 if( Get_Library(iLibrary)->Get_Type() == ESG_Library_Type::Chain )
525 {
526 for(int iTool=0; !pTool && iTool<Get_Library(iLibrary)->Get_Count(); iTool++)
527 {
528 if( FileName == ((CSG_Tool_Chain *)Get_Library(iLibrary)->Get_Tool(iTool))->Get_File_Name().c_str() )
529 {
530 pLibrary = (CSG_Tool_Chains *)Get_Library(iLibrary);
531 pTool = (CSG_Tool_Chain *)Get_Library(iLibrary)->Get_Tool(iTool);
532 }
533 }
534 }
535 }
536
537 if( pTool )
538 {
539 if( bReload ) // ...then try to reload !
540 {
542 CSG_Tool_Chain Tool(File); // don't reset loaded tool in case reloading fails!!!
544
545 if( Tool.is_Okay() )
546 {
547 pTool->Create(File);
548 }
549 }
550
551 return( pLibrary );
552 }
553
554 //-----------------------------------------------------
555 pTool = new CSG_Tool_Chain(File);
556
557 if( !pTool || !pTool->is_Okay() )
558 {
559 if( pTool )
560 {
561 delete(pTool);
562 }
563
564 return( NULL );
565 }
566
567 //-----------------------------------------------------
568 CSG_String Library = pTool->Get_Library();
569
570 for(int iLibrary=0; !pLibrary && iLibrary<Get_Count(); iLibrary++)
571 {
572 if( Get_Library(iLibrary)->Get_Type() == ESG_Library_Type::Chain
573 && Get_Library(iLibrary)->Get_Library_Name().Cmp(Library) == 0 )
574 {
575 pLibrary = (CSG_Tool_Chains *)Get_Library(iLibrary);
576 }
577 }
578
579 if( !pLibrary && (pLibrary = new CSG_Tool_Chains(pTool->Get_Library(), SG_File_Get_Path(File))) != NULL )
580 {
581 m_pLibraries = (CSG_Tool_Library **)SG_Realloc(m_pLibraries, (m_nLibraries + 1) * sizeof(CSG_Tool_Library *));
582 m_pLibraries[m_nLibraries++] = pLibrary;
583 }
584
585 if( !pLibrary ) // this should never happen, but who knows...
586 {
587 SG_UI_Msg_Add_Error(CSG_String::Format("%s %s: %s", _TL("ERROR"), _TL("tool chain library"), File.c_str()));
588
589 delete(pTool);
590
591 return( NULL );
592 }
593
594 pLibrary->Add_Tool(pTool);
595
596 //-----------------------------------------------------
597 return( pLibrary );
598}
599
600
602// //
604
605//---------------------------------------------------------
607{
608 if( m_pLibraries )
609 {
610 for(int i=0; i<Get_Count(); i++)
611 {
612// #ifndef _SAGA_MSW
613 if( !SG_UI_Get_Window_Main() && m_pLibraries[i]->m_pLibrary )
614 {
615 m_pLibraries[i]->m_pLibrary->Detach();
616 }
617// #endif
618
619 delete(m_pLibraries[i]);
620 }
621
622 SG_Free(m_pLibraries);
623
624 m_pLibraries = NULL;
625 m_nLibraries = 0;
626 }
627
628 return( true );
629}
630
631
633// //
635
636//---------------------------------------------------------
638{
639 for(int i=0; i<Get_Count(); i++)
640 {
641 if( pLibrary == Get_Library(i) )
642 {
643 return( Del_Library(i) );
644 }
645 }
646
647 return( false );
648}
649
650//---------------------------------------------------------
652{
653 if( i >= 0 && i < Get_Count() )
654 {
655 delete(m_pLibraries[i]);
656
657 for(m_nLibraries--; i<m_nLibraries; i++)
658 {
659 m_pLibraries[i] = m_pLibraries[i + 1];
660 }
661
662 m_pLibraries = (CSG_Tool_Library **)SG_Realloc(m_pLibraries, m_nLibraries * sizeof(CSG_Tool_Library *));
663
664 return( true );
665 }
666
667 return( false );
668}
669
670//---------------------------------------------------------
672{
673 for(int i=0; i<Get_Count(); i++)
674 {
675 if( pLibrary == Get_Library(i) )
676 {
677 return( true );
678 }
679 }
680
681 return( false );
682}
683
684
686// //
688
689//---------------------------------------------------------
690CSG_Tool_Library * CSG_Tool_Library_Manager::Get_Library(const char *Name, bool bLibrary, ESG_Library_Type Type) const { return( Get_Library(CSG_String(Name), bLibrary, Type) ); }
691CSG_Tool_Library * CSG_Tool_Library_Manager::Get_Library(const wchar_t *Name, bool bLibrary, ESG_Library_Type Type) const { return( Get_Library(CSG_String(Name), bLibrary, Type) ); }
693{
694 if( bLibrary )
695 {
696 SG_Get_Tool_Library_Manager()._Add_Library(Name);
697 }
698
699 for(int i=0; i<Get_Count(); i++)
700 {
701 CSG_Tool_Library *pLibrary = Get_Library(i);
702
703 if( Type == ESG_Library_Type::Undefined || Type == pLibrary->Get_Type() )
704 {
705 if( !Name.Cmp(bLibrary ? pLibrary->Get_Library_Name() : pLibrary->Get_Name()) )
706 {
707 return( pLibrary );
708 }
709 }
710 }
711
712 return( NULL );
713}
714
715//---------------------------------------------------------
716bool CSG_Tool_Library_Manager::_Add_Library(const CSG_String &Library)
717{
718 int bOkay = false;
719
721
722 //-----------------------------------------------------
723 #if defined(_SAGA_MSW)
724 if( Add_Library ( CSG_String::Format("%s\\tools\\%s.dll" , SG_UI_Get_API_Path().c_str() , Library.c_str())) ) { bOkay = true; }
725 if( _Add_Library_Chains(Library, CSG_String::Format("%s\\tools\\toolchains" , SG_UI_Get_API_Path().c_str() )) ) { bOkay = true; }
726 #elif defined(__WXMAC__)
727 if( Add_Library ( CSG_String::Format("%s/../Tools/lib%s.dylib", SG_UI_Get_Application_Path(true).c_str(), Library.c_str())) ) { bOkay = true; }
728 if( _Add_Library_Chains(Library, CSG_String::Format("%s/../Tools" , SG_UI_Get_Application_Path(true).c_str() )) ) { bOkay = true; }
729 #ifdef TOOLS_PATH
730 if( Add_Library ( CSG_String::Format("%s/lib%s.dylib" , CSG_String(TOOLS_PATH).c_str() , Library.c_str())) ) { bOkay = true; }
731 #endif
732 #ifdef SHARE_PATH
733 if( _Add_Library_Chains(Library, CSG_String::Format("%s/toolchains" , CSG_String(SHARE_PATH).c_str() )) ) { bOkay = true; }
734 #endif
735 #else // #if defined(_SAGA_LINUX)
736 #ifdef TOOLS_PATH
737 if( Add_Library ( CSG_String::Format("%s/lib%s.so" , CSG_String(TOOLS_PATH).c_str() , Library.c_str())) ) { bOkay = true; }
738 #endif
739 #ifdef SHARE_PATH
740 if( _Add_Library_Chains(Library, CSG_String::Format("%s/toolchains" , CSG_String(SHARE_PATH).c_str() )) ) { bOkay = true; }
741 #endif
742 #endif
743
744 //-----------------------------------------------------
745 CSG_String Paths;
746
747 if( SG_Get_Environment("SAGA_TLB", &Paths) )
748 {
749 #if defined(_SAGA_MSW)
750 const char *Format("%s\\%s.dll" ); CSG_Strings Path = SG_String_Tokenize(Paths, ";" ); // colon (':') would split drive from paths!
751 #elif defined(__WXMAC__)
752 const char *Format("%s/lib%s.dylib"); CSG_Strings Path = SG_String_Tokenize(Paths, ";:"); // colon (':') is more native to non-windows os than semi-colon (';'), we support both...
753 #else // #if defined(_SAGA_LINUX)
754 const char *Format("%s/lib%s.so" ); CSG_Strings Path = SG_String_Tokenize(Paths, ";:"); // colon (':') is more native to non-windows os than semi-colon (';'), we support both...
755 #endif
756
757 for(int i=0; i<Path.Get_Count(); i++)
758 {
759 if( Add_Library (CSG_String::Format(Format, Path[i].c_str(), Library.c_str())) ) { bOkay = true; }
760 if( _Add_Library_Chains(Library , Path[i]) ) { bOkay = true; }
761 }
762 }
763
764 //-----------------------------------------------------
766
767 return( bOkay );
768}
769
770//---------------------------------------------------------
771bool CSG_Tool_Library_Manager::_Add_Library_Chains(const CSG_String &Library, const CSG_String &Directory)
772{
773 wxDir dir; wxString file; bool bOkay = false;
774
775 if( dir.Open(Directory.c_str()) && dir.GetFirst(&file, wxEmptyString, wxDIR_FILES) )
776 {
777 do
778 {
779 CSG_String path(SG_File_Make_Path(Directory, &file)); CSG_Tool_Chain Tool(path);
780
781 if( Tool.is_Okay() && Tool.Get_Library().Cmp(Library) == 0 && _Add_Tool_Chain(path, false) )
782 {
783 bOkay = true;
784 }
785 }
786 while( dir.GetNext(&file) );
787 }
788
789 return( bOkay );
790}
791
792
794// //
796
797//---------------------------------------------------------
798CSG_Tool * CSG_Tool_Library_Manager::Get_Tool(const char *Library, int ID) const { return( Get_Tool(CSG_String(Library), ID) ); }
799CSG_Tool * CSG_Tool_Library_Manager::Get_Tool(const wchar_t *Library, int ID) const { return( Get_Tool(CSG_String(Library), ID) ); }
801{
802 return( Get_Tool(Library, CSG_String::Format("%d", ID)) );
803}
804
805//---------------------------------------------------------
806CSG_Tool * CSG_Tool_Library_Manager::Get_Tool(const char *Library, const char *Name) const { return( Get_Tool(CSG_String(Library), CSG_String(Name)) ); }
807CSG_Tool * CSG_Tool_Library_Manager::Get_Tool(const wchar_t *Library, const wchar_t *Name) const { return( Get_Tool(CSG_String(Library), CSG_String(Name)) ); }
809{
810 SG_Get_Tool_Library_Manager()._Add_Library(Library);
811
812 for(int i=0; i<Get_Count(); i++)
813 {
814 if( Get_Library(i)->Get_Library_Name().Cmp(Library) == 0 )
815 {
816 CSG_Tool *pTool = Get_Library(i)->Get_Tool(Name);
817
818 if( pTool )
819 {
820 return( pTool );
821 }
822 }
823 }
824
825 return( NULL );
826}
827
828
830// //
832
833//---------------------------------------------------------
834CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const char *Library, int Index, bool bWithGUI, bool bWithCMD) const { return( Create_Tool(CSG_String(Library), Index, bWithGUI, bWithCMD) ); }
835CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const wchar_t *Library, int Index, bool bWithGUI, bool bWithCMD) const { return( Create_Tool(CSG_String(Library), Index, bWithGUI, bWithCMD) ); }
836CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const CSG_String &Library, int Index, bool bWithGUI, bool bWithCMD) const
837{
838 return( Create_Tool(Library, CSG_String::Format("%d", Index), bWithGUI, bWithCMD) );
839}
840
841//---------------------------------------------------------
842CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const char *Library, const char *Name, bool bWithGUI, bool bWithCMD) const { return( Create_Tool(CSG_String(Library), CSG_String(Name), bWithGUI, bWithCMD) ); }
843CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const wchar_t *Library, const wchar_t *Name, bool bWithGUI, bool bWithCMD) const { return( Create_Tool(CSG_String(Library), CSG_String(Name), bWithGUI, bWithCMD) ); }
844CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const CSG_String &Library, const CSG_String &Name, bool bWithGUI, bool bWithCMD) const
845{
846 SG_Get_Tool_Library_Manager()._Add_Library(Library);
847
848 for(int i=0; i<Get_Count(); i++)
849 {
850 if( Get_Library(i)->Get_Library_Name().Cmp(Library) == 0 )
851 {
852 CSG_Tool *pTool = Get_Library(i)->Create_Tool(Name, bWithGUI, bWithCMD);
853
854 if( pTool )
855 {
856 return( pTool );
857 }
858 }
859 }
860
861 return( NULL );
862}
863
864//---------------------------------------------------------
866{
867 for(int i=0; i<Get_Count(); i++)
868 {
869 if( Get_Library(i)->Delete_Tool(pTool) )
870 {
871 return( true );
872 }
873 }
874
875 return( false );
876}
877
878
880// //
882
883//---------------------------------------------------------
884bool CSG_Tool_Library_Manager::Create_Python_ToolBox(const CSG_String &Destination, bool bClean, bool bName, bool bSingleFile) const
885{
886 const char *Header =
887 "#! /usr/bin/env python\n\n"
888 "'''\n"
889 "Python Interface to SAGA Tools Library\n"
890 "----------\n"
891 "- Category : %s\n"
892 "- Name : %s\n"
893 "- ID : %s\n\n"
894 "Description\n"
895 "----------\n%s\n"
896 "'''\n\n"
897 "from PySAGA.helper import Tool_Wrapper\n\n";
898
899 CSG_File Stream;
900
901 if( bSingleFile )
902 {
903 if( SG_File_Exists(Destination) )
904 {
905 SG_File_Delete(Destination);
906 }
907
908 if( !Stream.Open(Destination, SG_FILE_W, false, SG_FILE_ENCODING_UTF8) )
909 {
910 SG_UI_Msg_Add_Error(CSG_String::Format("%s: %s", _TL("failed to create destination file"), Destination.c_str()));
911
912 return( false );
913 }
914
915 Stream.Write("#! /usr/bin/env python\n\n'''\n");
916 Stream.Write("Python Interface to SAGA Tools\n");
917 Stream.Write("----------\n'''\n\n");
918 Stream.Write("from PySAGA.helper import Tool_Wrapper\n\n");
919 }
920 else
921 {
922 if( !SG_Dir_Exists(Destination) && !SG_Dir_Create(Destination, true) )
923 {
924 SG_UI_Msg_Add_Error(CSG_String::Format("%s: %s", _TL("failed to create destination folder"), Destination.c_str()));
925
926 return( false );
927 }
928
929 if( bClean )
930 {
931 CSG_Strings Files;
932
933 SG_Dir_List_Files(Files, Destination);
934
935 for(int i=0; i<Files.Get_Count(); i++)
936 {
937 SG_File_Delete(Files[i]);
938 }
939 }
940 }
941
943
944 //-----------------------------------------------------
945 // skip tool chains in 1st run...
946 for(int iLibrary=0; iLibrary<Get_Count() && SG_UI_Process_Set_Progress(iLibrary, Get_Count()); iLibrary++)
947 {
948 CSG_Tool_Library *pLibrary = Get_Library(iLibrary);
949
950 if( !pLibrary->Get_Category ().Cmp("SAGA Development" ) // generally exclude certain categories/libraries
951 || !pLibrary->Get_Category ().Cmp("Garden" )
952 || !pLibrary->Get_Library_Name().Cmp("grid_calculus_bsl")
953 || !pLibrary->Get_Library_Name().Cmp("pj_geotrans" )
954 || !pLibrary->Get_Library_Name().Cmp("vis_3d_viewer" )
955 || pLibrary->Get_Type() == ESG_Library_Type::Chain ) // exclude tool chains in 1st run
956 {
957 continue;
958 }
959
960 SG_UI_Process_Set_Text(CSG_String::Format("%s: %s", SG_T("Library"), pLibrary->Get_Library_Name().c_str()));
961
962 if( !bSingleFile )
963 {
964 if( !Stream.Open(SG_File_Make_Path(Destination, pLibrary->Get_Library_Name(), "py"), SG_FILE_W, false, SG_FILE_ENCODING_UTF8) )
965 {
966 continue;
967 }
968
969 Stream.Printf(Header, pLibrary->Get_Category().c_str(), pLibrary->Get_Name().c_str(), pLibrary->Get_Library_Name().c_str(), SG_HTML_Tag_Replacer(pLibrary->Get_Description()).c_str());
970 }
971
972 for(int iTool=0; iTool<pLibrary->Get_Count(); iTool++)
973 {
974 CSG_Tool *pTool = pLibrary->Create_Tool(pLibrary->Get_Tool(iTool)->Get_ID(), false, false);
975
976 if( pTool && !pTool->needs_GUI() && !pTool->is_Interactive() && pTool->Get_Parameters_Count() == 0 )
977 {
978 if( bName )
979 {
981 }
982
984 }
985
986 pLibrary->Delete_Tool(pTool);
987 }
988 }
989
990 //-----------------------------------------------------
991 // add tool chains in 2nd run...
992 for(int iLibrary=0; iLibrary<Get_Count() && SG_UI_Process_Set_Progress(iLibrary, Get_Count()); iLibrary++)
993 {
994 CSG_Tool_Library *pLibrary = Get_Library(iLibrary);
995
996 if( !pLibrary->Get_Category().Cmp("SAGA Development" ) // generally exclude certain categories
997 || !pLibrary->Get_Category().Cmp("Garden" )
998 || pLibrary->Get_Type() != ESG_Library_Type::Chain ) // only process tool chains in 2nd run
999 {
1000 continue;
1001 }
1002
1003 SG_UI_Process_Set_Text(CSG_String::Format("%s: %s", SG_T("Library"), pLibrary->Get_Library_Name().c_str()));
1004
1005 if( !bSingleFile )
1006 {
1007 CSG_String File(SG_File_Make_Path(Destination, pLibrary->Get_Library_Name(), "py"));
1008
1009 if( SG_File_Exists(File) )
1010 {
1011 if( !Stream.Open(File, SG_FILE_RW, false, SG_FILE_ENCODING_UTF8) )
1012 {
1013 continue;
1014 }
1015
1016 Stream.Seek_End();
1017 }
1018 else
1019 {
1020 if( !Stream.Open(File, SG_FILE_W, false, SG_FILE_ENCODING_UTF8) )
1021 {
1022 continue;
1023 }
1024
1025 Stream.Printf(Header, pLibrary->Get_Category().c_str(), pLibrary->Get_Name().c_str(), pLibrary->Get_Library_Name().c_str(), SG_HTML_Tag_Replacer(pLibrary->Get_Description()).c_str());
1026 }
1027 }
1028
1029 for(int iTool=0; iTool<pLibrary->Get_Count(); iTool++)
1030 {
1031 CSG_Tool *pTool = pLibrary->Create_Tool(pLibrary->Get_Tool(iTool)->Get_ID(), false, false);
1032
1033 if( pTool && !pTool->needs_GUI() && !pTool->is_Interactive() && pTool->Get_Parameters_Count() == 0 )
1034 {
1035 if( bName )
1036 {
1038 }
1039
1041 }
1042
1043 pLibrary->Delete_Tool(pTool);
1044 }
1045 }
1046
1047 //-----------------------------------------------------
1048 SG_UI_Set_Application_Name(); // reset
1049
1050 return( true );
1051}
1052
1053
1055// //
1056// //
1057// //
1059
1060//---------------------------------------------------------
void SG_UI_Msg_Add_Error(const char *Message)
void SG_UI_Msg_Add(const char *Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
void SG_UI_Process_Set_Text(const CSG_String &Text)
bool SG_UI_Process_Set_Progress(int Position, int Range)
void * SG_UI_Get_Window_Main(void)
int SG_UI_Msg_Lock(bool bOn)
void SG_UI_ProgressAndMsg_Lock(bool bOn)
CSG_String SG_UI_Set_Application_Name(const CSG_String &Name)
Definition api_core.cpp:388
CSG_String SG_UI_Get_Application_Path(bool bPathOnly)
Definition api_core.cpp:356
CSG_String SG_UI_Get_API_Path(void)
Definition api_core.cpp:345
SAGA_API_DLL_EXPORT bool SG_Get_Environment(const CSG_String &Variable, CSG_String *Value=NULL)
SAGA_API_DLL_EXPORT bool SG_File_Exists(const CSG_String &FileName)
@ SG_UI_MSG_STYLE_FAILURE
Definition api_core.h:1585
@ SG_UI_MSG_STYLE_SUCCESS
Definition api_core.h:1584
SAGA_API_DLL_EXPORT bool SG_File_Cmp_Extension(const CSG_String &File, const CSG_String &Extension)
SAGA_API_DLL_EXPORT CSG_String SG_HTML_Tag_Replacer(const CSG_String &Text)
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Path_Absolute(const CSG_String &full_Path)
SAGA_API_DLL_EXPORT bool SG_Dir_Create(const CSG_String &Directory, bool bFullPath=false)
Definition api_file.cpp:972
SAGA_API_DLL_EXPORT bool SG_Dir_List_Files(CSG_Strings &List, const CSG_String &Directory, const CSG_String &Extension="", bool bRecursive=false)
#define SG_T(s)
Definition api_core.h:537
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Path(const CSG_String &full_Path)
SAGA_API_DLL_EXPORT CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name)
SAGA_API_DLL_EXPORT bool SG_File_Delete(const CSG_String &FileName)
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
SAGA_API_DLL_EXPORT CSG_Strings SG_String_Tokenize(const CSG_String &String, const CSG_String &Delimiters=SG_DEFAULT_DELIMITERS, TSG_String_Tokenizer_Mode Mode=SG_TOKEN_DEFAULT)
SAGA_API_DLL_EXPORT bool SG_Dir_Exists(const CSG_String &Directory)
Definition api_file.cpp:966
#define SG_Char
Definition api_core.h:536
#define _TL(s)
Definition api_core.h:1568
@ SG_FILE_ENCODING_UTF8
Definition api_core.h:552
@ SG_FILE_W
Definition api_core.h:1115
@ SG_FILE_RW
Definition api_core.h:1116
virtual bool Open(const SG_Char *FileName, int Mode=SG_FILE_R, bool bBinary=true, int Encoding=SG_FILE_ENCODING_ANSI)
Definition api_file.cpp:113
bool Seek_End(void) const
Definition api_file.cpp:262
size_t Write(void *Buffer, size_t Size, size_t Count=1) const
Definition api_file.cpp:370
int Printf(const char *Format,...)
Definition api_file.cpp:287
static void Initialize(void)
int Cmp(const CSG_String &String) const
static CSG_String Format(const char *Format,...)
int asInt(void) const
const SG_Char * c_str(void) const
int Printf(const char *Format,...)
bool is_Empty(void) const
int Get_Count(void) const
Definition api_core.h:714
bool is_Okay(void) const
Definition tool_chain.h:96
bool Create(const CSG_Tool_Chain &Tool, bool bWithGUI, bool bWithCMD)
bool Add_Tool(CSG_Tool_Chain *pTool)
bool Create_Python_ToolBox(const CSG_String &Destination, bool bClean=true, bool bName=true, bool bSingleFile=false) const
int Get_Count(void) const
int Get_Tool_Count(void) const
virtual ~CSG_Tool_Library_Manager(void)
CSG_Tool_Library * Add_Library(const CSG_String &File)
int Add_Directory(const CSG_String &Directory, bool bOnlySubDirectories=false)
CSG_Tool_Library * Get_Library(int i) const
bool Delete_Tool(CSG_Tool *pTool) const
CSG_Tool * Get_Tool(const CSG_String &Library, int Index) const
CSG_Tool * Create_Tool(const CSG_String &Library, int Index, bool bWithGUI=false, bool bWithCMD=true) const
bool is_Loaded(CSG_Tool_Library *pLibrary) const
bool Add_Default_Libraries(bool bVerbose=false)
CSG_String m_File_Name
void Del_References(void)
virtual CSG_Tool * Create_Tool(int Index, bool bWithGUI=false, bool bWithCMD=true)
virtual CSG_Tool * Get_Tool(int Index, TSG_Tool_Type Type=TOOL_TYPE_Base) const
CSG_String Get_Menu(void) const
virtual bool Delete_Tools(void)
const CSG_String & Get_Library_Name(void) const
virtual bool Delete_Tool(CSG_Tool *pTool)
virtual ~CSG_Tool_Library(void)
CSG_String Get_Description(void) const
virtual CSG_String Get_Info(int Type) const
bool is_Valid(void) const
virtual ESG_Library_Type Get_Type(void) const
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)
virtual int Get_Count(void) const
CSG_String Get_Category(void) const
CSG_String m_Library_Name
CSG_String Get_Name(void) const
const CSG_String & Get_Library(void) const
Definition tool.cpp:115
int Get_Parameters_Count(void) const
Definition tool.h:167
virtual bool is_Interactive(void) const
Definition tool.h:223
CSG_String Get_Script(Script_Format Format, bool bHeader=true, int Arguments=0, bool bWrapArgs=true)
Definition tool.cpp:1466
const CSG_String & Get_Name(void) const
Definition tool.cpp:132
virtual bool needs_GUI(void) const
Definition tool.h:218
virtual TSG_Tool_Type Get_Type(void) const
Definition tool.h:148
const CSG_String & Get_ID(void) const
Definition tool.h:150
#define SYMBOL_TLB_Get_Interface
Definition tool.h:692
CSG_Tool_Library_Interface *(* TSG_PFNC_TLB_Get_Interface)(void)
Definition tool.h:693
@ TLB_INFO_Library
Definition tool.h:645
@ TLB_INFO_File
Definition tool.h:644
bool(* TSG_PFNC_TLB_Finalize)(void)
Definition tool.h:690
TSG_Tool_Type
Definition tool.h:101
@ TOOL_TYPE_Base
Definition tool.h:102
bool(* TSG_PFNC_TLB_Initialize)(const SG_Char *)
Definition tool.h:687
#define SYMBOL_TLB_Initialize
Definition tool.h:686
#define SYMBOL_TLB_Finalize
Definition tool.h:689
CSG_Tool_Library_Manager & SG_Get_Tool_Library_Manager(void)
CSG_Tool_Library_Manager g_Tool_Library_Manager
SAGA_API_DLL_EXPORT CSG_Tool_Library_Manager & SG_Get_Tool_Library_Manager(void)
ESG_Library_Type