SAGA API  v9.5
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 //---------------------------------------------------------
106 bool 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 //---------------------------------------------------------
157 CSG_Tool * CSG_Tool_Library::Get_Tool(const char *Name, TSG_Tool_Type Type) const { return( Get_Tool(CSG_String(Name), Type) ); }
158 CSG_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 //---------------------------------------------------------
190 CSG_Tool * CSG_Tool_Library::Create_Tool(int Index, bool bWithGUI)
191 {
192  return( m_pInterface ? m_pInterface->Create_Tool(Index, bWithGUI) : NULL );
193 }
194 
195 //---------------------------------------------------------
196 CSG_Tool * CSG_Tool_Library::Create_Tool(const char *Name, bool bWithGUI) { return( Create_Tool(CSG_String(Name), bWithGUI) ); }
197 CSG_Tool * CSG_Tool_Library::Create_Tool(const wchar_t *Name, bool bWithGUI) { return( Create_Tool(CSG_String(Name), bWithGUI) ); }
198 CSG_Tool * CSG_Tool_Library::Create_Tool(const CSG_String &Name, bool bWithGUI)
199 {
200  int Index; return( Name.asInt(Index) ? Create_Tool(Index, bWithGUI) : 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 //---------------------------------------------------------
242 void 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 //---------------------------------------------------------
262 void 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 //---------------------------------------------------------
287 {
288  return( g_Tool_Library_Manager );
289 }
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 //---------------------------------------------------------
310 {
311  Destroy();
312 }
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( !SG_File_Cmp_Extension(File, "mlb" )
341  && !SG_File_Cmp_Extension(File, "dll" )
342  && !SG_File_Cmp_Extension(File, "so" )
343  && !SG_File_Cmp_Extension(File, "dylib") )
344  {
345  return( _Add_Tool_Chain(File) );
346  }
347 
348  //-----------------------------------------------------
349  SG_UI_Msg_Add(CSG_String::Format("%s: %s...", _TL("Loading library"), File.c_str()), true);
350 
351  wxFileName fn(File.c_str());
352 
353  for(int i=0; i<Get_Count(); i++)
354  {
355  if( fn == Get_Library(i)->Get_File_Name().c_str() )
356  {
357  SG_UI_Msg_Add(_TL("has already been loaded"), false);
358 
359  return( NULL );
360  }
361  }
362 
363  //-----------------------------------------------------
364  CSG_Tool_Library *pLibrary = new CSG_Tool_Library(File);
365 
366  if( pLibrary->is_Valid() )
367  {
368  m_pLibraries = (CSG_Tool_Library **)SG_Realloc(m_pLibraries, (m_nLibraries + 1) * sizeof(CSG_Tool_Library *));
369  m_pLibraries[m_nLibraries++] = pLibrary;
370 
371  SG_UI_Msg_Add(_TL("okay"), false, SG_UI_MSG_STYLE_SUCCESS);
372 
373  return( pLibrary );
374  }
375 
376  delete(pLibrary);
377 
378  SG_UI_Msg_Add(_TL("failed"), false, SG_UI_MSG_STYLE_FAILURE);
379 
380  return( NULL );
381 }
382 
384 {
385  return( Add_Library(CSG_String(File)) );
386 }
387 
389 {
390  return( Add_Library(CSG_String(File)) );
391 }
392 
393 //---------------------------------------------------------
394 int CSG_Tool_Library_Manager::Add_Directory(const CSG_String &Directory, bool bOnlySubDirectories)
395 {
396  int nOpened = 0; wxDir Dir;
397 
398  if( Dir.Open(Directory.c_str()) )
399  {
400  wxString FileName, DirName(Dir.GetName());
401 
402  if( !bOnlySubDirectories && Dir.GetFirst(&FileName, wxEmptyString, wxDIR_FILES) )
403  {
404  do
405  { if( FileName.Find("saga_") < 0 && FileName.Find("wx") < 0 )
406  if( Add_Library(SG_File_Make_Path(&DirName, &FileName)) )
407  {
408  nOpened++;
409  }
410  }
411  while( Dir.GetNext(&FileName) );
412  }
413 
414  if( Dir.GetFirst(&FileName, wxEmptyString, wxDIR_DIRS) )
415  {
416  do
417  {
418  if( FileName.CmpNoCase("dll") )
419  {
420  nOpened += Add_Directory(SG_File_Make_Path(&DirName, &FileName), false);
421  }
422  }
423  while( Dir.GetNext(&FileName) );
424  }
425  }
426 
427  return( nOpened );
428 }
429 
430 int CSG_Tool_Library_Manager::Add_Directory(const char *Directory, bool bOnlySubDirectories)
431 {
432  return( Add_Directory(CSG_String(Directory), bOnlySubDirectories) );
433 }
434 
435 int CSG_Tool_Library_Manager::Add_Directory(const wchar_t *Directory, bool bOnlySubDirectories)
436 {
437  return( Add_Directory(CSG_String(Directory), bOnlySubDirectories) );
438 }
439 
440 
442 // //
444 
445 //---------------------------------------------------------
446 CSG_Tool_Library * CSG_Tool_Library_Manager::_Add_Tool_Chain(const CSG_String &File)
447 {
448  if( !SG_File_Cmp_Extension(File, "xml") )
449  {
450  return( NULL );
451  }
452 
453  //-----------------------------------------------------
454  CSG_Tool_Chains *pLibrary = NULL;
455  CSG_Tool_Chain *pTool = NULL;
456 
457  //-----------------------------------------------------
458  { // is tool chain already loaded ?
459  wxFileName fn(File.c_str());
460 
461  for(int iLibrary=0; !pTool && iLibrary<Get_Count(); iLibrary++)
462  {
463  if( Get_Library(iLibrary)->Get_Type() == TOOL_CHAINS )
464  {
465  for(int iTool=0; !pTool && iTool<Get_Library(iLibrary)->Get_Count(); iTool++)
466  {
467  if( fn == ((CSG_Tool_Chain *)Get_Library(iLibrary)->Get_Tool(iTool))->Get_File_Name().c_str() )
468  {
469  pLibrary = (CSG_Tool_Chains *)Get_Library(iLibrary);
470  pTool = (CSG_Tool_Chain *)Get_Library(iLibrary)->Get_Tool(iTool);
471  }
472  }
473  }
474  }
475 
476  if( pTool ) // ...then try to reload !
477  {
479  CSG_Tool_Chain Tool(File); // don't reset loaded tool in case reloading fails!!!
481 
482  if( Tool.is_Okay() )
483  {
484  pTool->Create(File);
485  }
486 
487  return( pLibrary );
488  }
489  }
490 
491  //-----------------------------------------------------
492  pTool = new CSG_Tool_Chain(File);
493 
494  if( !pTool || !pTool->is_Okay() )
495  {
496  if( pTool )
497  {
498  delete(pTool);
499  }
500 
501  return( NULL );
502  }
503 
504  //-----------------------------------------------------
505  CSG_String Library = pTool->Get_Library();
506 
507  for(int iLibrary=0; !pLibrary && iLibrary<Get_Count(); iLibrary++)
508  {
509  if( Get_Library(iLibrary)->Get_Type() == TOOL_CHAINS
510  && Get_Library(iLibrary)->Get_Library_Name().Cmp(Library) == 0 )
511  {
512  pLibrary = (CSG_Tool_Chains *)Get_Library(iLibrary);
513  }
514  }
515 
516  if( !pLibrary && (pLibrary = new CSG_Tool_Chains(pTool->Get_Library(), SG_File_Get_Path(File))) != NULL )
517  {
518  m_pLibraries = (CSG_Tool_Library **)SG_Realloc(m_pLibraries, (m_nLibraries + 1) * sizeof(CSG_Tool_Library *));
519  m_pLibraries[m_nLibraries++] = pLibrary;
520  }
521 
522  if( !pLibrary ) // this should never happen, but who knows...
523  {
524  SG_UI_Msg_Add_Error(CSG_String::Format("%s %s: %s", _TL("ERROR"), _TL("tool chain library"), File.c_str()));
525 
526  delete(pTool);
527 
528  return( NULL );
529  }
530 
531  pLibrary->Add_Tool(pTool);
532 
533  //-----------------------------------------------------
534  return( pLibrary );
535 }
536 
537 
539 // //
541 
542 //---------------------------------------------------------
544 {
545  if( m_pLibraries )
546  {
547  for(int i=0; i<Get_Count(); i++)
548  {
549 // #ifndef _SAGA_MSW
550  if( !SG_UI_Get_Window_Main() && m_pLibraries[i]->m_pLibrary )
551  {
552  m_pLibraries[i]->m_pLibrary->Detach();
553  }
554 // #endif
555 
556  delete(m_pLibraries[i]);
557  }
558 
559  SG_Free(m_pLibraries);
560 
561  m_pLibraries = NULL;
562  m_nLibraries = 0;
563  }
564 
565  return( true );
566 }
567 
568 
570 // //
572 
573 //---------------------------------------------------------
575 {
576  for(int i=0; i<Get_Count(); i++)
577  {
578  if( pLibrary == Get_Library(i) )
579  {
580  return( Del_Library(i) );
581  }
582  }
583 
584  return( false );
585 }
586 
587 //---------------------------------------------------------
589 {
590  if( i >= 0 && i < Get_Count() )
591  {
592  delete(m_pLibraries[i]);
593 
594  for(m_nLibraries--; i<m_nLibraries; i++)
595  {
596  m_pLibraries[i] = m_pLibraries[i + 1];
597  }
598 
599  m_pLibraries = (CSG_Tool_Library **)SG_Realloc(m_pLibraries, m_nLibraries * sizeof(CSG_Tool_Library *));
600 
601  return( true );
602  }
603 
604  return( false );
605 }
606 
607 
609 // //
611 
612 //---------------------------------------------------------
614 {
615  for(int i=0; i<Get_Count(); i++)
616  {
617  CSG_Tool_Library *pLibrary = Get_Library(i);
618 
619  if( !Name.Cmp(bLibrary ? pLibrary->Get_Library_Name() : pLibrary->Get_Name()) )
620  {
621  return( pLibrary );
622  }
623  }
624 
625  return( NULL );
626 }
627 
628 CSG_Tool_Library * CSG_Tool_Library_Manager::Get_Library(const char *Name, bool bLibrary) const
629 {
630  return( Get_Library(CSG_String(Name), bLibrary) );
631 }
632 
633 CSG_Tool_Library * CSG_Tool_Library_Manager::Get_Library(const wchar_t *Name, bool bLibrary) const
634 {
635  return( Get_Library(CSG_String(Name), bLibrary) );
636 }
637 
638 //---------------------------------------------------------
640 {
641  for(int i=0; i<Get_Count(); i++)
642  {
643  if( pLibrary == Get_Library(i) )
644  {
645  return( true );
646  }
647  }
648 
649  return( false );
650 }
651 
652 
654 // //
656 
657 //---------------------------------------------------------
658 CSG_Tool * CSG_Tool_Library_Manager::Get_Tool(const char *Library, int ID) const { return( Get_Tool(CSG_String(Library), ID) ); }
659 CSG_Tool * CSG_Tool_Library_Manager::Get_Tool(const wchar_t *Library, int ID) const { return( Get_Tool(CSG_String(Library), ID) ); }
661 {
662  return( Get_Tool(Library, CSG_String::Format("%d", ID)) );
663 }
664 
665 //---------------------------------------------------------
666 CSG_Tool * CSG_Tool_Library_Manager::Get_Tool(const char *Library, const char *Name) const { return( Get_Tool(CSG_String(Library), CSG_String(Name)) ); }
667 CSG_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)) ); }
669 {
670  for(int i=0; i<Get_Count(); i++)
671  {
672  CSG_Tool_Library *pLibrary = Get_Library(i);
673 
674  if( pLibrary->Get_Library_Name().Cmp(Library) == 0 )
675  {
676  CSG_Tool *pTool = pLibrary->Get_Tool(Name);
677 
678  if( pTool )
679  {
680  return( pTool );
681  }
682  }
683  }
684 
685  return( NULL );
686 }
687 
688 
690 // //
692 
693 //---------------------------------------------------------
694 CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const char *Library, int Index, bool bWithGUI) const { return( Create_Tool(CSG_String(Library), Index, bWithGUI) ); }
695 CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const wchar_t *Library, int Index, bool bWithGUI) const { return( Create_Tool(CSG_String(Library), Index, bWithGUI) ); }
696 CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const CSG_String &Library, int Index, bool bWithGUI) const
697 {
698  return( Create_Tool(Library, CSG_String::Format("%d", Index), bWithGUI) );
699 }
700 
701 //---------------------------------------------------------
702 CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const char *Library, const char *Name, bool bWithGUI) const { return( Create_Tool(CSG_String(Library), CSG_String(Name), bWithGUI) ); }
703 CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const wchar_t *Library, const wchar_t *Name, bool bWithGUI) const { return( Create_Tool(CSG_String(Library), CSG_String(Name), bWithGUI) ); }
704 CSG_Tool * CSG_Tool_Library_Manager::Create_Tool(const CSG_String &Library, const CSG_String &Name, bool bWithGUI) const
705 {
706  for(int i=0; i<Get_Count(); i++)
707  {
708  CSG_Tool_Library *pLibrary = Get_Library(i);
709 
710  if( pLibrary->Get_Library_Name().Cmp(Library) == 0 )
711  {
712  CSG_Tool *pTool = pLibrary->Create_Tool(Name, bWithGUI);
713 
714  if( pTool )
715  {
716  return( pTool );
717  }
718  }
719  }
720 
721  return( NULL );
722 }
723 
724 //---------------------------------------------------------
726 {
727  for(int i=0; i<Get_Count(); i++)
728  {
729  if( Get_Library(i)->Delete_Tool(pTool) )
730  {
731  return( true );
732  }
733  }
734 
735  return( false );
736 }
737 
738 
740 // //
742 
743 //---------------------------------------------------------
744 bool CSG_Tool_Library_Manager::Create_Python_ToolBox(const CSG_String &Destination, bool bClean, bool bName, bool bSingleFile) const
745 {
746  const char *Header =
747  "#! /usr/bin/env python\n\n"
748  "'''\n"
749  "Python Interface to SAGA Tools Library\n"
750  "----------\n"
751  "- Category : %s\n"
752  "- Name : %s\n"
753  "- ID : %s\n\n"
754  "Description\n"
755  "----------\n%s\n"
756  "'''\n\n"
757  "from PySAGA.helper import Tool_Wrapper\n\n";
758 
759  CSG_File Stream;
760 
761  if( bSingleFile )
762  {
763  if( SG_File_Exists(Destination) )
764  {
765  SG_File_Delete(Destination);
766  }
767 
768  if( !Stream.Open(Destination, SG_FILE_W, false, SG_FILE_ENCODING_UTF8) )
769  {
770  SG_UI_Msg_Add_Error(CSG_String::Format("%s: %s", _TL("failed to create destination file"), Destination.c_str()));
771 
772  return( false );
773  }
774 
775  Stream.Write("#! /usr/bin/env python\n\n'''\n");
776  Stream.Write("Python Interface to SAGA Tools\n");
777  Stream.Write("----------\n'''\n\n");
778  Stream.Write("from PySAGA.helper import Tool_Wrapper\n\n");
779  }
780  else
781  {
782  if( !SG_Dir_Exists(Destination) && !SG_Dir_Create(Destination, true) )
783  {
784  SG_UI_Msg_Add_Error(CSG_String::Format("%s: %s", _TL("failed to create destination folder"), Destination.c_str()));
785 
786  return( false );
787  }
788 
789  if( bClean )
790  {
791  CSG_Strings Files;
792 
793  SG_Dir_List_Files(Files, Destination);
794 
795  for(int i=0; i<Files.Get_Count(); i++)
796  {
797  SG_File_Delete(Files[i]);
798  }
799  }
800  }
801 
802  //-----------------------------------------------------
803  // skip tool chains in 1st run...
804  for(int iLibrary=0; iLibrary<Get_Count() && SG_UI_Process_Set_Progress(iLibrary, Get_Count()); iLibrary++)
805  {
806  CSG_Tool_Library *pLibrary = Get_Library(iLibrary);
807 
808  if( !pLibrary->Get_Category ().Cmp("SAGA Development" ) // generally exclude certain categories/libraries
809  || !pLibrary->Get_Category ().Cmp("Garden" )
810  || !pLibrary->Get_Library_Name().Cmp("grid_calculus_bsl")
811  || !pLibrary->Get_Library_Name().Cmp("pj_geotrans" )
812  || !pLibrary->Get_Library_Name().Cmp("vis_3d_viewer" )
813  || pLibrary->Get_Type() == TOOL_CHAINS ) // exclude tool chains in 1st run
814  {
815  continue;
816  }
817 
818  SG_UI_Process_Set_Text(CSG_String::Format("%s: %s", SG_T("Library"), pLibrary->Get_Library_Name().c_str()));
819 
820  if( !bSingleFile )
821  {
822  if( !Stream.Open(SG_File_Make_Path(Destination, pLibrary->Get_Library_Name(), "py"), SG_FILE_W, false, SG_FILE_ENCODING_UTF8) )
823  {
824  continue;
825  }
826 
827  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());
828  }
829 
830  for(int iTool=0; iTool<pLibrary->Get_Count(); iTool++)
831  {
832  CSG_Tool *pTool = pLibrary->Get_Tool(iTool);
833 
834  if( pTool && pTool != TLB_INTERFACE_SKIP_TOOL && !pTool->needs_GUI() && !pTool->is_Interactive() && pTool->Get_Parameters_Count() == 0 )
835  {
836  if( bName )
837  {
838  Stream.Write(pTool->Get_Script(TOOL_SCRIPT_PYTHON_WRAP_NAME, false));
839  }
840 
841  Stream.Write(pTool->Get_Script(TOOL_SCRIPT_PYTHON_WRAP_ID, false));
842  }
843  }
844  }
845 
846  //-----------------------------------------------------
847  // add tool chains in 2nd run...
848  for(int iLibrary=0; iLibrary<Get_Count() && SG_UI_Process_Set_Progress(iLibrary, Get_Count()); iLibrary++)
849  {
850  CSG_Tool_Library *pLibrary = Get_Library(iLibrary);
851 
852  if( !pLibrary->Get_Category().Cmp("SAGA Development" ) // generally exclude certain categories
853  || !pLibrary->Get_Category().Cmp("Garden" )
854  || pLibrary->Get_Type() != TOOL_CHAINS ) // only process tool chains in 2nd run
855  {
856  continue;
857  }
858 
859  SG_UI_Process_Set_Text(CSG_String::Format("%s: %s", SG_T("Library"), pLibrary->Get_Library_Name().c_str()));
860 
861  if( !bSingleFile )
862  {
863  CSG_String File(SG_File_Make_Path(Destination, pLibrary->Get_Library_Name(), "py"));
864 
865  if( SG_File_Exists(File) )
866  {
867  if( !Stream.Open(File, SG_FILE_RW, false, SG_FILE_ENCODING_UTF8) )
868  {
869  continue;
870  }
871 
872  Stream.Seek_End();
873  }
874  else
875  {
876  if( !Stream.Open(File, SG_FILE_W, false, SG_FILE_ENCODING_UTF8) )
877  {
878  continue;
879  }
880 
881  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());
882  }
883  }
884 
885  for(int iTool=0; iTool<pLibrary->Get_Count(); iTool++)
886  {
887  CSG_Tool *pTool = pLibrary->Get_Tool(iTool);
888 
889  if( pTool && pTool != TLB_INTERFACE_SKIP_TOOL && !pTool->needs_GUI() && !pTool->is_Interactive() && pTool->Get_Parameters_Count() == 0 )
890  {
891  if( bName )
892  {
893  Stream.Write(pTool->Get_Script(TOOL_SCRIPT_PYTHON_WRAP_NAME, false));
894  }
895 
896  Stream.Write(pTool->Get_Script(TOOL_SCRIPT_PYTHON_WRAP_ID, false));
897  }
898  }
899  }
900 
901  return( true );
902 }
903 
904 
906 // //
907 // //
908 // //
910 
911 //---------------------------------------------------------
CSG_Tool_Library_Interface::Get_Tool
CSG_Tool * Get_Tool(int i)
Definition: tool_library_interface.cpp:180
CSG_Tool_Chains
Definition: tool_chain.h:174
CSG_File::Open
virtual bool Open(const CSG_String &FileName, int Mode=SG_FILE_R, bool bBinary=true, int Encoding=SG_FILE_ENCODING_ANSI)
Definition: api_file.cpp:111
CSG_Tool_Chains::Add_Tool
bool Add_Tool(CSG_Tool_Chain *pTool)
CSG_Tool_Library_Manager::~CSG_Tool_Library_Manager
virtual ~CSG_Tool_Library_Manager(void)
Definition: tool_library.cpp:309
SG_File_Get_Path
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Path(const CSG_String &full_Path)
Definition: api_file.cpp:891
SG_T
#define SG_T(s)
Definition: api_core.h:537
CSG_String::Printf
int Printf(const char *Format,...)
Definition: api_string.cpp:308
_TL
#define _TL(s)
Definition: api_core.h:1489
CSG_Tool_Library_Manager::Destroy
bool Destroy(void)
Definition: tool_library.cpp:543
CSG_Tool_Library_Manager::Get_Tool
CSG_Tool * Get_Tool(const CSG_String &Library, int Index) const
Definition: tool_library.cpp:660
SG_HTML_Tag_Replacer
SAGA_API_DLL_EXPORT CSG_String SG_HTML_Tag_Replacer(const CSG_String &Text)
Definition: api_string.cpp:1408
TLB_INFO_Library
@ TLB_INFO_Library
Definition: tool.h:629
CSG_Tool_Library_Manager::Add_Library
CSG_Tool_Library * Add_Library(const CSG_String &File)
Definition: tool_library.cpp:338
SG_Dir_Exists
SAGA_API_DLL_EXPORT bool SG_Dir_Exists(const CSG_String &Directory)
Definition: api_file.cpp:743
CSG_Tool_Library
Definition: tool_library.h:107
CSG_Tool_Library::CSG_Tool_Library
CSG_Tool_Library(void)
Definition: tool_library.cpp:68
SG_UI_MSG_STYLE_SUCCESS
@ SG_UI_MSG_STYLE_SUCCESS
Definition: api_core.h:1505
CSG_String::asInt
int asInt(void) const
Definition: api_string.cpp:722
SG_File_Cmp_Extension
SAGA_API_DLL_EXPORT bool SG_File_Cmp_Extension(const CSG_String &FileName, const CSG_String &Extension)
Definition: api_file.cpp:952
CSG_Tool::needs_GUI
virtual bool needs_GUI(void) const
Definition: tool.h:233
tool_chain.h
CSG_Tool_Library::Get_Name
CSG_String Get_Name(void) const
Definition: tool_library.h:120
CSG_Tool
Definition: tool.h:151
CSG_Tool_Library::Del_References
void Del_References(void)
Definition: tool_library.cpp:270
CSG_Tool_Library_Manager::Del_Library
bool Del_Library(int i)
Definition: tool_library.cpp:588
CSG_Tool::Get_Name
const CSG_String & Get_Name(void) const
Definition: tool.cpp:132
SG_File_Delete
SAGA_API_DLL_EXPORT bool SG_File_Delete(const CSG_String &FileName)
Definition: api_file.cpp:856
SG_Free
SAGA_API_DLL_EXPORT void SG_Free(void *memblock)
Definition: api_memory.cpp:83
TSG_Tool_Type
TSG_Tool_Type
Definition: tool.h:101
CSG_Tool_Library::~CSG_Tool_Library
virtual ~CSG_Tool_Library(void)
Definition: tool_library.cpp:100
CSG_Tool_Library_Manager::Delete_Tool
bool Delete_Tool(CSG_Tool *pTool) const
Definition: tool_library.cpp:725
CSG_File
Definition: api_core.h:1124
CSG_String::Cmp
int Cmp(const CSG_String &String) const
Definition: api_string.cpp:515
CSG_Tool::Get_Parameters_Count
int Get_Parameters_Count(void) const
Definition: tool.h:182
SYMBOL_TLB_Initialize
#define SYMBOL_TLB_Initialize
Definition: tool.h:670
TSG_PFNC_TLB_Get_Interface
CSG_Tool_Library_Interface *(* TSG_PFNC_TLB_Get_Interface)(void)
Definition: tool.h:677
SG_UI_MSG_STYLE_FAILURE
@ SG_UI_MSG_STYLE_FAILURE
Definition: api_core.h:1506
SG_FILE_RW
@ SG_FILE_RW
Definition: api_core.h:1111
CSG_Random::Initialize
static void Initialize(void)
Definition: mat_tools.cpp:260
CSG_Tool_Library::Delete_Tools
virtual bool Delete_Tools(void)
Definition: tool_library.cpp:215
SG_File_Exists
SAGA_API_DLL_EXPORT bool SG_File_Exists(const CSG_String &FileName)
Definition: api_file.cpp:850
CSG_Strings::Clear
void Clear(void)
Definition: api_core.h:734
CSG_Tool_Library_Interface::Delete_Tool
bool Delete_Tool(CSG_Tool *pTool)
Definition: tool_library_interface.cpp:217
CSG_Tool_Library_Manager::is_Loaded
bool is_Loaded(CSG_Tool_Library *pLibrary) const
Definition: tool_library.cpp:639
CSG_Tool_Library::Get_File_Name
const CSG_String & Get_File_Name(void) const
Definition: tool_library.h:116
CSG_Tool_Library_Manager::Get_Count
int Get_Count(void) const
Definition: tool_library.h:193
TLB_INTERFACE_SKIP_TOOL
#define TLB_INTERFACE_SKIP_TOOL
Definition: tool.h:680
SG_Dir_Create
SAGA_API_DLL_EXPORT bool SG_Dir_Create(const CSG_String &Directory, bool bFullPath=false)
Definition: api_file.cpp:749
TLB_INFO_File
@ TLB_INFO_File
Definition: tool.h:628
CSG_Tool_Library::Get_Count
virtual int Get_Count(void) const
Definition: tool_library.h:130
SYMBOL_TLB_Get_Interface
#define SYMBOL_TLB_Get_Interface
Definition: tool.h:676
CSG_Tool_Library_Manager::Create_Tool
CSG_Tool * Create_Tool(const CSG_String &Library, int Index, bool bWithGUI=false) const
Definition: tool_library.cpp:696
CSG_Tool_Library::Delete_Tool
virtual bool Delete_Tool(CSG_Tool *pTool)
Definition: tool_library.cpp:209
CSG_Tool::Get_Library
const CSG_String & Get_Library(void) const
Definition: tool.cpp:115
CSG_Tool_Library::Get_Description
CSG_String Get_Description(void) const
Definition: tool_library.h:121
SG_Get_Tool_Library_Manager
CSG_Tool_Library_Manager & SG_Get_Tool_Library_Manager(void)
Definition: tool_library.cpp:286
CSG_Tool_Chain::Create
bool Create(const CSG_Tool_Chain &Tool, bool bWithGUI)
Definition: tool_chain.cpp:133
CSG_Tool_Library::Get_Library_Name
const CSG_String & Get_Library_Name(void) const
Definition: tool_library.h:117
CSG_Tool::Get_Script
CSG_String Get_Script(TSG_Tool_Script_Type Type, bool bHeader=true, bool bAllParameters=false)
Definition: tool.cpp:1322
TSG_PFNC_TLB_Finalize
bool(* TSG_PFNC_TLB_Finalize)(void)
Definition: tool.h:674
g_Tool_Library_Manager
CSG_Tool_Library_Manager g_Tool_Library_Manager
Definition: tool_library.cpp:283
SG_UI_Process_Set_Text
void SG_UI_Process_Set_Text(const CSG_String &Text)
Definition: api_callback.cpp:324
CSG_Tool_Library::m_Library_Name
CSG_String m_Library_Name
Definition: tool_library.h:161
CSG_Tool_Library_Interface::Get_Count
int Get_Count(void)
Definition: tool_library_interface.cpp:174
CSG_Tool::Get_ID
const CSG_String & Get_ID(void) const
Definition: tool.h:165
CSG_Tool_Library::Get_Info
virtual CSG_String Get_Info(int Type) const
Definition: tool_library.cpp:133
CSG_Tool_Library::Add_Reference
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)
Definition: tool_library.cpp:242
CSG_Tool_Library::Get_Menu
CSG_String Get_Menu(void) const
Definition: tool_library.h:124
SG_FILE_W
@ SG_FILE_W
Definition: api_core.h:1110
CSG_Strings
Definition: api_core.h:699
CSG_Tool_Library_Manager::CSG_Tool_Library_Manager
CSG_Tool_Library_Manager(void)
Definition: tool_library.cpp:297
TOOL_CHAINS
@ TOOL_CHAINS
Definition: tool_library.h:94
CSG_Tool_Library_Interface::Delete_Tools
bool Delete_Tools(void)
Definition: tool_library_interface.cpp:233
SG_Dir_List_Files
SAGA_API_DLL_EXPORT bool SG_Dir_List_Files(CSG_Strings &List, const CSG_String &Directory)
Definition: api_file.cpp:811
CSG_String::Format
static CSG_String Format(const char *Format,...)
Definition: api_string.cpp:270
CSG_Tool_Chain
Definition: tool_chain.h:78
CSG_Tool_Library::Get_Type
virtual TSG_Tool_Library_Type Get_Type(void) const
Definition: tool_library.h:112
CSG_Tool_Library_Manager::Create_Python_ToolBox
bool Create_Python_ToolBox(const CSG_String &Destination, bool bClean=true, bool bName=true, bool bSingleFile=false) const
Definition: tool_library.cpp:744
TOOL_SCRIPT_PYTHON_WRAP_NAME
@ TOOL_SCRIPT_PYTHON_WRAP_NAME
Definition: tool.h:126
SG_File_Get_Path_Absolute
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Path_Absolute(const CSG_String &full_Path)
Definition: api_file.cpp:899
SG_Char
#define SG_Char
Definition: api_core.h:536
TSG_PFNC_TLB_Initialize
bool(* TSG_PFNC_TLB_Initialize)(const SG_Char *)
Definition: tool.h:671
CSG_String
Definition: api_core.h:563
SYMBOL_TLB_Finalize
#define SYMBOL_TLB_Finalize
Definition: tool.h:673
CSG_Tool_Library_Interface::Get_Info
const CSG_String & Get_Info(int ID)
Definition: tool_library_interface.cpp:163
CSG_Tool_Library::Get_Tool
virtual CSG_Tool * Get_Tool(int Index, TSG_Tool_Type Type=TOOL_TYPE_Base) const
Definition: tool_library.cpp:149
CSG_Strings::Sort
bool Sort(bool Ascending=true)
Definition: api_string.cpp:1121
CSG_Tool_Chain::is_Okay
bool is_Okay(void) const
Definition: tool_chain.h:96
CSG_Tool_Library::is_Valid
bool is_Valid(void) const
Definition: tool_library.h:114
CSG_File::Printf
int Printf(const char *Format,...)
Definition: api_file.cpp:277
CSG_String::is_Empty
bool is_Empty(void) const
Definition: api_string.cpp:178
SG_UI_Process_Set_Progress
bool SG_UI_Process_Set_Progress(int Position, int Range)
Definition: api_callback.cpp:256
TOOL_TYPE_Base
@ TOOL_TYPE_Base
Definition: tool.h:102
CSG_Tool_Library::Create_Tool
virtual CSG_Tool * Create_Tool(int Index, bool bWithGUI=false)
Definition: tool_library.cpp:190
CSG_Tool::is_Interactive
virtual bool is_Interactive(void) const
Definition: tool.h:238
TOOL_SCRIPT_PYTHON_WRAP_ID
@ TOOL_SCRIPT_PYTHON_WRAP_ID
Definition: tool.h:129
CSG_Tool_Library_Manager::Add_Directory
int Add_Directory(const CSG_String &Directory, bool bOnlySubDirectories=false)
Definition: tool_library.cpp:394
CSG_Tool_Library_Manager::Get_Tool_Count
int Get_Tool_Count(void) const
Definition: tool_library.cpp:320
SG_UI_Get_Window_Main
void * SG_UI_Get_Window_Main(void)
Definition: api_callback.cpp:784
CSG_Tool_Library_Manager::Get_Library
CSG_Tool_Library * Get_Library(int i) const
Definition: tool_library.h:207
SG_File_Make_Path
SAGA_API_DLL_EXPORT CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name)
Definition: api_file.cpp:919
SG_UI_ProgressAndMsg_Lock
void SG_UI_ProgressAndMsg_Lock(bool bOn)
Definition: api_callback.cpp:590
CSG_String::c_str
const SG_Char * c_str(void) const
Definition: api_string.cpp:236
CSG_File::Write
size_t Write(void *Buffer, size_t Size, size_t Count=1) const
Definition: api_file.cpp:360
CSG_Strings::Get_Count
int Get_Count(void) const
Definition: api_core.h:712
SG_Realloc
SAGA_API_DLL_EXPORT void * SG_Realloc(void *memblock, size_t size)
Definition: api_memory.cpp:77
SG_UI_Msg_Add_Error
void SG_UI_Msg_Add_Error(const char *Message)
Definition: api_callback.cpp:557
CSG_Tool_Library_Interface::Create_Tool
CSG_Tool * Create_Tool(int i, bool bWithGUI=false)
Definition: tool_library_interface.cpp:196
CSG_Tool::Get_Type
virtual TSG_Tool_Type Get_Type(void) const
Definition: tool.h:163
SG_FILE_ENCODING_UTF8
@ SG_FILE_ENCODING_UTF8
Definition: api_core.h:552
CSG_Tool_Library::Get_Category
CSG_String Get_Category(void) const
Definition: tool_library.h:125
CSG_Tool_Library::m_File_Name
CSG_String m_File_Name
Definition: tool_library.h:161
CSG_File::Seek_End
bool Seek_End(void) const
Definition: api_file.cpp:258
CSG_Tool_Library_Manager
Definition: tool_library.h:186
SG_UI_Msg_Add
void SG_UI_Msg_Add(const char *Message, bool bNewLine, TSG_UI_MSG_STYLE Style)
Definition: api_callback.cpp:503