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