]>
git.saurik.com Git - wxWidgets.git/blob - src/common/filename.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileName - encapsulates candy
4 // Author: Robert Roebling
8 // Copyright: (c) 2000 Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filename.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
28 #include "wx/filename.h"
29 #include "wx/tokenzr.h"
32 //----------------------------------------------------------------------------
34 //----------------------------------------------------------------------------
36 wxFileName::wxFileName( const wxFileName
&filepath
)
38 m_ext
= filepath
.GetExt();
39 m_name
= filepath
.GetName();
40 const wxArrayString
&dirs
= filepath
.GetDirs();
41 for (size_t i
= 0; i
< dirs
.GetCount(); i
++)
42 m_dirs
.Add( dirs
[i
] );
45 void wxFileName::Assign( const wxFileName
&filepath
)
48 m_ext
= filepath
.GetExt();
49 m_name
= filepath
.GetName();
50 const wxArrayString
&dirs
= filepath
.GetDirs();
51 for (size_t i
= 0; i
< dirs
.GetCount(); i
++)
52 m_dirs
.Add( dirs
[i
] );
55 void wxFileName::Assign( const wxString
&path
, bool dir_only
, wxPathFormat format
)
57 m_ext
= wxEmptyString
;
58 m_name
= wxEmptyString
;
61 format
= GetFormat( format
);
64 if (format
== wxPATH_DOS
)
69 if (format
== wxPATH_UNIX
)
78 wxStringTokenizer
tn( path
, seps
, wxTOKEN_RET_EMPTY_ALL
);
80 while (tn
.HasMoreTokens())
82 wxString
token( tn
.GetNextToken() );
84 // If the path starts with a slash, we need the first
85 // dir entry to be an empty for later reassembly.
87 if (first
|| !token
.IsEmpty())
94 // make last m_dir -> m_name
95 size_t last
= m_dirs
.GetCount();
96 if (last
== 0) return;
98 m_name
= m_dirs
[last
];
99 m_dirs
.Remove( last
);
101 if (m_name
== wxT(".")) return;
102 if (m_name
== wxT("..")) return;
105 int pos
= m_name
.Find( wxT('.') );
106 if (pos
== -1) return;
108 bool has_starting_dot
= (pos
== 0);
109 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
115 pos
= m_name
.Find( wxT('.') );
119 m_name
.Prepend( "." );
124 m_ext
.Remove( 0, pos
+1 );
126 m_name
.Remove( pos
, m_name
.Len()-pos
);
128 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
131 m_name
.Prepend( "." );
137 bool wxFileName::FileExists()
139 return wxFileName::FileExists( GetFullPath() );
142 bool wxFileName::FileExists( const wxString
&file
)
144 return ::wxFileExists( file
);
147 bool wxFileName::DirExists()
149 return wxFileName::DirExists( GetFullPath() );
152 bool wxFileName::DirExists( const wxString
&dir
)
154 return ::wxDirExists( dir
);
157 void wxFileName::AssignCwd()
159 Assign( wxFileName::GetCwd(), TRUE
);
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 Assign( wxFileName::GetHomeDir(), TRUE
);
182 wxString
wxFileName::GetHomeDir()
184 return ::wxGetHomeDir();
187 void wxFileName::AssignTempFileName( const wxString
&prefix
)
191 bool wxFileName::Mkdir( int perm
)
193 return wxFileName::Mkdir( GetFullPath(), perm
);
196 bool wxFileName::Mkdir( const wxString
&dir
, int perm
)
198 return ::wxMkdir( dir
, perm
);
201 bool wxFileName::Rmdir()
203 return wxFileName::Rmdir( GetFullPath() );
206 bool wxFileName::Rmdir( const wxString
&dir
)
208 return ::wxRmdir( dir
);
211 bool wxFileName::Normalize( const wxString
&cwd
, const wxString
&home
)
213 wxFileName
tmp( *this );
215 const wxArrayString
&dirs
= tmp
.GetDirs();
217 if (dirs
.GetCount() == 0) return FALSE
;
221 if (dirs
[0] == wxT("."))
223 if (cwd
== wxEmptyString
)
224 Assign( wxFileName::GetCwd(), TRUE
);
230 if (dirs
[0] == wxT(".."))
232 if (cwd
== wxEmptyString
)
233 Assign( wxFileName::GetCwd(), TRUE
);
236 m_dirs
.Remove( m_dirs
.GetCount()-1 );
240 if (dirs
[0] == wxT("~"))
242 if (home
== wxEmptyString
)
243 Assign( wxFileName::GetHomeDir(), TRUE
);
249 for (size_t i
= start
; i
< dirs
.GetCount(); i
++)
251 if (dirs
[i
] == wxT(".")) continue;
253 if (dirs
[i
] == wxT(".."))
255 m_dirs
.Remove( m_dirs
.GetCount()-1 );
259 // expand env vars here ?
261 m_dirs
.Add( dirs
[i
] );
264 m_name
= tmp
.GetName();
265 m_ext
= tmp
.GetExt();
270 bool wxFileName::SameAs( const wxFileName
&filepath
, bool upper_case
)
272 wxString
file1( GetFullPath() );
273 wxString
file2( filepath
.GetFullPath() );
277 file1
.MakeUpper(); // what does MSW do to non-ascii chars etc? native funcs?
281 return (file1
== file2
);
284 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
286 format
= GetFormat( format
);
288 return (format
!= wxPATH_DOS
);
291 bool wxFileName::IsRelative( wxPathFormat format
)
293 format
= GetFormat( format
);
295 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
297 if ((format
== wxPATH_UNIX
) && (i
== 0) && (m_dirs
[0] == wxT("~"))) return TRUE
;
299 if (m_dirs
[i
] == wxT(".")) return TRUE
;
300 if (m_dirs
[i
] == wxT("..")) return TRUE
;
306 bool wxFileName::IsAbsolute( wxPathFormat format
)
308 return (!IsRelative(format
));
311 bool wxFileName::IsWild( wxPathFormat format
)
313 format
= GetFormat( format
);
315 if (format
== wxPATH_DOS
)
317 if (m_name
.Find( wxT('*') ) != -1) return TRUE
;
318 if (m_name
.Find( wxT('?') ) != -1) return TRUE
;
322 if (m_name
.Find( wxT('*') ) != -1) return TRUE
;
328 void wxFileName::AppendDir( const wxString
&dir
)
333 void wxFileName::PrependDir( const wxString
&dir
)
335 m_dirs
.Insert( dir
, 0 );
338 void wxFileName::InsertDir( int before
, const wxString
&dir
)
340 m_dirs
.Insert( dir
, before
);
343 void wxFileName::RemoveDir( int pos
)
345 m_dirs
.Remove( (size_t)pos
);
348 void wxFileName::SetFullName( const wxString name
, wxPathFormat format
)
350 format
= GetFormat( format
);
353 m_ext
= wxEmptyString
;
355 if (m_name
== wxT(".")) return;
356 if (m_name
== wxT("..")) return;
359 int pos
= m_name
.Find( wxT('.') );
360 if (pos
== -1) return;
362 bool has_starting_dot
= (pos
== 0);
363 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
369 pos
= m_name
.Find( wxT('.') );
373 m_name
.Prepend( "." );
379 m_ext
.Remove( 0, pos
+1 );
381 m_name
.Remove( pos
, m_name
.Len()-pos
);
383 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
386 m_name
.Prepend( "." );
391 wxString
wxFileName::GetFullName()
393 wxString
ret( m_name
);
394 if (!m_ext
.IsEmpty())
402 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
404 format
= GetFormat( format
);
407 if (format
== wxPATH_DOS
)
409 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
412 if (add_separator
|| (i
!= m_dirs
.GetCount()-1))
417 if (format
== wxPATH_UNIX
)
419 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
422 if (add_separator
|| (i
!= m_dirs
.GetCount()-1))
428 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
431 if (add_separator
|| (i
!= m_dirs
.GetCount()-1))
439 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
441 format
= GetFormat( format
);
444 if (format
== wxPATH_DOS
)
446 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
453 if (format
== wxPATH_UNIX
)
455 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
463 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
472 if (!m_ext
.IsEmpty())
481 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
483 if (format
== wxPATH_NATIVE
)
485 #if defined(__WXMSW__) || defined(__WXPM__)
488 #if defined(__WXMAC__)
491 #if !defined(__WXMSW__) && !defined(__WXPM__) && !defined(__WXMAC__)
492 format
= wxPATH_UNIX
;