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
)
365 wxString tempname
= CreateTempFileName(prefix
);
366 if ( tempname
.empty() )
368 // error, failed to get temp file name
378 wxString
wxFileName::CreateTempFileName(const wxString
& prefix
)
380 wxString path
, dir
, name
;
382 // use the directory specified by the prefix
383 SplitPath(prefix
, &dir
, &name
, NULL
/* extension */);
385 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
390 if ( !::GetTempPath(MAX_PATH
, wxStringBuffer(dir
, MAX_PATH
+ 1)) )
392 wxLogLastError(_T("GetTempPath"));
397 // GetTempFileName() fails if we pass it an empty string
402 if ( !::GetTempFileName(dir
, name
, 0, wxStringBuffer(path
, MAX_PATH
+ 1)) )
404 wxLogLastError(_T("GetTempFileName"));
409 if ( !::GetTempFileName(NULL
, prefix
, 0, wxStringBuffer(path
, 1025)) )
415 #elif defined(__WXPM__)
416 // for now just create a file
418 // future enhancements can be to set some extended attributes for file
419 // systems OS/2 supports that have them (HPFS, FAT32) and security
421 static const wxChar
*szMktempSuffix
= wxT("XXX");
422 path
<< dir
<< _T('/') << name
<< szMktempSuffix
;
424 // Temporarily remove - MN
426 ::DosCreateDir(wxStringBuffer(MAX_PATH
), NULL
);
429 #else // !Windows, !OS/2
432 dir
= wxGetenv(_T("TMP"));
435 dir
= wxGetenv(_T("TEMP"));
447 if ( !wxEndsWithPathSeparator(dir
) &&
448 (name
.empty() || !wxIsPathSeparator(name
[0u])) )
456 // scratch space for mkstemp()
457 path
+= _T("XXXXXX");
459 // can use the cast here because the length doesn't change and the string
461 if ( mkstemp((char *)path
.mb_str()) == -1 )
463 // this might be not necessary as mkstemp() on most systems should have
464 // already done it but it doesn't hurt neither...
467 //else: file already created
468 #else // !HAVE_MKSTEMP
472 path
+= _T("XXXXXX");
474 if ( !mktemp((char *)path
.mb_str()) )
478 #else // !HAVE_MKTEMP
479 // generate the unique file name ourselves
480 path
<< (unsigned int)getpid();
484 static const size_t numTries
= 1000;
485 for ( size_t n
= 0; n
< numTries
; n
++ )
487 // 3 hex digits is enough for numTries == 1000 < 4096
488 pathTry
= path
+ wxString::Format(_T("%.03x"), n
);
489 if ( !wxFile::Exists(pathTry
) )
498 #endif // HAVE_MKTEMP/!HAVE_MKTEMP
502 // create the file - of course, there is a race condition here, this is
503 // why we always prefer using mkstemp()...
505 if ( !file
.Open(path
, wxFile::write_excl
, wxS_IRUSR
| wxS_IWUSR
) )
507 // FIXME: If !ok here should we loop and try again with another
508 // file name? That is the standard recourse if open(O_EXCL)
509 // fails, though of course it should be protected against
510 // possible infinite looping too.
512 wxLogError(_("Failed to open temporary file."));
517 #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
519 #endif // Windows/!Windows
523 wxLogSysError(_("Failed to create a temporary file name"));
529 // ----------------------------------------------------------------------------
530 // directory operations
531 // ----------------------------------------------------------------------------
533 bool wxFileName::Mkdir( int perm
, bool full
)
535 return wxFileName::Mkdir( GetFullPath(), perm
, full
);
538 bool wxFileName::Mkdir( const wxString
&dir
, int perm
, bool full
)
542 wxFileName
filename(dir
);
543 wxArrayString dirs
= filename
.GetDirs();
544 dirs
.Add(filename
.GetName());
546 size_t count
= dirs
.GetCount();
550 for ( i
= 0; i
< count
; i
++ )
554 if (currPath
.Last() == wxT(':'))
556 // Can't create a root directory so continue to next dir
557 currPath
+= wxFILE_SEP_PATH
;
561 if (!DirExists(currPath
))
562 if (!wxMkdir(currPath
, perm
))
565 if ( (i
< (count
-1)) )
566 currPath
+= wxFILE_SEP_PATH
;
569 return (noErrors
== 0);
573 return ::wxMkdir( dir
, perm
);
576 bool wxFileName::Rmdir()
578 return wxFileName::Rmdir( GetFullPath() );
581 bool wxFileName::Rmdir( const wxString
&dir
)
583 return ::wxRmdir( dir
);
586 // ----------------------------------------------------------------------------
587 // path normalization
588 // ----------------------------------------------------------------------------
590 bool wxFileName::Normalize(wxPathNormalize flags
,
594 // the existing path components
595 wxArrayString dirs
= GetDirs();
597 // the path to prepend in front to make the path absolute
600 format
= GetFormat(format
);
602 // make the path absolute
603 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
607 curDir
.AssignCwd(GetVolume());
611 curDir
.AssignDir(cwd
);
614 // the path may be not absolute because it doesn't have the volume name
615 // but in this case we shouldn't modify the directory components of it
616 // but just set the current volume
617 if ( !HasVolume() && curDir
.HasVolume() )
619 SetVolume(curDir
.GetVolume());
623 // yes, it was the case - we don't need curDir then
629 // handle ~ stuff under Unix only
630 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
632 if ( !dirs
.IsEmpty() )
634 wxString dir
= dirs
[0u];
635 if ( !dir
.empty() && dir
[0u] == _T('~') )
637 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
644 // transform relative path into abs one
647 wxArrayString dirsNew
= curDir
.GetDirs();
648 size_t count
= dirs
.GetCount();
649 for ( size_t n
= 0; n
< count
; n
++ )
651 dirsNew
.Add(dirs
[n
]);
657 // now deal with ".", ".." and the rest
659 size_t count
= dirs
.GetCount();
660 for ( size_t n
= 0; n
< count
; n
++ )
662 wxString dir
= dirs
[n
];
664 if ( flags
&& wxPATH_NORM_DOTS
)
666 if ( dir
== wxT(".") )
672 if ( dir
== wxT("..") )
674 if ( m_dirs
.IsEmpty() )
676 wxLogError(_("The path '%s' contains too many \"..\"!"),
677 GetFullPath().c_str());
681 m_dirs
.Remove(m_dirs
.GetCount() - 1);
686 if ( flags
& wxPATH_NORM_ENV_VARS
)
688 dir
= wxExpandEnvVars(dir
);
691 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
699 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
701 // VZ: expand env vars here too?
707 #if defined(__WIN32__)
708 if ( (flags
& wxPATH_NORM_LONG
) && (format
== wxPATH_DOS
) )
710 Assign(GetLongPath());
717 // ----------------------------------------------------------------------------
718 // filename kind tests
719 // ----------------------------------------------------------------------------
721 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
723 wxFileName fn1
= *this,
726 // get cwd only once - small time saving
727 wxString cwd
= wxGetCwd();
728 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
729 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
731 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
734 // TODO: compare inodes for Unix, this works even when filenames are
735 // different but files are the same (symlinks) (VZ)
741 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
743 // only Unix filenames are truely case-sensitive
744 return GetFormat(format
) == wxPATH_UNIX
;
747 bool wxFileName::IsAbsolute( wxPathFormat format
)
749 // if we have no path, we can't be an abs filename
750 if ( m_dirs
.IsEmpty() )
755 format
= GetFormat(format
);
757 if ( format
== wxPATH_UNIX
)
759 const wxString
& str
= m_dirs
[0u];
762 // the path started with '/', it's an absolute one
766 // the path is absolute if it starts with a path separator or
767 // with "~" or "~user"
770 return IsPathSeparator(ch
, format
) || ch
== _T('~');
774 // must have the drive
775 if ( m_volume
.empty() )
781 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
785 return m_dirs
[0u].empty();
788 // TODO: what is the relative path format here?
792 return !m_dirs
[0u].empty();
798 wxString
wxFileName::GetVolumeSeparator(wxPathFormat format
)
802 if ( GetFormat(format
) != wxPATH_UNIX
)
804 // so far it is the same for all systems which have it
805 sepVol
= wxFILE_SEP_DSK
;
807 //else: leave empty, no volume separators under Unix
813 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
816 switch ( GetFormat(format
) )
819 // accept both as native APIs do but put the native one first as
820 // this is the one we use in GetFullPath()
821 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
825 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
829 seps
= wxFILE_SEP_PATH_UNIX
;
833 seps
= wxFILE_SEP_PATH_MAC
;
837 seps
= wxFILE_SEP_PATH_VMS
;
845 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
847 // wxString::Find() doesn't work as expected with NUL - it will always find
848 // it, so it is almost surely a bug if this function is called with NUL arg
849 wxASSERT_MSG( ch
!= _T('\0'), _T("shouldn't be called with NUL") );
851 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
854 bool wxFileName::IsWild( wxPathFormat format
)
856 // FIXME: this is probably false for Mac and this is surely wrong for most
857 // of Unix shells (think about "[...]")
859 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
862 // ----------------------------------------------------------------------------
863 // path components manipulation
864 // ----------------------------------------------------------------------------
866 void wxFileName::AppendDir( const wxString
&dir
)
871 void wxFileName::PrependDir( const wxString
&dir
)
873 m_dirs
.Insert( dir
, 0 );
876 void wxFileName::InsertDir( int before
, const wxString
&dir
)
878 m_dirs
.Insert( dir
, before
);
881 void wxFileName::RemoveDir( int pos
)
883 m_dirs
.Remove( (size_t)pos
);
886 // ----------------------------------------------------------------------------
888 // ----------------------------------------------------------------------------
890 void wxFileName::SetFullName(const wxString
& fullname
)
892 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
895 wxString
wxFileName::GetFullName() const
897 wxString fullname
= m_name
;
898 if ( !m_ext
.empty() )
900 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
906 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
908 format
= GetFormat( format
);
911 size_t count
= m_dirs
.GetCount();
912 for ( size_t i
= 0; i
< count
; i
++ )
915 if ( add_separator
|| (i
< count
) )
916 ret
+= wxFILE_SEP_PATH
;
922 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
924 format
= GetFormat(format
);
928 // first put the volume
929 if ( !m_volume
.empty() )
931 // special Windows UNC paths hack, part 2: undo what we did in
932 // SplitPath() and make an UNC path if we have a drive which is not a
933 // single letter (hopefully the network shares can't be one letter only
934 // although I didn't find any authoritative docs on this)
935 if ( format
== wxPATH_DOS
&& m_volume
.length() > 1 )
937 fullpath
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_DOS
<< m_volume
;
941 fullpath
<< m_volume
<< GetVolumeSeparator(format
);
945 // then concatenate all the path components using the path separator
946 size_t dirCount
= m_dirs
.GetCount();
949 // under Mac, we must have a path separator in the beginning of the
950 // relative path - otherwise it would be parsed as an absolute one
951 if ( format
== wxPATH_MAC
&& m_volume
.empty() && !m_dirs
[0].empty() )
953 fullpath
+= wxFILE_SEP_PATH_MAC
;
956 wxChar chPathSep
= GetPathSeparators(format
)[0u];
957 if ( format
== wxPATH_VMS
)
962 for ( size_t i
= 0; i
< dirCount
; i
++ )
964 // under VMS, we shouldn't have a leading dot
965 if ( i
&& (format
!= wxPATH_VMS
|| !m_dirs
[i
- 1].empty()) )
966 fullpath
+= chPathSep
;
968 fullpath
+= m_dirs
[i
];
971 if ( format
== wxPATH_VMS
)
977 // separate the file name from the last directory, notice that we
978 // intentionally do it even if the name and extension are empty as
979 // this allows us to distinguish the directories from the file
980 // names (the directories have the trailing slash)
981 fullpath
+= chPathSep
;
985 // finally add the file name and extension
986 fullpath
+= GetFullName();
991 // Return the short form of the path (returns identity on non-Windows platforms)
992 wxString
wxFileName::GetShortPath() const
994 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
995 wxString
path(GetFullPath());
997 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
1001 ok
= ::GetShortPathName
1004 pathOut
.GetWriteBuf(sz
),
1007 pathOut
.UngetWriteBuf();
1014 return GetFullPath();
1018 // Return the long form of the path (returns identity on non-Windows platforms)
1019 wxString
wxFileName::GetLongPath() const
1022 path
= GetFullPath();
1024 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1025 bool success
= FALSE
;
1027 // VZ: this code was disabled, why?
1028 #if 0 // wxUSE_DYNLIB_CLASS
1029 typedef DWORD (*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
1031 static bool s_triedToLoad
= FALSE
;
1033 if ( !s_triedToLoad
)
1035 s_triedToLoad
= TRUE
;
1036 wxDllType dllKernel
= wxDllLoader::LoadLibrary(_T("kernel32"));
1039 // may succeed or fail depending on the Windows version
1040 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
1042 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameW"));
1044 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameA"));
1047 wxDllLoader::UnloadLibrary(dllKernel
);
1049 if ( s_pfnGetLongPathName
)
1051 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1052 bool ok
= dwSize
> 0;
1056 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1060 ok
= (*s_pfnGetLongPathName
)
1063 pathOut
.GetWriteBuf(sz
),
1066 pathOut
.UngetWriteBuf();
1076 #endif // wxUSE_DYNLIB_CLASS
1080 // The OS didn't support GetLongPathName, or some other error.
1081 // We need to call FindFirstFile on each component in turn.
1083 WIN32_FIND_DATA findFileData
;
1085 pathOut
= wxEmptyString
;
1087 wxArrayString dirs
= GetDirs();
1088 dirs
.Add(GetFullName());
1092 size_t count
= dirs
.GetCount();
1093 for ( size_t i
= 0; i
< count
; i
++ )
1095 // We're using pathOut to collect the long-name path, but using a
1096 // temporary for appending the last path component which may be
1098 tmpPath
= pathOut
+ dirs
[i
];
1100 if ( tmpPath
.empty() )
1103 if ( tmpPath
.Last() == wxT(':') )
1105 // Can't pass a drive and root dir to FindFirstFile,
1106 // so continue to next dir
1107 tmpPath
+= wxFILE_SEP_PATH
;
1112 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
1113 if (hFind
== INVALID_HANDLE_VALUE
)
1115 // Error: return immediately with the original path
1119 pathOut
+= findFileData
.cFileName
;
1120 if ( (i
< (count
-1)) )
1121 pathOut
+= wxFILE_SEP_PATH
;
1128 #endif // Win32/!Win32
1133 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
1135 if (format
== wxPATH_NATIVE
)
1137 #if defined(__WXMSW__) || defined(__WXPM__)
1138 format
= wxPATH_DOS
;
1139 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1140 format
= wxPATH_MAC
;
1141 #elif defined(__VMS)
1142 format
= wxPATH_VMS
;
1144 format
= wxPATH_UNIX
;
1150 // ----------------------------------------------------------------------------
1151 // path splitting function
1152 // ----------------------------------------------------------------------------
1155 void wxFileName::SplitPath(const wxString
& fullpathWithVolume
,
1156 wxString
*pstrVolume
,
1160 wxPathFormat format
)
1162 format
= GetFormat(format
);
1164 wxString fullpath
= fullpathWithVolume
;
1166 // under VMS the end of the path is ']', not the path separator used to
1167 // separate the components
1168 wxString sepPath
= format
== wxPATH_VMS
? wxString(_T(']'))
1169 : GetPathSeparators(format
);
1171 // special Windows UNC paths hack: transform \\share\path into share:path
1172 if ( format
== wxPATH_DOS
)
1174 if ( fullpath
.length() >= 4 &&
1175 fullpath
[0u] == wxFILE_SEP_PATH_DOS
&&
1176 fullpath
[1u] == wxFILE_SEP_PATH_DOS
)
1178 fullpath
.erase(0, 2);
1180 size_t posFirstSlash
= fullpath
.find_first_of(sepPath
);
1181 if ( posFirstSlash
!= wxString::npos
)
1183 fullpath
[posFirstSlash
] = wxFILE_SEP_DSK
;
1185 // UNC paths are always absolute, right? (FIXME)
1186 fullpath
.insert(posFirstSlash
+ 1, wxFILE_SEP_PATH_DOS
);
1191 // do we have the volume name in the beginning?
1192 wxString sepVol
= GetVolumeSeparator(format
);
1193 if ( !sepVol
.empty() )
1195 size_t posFirstColon
= fullpath
.find_first_of(sepVol
);
1196 if ( posFirstColon
!= wxString::npos
)
1200 *pstrVolume
= fullpath
.Left(posFirstColon
);
1203 // remove the volume name and the separator from the full path
1204 fullpath
.erase(0, posFirstColon
+ sepVol
.length());
1208 // find the positions of the last dot and last path separator in the path
1209 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
1210 size_t posLastSlash
= fullpath
.find_last_of(sepPath
);
1212 if ( (posLastDot
!= wxString::npos
) &&
1213 ((format
== wxPATH_UNIX
) || (format
== wxPATH_VMS
)) )
1215 if ( (posLastDot
== 0) ||
1216 (fullpath
[posLastDot
- 1] == sepPath
[0u] ) )
1218 // under Unix and VMS, dot may be (and commonly is) the first
1219 // character of the filename, don't treat the entire filename as
1220 // extension in this case
1221 posLastDot
= wxString::npos
;
1225 // if we do have a dot and a slash, check that the dot is in the name part
1226 if ( (posLastDot
!= wxString::npos
) &&
1227 (posLastSlash
!= wxString::npos
) &&
1228 (posLastDot
< posLastSlash
) )
1230 // the dot is part of the path, not the start of the extension
1231 posLastDot
= wxString::npos
;
1234 // now fill in the variables provided by user
1237 if ( posLastSlash
== wxString::npos
)
1244 // take everything up to the path separator but take care to make
1245 // tha path equal to something like '/', not empty, for the files
1246 // immediately under root directory
1247 size_t len
= posLastSlash
;
1251 *pstrPath
= fullpath
.Left(len
);
1253 // special VMS hack: remove the initial bracket
1254 if ( format
== wxPATH_VMS
)
1256 if ( (*pstrPath
)[0u] == _T('[') )
1257 pstrPath
->erase(0, 1);
1264 // take all characters starting from the one after the last slash and
1265 // up to, but excluding, the last dot
1266 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
1268 if ( posLastDot
== wxString::npos
)
1270 // take all until the end
1271 count
= wxString::npos
;
1273 else if ( posLastSlash
== wxString::npos
)
1277 else // have both dot and slash
1279 count
= posLastDot
- posLastSlash
- 1;
1282 *pstrName
= fullpath
.Mid(nStart
, count
);
1287 if ( posLastDot
== wxString::npos
)
1294 // take everything after the dot
1295 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
1301 void wxFileName::SplitPath(const wxString
& fullpath
,
1305 wxPathFormat format
)
1308 SplitPath(fullpath
, &volume
, path
, name
, ext
, format
);
1310 if ( path
&& !volume
.empty() )
1312 path
->Prepend(volume
+ GetVolumeSeparator(format
));
1316 // ----------------------------------------------------------------------------
1318 // ----------------------------------------------------------------------------
1320 bool wxFileName::SetTimes(const wxDateTime
*dtCreate
,
1321 const wxDateTime
*dtAccess
,
1322 const wxDateTime
*dtMod
)
1324 #if defined(__UNIX_LIKE__)
1325 if ( !dtAccess
&& !dtMod
)
1327 // can't modify the creation time anyhow, don't try
1331 // if dtAccess or dtMod is not specified, use the other one (which must be
1332 // non NULL because of the test above) for both times
1334 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
1335 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
1336 if ( utime(GetFullPath(), &utm
) == 0 )
1340 #elif defined(__WIN32__)
1341 wxFileHandle
fh(GetFullPath());
1344 FILETIME ftAccess
, ftCreate
, ftWrite
;
1347 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
1349 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
1351 ConvertWxToFileTime(&ftWrite
, *dtMod
);
1353 if ( ::SetFileTime(fh
,
1354 dtCreate
? &ftCreate
: NULL
,
1355 dtAccess
? &ftAccess
: NULL
,
1356 dtMod
? &ftWrite
: NULL
) )
1361 #else // other platform
1364 wxLogSysError(_("Failed to modify file times for '%s'"),
1365 GetFullPath().c_str());
1370 bool wxFileName::Touch()
1372 #if defined(__UNIX_LIKE__)
1373 // under Unix touching file is simple: just pass NULL to utime()
1374 if ( utime(GetFullPath(), NULL
) == 0 )
1379 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1382 #else // other platform
1383 wxDateTime dtNow
= wxDateTime::Now();
1385 return SetTimes(NULL
/* don't change create time */, &dtNow
, &dtNow
);
1389 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
1391 wxDateTime
*dtChange
) const
1393 #if defined(__UNIX_LIKE__)
1395 if ( wxStat(GetFullPath(), &stBuf
) == 0 )
1398 dtAccess
->Set(stBuf
.st_atime
);
1400 dtMod
->Set(stBuf
.st_mtime
);
1402 dtChange
->Set(stBuf
.st_ctime
);
1406 #elif defined(__WXMAC__)
1408 if ( wxStat(GetFullPath(), &stBuf
) == 0 )
1411 dtAccess
->Set(stBuf
.st_atime
);
1413 dtMod
->Set(stBuf
.st_mtime
);
1415 dtChange
->Set(stBuf
.st_ctime
);
1419 #elif defined(__WIN32__)
1420 wxFileHandle
fh(GetFullPath());
1423 FILETIME ftAccess
, ftCreate
, ftWrite
;
1425 if ( ::GetFileTime(fh
,
1426 dtMod
? &ftCreate
: NULL
,
1427 dtAccess
? &ftAccess
: NULL
,
1428 dtChange
? &ftWrite
: NULL
) )
1431 ConvertFileTimeToWx(dtMod
, ftCreate
);
1433 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1435 ConvertFileTimeToWx(dtChange
, ftWrite
);
1440 #else // other platform
1443 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1444 GetFullPath().c_str());