]> git.saurik.com Git - wxWidgets.git/blob - src/common/filename.cpp
typo: & was used instead of &&
[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 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "filename.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #endif
35
36 #include "wx/filename.h"
37 #include "wx/tokenzr.h"
38 #include "wx/config.h" // for wxExpandEnvVars
39 #include "wx/utils.h"
40
41 #if wxUSE_DYNLIB_CLASS
42 #include "wx/dynlib.h"
43 #endif
44
45 // For GetShort/LongPathName
46 #ifdef __WIN32__
47 #include <windows.h>
48
49 #include "wx/msw/winundef.h"
50 #endif
51
52 // utime() is POSIX so should normally be available on all Unices
53 #ifdef __UNIX_LIKE__
54 #include <sys/types.h>
55 #include <utime.h>
56 #include <sys/stat.h>
57 #include <unistd.h>
58 #endif
59
60 #ifdef __MWERKS__
61 #include <stat.h>
62 #include <unistd.h>
63 #include <unix.h>
64 #endif
65
66 // ----------------------------------------------------------------------------
67 // private classes
68 // ----------------------------------------------------------------------------
69
70 // small helper class which opens and closes the file - we use it just to get
71 // a file handle for the given file name to pass it to some Win32 API function
72 #ifdef __WIN32__
73
74 class wxFileHandle
75 {
76 public:
77 wxFileHandle(const wxString& filename)
78 {
79 m_hFile = ::CreateFile
80 (
81 filename, // name
82 GENERIC_READ, // access mask
83 0, // no sharing
84 NULL, // no secutity attr
85 OPEN_EXISTING, // creation disposition
86 0, // no flags
87 NULL // no template file
88 );
89
90 if ( m_hFile == INVALID_HANDLE_VALUE )
91 {
92 wxLogSysError(_("Failed to open '%s' for reading"),
93 filename.c_str());
94 }
95 }
96
97 ~wxFileHandle()
98 {
99 if ( m_hFile != INVALID_HANDLE_VALUE )
100 {
101 if ( !::CloseHandle(m_hFile) )
102 {
103 wxLogSysError(_("Failed to close file handle"));
104 }
105 }
106 }
107
108 // return TRUE only if the file could be opened successfully
109 bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; }
110
111 // get the handle
112 operator HANDLE() const { return m_hFile; }
113
114 private:
115 HANDLE m_hFile;
116 };
117
118 #endif // __WIN32__
119
120 // ----------------------------------------------------------------------------
121 // private functions
122 // ----------------------------------------------------------------------------
123
124 #ifdef __WIN32__
125
126 // convert between wxDateTime and FILETIME which is a 64-bit value representing
127 // the number of 100-nanosecond intervals since January 1, 1601.
128
129 // the number of milliseconds between the Unix Epoch (January 1, 1970) and the
130 // FILETIME reference point (January 1, 1601)
131 static const wxLongLong FILETIME_EPOCH_OFFSET = wxLongLong(0xa97, 0x30b66800);
132
133 static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft)
134 {
135 wxLongLong ll(ft.dwHighDateTime, ft.dwLowDateTime);
136
137 // convert 100ns to ms
138 ll /= 10000;
139
140 // move it to our Epoch
141 ll -= FILETIME_EPOCH_OFFSET;
142
143 *dt = wxDateTime(ll);
144 }
145
146 static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt)
147 {
148 // do the reverse of ConvertFileTimeToWx()
149 wxLongLong ll = dt.GetValue();
150 ll *= 10000;
151 ll += FILETIME_EPOCH_OFFSET;
152
153 ft->dwHighDateTime = ll.GetHi();
154 ft->dwLowDateTime = ll.GetLo();
155 }
156
157 #endif // __WIN32__
158
159 // ============================================================================
160 // implementation
161 // ============================================================================
162
163 // ----------------------------------------------------------------------------
164 // wxFileName construction
165 // ----------------------------------------------------------------------------
166
167 void wxFileName::Assign( const wxFileName &filepath )
168 {
169 m_ext = filepath.GetExt();
170 m_name = filepath.GetName();
171 m_dirs = filepath.GetDirs();
172 }
173
174 void wxFileName::Assign( const wxString& path,
175 const wxString& name,
176 const wxString& ext,
177 wxPathFormat format )
178 {
179 wxStringTokenizer tn(path, GetPathSeparators(format),
180 wxTOKEN_RET_EMPTY_ALL);
181 int i = 0;
182 m_dirs.Clear();
183 while ( tn.HasMoreTokens() )
184 {
185 wxString token = tn.GetNextToken();
186
187 // If the path starts with a slash (or two for a network path),
188 // we need the first dir entry to be an empty for later reassembly.
189 if ((i < 2) || !token.IsEmpty())
190 m_dirs.Add( token );
191
192 i ++;
193 }
194
195 m_ext = ext;
196 m_name = name;
197 }
198
199 void wxFileName::Assign(const wxString& fullpath,
200 wxPathFormat format)
201 {
202 wxString path, name, ext;
203 SplitPath(fullpath, &path, &name, &ext, format);
204
205 Assign(path, name, ext, format);
206 }
207
208 void wxFileName::Assign(const wxString& path,
209 const wxString& fullname,
210 wxPathFormat format)
211 {
212 wxString name, ext;
213 SplitPath(fullname, NULL /* no path */, &name, &ext, format);
214
215 Assign(path, name, ext, format);
216 }
217
218 void wxFileName::Clear()
219 {
220 m_dirs.Clear();
221 m_name =
222 m_ext = wxEmptyString;
223 }
224
225 /* static */
226 wxFileName wxFileName::FileName(const wxString& file)
227 {
228 return wxFileName(file);
229 }
230
231 /* static */
232 wxFileName wxFileName::DirName(const wxString& dir)
233 {
234 wxFileName fn;
235 fn.AssignDir(dir);
236 return fn;
237 }
238
239 // ----------------------------------------------------------------------------
240 // existence tests
241 // ----------------------------------------------------------------------------
242
243 bool wxFileName::FileExists()
244 {
245 return wxFileName::FileExists( GetFullPath() );
246 }
247
248 bool wxFileName::FileExists( const wxString &file )
249 {
250 return ::wxFileExists( file );
251 }
252
253 bool wxFileName::DirExists()
254 {
255 return wxFileName::DirExists( GetFullPath() );
256 }
257
258 bool wxFileName::DirExists( const wxString &dir )
259 {
260 return ::wxDirExists( dir );
261 }
262
263 // ----------------------------------------------------------------------------
264 // CWD and HOME stuff
265 // ----------------------------------------------------------------------------
266
267 void wxFileName::AssignCwd()
268 {
269 AssignDir(wxFileName::GetCwd());
270 }
271
272 /* static */
273 wxString wxFileName::GetCwd()
274 {
275 return ::wxGetCwd();
276 }
277
278 bool wxFileName::SetCwd()
279 {
280 return wxFileName::SetCwd( GetFullPath() );
281 }
282
283 bool wxFileName::SetCwd( const wxString &cwd )
284 {
285 return ::wxSetWorkingDirectory( cwd );
286 }
287
288 void wxFileName::AssignHomeDir()
289 {
290 AssignDir(wxFileName::GetHomeDir());
291 }
292
293 wxString wxFileName::GetHomeDir()
294 {
295 return ::wxGetHomeDir();
296 }
297
298 void wxFileName::AssignTempFileName( const wxString &prefix )
299 {
300 wxString fullname;
301 if ( wxGetTempFileName(prefix, fullname) )
302 {
303 Assign(fullname);
304 }
305 else // error
306 {
307 Clear();
308 }
309 }
310
311 // ----------------------------------------------------------------------------
312 // directory operations
313 // ----------------------------------------------------------------------------
314
315 bool wxFileName::Mkdir( int perm, bool full )
316 {
317 return wxFileName::Mkdir( GetFullPath(), perm, full );
318 }
319
320 bool wxFileName::Mkdir( const wxString &dir, int perm, bool full )
321 {
322 if (full)
323 {
324 wxFileName filename(dir);
325 wxArrayString dirs = filename.GetDirs();
326 dirs.Add(filename.GetName());
327
328 size_t count = dirs.GetCount();
329 size_t i;
330 wxString currPath;
331 int noErrors = 0;
332 for ( i = 0; i < count; i++ )
333 {
334 currPath += dirs[i];
335
336 if (currPath.Last() == wxT(':'))
337 {
338 // Can't create a root directory so continue to next dir
339 currPath += wxFILE_SEP_PATH;
340 continue;
341 }
342
343 if (!DirExists(currPath))
344 if (!wxMkdir(currPath, perm))
345 noErrors ++;
346
347 if ( (i < (count-1)) )
348 currPath += wxFILE_SEP_PATH;
349 }
350
351 return (noErrors == 0);
352
353 }
354 else
355 return ::wxMkdir( dir, perm );
356 }
357
358 bool wxFileName::Rmdir()
359 {
360 return wxFileName::Rmdir( GetFullPath() );
361 }
362
363 bool wxFileName::Rmdir( const wxString &dir )
364 {
365 return ::wxRmdir( dir );
366 }
367
368 // ----------------------------------------------------------------------------
369 // path normalization
370 // ----------------------------------------------------------------------------
371
372 bool wxFileName::Normalize(wxPathNormalize flags,
373 const wxString& cwd,
374 wxPathFormat format)
375 {
376 // the existing path components
377 wxArrayString dirs = GetDirs();
378
379 // the path to prepend in front to make the path absolute
380 wxFileName curDir;
381
382 format = GetFormat(format);
383
384 // make the path absolute
385 if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute() )
386 {
387 if ( cwd.empty() )
388 curDir.AssignCwd();
389 else
390 curDir.AssignDir(cwd);
391 }
392
393 // handle ~ stuff under Unix only
394 if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
395 {
396 if ( !dirs.IsEmpty() )
397 {
398 wxString dir = dirs[0u];
399 if ( !dir.empty() && dir[0u] == _T('~') )
400 {
401 curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
402
403 dirs.RemoveAt(0u);
404 }
405 }
406 }
407
408 if ( curDir.IsOk() )
409 {
410 wxArrayString dirsNew = curDir.GetDirs();
411 size_t count = dirs.GetCount();
412 for ( size_t n = 0; n < count; n++ )
413 {
414 dirsNew.Add(dirs[n]);
415 }
416
417 dirs = dirsNew;
418 }
419
420 // now deal with ".", ".." and the rest
421 m_dirs.Empty();
422 size_t count = dirs.GetCount();
423 for ( size_t n = 0; n < count; n++ )
424 {
425 wxString dir = dirs[n];
426
427 if ( flags && wxPATH_NORM_DOTS )
428 {
429 if ( dir == wxT(".") )
430 {
431 // just ignore
432 continue;
433 }
434
435 if ( dir == wxT("..") )
436 {
437 if ( m_dirs.IsEmpty() )
438 {
439 wxLogError(_("The path '%s' contains too many \"..\"!"),
440 GetFullPath().c_str());
441 return FALSE;
442 }
443
444 m_dirs.Remove(m_dirs.GetCount() - 1);
445 continue;
446 }
447 }
448
449 if ( flags & wxPATH_NORM_ENV_VARS )
450 {
451 dir = wxExpandEnvVars(dir);
452 }
453
454 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
455 {
456 dir.MakeLower();
457 }
458
459 m_dirs.Add(dir);
460 }
461
462 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
463 {
464 // VZ: expand env vars here too?
465
466 m_name.MakeLower();
467 m_ext.MakeLower();
468 }
469
470 #if defined(__WXMSW__) && defined(__WIN32__)
471 if (flags & wxPATH_NORM_LONG)
472 {
473 Assign(GetLongPath());
474 }
475 #endif
476
477 return TRUE;
478 }
479
480 // ----------------------------------------------------------------------------
481 // filename kind tests
482 // ----------------------------------------------------------------------------
483
484 bool wxFileName::SameAs( const wxFileName &filepath, wxPathFormat format)
485 {
486 wxFileName fn1 = *this,
487 fn2 = filepath;
488
489 // get cwd only once - small time saving
490 wxString cwd = wxGetCwd();
491 fn1.Normalize(wxPATH_NORM_ALL, cwd, format);
492 fn2.Normalize(wxPATH_NORM_ALL, cwd, format);
493
494 if ( fn1.GetFullPath() == fn2.GetFullPath() )
495 return TRUE;
496
497 // TODO: compare inodes for Unix, this works even when filenames are
498 // different but files are the same (symlinks) (VZ)
499
500 return FALSE;
501 }
502
503 /* static */
504 bool wxFileName::IsCaseSensitive( wxPathFormat format )
505 {
506 // only DOS and OpenVMS filenames are case-sensitive
507 return GetFormat(format) != wxPATH_DOS && GetFormat(format) != wxPATH_VMS;
508 }
509
510 bool wxFileName::IsRelative( wxPathFormat format )
511 {
512 return !IsAbsolute(format);
513 }
514
515 bool wxFileName::IsAbsolute( wxPathFormat format )
516 {
517 wxChar ch = m_dirs.IsEmpty() ? _T('\0') : m_dirs[0u][0u];
518
519 // Hack to cope with e.g. c:\thing - need something better
520 wxChar driveSep = _T('\0');
521 if (!m_dirs.IsEmpty() && m_dirs[0].Length() > 1)
522 driveSep = m_dirs[0u][1u];
523
524 // the path is absolute if it starts with a path separator or, only for
525 // Unix filenames, with "~" or "~user"
526 return IsPathSeparator(ch, format) ||
527 driveSep == _T(':') ||
528 (GetFormat(format) == wxPATH_UNIX && ch == _T('~') );
529 }
530
531 /* static */
532 wxString wxFileName::GetPathSeparators(wxPathFormat format)
533 {
534 wxString seps;
535 switch ( GetFormat(format) )
536 {
537 case wxPATH_DOS:
538 // accept both as native APIs do
539 seps << wxFILE_SEP_PATH_UNIX << wxFILE_SEP_PATH_DOS;
540 break;
541
542 default:
543 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
544 // fall through
545
546 case wxPATH_UNIX:
547 seps = wxFILE_SEP_PATH_UNIX;
548 break;
549
550 case wxPATH_MAC:
551 seps = wxFILE_SEP_PATH_MAC;
552 break;
553
554 case wxPATH_VMS:
555 seps = wxFILE_SEP_PATH_VMS;
556 break;
557 }
558
559 return seps;
560 }
561
562 /* static */
563 bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format)
564 {
565 return GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
566 }
567
568 bool wxFileName::IsWild( wxPathFormat format )
569 {
570 // FIXME: this is probably false for Mac and this is surely wrong for most
571 // of Unix shells (think about "[...]")
572 (void)format;
573 return m_name.find_first_of(_T("*?")) != wxString::npos;
574 }
575
576 // ----------------------------------------------------------------------------
577 // path components manipulation
578 // ----------------------------------------------------------------------------
579
580 void wxFileName::AppendDir( const wxString &dir )
581 {
582 m_dirs.Add( dir );
583 }
584
585 void wxFileName::PrependDir( const wxString &dir )
586 {
587 m_dirs.Insert( dir, 0 );
588 }
589
590 void wxFileName::InsertDir( int before, const wxString &dir )
591 {
592 m_dirs.Insert( dir, before );
593 }
594
595 void wxFileName::RemoveDir( int pos )
596 {
597 m_dirs.Remove( (size_t)pos );
598 }
599
600 // ----------------------------------------------------------------------------
601 // accessors
602 // ----------------------------------------------------------------------------
603
604 void wxFileName::SetFullName(const wxString& fullname)
605 {
606 SplitPath(fullname, NULL /* no path */, &m_name, &m_ext);
607 }
608
609 wxString wxFileName::GetFullName() const
610 {
611 wxString fullname = m_name;
612 if ( !m_ext.empty() )
613 {
614 fullname << wxFILE_SEP_EXT << m_ext;
615 }
616
617 return fullname;
618 }
619
620 wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const
621 {
622 format = GetFormat( format );
623
624 wxString ret;
625 size_t count = m_dirs.GetCount();
626 for ( size_t i = 0; i < count; i++ )
627 {
628 ret += m_dirs[i];
629 if ( add_separator || (i < count) )
630 ret += wxFILE_SEP_PATH;
631 }
632
633 return ret;
634 }
635
636 wxString wxFileName::GetFullPath( wxPathFormat format ) const
637 {
638 format = GetFormat( format );
639
640 wxString ret;
641 if (format == wxPATH_DOS)
642 {
643 for (size_t i = 0; i < m_dirs.GetCount(); i++)
644 {
645 ret += m_dirs[i];
646 ret += '\\';
647 }
648 }
649 else
650 if (format == wxPATH_UNIX)
651 {
652 for (size_t i = 0; i < m_dirs.GetCount(); i++)
653 {
654 ret += m_dirs[i];
655 ret += '/';
656 }
657 }
658 else
659 if (format == wxPATH_VMS)
660 {
661 ret += '[';
662 for (size_t i = 0; i < m_dirs.GetCount(); i++)
663 {
664 ret += '.';
665 ret += m_dirs[i];
666 }
667 ret += ']';
668 }
669 else
670 {
671 for (size_t i = 0; i < m_dirs.GetCount(); i++)
672 {
673 ret += m_dirs[i];
674 ret += ':';
675 }
676 }
677
678 ret += m_name;
679
680 if (!m_ext.IsEmpty())
681 {
682 ret += '.';
683 ret += m_ext;
684 }
685
686 return ret;
687 }
688
689 // Return the short form of the path (returns identity on non-Windows platforms)
690 wxString wxFileName::GetShortPath() const
691 {
692 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
693 wxString path(GetFullPath());
694 wxString pathOut;
695 DWORD sz = ::GetShortPathName(path, NULL, 0);
696 bool ok = sz != 0;
697 if ( ok )
698 {
699 ok = ::GetShortPathName
700 (
701 path,
702 pathOut.GetWriteBuf(sz),
703 sz
704 ) != 0;
705 pathOut.UngetWriteBuf();
706 }
707 if (ok)
708 return pathOut;
709
710 return path;
711 #else
712 return GetFullPath();
713 #endif
714 }
715
716 // Return the long form of the path (returns identity on non-Windows platforms)
717 wxString wxFileName::GetLongPath() const
718 {
719 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
720 wxString path(GetFullPath());
721 wxString pathOut;
722 bool success = FALSE;
723
724 // VZ: this code was disabled, why?
725 #if 0 // wxUSE_DYNLIB_CLASS
726 typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
727
728 static bool s_triedToLoad = FALSE;
729
730 if ( !s_triedToLoad )
731 {
732 s_triedToLoad = TRUE;
733 wxDllType dllKernel = wxDllLoader::LoadLibrary(_T("kernel32"));
734 if ( dllKernel )
735 {
736 // may succeed or fail depending on the Windows version
737 static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
738 #ifdef _UNICODE
739 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameW"));
740 #else
741 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameA"));
742 #endif
743
744 wxDllLoader::UnloadLibrary(dllKernel);
745
746 if ( s_pfnGetLongPathName )
747 {
748 DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0);
749 bool ok = dwSize > 0;
750
751 if ( ok )
752 {
753 DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0);
754 ok = sz != 0;
755 if ( ok )
756 {
757 ok = (*s_pfnGetLongPathName)
758 (
759 path,
760 pathOut.GetWriteBuf(sz),
761 sz
762 ) != 0;
763 pathOut.UngetWriteBuf();
764
765 success = TRUE;
766 }
767 }
768 }
769 }
770 }
771 if (success)
772 return pathOut;
773 #endif // wxUSE_DYNLIB_CLASS
774
775 if (!success)
776 {
777 // The OS didn't support GetLongPathName, or some other error.
778 // We need to call FindFirstFile on each component in turn.
779
780 WIN32_FIND_DATA findFileData;
781 HANDLE hFind;
782 pathOut = wxEmptyString;
783
784 wxArrayString dirs = GetDirs();
785 dirs.Add(GetFullName());
786
787 size_t count = dirs.GetCount();
788 size_t i;
789 wxString tmpPath;
790
791 for ( i = 0; i < count; i++ )
792 {
793 // We're using pathOut to collect the long-name path,
794 // but using a temporary for appending the last path component which may be short-name
795 tmpPath = pathOut + dirs[i];
796
797 if (tmpPath.Last() == wxT(':'))
798 {
799 // Can't pass a drive and root dir to FindFirstFile,
800 // so continue to next dir
801 tmpPath += wxFILE_SEP_PATH;
802 pathOut = tmpPath;
803 continue;
804 }
805
806 hFind = ::FindFirstFile(tmpPath, &findFileData);
807 if (hFind == INVALID_HANDLE_VALUE)
808 {
809 // Error: return immediately with the original path
810 return path;
811 }
812 else
813 {
814 pathOut += findFileData.cFileName;
815 if ( (i < (count-1)) )
816 pathOut += wxFILE_SEP_PATH;
817
818 ::FindClose(hFind);
819 }
820 }
821 }
822
823 return pathOut;
824 #else
825 return GetFullPath();
826 #endif
827 }
828
829 wxPathFormat wxFileName::GetFormat( wxPathFormat format )
830 {
831 if (format == wxPATH_NATIVE)
832 {
833 #if defined(__WXMSW__) || defined(__WXPM__)
834 format = wxPATH_DOS;
835 #elif defined(__WXMAC__) && !defined(__DARWIN__)
836 format = wxPATH_MAC;
837 #elif defined(__VMS)
838 format = wxPATH_VMS;
839 #else
840 format = wxPATH_UNIX;
841 #endif
842 }
843 return format;
844 }
845
846 // ----------------------------------------------------------------------------
847 // path splitting function
848 // ----------------------------------------------------------------------------
849
850 void wxFileName::SplitPath(const wxString& fullpath,
851 wxString *pstrPath,
852 wxString *pstrName,
853 wxString *pstrExt,
854 wxPathFormat format)
855 {
856 format = GetFormat(format);
857
858 // find the positions of the last dot and last path separator in the path
859 size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT);
860 size_t posLastSlash = fullpath.find_last_of(GetPathSeparators(format));
861
862 if ( (posLastDot != wxString::npos) && (format == wxPATH_UNIX) )
863 {
864 if ( (posLastDot == 0) ||
865 (fullpath[posLastDot - 1] == wxFILE_SEP_PATH_UNIX) )
866 {
867 // under Unix, dot may be (and commonly is) the first character of
868 // the filename, don't treat the entire filename as extension in
869 // this case
870 posLastDot = wxString::npos;
871 }
872 }
873 else
874 if ( (posLastDot != wxString::npos) && (format == wxPATH_VMS) )
875 {
876 if ( (posLastDot == 0) ||
877 (fullpath[posLastDot - 1] == ']' ) )
878 {
879 // under OpenVMS, dot may be (and commonly is) the first character of
880 // the filename, don't treat the entire filename as extension in
881 // this case
882 posLastDot = wxString::npos;
883 }
884 }
885
886 // if we do have a dot and a slash, check that the dot is in the name part
887 if ( (posLastDot != wxString::npos) &&
888 (posLastSlash != wxString::npos) &&
889 (posLastDot < posLastSlash) )
890 {
891 // the dot is part of the path, not the start of the extension
892 posLastDot = wxString::npos;
893 }
894
895 // now fill in the variables provided by user
896 if ( pstrPath )
897 {
898 if ( posLastSlash == wxString::npos )
899 {
900 // no path at all
901 pstrPath->Empty();
902 }
903 else
904 {
905 // take all until the separator
906 *pstrPath = fullpath.Left(posLastSlash);
907 }
908 }
909
910 if ( pstrName )
911 {
912 // take all characters starting from the one after the last slash and
913 // up to, but excluding, the last dot
914 size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
915 size_t count;
916 if ( posLastDot == wxString::npos )
917 {
918 // take all until the end
919 count = wxString::npos;
920 }
921 else if ( posLastSlash == wxString::npos )
922 {
923 count = posLastDot;
924 }
925 else // have both dot and slash
926 {
927 count = posLastDot - posLastSlash - 1;
928 }
929
930 *pstrName = fullpath.Mid(nStart, count);
931 }
932
933 if ( pstrExt )
934 {
935 if ( posLastDot == wxString::npos )
936 {
937 // no extension
938 pstrExt->Empty();
939 }
940 else
941 {
942 // take everything after the dot
943 *pstrExt = fullpath.Mid(posLastDot + 1);
944 }
945 }
946 }
947
948 // ----------------------------------------------------------------------------
949 // time functions
950 // ----------------------------------------------------------------------------
951
952 bool wxFileName::SetTimes(const wxDateTime *dtCreate,
953 const wxDateTime *dtAccess,
954 const wxDateTime *dtMod)
955 {
956 #if defined(__UNIX_LIKE__)
957 if ( !dtAccess && !dtMod )
958 {
959 // can't modify the creation time anyhow, don't try
960 return TRUE;
961 }
962
963 // if dtAccess or dtMod is not specified, use the other one (which must be
964 // non NULL because of the test above) for both times
965 utimbuf utm;
966 utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks();
967 utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks();
968 if ( utime(GetFullPath(), &utm) == 0 )
969 {
970 return TRUE;
971 }
972 #elif defined(__WIN32__)
973 wxFileHandle fh(GetFullPath());
974 if ( fh.IsOk() )
975 {
976 FILETIME ftAccess, ftCreate, ftWrite;
977
978 if ( dtCreate )
979 ConvertWxToFileTime(&ftCreate, *dtCreate);
980 if ( dtAccess )
981 ConvertWxToFileTime(&ftAccess, *dtAccess);
982 if ( dtMod )
983 ConvertWxToFileTime(&ftWrite, *dtMod);
984
985 if ( ::SetFileTime(fh,
986 dtCreate ? &ftCreate : NULL,
987 dtAccess ? &ftAccess : NULL,
988 dtMod ? &ftWrite : NULL) )
989 {
990 return TRUE;
991 }
992 }
993 #else // other platform
994 #endif // platforms
995
996 wxLogSysError(_("Failed to modify file times for '%s'"),
997 GetFullPath().c_str());
998
999 return FALSE;
1000 }
1001
1002 bool wxFileName::Touch()
1003 {
1004 #if defined(__UNIX_LIKE__)
1005 // under Unix touching file is simple: just pass NULL to utime()
1006 if ( utime(GetFullPath(), NULL) == 0 )
1007 {
1008 return TRUE;
1009 }
1010
1011 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1012
1013 return FALSE;
1014 #else // other platform
1015 wxDateTime dtNow = wxDateTime::Now();
1016
1017 return SetTimes(NULL /* don't change create time */, &dtNow, &dtNow);
1018 #endif // platforms
1019 }
1020
1021 bool wxFileName::GetTimes(wxDateTime *dtAccess,
1022 wxDateTime *dtMod,
1023 wxDateTime *dtChange) const
1024 {
1025 #if defined(__UNIX_LIKE__)
1026 wxStructStat stBuf;
1027 if ( wxStat(GetFullPath(), &stBuf) == 0 )
1028 {
1029 if ( dtAccess )
1030 dtAccess->Set(stBuf.st_atime);
1031 if ( dtMod )
1032 dtMod->Set(stBuf.st_mtime);
1033 if ( dtChange )
1034 dtChange->Set(stBuf.st_ctime);
1035
1036 return TRUE;
1037 }
1038 #elif defined(__WXMAC__)
1039 wxStructStat stBuf;
1040 if ( wxStat(GetFullPath(), &stBuf) == 0 )
1041 {
1042 if ( dtAccess )
1043 dtAccess->Set(stBuf.st_atime);
1044 if ( dtMod )
1045 dtMod->Set(stBuf.st_mtime);
1046 if ( dtChange )
1047 dtChange->Set(stBuf.st_ctime);
1048
1049 return TRUE;
1050 }
1051 #elif defined(__WIN32__)
1052 wxFileHandle fh(GetFullPath());
1053 if ( fh.IsOk() )
1054 {
1055 FILETIME ftAccess, ftCreate, ftWrite;
1056
1057 if ( ::GetFileTime(fh,
1058 dtMod ? &ftCreate : NULL,
1059 dtAccess ? &ftAccess : NULL,
1060 dtChange ? &ftWrite : NULL) )
1061 {
1062 if ( dtMod )
1063 ConvertFileTimeToWx(dtMod, ftCreate);
1064 if ( dtAccess )
1065 ConvertFileTimeToWx(dtAccess, ftAccess);
1066 if ( dtChange )
1067 ConvertFileTimeToWx(dtChange, ftWrite);
1068
1069 return TRUE;
1070 }
1071 }
1072 #else // other platform
1073 #endif // platforms
1074
1075 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1076 GetFullPath().c_str());
1077
1078 return FALSE;
1079 }
1080