]>
git.saurik.com Git - wxWidgets.git/blob - src/common/filename.cpp
7fa61c0a556bcd949cd09acf2a920c78fe5d808f
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
);
79 while (tn
.HasMoreTokens())
81 wxString
token( tn
.GetNextToken() );
88 // make last m_dir -> m_name
89 size_t last
= m_dirs
.GetCount();
90 if (last
== 0) return;
92 m_name
= m_dirs
[last
];
93 m_dirs
.Remove( last
);
95 if (m_name
== wxT(".")) return;
96 if (m_name
== wxT("..")) return;
99 int pos
= m_name
.Find( wxT('.') );
100 if (pos
== -1) return;
102 bool has_starting_dot
= (pos
== 0);
103 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
109 pos
= m_name
.Find( wxT('.') );
113 m_name
.Prepend( "." );
118 m_ext
.Remove( 0, pos
+1 );
120 m_name
.Remove( pos
, m_name
.Len()-pos
);
122 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
125 m_name
.Prepend( "." );
131 bool wxFileName::FileExists()
133 return wxFileName::FileExists( GetFullPath() );
136 bool wxFileName::FileExists( const wxString
&file
)
138 return ::wxFileExists( file
);
141 bool wxFileName::DirExists()
143 return wxFileName::DirExists( GetFullPath() );
146 bool wxFileName::DirExists( const wxString
&dir
)
148 return ::wxDirExists( dir
);
151 void wxFileName::AssignCwd()
153 Assign( wxFileName::GetCwd(), TRUE
);
156 wxString
wxFileName::GetCwd()
161 bool wxFileName::SetCwd()
163 return wxFileName::SetCwd( GetFullPath() );
166 bool wxFileName::SetCwd( const wxString
&cwd
)
168 return ::wxSetWorkingDirectory( cwd
);
171 void wxFileName::AssignHomeDir()
173 Assign( wxFileName::GetHomeDir(), TRUE
);
176 wxString
wxFileName::GetHomeDir()
178 return ::wxGetHomeDir();
181 void wxFileName::AssignTempFileName( const wxString
&prefix
)
185 bool wxFileName::Mkdir( int perm
)
187 return wxFileName::Mkdir( GetFullPath(), perm
);
190 bool wxFileName::Mkdir( const wxString
&dir
, int perm
)
192 return ::wxMkdir( dir
, perm
);
195 bool wxFileName::Rmdir()
197 return wxFileName::Rmdir( GetFullPath() );
200 bool wxFileName::Rmdir( const wxString
&dir
)
202 return ::wxRmdir( dir
);
205 bool wxFileName::Normalize( const wxString
&cwd
, const wxString
&home
)
207 wxFileName
tmp( *this );
209 const wxArrayString
&dirs
= tmp
.GetDirs();
211 if (dirs
.GetCount() == 0) return FALSE
;
215 if (dirs
[0] == wxT("."))
217 if (cwd
== wxEmptyString
)
218 Assign( wxFileName::GetCwd(), TRUE
);
224 if (dirs
[0] == wxT(".."))
226 if (cwd
== wxEmptyString
)
227 Assign( wxFileName::GetCwd(), TRUE
);
230 m_dirs
.Remove( m_dirs
.GetCount()-1 );
234 if (dirs
[0] == wxT("~"))
236 if (home
== wxEmptyString
)
237 Assign( wxFileName::GetHomeDir(), TRUE
);
243 for (size_t i
= start
; i
< dirs
.GetCount(); i
++)
245 if (dirs
[i
] == wxT(".")) continue;
247 if (dirs
[i
] == wxT(".."))
249 m_dirs
.Remove( m_dirs
.GetCount()-1 );
253 // expand env vars here ?
255 m_dirs
.Add( dirs
[i
] );
258 m_name
= tmp
.GetName();
259 m_ext
= tmp
.GetExt();
264 bool wxFileName::SameAs( const wxFileName
&filepath
, bool upper_case
)
266 wxString
file1( GetFullPath() );
267 wxString
file2( filepath
.GetFullPath() );
271 file1
.MakeUpper(); // what does MSW do to non-ascii chars etc? native funcs?
275 return (file1
== file2
);
278 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
280 format
= GetFormat( format
);
282 return (format
!= wxPATH_DOS
);
285 bool wxFileName::IsRelative( wxPathFormat format
)
287 format
= GetFormat( format
);
289 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
291 if ((format
== wxPATH_UNIX
) && (i
== 0) && (m_dirs
[0] == wxT("~"))) return TRUE
;
293 if (m_dirs
[i
] == wxT(".")) return TRUE
;
294 if (m_dirs
[i
] == wxT("..")) return TRUE
;
300 bool wxFileName::IsAbsolute( wxPathFormat format
)
302 return (!IsRelative(format
));
305 bool wxFileName::IsWild( wxPathFormat format
)
307 format
= GetFormat( format
);
309 if (format
== wxPATH_DOS
)
311 if (m_name
.Find( wxT('*') ) != -1) return TRUE
;
312 if (m_name
.Find( wxT('?') ) != -1) return TRUE
;
316 if (m_name
.Find( wxT('*') ) != -1) return TRUE
;
322 void wxFileName::AppendDir( const wxString
&dir
)
327 void wxFileName::PrependDir( const wxString
&dir
)
329 m_dirs
.Insert( dir
, 0 );
332 void wxFileName::InsertDir( int before
, const wxString
&dir
)
334 m_dirs
.Insert( dir
, before
);
337 void wxFileName::RemoveDir( int pos
)
339 m_dirs
.Remove( (size_t)pos
);
342 void wxFileName::SetFullName( const wxString name
, wxPathFormat format
)
344 format
= GetFormat( format
);
348 if (m_name
== wxT(".")) return;
349 if (m_name
== wxT("..")) return;
352 int pos
= m_name
.Find( wxT('.') );
353 if (pos
== -1) return;
355 bool has_starting_dot
= (pos
== 0);
356 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
362 pos
= m_name
.Find( wxT('.') );
366 m_name
.Prepend( "." );
372 m_ext
.Remove( 0, pos
+1 );
374 m_name
.Remove( pos
, m_name
.Len()-pos
);
376 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
379 m_name
.Prepend( "." );
384 wxString
wxFileName::GetFullName()
386 wxString
ret( m_name
);
387 if (!m_ext
.IsEmpty())
395 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
397 format
= GetFormat( format
);
400 if (format
== wxPATH_DOS
)
402 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
405 if (add_separator
|| (i
!= m_dirs
.GetCount()-1))
410 if (format
== wxPATH_UNIX
)
412 ret
= '/'; // FIXME: must be absolute
413 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
416 if (add_separator
|| (i
!= m_dirs
.GetCount()-1))
422 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
425 if (add_separator
|| (i
!= m_dirs
.GetCount()-1))
433 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
435 format
= GetFormat( format
);
438 if (format
== wxPATH_DOS
)
440 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
447 if (format
== wxPATH_UNIX
)
449 ret
= '/'; // FIXME: must be absolute
450 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
458 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
467 if (!m_ext
.IsEmpty())
476 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
478 if (format
== wxPATH_NATIVE
)
480 #if defined(__WXMSW__) || defined(__WXPM__)
483 #if defined(__WXMAC__)
486 #if !defined(__WXMSW__) && !defined(__WXPM__) && !defined(__WXMAC__)
487 format
= wxPATH_UNIX
;