]>
git.saurik.com Git - wxWidgets.git/blob - src/common/filename.cpp
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 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
46 // wxFileName construction
47 // ----------------------------------------------------------------------------
49 void wxFileName::Assign( const wxFileName
&filepath
)
51 m_ext
= filepath
.GetExt();
52 m_name
= filepath
.GetName();
53 m_dirs
= filepath
.GetDirs();
56 void wxFileName::Assign( const wxString
& path
,
61 wxStringTokenizer
tn(path
, GetPathSeparators(format
),
62 wxTOKEN_RET_EMPTY_ALL
);
65 while ( tn
.HasMoreTokens() )
67 wxString token
= tn
.GetNextToken();
69 // If the path starts with a slash, we need the first
70 // dir entry to be an empty for later reassembly.
71 if (first
|| !token
.IsEmpty())
81 void wxFileName::Assign(const wxString
& fullpath
,
84 wxString path
, name
, ext
;
85 SplitPath(fullpath
, &path
, &name
, &ext
, format
);
87 Assign(path
, name
, ext
, format
);
90 void wxFileName::Assign(const wxString
& path
,
91 const wxString
& fullname
,
95 SplitPath(fullname
, NULL
/* no path */, &name
, &ext
, format
);
97 Assign(path
, name
, ext
, format
);
100 void wxFileName::Clear()
104 m_ext
= wxEmptyString
;
108 wxFileName
wxFileName::FileName(const wxString
& file
)
110 return wxFileName(file
);
114 wxFileName
wxFileName::DirName(const wxString
& dir
)
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 bool wxFileName::FileExists()
127 return wxFileName::FileExists( GetFullPath() );
130 bool wxFileName::FileExists( const wxString
&file
)
132 return ::wxFileExists( file
);
135 bool wxFileName::DirExists()
137 return wxFileName::DirExists( GetFullPath() );
140 bool wxFileName::DirExists( const wxString
&dir
)
142 return ::wxDirExists( dir
);
145 // ----------------------------------------------------------------------------
146 // CWD and HOME stuff
147 // ----------------------------------------------------------------------------
149 void wxFileName::AssignCwd()
151 AssignDir(wxFileName::GetCwd());
155 wxString
wxFileName::GetCwd()
160 bool wxFileName::SetCwd()
162 return wxFileName::SetCwd( GetFullPath() );
165 bool wxFileName::SetCwd( const wxString
&cwd
)
167 return ::wxSetWorkingDirectory( cwd
);
170 void wxFileName::AssignHomeDir()
172 AssignDir(wxFileName::GetHomeDir());
175 wxString
wxFileName::GetHomeDir()
177 return ::wxGetHomeDir();
180 void wxFileName::AssignTempFileName( const wxString
&prefix
)
183 if ( wxGetTempFileName(prefix
, fullname
) )
193 // ----------------------------------------------------------------------------
194 // directory operations
195 // ----------------------------------------------------------------------------
197 bool wxFileName::Mkdir( int perm
)
199 return wxFileName::Mkdir( GetFullPath(), perm
);
202 bool wxFileName::Mkdir( const wxString
&dir
, int perm
)
204 return ::wxMkdir( dir
, perm
);
207 bool wxFileName::Rmdir()
209 return wxFileName::Rmdir( GetFullPath() );
212 bool wxFileName::Rmdir( const wxString
&dir
)
214 return ::wxRmdir( dir
);
217 // ----------------------------------------------------------------------------
218 // path normalization
219 // ----------------------------------------------------------------------------
221 bool wxFileName::Normalize(wxPathNormalize flags
,
225 // the existing path components
226 wxArrayString dirs
= GetDirs();
228 // the path to prepend in front to make the path absolute
231 format
= GetFormat(format
);
233 // make the path absolute
234 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
239 curDir
.AssignDir(cwd
);
242 // handle ~ stuff under Unix only
243 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
245 if ( !dirs
.IsEmpty() )
247 wxString dir
= dirs
[0u];
248 if ( !dir
.empty() && dir
[0u] == _T('~') )
250 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
259 wxArrayString dirsNew
= curDir
.GetDirs();
260 size_t count
= dirs
.GetCount();
261 for ( size_t n
= 0; n
< count
; n
++ )
263 dirsNew
.Add(dirs
[n
]);
269 // now deal with ".", ".." and the rest
271 size_t count
= dirs
.GetCount();
272 for ( size_t n
= 0; n
< count
; n
++ )
274 wxString dir
= dirs
[n
];
276 if ( flags
&& wxPATH_NORM_DOTS
)
278 if ( dir
== wxT(".") )
284 if ( dir
== wxT("..") )
286 if ( m_dirs
.IsEmpty() )
288 wxLogError(_("The path '%s' contains too many \"..\"!"),
289 GetFullPath().c_str());
293 m_dirs
.Remove(m_dirs
.GetCount() - 1);
298 if ( flags
& wxPATH_NORM_ENV_VARS
)
300 dir
= wxExpandEnvVars(dir
);
303 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
311 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
313 // VZ: expand env vars here too?
322 // ----------------------------------------------------------------------------
323 // filename kind tests
324 // ----------------------------------------------------------------------------
326 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
328 wxFileName fn1
= *this,
331 // get cwd only once - small time saving
332 wxString cwd
= wxGetCwd();
333 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
334 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
336 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
339 // TODO: compare inodes for Unix, this works even when filenames are
340 // different but files are the same (symlinks) (VZ)
346 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
348 // only DOS filenames are case-sensitive
349 return GetFormat(format
) != wxPATH_DOS
;
352 bool wxFileName::IsRelative( wxPathFormat format
)
354 return !IsAbsolute(format
);
357 bool wxFileName::IsAbsolute( wxPathFormat format
)
359 wxChar ch
= m_dirs
.IsEmpty() ? _T('\0') : m_dirs
[0u][0u];
361 // the path is absolute if it starts with a path separator or, only for
362 // Unix filenames, with "~" or "~user"
363 return IsPathSeparator(ch
, format
) ||
364 (GetFormat(format
) == wxPATH_UNIX
&& ch
== _T('~') );
368 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
371 switch ( GetFormat(format
) )
374 // accept both as native APIs do
375 seps
<< wxFILE_SEP_PATH_UNIX
<< wxFILE_SEP_PATH_DOS
;
379 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
383 seps
= wxFILE_SEP_PATH_UNIX
;
387 seps
= wxFILE_SEP_PATH_MAC
;
395 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
397 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
400 bool wxFileName::IsWild( wxPathFormat format
)
402 // FIXME: this is probably false for Mac and this is surely wrong for most
403 // of Unix shells (think about "[...]")
404 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
407 // ----------------------------------------------------------------------------
408 // path components manipulation
409 // ----------------------------------------------------------------------------
411 void wxFileName::AppendDir( const wxString
&dir
)
416 void wxFileName::PrependDir( const wxString
&dir
)
418 m_dirs
.Insert( dir
, 0 );
421 void wxFileName::InsertDir( int before
, const wxString
&dir
)
423 m_dirs
.Insert( dir
, before
);
426 void wxFileName::RemoveDir( int pos
)
428 m_dirs
.Remove( (size_t)pos
);
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
435 wxString
wxFileName::GetFullName() const
437 wxString fullname
= m_name
;
438 if ( !m_ext
.empty() )
440 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
446 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
448 format
= GetFormat( format
);
451 size_t count
= m_dirs
.GetCount();
452 for ( size_t i
= 0; i
< count
; i
++ )
455 if ( add_separator
|| (i
< count
) )
456 ret
+= wxFILE_SEP_PATH
;
462 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
464 return GetPathWithSep() + GetFullName();
467 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
469 if (format
== wxPATH_NATIVE
)
471 #if defined(__WXMSW__) || defined(__WXPM__)
473 #elif defined(__WXMAC__)
476 format
= wxPATH_UNIX
;
482 // ----------------------------------------------------------------------------
483 // path splitting function
484 // ----------------------------------------------------------------------------
486 void wxFileName::SplitPath(const wxString
& fullpath
,
492 format
= GetFormat(format
);
494 // find the positions of the last dot and last path separator in the path
495 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
496 size_t posLastSlash
= fullpath
.find_last_of(GetPathSeparators(format
));
498 if ( (posLastDot
!= wxString::npos
) && (format
== wxPATH_UNIX
) )
500 if ( (posLastDot
== 0) ||
501 (fullpath
[posLastDot
- 1] == wxFILE_SEP_PATH_UNIX
) )
503 // under Unix, dot may be (and commonly is) the first character of
504 // the filename, don't treat the entire filename as extension in
506 posLastDot
= wxString::npos
;
510 if ( (posLastDot
!= wxString::npos
) && (posLastDot
< posLastSlash
) )
512 // the dot is part of the path, not the start of the extension
513 posLastDot
= wxString::npos
;
516 // now fill in the variables provided by user
519 if ( posLastSlash
== wxString::npos
)
526 // take all until the separator
527 *pstrPath
= fullpath
.Left(posLastSlash
);
533 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
534 size_t count
= posLastDot
== wxString::npos
? wxString::npos
535 : posLastDot
- posLastSlash
;
537 *pstrName
= fullpath
.Mid(nStart
, count
);
542 if ( posLastDot
== wxString::npos
)
549 // take everything after the dot
550 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);