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
48 #include "wx/msw/winundef.h"
51 // at least some of these are required for file mod time
53 #include <sys/types.h>
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxFileName construction
70 // ----------------------------------------------------------------------------
72 void wxFileName::Assign( const wxFileName
&filepath
)
74 m_ext
= filepath
.GetExt();
75 m_name
= filepath
.GetName();
76 m_dirs
= filepath
.GetDirs();
79 void wxFileName::Assign( const wxString
& path
,
84 wxStringTokenizer
tn(path
, GetPathSeparators(format
),
85 wxTOKEN_RET_EMPTY_ALL
);
88 while ( tn
.HasMoreTokens() )
90 wxString token
= tn
.GetNextToken();
92 // If the path starts with a slash, we need the first
93 // dir entry to be an empty for later reassembly.
94 if (first
|| !token
.IsEmpty())
104 void wxFileName::Assign(const wxString
& fullpath
,
107 wxString path
, name
, ext
;
108 SplitPath(fullpath
, &path
, &name
, &ext
, format
);
110 Assign(path
, name
, ext
, format
);
113 void wxFileName::Assign(const wxString
& path
,
114 const wxString
& fullname
,
118 SplitPath(fullname
, NULL
/* no path */, &name
, &ext
, format
);
120 Assign(path
, name
, ext
, format
);
123 void wxFileName::Clear()
127 m_ext
= wxEmptyString
;
131 wxFileName
wxFileName::FileName(const wxString
& file
)
133 return wxFileName(file
);
137 wxFileName
wxFileName::DirName(const wxString
& dir
)
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 bool wxFileName::FileExists()
150 return wxFileName::FileExists( GetFullPath() );
153 bool wxFileName::FileExists( const wxString
&file
)
155 return ::wxFileExists( file
);
158 bool wxFileName::DirExists()
160 return wxFileName::DirExists( GetFullPath() );
163 bool wxFileName::DirExists( const wxString
&dir
)
165 return ::wxDirExists( dir
);
168 wxDateTime
wxFileName::GetModificationTime()
172 stat( GetFullName().fn_str(), &buff
);
174 #if !defined( __EMX__ ) && !defined(__VMS)
176 lstat( GetFullName().fn_str(), &lbuff
);
177 struct tm
*t
= localtime( &lbuff
.st_mtime
);
179 struct tm
*t
= localtime( &buff
.st_mtime
);
182 wxDateTime
ret( t
->tm_mday
, (wxDateTime::Month
)t
->tm_mon
, t
->tm_year
+1900, t
->tm_hour
, t
->tm_min
, t
->tm_sec
);
185 wxDateTime ret
= wxDateTime::Now();
191 // ----------------------------------------------------------------------------
192 // CWD and HOME stuff
193 // ----------------------------------------------------------------------------
195 void wxFileName::AssignCwd()
197 AssignDir(wxFileName::GetCwd());
201 wxString
wxFileName::GetCwd()
206 bool wxFileName::SetCwd()
208 return wxFileName::SetCwd( GetFullPath() );
211 bool wxFileName::SetCwd( const wxString
&cwd
)
213 return ::wxSetWorkingDirectory( cwd
);
216 void wxFileName::AssignHomeDir()
218 AssignDir(wxFileName::GetHomeDir());
221 wxString
wxFileName::GetHomeDir()
223 return ::wxGetHomeDir();
226 void wxFileName::AssignTempFileName( const wxString
&prefix
)
229 if ( wxGetTempFileName(prefix
, fullname
) )
239 // ----------------------------------------------------------------------------
240 // directory operations
241 // ----------------------------------------------------------------------------
243 bool wxFileName::Mkdir( int perm
, bool full
)
245 return wxFileName::Mkdir( GetFullPath(), perm
, full
);
248 bool wxFileName::Mkdir( const wxString
&dir
, int perm
, bool full
)
252 wxFileName
filename(dir
);
253 wxArrayString dirs
= filename
.GetDirs();
254 dirs
.Add(filename
.GetName());
256 size_t count
= dirs
.GetCount();
260 for ( i
= 0; i
< count
; i
++ )
264 if (currPath
.Last() == wxT(':'))
266 // Can't create a root directory so continue to next dir
267 currPath
+= wxFILE_SEP_PATH
;
271 if (!DirExists(currPath
))
272 if (!wxMkdir(currPath
, perm
))
275 if ( (i
< (count
-1)) )
276 currPath
+= wxFILE_SEP_PATH
;
279 return (noErrors
== 0);
283 return ::wxMkdir( dir
, perm
);
286 bool wxFileName::Rmdir()
288 return wxFileName::Rmdir( GetFullPath() );
291 bool wxFileName::Rmdir( const wxString
&dir
)
293 return ::wxRmdir( dir
);
296 // ----------------------------------------------------------------------------
297 // path normalization
298 // ----------------------------------------------------------------------------
300 bool wxFileName::Normalize(wxPathNormalize flags
,
304 // the existing path components
305 wxArrayString dirs
= GetDirs();
307 // the path to prepend in front to make the path absolute
310 format
= GetFormat(format
);
312 // make the path absolute
313 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
318 curDir
.AssignDir(cwd
);
321 // handle ~ stuff under Unix only
322 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
324 if ( !dirs
.IsEmpty() )
326 wxString dir
= dirs
[0u];
327 if ( !dir
.empty() && dir
[0u] == _T('~') )
329 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
338 wxArrayString dirsNew
= curDir
.GetDirs();
339 size_t count
= dirs
.GetCount();
340 for ( size_t n
= 0; n
< count
; n
++ )
342 dirsNew
.Add(dirs
[n
]);
348 // now deal with ".", ".." and the rest
350 size_t count
= dirs
.GetCount();
351 for ( size_t n
= 0; n
< count
; n
++ )
353 wxString dir
= dirs
[n
];
355 if ( flags
&& wxPATH_NORM_DOTS
)
357 if ( dir
== wxT(".") )
363 if ( dir
== wxT("..") )
365 if ( m_dirs
.IsEmpty() )
367 wxLogError(_("The path '%s' contains too many \"..\"!"),
368 GetFullPath().c_str());
372 m_dirs
.Remove(m_dirs
.GetCount() - 1);
377 if ( flags
& wxPATH_NORM_ENV_VARS
)
379 dir
= wxExpandEnvVars(dir
);
382 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
390 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
392 // VZ: expand env vars here too?
398 #if defined(__WXMSW__) && defined(__WIN32__)
399 if (flags
& wxPATH_NORM_LONG
)
401 Assign(GetLongPath());
408 // ----------------------------------------------------------------------------
409 // filename kind tests
410 // ----------------------------------------------------------------------------
412 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
414 wxFileName fn1
= *this,
417 // get cwd only once - small time saving
418 wxString cwd
= wxGetCwd();
419 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
420 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
422 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
425 // TODO: compare inodes for Unix, this works even when filenames are
426 // different but files are the same (symlinks) (VZ)
432 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
434 // only DOS filenames are case-sensitive
435 return GetFormat(format
) != wxPATH_DOS
;
438 bool wxFileName::IsRelative( wxPathFormat format
)
440 return !IsAbsolute(format
);
443 bool wxFileName::IsAbsolute( wxPathFormat format
)
445 wxChar ch
= m_dirs
.IsEmpty() ? _T('\0') : m_dirs
[0u][0u];
447 // Hack to cope with e.g. c:\thing - need something better
448 wxChar driveSep
= _T('\0');
449 if (!m_dirs
.IsEmpty() && m_dirs
[0].Length() > 1)
450 driveSep
= m_dirs
[0u][1u];
452 // the path is absolute if it starts with a path separator or, only for
453 // Unix filenames, with "~" or "~user"
454 return IsPathSeparator(ch
, format
) ||
455 driveSep
== _T(':') ||
456 (GetFormat(format
) == wxPATH_UNIX
&& ch
== _T('~') );
460 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
463 switch ( GetFormat(format
) )
466 // accept both as native APIs do
467 seps
<< wxFILE_SEP_PATH_UNIX
<< wxFILE_SEP_PATH_DOS
;
471 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
475 seps
= wxFILE_SEP_PATH_UNIX
;
479 seps
= wxFILE_SEP_PATH_MAC
;
487 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
489 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
492 bool wxFileName::IsWild( wxPathFormat format
)
494 // FIXME: this is probably false for Mac and this is surely wrong for most
495 // of Unix shells (think about "[...]")
496 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
499 // ----------------------------------------------------------------------------
500 // path components manipulation
501 // ----------------------------------------------------------------------------
503 void wxFileName::AppendDir( const wxString
&dir
)
508 void wxFileName::PrependDir( const wxString
&dir
)
510 m_dirs
.Insert( dir
, 0 );
513 void wxFileName::InsertDir( int before
, const wxString
&dir
)
515 m_dirs
.Insert( dir
, before
);
518 void wxFileName::RemoveDir( int pos
)
520 m_dirs
.Remove( (size_t)pos
);
523 // ----------------------------------------------------------------------------
525 // ----------------------------------------------------------------------------
527 void wxFileName::SetFullName(const wxString
& fullname
)
529 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
532 wxString
wxFileName::GetFullName() const
534 wxString fullname
= m_name
;
535 if ( !m_ext
.empty() )
537 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
543 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
545 format
= GetFormat( format
);
548 size_t count
= m_dirs
.GetCount();
549 for ( size_t i
= 0; i
< count
; i
++ )
552 if ( add_separator
|| (i
< count
) )
553 ret
+= wxFILE_SEP_PATH
;
559 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
561 return GetPathWithSep() + GetFullName();
564 // Return the short form of the path (returns identity on non-Windows platforms)
565 wxString
wxFileName::GetShortPath() const
567 #if defined(__WXMSW__) && defined(__WIN32__)
568 wxString
path(GetFullPath());
570 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
574 ok
= ::GetShortPathName
577 pathOut
.GetWriteBuf(sz
),
580 pathOut
.UngetWriteBuf();
587 return GetFullPath();
591 // Return the long form of the path (returns identity on non-Windows platforms)
592 wxString
wxFileName::GetLongPath() const
594 #if defined(__WXMSW__) && defined(__WIN32__)
595 wxString
path(GetFullPath());
597 bool success
= FALSE
;
599 #if wxUSE_DYNLIB_CLASS
600 typedef DWORD (*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
602 static bool s_triedToLoad
= FALSE
;
603 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
605 if ( !s_triedToLoad
)
607 s_triedToLoad
= TRUE
;
609 wxDllType dllKernel
= wxDllLoader::LoadLibrary(_T("kernel32"));
610 if ( 0 ) // dllKernel )
612 // may succeed or fail depending on the Windows version
614 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameW"));
616 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameA"));
619 wxDllLoader::UnloadLibrary(dllKernel
);
621 if ( s_pfnGetLongPathName
)
623 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
624 bool ok
= dwSize
> 0;
628 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
632 ok
= (*s_pfnGetLongPathName
)
635 pathOut
.GetWriteBuf(sz
),
638 pathOut
.UngetWriteBuf();
649 // wxUSE_DYNLIB_CLASS
653 // The OS didn't support GetLongPathName, or some other error.
654 // We need to call FindFirstFile on each component in turn.
656 WIN32_FIND_DATA findFileData
;
658 pathOut
= wxEmptyString
;
660 wxArrayString dirs
= GetDirs();
663 size_t count
= dirs
.GetCount();
667 for ( i
= 0; i
< count
; i
++ )
669 // We're using pathOut to collect the long-name path,
670 // but using a temporary for appending the last path component which may be short-name
671 tmpPath
= pathOut
+ dirs
[i
];
673 if (tmpPath
.Last() == wxT(':'))
675 // Can't pass a drive and root dir to FindFirstFile,
676 // so continue to next dir
677 tmpPath
+= wxFILE_SEP_PATH
;
682 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
683 if (hFind
== INVALID_HANDLE_VALUE
)
685 // Error: return immediately with the original path
690 pathOut
+= findFileData
.cFileName
;
691 if ( (i
< (count
-1)) )
692 pathOut
+= wxFILE_SEP_PATH
;
700 return GetFullPath();
704 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
706 if (format
== wxPATH_NATIVE
)
708 #if defined(__WXMSW__) || defined(__WXPM__)
710 #elif defined(__WXMAC__)
711 format
= wxPATH_UNIX
; // that's the way the rest of wx' code works right now
713 format
= wxPATH_UNIX
;
719 // ----------------------------------------------------------------------------
720 // path splitting function
721 // ----------------------------------------------------------------------------
723 void wxFileName::SplitPath(const wxString
& fullpath
,
729 format
= GetFormat(format
);
731 // find the positions of the last dot and last path separator in the path
732 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
733 size_t posLastSlash
= fullpath
.find_last_of(GetPathSeparators(format
));
735 if ( (posLastDot
!= wxString::npos
) && (format
== wxPATH_UNIX
) )
737 if ( (posLastDot
== 0) ||
738 (fullpath
[posLastDot
- 1] == wxFILE_SEP_PATH_UNIX
) )
740 // under Unix, dot may be (and commonly is) the first character of
741 // the filename, don't treat the entire filename as extension in
743 posLastDot
= wxString::npos
;
747 // if we do have a dot and a slash, check that the dot is in the name part
748 if ( (posLastDot
!= wxString::npos
) &&
749 (posLastSlash
!= wxString::npos
) &&
750 (posLastDot
< posLastSlash
) )
752 // the dot is part of the path, not the start of the extension
753 posLastDot
= wxString::npos
;
756 // now fill in the variables provided by user
759 if ( posLastSlash
== wxString::npos
)
766 // take all until the separator
767 *pstrPath
= fullpath
.Left(posLastSlash
);
773 // take all characters starting from the one after the last slash and
774 // up to, but excluding, the last dot
775 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
777 if ( posLastDot
== wxString::npos
)
779 // take all until the end
780 count
= wxString::npos
;
782 else if ( posLastSlash
== wxString::npos
)
786 else // have both dot and slash
788 count
= posLastDot
- posLastSlash
- 1;
791 *pstrName
= fullpath
.Mid(nStart
, count
);
796 if ( posLastDot
== wxString::npos
)
803 // take everything after the dot
804 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);