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 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "filename.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/filename.h"
37 #include "wx/tokenzr.h"
38 #include "wx/config.h" // for wxExpandEnvVars
41 #if wxUSE_DYNLIB_CLASS
42 #include "wx/dynlib.h"
45 // For GetShort/LongPathName
49 #include "wx/msw/winundef.h"
52 // utime() is POSIX so should normally be available on all Unices
54 #include <sys/types.h>
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // small helper class which opens and closes the file - we use it just to get
63 // a file handle for the given file name to pass it to some Win32 API function
69 wxFileHandle(const wxString
& filename
)
71 m_hFile
= ::CreateFile
74 GENERIC_READ
, // access mask
76 NULL
, // no secutity attr
77 OPEN_EXISTING
, // creation disposition
79 NULL
// no template file
82 if ( m_hFile
== INVALID_HANDLE_VALUE
)
84 wxLogSysError(_("Failed to open '%s' for reading"),
91 if ( m_hFile
!= INVALID_HANDLE_VALUE
)
93 if ( !::CloseHandle(m_hFile
) )
95 wxLogSysError(_("Failed to close file handle"));
100 // return TRUE only if the file could be opened successfully
101 bool IsOk() const { return m_hFile
!= INVALID_HANDLE_VALUE
; }
104 operator HANDLE() const { return m_hFile
; }
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
118 // convert between wxDateTime and FILETIME which is a 64-bit value representing
119 // the number of 100-nanosecond intervals since January 1, 1601.
121 static void ConvertWxToFileTime(FILETIME
*ft
, const wxDateTime
& dt
)
126 static void ConvertFileTimeToWx(wxDateTime
*dt
, const FILETIME
&ft
)
133 // ============================================================================
135 // ============================================================================
137 // ----------------------------------------------------------------------------
138 // wxFileName construction
139 // ----------------------------------------------------------------------------
141 void wxFileName::Assign( const wxFileName
&filepath
)
143 m_ext
= filepath
.GetExt();
144 m_name
= filepath
.GetName();
145 m_dirs
= filepath
.GetDirs();
148 void wxFileName::Assign( const wxString
& path
,
149 const wxString
& name
,
151 wxPathFormat format
)
153 wxStringTokenizer
tn(path
, GetPathSeparators(format
),
154 wxTOKEN_RET_EMPTY_ALL
);
157 while ( tn
.HasMoreTokens() )
159 wxString token
= tn
.GetNextToken();
161 // If the path starts with a slash (or two for a network path),
162 // we need the first dir entry to be an empty for later reassembly.
163 if ((i
< 2) || !token
.IsEmpty())
173 void wxFileName::Assign(const wxString
& fullpath
,
176 wxString path
, name
, ext
;
177 SplitPath(fullpath
, &path
, &name
, &ext
, format
);
179 Assign(path
, name
, ext
, format
);
182 void wxFileName::Assign(const wxString
& path
,
183 const wxString
& fullname
,
187 SplitPath(fullname
, NULL
/* no path */, &name
, &ext
, format
);
189 Assign(path
, name
, ext
, format
);
192 void wxFileName::Clear()
196 m_ext
= wxEmptyString
;
200 wxFileName
wxFileName::FileName(const wxString
& file
)
202 return wxFileName(file
);
206 wxFileName
wxFileName::DirName(const wxString
& dir
)
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 bool wxFileName::FileExists()
219 return wxFileName::FileExists( GetFullPath() );
222 bool wxFileName::FileExists( const wxString
&file
)
224 return ::wxFileExists( file
);
227 bool wxFileName::DirExists()
229 return wxFileName::DirExists( GetFullPath() );
232 bool wxFileName::DirExists( const wxString
&dir
)
234 return ::wxDirExists( dir
);
237 // ----------------------------------------------------------------------------
238 // CWD and HOME stuff
239 // ----------------------------------------------------------------------------
241 void wxFileName::AssignCwd()
243 AssignDir(wxFileName::GetCwd());
247 wxString
wxFileName::GetCwd()
252 bool wxFileName::SetCwd()
254 return wxFileName::SetCwd( GetFullPath() );
257 bool wxFileName::SetCwd( const wxString
&cwd
)
259 return ::wxSetWorkingDirectory( cwd
);
262 void wxFileName::AssignHomeDir()
264 AssignDir(wxFileName::GetHomeDir());
267 wxString
wxFileName::GetHomeDir()
269 return ::wxGetHomeDir();
272 void wxFileName::AssignTempFileName( const wxString
&prefix
)
275 if ( wxGetTempFileName(prefix
, fullname
) )
285 // ----------------------------------------------------------------------------
286 // directory operations
287 // ----------------------------------------------------------------------------
289 bool wxFileName::Mkdir( int perm
, bool full
)
291 return wxFileName::Mkdir( GetFullPath(), perm
, full
);
294 bool wxFileName::Mkdir( const wxString
&dir
, int perm
, bool full
)
298 wxFileName
filename(dir
);
299 wxArrayString dirs
= filename
.GetDirs();
300 dirs
.Add(filename
.GetName());
302 size_t count
= dirs
.GetCount();
306 for ( i
= 0; i
< count
; i
++ )
310 if (currPath
.Last() == wxT(':'))
312 // Can't create a root directory so continue to next dir
313 currPath
+= wxFILE_SEP_PATH
;
317 if (!DirExists(currPath
))
318 if (!wxMkdir(currPath
, perm
))
321 if ( (i
< (count
-1)) )
322 currPath
+= wxFILE_SEP_PATH
;
325 return (noErrors
== 0);
329 return ::wxMkdir( dir
, perm
);
332 bool wxFileName::Rmdir()
334 return wxFileName::Rmdir( GetFullPath() );
337 bool wxFileName::Rmdir( const wxString
&dir
)
339 return ::wxRmdir( dir
);
342 // ----------------------------------------------------------------------------
343 // path normalization
344 // ----------------------------------------------------------------------------
346 bool wxFileName::Normalize(wxPathNormalize flags
,
350 // the existing path components
351 wxArrayString dirs
= GetDirs();
353 // the path to prepend in front to make the path absolute
356 format
= GetFormat(format
);
358 // make the path absolute
359 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
364 curDir
.AssignDir(cwd
);
367 // handle ~ stuff under Unix only
368 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
370 if ( !dirs
.IsEmpty() )
372 wxString dir
= dirs
[0u];
373 if ( !dir
.empty() && dir
[0u] == _T('~') )
375 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
384 wxArrayString dirsNew
= curDir
.GetDirs();
385 size_t count
= dirs
.GetCount();
386 for ( size_t n
= 0; n
< count
; n
++ )
388 dirsNew
.Add(dirs
[n
]);
394 // now deal with ".", ".." and the rest
396 size_t count
= dirs
.GetCount();
397 for ( size_t n
= 0; n
< count
; n
++ )
399 wxString dir
= dirs
[n
];
401 if ( flags
&& wxPATH_NORM_DOTS
)
403 if ( dir
== wxT(".") )
409 if ( dir
== wxT("..") )
411 if ( m_dirs
.IsEmpty() )
413 wxLogError(_("The path '%s' contains too many \"..\"!"),
414 GetFullPath().c_str());
418 m_dirs
.Remove(m_dirs
.GetCount() - 1);
423 if ( flags
& wxPATH_NORM_ENV_VARS
)
425 dir
= wxExpandEnvVars(dir
);
428 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
436 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
438 // VZ: expand env vars here too?
444 #if defined(__WXMSW__) && defined(__WIN32__)
445 if (flags
& wxPATH_NORM_LONG
)
447 Assign(GetLongPath());
454 // ----------------------------------------------------------------------------
455 // filename kind tests
456 // ----------------------------------------------------------------------------
458 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
460 wxFileName fn1
= *this,
463 // get cwd only once - small time saving
464 wxString cwd
= wxGetCwd();
465 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
466 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
468 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
471 // TODO: compare inodes for Unix, this works even when filenames are
472 // different but files are the same (symlinks) (VZ)
478 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
480 // only DOS filenames are case-sensitive
481 return GetFormat(format
) != wxPATH_DOS
;
484 bool wxFileName::IsRelative( wxPathFormat format
)
486 return !IsAbsolute(format
);
489 bool wxFileName::IsAbsolute( wxPathFormat format
)
491 wxChar ch
= m_dirs
.IsEmpty() ? _T('\0') : m_dirs
[0u][0u];
493 // Hack to cope with e.g. c:\thing - need something better
494 wxChar driveSep
= _T('\0');
495 if (!m_dirs
.IsEmpty() && m_dirs
[0].Length() > 1)
496 driveSep
= m_dirs
[0u][1u];
498 // the path is absolute if it starts with a path separator or, only for
499 // Unix filenames, with "~" or "~user"
500 return IsPathSeparator(ch
, format
) ||
501 driveSep
== _T(':') ||
502 (GetFormat(format
) == wxPATH_UNIX
&& ch
== _T('~') );
506 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
509 switch ( GetFormat(format
) )
512 // accept both as native APIs do
513 seps
<< wxFILE_SEP_PATH_UNIX
<< wxFILE_SEP_PATH_DOS
;
517 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
521 seps
= wxFILE_SEP_PATH_UNIX
;
525 seps
= wxFILE_SEP_PATH_MAC
;
533 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
535 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
538 bool wxFileName::IsWild( wxPathFormat format
)
540 // FIXME: this is probably false for Mac and this is surely wrong for most
541 // of Unix shells (think about "[...]")
543 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
546 // ----------------------------------------------------------------------------
547 // path components manipulation
548 // ----------------------------------------------------------------------------
550 void wxFileName::AppendDir( const wxString
&dir
)
555 void wxFileName::PrependDir( const wxString
&dir
)
557 m_dirs
.Insert( dir
, 0 );
560 void wxFileName::InsertDir( int before
, const wxString
&dir
)
562 m_dirs
.Insert( dir
, before
);
565 void wxFileName::RemoveDir( int pos
)
567 m_dirs
.Remove( (size_t)pos
);
570 // ----------------------------------------------------------------------------
572 // ----------------------------------------------------------------------------
574 void wxFileName::SetFullName(const wxString
& fullname
)
576 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
579 wxString
wxFileName::GetFullName() const
581 wxString fullname
= m_name
;
582 if ( !m_ext
.empty() )
584 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
590 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
592 format
= GetFormat( format
);
595 size_t count
= m_dirs
.GetCount();
596 for ( size_t i
= 0; i
< count
; i
++ )
599 if ( add_separator
|| (i
< count
) )
600 ret
+= wxFILE_SEP_PATH
;
606 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
608 format
= GetFormat( format
);
611 if (format
== wxPATH_DOS
)
613 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
620 if (format
== wxPATH_UNIX
)
622 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
630 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
639 if (!m_ext
.IsEmpty())
648 // Return the short form of the path (returns identity on non-Windows platforms)
649 wxString
wxFileName::GetShortPath() const
651 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
652 wxString
path(GetFullPath());
654 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
658 ok
= ::GetShortPathName
661 pathOut
.GetWriteBuf(sz
),
664 pathOut
.UngetWriteBuf();
671 return GetFullPath();
675 // Return the long form of the path (returns identity on non-Windows platforms)
676 wxString
wxFileName::GetLongPath() const
678 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
679 wxString
path(GetFullPath());
681 bool success
= FALSE
;
683 // VZ: this code was disabled, why?
684 #if 0 // wxUSE_DYNLIB_CLASS
685 typedef DWORD (*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
687 static bool s_triedToLoad
= FALSE
;
689 if ( !s_triedToLoad
)
691 s_triedToLoad
= TRUE
;
692 wxDllType dllKernel
= wxDllLoader::LoadLibrary(_T("kernel32"));
695 // may succeed or fail depending on the Windows version
696 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
698 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameW"));
700 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameA"));
703 wxDllLoader::UnloadLibrary(dllKernel
);
705 if ( s_pfnGetLongPathName
)
707 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
708 bool ok
= dwSize
> 0;
712 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
716 ok
= (*s_pfnGetLongPathName
)
719 pathOut
.GetWriteBuf(sz
),
722 pathOut
.UngetWriteBuf();
732 #endif // wxUSE_DYNLIB_CLASS
736 // The OS didn't support GetLongPathName, or some other error.
737 // We need to call FindFirstFile on each component in turn.
739 WIN32_FIND_DATA findFileData
;
741 pathOut
= wxEmptyString
;
743 wxArrayString dirs
= GetDirs();
744 dirs
.Add(GetFullName());
746 size_t count
= dirs
.GetCount();
750 for ( i
= 0; i
< count
; i
++ )
752 // We're using pathOut to collect the long-name path,
753 // but using a temporary for appending the last path component which may be short-name
754 tmpPath
= pathOut
+ dirs
[i
];
756 if (tmpPath
.Last() == wxT(':'))
758 // Can't pass a drive and root dir to FindFirstFile,
759 // so continue to next dir
760 tmpPath
+= wxFILE_SEP_PATH
;
765 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
766 if (hFind
== INVALID_HANDLE_VALUE
)
768 // Error: return immediately with the original path
773 pathOut
+= findFileData
.cFileName
;
774 if ( (i
< (count
-1)) )
775 pathOut
+= wxFILE_SEP_PATH
;
784 return GetFullPath();
788 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
790 if (format
== wxPATH_NATIVE
)
792 #if defined(__WXMSW__) || defined(__WXPM__)
794 #elif defined(__WXMAC__)
797 format
= wxPATH_UNIX
;
803 // ----------------------------------------------------------------------------
804 // path splitting function
805 // ----------------------------------------------------------------------------
807 void wxFileName::SplitPath(const wxString
& fullpath
,
813 format
= GetFormat(format
);
815 // find the positions of the last dot and last path separator in the path
816 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
817 size_t posLastSlash
= fullpath
.find_last_of(GetPathSeparators(format
));
819 if ( (posLastDot
!= wxString::npos
) && (format
== wxPATH_UNIX
) )
821 if ( (posLastDot
== 0) ||
822 (fullpath
[posLastDot
- 1] == wxFILE_SEP_PATH_UNIX
) )
824 // under Unix, dot may be (and commonly is) the first character of
825 // the filename, don't treat the entire filename as extension in
827 posLastDot
= wxString::npos
;
831 // if we do have a dot and a slash, check that the dot is in the name part
832 if ( (posLastDot
!= wxString::npos
) &&
833 (posLastSlash
!= wxString::npos
) &&
834 (posLastDot
< posLastSlash
) )
836 // the dot is part of the path, not the start of the extension
837 posLastDot
= wxString::npos
;
840 // now fill in the variables provided by user
843 if ( posLastSlash
== wxString::npos
)
850 // take all until the separator
851 *pstrPath
= fullpath
.Left(posLastSlash
);
857 // take all characters starting from the one after the last slash and
858 // up to, but excluding, the last dot
859 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
861 if ( posLastDot
== wxString::npos
)
863 // take all until the end
864 count
= wxString::npos
;
866 else if ( posLastSlash
== wxString::npos
)
870 else // have both dot and slash
872 count
= posLastDot
- posLastSlash
- 1;
875 *pstrName
= fullpath
.Mid(nStart
, count
);
880 if ( posLastDot
== wxString::npos
)
887 // take everything after the dot
888 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
893 // ----------------------------------------------------------------------------
895 // ----------------------------------------------------------------------------
897 bool wxFileName::SetTimes(const wxDateTime
*dtCreate
,
898 const wxDateTime
*dtAccess
,
899 const wxDateTime
*dtMod
)
901 #if defined(__UNIX_LIKE__)
902 if ( !dtAccess
&& !dtMod
)
904 // can't modify the creation time anyhow, don't try
908 // if dtAccess or dtMod is not specified, use the other one (which must be
909 // non NULL because of the test above) for both times
911 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
912 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
913 if ( utime(GetFullPath(), &utm
) == 0 )
917 #elif defined(__WIN32__)
918 wxFileHandle
fh(GetFullPath());
921 FILETIME ftAccess
, ftCreate
, ftWrite
;
924 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
926 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
928 ConvertWxToFileTime(&ftWrite
, *dtMod
);
930 if ( ::SetFileTime(fh
,
931 dtCreate
? &ftCreate
: NULL
,
932 dtAccess
? &ftAccess
: NULL
,
933 dtMod
? &ftWrite
: NULL
) )
938 #else // other platform
941 wxLogSysError(_("Failed to modify file times for '%s'"),
942 GetFullPath().c_str());
947 bool wxFileName::Touch()
949 #if defined(__UNIX_LIKE__)
950 // under Unix touching file is simple: just pass NULL to utime()
951 if ( utime(GetFullPath(), NULL
) == 0 )
956 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
959 #else // other platform
960 wxDateTime dtNow
= wxDateTime::Now();
962 return SetTimes(NULL
/* don't change create time */, &dtNow
, &dtNow
);
966 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
968 wxDateTime
*dtChange
) const
970 #if defined(__UNIX_LIKE__)
972 if ( wxStat(GetFullPath(), &stBuf
) == 0 )
975 dtAccess
->Set(stBuf
.st_atime
);
977 dtMod
->Set(stBuf
.st_mtime
);
979 dtChange
->Set(stBuf
.st_ctime
);
983 #elif defined(__WIN32__)
984 wxFileHandle
fh(GetFullPath());
987 FILETIME ftAccess
, ftCreate
, ftWrite
;
989 if ( ::GetFileTime(fh
,
990 dtMod
? &ftCreate
: NULL
,
991 dtAccess
? &ftAccess
: NULL
,
992 dtChange
? &ftWrite
: NULL
) )
995 ConvertFileTimeToWx(dtMod
, ftCreate
);
997 ConvertFileTimeToWx(dtAccess
, ftAccess
);
999 ConvertFileTimeToWx(dtChange
, ftWrite
);
1004 #else // other platform
1007 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1008 GetFullPath().c_str());