1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/filename.cpp
3 // Purpose: wxFileName - encapsulates a file path
4 // Author: Robert Roebling, Vadim Zeitlin
8 // Copyright: (c) 2000 Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 Here are brief descriptions of the filename formats supported by this class:
15 wxPATH_UNIX: standard Unix format, absolute file names have the form
16 /dir1/dir2/.../dirN/filename, "." and ".." stand for the
17 current and parent directory respectively, "~" is parsed as the
18 user HOME and "~username" as the HOME of that user
20 wxPATH_DOS: DOS/Windows format, absolute file names have the form
21 drive:\dir1\dir2\...\dirN\filename.ext where drive is a single
22 letter. "." and ".." as for Unix but no "~".
24 There are also UNC names of the form \\share\fullpath
26 wxPATH_MAC: Mac OS 8/9 format, absolute file names have the form
27 volume:dir1:...:dirN:filename
28 and the relative file names are either
29 :dir1:...:dirN:filename
32 (although :filename works as well).
34 wxPATH_VMS: VMS native format, absolute file names have the form
35 <device>:[dir1.dir2.dir3]file.txt
37 <device>:[000000.dir1.dir2.dir3]file.txt
39 the <device> is the physical device (i.e. disk). 000000 is the
40 root directory on the device which can be omitted.
42 Note that VMS uses different separators unlike Unix:
43 : always after the device. If the path does not contain : than
44 the default (the device of the current directory) is assumed.
45 [ start of directory specyfication
46 . separator between directory and subdirectory
47 ] between directory and file
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
59 #pragma implementation "filename.h"
62 // For compilers that support precompilation, includes "wx.h".
63 #include "wx/wxprec.h"
74 #include "wx/filename.h"
75 #include "wx/tokenzr.h"
76 #include "wx/config.h" // for wxExpandEnvVars
79 #if wxUSE_DYNLIB_CLASS
80 #include "wx/dynlib.h"
83 // For GetShort/LongPathName
87 #include "wx/msw/winundef.h"
90 // utime() is POSIX so should normally be available on all Unices
92 #include <sys/types.h>
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 // small helper class which opens and closes the file - we use it just to get
109 // a file handle for the given file name to pass it to some Win32 API function
110 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
115 wxFileHandle(const wxString
& filename
)
117 m_hFile
= ::CreateFile
120 GENERIC_READ
, // access mask
122 NULL
, // no secutity attr
123 OPEN_EXISTING
, // creation disposition
125 NULL
// no template file
128 if ( m_hFile
== INVALID_HANDLE_VALUE
)
130 wxLogSysError(_("Failed to open '%s' for reading"),
137 if ( m_hFile
!= INVALID_HANDLE_VALUE
)
139 if ( !::CloseHandle(m_hFile
) )
141 wxLogSysError(_("Failed to close file handle"));
146 // return TRUE only if the file could be opened successfully
147 bool IsOk() const { return m_hFile
!= INVALID_HANDLE_VALUE
; }
150 operator HANDLE() const { return m_hFile
; }
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
164 // convert between wxDateTime and FILETIME which is a 64-bit value representing
165 // the number of 100-nanosecond intervals since January 1, 1601.
167 // the number of milliseconds between the Unix Epoch (January 1, 1970) and the
168 // FILETIME reference point (January 1, 1601)
169 static const wxLongLong FILETIME_EPOCH_OFFSET
= wxLongLong(0xa97, 0x30b66800);
171 static void ConvertFileTimeToWx(wxDateTime
*dt
, const FILETIME
&ft
)
173 wxLongLong
ll(ft
.dwHighDateTime
, ft
.dwLowDateTime
);
175 // convert 100ns to ms
178 // move it to our Epoch
179 ll
-= FILETIME_EPOCH_OFFSET
;
181 *dt
= wxDateTime(ll
);
184 static void ConvertWxToFileTime(FILETIME
*ft
, const wxDateTime
& dt
)
186 // do the reverse of ConvertFileTimeToWx()
187 wxLongLong ll
= dt
.GetValue();
189 ll
+= FILETIME_EPOCH_OFFSET
;
191 ft
->dwHighDateTime
= ll
.GetHi();
192 ft
->dwLowDateTime
= ll
.GetLo();
197 // ============================================================================
199 // ============================================================================
201 // ----------------------------------------------------------------------------
202 // wxFileName construction
203 // ----------------------------------------------------------------------------
205 void wxFileName::Assign( const wxFileName
&filepath
)
207 m_volume
= filepath
.GetVolume();
208 m_dirs
= filepath
.GetDirs();
209 m_name
= filepath
.GetName();
210 m_ext
= filepath
.GetExt();
213 void wxFileName::Assign(const wxString
& volume
,
214 const wxString
& path
,
215 const wxString
& name
,
217 wxPathFormat format
)
219 wxStringTokenizer
tn(path
, GetPathSeparators(format
));
222 while ( tn
.HasMoreTokens() )
224 wxString token
= tn
.GetNextToken();
226 // if the path starts with a slash, we do need the first empty dir
227 // entry to be able to tell later that it was an absolute path, but
228 // otherwise ignore the double slashes
229 if ( m_dirs
.IsEmpty() || !token
.IsEmpty() )
238 void wxFileName::Assign(const wxString
& fullpath
,
241 wxString volume
, path
, name
, ext
;
242 SplitPath(fullpath
, &volume
, &path
, &name
, &ext
, format
);
244 Assign(volume
, path
, name
, ext
, format
);
247 void wxFileName::Assign(const wxString
& fullpath
,
248 const wxString
& fullname
,
251 wxString volume
, path
, name
, ext
;
252 SplitPath(fullname
, NULL
/* no path */, &name
, &ext
, format
);
253 SplitPath(fullpath
, &volume
, &path
, NULL
, NULL
, format
);
255 Assign(volume
, path
, name
, ext
, format
);
258 void wxFileName::AssignDir(const wxString
& dir
, wxPathFormat format
)
260 Assign(dir
, _T(""), format
);
263 void wxFileName::Clear()
269 m_ext
= wxEmptyString
;
273 wxFileName
wxFileName::FileName(const wxString
& file
)
275 return wxFileName(file
);
279 wxFileName
wxFileName::DirName(const wxString
& dir
)
286 // ----------------------------------------------------------------------------
288 // ----------------------------------------------------------------------------
290 bool wxFileName::FileExists()
292 return wxFileName::FileExists( GetFullPath() );
295 bool wxFileName::FileExists( const wxString
&file
)
297 return ::wxFileExists( file
);
300 bool wxFileName::DirExists()
302 return wxFileName::DirExists( GetFullPath() );
305 bool wxFileName::DirExists( const wxString
&dir
)
307 return ::wxDirExists( dir
);
310 // ----------------------------------------------------------------------------
311 // CWD and HOME stuff
312 // ----------------------------------------------------------------------------
314 void wxFileName::AssignCwd()
316 AssignDir(wxFileName::GetCwd());
320 wxString
wxFileName::GetCwd()
325 bool wxFileName::SetCwd()
327 return wxFileName::SetCwd( GetFullPath() );
330 bool wxFileName::SetCwd( const wxString
&cwd
)
332 return ::wxSetWorkingDirectory( cwd
);
335 void wxFileName::AssignHomeDir()
337 AssignDir(wxFileName::GetHomeDir());
340 wxString
wxFileName::GetHomeDir()
342 return ::wxGetHomeDir();
345 void wxFileName::AssignTempFileName( const wxString
&prefix
)
348 if ( wxGetTempFileName(prefix
, fullname
) )
358 // ----------------------------------------------------------------------------
359 // directory operations
360 // ----------------------------------------------------------------------------
362 bool wxFileName::Mkdir( int perm
, bool full
)
364 return wxFileName::Mkdir( GetFullPath(), perm
, full
);
367 bool wxFileName::Mkdir( const wxString
&dir
, int perm
, bool full
)
371 wxFileName
filename(dir
);
372 wxArrayString dirs
= filename
.GetDirs();
373 dirs
.Add(filename
.GetName());
375 size_t count
= dirs
.GetCount();
379 for ( i
= 0; i
< count
; i
++ )
383 if (currPath
.Last() == wxT(':'))
385 // Can't create a root directory so continue to next dir
386 currPath
+= wxFILE_SEP_PATH
;
390 if (!DirExists(currPath
))
391 if (!wxMkdir(currPath
, perm
))
394 if ( (i
< (count
-1)) )
395 currPath
+= wxFILE_SEP_PATH
;
398 return (noErrors
== 0);
402 return ::wxMkdir( dir
, perm
);
405 bool wxFileName::Rmdir()
407 return wxFileName::Rmdir( GetFullPath() );
410 bool wxFileName::Rmdir( const wxString
&dir
)
412 return ::wxRmdir( dir
);
415 // ----------------------------------------------------------------------------
416 // path normalization
417 // ----------------------------------------------------------------------------
419 bool wxFileName::Normalize(wxPathNormalize flags
,
423 // the existing path components
424 wxArrayString dirs
= GetDirs();
426 // the path to prepend in front to make the path absolute
429 format
= GetFormat(format
);
431 // make the path absolute
432 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
437 curDir
.AssignDir(cwd
);
439 // the path may be not absolute because it doesn't have the volume name
440 // but in this case we shouldn't modify the directory components of it
441 // but just set the current volume
442 if ( !HasVolume() && curDir
.HasVolume() )
444 SetVolume(curDir
.GetVolume());
448 // yes, it was the case - we don't need curDir then
454 // handle ~ stuff under Unix only
455 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
457 if ( !dirs
.IsEmpty() )
459 wxString dir
= dirs
[0u];
460 if ( !dir
.empty() && dir
[0u] == _T('~') )
462 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
469 // transform relative path into abs one
472 wxArrayString dirsNew
= curDir
.GetDirs();
473 size_t count
= dirs
.GetCount();
474 for ( size_t n
= 0; n
< count
; n
++ )
476 dirsNew
.Add(dirs
[n
]);
482 // now deal with ".", ".." and the rest
484 size_t count
= dirs
.GetCount();
485 for ( size_t n
= 0; n
< count
; n
++ )
487 wxString dir
= dirs
[n
];
489 if ( flags
&& wxPATH_NORM_DOTS
)
491 if ( dir
== wxT(".") )
497 if ( dir
== wxT("..") )
499 if ( m_dirs
.IsEmpty() )
501 wxLogError(_("The path '%s' contains too many \"..\"!"),
502 GetFullPath().c_str());
506 m_dirs
.Remove(m_dirs
.GetCount() - 1);
511 if ( flags
& wxPATH_NORM_ENV_VARS
)
513 dir
= wxExpandEnvVars(dir
);
516 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
524 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
526 // VZ: expand env vars here too?
532 #if defined(__WIN32__)
533 if ( (flags
& wxPATH_NORM_LONG
) && (format
== wxPATH_DOS
) )
535 Assign(GetLongPath());
542 // ----------------------------------------------------------------------------
543 // filename kind tests
544 // ----------------------------------------------------------------------------
546 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
548 wxFileName fn1
= *this,
551 // get cwd only once - small time saving
552 wxString cwd
= wxGetCwd();
553 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
554 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
556 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
559 // TODO: compare inodes for Unix, this works even when filenames are
560 // different but files are the same (symlinks) (VZ)
566 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
568 // only Unix filenames are truely case-sensitive
569 return GetFormat(format
) == wxPATH_UNIX
;
572 bool wxFileName::IsAbsolute( wxPathFormat format
)
574 // if we have no path, we can't be an abs filename
575 if ( m_dirs
.IsEmpty() )
580 format
= GetFormat(format
);
582 if ( format
== wxPATH_UNIX
)
584 const wxString
& str
= m_dirs
[0u];
587 // the path started with '/', it's an absolute one
591 // the path is absolute if it starts with a path separator or
592 // with "~" or "~user"
595 return IsPathSeparator(ch
, format
) || ch
== _T('~');
599 // must have the drive
600 if ( m_volume
.empty() )
606 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
610 return m_dirs
[0u].empty();
613 // TODO: what is the relative path format here?
617 return !m_dirs
[0u].empty();
623 wxString
wxFileName::GetVolumeSeparator(wxPathFormat format
)
627 if ( GetFormat(format
) != wxPATH_UNIX
)
629 // so far it is the same for all systems which have it
630 sepVol
= wxFILE_SEP_DSK
;
632 //else: leave empty, no volume separators under Unix
638 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
641 switch ( GetFormat(format
) )
644 // accept both as native APIs do but put the native one first as
645 // this is the one we use in GetFullPath()
646 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
650 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
654 seps
= wxFILE_SEP_PATH_UNIX
;
658 seps
= wxFILE_SEP_PATH_MAC
;
662 seps
= wxFILE_SEP_PATH_VMS
;
670 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
672 // wxString::Find() doesn't work as expected with NUL - it will always find
673 // it, so it is almost surely a bug if this function is called with NUL arg
674 wxASSERT_MSG( ch
!= _T('\0'), _T("shouldn't be called with NUL") );
676 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
679 bool wxFileName::IsWild( wxPathFormat format
)
681 // FIXME: this is probably false for Mac and this is surely wrong for most
682 // of Unix shells (think about "[...]")
684 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
687 // ----------------------------------------------------------------------------
688 // path components manipulation
689 // ----------------------------------------------------------------------------
691 void wxFileName::AppendDir( const wxString
&dir
)
696 void wxFileName::PrependDir( const wxString
&dir
)
698 m_dirs
.Insert( dir
, 0 );
701 void wxFileName::InsertDir( int before
, const wxString
&dir
)
703 m_dirs
.Insert( dir
, before
);
706 void wxFileName::RemoveDir( int pos
)
708 m_dirs
.Remove( (size_t)pos
);
711 // ----------------------------------------------------------------------------
713 // ----------------------------------------------------------------------------
715 void wxFileName::SetFullName(const wxString
& fullname
)
717 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
720 wxString
wxFileName::GetFullName() const
722 wxString fullname
= m_name
;
723 if ( !m_ext
.empty() )
725 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
731 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
733 format
= GetFormat( format
);
736 size_t count
= m_dirs
.GetCount();
737 for ( size_t i
= 0; i
< count
; i
++ )
740 if ( add_separator
|| (i
< count
) )
741 ret
+= wxFILE_SEP_PATH
;
747 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
749 format
= GetFormat(format
);
753 // first put the volume
754 if ( !m_volume
.empty() )
756 // special Windows UNC paths hack, part 2: undo what we did in
757 // SplitPath() and make an UNC path if we have a drive which is not a
758 // single letter (hopefully the network shares can't be one letter only
759 // although I didn't find any authoritative docs on this)
760 if ( format
== wxPATH_DOS
&& m_volume
.length() > 1 )
762 fullpath
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_DOS
<< m_volume
;
766 fullpath
<< m_volume
<< GetVolumeSeparator(format
);
770 // then concatenate all the path components using the path separator
771 size_t dirCount
= m_dirs
.GetCount();
774 // under Mac, we must have a path separator in the beginning of the
775 // relative path - otherwise it would be parsed as an absolute one
776 if ( format
== wxPATH_MAC
&& m_volume
.empty() && !m_dirs
[0].empty() )
778 fullpath
+= wxFILE_SEP_PATH_MAC
;
781 wxChar chPathSep
= GetPathSeparators(format
)[0u];
782 if ( format
== wxPATH_VMS
)
787 for ( size_t i
= 0; i
< dirCount
; i
++ )
789 // under VMS, we shouldn't have a leading dot
790 if ( i
&& (format
!= wxPATH_VMS
|| !m_dirs
[i
- 1].empty()) )
791 fullpath
+= chPathSep
;
793 fullpath
+= m_dirs
[i
];
796 if ( format
== wxPATH_VMS
)
802 // separate the file name from the last directory, notice that we
803 // intentionally do it even if the name and extension are empty as
804 // this allows us to distinguish the directories from the file
805 // names (the directories have the trailing slash)
806 fullpath
+= chPathSep
;
810 // finally add the file name and extension
811 fullpath
+= GetFullName();
816 // Return the short form of the path (returns identity on non-Windows platforms)
817 wxString
wxFileName::GetShortPath() const
819 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
820 wxString
path(GetFullPath());
822 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
826 ok
= ::GetShortPathName
829 pathOut
.GetWriteBuf(sz
),
832 pathOut
.UngetWriteBuf();
839 return GetFullPath();
843 // Return the long form of the path (returns identity on non-Windows platforms)
844 wxString
wxFileName::GetLongPath() const
847 path
= GetFullPath();
849 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
850 bool success
= FALSE
;
852 // VZ: this code was disabled, why?
853 #if 0 // wxUSE_DYNLIB_CLASS
854 typedef DWORD (*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
856 static bool s_triedToLoad
= FALSE
;
858 if ( !s_triedToLoad
)
860 s_triedToLoad
= TRUE
;
861 wxDllType dllKernel
= wxDllLoader::LoadLibrary(_T("kernel32"));
864 // may succeed or fail depending on the Windows version
865 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
867 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameW"));
869 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameA"));
872 wxDllLoader::UnloadLibrary(dllKernel
);
874 if ( s_pfnGetLongPathName
)
876 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
877 bool ok
= dwSize
> 0;
881 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
885 ok
= (*s_pfnGetLongPathName
)
888 pathOut
.GetWriteBuf(sz
),
891 pathOut
.UngetWriteBuf();
901 #endif // wxUSE_DYNLIB_CLASS
905 // The OS didn't support GetLongPathName, or some other error.
906 // We need to call FindFirstFile on each component in turn.
908 WIN32_FIND_DATA findFileData
;
910 pathOut
= wxEmptyString
;
912 wxArrayString dirs
= GetDirs();
913 dirs
.Add(GetFullName());
917 size_t count
= dirs
.GetCount();
918 for ( size_t i
= 0; i
< count
; i
++ )
920 // We're using pathOut to collect the long-name path, but using a
921 // temporary for appending the last path component which may be
923 tmpPath
= pathOut
+ dirs
[i
];
925 if ( tmpPath
.empty() )
928 if ( tmpPath
.Last() == wxT(':') )
930 // Can't pass a drive and root dir to FindFirstFile,
931 // so continue to next dir
932 tmpPath
+= wxFILE_SEP_PATH
;
937 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
938 if (hFind
== INVALID_HANDLE_VALUE
)
940 // Error: return immediately with the original path
944 pathOut
+= findFileData
.cFileName
;
945 if ( (i
< (count
-1)) )
946 pathOut
+= wxFILE_SEP_PATH
;
953 #endif // Win32/!Win32
958 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
960 if (format
== wxPATH_NATIVE
)
962 #if defined(__WXMSW__) || defined(__WXPM__)
964 #elif defined(__WXMAC__) && !defined(__DARWIN__)
969 format
= wxPATH_UNIX
;
975 // ----------------------------------------------------------------------------
976 // path splitting function
977 // ----------------------------------------------------------------------------
979 void wxFileName::SplitPath(const wxString
& fullpathWithVolume
,
980 wxString
*pstrVolume
,
986 format
= GetFormat(format
);
988 wxString fullpath
= fullpathWithVolume
;
990 // under VMS the end of the path is ']', not the path separator used to
991 // separate the components
992 wxString sepPath
= format
== wxPATH_VMS
? _T(']')
993 : GetPathSeparators(format
);
995 // special Windows UNC paths hack: transform \\share\path into share:path
996 if ( format
== wxPATH_DOS
)
998 if ( fullpath
.length() >= 4 &&
999 fullpath
[0u] == wxFILE_SEP_PATH_DOS
&&
1000 fullpath
[1u] == wxFILE_SEP_PATH_DOS
)
1002 fullpath
.erase(0, 2);
1004 size_t posFirstSlash
= fullpath
.find_first_of(sepPath
);
1005 if ( posFirstSlash
!= wxString::npos
)
1007 fullpath
[posFirstSlash
] = wxFILE_SEP_DSK
;
1009 // UNC paths are always absolute, right? (FIXME)
1010 fullpath
.insert(posFirstSlash
+ 1, wxFILE_SEP_PATH_DOS
);
1015 // do we have the volume name in the beginning?
1016 wxString sepVol
= GetVolumeSeparator(format
);
1017 if ( !sepVol
.empty() )
1019 size_t posFirstColon
= fullpath
.find_first_of(sepVol
);
1020 if ( posFirstColon
!= wxString::npos
)
1024 *pstrVolume
= fullpath
.Left(posFirstColon
);
1027 // remove the volume name and the separator from the full path
1028 fullpath
.erase(0, posFirstColon
+ sepVol
.length());
1032 // find the positions of the last dot and last path separator in the path
1033 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
1034 size_t posLastSlash
= fullpath
.find_last_of(sepPath
);
1036 if ( (posLastDot
!= wxString::npos
) &&
1037 ((format
== wxPATH_UNIX
) || (format
== wxPATH_VMS
)) )
1039 if ( (posLastDot
== 0) ||
1040 (fullpath
[posLastDot
- 1] == sepPath
[0u] ) )
1042 // under Unix and VMS, dot may be (and commonly is) the first
1043 // character of the filename, don't treat the entire filename as
1044 // extension in this case
1045 posLastDot
= wxString::npos
;
1049 // if we do have a dot and a slash, check that the dot is in the name part
1050 if ( (posLastDot
!= wxString::npos
) &&
1051 (posLastSlash
!= wxString::npos
) &&
1052 (posLastDot
< posLastSlash
) )
1054 // the dot is part of the path, not the start of the extension
1055 posLastDot
= wxString::npos
;
1058 // now fill in the variables provided by user
1061 if ( posLastSlash
== wxString::npos
)
1068 // take everything up to the path separator but take care to make
1069 // tha path equal to something like '/', not empty, for the files
1070 // immediately under root directory
1071 size_t len
= posLastSlash
;
1075 *pstrPath
= fullpath
.Left(len
);
1077 // special VMS hack: remove the initial bracket
1078 if ( format
== wxPATH_VMS
)
1080 if ( (*pstrPath
)[0u] == _T('[') )
1081 pstrPath
->erase(0, 1);
1088 // take all characters starting from the one after the last slash and
1089 // up to, but excluding, the last dot
1090 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
1092 if ( posLastDot
== wxString::npos
)
1094 // take all until the end
1095 count
= wxString::npos
;
1097 else if ( posLastSlash
== wxString::npos
)
1101 else // have both dot and slash
1103 count
= posLastDot
- posLastSlash
- 1;
1106 *pstrName
= fullpath
.Mid(nStart
, count
);
1111 if ( posLastDot
== wxString::npos
)
1118 // take everything after the dot
1119 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
1124 // ----------------------------------------------------------------------------
1126 // ----------------------------------------------------------------------------
1128 bool wxFileName::SetTimes(const wxDateTime
*dtCreate
,
1129 const wxDateTime
*dtAccess
,
1130 const wxDateTime
*dtMod
)
1132 #if defined(__UNIX_LIKE__)
1133 if ( !dtAccess
&& !dtMod
)
1135 // can't modify the creation time anyhow, don't try
1139 // if dtAccess or dtMod is not specified, use the other one (which must be
1140 // non NULL because of the test above) for both times
1142 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
1143 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
1144 if ( utime(GetFullPath(), &utm
) == 0 )
1148 #elif defined(__WIN32__)
1149 wxFileHandle
fh(GetFullPath());
1152 FILETIME ftAccess
, ftCreate
, ftWrite
;
1155 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
1157 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
1159 ConvertWxToFileTime(&ftWrite
, *dtMod
);
1161 if ( ::SetFileTime(fh
,
1162 dtCreate
? &ftCreate
: NULL
,
1163 dtAccess
? &ftAccess
: NULL
,
1164 dtMod
? &ftWrite
: NULL
) )
1169 #else // other platform
1172 wxLogSysError(_("Failed to modify file times for '%s'"),
1173 GetFullPath().c_str());
1178 bool wxFileName::Touch()
1180 #if defined(__UNIX_LIKE__)
1181 // under Unix touching file is simple: just pass NULL to utime()
1182 if ( utime(GetFullPath(), NULL
) == 0 )
1187 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1190 #else // other platform
1191 wxDateTime dtNow
= wxDateTime::Now();
1193 return SetTimes(NULL
/* don't change create time */, &dtNow
, &dtNow
);
1197 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
1199 wxDateTime
*dtChange
) const
1201 #if defined(__UNIX_LIKE__)
1203 if ( wxStat(GetFullPath(), &stBuf
) == 0 )
1206 dtAccess
->Set(stBuf
.st_atime
);
1208 dtMod
->Set(stBuf
.st_mtime
);
1210 dtChange
->Set(stBuf
.st_ctime
);
1214 #elif defined(__WXMAC__)
1216 if ( wxStat(GetFullPath(), &stBuf
) == 0 )
1219 dtAccess
->Set(stBuf
.st_atime
);
1221 dtMod
->Set(stBuf
.st_mtime
);
1223 dtChange
->Set(stBuf
.st_ctime
);
1227 #elif defined(__WIN32__)
1228 wxFileHandle
fh(GetFullPath());
1231 FILETIME ftAccess
, ftCreate
, ftWrite
;
1233 if ( ::GetFileTime(fh
,
1234 dtMod
? &ftCreate
: NULL
,
1235 dtAccess
? &ftAccess
: NULL
,
1236 dtChange
? &ftWrite
: NULL
) )
1239 ConvertFileTimeToWx(dtMod
, ftCreate
);
1241 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1243 ConvertFileTimeToWx(dtChange
, ftWrite
);
1248 #else // other platform
1251 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1252 GetFullPath().c_str());