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 // always recognize dir as directory, even if it doesn't end with a slash
261 wxString dirname
= dir
;
262 if ( !wxEndsWithPathSeparator(dirname
) )
264 dirname
+= GetPathSeparators(format
)[0u];
267 Assign(dirname
, _T(""), format
);
270 void wxFileName::Clear()
276 m_ext
= wxEmptyString
;
280 wxFileName
wxFileName::FileName(const wxString
& file
)
282 return wxFileName(file
);
286 wxFileName
wxFileName::DirName(const wxString
& dir
)
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
297 bool wxFileName::FileExists()
299 return wxFileName::FileExists( GetFullPath() );
302 bool wxFileName::FileExists( const wxString
&file
)
304 return ::wxFileExists( file
);
307 bool wxFileName::DirExists()
309 return wxFileName::DirExists( GetFullPath() );
312 bool wxFileName::DirExists( const wxString
&dir
)
314 return ::wxDirExists( dir
);
317 // ----------------------------------------------------------------------------
318 // CWD and HOME stuff
319 // ----------------------------------------------------------------------------
321 void wxFileName::AssignCwd(const wxString
& volume
)
323 AssignDir(wxFileName::GetCwd(volume
));
327 wxString
wxFileName::GetCwd(const wxString
& volume
)
329 // if we have the volume, we must get the current directory on this drive
330 // and to do this we have to chdir to this volume - at least under Windows,
331 // I don't know how to get the current drive on another volume elsewhere
334 if ( !volume
.empty() )
337 SetCwd(volume
+ GetVolumeSeparator());
340 wxString cwd
= ::wxGetCwd();
342 if ( !volume
.empty() )
350 bool wxFileName::SetCwd()
352 return wxFileName::SetCwd( GetFullPath() );
355 bool wxFileName::SetCwd( const wxString
&cwd
)
357 return ::wxSetWorkingDirectory( cwd
);
360 void wxFileName::AssignHomeDir()
362 AssignDir(wxFileName::GetHomeDir());
365 wxString
wxFileName::GetHomeDir()
367 return ::wxGetHomeDir();
370 void wxFileName::AssignTempFileName( const wxString
& prefix
)
372 wxString tempname
= CreateTempFileName(prefix
);
373 if ( tempname
.empty() )
375 // error, failed to get temp file name
385 wxString
wxFileName::CreateTempFileName(const wxString
& prefix
)
387 wxString path
, dir
, name
;
389 // use the directory specified by the prefix
390 SplitPath(prefix
, &dir
, &name
, NULL
/* extension */);
392 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
397 if ( !::GetTempPath(MAX_PATH
, wxStringBuffer(dir
, MAX_PATH
+ 1)) )
399 wxLogLastError(_T("GetTempPath"));
404 // GetTempFileName() fails if we pass it an empty string
409 if ( !::GetTempFileName(dir
, name
, 0, wxStringBuffer(path
, MAX_PATH
+ 1)) )
411 wxLogLastError(_T("GetTempFileName"));
416 if ( !::GetTempFileName(NULL
, prefix
, 0, wxStringBuffer(path
, 1025)) )
422 #elif defined(__WXPM__)
423 // for now just create a file
425 // future enhancements can be to set some extended attributes for file
426 // systems OS/2 supports that have them (HPFS, FAT32) and security
428 static const wxChar
*szMktempSuffix
= wxT("XXX");
429 path
<< dir
<< _T('/') << name
<< szMktempSuffix
;
431 // Temporarily remove - MN
433 ::DosCreateDir(wxStringBuffer(MAX_PATH
), NULL
);
436 #else // !Windows, !OS/2
439 dir
= wxGetenv(_T("TMP"));
442 dir
= wxGetenv(_T("TEMP"));
454 if ( !wxEndsWithPathSeparator(dir
) &&
455 (name
.empty() || !wxIsPathSeparator(name
[0u])) )
463 // scratch space for mkstemp()
464 path
+= _T("XXXXXX");
466 // can use the cast here because the length doesn't change and the string
468 if ( mkstemp((char *)path
.mb_str()) == -1 )
470 // this might be not necessary as mkstemp() on most systems should have
471 // already done it but it doesn't hurt neither...
474 //else: file already created
475 #else // !HAVE_MKSTEMP
479 path
+= _T("XXXXXX");
481 if ( !mktemp((char *)path
.mb_str()) )
485 #else // !HAVE_MKTEMP
486 // generate the unique file name ourselves
487 path
<< (unsigned int)getpid();
491 static const size_t numTries
= 1000;
492 for ( size_t n
= 0; n
< numTries
; n
++ )
494 // 3 hex digits is enough for numTries == 1000 < 4096
495 pathTry
= path
+ wxString::Format(_T("%.03x"), n
);
496 if ( !wxFile::Exists(pathTry
) )
505 #endif // HAVE_MKTEMP/!HAVE_MKTEMP
509 // create the file - of course, there is a race condition here, this is
510 // why we always prefer using mkstemp()...
512 if ( !file
.Open(path
, wxFile::write_excl
, wxS_IRUSR
| wxS_IWUSR
) )
514 // FIXME: If !ok here should we loop and try again with another
515 // file name? That is the standard recourse if open(O_EXCL)
516 // fails, though of course it should be protected against
517 // possible infinite looping too.
519 wxLogError(_("Failed to open temporary file."));
524 #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
526 #endif // Windows/!Windows
530 wxLogSysError(_("Failed to create a temporary file name"));
536 // ----------------------------------------------------------------------------
537 // directory operations
538 // ----------------------------------------------------------------------------
540 bool wxFileName::Mkdir( int perm
, bool full
)
542 return wxFileName::Mkdir( GetFullPath(), perm
, full
);
545 bool wxFileName::Mkdir( const wxString
&dir
, int perm
, bool full
)
549 wxFileName
filename(dir
);
550 wxArrayString dirs
= filename
.GetDirs();
551 dirs
.Add(filename
.GetName());
553 size_t count
= dirs
.GetCount();
557 for ( i
= 0; i
< count
; i
++ )
561 if (currPath
.Last() == wxT(':'))
563 // Can't create a root directory so continue to next dir
564 currPath
+= wxFILE_SEP_PATH
;
568 if (!DirExists(currPath
))
569 if (!wxMkdir(currPath
, perm
))
572 if ( (i
< (count
-1)) )
573 currPath
+= wxFILE_SEP_PATH
;
576 return (noErrors
== 0);
580 return ::wxMkdir( dir
, perm
);
583 bool wxFileName::Rmdir()
585 return wxFileName::Rmdir( GetFullPath() );
588 bool wxFileName::Rmdir( const wxString
&dir
)
590 return ::wxRmdir( dir
);
593 // ----------------------------------------------------------------------------
594 // path normalization
595 // ----------------------------------------------------------------------------
597 bool wxFileName::Normalize(wxPathNormalize flags
,
601 // the existing path components
602 wxArrayString dirs
= GetDirs();
604 // the path to prepend in front to make the path absolute
607 format
= GetFormat(format
);
609 // make the path absolute
610 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
614 curDir
.AssignCwd(GetVolume());
618 curDir
.AssignDir(cwd
);
621 // the path may be not absolute because it doesn't have the volume name
622 // but in this case we shouldn't modify the directory components of it
623 // but just set the current volume
624 if ( !HasVolume() && curDir
.HasVolume() )
626 SetVolume(curDir
.GetVolume());
630 // yes, it was the case - we don't need curDir then
636 // handle ~ stuff under Unix only
637 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
639 if ( !dirs
.IsEmpty() )
641 wxString dir
= dirs
[0u];
642 if ( !dir
.empty() && dir
[0u] == _T('~') )
644 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
651 // transform relative path into abs one
654 wxArrayString dirsNew
= curDir
.GetDirs();
655 size_t count
= dirs
.GetCount();
656 for ( size_t n
= 0; n
< count
; n
++ )
658 dirsNew
.Add(dirs
[n
]);
664 // now deal with ".", ".." and the rest
666 size_t count
= dirs
.GetCount();
667 for ( size_t n
= 0; n
< count
; n
++ )
669 wxString dir
= dirs
[n
];
671 if ( flags
&& wxPATH_NORM_DOTS
)
673 if ( dir
== wxT(".") )
679 if ( dir
== wxT("..") )
681 if ( m_dirs
.IsEmpty() )
683 wxLogError(_("The path '%s' contains too many \"..\"!"),
684 GetFullPath().c_str());
688 m_dirs
.Remove(m_dirs
.GetCount() - 1);
693 if ( flags
& wxPATH_NORM_ENV_VARS
)
695 dir
= wxExpandEnvVars(dir
);
698 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
706 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
708 // VZ: expand env vars here too?
714 #if defined(__WIN32__)
715 if ( (flags
& wxPATH_NORM_LONG
) && (format
== wxPATH_DOS
) )
717 Assign(GetLongPath());
724 // ----------------------------------------------------------------------------
725 // filename kind tests
726 // ----------------------------------------------------------------------------
728 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
730 wxFileName fn1
= *this,
733 // get cwd only once - small time saving
734 wxString cwd
= wxGetCwd();
735 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
736 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
738 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
741 // TODO: compare inodes for Unix, this works even when filenames are
742 // different but files are the same (symlinks) (VZ)
748 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
750 // only Unix filenames are truely case-sensitive
751 return GetFormat(format
) == wxPATH_UNIX
;
754 bool wxFileName::IsAbsolute( wxPathFormat format
)
756 // if we have no path, we can't be an abs filename
757 if ( m_dirs
.IsEmpty() )
762 format
= GetFormat(format
);
764 if ( format
== wxPATH_UNIX
)
766 const wxString
& str
= m_dirs
[0u];
769 // the path started with '/', it's an absolute one
773 // the path is absolute if it starts with a path separator or
774 // with "~" or "~user"
777 return IsPathSeparator(ch
, format
) || ch
== _T('~');
781 // must have the drive
782 if ( m_volume
.empty() )
788 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
792 return m_dirs
[0u].empty();
795 // TODO: what is the relative path format here?
799 return !m_dirs
[0u].empty();
805 wxString
wxFileName::GetVolumeSeparator(wxPathFormat format
)
809 if ( GetFormat(format
) != wxPATH_UNIX
)
811 // so far it is the same for all systems which have it
812 sepVol
= wxFILE_SEP_DSK
;
814 //else: leave empty, no volume separators under Unix
820 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
823 switch ( GetFormat(format
) )
826 // accept both as native APIs do but put the native one first as
827 // this is the one we use in GetFullPath()
828 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
832 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
836 seps
= wxFILE_SEP_PATH_UNIX
;
840 seps
= wxFILE_SEP_PATH_MAC
;
844 seps
= wxFILE_SEP_PATH_VMS
;
852 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
854 // wxString::Find() doesn't work as expected with NUL - it will always find
855 // it, so it is almost surely a bug if this function is called with NUL arg
856 wxASSERT_MSG( ch
!= _T('\0'), _T("shouldn't be called with NUL") );
858 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
861 bool wxFileName::IsWild( wxPathFormat format
)
863 // FIXME: this is probably false for Mac and this is surely wrong for most
864 // of Unix shells (think about "[...]")
866 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
869 // ----------------------------------------------------------------------------
870 // path components manipulation
871 // ----------------------------------------------------------------------------
873 void wxFileName::AppendDir( const wxString
&dir
)
878 void wxFileName::PrependDir( const wxString
&dir
)
880 m_dirs
.Insert( dir
, 0 );
883 void wxFileName::InsertDir( int before
, const wxString
&dir
)
885 m_dirs
.Insert( dir
, before
);
888 void wxFileName::RemoveDir( int pos
)
890 m_dirs
.Remove( (size_t)pos
);
893 // ----------------------------------------------------------------------------
895 // ----------------------------------------------------------------------------
897 void wxFileName::SetFullName(const wxString
& fullname
)
899 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
902 wxString
wxFileName::GetFullName() const
904 wxString fullname
= m_name
;
905 if ( !m_ext
.empty() )
907 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
913 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
915 format
= GetFormat( format
);
918 size_t count
= m_dirs
.GetCount();
919 for ( size_t i
= 0; i
< count
; i
++ )
922 if ( add_separator
|| (i
< count
) )
923 ret
+= wxFILE_SEP_PATH
;
929 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
931 format
= GetFormat(format
);
935 // first put the volume
936 if ( !m_volume
.empty() )
938 // special Windows UNC paths hack, part 2: undo what we did in
939 // SplitPath() and make an UNC path if we have a drive which is not a
940 // single letter (hopefully the network shares can't be one letter only
941 // although I didn't find any authoritative docs on this)
942 if ( format
== wxPATH_DOS
&& m_volume
.length() > 1 )
944 fullpath
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_DOS
<< m_volume
;
948 fullpath
<< m_volume
<< GetVolumeSeparator(format
);
952 // then concatenate all the path components using the path separator
953 size_t dirCount
= m_dirs
.GetCount();
956 // under Mac, we must have a path separator in the beginning of the
957 // relative path - otherwise it would be parsed as an absolute one
958 if ( format
== wxPATH_MAC
&& m_volume
.empty() && !m_dirs
[0].empty() )
960 fullpath
+= wxFILE_SEP_PATH_MAC
;
963 wxChar chPathSep
= GetPathSeparators(format
)[0u];
964 if ( format
== wxPATH_VMS
)
969 for ( size_t i
= 0; i
< dirCount
; i
++ )
971 // under VMS, we shouldn't have a leading dot
972 if ( i
&& (format
!= wxPATH_VMS
|| !m_dirs
[i
- 1].empty()) )
973 fullpath
+= chPathSep
;
975 fullpath
+= m_dirs
[i
];
978 if ( format
== wxPATH_VMS
)
984 // separate the file name from the last directory, notice that we
985 // intentionally do it even if the name and extension are empty as
986 // this allows us to distinguish the directories from the file
987 // names (the directories have the trailing slash)
988 fullpath
+= chPathSep
;
992 // finally add the file name and extension
993 fullpath
+= GetFullName();
998 // Return the short form of the path (returns identity on non-Windows platforms)
999 wxString
wxFileName::GetShortPath() const
1001 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
1002 wxString
path(GetFullPath());
1004 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
1008 ok
= ::GetShortPathName
1011 pathOut
.GetWriteBuf(sz
),
1014 pathOut
.UngetWriteBuf();
1021 return GetFullPath();
1025 // Return the long form of the path (returns identity on non-Windows platforms)
1026 wxString
wxFileName::GetLongPath() const
1029 path
= GetFullPath();
1031 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1032 bool success
= FALSE
;
1034 // VZ: this code was disabled, why?
1035 #if 0 // wxUSE_DYNLIB_CLASS
1036 typedef DWORD (*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
1038 static bool s_triedToLoad
= FALSE
;
1040 if ( !s_triedToLoad
)
1042 s_triedToLoad
= TRUE
;
1043 wxDllType dllKernel
= wxDllLoader::LoadLibrary(_T("kernel32"));
1046 // may succeed or fail depending on the Windows version
1047 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
1049 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameW"));
1051 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameA"));
1054 wxDllLoader::UnloadLibrary(dllKernel
);
1056 if ( s_pfnGetLongPathName
)
1058 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1059 bool ok
= dwSize
> 0;
1063 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1067 ok
= (*s_pfnGetLongPathName
)
1070 pathOut
.GetWriteBuf(sz
),
1073 pathOut
.UngetWriteBuf();
1083 #endif // wxUSE_DYNLIB_CLASS
1087 // The OS didn't support GetLongPathName, or some other error.
1088 // We need to call FindFirstFile on each component in turn.
1090 WIN32_FIND_DATA findFileData
;
1092 pathOut
= wxEmptyString
;
1094 wxArrayString dirs
= GetDirs();
1095 dirs
.Add(GetFullName());
1099 size_t count
= dirs
.GetCount();
1100 for ( size_t i
= 0; i
< count
; i
++ )
1102 // We're using pathOut to collect the long-name path, but using a
1103 // temporary for appending the last path component which may be
1105 tmpPath
= pathOut
+ dirs
[i
];
1107 if ( tmpPath
.empty() )
1110 if ( tmpPath
.Last() == wxT(':') )
1112 // Can't pass a drive and root dir to FindFirstFile,
1113 // so continue to next dir
1114 tmpPath
+= wxFILE_SEP_PATH
;
1119 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
1120 if (hFind
== INVALID_HANDLE_VALUE
)
1122 // Error: return immediately with the original path
1126 pathOut
+= findFileData
.cFileName
;
1127 if ( (i
< (count
-1)) )
1128 pathOut
+= wxFILE_SEP_PATH
;
1135 #endif // Win32/!Win32
1140 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
1142 if (format
== wxPATH_NATIVE
)
1144 #if defined(__WXMSW__) || defined(__WXPM__)
1145 format
= wxPATH_DOS
;
1146 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1147 format
= wxPATH_MAC
;
1148 #elif defined(__VMS)
1149 format
= wxPATH_VMS
;
1151 format
= wxPATH_UNIX
;
1157 // ----------------------------------------------------------------------------
1158 // path splitting function
1159 // ----------------------------------------------------------------------------
1162 void wxFileName::SplitPath(const wxString
& fullpathWithVolume
,
1163 wxString
*pstrVolume
,
1167 wxPathFormat format
)
1169 format
= GetFormat(format
);
1171 wxString fullpath
= fullpathWithVolume
;
1173 // under VMS the end of the path is ']', not the path separator used to
1174 // separate the components
1175 wxString sepPath
= format
== wxPATH_VMS
? wxString(_T(']'))
1176 : GetPathSeparators(format
);
1178 // special Windows UNC paths hack: transform \\share\path into share:path
1179 if ( format
== wxPATH_DOS
)
1181 if ( fullpath
.length() >= 4 &&
1182 fullpath
[0u] == wxFILE_SEP_PATH_DOS
&&
1183 fullpath
[1u] == wxFILE_SEP_PATH_DOS
)
1185 fullpath
.erase(0, 2);
1187 size_t posFirstSlash
= fullpath
.find_first_of(sepPath
);
1188 if ( posFirstSlash
!= wxString::npos
)
1190 fullpath
[posFirstSlash
] = wxFILE_SEP_DSK
;
1192 // UNC paths are always absolute, right? (FIXME)
1193 fullpath
.insert(posFirstSlash
+ 1, wxFILE_SEP_PATH_DOS
);
1198 // do we have the volume name in the beginning?
1199 wxString sepVol
= GetVolumeSeparator(format
);
1200 if ( !sepVol
.empty() )
1202 size_t posFirstColon
= fullpath
.find_first_of(sepVol
);
1203 if ( posFirstColon
!= wxString::npos
)
1207 *pstrVolume
= fullpath
.Left(posFirstColon
);
1210 // remove the volume name and the separator from the full path
1211 fullpath
.erase(0, posFirstColon
+ sepVol
.length());
1215 // find the positions of the last dot and last path separator in the path
1216 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
1217 size_t posLastSlash
= fullpath
.find_last_of(sepPath
);
1219 if ( (posLastDot
!= wxString::npos
) &&
1220 ((format
== wxPATH_UNIX
) || (format
== wxPATH_VMS
)) )
1222 if ( (posLastDot
== 0) ||
1223 (fullpath
[posLastDot
- 1] == sepPath
[0u] ) )
1225 // under Unix and VMS, dot may be (and commonly is) the first
1226 // character of the filename, don't treat the entire filename as
1227 // extension in this case
1228 posLastDot
= wxString::npos
;
1232 // if we do have a dot and a slash, check that the dot is in the name part
1233 if ( (posLastDot
!= wxString::npos
) &&
1234 (posLastSlash
!= wxString::npos
) &&
1235 (posLastDot
< posLastSlash
) )
1237 // the dot is part of the path, not the start of the extension
1238 posLastDot
= wxString::npos
;
1241 // now fill in the variables provided by user
1244 if ( posLastSlash
== wxString::npos
)
1251 // take everything up to the path separator but take care to make
1252 // tha path equal to something like '/', not empty, for the files
1253 // immediately under root directory
1254 size_t len
= posLastSlash
;
1258 *pstrPath
= fullpath
.Left(len
);
1260 // special VMS hack: remove the initial bracket
1261 if ( format
== wxPATH_VMS
)
1263 if ( (*pstrPath
)[0u] == _T('[') )
1264 pstrPath
->erase(0, 1);
1271 // take all characters starting from the one after the last slash and
1272 // up to, but excluding, the last dot
1273 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
1275 if ( posLastDot
== wxString::npos
)
1277 // take all until the end
1278 count
= wxString::npos
;
1280 else if ( posLastSlash
== wxString::npos
)
1284 else // have both dot and slash
1286 count
= posLastDot
- posLastSlash
- 1;
1289 *pstrName
= fullpath
.Mid(nStart
, count
);
1294 if ( posLastDot
== wxString::npos
)
1301 // take everything after the dot
1302 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
1308 void wxFileName::SplitPath(const wxString
& fullpath
,
1312 wxPathFormat format
)
1315 SplitPath(fullpath
, &volume
, path
, name
, ext
, format
);
1317 if ( path
&& !volume
.empty() )
1319 path
->Prepend(volume
+ GetVolumeSeparator(format
));
1323 // ----------------------------------------------------------------------------
1325 // ----------------------------------------------------------------------------
1327 bool wxFileName::SetTimes(const wxDateTime
*dtCreate
,
1328 const wxDateTime
*dtAccess
,
1329 const wxDateTime
*dtMod
)
1331 #if defined(__UNIX_LIKE__)
1332 if ( !dtAccess
&& !dtMod
)
1334 // can't modify the creation time anyhow, don't try
1338 // if dtAccess or dtMod is not specified, use the other one (which must be
1339 // non NULL because of the test above) for both times
1341 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
1342 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
1343 if ( utime(GetFullPath(), &utm
) == 0 )
1347 #elif defined(__WIN32__)
1348 wxFileHandle
fh(GetFullPath());
1351 FILETIME ftAccess
, ftCreate
, ftWrite
;
1354 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
1356 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
1358 ConvertWxToFileTime(&ftWrite
, *dtMod
);
1360 if ( ::SetFileTime(fh
,
1361 dtCreate
? &ftCreate
: NULL
,
1362 dtAccess
? &ftAccess
: NULL
,
1363 dtMod
? &ftWrite
: NULL
) )
1368 #else // other platform
1371 wxLogSysError(_("Failed to modify file times for '%s'"),
1372 GetFullPath().c_str());
1377 bool wxFileName::Touch()
1379 #if defined(__UNIX_LIKE__)
1380 // under Unix touching file is simple: just pass NULL to utime()
1381 if ( utime(GetFullPath(), NULL
) == 0 )
1386 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1389 #else // other platform
1390 wxDateTime dtNow
= wxDateTime::Now();
1392 return SetTimes(NULL
/* don't change create time */, &dtNow
, &dtNow
);
1396 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
1398 wxDateTime
*dtChange
) const
1400 #if defined(__UNIX_LIKE__)
1402 if ( wxStat(GetFullPath(), &stBuf
) == 0 )
1405 dtAccess
->Set(stBuf
.st_atime
);
1407 dtMod
->Set(stBuf
.st_mtime
);
1409 dtChange
->Set(stBuf
.st_ctime
);
1413 #elif defined(__WXMAC__)
1415 if ( wxStat(GetFullPath(), &stBuf
) == 0 )
1418 dtAccess
->Set(stBuf
.st_atime
);
1420 dtMod
->Set(stBuf
.st_mtime
);
1422 dtChange
->Set(stBuf
.st_ctime
);
1426 #elif defined(__WIN32__)
1427 wxFileHandle
fh(GetFullPath());
1430 FILETIME ftAccess
, ftCreate
, ftWrite
;
1432 if ( ::GetFileTime(fh
,
1433 dtMod
? &ftCreate
: NULL
,
1434 dtAccess
? &ftAccess
: NULL
,
1435 dtChange
? &ftWrite
: NULL
) )
1438 ConvertFileTimeToWx(dtMod
, ftCreate
);
1440 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1442 ConvertFileTimeToWx(dtChange
, ftWrite
);
1447 #else // other platform
1450 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1451 GetFullPath().c_str());