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();
218 dirs
.Add(filename
.GetName());
220 size_t count
= dirs
.GetCount();
224 for ( i
= 0; i
< count
; i
++ )
228 if (currPath
.Last() == wxT(':'))
230 // Can't create a root directory so continue to next dir
231 currPath
+= wxFILE_SEP_PATH
;
235 if (!DirExists(currPath
))
236 if (!wxMkdir(currPath
, perm
))
239 if ( (i
< (count
-1)) )
240 currPath
+= wxFILE_SEP_PATH
;
243 return (noErrors
== 0);
247 return ::wxMkdir( dir
, perm
);
250 bool wxFileName::Rmdir()
252 return wxFileName::Rmdir( GetFullPath() );
255 bool wxFileName::Rmdir( const wxString
&dir
)
257 return ::wxRmdir( dir
);
260 // ----------------------------------------------------------------------------
261 // path normalization
262 // ----------------------------------------------------------------------------
264 bool wxFileName::Normalize(wxPathNormalize flags
,
268 // the existing path components
269 wxArrayString dirs
= GetDirs();
271 // the path to prepend in front to make the path absolute
274 format
= GetFormat(format
);
276 // make the path absolute
277 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
282 curDir
.AssignDir(cwd
);
285 // handle ~ stuff under Unix only
286 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
288 if ( !dirs
.IsEmpty() )
290 wxString dir
= dirs
[0u];
291 if ( !dir
.empty() && dir
[0u] == _T('~') )
293 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
302 wxArrayString dirsNew
= curDir
.GetDirs();
303 size_t count
= dirs
.GetCount();
304 for ( size_t n
= 0; n
< count
; n
++ )
306 dirsNew
.Add(dirs
[n
]);
312 // now deal with ".", ".." and the rest
314 size_t count
= dirs
.GetCount();
315 for ( size_t n
= 0; n
< count
; n
++ )
317 wxString dir
= dirs
[n
];
319 if ( flags
&& wxPATH_NORM_DOTS
)
321 if ( dir
== wxT(".") )
327 if ( dir
== wxT("..") )
329 if ( m_dirs
.IsEmpty() )
331 wxLogError(_("The path '%s' contains too many \"..\"!"),
332 GetFullPath().c_str());
336 m_dirs
.Remove(m_dirs
.GetCount() - 1);
341 if ( flags
& wxPATH_NORM_ENV_VARS
)
343 dir
= wxExpandEnvVars(dir
);
346 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
354 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
356 // VZ: expand env vars here too?
362 #if defined(__WXMSW__) && defined(__WIN32__)
363 if (flags
& wxPATH_NORM_LONG
)
365 Assign(GetLongPath());
372 // ----------------------------------------------------------------------------
373 // filename kind tests
374 // ----------------------------------------------------------------------------
376 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
378 wxFileName fn1
= *this,
381 // get cwd only once - small time saving
382 wxString cwd
= wxGetCwd();
383 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
384 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
386 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
389 // TODO: compare inodes for Unix, this works even when filenames are
390 // different but files are the same (symlinks) (VZ)
396 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
398 // only DOS filenames are case-sensitive
399 return GetFormat(format
) != wxPATH_DOS
;
402 bool wxFileName::IsRelative( wxPathFormat format
)
404 return !IsAbsolute(format
);
407 bool wxFileName::IsAbsolute( wxPathFormat format
)
409 wxChar ch
= m_dirs
.IsEmpty() ? _T('\0') : m_dirs
[0u][0u];
411 // Hack to cope with e.g. c:\thing - need something better
412 wxChar driveSep
= _T('\0');
413 if (!m_dirs
.IsEmpty() && m_dirs
[0].Length() > 1)
414 driveSep
= m_dirs
[0u][1u];
416 // the path is absolute if it starts with a path separator or, only for
417 // Unix filenames, with "~" or "~user"
418 return IsPathSeparator(ch
, format
) ||
419 driveSep
== _T(':') ||
420 (GetFormat(format
) == wxPATH_UNIX
&& ch
== _T('~') );
424 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
427 switch ( GetFormat(format
) )
430 // accept both as native APIs do
431 seps
<< wxFILE_SEP_PATH_UNIX
<< wxFILE_SEP_PATH_DOS
;
435 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
439 seps
= wxFILE_SEP_PATH_UNIX
;
443 seps
= wxFILE_SEP_PATH_MAC
;
451 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
453 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
456 bool wxFileName::IsWild( wxPathFormat format
)
458 // FIXME: this is probably false for Mac and this is surely wrong for most
459 // of Unix shells (think about "[...]")
460 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
463 // ----------------------------------------------------------------------------
464 // path components manipulation
465 // ----------------------------------------------------------------------------
467 void wxFileName::AppendDir( const wxString
&dir
)
472 void wxFileName::PrependDir( const wxString
&dir
)
474 m_dirs
.Insert( dir
, 0 );
477 void wxFileName::InsertDir( int before
, const wxString
&dir
)
479 m_dirs
.Insert( dir
, before
);
482 void wxFileName::RemoveDir( int pos
)
484 m_dirs
.Remove( (size_t)pos
);
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 void wxFileName::SetFullName(const wxString
& fullname
)
493 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
496 wxString
wxFileName::GetFullName() const
498 wxString fullname
= m_name
;
499 if ( !m_ext
.empty() )
501 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
507 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
509 format
= GetFormat( format
);
512 size_t count
= m_dirs
.GetCount();
513 for ( size_t i
= 0; i
< count
; i
++ )
516 if ( add_separator
|| (i
< count
) )
517 ret
+= wxFILE_SEP_PATH
;
523 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
525 return GetPathWithSep() + GetFullName();
528 // Return the short form of the path (returns identity on non-Windows platforms)
529 wxString
wxFileName::GetShortPath() const
531 #if defined(__WXMSW__) && defined(__WIN32__)
532 wxString
path(GetFullPath());
534 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
538 ok
= ::GetShortPathName
541 pathOut
.GetWriteBuf(sz
),
544 pathOut
.UngetWriteBuf();
551 return GetFullPath();
555 // Return the long form of the path (returns identity on non-Windows platforms)
556 wxString
wxFileName::GetLongPath() const
558 #if defined(__WXMSW__) && defined(__WIN32__)
559 wxString
path(GetFullPath());
561 bool success
= FALSE
;
563 #if wxUSE_DYNLIB_CLASS
564 typedef DWORD (*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
566 static bool s_triedToLoad
= FALSE
;
567 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
569 if ( !s_triedToLoad
)
571 s_triedToLoad
= TRUE
;
573 wxDllType dllKernel
= wxDllLoader::LoadLibrary(_T("kernel32"));
574 if ( 0 ) // dllKernel )
576 // may succeed or fail depending on the Windows version
578 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameW"));
580 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameA"));
583 wxDllLoader::UnloadLibrary(dllKernel
);
585 if ( s_pfnGetLongPathName
)
587 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
588 bool ok
= dwSize
> 0;
592 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
596 ok
= (*s_pfnGetLongPathName
)
599 pathOut
.GetWriteBuf(sz
),
602 pathOut
.UngetWriteBuf();
613 // wxUSE_DYNLIB_CLASS
617 // The OS didn't support GetLongPathName, or some other error.
618 // We need to call FindFirstFile on each component in turn.
620 WIN32_FIND_DATA findFileData
;
622 pathOut
= wxEmptyString
;
624 wxArrayString dirs
= GetDirs();
627 size_t count
= dirs
.GetCount();
631 for ( i
= 0; i
< count
; i
++ )
633 // We're using pathOut to collect the long-name path,
634 // but using a temporary for appending the last path component which may be short-name
635 tmpPath
= pathOut
+ dirs
[i
];
637 if (tmpPath
.Last() == wxT(':'))
639 // Can't pass a drive and root dir to FindFirstFile,
640 // so continue to next dir
641 tmpPath
+= wxFILE_SEP_PATH
;
646 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
647 if (hFind
== INVALID_HANDLE_VALUE
)
649 // Error: return immediately with the original path
654 pathOut
+= findFileData
.cFileName
;
655 if ( (i
< (count
-1)) )
656 pathOut
+= wxFILE_SEP_PATH
;
664 return GetFullPath();
668 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
670 if (format
== wxPATH_NATIVE
)
672 #if defined(__WXMSW__) || defined(__WXPM__)
674 #elif defined(__WXMAC__)
675 format
= wxPATH_UNIX
; // that's the way the rest of wx' code works right now
677 format
= wxPATH_UNIX
;
683 // ----------------------------------------------------------------------------
684 // path splitting function
685 // ----------------------------------------------------------------------------
687 void wxFileName::SplitPath(const wxString
& fullpath
,
693 format
= GetFormat(format
);
695 // find the positions of the last dot and last path separator in the path
696 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
697 size_t posLastSlash
= fullpath
.find_last_of(GetPathSeparators(format
));
699 if ( (posLastDot
!= wxString::npos
) && (format
== wxPATH_UNIX
) )
701 if ( (posLastDot
== 0) ||
702 (fullpath
[posLastDot
- 1] == wxFILE_SEP_PATH_UNIX
) )
704 // under Unix, dot may be (and commonly is) the first character of
705 // the filename, don't treat the entire filename as extension in
707 posLastDot
= wxString::npos
;
711 // if we do have a dot and a slash, check that the dot is in the name part
712 if ( (posLastDot
!= wxString::npos
) &&
713 (posLastSlash
!= wxString::npos
) &&
714 (posLastDot
< posLastSlash
) )
716 // the dot is part of the path, not the start of the extension
717 posLastDot
= wxString::npos
;
720 // now fill in the variables provided by user
723 if ( posLastSlash
== wxString::npos
)
730 // take all until the separator
731 *pstrPath
= fullpath
.Left(posLastSlash
);
737 // take all characters starting from the one after the last slash and
738 // up to, but excluding, the last dot
739 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
741 if ( posLastDot
== wxString::npos
)
743 // take all until the end
744 count
= wxString::npos
;
746 else if ( posLastSlash
== wxString::npos
)
750 else // have both dot and slash
752 count
= posLastDot
- posLastSlash
- 1;
755 *pstrName
= fullpath
.Mid(nStart
, count
);
760 if ( posLastDot
== wxString::npos
)
767 // take everything after the dot
768 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);