]>
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 // the character separating the extension from the base name
46 #define EXT_SEP _T('.')
48 // ============================================================================
50 // ============================================================================
52 // ----------------------------------------------------------------------------
53 // wxFileName construction
54 // ----------------------------------------------------------------------------
56 void wxFileName::Assign( const wxFileName
&filepath
)
58 m_ext
= filepath
.GetExt();
59 m_name
= filepath
.GetName();
60 m_dirs
= filepath
.GetDirs();
63 void wxFileName::Assign( const wxString
& path
,
68 wxStringTokenizer
tn(path
, GetPathSeparators(format
),
69 wxTOKEN_RET_EMPTY_ALL
);
72 while ( tn
.HasMoreTokens() )
74 wxString token
= tn
.GetNextToken();
76 // If the path starts with a slash, we need the first
77 // dir entry to be an empty for later reassembly.
78 if (first
|| !token
.IsEmpty())
88 void wxFileName::Assign(const wxString
& fullpath
,
91 wxString path
, name
, ext
;
92 wxSplitPath(fullpath
, &path
, &name
, &ext
);
94 Assign(path
, name
, ext
, format
);
97 void wxFileName::Assign(const wxString
& path
,
98 const wxString
& fullname
,
102 wxSplitPath(fullname
, NULL
/* no path */, &name
, &ext
);
104 Assign(path
, name
, ext
, format
);
107 void wxFileName::Clear()
111 m_ext
= wxEmptyString
;
115 wxFileName
wxFileName::FileName(const wxString
& file
)
117 return wxFileName(file
);
121 wxFileName
wxFileName::DirName(const wxString
& dir
)
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 bool wxFileName::FileExists()
134 return wxFileName::FileExists( GetFullPath() );
137 bool wxFileName::FileExists( const wxString
&file
)
139 return ::wxFileExists( file
);
142 bool wxFileName::DirExists()
144 return wxFileName::DirExists( GetFullPath() );
147 bool wxFileName::DirExists( const wxString
&dir
)
149 return ::wxDirExists( dir
);
152 // ----------------------------------------------------------------------------
153 // CWD and HOME stuff
154 // ----------------------------------------------------------------------------
156 void wxFileName::AssignCwd()
158 AssignDir(wxFileName::GetCwd());
162 wxString
wxFileName::GetCwd()
167 bool wxFileName::SetCwd()
169 return wxFileName::SetCwd( GetFullPath() );
172 bool wxFileName::SetCwd( const wxString
&cwd
)
174 return ::wxSetWorkingDirectory( cwd
);
177 void wxFileName::AssignHomeDir()
179 AssignDir(wxFileName::GetHomeDir());
182 wxString
wxFileName::GetHomeDir()
184 return ::wxGetHomeDir();
187 void wxFileName::AssignTempFileName( const wxString
&prefix
)
190 if ( wxGetTempFileName(prefix
, fullname
) )
200 // ----------------------------------------------------------------------------
201 // directory operations
202 // ----------------------------------------------------------------------------
204 bool wxFileName::Mkdir( int perm
)
206 return wxFileName::Mkdir( GetFullPath(), perm
);
209 bool wxFileName::Mkdir( const wxString
&dir
, int perm
)
211 return ::wxMkdir( dir
, perm
);
214 bool wxFileName::Rmdir()
216 return wxFileName::Rmdir( GetFullPath() );
219 bool wxFileName::Rmdir( const wxString
&dir
)
221 return ::wxRmdir( dir
);
224 // ----------------------------------------------------------------------------
225 // path normalization
226 // ----------------------------------------------------------------------------
228 bool wxFileName::Normalize(wxPathNormalize flags
,
232 // the existing path components
233 wxArrayString dirs
= GetDirs();
235 // the path to prepend in front to make the path absolute
238 format
= GetFormat(format
);
240 // make the path absolute
241 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
246 curDir
.AssignDir(cwd
);
249 // handle ~ stuff under Unix only
250 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
252 if ( !dirs
.IsEmpty() )
254 wxString dir
= dirs
[0u];
255 if ( !dir
.empty() && dir
[0u] == _T('~') )
257 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
266 wxArrayString dirsNew
= curDir
.GetDirs();
267 size_t count
= dirs
.GetCount();
268 for ( size_t n
= 0; n
< count
; n
++ )
270 dirsNew
.Add(dirs
[n
]);
276 // now deal with ".", ".." and the rest
278 size_t count
= dirs
.GetCount();
279 for ( size_t n
= 0; n
< count
; n
++ )
281 wxString dir
= dirs
[n
];
283 if ( flags
&& wxPATH_NORM_DOTS
)
285 if ( dir
== wxT(".") )
291 if ( dir
== wxT("..") )
293 if ( m_dirs
.IsEmpty() )
295 wxLogError(_("The path '%s' contains too many \"..\"!"),
296 GetFullPath().c_str());
300 m_dirs
.Remove(m_dirs
.GetCount() - 1);
305 if ( flags
& wxPATH_NORM_ENV_VARS
)
307 dir
= wxExpandEnvVars(dir
);
310 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
318 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
320 // VZ: expand env vars here too?
329 // ----------------------------------------------------------------------------
330 // filename kind tests
331 // ----------------------------------------------------------------------------
333 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
335 wxFileName fn1
= *this,
338 // get cwd only once - small time saving
339 wxString cwd
= wxGetCwd();
340 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
341 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
343 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
346 // TODO: compare inodes for Unix, this works even when filenames are
347 // different but files are the same (symlinks) (VZ)
353 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
355 // only DOS filenames are case-sensitive
356 return GetFormat(format
) != wxPATH_DOS
;
359 bool wxFileName::IsRelative( wxPathFormat format
)
361 return !IsAbsolute(format
);
364 bool wxFileName::IsAbsolute( wxPathFormat format
)
366 wxChar ch
= m_dirs
.IsEmpty() ? _T('\0') : m_dirs
[0u][0u];
368 // the path is absolute if it starts with a path separator or, only for
369 // Unix filenames, with "~" or "~user"
370 return IsPathSeparator(ch
, format
) ||
371 (GetFormat(format
) == wxPATH_UNIX
&& ch
== _T('~') );
375 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
378 switch ( GetFormat(format
) )
381 // accept both as native APIs do
386 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
402 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
404 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
407 bool wxFileName::IsWild( wxPathFormat format
)
409 // FIXME: this is probably false for Mac and this is surely wrong for most
410 // of Unix shells (think about "[...]")
411 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
414 // ----------------------------------------------------------------------------
415 // path components manipulation
416 // ----------------------------------------------------------------------------
418 void wxFileName::AppendDir( const wxString
&dir
)
423 void wxFileName::PrependDir( const wxString
&dir
)
425 m_dirs
.Insert( dir
, 0 );
428 void wxFileName::InsertDir( int before
, const wxString
&dir
)
430 m_dirs
.Insert( dir
, before
);
433 void wxFileName::RemoveDir( int pos
)
435 m_dirs
.Remove( (size_t)pos
);
438 // ----------------------------------------------------------------------------
440 // ----------------------------------------------------------------------------
442 wxString
wxFileName::GetFullName() const
444 wxString fullname
= m_name
;
445 if ( !m_ext
.empty() )
447 fullname
<< EXT_SEP
<< m_ext
;
453 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
455 format
= GetFormat( format
);
458 size_t count
= m_dirs
.GetCount();
459 for ( size_t i
= 0; i
< count
; i
++ )
462 if ( add_separator
|| (i
< count
) )
463 ret
+= wxFILE_SEP_PATH
;
469 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
471 return GetPathWithSep() + GetFullName();
474 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
476 if (format
== wxPATH_NATIVE
)
478 #if defined(__WXMSW__) || defined(__WXPM__)
480 #elif defined(__WXMAC__)
483 format
= wxPATH_UNIX
;