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 // ============================================================================
53 // ============================================================================
55 // ----------------------------------------------------------------------------
56 // wxFileName construction
57 // ----------------------------------------------------------------------------
59 void wxFileName::Assign( const wxFileName
&filepath
)
61 m_ext
= filepath
.GetExt();
62 m_name
= filepath
.GetName();
63 m_dirs
= filepath
.GetDirs();
66 void wxFileName::Assign( const wxString
& path
,
71 wxStringTokenizer
tn(path
, GetPathSeparators(format
),
72 wxTOKEN_RET_EMPTY_ALL
);
75 while ( tn
.HasMoreTokens() )
77 wxString token
= tn
.GetNextToken();
79 // If the path starts with a slash, we need the first
80 // dir entry to be an empty for later reassembly.
81 if (first
|| !token
.IsEmpty())
91 void wxFileName::Assign(const wxString
& fullpath
,
94 wxString path
, name
, ext
;
95 SplitPath(fullpath
, &path
, &name
, &ext
, format
);
97 Assign(path
, name
, ext
, format
);
100 void wxFileName::Assign(const wxString
& path
,
101 const wxString
& fullname
,
105 SplitPath(fullname
, NULL
/* no path */, &name
, &ext
, format
);
107 Assign(path
, name
, ext
, format
);
110 void wxFileName::Clear()
114 m_ext
= wxEmptyString
;
118 wxFileName
wxFileName::FileName(const wxString
& file
)
120 return wxFileName(file
);
124 wxFileName
wxFileName::DirName(const wxString
& dir
)
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 bool wxFileName::FileExists()
137 return wxFileName::FileExists( GetFullPath() );
140 bool wxFileName::FileExists( const wxString
&file
)
142 return ::wxFileExists( file
);
145 bool wxFileName::DirExists()
147 return wxFileName::DirExists( GetFullPath() );
150 bool wxFileName::DirExists( const wxString
&dir
)
152 return ::wxDirExists( dir
);
155 // ----------------------------------------------------------------------------
156 // CWD and HOME stuff
157 // ----------------------------------------------------------------------------
159 void wxFileName::AssignCwd()
161 AssignDir(wxFileName::GetCwd());
165 wxString
wxFileName::GetCwd()
170 bool wxFileName::SetCwd()
172 return wxFileName::SetCwd( GetFullPath() );
175 bool wxFileName::SetCwd( const wxString
&cwd
)
177 return ::wxSetWorkingDirectory( cwd
);
180 void wxFileName::AssignHomeDir()
182 AssignDir(wxFileName::GetHomeDir());
185 wxString
wxFileName::GetHomeDir()
187 return ::wxGetHomeDir();
190 void wxFileName::AssignTempFileName( const wxString
&prefix
)
193 if ( wxGetTempFileName(prefix
, fullname
) )
203 // ----------------------------------------------------------------------------
204 // directory operations
205 // ----------------------------------------------------------------------------
207 bool wxFileName::Mkdir( int perm
, bool full
)
209 return wxFileName::Mkdir( GetFullPath(), perm
, full
);
212 bool wxFileName::Mkdir( const wxString
&dir
, int perm
, bool full
)
216 wxFileName
filename(dir
);
217 wxArrayString dirs
= filename
.GetDirs();
219 size_t count
= dirs
.GetCount();
223 for ( i
= 0; i
< count
; i
++ )
227 if (currPath
.Last() == wxT(':'))
229 // Can't create a root directory so continue to next dir
230 currPath
+= wxFILE_SEP_PATH
;
234 if (!DirExists(currPath
))
235 if (!wxMkdir(currPath
, perm
))
238 if ( (i
< (count
-1)) )
239 currPath
+= wxFILE_SEP_PATH
;
242 return (noErrors
== 0);
246 return ::wxMkdir( dir
, perm
);
249 bool wxFileName::Rmdir()
251 return wxFileName::Rmdir( GetFullPath() );
254 bool wxFileName::Rmdir( const wxString
&dir
)
256 return ::wxRmdir( dir
);
259 // ----------------------------------------------------------------------------
260 // path normalization
261 // ----------------------------------------------------------------------------
263 bool wxFileName::Normalize(wxPathNormalize flags
,
267 // the existing path components
268 wxArrayString dirs
= GetDirs();
270 // the path to prepend in front to make the path absolute
273 format
= GetFormat(format
);
275 // make the path absolute
276 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
281 curDir
.AssignDir(cwd
);
284 // handle ~ stuff under Unix only
285 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
287 if ( !dirs
.IsEmpty() )
289 wxString dir
= dirs
[0u];
290 if ( !dir
.empty() && dir
[0u] == _T('~') )
292 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
301 wxArrayString dirsNew
= curDir
.GetDirs();
302 size_t count
= dirs
.GetCount();
303 for ( size_t n
= 0; n
< count
; n
++ )
305 dirsNew
.Add(dirs
[n
]);
311 // now deal with ".", ".." and the rest
313 size_t count
= dirs
.GetCount();
314 for ( size_t n
= 0; n
< count
; n
++ )
316 wxString dir
= dirs
[n
];
318 if ( flags
&& wxPATH_NORM_DOTS
)
320 if ( dir
== wxT(".") )
326 if ( dir
== wxT("..") )
328 if ( m_dirs
.IsEmpty() )
330 wxLogError(_("The path '%s' contains too many \"..\"!"),
331 GetFullPath().c_str());
335 m_dirs
.Remove(m_dirs
.GetCount() - 1);
340 if ( flags
& wxPATH_NORM_ENV_VARS
)
342 dir
= wxExpandEnvVars(dir
);
345 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
353 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
355 // VZ: expand env vars here too?
361 #if defined(__WXMSW__) && defined(__WIN32__)
362 if (flags
& wxPATH_NORM_LONG
)
364 Assign(GetLongPath());
371 // ----------------------------------------------------------------------------
372 // filename kind tests
373 // ----------------------------------------------------------------------------
375 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
377 wxFileName fn1
= *this,
380 // get cwd only once - small time saving
381 wxString cwd
= wxGetCwd();
382 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
383 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
385 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
388 // TODO: compare inodes for Unix, this works even when filenames are
389 // different but files are the same (symlinks) (VZ)
395 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
397 // only DOS filenames are case-sensitive
398 return GetFormat(format
) != wxPATH_DOS
;
401 bool wxFileName::IsRelative( wxPathFormat format
)
403 return !IsAbsolute(format
);
406 bool wxFileName::IsAbsolute( wxPathFormat format
)
408 wxChar ch
= m_dirs
.IsEmpty() ? _T('\0') : m_dirs
[0u][0u];
410 // Hack to cope with e.g. c:\thing - need something better
411 wxChar driveSep
= _T('\0');
412 if (!m_dirs
.IsEmpty() && m_dirs
[0].Length() > 1)
413 driveSep
= m_dirs
[0u][1u];
415 // the path is absolute if it starts with a path separator or, only for
416 // Unix filenames, with "~" or "~user"
417 return IsPathSeparator(ch
, format
) ||
418 driveSep
== _T(':') ||
419 (GetFormat(format
) == wxPATH_UNIX
&& ch
== _T('~') );
423 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
426 switch ( GetFormat(format
) )
429 // accept both as native APIs do
430 seps
<< wxFILE_SEP_PATH_UNIX
<< wxFILE_SEP_PATH_DOS
;
434 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
438 seps
= wxFILE_SEP_PATH_UNIX
;
442 seps
= wxFILE_SEP_PATH_MAC
;
450 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
452 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
455 bool wxFileName::IsWild( wxPathFormat format
)
457 // FIXME: this is probably false for Mac and this is surely wrong for most
458 // of Unix shells (think about "[...]")
459 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
462 // ----------------------------------------------------------------------------
463 // path components manipulation
464 // ----------------------------------------------------------------------------
466 void wxFileName::AppendDir( const wxString
&dir
)
471 void wxFileName::PrependDir( const wxString
&dir
)
473 m_dirs
.Insert( dir
, 0 );
476 void wxFileName::InsertDir( int before
, const wxString
&dir
)
478 m_dirs
.Insert( dir
, before
);
481 void wxFileName::RemoveDir( int pos
)
483 m_dirs
.Remove( (size_t)pos
);
486 // ----------------------------------------------------------------------------
488 // ----------------------------------------------------------------------------
490 void wxFileName::SetFullName(const wxString
& fullname
)
492 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
495 wxString
wxFileName::GetFullName() const
497 wxString fullname
= m_name
;
498 if ( !m_ext
.empty() )
500 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
506 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
508 format
= GetFormat( format
);
511 size_t count
= m_dirs
.GetCount();
512 for ( size_t i
= 0; i
< count
; i
++ )
515 if ( add_separator
|| (i
< count
) )
516 ret
+= wxFILE_SEP_PATH
;
522 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
524 return GetPathWithSep() + GetFullName();
527 // Return the short form of the path (returns identity on non-Windows platforms)
528 wxString
wxFileName::GetShortPath() const
530 #if defined(__WXMSW__) && defined(__WIN32__)
531 wxString
path(GetFullPath());
533 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
537 ok
= ::GetShortPathName
540 pathOut
.GetWriteBuf(sz
),
543 pathOut
.UngetWriteBuf();
550 return GetFullPath();
554 // Return the long form of the path (returns identity on non-Windows platforms)
555 wxString
wxFileName::GetLongPath() const
557 #if defined(__WXMSW__) && defined(__WIN32__)
558 wxString
path(GetFullPath());
560 bool success
= FALSE
;
562 #if wxUSE_DYNLIB_CLASS
563 typedef DWORD (*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
565 static bool s_triedToLoad
= FALSE
;
566 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
568 if ( !s_triedToLoad
)
570 s_triedToLoad
= TRUE
;
572 wxDllType dllKernel
= wxDllLoader::LoadLibrary(_T("kernel32"));
573 if ( 0 ) // dllKernel )
575 // may succeed or fail depending on the Windows version
577 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameW"));
579 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameA"));
582 wxDllLoader::UnloadLibrary(dllKernel
);
584 if ( s_pfnGetLongPathName
)
586 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
587 bool ok
= dwSize
> 0;
591 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
595 ok
= (*s_pfnGetLongPathName
)
598 pathOut
.GetWriteBuf(sz
),
601 pathOut
.UngetWriteBuf();
612 // wxUSE_DYNLIB_CLASS
616 // The OS didn't support GetLongPathName, or some other error.
617 // We need to call FindFirstFile on each component in turn.
619 WIN32_FIND_DATA findFileData
;
621 pathOut
= wxEmptyString
;
623 size_t count
= m_dirs
.GetCount();
627 for ( i
= 0; i
< count
; i
++ )
629 // We're using pathOut to collect the long-name path,
630 // but using a temporary for appending the last path component which may be short-name
631 tmpPath
= pathOut
+ m_dirs
[i
];
633 if (tmpPath
.Last() == wxT(':'))
635 // Can't pass a drive and root dir to FindFirstFile,
636 // so continue to next dir
637 tmpPath
+= wxFILE_SEP_PATH
;
642 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
643 if (hFind
== INVALID_HANDLE_VALUE
)
645 // Error: return immediately with the original path
650 pathOut
+= findFileData
.cFileName
;
651 if ( (i
< (count
-1)) )
652 pathOut
+= wxFILE_SEP_PATH
;
660 return GetFullPath();
664 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
666 if (format
== wxPATH_NATIVE
)
668 #if defined(__WXMSW__) || defined(__WXPM__)
670 #elif defined(__WXMAC__)
671 format
= wxPATH_UNIX
; // that's the way the rest of wx' code works right now
673 format
= wxPATH_UNIX
;
679 // ----------------------------------------------------------------------------
680 // path splitting function
681 // ----------------------------------------------------------------------------
683 void wxFileName::SplitPath(const wxString
& fullpath
,
689 format
= GetFormat(format
);
691 // find the positions of the last dot and last path separator in the path
692 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
693 size_t posLastSlash
= fullpath
.find_last_of(GetPathSeparators(format
));
695 if ( (posLastDot
!= wxString::npos
) && (format
== wxPATH_UNIX
) )
697 if ( (posLastDot
== 0) ||
698 (fullpath
[posLastDot
- 1] == wxFILE_SEP_PATH_UNIX
) )
700 // under Unix, dot may be (and commonly is) the first character of
701 // the filename, don't treat the entire filename as extension in
703 posLastDot
= wxString::npos
;
707 // if we do have a dot and a slash, check that the dot is in the name part
708 if ( (posLastDot
!= wxString::npos
) &&
709 (posLastSlash
!= wxString::npos
) &&
710 (posLastDot
< posLastSlash
) )
712 // the dot is part of the path, not the start of the extension
713 posLastDot
= wxString::npos
;
716 // now fill in the variables provided by user
719 if ( posLastSlash
== wxString::npos
)
726 // take all until the separator
727 *pstrPath
= fullpath
.Left(posLastSlash
);
733 // take all characters starting from the one after the last slash and
734 // up to, but excluding, the last dot
735 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
737 if ( posLastDot
== wxString::npos
)
739 // take all until the end
740 count
= wxString::npos
;
742 else if ( posLastSlash
== wxString::npos
)
746 else // have both dot and slash
748 count
= posLastDot
- posLastSlash
- 1;
751 *pstrName
= fullpath
.Mid(nStart
, count
);
756 if ( posLastDot
== wxString::npos
)
763 // take everything after the dot
764 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);