SAGA API  v9.5
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/zipstrm.h>
59 #include <wx/txtstrm.h>
60 #include <wx/log.h>
61 #include <wx/version.h>
62 
63 #include "api_core.h"
64 
65 
67 // //
68 // //
69 // //
71 
72 //---------------------------------------------------------
73 #define m_pStream_Base ((wxStreamBase *)m_pStream)
74 #define m_pStream_I ((wxFFileInputStream *)m_pStream)
75 #define m_pStream_O ((wxFFileOutputStream *)m_pStream)
76 #define m_pStream_IO ((wxFFileStream *)m_pStream)
77 
78 
80 // //
81 // //
82 // //
84 
85 //---------------------------------------------------------
87 {
89 }
90 
91 //---------------------------------------------------------
92 CSG_File::CSG_File(const CSG_String &FileName, int Mode, bool bBinary, int Encoding)
93 {
95 
96  Open(FileName, Mode, bBinary, Encoding);
97 }
98 
99 //---------------------------------------------------------
101 {
102  Close();
103 }
104 
105 
107 // //
109 
110 //---------------------------------------------------------
111 bool CSG_File::Open(const CSG_String &FileName, int Mode, bool bBinary, int Encoding)
112 {
113  Close();
114 
115  CSG_String Path(SG_File_Get_Path(FileName));
116 
117  if( !Path.is_Empty() && !SG_Dir_Exists(Path) )
118  {
119  return( false );
120  }
121 
122  if( Mode == SG_FILE_R && !SG_File_Exists(FileName) )
123  {
124  return( false );
125  }
126 
127  m_FileName = FileName; m_Mode = Mode;
128 
129  Set_Encoding(Encoding);
130 
131  switch( m_Mode )
132  {
133  case SG_FILE_W:
134  m_pStream = new wxFFileOutputStream(FileName.c_str(), bBinary ? "wb" : "w");
135  break;
136 
137  case SG_FILE_R:
138  m_pStream = new wxFFileInputStream (FileName.c_str(), bBinary ? "rb" : "r");
139  break;
140 
141  default: // SG_FILE_RW
142  m_pStream = new wxFFileStream (FileName.c_str(), SG_File_Exists(FileName)
143  ? (bBinary ? "r+b" : "r+")
144  : (bBinary ? "w+b" : "w+")
145  );
146  break;
147  }
148 
149  if( !m_pStream || !m_pStream_Base->IsOk() )
150  {
151  Close();
152 
153  return( false );
154  }
155 
156  return( true );
157 }
158 
159 //---------------------------------------------------------
160 bool CSG_File::Close(void)
161 {
162  if( m_pStream )
163  {
164  delete(m_pStream_Base);
165 
166  m_pStream = NULL;
167  }
168 
170 
171  return( true );
172 }
173 
174 
176 // //
178 
179 //---------------------------------------------------------
181 {
182  m_pStream = NULL;
183  m_pConvert = NULL;
185 }
186 
187 //---------------------------------------------------------
188 bool CSG_File::Set_Encoding(int Encoding)
189 {
190  if( m_pConvert )
191  {
192  if( m_pConvert != &wxConvLocal
193  && m_pConvert != &wxConvLibc
194  && m_pConvert != &wxConvUTF7
195  && m_pConvert != &wxConvUTF8 )
196  {
197  delete((wxMBConv *)m_pConvert);
198  }
199 
200  m_pConvert = NULL;
201  }
202 
203  m_Encoding = Encoding;
204 
205  switch( Encoding )
206  {
207  case SG_FILE_ENCODING_ANSI : break;
208  case SG_FILE_ENCODING_UTF7 : m_pConvert = &wxConvUTF7 ; break;
209  case SG_FILE_ENCODING_UTF8 : m_pConvert = &wxConvUTF8 ; break;
210  case SG_FILE_ENCODING_UTF16LE: m_pConvert = new wxMBConvUTF16LE(); break;
211  case SG_FILE_ENCODING_UTF16BE: m_pConvert = new wxMBConvUTF16BE(); break;
212  case SG_FILE_ENCODING_UTF32LE: m_pConvert = new wxMBConvUTF32LE(); break;
213  case SG_FILE_ENCODING_UTF32BE: m_pConvert = new wxMBConvUTF32BE(); break;
214  default : break;
215  }
216 
217  return( true );
218 }
219 
220 
222 // //
224 
225 //---------------------------------------------------------
227 {
228  return( m_pStream ? m_pStream_Base->GetLength() : -1 );
229 }
230 
231 //---------------------------------------------------------
232 bool CSG_File::is_EOF(void) const
233 {
234  return( is_Reading() && (m_Mode == SG_FILE_R ? m_pStream_I->Eof() : m_pStream_IO->Eof()) );
235 }
236 
237 //---------------------------------------------------------
238 bool CSG_File::Seek(sLong Offset, int Origin) const
239 {
240  if( m_pStream )
241  {
242  wxSeekMode Seek = Origin == SG_FILE_CURRENT ? wxFromCurrent : Origin == SG_FILE_END ? wxFromEnd : wxFromStart;
243 
244  switch( m_Mode )
245  {
246  case SG_FILE_R : return( m_pStream_I ->SeekI(Offset, Seek) != wxInvalidOffset );
247  case SG_FILE_W : return( m_pStream_O ->SeekO(Offset, Seek) != wxInvalidOffset );
248  default : return( m_pStream_IO->SeekI(Offset, Seek) != wxInvalidOffset
249  && m_pStream_IO->SeekO(Offset, Seek) != wxInvalidOffset );
250  }
251  }
252 
253  return( false );
254 }
255 
256 //---------------------------------------------------------
257 bool CSG_File::Seek_Start(void) const { return( Seek(0, SEEK_SET) ); }
258 bool CSG_File::Seek_End (void) const { return( Seek(0, SEEK_END) ); }
259 
260 //---------------------------------------------------------
262 {
263  if( m_pStream )
264  {
265  switch( m_Mode )
266  {
267  case SG_FILE_R : return( m_pStream_I ->TellI() );
268  case SG_FILE_W : return( m_pStream_O ->TellO() );
269  default : return( m_pStream_IO->TellI() );
270  }
271  }
272 
273  return( -1 );
274 }
275 
276 //---------------------------------------------------------
277 int CSG_File::Printf(const char *Format, ...)
278 {
279  if( !is_Writing() )
280  {
281  return( 0 );
282  }
283 
284  wxString String;
285 
286 #ifdef _SAGA_LINUX
287  wxString _Format(Format); _Format.Replace("%s", "%ls"); // workaround as we only use wide characters since wx 2.9.4 so interpret strings as multibyte
288  va_list argptr; va_start(argptr, _Format);
289  int Result = String.PrintfV(_Format, argptr);
290 #else
291  va_list argptr; va_start(argptr, Format);
292  int Result = String.PrintfV(Format, argptr);
293 #endif
294  va_end(argptr);
295 
296  Write(&String);
297 
298  return( Result );
299 }
300 
301 //---------------------------------------------------------
302 int CSG_File::Printf(const wchar_t *Format, ...)
303 {
304  if( !is_Writing() )
305  {
306  return( 0 );
307  }
308 
309  wxString String;
310 
311 #ifdef _SAGA_LINUX
312  wxString _Format(Format); _Format.Replace("%s", "%ls"); // workaround as we only use wide characters since wx 2.9.4 so interpret strings as multibyte
313  va_list argptr; va_start(argptr, _Format);
314  int Result = String.PrintfV(_Format, argptr);
315 #else
316  va_list argptr; va_start(argptr, Format);
317  int Result = String.PrintfV(Format, argptr);
318 #endif
319 
320  va_end(argptr);
321 
322  Write(&String);
323 
324  return( Result );
325 }
326 
327 //---------------------------------------------------------
328 size_t CSG_File::Read(void *Buffer, size_t Size, size_t Count) const
329 {
330  return( !is_Reading() || Size == 0 || Count == 0 ? 0 : m_Mode == SG_FILE_R
331  ? m_pStream_I ->Read(Buffer, Size * Count).LastRead() / Size
332  : m_pStream_IO->Read(Buffer, Size * Count).LastRead() / Size
333  );
334 }
335 
336 size_t CSG_File::Read(CSG_String &Buffer, size_t Size) const
337 {
338  if( is_Reading() && Size > 0 )
339  {
340  CSG_Buffer s(Size + 1);
341 
342  size_t i = Read(s.Get_Data(), sizeof(char), Size);
343 
344  if( i > 0 )
345  {
346  s[Size] = '\0';
347 
348  Buffer = s.Get_Data();
349 
350  return( i );
351  }
352  }
353 
354  Buffer.Clear();
355 
356  return( 0 );
357 }
358 
359 //---------------------------------------------------------
360 size_t CSG_File::Write(void *Buffer, size_t Size, size_t Count) const
361 {
362  return( !is_Writing() || Size == 0 || Count == 0 ? 0 : m_Mode == SG_FILE_W
363  ? m_pStream_O ->Write(Buffer, Size * Count).LastWrite()
364  : m_pStream_IO->Write(Buffer, Size * Count).LastWrite()
365  );
366 }
367 
368 size_t CSG_File::Write(const CSG_String &Buffer) const
369 {
370  if( m_pConvert )
371  {
372  wxString _Buffer(Buffer.w_str());
373 
374  const wxScopedCharBuffer s(_Buffer.mb_str(*((wxMBConv *)m_pConvert)));
375 
376  return( Write((void *)s.data(), sizeof(char), s.length()) );
377  }
378 
379  CSG_Buffer s(Buffer.to_ASCII()); // returns NULL terminated char sequence, Get_Size() count includes terminating NULL!!!
380 
381  return( s.Get_Size() > 1 ? Write((void *)s.Get_Data(), sizeof(char), s.Get_Size() - 1) : 0 );
382 }
383 
384 //---------------------------------------------------------
385 bool CSG_File::Read_Line(CSG_String &sLine) const
386 {
387  if( !is_Reading() || is_EOF() )
388  {
389  return( false );
390  }
391 
392  wxString s;
393 
394  if( m_pConvert )
395  {
396  if( m_Mode == SG_FILE_R )
397  {
398  wxTextInputStream Stream(*m_pStream_I , " \t", *((wxMBConv *)m_pConvert)); s = Stream.ReadLine();
399  }
400  else
401  {
402  wxTextInputStream Stream(*m_pStream_IO, " \t", *((wxMBConv *)m_pConvert)); s = Stream.ReadLine();
403  }
404  }
405  else
406  {
407  if( m_Mode == SG_FILE_R )
408  {
409  wxTextInputStream Stream(*m_pStream_I , " \t" ); s = Stream.ReadLine();
410  }
411  else
412  {
413  wxTextInputStream Stream(*m_pStream_IO, " \t" ); s = Stream.ReadLine();
414  }
415  }
416 
417  sLine = CSG_String(&s);
418 
419  return( !sLine.is_Empty() || !is_EOF() );
420 }
421 
422 //---------------------------------------------------------
423 int CSG_File::Read_Char(void) const
424 {
425  return( !is_Reading() ? 0 : m_Mode == SG_FILE_R ? m_pStream_I->GetC() : m_pStream_IO->GetC() );
426 }
427 
428 //---------------------------------------------------------
429 int CSG_File::Read_Int(bool bByteOrderBig) const
430 {
431  int Value = 0;
432 
433  if( Read(&Value, sizeof(Value)) == 1 )
434  {
435  if( bByteOrderBig )
436  {
437  SG_Swap_Bytes(&Value, sizeof(Value));
438  }
439  }
440 
441  return( Value );
442 }
443 
444 bool CSG_File::Write_Int(int Value, bool bByteOrderBig)
445 {
446  if( bByteOrderBig )
447  {
448  SG_Swap_Bytes(&Value, sizeof(Value));
449  }
450 
451  return( Write(&Value, sizeof(Value)) == sizeof(Value) );
452 }
453 
454 //---------------------------------------------------------
455 double CSG_File::Read_Double(bool bByteOrderBig) const
456 {
457  double Value = 0;
458 
459  if( Read(&Value, sizeof(Value)) == 1 )
460  {
461  if( bByteOrderBig )
462  {
463  SG_Swap_Bytes(&Value, sizeof(Value));
464  }
465  }
466 
467  return( Value );
468 }
469 
470 bool CSG_File::Write_Double(double Value, bool bByteOrderBig)
471 {
472  if( bByteOrderBig )
473  {
474  SG_Swap_Bytes(&Value, sizeof(Value));
475  }
476 
477  return( Write(&Value, sizeof(Value)) == sizeof(Value) );
478 }
479 
480 //---------------------------------------------------------
481 bool CSG_File::Scan(int &Value) const
482 {
483  if( is_Reading() )
484  {
485  int c; while( !is_EOF() && isspace(c = Read_Char()) ); // remove leading white space
486 
487  if( isdigit(c) || strchr("-+", c) )
488  {
489  CSG_String s = (char)c;
490 
491  while( !is_EOF() && isdigit(c = Read_Char()) )
492  {
493  s += (char)c;
494  }
495 
496  return( s.asInt(Value) );
497  }
498  }
499 
500  return( false );
501 }
502 
503 bool CSG_File::Scan(double &Value) const
504 {
505  if( is_Reading() )
506  {
507  int c; while( !is_EOF() && isspace(c = Read_Char()) ); // remove leading white space
508 
509  if( isdigit(c) || strchr("-+.,eE", c) )
510  {
511  CSG_String s = (char)c;
512 
513  while( !is_EOF() && (isdigit(c = Read_Char()) || strchr(".,eE", c) || strchr("", c)) )
514  {
515  s += (char)c;
516  }
517 
518  return( s.asDouble(Value) );
519  }
520  }
521 
522  return( false );
523 }
524 
525 bool CSG_File::Scan(CSG_String &Value, SG_Char Separator) const
526 {
527  if( is_Reading() && !is_EOF() )
528  {
529  Value.Clear();
530 
531  int c; while( !is_EOF() && (c = Read_Char()) != Separator && c != EOF )
532  {
533  Value += (char)c;
534  }
535 
536  return( true );
537  }
538 
539  return( false );
540 }
541 
542 //---------------------------------------------------------
543 int CSG_File::Scan_Int(void) const
544 {
545  int Value; return( Scan(Value) ? Value : 0 );
546 }
547 
548 double CSG_File::Scan_Double(void) const
549 {
550  double Value; return( Scan(Value) ? Value : 0.0 );
551 }
552 
554 {
555  CSG_String Value; Scan(Value, Separator); return( Value );
556 }
557 
558 
560 // //
561 // //
562 // //
564 
565 //---------------------------------------------------------
567 {
568  On_Construction();
569 }
570 
571 //---------------------------------------------------------
572 CSG_File_Zip::CSG_File_Zip(const CSG_String &FileName, int Mode, int Encoding)
573 {
574  On_Construction();
575 
576  Open(FileName, Mode, Encoding);
577 }
578 
579 //---------------------------------------------------------
581 {
582  Close();
583 }
584 
585 //---------------------------------------------------------
586 bool CSG_File_Zip::Open(const CSG_String &FileName, int Mode, int Encoding)
587 {
588  Close();
589 
590  CSG_String Path(SG_File_Get_Path(FileName));
591 
592  if( !Path.is_Empty() && !SG_Dir_Exists(Path) )
593  {
594  return( false );
595  }
596 
597  if( Mode == SG_FILE_R && !SG_File_Exists(FileName) )
598  {
599  return( false );
600  }
601 
602  wxLogNull logNo; // suppress user notification dialog for invalid zip files
603 
604  m_Mode = Mode; Set_Encoding(Encoding);
605 
606  if( Mode == SG_FILE_W )
607  {
608  m_pStream = new wxZipOutputStream(new wxFileOutputStream(FileName.c_str()));
609  }
610  else if( Mode == SG_FILE_R && SG_File_Exists(FileName) )
611  {
612  m_pStream = new wxZipInputStream (new wxFileInputStream (FileName.c_str()));
613  }
614 
615  if( !m_pStream || !m_pStream_Base->IsOk() )
616  {
617  Close();
618 
619  return( false );
620  }
621 
622  if( is_Reading() )
623  {
624  wxZipEntry *pEntry;
625 
626  while( (pEntry = ((wxZipInputStream *)m_pStream)->GetNextEntry()) != NULL )
627  {
628  m_Files += pEntry;
629  }
630  }
631 
632  return( true );
633 }
634 
635 //---------------------------------------------------------
637 {
638  for(sLong i=0; i<m_Files.Get_Size(); i++)
639  {
640  delete((wxZipEntry *)m_Files[i]);
641  }
642 
643  m_Files.Set_Array(0);
644 
645  return( CSG_File::Close() );
646 }
647 
648 //---------------------------------------------------------
650 {
651  return( is_Writing() && ((wxZipOutputStream *)m_pStream)->PutNextDirEntry(Name.c_str()) );
652 }
653 
654 //---------------------------------------------------------
655 bool CSG_File_Zip::Add_File(const CSG_String &Name, bool bBinary)
656 {
657  if( is_Writing() )
658  {
659  wxZipEntry *pEntry = new wxZipEntry(Name.c_str());
660 
661  pEntry->SetIsText(bBinary == false);
662 
663  #if wxCHECK_VERSION(3, 1, 1)
664  ((wxZipOutputStream *)m_pStream)->SetFormat(wxZIP_FORMAT_ZIP64);
665  #endif
666 
667  if( ((wxZipOutputStream *)m_pStream)->PutNextEntry(pEntry) )
668  {
669  m_FileName = Name;
670 
671  return( true );
672  }
673  }
674 
675  return( false );
676 }
677 
678 //---------------------------------------------------------
679 bool CSG_File_Zip::is_Directory(size_t Index)
680 {
681  if( is_Reading() && m_Files[Index] )
682  {
683  return( ((wxZipEntry *)m_Files[Index])->IsDir() );
684  }
685 
686  return( false );
687 }
688 
689 //---------------------------------------------------------
690 bool CSG_File_Zip::Get_File(size_t Index)
691 {
692  if( is_Reading() && m_Files[Index] )
693  {
694  if( ((wxZipInputStream *)m_pStream)->OpenEntry(*(wxZipEntry *)m_Files[Index]) )
695  {
696  m_FileName = Get_File_Name(Index);
697 
698  return( true );
699  }
700  }
701 
702  return( false );
703 }
704 
705 //---------------------------------------------------------
707 {
708  if( is_Reading() )
709  {
710  for(sLong i=0; i<m_Files.Get_Size(); i++)
711  {
712  if( !((wxZipEntry *)m_Files[i])->GetName().Cmp(Name.c_str()) )
713  {
714  return( Get_File(i) );
715  }
716  }
717  }
718 
719  return( false );
720 }
721 
722 //---------------------------------------------------------
724 {
725  CSG_String s;
726 
727  if( is_Reading() && m_Files[Index] )
728  {
729  wxString Name(((wxZipEntry *)m_Files[Index])->GetName()); s = &Name;
730  }
731 
732  return( s );
733 }
734 
735 
737 // //
738 // //
739 // //
741 
742 //---------------------------------------------------------
743 bool SG_Dir_Exists(const CSG_String &Directory)
744 {
745  return( wxFileName::DirExists(Directory.c_str()) );
746 }
747 
748 //---------------------------------------------------------
749 bool SG_Dir_Create(const CSG_String &Directory, bool bFullPath)
750 {
751  if( SG_Dir_Exists(Directory) )
752  {
753  return( true );
754  }
755 
756  return( wxFileName::Mkdir(Directory.c_str(), wxS_DIR_DEFAULT, bFullPath ? wxPATH_MKDIR_FULL : 0) );
757 }
758 
759 //---------------------------------------------------------
760 bool SG_Dir_Delete(const CSG_String &Directory, bool bRecursive)
761 {
762  if( !SG_Dir_Exists(Directory) )
763  {
764  return( true );
765  }
766 
767  return( wxDir::Remove(Directory.c_str(), bRecursive ? wxPATH_RMDIR_RECURSIVE : 0) );
768 }
769 
770 //---------------------------------------------------------
772 {
773  wxString cwd = wxFileName::GetCwd();
774 
775  return( CSG_String(&cwd) );
776 }
777 
778 //---------------------------------------------------------
780 {
781  wxString fname = wxFileName::GetTempDir();
782 
783  return( CSG_String(&fname) );
784 }
785 
786 //---------------------------------------------------------
787 bool SG_Dir_List_Subdirectories (CSG_Strings &List, const CSG_String &Directory)
788 {
789  List.Clear();
790 
791  wxDir Dir;
792 
793  if( Dir.Open(Directory.c_str()) )
794  {
795  wxString FileName;
796 
797  if( Dir.GetFirst(&FileName, wxEmptyString, wxDIR_DIRS) )
798  {
799  do
800  {
801  List += SG_File_Make_Path(Directory, &FileName);
802  }
803  while( Dir.GetNext(&FileName) );
804  }
805  }
806 
807  return( List.Get_Count() > 0 );
808 }
809 
810 //---------------------------------------------------------
811 bool SG_Dir_List_Files (CSG_Strings &List, const CSG_String &Directory)
812 {
813  return( SG_Dir_List_Files(List, Directory, "") );
814 }
815 
816 bool SG_Dir_List_Files (CSG_Strings &List, const CSG_String &Directory, const CSG_String &Extension)
817 {
818  List.Clear();
819 
820  wxDir Dir;
821 
822  if( Dir.Open(Directory.c_str()) )
823  {
824  wxString FileName;
825 
826  if( Dir.GetFirst(&FileName, wxEmptyString, wxDIR_FILES) )
827  {
828  do
829  {
830  if( Extension.is_Empty() || SG_File_Cmp_Extension(&FileName, Extension) )
831  {
832  List += SG_File_Make_Path(Directory, &FileName);
833  }
834  }
835  while( Dir.GetNext(&FileName) );
836  }
837  }
838 
839  return( List.Get_Count() > 0 );
840 }
841 
842 
844 // //
845 // //
846 // //
848 
849 //---------------------------------------------------------
850 bool SG_File_Exists(const CSG_String &FileName)
851 {
852  return( wxFileExists(FileName.c_str()) );
853 }
854 
855 //---------------------------------------------------------
856 bool SG_File_Delete(const CSG_String &FileName)
857 {
858  return( SG_File_Exists(FileName) && wxRemoveFile(FileName.c_str()) );
859 }
860 
861 //---------------------------------------------------------
863 {
864  return( SG_File_Get_Name_Temp(Prefix, "") );
865 }
866 
867 CSG_String SG_File_Get_Name_Temp(const CSG_String &Prefix, const CSG_String &Directory)
868 {
869  if( !SG_Dir_Exists(Directory) )
870  {
871  return( CSG_String(wxFileName::CreateTempFileName(Prefix.c_str()).wc_str()) );
872  }
873 
874  return( CSG_String(wxFileName::CreateTempFileName(SG_File_Make_Path(Directory, Prefix).w_str()).wc_str()) );
875 }
876 
877 //---------------------------------------------------------
878 CSG_String SG_File_Get_Name(const CSG_String &full_Path, bool bExtension)
879 {
880  wxFileName fn(full_Path.c_str());
881 
882  if( bExtension )
883  {
884  wxString s(fn.GetFullName()); return( CSG_String(&s) );
885  }
886 
887  wxString s(fn.GetName()); return( &s );
888 }
889 
890 //---------------------------------------------------------
892 {
893  wxString s(wxFileName(full_Path.c_str()).GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR));
894 
895  return( CSG_String(&s) );
896 }
897 
898 //---------------------------------------------------------
900 {
901  wxFileName fn(full_Path.c_str());
902 
903  fn.MakeAbsolute();
904 
905  wxString s(fn.GetFullPath()); return( &s );
906 }
907 
908 //---------------------------------------------------------
909 CSG_String SG_File_Get_Path_Relative(const CSG_String &Directory, const CSG_String &full_Path)
910 {
911  wxFileName fn(full_Path.c_str());
912 
913  fn.MakeRelativeTo(Directory.c_str());
914 
915  wxString s(fn.GetFullPath()); return( &s );
916 }
917 
918 //---------------------------------------------------------
919 CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name)
920 {
921  return( SG_File_Make_Path(Directory, Name, "") );
922 }
923 
924 CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name, const CSG_String &Extension)
925 {
926  wxFileName fn;
927 
928  fn.AssignDir(!Directory.is_Empty() ? Directory.c_str() : SG_File_Get_Path(Name).c_str());
929 
930  if( !Extension.is_Empty() )
931  {
932  fn.SetName (SG_File_Get_Name(Name, false).c_str());
933  fn.SetExt (Extension.c_str());
934  }
935  else
936  {
937  fn.SetFullName (SG_File_Get_Name(Name, true).c_str());
938  }
939 
940  wxString s(fn.GetFullPath()); return( &s );
941 }
942 
943 //---------------------------------------------------------
944 bool SG_File_Cmp_Path(const CSG_String &Path1, const CSG_String &Path2)
945 {
946  wxFileName a(wxString(Path1.c_str())), b(wxString(Path2.c_str()));
947 
948  return( a.SameAs(b) );
949 }
950 
951 //---------------------------------------------------------
952 bool SG_File_Cmp_Extension(const CSG_String &FileName, const CSG_String &Extension)
953 {
954  return( SG_File_Get_Extension(FileName).CmpNoCase(Extension) == 0 );
955 }
956 
957 //---------------------------------------------------------
958 bool SG_File_Set_Extension(CSG_String &FileName, const CSG_String &Extension)
959 {
960  if( FileName.Length() > 0 )
961  {
962  wxFileName fn(FileName.c_str());
963 
964  fn.SetExt(Extension.c_str());
965 
966  wxString s(fn.GetFullPath());
967 
968  FileName = &s;
969 
970  return( true );
971  }
972 
973  return( false );
974 }
975 
976 //---------------------------------------------------------
978 {
979  wxFileName fn(FileName.c_str());
980 
981  wxString s(fn.GetExt()); return( &s );
982 }
983 
984 
986 // //
987 // //
988 // //
990 
991 //---------------------------------------------------------
992 bool SG_Get_Environment(const CSG_String &Variable, CSG_String *Value)
993 {
994  if( Value == NULL )
995  {
996  return( wxGetEnv(Variable.w_str(), NULL) );
997  }
998 
999  wxString s;
1000 
1001  if( wxGetEnv(Variable.w_str(), &s) )
1002  {
1003  *Value = s.wc_str();
1004 
1005  return( true );
1006  }
1007 
1008  return( false );
1009 }
1010 
1011 //---------------------------------------------------------
1012 bool SG_Set_Environment(const CSG_String &Variable, const CSG_String &Value)
1013 {
1014  return( wxSetEnv(Variable.w_str(), Value.w_str()) );
1015 }
1016 
1017 
1019 // //
1020 // //
1021 // //
1023 
1024 //---------------------------------------------------------
m_pStream_O
#define m_pStream_O
Definition: api_file.cpp:75
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_File::Set_Encoding
bool Set_Encoding(int Encoding)
Definition: api_file.cpp:188
CSG_File_Zip::Get_File
bool Get_File(const CSG_String &Name)
Definition: api_file.cpp:706
CSG_File_Zip::is_Directory
bool is_Directory(size_t Index)
Definition: api_file.cpp:679
CSG_File::Seek_Start
bool Seek_Start(void) const
Definition: api_file.cpp:257
CSG_String::to_ASCII
bool to_ASCII(char **pString, char Replace='_') const
Definition: api_string.cpp:909
SG_Swap_Bytes
SAGA_API_DLL_EXPORT void SG_Swap_Bytes(void *Buffer, int nBytes)
Definition: api_memory.cpp:154
CSG_String::w_str
const wchar_t * w_str(void) const
Definition: api_string.cpp:248
CSG_String::Length
size_t Length(void) const
Definition: api_string.cpp:172
CSG_File::m_FileName
CSG_String m_FileName
Definition: api_core.h:1185
SG_File_Get_Extension
CSG_String SG_File_Get_Extension(const CSG_String &FileName)
Definition: api_file.cpp:977
CSG_Array_Pointer::Get_Size
sLong Get_Size(void) const
Definition: api_core.h:381
CSG_File::Seek
bool Seek(sLong Offset, int Origin=SG_FILE_START) const
Definition: api_file.cpp:238
CSG_String::asInt
int asInt(void) const
Definition: api_string.cpp:722
SG_File_Exists
bool SG_File_Exists(const CSG_String &FileName)
Definition: api_file.cpp:850
CSG_File_Zip::Close
virtual bool Close(void)
Definition: api_file.cpp:636
CSG_File::Scan
bool Scan(int &Value) const
Definition: api_file.cpp:481
SG_FILE_ENCODING_UTF32BE
@ SG_FILE_ENCODING_UTF32BE
Definition: api_core.h:556
SG_File_Make_Path
CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name)
Definition: api_file.cpp:919
api_core.h
CSG_File::Scan_Int
int Scan_Int(void) const
Definition: api_file.cpp:543
SG_Dir_List_Subdirectories
bool SG_Dir_List_Subdirectories(CSG_Strings &List, const CSG_String &Directory)
Definition: api_file.cpp:787
SG_FILE_R
@ SG_FILE_R
Definition: api_core.h:1109
CSG_File::m_Mode
int m_Mode
Definition: api_core.h:1183
SG_FILE_END
@ SG_FILE_END
Definition: api_core.h:1119
CSG_File::Read
size_t Read(void *Buffer, size_t Size, size_t Count=1) const
Definition: api_file.cpp:328
SG_FILE_ENCODING_UTF7
@ SG_FILE_ENCODING_UTF7
Definition: api_core.h:551
CSG_Strings::Clear
void Clear(void)
Definition: api_core.h:734
CSG_Buffer
Definition: api_core.h:224
SG_Dir_Create
bool SG_Dir_Create(const CSG_String &Directory, bool bFullPath)
Definition: api_file.cpp:749
CSG_File::Scan_String
CSG_String Scan_String(SG_Char Separator) const
Definition: api_file.cpp:553
SG_Dir_Get_Current
CSG_String SG_Dir_Get_Current(void)
Definition: api_file.cpp:771
SG_FILE_ENCODING_UNDEFINED
@ SG_FILE_ENCODING_UNDEFINED
Definition: api_core.h:557
CSG_File_Zip::~CSG_File_Zip
virtual ~CSG_File_Zip(void)
Definition: api_file.cpp:580
SG_File_Cmp_Extension
bool SG_File_Cmp_Extension(const CSG_String &FileName, const CSG_String &Extension)
Definition: api_file.cpp:952
CSG_File::Read_Line
bool Read_Line(CSG_String &sLine) const
Definition: api_file.cpp:385
CSG_File::On_Construction
void On_Construction(void)
Definition: api_file.cpp:180
CSG_File_Zip::CSG_File_Zip
CSG_File_Zip(void)
Definition: api_file.cpp:566
CSG_File::is_Writing
bool is_Writing(void) const
Definition: api_core.h:1145
CSG_File::Scan_Double
double Scan_Double(void) const
Definition: api_file.cpp:548
sLong
signed long long sLong
Definition: api_core.h:158
CSG_File::Read_Int
int Read_Int(bool bBigEndian=false) const
Definition: api_file.cpp:429
SG_File_Get_Name
CSG_String SG_File_Get_Name(const CSG_String &full_Path, bool bExtension)
Definition: api_file.cpp:878
SG_FILE_ENCODING_UTF32LE
@ SG_FILE_ENCODING_UTF32LE
Definition: api_core.h:555
CSG_Buffer::Get_Size
size_t Get_Size(void) const
Definition: api_core.h:241
SG_Get_Environment
bool SG_Get_Environment(const CSG_String &Variable, CSG_String *Value)
Definition: api_file.cpp:992
CSG_File_Zip::m_Files
CSG_Array_Pointer m_Files
Definition: api_core.h:1221
CSG_File::m_Encoding
int m_Encoding
Definition: api_core.h:1183
SG_FILE_CURRENT
@ SG_FILE_CURRENT
Definition: api_core.h:1118
CSG_File::Close
virtual bool Close(void)
Definition: api_file.cpp:160
CSG_File::Read_Char
int Read_Char(void) const
Definition: api_file.cpp:423
CSG_File_Zip::Add_Directory
bool Add_Directory(const CSG_String &Name)
Definition: api_file.cpp:649
SG_FILE_W
@ SG_FILE_W
Definition: api_core.h:1110
CSG_Strings
Definition: api_core.h:699
SG_File_Delete
bool SG_File_Delete(const CSG_String &FileName)
Definition: api_file.cpp:856
SG_File_Cmp_Path
bool SG_File_Cmp_Path(const CSG_String &Path1, const CSG_String &Path2)
Definition: api_file.cpp:944
CSG_Buffer::Get_Data
char * Get_Data(int Offset=0) const
Definition: api_core.h:244
SG_File_Get_Path_Relative
CSG_String SG_File_Get_Path_Relative(const CSG_String &Directory, const CSG_String &full_Path)
Definition: api_file.cpp:909
SG_FILE_ENCODING_UTF16LE
@ SG_FILE_ENCODING_UTF16LE
Definition: api_core.h:553
CSG_File::Write_Double
bool Write_Double(double Value, bool bBigEndian=false)
Definition: api_file.cpp:470
m_pStream_I
#define m_pStream_I
Definition: api_file.cpp:74
CSG_File::m_pConvert
void * m_pConvert
Definition: api_core.h:1187
CSG_File::Length
sLong Length(void) const
Definition: api_file.cpp:226
SG_File_Get_Name_Temp
CSG_String SG_File_Get_Name_Temp(const CSG_String &Prefix)
Definition: api_file.cpp:862
CSG_String::Clear
void Clear(void)
Definition: api_string.cpp:259
SG_Char
#define SG_Char
Definition: api_core.h:536
m_pStream_IO
#define m_pStream_IO
Definition: api_file.cpp:76
CSG_String
Definition: api_core.h:563
SG_Dir_List_Files
bool SG_Dir_List_Files(CSG_Strings &List, const CSG_String &Directory)
Definition: api_file.cpp:811
CSG_File::m_pStream
void * m_pStream
Definition: api_core.h:1187
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_Set_Environment
bool SG_Set_Environment(const CSG_String &Variable, const CSG_String &Value)
Definition: api_file.cpp:1012
SG_FILE_ENCODING_UTF16BE
@ SG_FILE_ENCODING_UTF16BE
Definition: api_core.h:554
CSG_File::Write_Int
bool Write_Int(int Value, bool bBigEndian=false)
Definition: api_file.cpp:444
CSG_Array_Pointer::Set_Array
bool Set_Array(sLong nValues, bool bShrink=true)
Definition: api_core.h:387
CSG_File_Zip::Open
virtual bool Open(const CSG_String &FileName, int Mode=SG_FILE_R, int Encoding=SG_FILE_ENCODING_ANSI)
Definition: api_file.cpp:586
SG_File_Get_Path
CSG_String SG_File_Get_Path(const CSG_String &full_Path)
Definition: api_file.cpp:891
CSG_File::Read_Double
double Read_Double(bool bBigEndian=false) const
Definition: api_file.cpp:455
SG_Dir_Delete
bool SG_Dir_Delete(const CSG_String &Directory, bool bRecursive)
Definition: api_file.cpp:760
SG_File_Set_Extension
bool SG_File_Set_Extension(CSG_String &FileName, const CSG_String &Extension)
Definition: api_file.cpp:958
CSG_String::asDouble
double asDouble(void) const
Definition: api_string.cpp:760
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
CSG_File::CSG_File
CSG_File(void)
Definition: api_file.cpp:86
SG_Dir_Get_Temp
CSG_String SG_Dir_Get_Temp(void)
Definition: api_file.cpp:779
CSG_File_Zip::Add_File
bool Add_File(const CSG_String &Name, bool bBinary=true)
Definition: api_file.cpp:655
SG_FILE_ENCODING_ANSI
@ SG_FILE_ENCODING_ANSI
Definition: api_core.h:550
CSG_File::is_EOF
bool is_EOF(void) const
Definition: api_file.cpp:232
SG_FILE_ENCODING_UTF8
@ SG_FILE_ENCODING_UTF8
Definition: api_core.h:552
m_pStream_Base
#define m_pStream_Base
Definition: api_file.cpp:73
CSG_File::is_Reading
bool is_Reading(void) const
Definition: api_core.h:1144
SG_Dir_Exists
bool SG_Dir_Exists(const CSG_String &Directory)
Definition: api_file.cpp:743
CSG_File::Get_File_Name
virtual const CSG_String & Get_File_Name(void) const
Definition: api_core.h:1135
CSG_File::Seek_End
bool Seek_End(void) const
Definition: api_file.cpp:258
CSG_File::Tell
sLong Tell(void) const
Definition: api_file.cpp:261
SG_File_Get_Path_Absolute
CSG_String SG_File_Get_Path_Absolute(const CSG_String &full_Path)
Definition: api_file.cpp:899
CSG_File::~CSG_File
virtual ~CSG_File(void)
Definition: api_file.cpp:100