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(const wxString
& volume
)
316 AssignDir(wxFileName::GetCwd(volume
));
320 wxString
wxFileName::GetCwd(const wxString
& volume
)
322 // if we have the volume, we must get the current directory on this drive
323 // and to do this we have to chdir to this volume - at least under Windows,
324 // I don't know how to get the current drive on another volume elsewhere
327 if ( !volume
.empty() )
330 SetCwd(volume
+ GetVolumeSeparator());
333 wxString cwd
= ::wxGetCwd();
335 if ( !volume
.empty() )
343 bool wxFileName::SetCwd()
345 return wxFileName::SetCwd( GetFullPath() );
348 bool wxFileName::SetCwd( const wxString
&cwd
)
350 return ::wxSetWorkingDirectory( cwd
);
353 void wxFileName::AssignHomeDir()
355 AssignDir(wxFileName::GetHomeDir());
358 wxString
wxFileName::GetHomeDir()
360 return ::wxGetHomeDir();
363 void wxFileName::AssignTempFileName( const wxString
&prefix
)
366 if ( wxGetTempFileName(prefix
, fullname
) )
376 // ----------------------------------------------------------------------------
377 // directory operations
378 // ----------------------------------------------------------------------------
380 bool wxFileName::Mkdir( int perm
, bool full
)
382 return wxFileName::Mkdir( GetFullPath(), perm
, full
);
385 bool wxFileName::Mkdir( const wxString
&dir
, int perm
, bool full
)
389 wxFileName
filename(dir
);
390 wxArrayString dirs
= filename
.GetDirs();
391 dirs
.Add(filename
.GetName());
393 size_t count
= dirs
.GetCount();
397 for ( i
= 0; i
< count
; i
++ )
401 if (currPath
.Last() == wxT(':'))
403 // Can't create a root directory so continue to next dir
404 currPath
+= wxFILE_SEP_PATH
;
408 if (!DirExists(currPath
))
409 if (!wxMkdir(currPath
, perm
))
412 if ( (i
< (count
-1)) )
413 currPath
+= wxFILE_SEP_PATH
;
416 return (noErrors
== 0);
420 return ::wxMkdir( dir
, perm
);
423 bool wxFileName::Rmdir()
425 return wxFileName::Rmdir( GetFullPath() );
428 bool wxFileName::Rmdir( const wxString
&dir
)
430 return ::wxRmdir( dir
);
433 // ----------------------------------------------------------------------------
434 // path normalization
435 // ----------------------------------------------------------------------------
437 bool wxFileName::Normalize(wxPathNormalize flags
,
441 // the existing path components
442 wxArrayString dirs
= GetDirs();
444 // the path to prepend in front to make the path absolute
447 format
= GetFormat(format
);
449 // make the path absolute
450 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
454 curDir
.AssignCwd(GetVolume());
458 curDir
.AssignDir(cwd
);
461 // the path may be not absolute because it doesn't have the volume name
462 // but in this case we shouldn't modify the directory components of it
463 // but just set the current volume
464 if ( !HasVolume() && curDir
.HasVolume() )
466 SetVolume(curDir
.GetVolume());
470 // yes, it was the case - we don't need curDir then
476 // handle ~ stuff under Unix only
477 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
479 if ( !dirs
.IsEmpty() )
481 wxString dir
= dirs
[0u];
482 if ( !dir
.empty() && dir
[0u] == _T('~') )
484 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
491 // transform relative path into abs one
494 wxArrayString dirsNew
= curDir
.GetDirs();
495 size_t count
= dirs
.GetCount();
496 for ( size_t n
= 0; n
< count
; n
++ )
498 dirsNew
.Add(dirs
[n
]);
504 // now deal with ".", ".." and the rest
506 size_t count
= dirs
.GetCount();
507 for ( size_t n
= 0; n
< count
; n
++ )
509 wxString dir
= dirs
[n
];
511 if ( flags
&& wxPATH_NORM_DOTS
)
513 if ( dir
== wxT(".") )
519 if ( dir
== wxT("..") )
521 if ( m_dirs
.IsEmpty() )
523 wxLogError(_("The path '%s' contains too many \"..\"!"),
524 GetFullPath().c_str());
528 m_dirs
.Remove(m_dirs
.GetCount() - 1);
533 if ( flags
& wxPATH_NORM_ENV_VARS
)
535 dir
= wxExpandEnvVars(dir
);
538 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
546 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
548 // VZ: expand env vars here too?
554 #if defined(__WIN32__)
555 if ( (flags
& wxPATH_NORM_LONG
) && (format
== wxPATH_DOS
) )
557 Assign(GetLongPath());
564 // ----------------------------------------------------------------------------
565 // filename kind tests
566 // ----------------------------------------------------------------------------
568 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
570 wxFileName fn1
= *this,
573 // get cwd only once - small time saving
574 wxString cwd
= wxGetCwd();
575 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
576 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
578 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
581 // TODO: compare inodes for Unix, this works even when filenames are
582 // different but files are the same (symlinks) (VZ)
588 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
590 // only Unix filenames are truely case-sensitive
591 return GetFormat(format
) == wxPATH_UNIX
;
594 bool wxFileName::IsAbsolute( wxPathFormat format
)
596 // if we have no path, we can't be an abs filename
597 if ( m_dirs
.IsEmpty() )
602 format
= GetFormat(format
);
604 if ( format
== wxPATH_UNIX
)
606 const wxString
& str
= m_dirs
[0u];
609 // the path started with '/', it's an absolute one
613 // the path is absolute if it starts with a path separator or
614 // with "~" or "~user"
617 return IsPathSeparator(ch
, format
) || ch
== _T('~');
621 // must have the drive
622 if ( m_volume
.empty() )
628 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
632 return m_dirs
[0u].empty();
635 // TODO: what is the relative path format here?
639 return !m_dirs
[0u].empty();
645 wxString
wxFileName::GetVolumeSeparator(wxPathFormat format
)
649 if ( GetFormat(format
) != wxPATH_UNIX
)
651 // so far it is the same for all systems which have it
652 sepVol
= wxFILE_SEP_DSK
;
654 //else: leave empty, no volume separators under Unix
660 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
663 switch ( GetFormat(format
) )
666 // accept both as native APIs do but put the native one first as
667 // this is the one we use in GetFullPath()
668 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
672 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
676 seps
= wxFILE_SEP_PATH_UNIX
;
680 seps
= wxFILE_SEP_PATH_MAC
;
684 seps
= wxFILE_SEP_PATH_VMS
;
692 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
694 // wxString::Find() doesn't work as expected with NUL - it will always find
695 // it, so it is almost surely a bug if this function is called with NUL arg
696 wxASSERT_MSG( ch
!= _T('\0'), _T("shouldn't be called with NUL") );
698 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
701 bool wxFileName::IsWild( wxPathFormat format
)
703 // FIXME: this is probably false for Mac and this is surely wrong for most
704 // of Unix shells (think about "[...]")
706 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
709 // ----------------------------------------------------------------------------
710 // path components manipulation
711 // ----------------------------------------------------------------------------
713 void wxFileName::AppendDir( const wxString
&dir
)
718 void wxFileName::PrependDir( const wxString
&dir
)
720 m_dirs
.Insert( dir
, 0 );
723 void wxFileName::InsertDir( int before
, const wxString
&dir
)
725 m_dirs
.Insert( dir
, before
);
728 void wxFileName::RemoveDir( int pos
)
730 m_dirs
.Remove( (size_t)pos
);
733 // ----------------------------------------------------------------------------
735 // ----------------------------------------------------------------------------
737 void wxFileName::SetFullName(const wxString
& fullname
)
739 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
742 wxString
wxFileName::GetFullName() const
744 wxString fullname
= m_name
;
745 if ( !m_ext
.empty() )
747 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
753 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
755 format
= GetFormat( format
);
758 size_t count
= m_dirs
.GetCount();
759 for ( size_t i
= 0; i
< count
; i
++ )
762 if ( add_separator
|| (i
< count
) )
763 ret
+= wxFILE_SEP_PATH
;
769 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
771 format
= GetFormat(format
);
775 // first put the volume
776 if ( !m_volume
.empty() )
778 // special Windows UNC paths hack, part 2: undo what we did in
779 // SplitPath() and make an UNC path if we have a drive which is not a
780 // single letter (hopefully the network shares can't be one letter only
781 // although I didn't find any authoritative docs on this)
782 if ( format
== wxPATH_DOS
&& m_volume
.length() > 1 )
784 fullpath
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_DOS
<< m_volume
;
788 fullpath
<< m_volume
<< GetVolumeSeparator(format
);
792 // then concatenate all the path components using the path separator
793 size_t dirCount
= m_dirs
.GetCount();
796 // under Mac, we must have a path separator in the beginning of the
797 // relative path - otherwise it would be parsed as an absolute one
798 if ( format
== wxPATH_MAC
&& m_volume
.empty() && !m_dirs
[0].empty() )
800 fullpath
+= wxFILE_SEP_PATH_MAC
;
803 wxChar chPathSep
= GetPathSeparators(format
)[0u];
804 if ( format
== wxPATH_VMS
)
809 for ( size_t i
= 0; i
< dirCount
; i
++ )
811 // under VMS, we shouldn't have a leading dot
812 if ( i
&& (format
!= wxPATH_VMS
|| !m_dirs
[i
- 1].empty()) )
813 fullpath
+= chPathSep
;
815 fullpath
+= m_dirs
[i
];
818 if ( format
== wxPATH_VMS
)
824 // separate the file name from the last directory, notice that we
825 // intentionally do it even if the name and extension are empty as
826 // this allows us to distinguish the directories from the file
827 // names (the directories have the trailing slash)
828 fullpath
+= chPathSep
;
832 // finally add the file name and extension
833 fullpath
+= GetFullName();
838 // Return the short form of the path (returns identity on non-Windows platforms)
839 wxString
wxFileName::GetShortPath() const
841 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
842 wxString
path(GetFullPath());
844 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
848 ok
= ::GetShortPathName
851 pathOut
.GetWriteBuf(sz
),
854 pathOut
.UngetWriteBuf();
861 return GetFullPath();
865 // Return the long form of the path (returns identity on non-Windows platforms)
866 wxString
wxFileName::GetLongPath() const
869 path
= GetFullPath();
871 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
872 bool success
= FALSE
;
874 // VZ: this code was disabled, why?
875 #if 0 // wxUSE_DYNLIB_CLASS
876 typedef DWORD (*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
878 static bool s_triedToLoad
= FALSE
;
880 if ( !s_triedToLoad
)
882 s_triedToLoad
= TRUE
;
883 wxDllType dllKernel
= wxDllLoader::LoadLibrary(_T("kernel32"));
886 // may succeed or fail depending on the Windows version
887 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
889 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameW"));
891 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameA"));
894 wxDllLoader::UnloadLibrary(dllKernel
);
896 if ( s_pfnGetLongPathName
)
898 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
899 bool ok
= dwSize
> 0;
903 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
907 ok
= (*s_pfnGetLongPathName
)
910 pathOut
.GetWriteBuf(sz
),
913 pathOut
.UngetWriteBuf();
923 #endif // wxUSE_DYNLIB_CLASS
927 // The OS didn't support GetLongPathName, or some other error.
928 // We need to call FindFirstFile on each component in turn.
930 WIN32_FIND_DATA findFileData
;
932 pathOut
= wxEmptyString
;
934 wxArrayString dirs
= GetDirs();
935 dirs
.Add(GetFullName());
939 size_t count
= dirs
.GetCount();
940 for ( size_t i
= 0; i
< count
; i
++ )
942 // We're using pathOut to collect the long-name path, but using a
943 // temporary for appending the last path component which may be
945 tmpPath
= pathOut
+ dirs
[i
];
947 if ( tmpPath
.empty() )
950 if ( tmpPath
.Last() == wxT(':') )
952 // Can't pass a drive and root dir to FindFirstFile,
953 // so continue to next dir
954 tmpPath
+= wxFILE_SEP_PATH
;
959 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
960 if (hFind
== INVALID_HANDLE_VALUE
)
962 // Error: return immediately with the original path
966 pathOut
+= findFileData
.cFileName
;
967 if ( (i
< (count
-1)) )
968 pathOut
+= wxFILE_SEP_PATH
;
975 #endif // Win32/!Win32
980 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
982 if (format
== wxPATH_NATIVE
)
984 #if defined(__WXMSW__) || defined(__WXPM__)
986 #elif defined(__WXMAC__) && !defined(__DARWIN__)
991 format
= wxPATH_UNIX
;
997 // ----------------------------------------------------------------------------
998 // path splitting function
999 // ----------------------------------------------------------------------------
1002 void wxFileName::SplitPath(const wxString
& fullpathWithVolume
,
1003 wxString
*pstrVolume
,
1007 wxPathFormat format
)
1009 format
= GetFormat(format
);
1011 wxString fullpath
= fullpathWithVolume
;
1013 // under VMS the end of the path is ']', not the path separator used to
1014 // separate the components
1015 wxString sepPath
= format
== wxPATH_VMS
? _T(']')
1016 : GetPathSeparators(format
);
1018 // special Windows UNC paths hack: transform \\share\path into share:path
1019 if ( format
== wxPATH_DOS
)
1021 if ( fullpath
.length() >= 4 &&
1022 fullpath
[0u] == wxFILE_SEP_PATH_DOS
&&
1023 fullpath
[1u] == wxFILE_SEP_PATH_DOS
)
1025 fullpath
.erase(0, 2);
1027 size_t posFirstSlash
= fullpath
.find_first_of(sepPath
);
1028 if ( posFirstSlash
!= wxString::npos
)
1030 fullpath
[posFirstSlash
] = wxFILE_SEP_DSK
;
1032 // UNC paths are always absolute, right? (FIXME)
1033 fullpath
.insert(posFirstSlash
+ 1, wxFILE_SEP_PATH_DOS
);
1038 // do we have the volume name in the beginning?
1039 wxString sepVol
= GetVolumeSeparator(format
);
1040 if ( !sepVol
.empty() )
1042 size_t posFirstColon
= fullpath
.find_first_of(sepVol
);
1043 if ( posFirstColon
!= wxString::npos
)
1047 *pstrVolume
= fullpath
.Left(posFirstColon
);
1050 // remove the volume name and the separator from the full path
1051 fullpath
.erase(0, posFirstColon
+ sepVol
.length());
1055 // find the positions of the last dot and last path separator in the path
1056 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
1057 size_t posLastSlash
= fullpath
.find_last_of(sepPath
);
1059 if ( (posLastDot
!= wxString::npos
) &&
1060 ((format
== wxPATH_UNIX
) || (format
== wxPATH_VMS
)) )
1062 if ( (posLastDot
== 0) ||
1063 (fullpath
[posLastDot
- 1] == sepPath
[0u] ) )
1065 // under Unix and VMS, dot may be (and commonly is) the first
1066 // character of the filename, don't treat the entire filename as
1067 // extension in this case
1068 posLastDot
= wxString::npos
;
1072 // if we do have a dot and a slash, check that the dot is in the name part
1073 if ( (posLastDot
!= wxString::npos
) &&
1074 (posLastSlash
!= wxString::npos
) &&
1075 (posLastDot
< posLastSlash
) )
1077 // the dot is part of the path, not the start of the extension
1078 posLastDot
= wxString::npos
;
1081 // now fill in the variables provided by user
1084 if ( posLastSlash
== wxString::npos
)
1091 // take everything up to the path separator but take care to make
1092 // tha path equal to something like '/', not empty, for the files
1093 // immediately under root directory
1094 size_t len
= posLastSlash
;
1098 *pstrPath
= fullpath
.Left(len
);
1100 // special VMS hack: remove the initial bracket
1101 if ( format
== wxPATH_VMS
)
1103 if ( (*pstrPath
)[0u] == _T('[') )
1104 pstrPath
->erase(0, 1);
1111 // take all characters starting from the one after the last slash and
1112 // up to, but excluding, the last dot
1113 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
1115 if ( posLastDot
== wxString::npos
)
1117 // take all until the end
1118 count
= wxString::npos
;
1120 else if ( posLastSlash
== wxString::npos
)
1124 else // have both dot and slash
1126 count
= posLastDot
- posLastSlash
- 1;
1129 *pstrName
= fullpath
.Mid(nStart
, count
);
1134 if ( posLastDot
== wxString::npos
)
1141 // take everything after the dot
1142 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
1148 void wxFileName::SplitPath(const wxString
& fullpath
,
1152 wxPathFormat format
)
1155 SplitPath(fullpath
, &volume
, path
, name
, ext
, format
);
1157 if ( path
&& !volume
.empty() )
1159 path
->Prepend(volume
+ GetVolumeSeparator(format
));
1163 // ----------------------------------------------------------------------------
1165 // ----------------------------------------------------------------------------
1167 bool wxFileName::SetTimes(const wxDateTime
*dtCreate
,
1168 const wxDateTime
*dtAccess
,
1169 const wxDateTime
*dtMod
)
1171 #if defined(__UNIX_LIKE__)
1172 if ( !dtAccess
&& !dtMod
)
1174 // can't modify the creation time anyhow, don't try
1178 // if dtAccess or dtMod is not specified, use the other one (which must be
1179 // non NULL because of the test above) for both times
1181 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
1182 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
1183 if ( utime(GetFullPath(), &utm
) == 0 )
1187 #elif defined(__WIN32__)
1188 wxFileHandle
fh(GetFullPath());
1191 FILETIME ftAccess
, ftCreate
, ftWrite
;
1194 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
1196 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
1198 ConvertWxToFileTime(&ftWrite
, *dtMod
);
1200 if ( ::SetFileTime(fh
,
1201 dtCreate
? &ftCreate
: NULL
,
1202 dtAccess
? &ftAccess
: NULL
,
1203 dtMod
? &ftWrite
: NULL
) )
1208 #else // other platform
1211 wxLogSysError(_("Failed to modify file times for '%s'"),
1212 GetFullPath().c_str());
1217 bool wxFileName::Touch()
1219 #if defined(__UNIX_LIKE__)
1220 // under Unix touching file is simple: just pass NULL to utime()
1221 if ( utime(GetFullPath(), NULL
) == 0 )
1226 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1229 #else // other platform
1230 wxDateTime dtNow
= wxDateTime::Now();
1232 return SetTimes(NULL
/* don't change create time */, &dtNow
, &dtNow
);
1236 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
1238 wxDateTime
*dtChange
) const
1240 #if defined(__UNIX_LIKE__)
1242 if ( wxStat(GetFullPath(), &stBuf
) == 0 )
1245 dtAccess
->Set(stBuf
.st_atime
);
1247 dtMod
->Set(stBuf
.st_mtime
);
1249 dtChange
->Set(stBuf
.st_ctime
);
1253 #elif defined(__WXMAC__)
1255 if ( wxStat(GetFullPath(), &stBuf
) == 0 )
1258 dtAccess
->Set(stBuf
.st_atime
);
1260 dtMod
->Set(stBuf
.st_mtime
);
1262 dtChange
->Set(stBuf
.st_ctime
);
1266 #elif defined(__WIN32__)
1267 wxFileHandle
fh(GetFullPath());
1270 FILETIME ftAccess
, ftCreate
, ftWrite
;
1272 if ( ::GetFileTime(fh
,
1273 dtMod
? &ftCreate
: NULL
,
1274 dtAccess
? &ftAccess
: NULL
,
1275 dtChange
? &ftWrite
: NULL
) )
1278 ConvertFileTimeToWx(dtMod
, ftCreate
);
1280 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1282 ConvertFileTimeToWx(dtChange
, ftWrite
);
1287 #else // other platform
1290 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1291 GetFullPath().c_str());