]> git.saurik.com Git - wxWidgets.git/blob - src/common/filename.cpp
fix for temp file creation under Windows
[wxWidgets.git] / src / common / filename.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/filename.cpp
3 // Purpose: wxFileName - encapsulates a file path
4 // Author: Robert Roebling, Vadim Zeitlin
5 // Modified by:
6 // Created: 28.12.2000
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 /*
13 Here are brief descriptions of the filename formats supported by this class:
14
15 wxPATH_UNIX: standard Unix format, used under Darwin as well, absolute file
16 names have the form:
17 /dir1/dir2/.../dirN/filename, "." and ".." stand for the
18 current and parent directory respectively, "~" is parsed as the
19 user HOME and "~username" as the HOME of that user
20
21 wxPATH_DOS: DOS/Windows format, absolute file names have the form:
22 drive:\dir1\dir2\...\dirN\filename.ext where drive is a single
23 letter. "." and ".." as for Unix but no "~".
24
25 There are also UNC names of the form \\share\fullpath
26
27 wxPATH_MAC: Mac OS 8/9 and Mac OS X under CodeWarrior 7 format, absolute file
28 names have the form
29 volume:dir1:...:dirN:filename
30 and the relative file names are either
31 :dir1:...:dirN:filename
32 or just
33 filename
34 (although :filename works as well).
35 Since the volume is just part of the file path, it is not
36 treated like a separate entity as it is done under DOS and
37 VMS, it is just treated as another dir.
38
39 wxPATH_VMS: VMS native format, absolute file names have the form
40 <device>:[dir1.dir2.dir3]file.txt
41 or
42 <device>:[000000.dir1.dir2.dir3]file.txt
43
44 the <device> is the physical device (i.e. disk). 000000 is the
45 root directory on the device which can be omitted.
46
47 Note that VMS uses different separators unlike Unix:
48 : always after the device. If the path does not contain : than
49 the default (the device of the current directory) is assumed.
50 [ start of directory specyfication
51 . separator between directory and subdirectory
52 ] between directory and file
53 */
54
55 // ============================================================================
56 // declarations
57 // ============================================================================
58
59 // ----------------------------------------------------------------------------
60 // headers
61 // ----------------------------------------------------------------------------
62
63 #ifdef __GNUG__
64 #pragma implementation "filename.h"
65 #endif
66
67 // For compilers that support precompilation, includes "wx.h".
68 #include "wx/wxprec.h"
69
70 #ifdef __BORLANDC__
71 #pragma hdrstop
72 #endif
73
74 #ifndef WX_PRECOMP
75 #include "wx/intl.h"
76 #include "wx/log.h"
77 #include "wx/file.h"
78 #endif
79
80 #include "wx/filename.h"
81 #include "wx/tokenzr.h"
82 #include "wx/config.h" // for wxExpandEnvVars
83 #include "wx/utils.h"
84 #include "wx/file.h"
85
86 #if wxUSE_DYNAMIC_LOADER || wxUSE_DYNLIB_CLASS
87 #include "wx/dynlib.h"
88 #endif
89
90 // For GetShort/LongPathName
91 #ifdef __WIN32__
92 #include <windows.h>
93 #include "wx/msw/winundef.h"
94 #endif
95
96 // utime() is POSIX so should normally be available on all Unices
97 #ifdef __UNIX_LIKE__
98 #include <sys/types.h>
99 #include <utime.h>
100 #include <sys/stat.h>
101 #include <unistd.h>
102 #endif
103
104 #ifdef __MWERKS__
105 #include <stat.h>
106 #include <unistd.h>
107 #include <unix.h>
108 #endif
109
110 #ifdef __WATCOMC__
111 #include <io.h>
112 #include <sys/utime.h>
113 #include <sys/stat.h>
114 #endif
115
116 #ifdef __VISAGECPP__
117 #ifndef MAX_PATH
118 #define MAX_PATH 256
119 #endif
120 #endif
121
122 // ----------------------------------------------------------------------------
123 // private classes
124 // ----------------------------------------------------------------------------
125
126 // small helper class which opens and closes the file - we use it just to get
127 // a file handle for the given file name to pass it to some Win32 API function
128 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
129
130 class wxFileHandle
131 {
132 public:
133 wxFileHandle(const wxString& filename)
134 {
135 m_hFile = ::CreateFile
136 (
137 filename, // name
138 GENERIC_READ, // access mask
139 0, // no sharing
140 NULL, // no secutity attr
141 OPEN_EXISTING, // creation disposition
142 0, // no flags
143 NULL // no template file
144 );
145
146 if ( m_hFile == INVALID_HANDLE_VALUE )
147 {
148 wxLogSysError(_("Failed to open '%s' for reading"),
149 filename.c_str());
150 }
151 }
152
153 ~wxFileHandle()
154 {
155 if ( m_hFile != INVALID_HANDLE_VALUE )
156 {
157 if ( !::CloseHandle(m_hFile) )
158 {
159 wxLogSysError(_("Failed to close file handle"));
160 }
161 }
162 }
163
164 // return TRUE only if the file could be opened successfully
165 bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; }
166
167 // get the handle
168 operator HANDLE() const { return m_hFile; }
169
170 private:
171 HANDLE m_hFile;
172 };
173
174 #endif // __WIN32__
175
176 // ----------------------------------------------------------------------------
177 // private functions
178 // ----------------------------------------------------------------------------
179
180 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
181
182 // convert between wxDateTime and FILETIME which is a 64-bit value representing
183 // the number of 100-nanosecond intervals since January 1, 1601.
184
185 // the number of milliseconds between the Unix Epoch (January 1, 1970) and the
186 // FILETIME reference point (January 1, 1601)
187 static const wxLongLong FILETIME_EPOCH_OFFSET = wxLongLong(0xa97, 0x30b66800);
188
189 static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft)
190 {
191 wxLongLong ll(ft.dwHighDateTime, ft.dwLowDateTime);
192
193 // convert 100ns to ms
194 ll /= 10000;
195
196 // move it to our Epoch
197 ll -= FILETIME_EPOCH_OFFSET;
198
199 *dt = wxDateTime(ll);
200 }
201
202 static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt)
203 {
204 // do the reverse of ConvertFileTimeToWx()
205 wxLongLong ll = dt.GetValue();
206 ll *= 10000;
207 ll += FILETIME_EPOCH_OFFSET;
208
209 ft->dwHighDateTime = ll.GetHi();
210 ft->dwLowDateTime = ll.GetLo();
211 }
212
213 #endif // __WIN32__
214
215 // ============================================================================
216 // implementation
217 // ============================================================================
218
219 // ----------------------------------------------------------------------------
220 // wxFileName construction
221 // ----------------------------------------------------------------------------
222
223 void wxFileName::Assign( const wxFileName &filepath )
224 {
225 m_volume = filepath.GetVolume();
226 m_dirs = filepath.GetDirs();
227 m_name = filepath.GetName();
228 m_ext = filepath.GetExt();
229 m_relative = filepath.IsRelative();
230 }
231
232 void wxFileName::Assign(const wxString& volume,
233 const wxString& path,
234 const wxString& name,
235 const wxString& ext,
236 wxPathFormat format )
237 {
238 wxPathFormat my_format = GetFormat( format );
239 wxString my_path = path;
240
241 m_dirs.Clear();
242
243 if (!my_path.empty())
244 {
245 // 1) Determine if the path is relative or absolute.
246
247 switch (my_format)
248 {
249 case wxPATH_MAC:
250 m_relative = ( my_path[0u] == wxT(':') );
251 // We then remove a leading ":". The reason is in our
252 // storage form for relative paths:
253 // ":dir:file.txt" actually means "./dir/file.txt" in
254 // DOS notation and should get stored as
255 // (relative) (dir) (file.txt)
256 // "::dir:file.txt" actually means "../dir/file.txt"
257 // stored as (relative) (..) (dir) (file.txt)
258 // This is important only for the Mac as an empty dir
259 // actually means <UP>, whereas under DOS, double
260 // slashes can be ignored: "\\\\" is the same as "\\".
261 if (m_relative)
262 my_path.Remove( 0, 1 );
263 break;
264 case wxPATH_VMS:
265 // TODO: what is the relative path format here?
266 m_relative = FALSE;
267 break;
268 case wxPATH_UNIX:
269 m_relative = ( my_path[0u] != wxT('/') );
270 break;
271 case wxPATH_DOS:
272 m_relative = ( (my_path[0u] != wxT('/')) && (my_path[0u] != wxT('\\')) );
273 break;
274 default:
275 wxFAIL_MSG( wxT("error") );
276 break;
277 }
278
279 // 2) Break up the path into its members. If the original path
280 // was just "/" or "\\", m_dirs will be empty. We know from
281 // the m_relative field, if this means "nothing" or "root dir".
282
283 wxStringTokenizer tn( my_path, GetPathSeparators(my_format) );
284
285 while ( tn.HasMoreTokens() )
286 {
287 wxString token = tn.GetNextToken();
288
289 // Remove empty token under DOS and Unix, interpret them
290 // as .. under Mac.
291 if (token.empty())
292 {
293 if (my_format == wxPATH_MAC)
294 m_dirs.Add( wxT("..") );
295 // else ignore
296 }
297 else
298 {
299 m_dirs.Add( token );
300 }
301 }
302 }
303
304 m_volume = volume;
305 m_ext = ext;
306 m_name = name;
307 }
308
309 void wxFileName::Assign(const wxString& fullpath,
310 wxPathFormat format)
311 {
312 wxString volume, path, name, ext;
313 SplitPath(fullpath, &volume, &path, &name, &ext, format);
314
315 Assign(volume, path, name, ext, format);
316 }
317
318 void wxFileName::Assign(const wxString& fullpathOrig,
319 const wxString& fullname,
320 wxPathFormat format)
321 {
322 // always recognize fullpath as directory, even if it doesn't end with a
323 // slash
324 wxString fullpath = fullpathOrig;
325 if ( !wxEndsWithPathSeparator(fullpath) )
326 {
327 fullpath += GetPathSeparators(format)[0u];
328 }
329
330 wxString volume, path, name, ext;
331
332 // do some consistency checks in debug mode: the name should be really just
333 // the filename and the path should be really just a path
334 #ifdef __WXDEBUG__
335 wxString pathDummy, nameDummy, extDummy;
336
337 SplitPath(fullname, &pathDummy, &name, &ext, format);
338
339 wxASSERT_MSG( pathDummy.empty(),
340 _T("the file name shouldn't contain the path") );
341
342 SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format);
343
344 wxASSERT_MSG( nameDummy.empty() && extDummy.empty(),
345 _T("the path shouldn't contain file name nor extension") );
346
347 #else // !__WXDEBUG__
348 SplitPath(fullname, NULL /* no path */, &name, &ext, format);
349 SplitPath(fullpath, &volume, &path, NULL, NULL, format);
350 #endif // __WXDEBUG__/!__WXDEBUG__
351
352 Assign(volume, path, name, ext, format);
353 }
354
355 void wxFileName::AssignDir(const wxString& dir, wxPathFormat format)
356 {
357 Assign(dir, _T(""), format);
358 }
359
360 void wxFileName::Clear()
361 {
362 m_dirs.Clear();
363
364 m_volume =
365 m_name =
366 m_ext = wxEmptyString;
367 }
368
369 /* static */
370 wxFileName wxFileName::FileName(const wxString& file)
371 {
372 return wxFileName(file);
373 }
374
375 /* static */
376 wxFileName wxFileName::DirName(const wxString& dir)
377 {
378 wxFileName fn;
379 fn.AssignDir(dir);
380 return fn;
381 }
382
383 // ----------------------------------------------------------------------------
384 // existence tests
385 // ----------------------------------------------------------------------------
386
387 bool wxFileName::FileExists()
388 {
389 return wxFileName::FileExists( GetFullPath() );
390 }
391
392 bool wxFileName::FileExists( const wxString &file )
393 {
394 return ::wxFileExists( file );
395 }
396
397 bool wxFileName::DirExists()
398 {
399 return wxFileName::DirExists( GetFullPath() );
400 }
401
402 bool wxFileName::DirExists( const wxString &dir )
403 {
404 return ::wxDirExists( dir );
405 }
406
407 // ----------------------------------------------------------------------------
408 // CWD and HOME stuff
409 // ----------------------------------------------------------------------------
410
411 void wxFileName::AssignCwd(const wxString& volume)
412 {
413 AssignDir(wxFileName::GetCwd(volume));
414 }
415
416 /* static */
417 wxString wxFileName::GetCwd(const wxString& volume)
418 {
419 // if we have the volume, we must get the current directory on this drive
420 // and to do this we have to chdir to this volume - at least under Windows,
421 // I don't know how to get the current drive on another volume elsewhere
422 // (TODO)
423 wxString cwdOld;
424 if ( !volume.empty() )
425 {
426 cwdOld = wxGetCwd();
427 SetCwd(volume + GetVolumeSeparator());
428 }
429
430 wxString cwd = ::wxGetCwd();
431
432 if ( !volume.empty() )
433 {
434 SetCwd(cwdOld);
435 }
436
437 return cwd;
438 }
439
440 bool wxFileName::SetCwd()
441 {
442 return wxFileName::SetCwd( GetFullPath() );
443 }
444
445 bool wxFileName::SetCwd( const wxString &cwd )
446 {
447 return ::wxSetWorkingDirectory( cwd );
448 }
449
450 void wxFileName::AssignHomeDir()
451 {
452 AssignDir(wxFileName::GetHomeDir());
453 }
454
455 wxString wxFileName::GetHomeDir()
456 {
457 return ::wxGetHomeDir();
458 }
459
460 void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp)
461 {
462 wxString tempname = CreateTempFileName(prefix, fileTemp);
463 if ( tempname.empty() )
464 {
465 // error, failed to get temp file name
466 Clear();
467 }
468 else // ok
469 {
470 Assign(tempname);
471 }
472 }
473
474 /* static */
475 wxString
476 wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
477 {
478 wxString path, dir, name;
479
480 // use the directory specified by the prefix
481 SplitPath(prefix, &dir, &name, NULL /* extension */);
482
483 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
484
485 #ifdef __WIN32__
486 if ( dir.empty() )
487 {
488 if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) )
489 {
490 wxLogLastError(_T("GetTempPath"));
491 }
492
493 if ( dir.empty() )
494 {
495 // GetTempFileName() fails if we pass it an empty string
496 dir = _T('.');
497 }
498 }
499
500 if ( !::GetTempFileName(dir, name, 0, wxStringBuffer(path, MAX_PATH + 1)) )
501 {
502 wxLogLastError(_T("GetTempFileName"));
503
504 path.clear();
505 }
506 #else // Win16
507 if ( !::GetTempFileName(NULL, prefix, 0, wxStringBuffer(path, 1025)) )
508 {
509 path.clear();
510 }
511 #endif // Win32/16
512
513 #elif defined(__WXPM__)
514 // for now just create a file
515 //
516 // future enhancements can be to set some extended attributes for file
517 // systems OS/2 supports that have them (HPFS, FAT32) and security
518 // (HPFS386)
519 static const wxChar *szMktempSuffix = wxT("XXX");
520 path << dir << _T('/') << name << szMktempSuffix;
521
522 // Temporarily remove - MN
523 #ifndef __WATCOMC__
524 ::DosCreateDir(wxStringBuffer(path, MAX_PATH), NULL);
525 #endif
526
527 #else // !Windows, !OS/2
528 if ( dir.empty() )
529 {
530 dir = wxGetenv(_T("TMP"));
531 if ( path.empty() )
532 {
533 dir = wxGetenv(_T("TEMP"));
534 }
535
536 if ( dir.empty() )
537 {
538 // default
539 #ifdef __DOS__
540 dir = _T(".");
541 #else
542 dir = _T("/tmp");
543 #endif
544 }
545 }
546
547 path = dir;
548
549 if ( !wxEndsWithPathSeparator(dir) &&
550 (name.empty() || !wxIsPathSeparator(name[0u])) )
551 {
552 path += wxFILE_SEP_PATH;
553 }
554
555 path += name;
556
557 #if defined(HAVE_MKSTEMP)
558 // scratch space for mkstemp()
559 path += _T("XXXXXX");
560
561 // can use the cast here because the length doesn't change and the string
562 // is not shared
563 int fdTemp = mkstemp((char *)path.mb_str());
564 if ( fdTemp == -1 )
565 {
566 // this might be not necessary as mkstemp() on most systems should have
567 // already done it but it doesn't hurt neither...
568 path.clear();
569 }
570 else // mkstemp() succeeded
571 {
572 // avoid leaking the fd
573 if ( fileTemp )
574 {
575 fileTemp->Attach(fdTemp);
576 }
577 else
578 {
579 close(fdTemp);
580 }
581 }
582 #else // !HAVE_MKSTEMP
583
584 #ifdef HAVE_MKTEMP
585 // same as above
586 path += _T("XXXXXX");
587
588 if ( !mktemp((char *)path.mb_str()) )
589 {
590 path.clear();
591 }
592 #else // !HAVE_MKTEMP (includes __DOS__)
593 // generate the unique file name ourselves
594 #ifndef __DOS__
595 path << (unsigned int)getpid();
596 #endif
597
598 wxString pathTry;
599
600 static const size_t numTries = 1000;
601 for ( size_t n = 0; n < numTries; n++ )
602 {
603 // 3 hex digits is enough for numTries == 1000 < 4096
604 pathTry = path + wxString::Format(_T("%.03x"), n);
605 if ( !wxFile::Exists(pathTry) )
606 {
607 break;
608 }
609
610 pathTry.clear();
611 }
612
613 path = pathTry;
614 #endif // HAVE_MKTEMP/!HAVE_MKTEMP
615
616 if ( !path.empty() )
617 {
618 }
619 #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
620
621 #endif // Windows/!Windows
622
623 if ( path.empty() )
624 {
625 wxLogSysError(_("Failed to create a temporary file name"));
626 }
627 else if ( fileTemp && !fileTemp->IsOpened() )
628 {
629 // open the file - of course, there is a race condition here, this is
630 // why we always prefer using mkstemp()...
631 //
632 // NB: GetTempFileName() under Windows creates the file, so using
633 // write_excl there would fail
634 if ( !fileTemp->Open(path,
635 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
636 wxFile::write,
637 #else
638 wxFile::write_excl,
639 #endif
640 wxS_IRUSR | wxS_IWUSR) )
641 {
642 // FIXME: If !ok here should we loop and try again with another
643 // file name? That is the standard recourse if open(O_EXCL)
644 // fails, though of course it should be protected against
645 // possible infinite looping too.
646
647 wxLogError(_("Failed to open temporary file."));
648
649 path.clear();
650 }
651 }
652
653 return path;
654 }
655
656 // ----------------------------------------------------------------------------
657 // directory operations
658 // ----------------------------------------------------------------------------
659
660 bool wxFileName::Mkdir( int perm, bool full )
661 {
662 return wxFileName::Mkdir( GetFullPath(), perm, full );
663 }
664
665 bool wxFileName::Mkdir( const wxString &dir, int perm, bool full )
666 {
667 if (full)
668 {
669 wxFileName filename(dir);
670 wxArrayString dirs = filename.GetDirs();
671 dirs.Add(filename.GetName());
672
673 size_t count = dirs.GetCount();
674 size_t i;
675 wxString currPath;
676 int noErrors = 0;
677 for ( i = 0; i < count; i++ )
678 {
679 currPath += dirs[i];
680
681 if (currPath.Last() == wxT(':'))
682 {
683 // Can't create a root directory so continue to next dir
684 currPath += wxFILE_SEP_PATH;
685 continue;
686 }
687
688 if (!DirExists(currPath))
689 if (!wxMkdir(currPath, perm))
690 noErrors ++;
691
692 if ( (i < (count-1)) )
693 currPath += wxFILE_SEP_PATH;
694 }
695
696 return (noErrors == 0);
697
698 }
699 else
700 return ::wxMkdir( dir, perm );
701 }
702
703 bool wxFileName::Rmdir()
704 {
705 return wxFileName::Rmdir( GetFullPath() );
706 }
707
708 bool wxFileName::Rmdir( const wxString &dir )
709 {
710 return ::wxRmdir( dir );
711 }
712
713 // ----------------------------------------------------------------------------
714 // path normalization
715 // ----------------------------------------------------------------------------
716
717 bool wxFileName::Normalize(wxPathNormalize flags,
718 const wxString& cwd,
719 wxPathFormat format)
720 {
721 // the existing path components
722 wxArrayString dirs = GetDirs();
723
724 // the path to prepend in front to make the path absolute
725 wxFileName curDir;
726
727 format = GetFormat(format);
728
729 // make the path absolute
730 if ( (flags & wxPATH_NORM_ABSOLUTE) && m_relative )
731 {
732 if ( cwd.empty() )
733 {
734 curDir.AssignCwd(GetVolume());
735 }
736 else // cwd provided
737 {
738 curDir.AssignDir(cwd);
739 }
740
741 #if 0
742 // the path may be not absolute because it doesn't have the volume name
743 // but in this case we shouldn't modify the directory components of it
744 // but just set the current volume
745 if ( !HasVolume() && curDir.HasVolume() )
746 {
747 SetVolume(curDir.GetVolume());
748
749 if ( IsAbsolute() )
750 {
751 // yes, it was the case - we don't need curDir then
752 curDir.Clear();
753 }
754 }
755 #endif
756 m_relative = FALSE;
757 }
758
759 // handle ~ stuff under Unix only
760 if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
761 {
762 if ( !dirs.IsEmpty() )
763 {
764 wxString dir = dirs[0u];
765 if ( !dir.empty() && dir[0u] == _T('~') )
766 {
767 curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
768
769 dirs.RemoveAt(0u);
770 }
771 }
772 }
773
774 // transform relative path into abs one
775 if ( curDir.IsOk() )
776 {
777 wxArrayString dirsNew = curDir.GetDirs();
778 size_t count = dirs.GetCount();
779 for ( size_t n = 0; n < count; n++ )
780 {
781 dirsNew.Add(dirs[n]);
782 }
783
784 dirs = dirsNew;
785 }
786
787 // now deal with ".", ".." and the rest
788 m_dirs.Empty();
789 size_t count = dirs.GetCount();
790 for ( size_t n = 0; n < count; n++ )
791 {
792 wxString dir = dirs[n];
793
794 if ( flags & wxPATH_NORM_DOTS )
795 {
796 if ( dir == wxT(".") )
797 {
798 // just ignore
799 continue;
800 }
801
802 if ( dir == wxT("..") )
803 {
804 if ( m_dirs.IsEmpty() )
805 {
806 wxLogError(_("The path '%s' contains too many \"..\"!"),
807 GetFullPath().c_str());
808 return FALSE;
809 }
810
811 m_dirs.RemoveAt(m_dirs.GetCount() - 1);
812 continue;
813 }
814 }
815
816 if ( flags & wxPATH_NORM_ENV_VARS )
817 {
818 dir = wxExpandEnvVars(dir);
819 }
820
821 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
822 {
823 dir.MakeLower();
824 }
825
826 m_dirs.Add(dir);
827 }
828
829 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
830 {
831 // VZ: expand env vars here too?
832
833 m_name.MakeLower();
834 m_ext.MakeLower();
835 }
836
837 #if defined(__WIN32__)
838 if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) )
839 {
840 Assign(GetLongPath());
841 }
842 #endif // Win32
843
844 return TRUE;
845 }
846
847 bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format)
848 {
849 wxFileName fnBase(pathBase, format);
850
851 // get cwd only once - small time saving
852 wxString cwd = wxGetCwd();
853 Normalize(wxPATH_NORM_ALL, cwd, format);
854 fnBase.Normalize(wxPATH_NORM_ALL, cwd, format);
855
856 bool withCase = IsCaseSensitive(format);
857
858 // we can't do anything if the files live on different volumes
859 if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) )
860 {
861 // nothing done
862 return FALSE;
863 }
864
865 // same drive, so we don't need our volume
866 m_volume.clear();
867
868 // remove common directories starting at the top
869 while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() &&
870 m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) )
871 {
872 m_dirs.RemoveAt(0);
873 fnBase.m_dirs.RemoveAt(0);
874 }
875
876 // add as many ".." as needed
877 size_t count = fnBase.m_dirs.GetCount();
878 for ( size_t i = 0; i < count; i++ )
879 {
880 m_dirs.Insert(wxT(".."), 0u);
881 }
882
883 m_relative = TRUE;
884
885 // we were modified
886 return TRUE;
887 }
888
889 // ----------------------------------------------------------------------------
890 // filename kind tests
891 // ----------------------------------------------------------------------------
892
893 bool wxFileName::SameAs(const wxFileName &filepath, wxPathFormat format)
894 {
895 wxFileName fn1 = *this,
896 fn2 = filepath;
897
898 // get cwd only once - small time saving
899 wxString cwd = wxGetCwd();
900 fn1.Normalize(wxPATH_NORM_ALL, cwd, format);
901 fn2.Normalize(wxPATH_NORM_ALL, cwd, format);
902
903 if ( fn1.GetFullPath() == fn2.GetFullPath() )
904 return TRUE;
905
906 // TODO: compare inodes for Unix, this works even when filenames are
907 // different but files are the same (symlinks) (VZ)
908
909 return FALSE;
910 }
911
912 /* static */
913 bool wxFileName::IsCaseSensitive( wxPathFormat format )
914 {
915 // only Unix filenames are truely case-sensitive
916 return GetFormat(format) == wxPATH_UNIX;
917 }
918
919 /* static */
920 wxString wxFileName::GetVolumeSeparator(wxPathFormat format)
921 {
922 wxString sepVol;
923
924 if ( (GetFormat(format) == wxPATH_DOS) ||
925 (GetFormat(format) == wxPATH_VMS) )
926 {
927 sepVol = wxFILE_SEP_DSK;
928 }
929 //else: leave empty
930
931 return sepVol;
932 }
933
934 /* static */
935 wxString wxFileName::GetPathSeparators(wxPathFormat format)
936 {
937 wxString seps;
938 switch ( GetFormat(format) )
939 {
940 case wxPATH_DOS:
941 // accept both as native APIs do but put the native one first as
942 // this is the one we use in GetFullPath()
943 seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX;
944 break;
945
946 default:
947 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
948 // fall through
949
950 case wxPATH_UNIX:
951 seps = wxFILE_SEP_PATH_UNIX;
952 break;
953
954 case wxPATH_MAC:
955 seps = wxFILE_SEP_PATH_MAC;
956 break;
957
958 case wxPATH_VMS:
959 seps = wxFILE_SEP_PATH_VMS;
960 break;
961 }
962
963 return seps;
964 }
965
966 /* static */
967 bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format)
968 {
969 // wxString::Find() doesn't work as expected with NUL - it will always find
970 // it, so it is almost surely a bug if this function is called with NUL arg
971 wxASSERT_MSG( ch != _T('\0'), _T("shouldn't be called with NUL") );
972
973 return GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
974 }
975
976 bool wxFileName::IsWild( wxPathFormat format )
977 {
978 // FIXME: this is probably false for Mac and this is surely wrong for most
979 // of Unix shells (think about "[...]")
980 (void)format;
981 return m_name.find_first_of(_T("*?")) != wxString::npos;
982 }
983
984 // ----------------------------------------------------------------------------
985 // path components manipulation
986 // ----------------------------------------------------------------------------
987
988 void wxFileName::AppendDir( const wxString &dir )
989 {
990 m_dirs.Add( dir );
991 }
992
993 void wxFileName::PrependDir( const wxString &dir )
994 {
995 m_dirs.Insert( dir, 0 );
996 }
997
998 void wxFileName::InsertDir( int before, const wxString &dir )
999 {
1000 m_dirs.Insert( dir, before );
1001 }
1002
1003 void wxFileName::RemoveDir( int pos )
1004 {
1005 m_dirs.Remove( (size_t)pos );
1006 }
1007
1008 // ----------------------------------------------------------------------------
1009 // accessors
1010 // ----------------------------------------------------------------------------
1011
1012 void wxFileName::SetFullName(const wxString& fullname)
1013 {
1014 SplitPath(fullname, NULL /* no path */, &m_name, &m_ext);
1015 }
1016
1017 wxString wxFileName::GetFullName() const
1018 {
1019 wxString fullname = m_name;
1020 if ( !m_ext.empty() )
1021 {
1022 fullname << wxFILE_SEP_EXT << m_ext;
1023 }
1024
1025 return fullname;
1026 }
1027
1028 wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const
1029 {
1030 format = GetFormat( format );
1031
1032 wxString fullpath;
1033
1034 // the leading character
1035 if ( format == wxPATH_MAC && m_relative )
1036 {
1037 fullpath += wxFILE_SEP_PATH_MAC;
1038 }
1039 else if ( format == wxPATH_DOS )
1040 {
1041 if (!m_relative)
1042 fullpath += wxFILE_SEP_PATH_DOS;
1043 }
1044 else if ( format == wxPATH_UNIX )
1045 {
1046 if (!m_relative)
1047 fullpath += wxFILE_SEP_PATH_UNIX;
1048 }
1049
1050 // then concatenate all the path components using the path separator
1051 size_t dirCount = m_dirs.GetCount();
1052 if ( dirCount )
1053 {
1054 if ( format == wxPATH_VMS )
1055 {
1056 fullpath += wxT('[');
1057 }
1058
1059
1060 for ( size_t i = 0; i < dirCount; i++ )
1061 {
1062 // TODO: What to do with ".." under VMS
1063
1064 switch (format)
1065 {
1066 case wxPATH_MAC:
1067 {
1068 if (m_dirs[i] == wxT("."))
1069 break;
1070 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1071 fullpath += m_dirs[i];
1072 fullpath += wxT(':');
1073 break;
1074 }
1075 case wxPATH_DOS:
1076 {
1077 fullpath += m_dirs[i];
1078 fullpath += wxT('\\');
1079 break;
1080 }
1081 case wxPATH_UNIX:
1082 {
1083 fullpath += m_dirs[i];
1084 fullpath += wxT('/');
1085 break;
1086 }
1087 case wxPATH_VMS:
1088 {
1089 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1090 fullpath += m_dirs[i];
1091 if (i == dirCount-1)
1092 fullpath += wxT(']');
1093 else
1094 fullpath += wxT('.');
1095 break;
1096 }
1097 default:
1098 {
1099 wxFAIL_MSG( wxT("error") );
1100 }
1101 }
1102 }
1103 }
1104
1105
1106
1107 return fullpath;
1108 }
1109
1110 wxString wxFileName::GetFullPath( wxPathFormat format ) const
1111 {
1112 format = GetFormat(format);
1113
1114 wxString fullpath;
1115
1116 // first put the volume
1117 if ( !m_volume.empty() )
1118 {
1119 {
1120 // Special Windows UNC paths hack, part 2: undo what we did in
1121 // SplitPath() and make an UNC path if we have a drive which is not a
1122 // single letter (hopefully the network shares can't be one letter only
1123 // although I didn't find any authoritative docs on this)
1124 if ( format == wxPATH_DOS && m_volume.length() > 1 )
1125 {
1126 fullpath << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << m_volume;
1127 }
1128 else if ( format == wxPATH_DOS || format == wxPATH_VMS )
1129 {
1130 fullpath << m_volume << GetVolumeSeparator(format);
1131 }
1132 // else ignore
1133 }
1134 }
1135
1136 // the leading character
1137 if ( format == wxPATH_MAC && m_relative )
1138 {
1139 fullpath += wxFILE_SEP_PATH_MAC;
1140 }
1141 else if ( format == wxPATH_DOS )
1142 {
1143 if (!m_relative)
1144 fullpath += wxFILE_SEP_PATH_DOS;
1145 }
1146 else if ( format == wxPATH_UNIX )
1147 {
1148 if (!m_relative)
1149 fullpath += wxFILE_SEP_PATH_UNIX;
1150 }
1151
1152 // then concatenate all the path components using the path separator
1153 size_t dirCount = m_dirs.GetCount();
1154 if ( dirCount )
1155 {
1156 if ( format == wxPATH_VMS )
1157 {
1158 fullpath += wxT('[');
1159 }
1160
1161
1162 for ( size_t i = 0; i < dirCount; i++ )
1163 {
1164 // TODO: What to do with ".." under VMS
1165
1166 switch (format)
1167 {
1168 case wxPATH_MAC:
1169 {
1170 if (m_dirs[i] == wxT("."))
1171 break;
1172 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1173 fullpath += m_dirs[i];
1174 fullpath += wxT(':');
1175 break;
1176 }
1177 case wxPATH_DOS:
1178 {
1179 fullpath += m_dirs[i];
1180 fullpath += wxT('\\');
1181 break;
1182 }
1183 case wxPATH_UNIX:
1184 {
1185 fullpath += m_dirs[i];
1186 fullpath += wxT('/');
1187 break;
1188 }
1189 case wxPATH_VMS:
1190 {
1191 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1192 fullpath += m_dirs[i];
1193 if (i == dirCount-1)
1194 fullpath += wxT(']');
1195 else
1196 fullpath += wxT('.');
1197 break;
1198 }
1199 default:
1200 {
1201 wxFAIL_MSG( wxT("error") );
1202 }
1203 }
1204 }
1205 }
1206
1207 // finally add the file name and extension
1208 fullpath += GetFullName();
1209
1210 return fullpath;
1211 }
1212
1213 // Return the short form of the path (returns identity on non-Windows platforms)
1214 wxString wxFileName::GetShortPath() const
1215 {
1216 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
1217 wxString path(GetFullPath());
1218 wxString pathOut;
1219 DWORD sz = ::GetShortPathName(path, NULL, 0);
1220 bool ok = sz != 0;
1221 if ( ok )
1222 {
1223 ok = ::GetShortPathName
1224 (
1225 path,
1226 pathOut.GetWriteBuf(sz),
1227 sz
1228 ) != 0;
1229 pathOut.UngetWriteBuf();
1230 }
1231 if (ok)
1232 return pathOut;
1233
1234 return path;
1235 #else
1236 return GetFullPath();
1237 #endif
1238 }
1239
1240 // Return the long form of the path (returns identity on non-Windows platforms)
1241 wxString wxFileName::GetLongPath() const
1242 {
1243 wxString pathOut,
1244 path = GetFullPath();
1245
1246 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1247 bool success = FALSE;
1248
1249 // VZ: this code was disabled, why?
1250 #if 0 // wxUSE_DYNAMIC_LOADER
1251 typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
1252
1253 static bool s_triedToLoad = FALSE;
1254
1255 if ( !s_triedToLoad )
1256 {
1257 s_triedToLoad = TRUE;
1258 wxDllType dllKernel = wxDllLoader::LoadLibrary(_T("kernel32"));
1259 if ( dllKernel )
1260 {
1261 // may succeed or fail depending on the Windows version
1262 static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
1263 #ifdef _UNICODE
1264 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameW"));
1265 #else
1266 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameA"));
1267 #endif
1268
1269 wxDllLoader::UnloadLibrary(dllKernel);
1270
1271 if ( s_pfnGetLongPathName )
1272 {
1273 DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0);
1274 bool ok = dwSize > 0;
1275
1276 if ( ok )
1277 {
1278 DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0);
1279 ok = sz != 0;
1280 if ( ok )
1281 {
1282 ok = (*s_pfnGetLongPathName)
1283 (
1284 path,
1285 pathOut.GetWriteBuf(sz),
1286 sz
1287 ) != 0;
1288 pathOut.UngetWriteBuf();
1289
1290 success = TRUE;
1291 }
1292 }
1293 }
1294 }
1295 }
1296 if (success)
1297 return pathOut;
1298 #endif // wxUSE_DYNAMIC_LOADER
1299
1300 if (!success)
1301 {
1302 // The OS didn't support GetLongPathName, or some other error.
1303 // We need to call FindFirstFile on each component in turn.
1304
1305 WIN32_FIND_DATA findFileData;
1306 HANDLE hFind;
1307 pathOut = wxEmptyString;
1308
1309 wxArrayString dirs = GetDirs();
1310 dirs.Add(GetFullName());
1311
1312 wxString tmpPath;
1313
1314 size_t count = dirs.GetCount();
1315 for ( size_t i = 0; i < count; i++ )
1316 {
1317 // We're using pathOut to collect the long-name path, but using a
1318 // temporary for appending the last path component which may be
1319 // short-name
1320 tmpPath = pathOut + dirs[i];
1321
1322 if ( tmpPath.empty() )
1323 continue;
1324
1325 if ( tmpPath.Last() == wxT(':') )
1326 {
1327 // Can't pass a drive and root dir to FindFirstFile,
1328 // so continue to next dir
1329 tmpPath += wxFILE_SEP_PATH;
1330 pathOut = tmpPath;
1331 continue;
1332 }
1333
1334 hFind = ::FindFirstFile(tmpPath, &findFileData);
1335 if (hFind == INVALID_HANDLE_VALUE)
1336 {
1337 // Error: return immediately with the original path
1338 return path;
1339 }
1340
1341 pathOut += findFileData.cFileName;
1342 if ( (i < (count-1)) )
1343 pathOut += wxFILE_SEP_PATH;
1344
1345 ::FindClose(hFind);
1346 }
1347 }
1348 #else // !Win32
1349 pathOut = path;
1350 #endif // Win32/!Win32
1351
1352 return pathOut;
1353 }
1354
1355 wxPathFormat wxFileName::GetFormat( wxPathFormat format )
1356 {
1357 if (format == wxPATH_NATIVE)
1358 {
1359 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__DOS__)
1360 format = wxPATH_DOS;
1361 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1362 format = wxPATH_MAC;
1363 #elif defined(__VMS)
1364 format = wxPATH_VMS;
1365 #else
1366 format = wxPATH_UNIX;
1367 #endif
1368 }
1369 return format;
1370 }
1371
1372 // ----------------------------------------------------------------------------
1373 // path splitting function
1374 // ----------------------------------------------------------------------------
1375
1376 /* static */
1377 void wxFileName::SplitPath(const wxString& fullpathWithVolume,
1378 wxString *pstrVolume,
1379 wxString *pstrPath,
1380 wxString *pstrName,
1381 wxString *pstrExt,
1382 wxPathFormat format)
1383 {
1384 format = GetFormat(format);
1385
1386 wxString fullpath = fullpathWithVolume;
1387
1388 // under VMS the end of the path is ']', not the path separator used to
1389 // separate the components
1390 wxString sepPath = format == wxPATH_VMS ? wxString(_T(']'))
1391 : GetPathSeparators(format);
1392
1393 // special Windows UNC paths hack: transform \\share\path into share:path
1394 if ( format == wxPATH_DOS )
1395 {
1396 if ( fullpath.length() >= 4 &&
1397 fullpath[0u] == wxFILE_SEP_PATH_DOS &&
1398 fullpath[1u] == wxFILE_SEP_PATH_DOS )
1399 {
1400 fullpath.erase(0, 2);
1401
1402 size_t posFirstSlash = fullpath.find_first_of(sepPath);
1403 if ( posFirstSlash != wxString::npos )
1404 {
1405 fullpath[posFirstSlash] = wxFILE_SEP_DSK;
1406
1407 // UNC paths are always absolute, right? (FIXME)
1408 fullpath.insert(posFirstSlash + 1, wxFILE_SEP_PATH_DOS);
1409 }
1410 }
1411 }
1412
1413 // We separate the volume here
1414 if ( format == wxPATH_DOS || format == wxPATH_VMS )
1415 {
1416 wxString sepVol = GetVolumeSeparator(format);
1417
1418 size_t posFirstColon = fullpath.find_first_of(sepVol);
1419 if ( posFirstColon != wxString::npos )
1420 {
1421 if ( pstrVolume )
1422 {
1423 *pstrVolume = fullpath.Left(posFirstColon);
1424 }
1425
1426 // remove the volume name and the separator from the full path
1427 fullpath.erase(0, posFirstColon + sepVol.length());
1428 }
1429 }
1430
1431 // find the positions of the last dot and last path separator in the path
1432 size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT);
1433 size_t posLastSlash = fullpath.find_last_of(sepPath);
1434
1435 if ( (posLastDot != wxString::npos) &&
1436 ((format == wxPATH_UNIX) || (format == wxPATH_VMS)) )
1437 {
1438 if ( (posLastDot == 0) ||
1439 (fullpath[posLastDot - 1] == sepPath[0u] ) )
1440 {
1441 // under Unix and VMS, dot may be (and commonly is) the first
1442 // character of the filename, don't treat the entire filename as
1443 // extension in this case
1444 posLastDot = wxString::npos;
1445 }
1446 }
1447
1448 // if we do have a dot and a slash, check that the dot is in the name part
1449 if ( (posLastDot != wxString::npos) &&
1450 (posLastSlash != wxString::npos) &&
1451 (posLastDot < posLastSlash) )
1452 {
1453 // the dot is part of the path, not the start of the extension
1454 posLastDot = wxString::npos;
1455 }
1456
1457 // now fill in the variables provided by user
1458 if ( pstrPath )
1459 {
1460 if ( posLastSlash == wxString::npos )
1461 {
1462 // no path at all
1463 pstrPath->Empty();
1464 }
1465 else
1466 {
1467 // take everything up to the path separator but take care to make
1468 // the path equal to something like '/', not empty, for the files
1469 // immediately under root directory
1470 size_t len = posLastSlash;
1471 if ( !len )
1472 len++;
1473
1474 *pstrPath = fullpath.Left(len);
1475
1476 // special VMS hack: remove the initial bracket
1477 if ( format == wxPATH_VMS )
1478 {
1479 if ( (*pstrPath)[0u] == _T('[') )
1480 pstrPath->erase(0, 1);
1481 }
1482 }
1483 }
1484
1485 if ( pstrName )
1486 {
1487 // take all characters starting from the one after the last slash and
1488 // up to, but excluding, the last dot
1489 size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
1490 size_t count;
1491 if ( posLastDot == wxString::npos )
1492 {
1493 // take all until the end
1494 count = wxString::npos;
1495 }
1496 else if ( posLastSlash == wxString::npos )
1497 {
1498 count = posLastDot;
1499 }
1500 else // have both dot and slash
1501 {
1502 count = posLastDot - posLastSlash - 1;
1503 }
1504
1505 *pstrName = fullpath.Mid(nStart, count);
1506 }
1507
1508 if ( pstrExt )
1509 {
1510 if ( posLastDot == wxString::npos )
1511 {
1512 // no extension
1513 pstrExt->Empty();
1514 }
1515 else
1516 {
1517 // take everything after the dot
1518 *pstrExt = fullpath.Mid(posLastDot + 1);
1519 }
1520 }
1521 }
1522
1523 /* static */
1524 void wxFileName::SplitPath(const wxString& fullpath,
1525 wxString *path,
1526 wxString *name,
1527 wxString *ext,
1528 wxPathFormat format)
1529 {
1530 wxString volume;
1531 SplitPath(fullpath, &volume, path, name, ext, format);
1532
1533 if ( path && !volume.empty() )
1534 {
1535 path->Prepend(volume + GetVolumeSeparator(format));
1536 }
1537 }
1538
1539 // ----------------------------------------------------------------------------
1540 // time functions
1541 // ----------------------------------------------------------------------------
1542
1543 bool wxFileName::SetTimes(const wxDateTime *dtCreate,
1544 const wxDateTime *dtAccess,
1545 const wxDateTime *dtMod)
1546 {
1547 #if defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
1548 if ( !dtAccess && !dtMod )
1549 {
1550 // can't modify the creation time anyhow, don't try
1551 return TRUE;
1552 }
1553
1554 // if dtAccess or dtMod is not specified, use the other one (which must be
1555 // non NULL because of the test above) for both times
1556 utimbuf utm;
1557 utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks();
1558 utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks();
1559 if ( utime(GetFullPath(), &utm) == 0 )
1560 {
1561 return TRUE;
1562 }
1563 #elif defined(__WIN32__)
1564 wxFileHandle fh(GetFullPath());
1565 if ( fh.IsOk() )
1566 {
1567 FILETIME ftAccess, ftCreate, ftWrite;
1568
1569 if ( dtCreate )
1570 ConvertWxToFileTime(&ftCreate, *dtCreate);
1571 if ( dtAccess )
1572 ConvertWxToFileTime(&ftAccess, *dtAccess);
1573 if ( dtMod )
1574 ConvertWxToFileTime(&ftWrite, *dtMod);
1575
1576 if ( ::SetFileTime(fh,
1577 dtCreate ? &ftCreate : NULL,
1578 dtAccess ? &ftAccess : NULL,
1579 dtMod ? &ftWrite : NULL) )
1580 {
1581 return TRUE;
1582 }
1583 }
1584 #else // other platform
1585 #endif // platforms
1586
1587 wxLogSysError(_("Failed to modify file times for '%s'"),
1588 GetFullPath().c_str());
1589
1590 return FALSE;
1591 }
1592
1593 bool wxFileName::Touch()
1594 {
1595 #if defined(__UNIX_LIKE__)
1596 // under Unix touching file is simple: just pass NULL to utime()
1597 if ( utime(GetFullPath(), NULL) == 0 )
1598 {
1599 return TRUE;
1600 }
1601
1602 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1603
1604 return FALSE;
1605 #else // other platform
1606 wxDateTime dtNow = wxDateTime::Now();
1607
1608 return SetTimes(NULL /* don't change create time */, &dtNow, &dtNow);
1609 #endif // platforms
1610 }
1611
1612 bool wxFileName::GetTimes(wxDateTime *dtAccess,
1613 wxDateTime *dtMod,
1614 wxDateTime *dtChange) const
1615 {
1616 #if defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
1617 wxStructStat stBuf;
1618 if ( wxStat(GetFullPath(), &stBuf) == 0 )
1619 {
1620 if ( dtAccess )
1621 dtAccess->Set(stBuf.st_atime);
1622 if ( dtMod )
1623 dtMod->Set(stBuf.st_mtime);
1624 if ( dtChange )
1625 dtChange->Set(stBuf.st_ctime);
1626
1627 return TRUE;
1628 }
1629 #elif defined(__WIN32__)
1630 wxFileHandle fh(GetFullPath());
1631 if ( fh.IsOk() )
1632 {
1633 FILETIME ftAccess, ftCreate, ftWrite;
1634
1635 if ( ::GetFileTime(fh,
1636 dtMod ? &ftCreate : NULL,
1637 dtAccess ? &ftAccess : NULL,
1638 dtChange ? &ftWrite : NULL) )
1639 {
1640 if ( dtMod )
1641 ConvertFileTimeToWx(dtMod, ftCreate);
1642 if ( dtAccess )
1643 ConvertFileTimeToWx(dtAccess, ftAccess);
1644 if ( dtChange )
1645 ConvertFileTimeToWx(dtChange, ftWrite);
1646
1647 return TRUE;
1648 }
1649 }
1650 #else // other platform
1651 #endif // platforms
1652
1653 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1654 GetFullPath().c_str());
1655
1656 return FALSE;
1657 }
1658