SAGA API Version 9.14
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 m_Description = m_pInterface->Get_Info(TLB_INFO_Description);
92
93 m_References.Set_Name(_TL("References"));
94 m_References.Add_Field("Authors" , SG_DATATYPE_String);
95 m_References.Add_Field("Date" , SG_DATATYPE_String);
96 m_References.Add_Field("Title" , SG_DATATYPE_String);
97 m_References.Add_Field("Source" , SG_DATATYPE_String);
98 m_References.Add_Field("Link" , SG_DATATYPE_String);
99 m_References.Add_Field("LinkText" , SG_DATATYPE_String);
100
102
103 return; // success
104 }
105 }
106
107 _Destroy();
108}
109
110//---------------------------------------------------------
112{
113 _Destroy();
114}
115
116//---------------------------------------------------------
117bool CSG_Tool_Library::_Destroy(void)
118{
119 if( m_pLibrary )
120 {
121 if( m_pLibrary->IsLoaded() && m_pLibrary->HasSymbol(SYMBOL_TLB_Finalize) )
122 {
123 TSG_PFNC_TLB_Finalize TLB_Finalize = (TSG_PFNC_TLB_Finalize)m_pLibrary->GetSymbol(SYMBOL_TLB_Finalize);
124
125 TLB_Finalize();
126 }
127
128 Del_References(); m_Description.Clear();
129
130 delete(m_pLibrary);
131
132 m_pLibrary = NULL;
133 }
134
135 m_pInterface = NULL;
136
137 return( true );
138}
139
140
142// //
144
145//---------------------------------------------------------
147{
148 if( m_pInterface != NULL )
149 {
150 return( m_pInterface->Get_Info(Type) );
151 }
152
153 static CSG_String Dummy; return( Dummy );
154}
155
156
158// //
160
161//---------------------------------------------------------
163{
164 CSG_Tool *pTool = m_pInterface && Index >= 0 && Index < Get_Count() ? m_pInterface->Get_Tool(Index) : NULL;
165
166 return( pTool && (Type == TOOL_TYPE_Base || Type == pTool->Get_Type()) ? pTool : NULL );
167}
168
169//---------------------------------------------------------
170CSG_Tool * CSG_Tool_Library::Get_Tool(const char *Name, TSG_Tool_Type Type) const { return( Get_Tool(CSG_String(Name), Type) ); }
171CSG_Tool * CSG_Tool_Library::Get_Tool(const wchar_t *Name, TSG_Tool_Type Type) const { return( Get_Tool(CSG_String(Name), Type) ); }
173{
174 for(int i=0; i<Get_Count(); i++)
175 {
176 CSG_Tool *pTool = Get_Tool(i, Type);
177
178 if( pTool && (!pTool->Get_ID().Cmp(Name) || !pTool->Get_Name().Cmp(Name)) )
179 {
180 return( pTool );
181 }
182 }
183
184 return( NULL );
185}
186
187
189// //
191
192//---------------------------------------------------------
202//---------------------------------------------------------
203CSG_Tool * CSG_Tool_Library::Create_Tool(int Index, bool bWithGUI, bool bWithCMD)
204{
205 return( m_pInterface ? m_pInterface->Create_Tool(Index, bWithGUI, bWithCMD) : NULL );
206}
207
208//---------------------------------------------------------
209CSG_Tool * CSG_Tool_Library::Create_Tool(const char *Name, bool bWithGUI, bool bWithCMD) { return( Create_Tool(CSG_String(Name), bWithGUI, bWithCMD) ); }
210CSG_Tool * CSG_Tool_Library::Create_Tool(const wchar_t *Name, bool bWithGUI, bool bWithCMD) { return( Create_Tool(CSG_String(Name), bWithGUI, bWithCMD) ); }
211CSG_Tool * CSG_Tool_Library::Create_Tool(const CSG_String &Name, bool bWithGUI, bool bWithCMD)
212{
213 int Index; return( Name.asInt(Index) ? Create_Tool(Index, bWithGUI, bWithCMD) : NULL );
214}
215
216//---------------------------------------------------------
221//---------------------------------------------------------
223{
224 return( m_pInterface ? m_pInterface->Delete_Tool(pTool) : false );
225}
226
227//---------------------------------------------------------
229{
230 return( m_pInterface ? m_pInterface->Delete_Tools() : false );
231}
232
233
235// //
237
238//---------------------------------------------------------
240{
241 if( Get_Tool(i) )
242 {
243 return( Get_Tool(i)->Get_MenuPath(true) );
244 }
245
246 return( "" );
247}
248
249
251// //
253
254//---------------------------------------------------------
256{
258
259 if( SG_Dir_Exists(Folder) )
260 {
261 CSG_String Description; CSG_File Stream; CSG_Table Table;
262
263 if( Stream.Open(SG_File_Make_Path(Folder, m_Library_Name, "txt"), SG_FILE_R, true, SG_FILE_ENCODING_UTF8) && Stream.Read(Description) > 0 )
264 {
265 m_Description = CSG_Text_Format_Converter::Markdown_to_Html(Description, true);
266 m_Description = CSG_Text_Format_Converter::Html_Absolute_Image_Paths(m_Description, Folder, true);
267 }
268
270
272
273 if( Table.Create(SG_File_Make_Path(Folder, m_Library_Name + "_references", "txt")) && Table.Get_Field_Count() == 6 )
274 {
275 for(int i=0; i<Table.Get_Field_Count(); i++) { Table.Set_Field_Type(i, SG_DATATYPE_String); }
276
277 for(sLong i=0; i<Table.Get_Count(); i++)
278 {
279 Add_Reference(Table[i].asString(0), Table[i].asString(1), Table[i].asString(2), Table[i].asString(3), Table[i].asString(4), Table[i].asString(5));
280 }
281 }
282
284
285 return( true );
286 }
287
288 return( false );
289}
290
291//---------------------------------------------------------
292void CSG_Tool_Library::Add_Reference(const CSG_String &Authors, const CSG_String &Date, const CSG_String &Title, const CSG_String &Source, const CSG_String &Link, const CSG_String &LinkText)
293{
294 CSG_Table_Record &Reference = *m_References.Add_Record();
295
296 Reference.Set_Value(0, Authors );
297 Reference.Set_Value(1, Date );
298 Reference.Set_Value(2, Title );
299 Reference.Set_Value(3, Source );
300 Reference.Set_Value(4, Link );
301 Reference.Set_Value(5, LinkText);
302
303 m_References.Sort(0);
304}
305
306//---------------------------------------------------------
307void CSG_Tool_Library::Add_Reference(const CSG_String &Link, const CSG_String &Link_Text)
308{
309 Add_Reference("", "", "", "", Link, Link_Text);
310}
311
312//---------------------------------------------------------
314{
315 return( m_References.Del_Records() );
316}
317
318//---------------------------------------------------------
320{
321 CSG_Strings References;
322
323 for(int i=0; i<m_References.Get_Count(); i++)
324 {
325 CSG_String Reference,
326 Authors(m_References[i].asString(0)),
327 Date (m_References[i].asString(1)),
328 Title (m_References[i].asString(2)),
329 Source (m_References[i].asString(3)),
330 Link (m_References[i].asString(4)),
331 LinkTxt(m_References[i].asString(5));
332
333 if( !Authors.is_Empty() )
334 {
335 if( Date.is_Empty() ) { Date = "n.d."; }
336
337 Reference += bHTML ? "<b>" + Authors + " (" + Date + "):</b>" : Authors + " (" + Date + "):";
338 }
339 else if( !Date.is_Empty() ) // date only
340 {
341 Reference += bHTML ? "<b>(" + Date + "):</b>" : "(" + Date + "):";
342 }
343
344 if( !Title.is_Empty() ) // title
345 {
346 if( Title[Title.Length() - 1] != '.' ) { Title += '.'; }
347
348 if( !Reference.is_Empty() ) { Reference += " "; }
349
350 Reference += bHTML ? "<i>" + Title + "</i>" : Title;
351 }
352
353 if( !Source.is_Empty() ) // title
354 {
355 if( Source[Source.Length() - 1] != '.' ) { Source += '.'; }
356
357 if( !Reference.is_Empty() ) { Reference += " "; }
358
359 Reference += Source;
360 }
361
362 if( !Link.is_Empty() )
363 {
364 if( !Reference.is_Empty() ) { Reference += " "; }
365
366 Reference += bHTML ? "<a href=\"" + Link + "\">" + (LinkTxt.is_Empty() ? Link : LinkTxt) + "</a>" : "[" + LinkTxt + "](" + Link + ")";
367 }
368
369 if( !Reference.is_Empty() )
370 {
371 References += Reference;
372 }
373 }
374
375 return( References );
376}
377
378
380// //
381// //
382// //
384
385//---------------------------------------------------------
387
388//---------------------------------------------------------
393
394
396// //
398
399//---------------------------------------------------------
401{
402 m_pLibraries = NULL;
403 m_nLibraries = 0;
404
405 if( this == &g_Tool_Library_Manager )
406 {
407 CSG_Random::Initialize(); // initialize with current time on startup
408 }
409}
410
411//---------------------------------------------------------
416
417
419// //
421
422//---------------------------------------------------------
424{
425 int nTools = 0;
426
427 for(int i=0; i<m_nLibraries; i++)
428 {
429 nTools += m_pLibraries[i]->Get_Count();
430 }
431
432 return( nTools );
433}
434
435
437// //
439
440//---------------------------------------------------------
442{
443 if( bVerbose == false )
444 {
445 SG_UI_Msg_Lock(true);
446 }
447
448 //-----------------------------------------------------
450
451 for(int i=0; i<Paths.Get_Count(); i++)
452 {
454 }
455
456 //-----------------------------------------------------
457 if( bVerbose == false )
458 {
459 SG_UI_Msg_Lock(false);
460 }
461
462 return( Get_Count() > 0 );
463}
464
465
467// //
469
470//---------------------------------------------------------
474{
475 if( SG_File_Exists(File) == false )
476 {
477 return( NULL );
478 }
479
480 if( SG_File_Cmp_Extension(File, "xml") )
481 {
482 return( _Add_Tool_Chain(File) );
483 }
484
485 if( SG_File_Cmp_Extension(File, "dll" ) == false
486 && SG_File_Cmp_Extension(File, "dylib") == false
487 && SG_File_Cmp_Extension(File, "so" ) == false )
488 {
489 return( NULL );
490 }
491
492 //-----------------------------------------------------
493 wxFileName FileName(File.c_str());
494
495 SG_UI_Msg_Add(CSG_String::Format("%s: %s...", _TL("Loading library"), File.c_str()), true);
496
497 for(int i=0; i<Get_Count(); i++)
498 {
499 if( FileName == Get_Library(i)->Get_File_Name().c_str() )
500 {
501 SG_UI_Msg_Add(_TL("has already been loaded"), false);
502
503 return( Get_Library(i) );
504 }
505 }
506
507 //-----------------------------------------------------
508 CSG_Tool_Library *pLibrary = new CSG_Tool_Library(File);
509
510 if( pLibrary->is_Valid() )
511 {
512 m_pLibraries = (CSG_Tool_Library **)SG_Realloc(m_pLibraries, (m_nLibraries + 1) * sizeof(CSG_Tool_Library *));
513 m_pLibraries[m_nLibraries++] = pLibrary;
514
516
517 return( pLibrary );
518 }
519
520 delete(pLibrary);
521
522 SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE);
523
524 return( NULL );
525}
526
527//---------------------------------------------------------
528int CSG_Tool_Library_Manager::Add_Directory(const char *Directory, bool bOnlySubDirectories) { return( Add_Directory(CSG_String(Directory), bOnlySubDirectories) ); }
529int CSG_Tool_Library_Manager::Add_Directory(const wchar_t *Directory, bool bOnlySubDirectories) { return( Add_Directory(CSG_String(Directory), bOnlySubDirectories) ); }
530int CSG_Tool_Library_Manager::Add_Directory(const CSG_String &Directory, bool bOnlySubDirectories)
531{
532 int nOpened = 0; wxDir Dir;
533
534 if( SG_Dir_Exists(Directory) && Dir.Open(Directory.wx_str()) )
535 {
536 wxString FileName, DirName(Dir.GetName());
537
538 if( !bOnlySubDirectories && Dir.GetFirst(&FileName, wxEmptyString, wxDIR_FILES) )
539 {
540 do
541 { if( FileName.Find("saga_") < 0 && FileName.Find("wx") < 0 )
542 if( Add_Library(SG_File_Make_Path(&DirName, &FileName)) )
543 {
544 nOpened++;
545 }
546 }
547 while( Dir.GetNext(&FileName) );
548 }
549
550 if( Dir.GetFirst(&FileName, wxEmptyString, wxDIR_DIRS) )
551 {
552 do
553 {
554 if( FileName.CmpNoCase("dll") )
555 {
556 nOpened += Add_Directory(SG_File_Make_Path(&DirName, &FileName), false);
557 }
558 }
559 while( Dir.GetNext(&FileName) );
560 }
561 }
562
563 return( nOpened );
564}
565
566
568// //
570
571//---------------------------------------------------------
572CSG_Tool_Library * CSG_Tool_Library_Manager::_Add_Tool_Chain(const CSG_String &File, bool bReload)
573{
574 if( !SG_File_Cmp_Extension(File, "xml") )
575 {
576 return( NULL );
577 }
578
579 //-----------------------------------------------------
580 CSG_Tool_Chains *pLibrary = NULL;
581 CSG_Tool_Chain *pTool = NULL;
582
583 //-----------------------------------------------------
584 wxFileName FileName(File.c_str()); // check if tool chain is already loaded ?
585
586 for(int iLibrary=0; !pTool && iLibrary<Get_Count(); iLibrary++)
587 {
588 if( Get_Library(iLibrary)->Get_Type() == ESG_Library_Type::Chain )
589 {
590 for(int iTool=0; !pTool && iTool<Get_Library(iLibrary)->Get_Count(); iTool++)
591 {
592 if( FileName == ((CSG_Tool_Chain *)Get_Library(iLibrary)->Get_Tool(iTool))->Get_File_Name().c_str() )
593 {
594 pLibrary = (CSG_Tool_Chains *)Get_Library(iLibrary);
595 pTool = (CSG_Tool_Chain *)Get_Library(iLibrary)->Get_Tool(iTool);
596 }
597 }
598 }
599 }
600
601 if( pTool )
602 {
603 if( bReload ) // ...then try to reload !
604 {
606 CSG_Tool_Chain Tool(File); // don't reset loaded tool in case reloading fails!!!
608
609 if( Tool.is_Okay() )
610 {
611 pTool->Create(File);
612 }
613 }
614
615 return( pLibrary );
616 }
617
618 //-----------------------------------------------------
619 pTool = new CSG_Tool_Chain(File);
620
621 if( !pTool || !pTool->is_Okay() )
622 {
623 if( pTool )
624 {
625 delete(pTool);
626 }
627
628 return( NULL );
629 }
630
631 //-----------------------------------------------------
632 CSG_String Library = pTool->Get_Library();
633
634 for(int iLibrary=0; !pLibrary && iLibrary<Get_Count(); iLibrary++)
635 {
636 if( Get_Library(iLibrary)->Get_Type() == ESG_Library_Type::Chain
637 && Get_Library(iLibrary)->Get_Library_Name().Cmp(Library) == 0 )
638 {
639 pLibrary = (CSG_Tool_Chains *)Get_Library(iLibrary);
640 }
641 }
642
643 if( !pLibrary && (pLibrary = new CSG_Tool_Chains(pTool->Get_Library(), SG_File_Get_Path(File))) != NULL )
644 {
645 m_pLibraries = (CSG_Tool_Library **)SG_Realloc(m_pLibraries, (m_nLibraries + 1) * sizeof(CSG_Tool_Library *));
646 m_pLibraries[m_nLibraries++] = pLibrary;
647 }
648
649 if( !pLibrary ) // this should never happen, but who knows...
650 {
651 SG_UI_Msg_Add_Error(CSG_String::Format("%s %s: %s", _TL("ERROR"), _TL("tool chain library"), File.c_str()));
652
653 delete(pTool);
654
655 return( NULL );
656 }
657
658 pLibrary->Add_Tool(pTool);
659
660 //-----------------------------------------------------
661 return( pLibrary );
662}
663
664
666// //
668
669//---------------------------------------------------------
671{
672 if( m_pLibraries )
673 {
674 for(int i=0; i<Get_Count(); i++)
675 {
676// #ifndef _SAGA_MSW
677 if( !SG_UI_Get_Window_Main() && m_pLibraries[i]->m_pLibrary )
678 {
679 m_pLibraries[i]->m_pLibrary->Detach();
680 }
681// #endif
682
683 delete(m_pLibraries[i]);
684 }
685
686 SG_Free(m_pLibraries);
687
688 m_pLibraries = NULL;
689 m_nLibraries = 0;
690 }
691
692 return( true );
693}
694
695
697// //
699
700//---------------------------------------------------------
702{
703 for(int i=0; i<Get_Count(); i++)
704 {
705 if( pLibrary == Get_Library(i) )
706 {
707 return( Del_Library(i) );
708 }
709 }
710
711 return( false );
712}
713
714//---------------------------------------------------------
716{
717 if( i >= 0 && i < Get_Count() )
718 {
719 delete(m_pLibraries[i]);
720
721 for(m_nLibraries--; i<m_nLibraries; i++)
722 {
723 m_pLibraries[i] = m_pLibraries[i + 1];
724 }
725
726 m_pLibraries = (CSG_Tool_Library **)SG_Realloc(m_pLibraries, m_nLibraries * sizeof(CSG_Tool_Library *));
727
728 return( true );
729 }
730
731 return( false );
732}
733
734//---------------------------------------------------------
736{
737 for(int i=0; i<Get_Count(); i++)
738 {
739 if( pLibrary == Get_Library(i) )
740 {
741 return( true );
742 }
743 }
744
745 return( false );
746}
747
748
750// //
752
753//---------------------------------------------------------
758
759//---------------------------------------------------------
760CSG_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) ); }
761CSG_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) ); }
763{
764 if( bLibrary )
765 {
766 SG_Get_Tool_Library_Manager()._Add_Library(Name);
767 }
768
769 for(int i=0; i<Get_Count(); i++)
770 {
771 CSG_Tool_Library *pLibrary = Get_Library(i);
772
773 if( Type == ESG_Library_Type::Undefined || Type == pLibrary->Get_Type() )
774 {
775 if( !Name.Cmp(bLibrary ? pLibrary->Get_Library_Name() : pLibrary->Get_Name()) )
776 {
777 return( pLibrary );
778 }
779 }
780 }
781
782 return( NULL );
783}
784
785//---------------------------------------------------------
786bool CSG_Tool_Library_Manager::_Add_Library(const CSG_String &Library)
787{
788 int bOkay = false;
789
791
792 //-----------------------------------------------------
793 #if defined(_SAGA_MSW)
794 if( Add_Library ( CSG_String::Format("%s\\tools\\%s.dll" , CSG_Environment::Get_API_Path().c_str(), Library.c_str())) ) { bOkay = true; }
795 if( _Add_Library_Chains(Library, CSG_String::Format("%s\\tools\\toolchains" , CSG_Environment::Get_API_Path().c_str() )) ) { bOkay = true; }
796 #elif defined(__WXMAC__)
797 if( Add_Library ( CSG_String::Format("%s/../Tools/lib%s.dylib", CSG_Environment::Get_API_Path().c_str(), Library.c_str())) ) { bOkay = true; }
798 if( _Add_Library_Chains(Library, CSG_String::Format("%s/../Share/toolchains" , CSG_Environment::Get_API_Path().c_str() )) ) { bOkay = true; }
799 #ifdef TOOLS_PATH
800 if( Add_Library ( CSG_String::Format("%s/lib%s.dylib" , CSG_String(TOOLS_PATH).c_str() , Library.c_str())) ) { bOkay = true; }
801 #endif
802 #ifdef SHARE_PATH
803 if( _Add_Library_Chains(Library, CSG_String::Format("%s/toolchains" , CSG_String(SHARE_PATH).c_str() )) ) { bOkay = true; }
804 #endif
805 #else // #if defined(_SAGA_LINUX)
806 #ifdef TOOLS_PATH
807 if( Add_Library ( CSG_String::Format("%s/lib%s.so" , CSG_String(TOOLS_PATH).c_str() , Library.c_str())) ) { bOkay = true; }
808 #endif
809 #ifdef SHARE_PATH
810 if( _Add_Library_Chains(Library, CSG_String::Format("%s/toolchains" , CSG_String(SHARE_PATH).c_str() )) ) { bOkay = true; }
811 #endif
812 #endif
813
814 //-----------------------------------------------------
815 CSG_String Paths;
816
817 if( CSG_Environment::Get_Variable("SAGA_TLB", Paths) )
818 {
819 #if defined(_SAGA_MSW)
820 const char *Format("%s\\%s.dll" ); CSG_Strings Path = SG_String_Tokenize(Paths, ";" ); // colon (':') would split drive from paths!
821 #elif defined(__WXMAC__)
822 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...
823 #else // #if defined(_SAGA_LINUX)
824 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...
825 #endif
826
827 for(int i=0; i<Path.Get_Count(); i++)
828 {
829 if( Add_Library (CSG_String::Format(Format, Path[i].c_str(), Library.c_str())) ) { bOkay = true; }
830 if( _Add_Library_Chains(Library , Path[i]) ) { bOkay = true; }
831 }
832 }
833
834 //-----------------------------------------------------
836
837 return( bOkay );
838}
839
840//---------------------------------------------------------
841bool CSG_Tool_Library_Manager::_Add_Library_Chains(const CSG_String &Library, const CSG_String &Directory)
842{
843 wxDir dir; wxString file; bool bOkay = false;
844
845 if( dir.Open(Directory.c_str()) && dir.GetFirst(&file, wxEmptyString, wxDIR_FILES) )
846 {
847 do
848 {
849 CSG_String path(SG_File_Make_Path(Directory, &file)); CSG_Tool_Chain Tool(path);
850
851 if( Tool.is_Okay() && Tool.Get_Library().Cmp(Library) == 0 && _Add_Tool_Chain(path, false) )
852 {
853 bOkay = true;
854 }
855 }
856 while( dir.GetNext(&file) );
857 }
858
859 return( bOkay );
860}
861
862
864// //
866
867//---------------------------------------------------------
868CSG_Tool * CSG_Tool_Library_Manager::Get_Tool(const char *Library, int ID) const { return( Get_Tool(CSG_String(Library), ID) ); }
869CSG_Tool * CSG_Tool_Library_Manager::Get_Tool(const wchar_t *Library, int ID) const { return( Get_Tool(CSG_String(Library), ID) ); }
871{
872 return( Get_Tool(Library, CSG_String::Format("%d", ID)) );
873}
874
875//---------------------------------------------------------
876CSG_Tool * CSG_Tool_Library_Manager::Get_Tool(const char *Library, const char *Name) const { return( Get_Tool(CSG_String(Library), CSG_String(Name)) ); }
877CSG_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)) ); }
879{
880 SG_Get_Tool_Library_Manager()._Add_Library(Library);
881
882 for(int i=0; i<Get_Count(); i++)
883 {
884 if( Get_Library(i)->Get_Library_Name().Cmp(Library) == 0 )
885 {
886 CSG_Tool *pTool = Get_Library(i)->Get_Tool(Name);
887
888 if( pTool )
889 {
890 return( pTool );
891 }
892 }
893 }
894
895 return( NULL );
896}
897
898
900// //
902
903//---------------------------------------------------------
904CSG_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) ); }
905CSG_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) ); }
906CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const CSG_String &Library, int Index, bool bWithGUI, bool bWithCMD) const
907{
908 return( Create_Tool(Library, CSG_String::Format("%d", Index), bWithGUI, bWithCMD) );
909}
910
911//---------------------------------------------------------
912CSG_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) ); }
913CSG_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) ); }
914CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const CSG_String &Library, const CSG_String &Name, bool bWithGUI, bool bWithCMD) const
915{
916 SG_Get_Tool_Library_Manager()._Add_Library(Library);
917
918 for(int i=0; i<Get_Count(); i++)
919 {
920 if( Get_Library(i)->Get_Library_Name().Cmp(Library) == 0 )
921 {
922 CSG_Tool *pTool = Get_Library(i)->Create_Tool(Name, bWithGUI, bWithCMD);
923
924 if( pTool )
925 {
926 return( pTool );
927 }
928 }
929 }
930
931 return( NULL );
932}
933
934//---------------------------------------------------------
936{
937 for(int i=0; i<Get_Count(); i++)
938 {
939 if( Get_Library(i)->Delete_Tool(pTool) )
940 {
941 return( true );
942 }
943 }
944
945 return( false );
946}
947
948
950// //
952
953//---------------------------------------------------------
954bool CSG_Tool_Library_Manager::Create_Python_ToolBox(const CSG_String &Destination, bool bClean, bool bName, bool bSingleFile, CSG_Table *pDeprecated_Names) const
955{
956 const char *Header =
957 "#! /usr/bin/env python\n\n"
958 "'''\n"
959 "Python Interface to SAGA Tools Library\n"
960 "----------\n"
961 "- Category : %s\n"
962 "- Name : %s\n"
963 "- ID : %s\n\n"
964 "Description\n"
965 "----------\n%s\n"
966 "'''\n\n"
967 "from PySAGA.helper import Tool_Wrapper\n\n";
968
969 //-----------------------------------------------------
970 if( pDeprecated_Names && (pDeprecated_Names->Get_Field_Count() != 3 || pDeprecated_Names->Get_Count() < 1) ) // LibraryID, ToolID, Name
971 {
972 pDeprecated_Names = NULL;
973 }
974
975 //-----------------------------------------------------
976 CSG_File Stream, Init;
977
978 if( bSingleFile )
979 {
980 if( SG_File_Exists(Destination) )
981 {
982 SG_File_Delete(Destination);
983 }
984
985 if( !Stream.Open(Destination, SG_FILE_W, false, SG_FILE_ENCODING_UTF8) )
986 {
987 SG_UI_Msg_Add_Error(CSG_String::Format("%s: %s", _TL("failed to create destination file"), Destination.c_str()));
988
989 return( false );
990 }
991
992 Stream.Write("#! /usr/bin/env python\n\n'''\n");
993 Stream.Write("Python Interface to SAGA Tools\n");
994 Stream.Write("----------\n'''\n\n");
995 Stream.Write("from PySAGA.helper import Tool_Wrapper\n\n");
996 }
997 else
998 {
999 if( !SG_Dir_Exists(Destination) && !SG_Dir_Create(Destination, true) )
1000 {
1001 SG_UI_Msg_Add_Error(CSG_String::Format("%s: %s", _TL("failed to create destination folder"), Destination.c_str()));
1002
1003 return( false );
1004 }
1005
1006 if( bClean )
1007 {
1008 CSG_Strings Files;
1009
1010 SG_Dir_List_Files(Files, Destination);
1011
1012 for(int i=0; i<Files.Get_Count(); i++)
1013 {
1014 SG_File_Delete(Files[i]);
1015 }
1016 }
1017
1018 if( !Init.Open(SG_File_Make_Path(Destination, SG_T("__init__"), "py"), SG_FILE_W, false, SG_FILE_ENCODING_UTF8) )
1019 {
1020 SG_UI_Msg_Add_Error(CSG_String::Format("%s: %s", _TL("failed to create file"), SG_T("__init__.py")));
1021
1022 return( false );
1023 }
1024 }
1025
1027
1028 //-----------------------------------------------------
1029 // skip tool chains in 1st run...
1030 for(int iLibrary=0; iLibrary<Get_Count() && SG_UI_Process_Set_Progress(iLibrary, Get_Count()); iLibrary++)
1031 {
1032 CSG_Tool_Library *pLibrary = Get_Library(iLibrary);
1033
1034 if( !pLibrary->Get_Category ().Cmp("SAGA Development" ) // generally exclude certain categories/libraries
1035 || !pLibrary->Get_Category ().Cmp("Garden" )
1036 || !pLibrary->Get_Library_Name().Cmp("grid_calculus_bsl")
1037 || !pLibrary->Get_Library_Name().Cmp("pj_geotrans" )
1038 // || !pLibrary->Get_Library_Name().Cmp("vis_3d_viewer" )
1039 || pLibrary->Get_Type() == ESG_Library_Type::Chain ) // exclude tool chains in 1st run
1040 {
1041 continue;
1042 }
1043
1044 SG_UI_StatusBar_Set_Text(CSG_String::Format("%s: %s", SG_T("Library"), pLibrary->Get_Library_Name().c_str()));
1045
1046 if( !bSingleFile )
1047 {
1048 if( !Stream.Open(SG_File_Make_Path(Destination, pLibrary->Get_Library_Name(), "py"), SG_FILE_W, false, SG_FILE_ENCODING_UTF8) )
1049 {
1050 continue;
1051 }
1052
1053 Stream.Printf(Header, pLibrary->Get_Category().c_str(), pLibrary->Get_Name().c_str(), pLibrary->Get_Library_Name().c_str(), CSG_Text_Format_Converter::Html_to_SimpleText(pLibrary->Get_Description()).c_str());
1054
1055 Init.Printf("from . import %s\n", pLibrary->Get_Library_Name().c_str());
1056 }
1057
1058 for(int iTool=0; iTool<pLibrary->Get_Count(); iTool++)
1059 {
1060 CSG_Tool *pTool = pLibrary->Create_Tool(pLibrary->Get_Tool(iTool)->Get_ID(), false, false);
1061
1062 if( pTool && !pTool->is_Interactive() && pTool->Get_Parameters_Count() == 0 )
1063 {
1065
1066 if( bName )
1067 {
1069
1070 for(int i=0; pDeprecated_Names && i<pDeprecated_Names->Get_Count(); i++)
1071 {
1072 CSG_Table_Record &Name = *pDeprecated_Names->Get_Record(i);
1073
1074 if( !pLibrary->Get_Library_Name().Cmp(Name.asString(0)) && !pTool->Get_ID().Cmp(Name.asString(1)) )
1075 {
1077 }
1078 }
1079 }
1080 }
1081
1082 pLibrary->Delete_Tool(pTool);
1083 }
1084 }
1085
1086 //-----------------------------------------------------
1087 // add tool chains in 2nd run...
1088 for(int iLibrary=0; iLibrary<Get_Count() && SG_UI_Process_Set_Progress(iLibrary, Get_Count()); iLibrary++)
1089 {
1090 CSG_Tool_Library *pLibrary = Get_Library(iLibrary);
1091
1092 if( !pLibrary->Get_Category().Cmp("SAGA Development" ) // generally exclude certain categories
1093 || !pLibrary->Get_Category().Cmp("Garden" )
1094 || pLibrary->Get_Type() != ESG_Library_Type::Chain ) // only process tool chains in 2nd run
1095 {
1096 continue;
1097 }
1098
1099 SG_UI_StatusBar_Set_Text(CSG_String::Format("%s: %s", SG_T("Library"), pLibrary->Get_Library_Name().c_str()));
1100
1101 if( !bSingleFile )
1102 {
1103 CSG_String File(SG_File_Make_Path(Destination, pLibrary->Get_Library_Name(), "py"));
1104
1105 if( SG_File_Exists(File) )
1106 {
1107 if( !Stream.Open(File, SG_FILE_RW, false, SG_FILE_ENCODING_UTF8) )
1108 {
1109 continue;
1110 }
1111
1112 Stream.Seek_End();
1113 }
1114 else
1115 {
1116 if( !Stream.Open(File, SG_FILE_W, false, SG_FILE_ENCODING_UTF8) )
1117 {
1118 continue;
1119 }
1120
1121 Stream.Printf(Header, pLibrary->Get_Category().c_str(), pLibrary->Get_Name().c_str(), pLibrary->Get_Library_Name().c_str(), CSG_Text_Format_Converter::Html_to_SimpleText(pLibrary->Get_Description()).c_str());
1122
1123 Init.Printf("from . import %s\n", pLibrary->Get_Library_Name().c_str());
1124 }
1125 }
1126
1127 for(int iTool=0; iTool<pLibrary->Get_Count(); iTool++)
1128 {
1129 CSG_Tool *pTool = pLibrary->Create_Tool(pLibrary->Get_Tool(iTool)->Get_ID(), false, false);
1130
1131 if( pTool && !pTool->needs_GUI() && !pTool->is_Interactive() && pTool->Get_Parameters_Count() == 0 )
1132 {
1134
1135 if( bName )
1136 {
1138
1139 for(int i=0; pDeprecated_Names && i<pDeprecated_Names->Get_Count(); i++)
1140 {
1141 CSG_Table_Record &Name = *pDeprecated_Names->Get_Record(i);
1142
1143 if( !pLibrary->Get_Library_Name().Cmp(Name.asString(0)) && !pTool->Get_ID().Cmp(Name.asString(1)) )
1144 {
1146 }
1147 }
1148 }
1149 }
1150
1151 pLibrary->Delete_Tool(pTool);
1152 }
1153 }
1154
1155 //-----------------------------------------------------
1157
1158 return( true );
1159}
1160
1161
1163// //
1164// //
1165// //
1167
1168//---------------------------------------------------------
void SG_UI_Msg_Add_Error(const char *Message)
void SG_UI_Msg_Add(const char *Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
bool SG_UI_Process_Set_Progress(int Position, int Range)
void * SG_UI_Get_Window_Main(void)
void SG_UI_StatusBar_Set_Text(const CSG_String &Text)
int SG_UI_Msg_Lock(bool bOn)
void SG_UI_ProgressAndMsg_Lock(bool bOn)
SAGA_API_DLL_EXPORT bool SG_File_Exists(const CSG_String &FileName)
@ SG_UI_MSG_STYLE_FAILURE
Definition api_core.h:1636
@ SG_UI_MSG_STYLE_SUCCESS
Definition api_core.h:1635
SAGA_API_DLL_EXPORT bool SG_File_Cmp_Extension(const CSG_String &File, const CSG_String &Extension)
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Path_Absolute(const CSG_String &full_Path, const CSG_String &CWD="")
signed long long sLong
Definition api_core.h:158
SAGA_API_DLL_EXPORT bool SG_Dir_Create(const CSG_String &Directory, bool bFullPath=false)
Definition api_file.cpp:982
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:976
@ SG_DATATYPE_String
Definition api_core.h:1055
#define _TL(s)
Definition api_core.h:1619
@ SG_FILE_ENCODING_UTF8
Definition api_core.h:552
@ SG_FILE_W
Definition api_core.h:1161
@ SG_FILE_R
Definition api_core.h:1160
@ SG_FILE_RW
Definition api_core.h:1162
static CSG_Strings Get_Tool_Paths(void)
Definition api_core.cpp:542
static bool Set_Application_Name(const CSG_String &Name="")
Definition api_core.cpp:459
static const CSG_String & Get_API_Path(void)
Definition api_core.cpp:480
static bool Get_Variable(const CSG_String &Variable)
Definition api_core.cpp:662
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:382
size_t Read(void *Buffer, size_t Size, size_t Count=1) const
Definition api_file.cpp:338
int Printf(const char *Format,...)
Definition api_file.cpp:287
static void Initialize(void)
bool asInt(int &Value) const
int Cmp(const CSG_String &String) const
static CSG_String Format(const char *Format,...)
const SG_Char * c_str(void) const
bool is_Empty(void) const
const class wxString & wx_str(void) const
Definition api_core.h:595
int Get_Count(void) const
Definition api_core.h:725
bool Set_Value(int Field, const CSG_String &Value)
const SG_Char * asString(int Field, int Decimals=-99) const
bool Create(void)
Definition table.cpp:153
sLong Get_Count(void) const
Definition table.h:410
virtual CSG_Table_Record * Get_Record(sLong Index) const
Definition table.h:412
int Get_Field_Count(void) const
Definition table.h:371
virtual bool Set_Field_Type(int Field, TSG_Data_Type Type)
Definition table.cpp:610
static CSG_String Html_Absolute_Image_Paths(const CSG_String &Text, const CSG_String &Path, bool bAltAsText=false)
static CSG_String Markdown_to_Html(const CSG_String &Text, bool bSimple=true)
static CSG_String Html_to_SimpleText(const CSG_String &Text)
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)
int Get_Count(void) const
int Get_Tool_Count(void) const
virtual ~CSG_Tool_Library_Manager(void)
bool Create_Python_ToolBox(const CSG_String &Destination, bool bClean=true, bool bName=true, bool bSingleFile=false, CSG_Table *pDeprecated_Names=NULL) const
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)
bool Load_Description(void)
CSG_String m_File_Name
virtual CSG_Tool * Create_Tool(int Index, bool bWithGUI=false, bool bWithCMD=true)
const CSG_String & Get_Menu(void) const
void Add_Reference(const CSG_String &Authors, const CSG_String &Date, const CSG_String &Title, const CSG_String &Source, const CSG_String &Link="", const CSG_String &Link_Text="")
const CSG_String & Get_Description(void) const
bool Del_References(void)
virtual CSG_Tool * Get_Tool(int Index, TSG_Tool_Type Type=TOOL_TYPE_Base) 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)
const CSG_String & Get_Name(void) const
CSG_Strings Get_References(bool bHTML=true) const
virtual const CSG_String & Get_Info(int Type) const
bool is_Valid(void) const
const CSG_String & Get_Category(void) const
virtual ESG_Library_Type Get_Type(void) const
virtual int Get_Count(void) const
CSG_String m_Library_Name
const CSG_String & Get_Library(void) const
Definition tool.cpp:129
int Get_Parameters_Count(void) const
Definition tool.h:177
virtual bool is_Interactive(void) const
Definition tool.h:236
CSG_String Get_Script(Script_Format Format, bool bHeader=true, int Arguments=0, bool bWrapArgs=true)
Definition tool.cpp:1851
const CSG_String & Get_Name(void) const
Definition tool.cpp:146
virtual bool needs_GUI(void) const
Definition tool.h:231
virtual TSG_Tool_Type Get_Type(void) const
Definition tool.h:148
const CSG_String & Get_ID(void) const
Definition tool.h:150
static const CSG_String & Get_Description_Source(void)
Definition tool.cpp:453
void Set_Name(const CSG_String &String)
Definition tool.cpp:141
#define SYMBOL_TLB_Get_Interface
Definition tool.h:724
CSG_Tool_Library_Interface *(* TSG_PFNC_TLB_Get_Interface)(void)
Definition tool.h:725
@ TLB_INFO_Library
Definition tool.h:677
@ TLB_INFO_Description
Definition tool.h:670
@ TLB_INFO_File
Definition tool.h:676
bool(* TSG_PFNC_TLB_Finalize)(void)
Definition tool.h:722
TSG_Tool_Type
Definition tool.h:101
@ TOOL_TYPE_Base
Definition tool.h:102
@ TOOL_TYPE_Chain
Definition tool.h:106
bool(* TSG_PFNC_TLB_Initialize)(const SG_Char *)
Definition tool.h:719
#define SYMBOL_TLB_Initialize
Definition tool.h:718
#define SYMBOL_TLB_Finalize
Definition tool.h:721
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