SAGA API v9.10
Loading...
Searching...
No Matches
api_file.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// api_file.cpp //
15// //
16// Copyright (C) 2005 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/utils.h>
54#include <wx/filename.h>
55#include <wx/dir.h>
56#include <wx/wxcrtvararg.h>
57#include <wx/wfstream.h>
58#include <wx/txtstrm.h>
59#include <wx/zipstrm.h>
60#include <wx/tarstrm.h>
61#include <wx/zstream.h>
62#include <wx/log.h>
63#include <wx/version.h>
64
65#include "api_core.h"
66
67
69// //
70// //
71// //
73
74//---------------------------------------------------------
75#define m_pStream_Base ((wxStreamBase *)m_pStream)
76#define m_pStream_I ((wxFFileInputStream *)m_pStream)
77#define m_pStream_O ((wxFFileOutputStream *)m_pStream)
78#define m_pStream_IO ((wxFFileStream *)m_pStream)
79
80
82// //
83// //
84// //
86
87//---------------------------------------------------------
92
93//---------------------------------------------------------
94CSG_File::CSG_File(const SG_Char *FileName, int Mode, bool bBinary, int Encoding)
95{
97
98 Open(FileName, Mode, bBinary, Encoding);
99}
100
101//---------------------------------------------------------
103{
104 Close();
105}
106
107
109// //
111
112//---------------------------------------------------------
113bool CSG_File::Open(const SG_Char *_FileName, int Mode, bool bBinary, int Encoding)
114{
115 Close();
116
117 if( !_FileName ) { return( false ); } CSG_String FileName(_FileName);
118
119 CSG_String Path(SG_File_Get_Path(FileName));
120
121 if( !Path.is_Empty() && !SG_Dir_Exists(Path) )
122 {
123 return( false );
124 }
125
126 if( Mode == SG_FILE_R && !SG_File_Exists(FileName) )
127 {
128 return( false );
129 }
130
131 m_FileName = FileName; m_Mode = Mode;
132
133 Set_Encoding(Encoding);
134
135 switch( m_Mode )
136 {
137 case SG_FILE_W:
138 m_pStream = new wxFFileOutputStream(FileName.c_str(), bBinary ? "wb" : "w");
139 break;
140
141 case SG_FILE_R:
142 m_pStream = new wxFFileInputStream (FileName.c_str(), bBinary ? "rb" : "r");
143 break;
144
145 default: // SG_FILE_RW
146 m_pStream = new wxFFileStream (FileName.c_str(), SG_File_Exists(FileName)
147 ? (bBinary ? "r+b" : "r+")
148 : (bBinary ? "w+b" : "w+")
149 );
150 break;
151 }
152
153 if( !m_pStream || !m_pStream_Base->IsOk() )
154 {
155 Close();
156
157 return( false );
158 }
159
160 return( true );
161}
162
163//---------------------------------------------------------
165{
166 if( m_pStream )
167 {
168 delete(m_pStream_Base);
169
170 m_pStream = NULL;
171 }
172
174
175 return( true );
176}
177
178
180// //
182
183//---------------------------------------------------------
185{
186 m_pStream = NULL;
187 m_pConvert = NULL;
189}
190
191//---------------------------------------------------------
192bool CSG_File::Set_Encoding(int Encoding)
193{
194 if( m_pConvert )
195 {
196 if( m_pConvert != &wxConvLocal
197 && m_pConvert != &wxConvLibc
198 && m_pConvert != &wxConvUTF7
199 && m_pConvert != &wxConvUTF8 )
200 {
201 delete((wxMBConv *)m_pConvert);
202 }
203
204 m_pConvert = NULL;
205 }
206
207 m_Encoding = Encoding;
208
209 switch( Encoding )
210 {
211 case SG_FILE_ENCODING_ANSI : break;
212 case SG_FILE_ENCODING_UTF7 : m_pConvert = &wxConvUTF7 ; break;
213 case SG_FILE_ENCODING_UTF8 : m_pConvert = &wxConvUTF8 ; break;
214 case SG_FILE_ENCODING_UTF16LE: m_pConvert = new wxMBConvUTF16LE(); break;
215 case SG_FILE_ENCODING_UTF16BE: m_pConvert = new wxMBConvUTF16BE(); break;
216 case SG_FILE_ENCODING_UTF32LE: m_pConvert = new wxMBConvUTF32LE(); break;
217 case SG_FILE_ENCODING_UTF32BE: m_pConvert = new wxMBConvUTF32BE(); break;
218 default : break;
219 }
220
221 return( true );
222}
223
224
226// //
228
229//---------------------------------------------------------
231{
232 return( m_pStream ? m_pStream_Base->GetLength() : -1 );
233}
234
235//---------------------------------------------------------
236bool CSG_File::is_EOF(void) const
237{
238 return( is_Reading() && (m_Mode == SG_FILE_R ? m_pStream_I->Eof() : m_pStream_IO->Eof()) );
239}
240
241//---------------------------------------------------------
242bool CSG_File::Seek(sLong Offset, int Origin) const
243{
244 if( m_pStream )
245 {
246 wxSeekMode Seek = Origin == SG_FILE_CURRENT ? wxFromCurrent : Origin == SG_FILE_END ? wxFromEnd : wxFromStart;
247
248 switch( m_Mode )
249 {
250 case SG_FILE_R : return( m_pStream_I ->SeekI(Offset, Seek) != wxInvalidOffset );
251 case SG_FILE_W : return( m_pStream_O ->SeekO(Offset, Seek) != wxInvalidOffset );
252 default : return( m_pStream_IO->SeekI(Offset, Seek) != wxInvalidOffset
253 && m_pStream_IO->SeekO(Offset, Seek) != wxInvalidOffset );
254 }
255 }
256
257 return( false );
258}
259
260//---------------------------------------------------------
261bool CSG_File::Seek_Start(void) const { return( Seek(0, SEEK_SET) ); }
262bool CSG_File::Seek_End (void) const { return( Seek(0, SEEK_END) ); }
263
264//---------------------------------------------------------
266{
267 if( m_pStream )
268 {
269 switch( m_Mode )
270 {
271 case SG_FILE_R : return( m_pStream_I ->TellI() );
272 case SG_FILE_W : return( m_pStream_O ->TellO() );
273 default : return( m_pStream_IO->TellI() );
274 }
275 }
276
277 return( -1 );
278}
279
280//---------------------------------------------------------
282{
283 return( m_pStream_O && m_pStream_O->GetFile()->Flush() );
284}
285
286//---------------------------------------------------------
287int CSG_File::Printf(const char *Format, ...)
288{
289 if( !is_Writing() )
290 {
291 return( 0 );
292 }
293
294 wxString String;
295
296#ifdef _SAGA_LINUX
297 wxString _Format(Format); _Format.Replace("%s", "%ls"); // workaround as we only use wide characters since wx 2.9.4 so interpret strings as multibyte
298 va_list argptr; va_start(argptr, _Format);
299 int Result = String.PrintfV(_Format, argptr);
300#else
301 va_list argptr; va_start(argptr, Format);
302 int Result = String.PrintfV(Format, argptr);
303#endif
304 va_end(argptr);
305
306 Write(&String);
307
308 return( Result );
309}
310
311//---------------------------------------------------------
312int CSG_File::Printf(const wchar_t *Format, ...)
313{
314 if( !is_Writing() )
315 {
316 return( 0 );
317 }
318
319 wxString String;
320
321#ifdef _SAGA_LINUX
322 wxString _Format(Format); _Format.Replace("%s", "%ls"); // workaround as we only use wide characters since wx 2.9.4 so interpret strings as multibyte
323 va_list argptr; va_start(argptr, _Format);
324 int Result = String.PrintfV(_Format, argptr);
325#else
326 va_list argptr; va_start(argptr, Format);
327 int Result = String.PrintfV(Format, argptr);
328#endif
329
330 va_end(argptr);
331
332 Write(&String);
333
334 return( Result );
335}
336
337//---------------------------------------------------------
338size_t CSG_File::Read(void *Buffer, size_t Size, size_t Count) const
339{
340 return( !is_Reading() || Size == 0 || Count == 0 ? 0 : m_Mode == SG_FILE_R
341 ? m_pStream_I ->Read(Buffer, Size * Count).LastRead() / Size
342 : m_pStream_IO->Read(Buffer, Size * Count).LastRead() / Size
343 );
344}
345
346size_t CSG_File::Read(CSG_String &Buffer, size_t Size) const
347{
348 if( is_Reading() && Size > 0 )
349 {
350 CSG_Buffer s(Size + 1);
351
352 size_t i = Read(s.Get_Data(), sizeof(char), Size);
353
354 if( i > 0 )
355 {
356 s[Size] = '\0';
357
358 Buffer = s.Get_Data();
359
360 return( i );
361 }
362 }
363
364 Buffer.Clear();
365
366 return( 0 );
367}
368
369//---------------------------------------------------------
370size_t CSG_File::Write(void *Buffer, size_t Size, size_t Count) const
371{
372 return( !is_Writing() || Size == 0 || Count == 0 ? 0 : m_Mode == SG_FILE_W
373 ? m_pStream_O ->Write(Buffer, Size * Count).LastWrite()
374 : m_pStream_IO->Write(Buffer, Size * Count).LastWrite()
375 );
376}
377
378size_t CSG_File::Write(const CSG_String &Buffer) const
379{
380 if( m_pConvert )
381 {
382 wxString _Buffer(Buffer.w_str());
383
384 const wxScopedCharBuffer s(_Buffer.mb_str(*((wxMBConv *)m_pConvert)));
385
386 return( Write((void *)s.data(), sizeof(char), s.length()) );
387 }
388
389 CSG_String s(Buffer);
390
391 return( s.Length() > 0 ? Write((void *)s.b_str(), sizeof(char), s.Length()) : 0 );
392
393// CSG_Buffer s(Buffer.to_ASCII()); // returns NULL terminated char sequence, Get_Size() count includes terminating NULL!!!
394
395// return( s.Get_Size() > 1 ? Write((void *)s.Get_Data(), sizeof(char), s.Get_Size() - 1) : 0 );
396}
397
398//---------------------------------------------------------
400{
401 if( !is_Reading() || is_EOF() )
402 {
403 return( false );
404 }
405
406 wxString s;
407
408 if( m_pConvert )
409 {
410 if( m_Mode == SG_FILE_R )
411 {
412 wxTextInputStream Stream(*m_pStream_I , " \t", *((wxMBConv *)m_pConvert)); s = Stream.ReadLine();
413 }
414 else
415 {
416 wxTextInputStream Stream(*m_pStream_IO, " \t", *((wxMBConv *)m_pConvert)); s = Stream.ReadLine();
417 }
418 }
419 else
420 {
421 if( m_Mode == SG_FILE_R )
422 {
423 wxTextInputStream Stream(*m_pStream_I , " \t" ); s = Stream.ReadLine();
424 }
425 else
426 {
427 wxTextInputStream Stream(*m_pStream_IO, " \t" ); s = Stream.ReadLine();
428 }
429 }
430
431 sLine = CSG_String(&s);
432
433 return( !sLine.is_Empty() || !is_EOF() );
434}
435
436//---------------------------------------------------------
437int CSG_File::Read_Char(void) const
438{
439 return( !is_Reading() ? 0 : m_Mode == SG_FILE_R ? m_pStream_I->GetC() : m_pStream_IO->GetC() );
440}
441
442//---------------------------------------------------------
443int CSG_File::Read_Int(bool bByteOrderBig) const
444{
445 int Value = 0;
446
447 if( Read(&Value, sizeof(Value)) == 1 )
448 {
449 if( bByteOrderBig )
450 {
451 SG_Swap_Bytes(&Value, sizeof(Value));
452 }
453 }
454
455 return( Value );
456}
457
458bool CSG_File::Write_Int(int Value, bool bByteOrderBig)
459{
460 if( bByteOrderBig )
461 {
462 SG_Swap_Bytes(&Value, sizeof(Value));
463 }
464
465 return( Write(&Value, sizeof(Value)) == sizeof(Value) );
466}
467
468//---------------------------------------------------------
469double CSG_File::Read_Double(bool bByteOrderBig) const
470{
471 double Value = 0;
472
473 if( Read(&Value, sizeof(Value)) == 1 )
474 {
475 if( bByteOrderBig )
476 {
477 SG_Swap_Bytes(&Value, sizeof(Value));
478 }
479 }
480
481 return( Value );
482}
483
484bool CSG_File::Write_Double(double Value, bool bByteOrderBig)
485{
486 if( bByteOrderBig )
487 {
488 SG_Swap_Bytes(&Value, sizeof(Value));
489 }
490
491 return( Write(&Value, sizeof(Value)) == sizeof(Value) );
492}
493
494//---------------------------------------------------------
495bool CSG_File::Scan(int &Value) const
496{
497 if( is_Reading() )
498 {
499 int c; while( !is_EOF() && isspace(c = Read_Char()) ); // remove leading white space
500
501 if( isdigit(c) || strchr("-+", c) )
502 {
503 CSG_String s = (char)c;
504
505 while( !is_EOF() && isdigit(c = Read_Char()) )
506 {
507 s += (char)c;
508 }
509
510 return( s.asInt(Value) );
511 }
512 }
513
514 return( false );
515}
516
517bool CSG_File::Scan(double &Value) const
518{
519 if( is_Reading() )
520 {
521 int c; while( !is_EOF() && isspace(c = Read_Char()) ); // remove leading white space
522
523 if( isdigit(c) || strchr("-+.,eE", c) )
524 {
525 CSG_String s = (char)c;
526
527 while( !is_EOF() && (isdigit(c = Read_Char()) || strchr(".,eE", c) || strchr("", c)) )
528 {
529 s += (char)c;
530 }
531
532 return( s.asDouble(Value) );
533 }
534 }
535
536 return( false );
537}
538
539bool CSG_File::Scan(CSG_String &Value, SG_Char Separator) const
540{
541 if( is_Reading() && !is_EOF() )
542 {
543 Value.Clear();
544
545 int c; while( !is_EOF() && (c = Read_Char()) != Separator && c != EOF )
546 {
547 Value += (char)c;
548 }
549
550 return( true );
551 }
552
553 return( false );
554}
555
556//---------------------------------------------------------
557int CSG_File::Scan_Int(void) const
558{
559 int Value; return( Scan(Value) ? Value : 0 );
560}
561
562double CSG_File::Scan_Double(void) const
563{
564 double Value; return( Scan(Value) ? Value : 0.0 );
565}
566
568{
569 CSG_String Value; Scan(Value, Separator); return( Value );
570}
571
572
574// //
575// //
576// //
578
579//---------------------------------------------------------
584
585//---------------------------------------------------------
586CSG_Archive::CSG_Archive(const SG_Char *FileName, int Mode, int Encoding)
587{
589
590 Open(FileName, Mode, Encoding);
591}
592
593//---------------------------------------------------------
595{
596 Close();
597}
598
599//---------------------------------------------------------
600bool CSG_Archive::Open(const SG_Char *_FileName, int Mode, int Encoding)
601{
602 Close();
603
604 if( !_FileName ) { return( false ); } CSG_String FileName(_FileName);
605
606 if( SG_File_Cmp_Extension(FileName, "tar") )
607 {
609 }
610 else // if( SG_File_Cmp_Extension(FileName, "zip") )
611 {
613 }
614
615 m_Archive = FileName;
616
618
619 if( !Path.is_Empty() && !SG_Dir_Exists(Path) )
620 {
621 return( false );
622 }
623
624 if( Mode == SG_FILE_R && !SG_File_Exists(m_Archive) )
625 {
626 return( false );
627 }
628
629 wxLogNull logNo; // suppress user notification dialog for invalid zip files
630
631 m_Mode = Mode; Set_Encoding(Encoding);
632
633 if( Mode == SG_FILE_W )
634 {
635 if( is_Zip() )
636 {
637 m_pStream = new wxZipOutputStream(new wxFileOutputStream(m_Archive.c_str()));
638 }
639 else
640 {
641 m_pStream = new wxTarOutputStream(new wxFileOutputStream(m_Archive.c_str()));
642 }
643 }
644 else if( Mode == SG_FILE_R && SG_File_Exists(m_Archive) )
645 {
646 if( is_Zip() )
647 {
648 m_pStream = new wxZipInputStream (new wxFileInputStream (m_Archive.c_str()));
649 }
650 else
651 {
652 m_pStream = new wxTarInputStream (new wxFileInputStream (m_Archive.c_str()));
653 }
654 }
655
656 if( !m_pStream || !m_pStream_Base->IsOk() )
657 {
658 Close();
659
660 return( false );
661 }
662
663 if( is_Reading() )
664 {
665 wxArchiveEntry *pEntry;
666
667 while( (pEntry = ((wxArchiveInputStream *)m_pStream)->GetNextEntry()) != NULL )
668 {
669 m_Files += pEntry;
670 }
671 }
672
673 return( true );
674}
675
676//---------------------------------------------------------
678{
679 for(sLong i=0; i<m_Files.Get_Size(); i++)
680 {
681 if( is_Zip() )
682 {
683 delete((wxZipEntry *)m_Files[i]);
684 }
685 else
686 {
687 delete((wxTarEntry *)m_Files[i]);
688 }
689 }
690
691 m_Files.Set_Array(0);
692
693 m_Archive.Clear();
694
695 return( CSG_File::Close() );
696}
697
698//---------------------------------------------------------
700{
701 return( is_Writing() && Name && ((wxArchiveOutputStream *)m_pStream)->PutNextDirEntry(Name) );
702}
703
704//---------------------------------------------------------
705bool CSG_Archive::Add_File(const SG_Char *Name, bool bBinary)
706{
707 if( is_Writing() && Name )
708 {
709 wxArchiveEntry *pEntry = NULL;
710
711 if( is_Zip() )
712 {
713 pEntry = new wxZipEntry(Name);
714
715 ((wxZipEntry *)pEntry)->SetIsText(bBinary == false);
716
717 #if wxCHECK_VERSION(3, 1, 1)
718 ((wxZipOutputStream *)m_pStream)->SetFormat(wxZIP_FORMAT_ZIP64);
719 #endif
720 }
721 else
722 {
723 pEntry = new wxTarEntry(Name);
724 }
725
726 if( ((wxArchiveOutputStream *)m_pStream)->PutNextEntry(pEntry) )
727 {
728 m_FileName = Name;
729
730 return( true );
731 }
732 }
733
734 return( false );
735}
736
737//---------------------------------------------------------
739{
740 if( is_Reading() && m_Files[Index] )
741 {
742 return( ((wxArchiveEntry *)m_Files[Index])->IsDir() );
743 }
744
745 return( false );
746}
747
748//---------------------------------------------------------
749bool CSG_Archive::Get_File(size_t Index)
750{
751 if( is_Reading() && m_Files[Index] )
752 {
753 if( ((wxArchiveInputStream *)m_pStream)->OpenEntry(*(wxArchiveEntry *)m_Files[Index]) )
754 {
755 m_FileName = Get_File_Name(Index);
756
757 return( true );
758 }
759 }
760
761 return( false );
762}
763
764//---------------------------------------------------------
766{
767 if( is_Reading() && Name )
768 {
769 for(sLong i=0; i<m_Files.Get_Size(); i++)
770 {
771 if( !((wxArchiveEntry *)m_Files[i])->GetName().Cmp(Name) )
772 {
773 return( Get_File(i) );
774 }
775 }
776 }
777
778 return( false );
779}
780
781//---------------------------------------------------------
783{
784 CSG_String s;
785
786 if( is_Reading() && m_Files[Index] )
787 {
788 wxString Name(((wxArchiveEntry *)m_Files[Index])->GetName()); s = &Name;
789 }
790
791 return( s );
792}
793
794
796// //
798
799//---------------------------------------------------------
800bool CSG_Archive::Extract_All(const SG_Char *_Directory)
801{
802 if( !is_Reading() )
803 {
804 return( false );
805 }
806
807 #ifdef _SAGA_MSW
808 const char Separator = '\\';
809 #else
810 const char Separator = '/';
811 #endif
812
813 CSG_String Directory(_Directory ? _Directory : SG_T(""));
814
815 if( Directory.is_Empty() )
816 {
817 Directory = SG_File_Get_Path(m_Archive);
818 }
819 else if( !SG_Dir_Exists(Directory) )
820 {
821 SG_Dir_Create(Directory, true);
822 }
823
824 Directory += Separator;
825
826 //-----------------------------------------------------
827 for(size_t i=0; i<Get_File_Count() && SG_UI_Process_Set_Progress((int)i, (int)Get_File_Count()); i++)
828 {
829 if( is_Directory(i) )
830 {
831 SG_Dir_Create(Directory + Get_File_Name(i));
832 }
833 else
834 {
835 Extract(Get_File_Name(i), Directory + Get_File_Name(i));
836 }
837 }
838
839 return( true );
840}
841
842//---------------------------------------------------------
843bool CSG_Archive::Extract(const SG_Char *File, const SG_Char *_toFile)
844{
845 if( !is_Reading() || !Get_File(File) )
846 {
847 return( false );
848 }
849
850 CSG_String toFile(_toFile ? _toFile : SG_T(""));
851
852 if( toFile.is_Empty() )
853 {
855 }
856
857 CSG_File Stream(toFile, SG_FILE_W, true, m_Encoding);
858
859 if( !Stream.is_Open() )
860 {
861 return( false );
862 }
863
864 //-----------------------------------------------------
865 #define UNZIP_BUFFER 4096
866
867 char *Buffer[UNZIP_BUFFER];
868
869 while( !is_EOF() )
870 {
871 size_t nBytes = Read(Buffer, sizeof(char), UNZIP_BUFFER);
872
873 Stream.Write(Buffer, 1, nBytes); Stream.Flush();
874 }
875
876 return( true );
877}
878
879
881// //
882// //
883// //
885
886//---------------------------------------------------------
888{
889 // nop
890}
891
892//---------------------------------------------------------
894{
895 return( wxZlibInputStream::CanHandleGZip() );
896}
897
898//---------------------------------------------------------
900{
901 if( SG_File_Exists(File) )
902 {
903 wxFFileInputStream Input(File.c_str());
904
905 if( Input.IsOk() && Input.CanRead() )
906 {
907 CSG_String Target(_Target);
908
909 if( Target.is_Empty() )
910 {
911 Target = File + ".gz";
912 }
913
914 wxZlibOutputStream Output(new wxFFileOutputStream(Target.c_str()));
915
916 if( Output.IsOk() )
917 {
918 Output.Write(Input);
919
920 return( Target );
921 }
922 }
923 }
924
925 return( "" );
926}
927
928//---------------------------------------------------------
930{
931 if( SG_File_Exists(File) )
932 {
933 wxZlibInputStream Input(new wxFFileInputStream(File.c_str()));
934
935 if( Input.IsOk() && Input.CanRead() )
936 {
937 CSG_String Target(_Target);
938
939 if( Target.is_Empty() )
940 {
941 Target = SG_File_Make_Path(SG_File_Get_Path(File), SG_File_Get_Name(File, false), "");
942 }
943
944 wxFFileOutputStream Output(Target.c_str());
945
946 if( Output.IsOk() )
947 {
948 Output.Write(Input);
949
950 return( Target );
951 }
952 }
953 }
954
955 return( "" );
956}
957
958
960// //
961// //
962// //
964
965//---------------------------------------------------------
966bool SG_Dir_Exists(const CSG_String &Directory)
967{
968 return( wxFileName::DirExists(Directory.c_str()) );
969}
970
971//---------------------------------------------------------
972bool SG_Dir_Create(const CSG_String &Directory, bool bFullPath)
973{
974 if( SG_Dir_Exists(Directory) )
975 {
976 return( true );
977 }
978
979 return( wxFileName::Mkdir(Directory.c_str(), wxS_DIR_DEFAULT, bFullPath ? wxPATH_MKDIR_FULL : 0) );
980}
981
982//---------------------------------------------------------
983bool SG_Dir_Delete(const CSG_String &Directory, bool bRecursive)
984{
985 if( !SG_Dir_Exists(Directory) )
986 {
987 return( true );
988 }
989
990 return( wxDir::Remove(Directory.c_str(), bRecursive ? wxPATH_RMDIR_RECURSIVE : 0) );
991}
992
993//---------------------------------------------------------
995{
996 wxString cwd = wxFileName::GetCwd();
997
998 return( CSG_String(&cwd) );
999}
1000
1001//---------------------------------------------------------
1003{
1004 wxString fname = wxFileName::GetTempDir();
1005
1006 return( CSG_String(&fname) );
1007}
1008
1009//---------------------------------------------------------
1010bool SG_Dir_List_Subdirectories (CSG_Strings &List, const CSG_String &Directory, bool bRecursive)
1011{
1012 List.Clear(); wxDir Dir;
1013
1014 if( Dir.Open(Directory.c_str()) )
1015 {
1016 wxString FileName;
1017
1018 if( Dir.GetFirst(&FileName, wxEmptyString, wxDIR_DIRS) )
1019 {
1020 do
1021 {
1022 List += SG_File_Make_Path(Directory, &FileName);
1023
1024 if( bRecursive )
1025 {
1026 CSG_Strings _List; SG_Dir_List_Subdirectories(_List, SG_File_Make_Path(Directory, &FileName), bRecursive); List += _List;
1027 }
1028 }
1029 while( Dir.GetNext(&FileName) );
1030 }
1031 }
1032
1033 return( List.Get_Count() > 0 );
1034}
1035
1036//---------------------------------------------------------
1037bool SG_Dir_List_Files (CSG_Strings &List, const CSG_String &Directory, const CSG_String &Extension, bool bRecursive)
1038{
1039 List.Clear(); wxDir Dir;
1040
1041 if( Dir.Open(Directory.c_str()) )
1042 {
1043 wxString FileName;
1044
1045 if( Dir.GetFirst(&FileName, wxEmptyString, wxDIR_FILES) )
1046 {
1047 do
1048 {
1049 if( Extension.is_Empty() || SG_File_Cmp_Extension(&FileName, Extension) )
1050 {
1051 List += SG_File_Make_Path(Directory, &FileName);
1052 }
1053 }
1054 while( Dir.GetNext(&FileName) );
1055 }
1056
1057 if( bRecursive && Dir.GetFirst(&FileName, wxEmptyString, wxDIR_DIRS) )
1058 {
1059 do
1060 {
1061 CSG_Strings _List; SG_Dir_List_Files(_List, SG_File_Make_Path(Directory, &FileName), Extension, bRecursive); List += _List;
1062 }
1063 while( Dir.GetNext(&FileName) );
1064 }
1065 }
1066
1067 return( List.Get_Count() > 0 );
1068}
1069
1070
1072// //
1073// //
1074// //
1076
1077//---------------------------------------------------------
1078bool SG_File_Exists(const CSG_String &FileName)
1079{
1080 return( wxFileExists(FileName.c_str()) );
1081}
1082
1083//---------------------------------------------------------
1084bool SG_File_Delete(const CSG_String &FileName)
1085{
1086 return( SG_File_Exists(FileName) && wxRemoveFile(FileName.c_str()) );
1087}
1088
1089//---------------------------------------------------------
1091{
1092 return( SG_File_Get_Name_Temp(Prefix, "") );
1093}
1094
1096{
1097 if( !SG_Dir_Exists(Directory) )
1098 {
1099 return( CSG_String(wxFileName::CreateTempFileName(Prefix.c_str()).wc_str()) );
1100 }
1101
1102 return( CSG_String(wxFileName::CreateTempFileName(SG_File_Make_Path(Directory, Prefix).w_str()).wc_str()) );
1103}
1104
1105//---------------------------------------------------------
1106CSG_String SG_File_Get_Name(const CSG_String &full_Path, bool bExtension)
1107{
1108 wxFileName fn(full_Path.c_str());
1109
1110 if( bExtension )
1111 {
1112 wxString s(fn.GetFullName()); return( CSG_String(&s) );
1113 }
1114
1115 wxString s(fn.GetName()); return( &s );
1116}
1117
1118//---------------------------------------------------------
1120{
1121 wxString s(wxFileName(full_Path.c_str()).GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR));
1122
1123 return( CSG_String(&s) );
1124}
1125
1126//---------------------------------------------------------
1128{
1129 wxFileName fn(full_Path.c_str());
1130
1131 fn.MakeAbsolute();
1132
1133 wxString s(fn.GetFullPath()); return( &s );
1134}
1135
1136//---------------------------------------------------------
1138{
1139 wxFileName fn(full_Path.c_str());
1140
1141 fn.MakeRelativeTo(Directory.c_str());
1142
1143 wxString s(fn.GetFullPath()); return( &s );
1144}
1145
1146//---------------------------------------------------------
1148{
1149 return( SG_File_Make_Path(Directory, Name, "") );
1150}
1151
1152CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name, const CSG_String &Extension)
1153{
1154 wxFileName fn;
1155
1156 fn.AssignDir(!Directory.is_Empty() ? Directory.c_str() : SG_File_Get_Path(Name).c_str());
1157
1158 if( !Extension.is_Empty() )
1159 {
1160 fn.SetName (SG_File_Get_Name(Name, false).c_str());
1161 fn.SetExt (Extension.c_str());
1162 }
1163 else
1164 {
1165 fn.SetFullName (SG_File_Get_Name(Name, true).c_str());
1166 }
1167
1168 wxString s(fn.GetFullPath()); return( &s );
1169}
1170
1171//---------------------------------------------------------
1172bool SG_File_Cmp_Path(const CSG_String &Path1, const CSG_String &Path2)
1173{
1174 wxFileName a(wxString(Path1.c_str())), b(wxString(Path2.c_str()));
1175
1176 return( a.SameAs(b) );
1177}
1178
1179//---------------------------------------------------------
1180bool SG_File_Cmp_Extension(const CSG_String &FileName, const CSG_String &Extension)
1181{
1182 return( SG_File_Get_Extension(FileName).CmpNoCase(Extension) == 0 );
1183}
1184
1185//---------------------------------------------------------
1186bool SG_File_Set_Extension(CSG_String &FileName, const CSG_String &Extension)
1187{
1188 if( FileName.Length() > 0 )
1189 {
1190 wxFileName fn(FileName.c_str());
1191
1192 fn.SetExt(Extension.c_str());
1193
1194 wxString s(fn.GetFullPath());
1195
1196 FileName = &s;
1197
1198 return( true );
1199 }
1200
1201 return( false );
1202}
1203
1204//---------------------------------------------------------
1206{
1207 wxFileName fn(FileName.c_str());
1208
1209 wxString s(fn.GetExt()); return( &s );
1210}
1211
1212
1214// //
1215// //
1216// //
1218
1219//---------------------------------------------------------
1220bool SG_Get_Environment(const CSG_String &Variable, CSG_String *Value)
1221{
1222 if( Value == NULL )
1223 {
1224 return( wxGetEnv(Variable.w_str(), NULL) );
1225 }
1226
1227 wxString s;
1228
1229 if( wxGetEnv(Variable.w_str(), &s) )
1230 {
1231 *Value = s.wc_str();
1232
1233 return( true );
1234 }
1235
1236 return( false );
1237}
1238
1239//---------------------------------------------------------
1240bool SG_Set_Environment(const CSG_String &Variable, const CSG_String &Value)
1241{
1242 return( wxSetEnv(Variable.w_str(), Value.w_str()) );
1243}
1244
1245
1247// //
1248// //
1249// //
1251
1252//---------------------------------------------------------
SAGA_API_DLL_EXPORT bool SG_File_Exists(const CSG_String &FileName)
SAGA_API_DLL_EXPORT bool SG_File_Cmp_Extension(const CSG_String &File, const CSG_String &Extension)
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:972
#define SG_T(s)
Definition api_core.h:537
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_UI_Process_Set_Progress(int Position, int Range)
SAGA_API_DLL_EXPORT CSG_String SG_File_Get_Name(const CSG_String &full_Path, bool bExtension)
SAGA_API_DLL_EXPORT bool SG_Dir_Exists(const CSG_String &Directory)
Definition api_file.cpp:966
@ SG_FILE_TYPE_TAR
Definition api_core.h:1107
@ SG_FILE_TYPE_ZIP
Definition api_core.h:1106
@ SG_FILE_CURRENT
Definition api_core.h:1123
@ SG_FILE_END
Definition api_core.h:1124
#define SG_Char
Definition api_core.h:536
@ SG_FILE_ENCODING_UTF16LE
Definition api_core.h:553
@ SG_FILE_ENCODING_UTF16BE
Definition api_core.h:554
@ SG_FILE_ENCODING_UTF8
Definition api_core.h:552
@ SG_FILE_ENCODING_UTF7
Definition api_core.h:551
@ SG_FILE_ENCODING_UNDEFINED
Definition api_core.h:557
@ SG_FILE_ENCODING_ANSI
Definition api_core.h:550
@ SG_FILE_ENCODING_UTF32BE
Definition api_core.h:556
@ SG_FILE_ENCODING_UTF32LE
Definition api_core.h:555
SAGA_API_DLL_EXPORT void SG_Swap_Bytes(void *Buffer, int nBytes)
@ SG_FILE_W
Definition api_core.h:1115
@ SG_FILE_R
Definition api_core.h:1114
CSG_String SG_Dir_Get_Temp(void)
CSG_String SG_File_Get_Name_Temp(const CSG_String &Prefix)
bool SG_File_Cmp_Path(const CSG_String &Path1, const CSG_String &Path2)
bool SG_File_Exists(const CSG_String &FileName)
bool SG_Dir_Create(const CSG_String &Directory, bool bFullPath)
Definition api_file.cpp:972
bool SG_Dir_List_Subdirectories(CSG_Strings &List, const CSG_String &Directory, bool bRecursive)
CSG_String SG_Dir_Get_Current(void)
Definition api_file.cpp:994
#define m_pStream_I
Definition api_file.cpp:76
#define m_pStream_O
Definition api_file.cpp:77
CSG_String SG_File_Get_Path_Absolute(const CSG_String &full_Path)
CSG_String SG_File_Get_Extension(const CSG_String &FileName)
#define m_pStream_IO
Definition api_file.cpp:78
bool SG_File_Set_Extension(CSG_String &FileName, const CSG_String &Extension)
bool SG_Set_Environment(const CSG_String &Variable, const CSG_String &Value)
bool SG_Dir_Delete(const CSG_String &Directory, bool bRecursive)
Definition api_file.cpp:983
bool SG_Dir_Exists(const CSG_String &Directory)
Definition api_file.cpp:966
bool SG_File_Cmp_Extension(const CSG_String &FileName, const CSG_String &Extension)
bool SG_File_Delete(const CSG_String &FileName)
CSG_String SG_File_Get_Path(const CSG_String &full_Path)
CSG_String SG_File_Get_Path_Relative(const CSG_String &Directory, const CSG_String &full_Path)
#define m_pStream_Base
Definition api_file.cpp:75
CSG_String SG_File_Get_Name(const CSG_String &full_Path, bool bExtension)
bool SG_Get_Environment(const CSG_String &Variable, CSG_String *Value)
#define UNZIP_BUFFER
bool SG_Dir_List_Files(CSG_Strings &List, const CSG_String &Directory, const CSG_String &Extension, bool bRecursive)
CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name)
bool Add_Directory(const SG_Char *Name)
Definition api_file.cpp:699
bool is_Zip(void) const
Definition api_core.h:1218
bool Add_File(const SG_Char *Name, bool bBinary=true)
Definition api_file.cpp:705
TSG_File_Type m_Type
Definition api_core.h:1236
bool is_Directory(size_t Index)
Definition api_file.cpp:738
bool Extract_All(const SG_Char *toDirectory=NULL)
Definition api_file.cpp:800
CSG_Archive(void)
Definition api_file.cpp:580
size_t Get_File_Count(void)
Definition api_core.h:1224
bool Get_File(const SG_Char *Name)
Definition api_file.cpp:765
virtual bool Close(void)
Definition api_file.cpp:677
virtual bool Open(const SG_Char *FileName, int Mode=SG_FILE_R, int Encoding=SG_FILE_ENCODING_ANSI)
Definition api_file.cpp:600
CSG_Array_Pointer m_Files
Definition api_core.h:1240
virtual ~CSG_Archive(void)
Definition api_file.cpp:594
bool Extract(const SG_Char *File, const SG_Char *toFile=NULL)
Definition api_file.cpp:843
CSG_String m_Archive
Definition api_core.h:1238
char * Get_Data(int Offset=0) const
Definition api_core.h:244
bool Seek(sLong Offset, int Origin=SG_FILE_START) const
Definition api_file.cpp:242
double Read_Double(bool bBigEndian=false) const
Definition api_file.cpp:469
sLong Length(void) const
Definition api_file.cpp:230
sLong Tell(void) const
Definition api_file.cpp:265
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
CSG_String m_FileName
Definition api_core.h:1193
int Read_Int(bool bBigEndian=false) const
Definition api_file.cpp:443
int m_Mode
Definition api_core.h:1191
bool Write_Int(int Value, bool bBigEndian=false)
Definition api_file.cpp:458
bool Set_Encoding(int Encoding)
Definition api_file.cpp:192
virtual const CSG_String & Get_File_Name(void) const
Definition api_core.h:1140
virtual bool Close(void)
Definition api_file.cpp:164
bool Seek_End(void) const
Definition api_file.cpp:262
bool is_Open(void) const
Definition api_core.h:1149
bool Read_Line(CSG_String &Line) const
Definition api_file.cpp:399
size_t Write(void *Buffer, size_t Size, size_t Count=1) const
Definition api_file.cpp:370
int Scan_Int(void) const
Definition api_file.cpp:557
CSG_File(void)
Definition api_file.cpp:88
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
bool Flush(void)
Definition api_file.cpp:281
bool is_EOF(void) const
Definition api_file.cpp:236
CSG_String Scan_String(SG_Char Separator) const
Definition api_file.cpp:567
void * m_pStream
Definition api_core.h:1195
bool is_Writing(void) const
Definition api_core.h:1151
int Read_Char(void) const
Definition api_file.cpp:437
bool is_Reading(void) const
Definition api_core.h:1150
bool Seek_Start(void) const
Definition api_file.cpp:261
bool Scan(int &Value) const
Definition api_file.cpp:495
bool Write_Double(double Value, bool bBigEndian=false)
Definition api_file.cpp:484
virtual ~CSG_File(void)
Definition api_file.cpp:102
double Scan_Double(void) const
Definition api_file.cpp:562
void * m_pConvert
Definition api_core.h:1195
void On_Construction(void)
Definition api_file.cpp:184
int m_Encoding
Definition api_core.h:1191
size_t Length(void) const
const char * b_str(void) const
void Clear(void)
int asInt(void) const
const SG_Char * c_str(void) const
bool is_Empty(void) const
double asDouble(void) const
const wchar_t * w_str(void) const
int Get_Count(void) const
Definition api_core.h:714
void Clear(void)
Definition api_core.h:736
static CSG_String Uncompress(const CSG_String &File, const CSG_String &Target="")
Definition api_file.cpp:929
static bool is_GZip_Supported(void)
Definition api_file.cpp:893
static CSG_String Compress(const CSG_String &File, const CSG_String &Target="")
Definition api_file.cpp:899
CSG_ZLib(void)
Definition api_file.cpp:887