]>
git.saurik.com Git - wxWidgets.git/blob - src/common/filename.cpp
380ee05a6bd04bdb2a775701f996702dbdab6490
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/filename.cpp
3 // Purpose: wxFileName - encapsulates a file path
4 // Author: Robert Roebling
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"
40 // ============================================================================
42 // ============================================================================
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 wxFileName::wxFileName( const wxFileName
&filepath
)
50 m_ext
= filepath
.GetExt();
51 m_name
= filepath
.GetName();
52 const wxArrayString
&dirs
= filepath
.GetDirs();
53 for (size_t i
= 0; i
< dirs
.GetCount(); i
++)
54 m_dirs
.Add( dirs
[i
] );
57 void wxFileName::Assign( const wxFileName
&filepath
)
60 m_ext
= filepath
.GetExt();
61 m_name
= filepath
.GetName();
62 const wxArrayString
&dirs
= filepath
.GetDirs();
63 for (size_t i
= 0; i
< dirs
.GetCount(); i
++)
64 m_dirs
.Add( dirs
[i
] );
67 void wxFileName::Assign( const wxString
&path
, bool dir_only
, wxPathFormat format
)
69 m_ext
= wxEmptyString
;
70 m_name
= wxEmptyString
;
73 format
= GetFormat( format
);
76 if (format
== wxPATH_DOS
)
81 if (format
== wxPATH_UNIX
)
90 wxStringTokenizer
tn( path
, seps
, wxTOKEN_RET_EMPTY_ALL
);
92 while (tn
.HasMoreTokens())
94 wxString
token( tn
.GetNextToken() );
96 // If the path starts with a slash, we need the first
97 // dir entry to be an empty for later reassembly.
99 if (first
|| !token
.IsEmpty())
106 // make last m_dir -> m_name
107 size_t last
= m_dirs
.GetCount();
108 if (last
== 0) return;
110 m_name
= m_dirs
[last
];
111 m_dirs
.Remove( last
);
113 if (m_name
== wxT(".")) return;
114 if (m_name
== wxT("..")) return;
117 int pos
= m_name
.Find( wxT('.') );
118 if (pos
== -1) return;
120 bool has_starting_dot
= (pos
== 0);
121 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
127 pos
= m_name
.Find( wxT('.') );
131 m_name
.Prepend( _T(".") );
136 m_ext
.Remove( 0, pos
+1 );
138 m_name
.Remove( pos
, m_name
.Len()-pos
);
140 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
143 m_name
.Prepend( _T(".") );
149 bool wxFileName::FileExists()
151 return wxFileName::FileExists( GetFullPath() );
154 bool wxFileName::FileExists( const wxString
&file
)
156 return ::wxFileExists( file
);
159 bool wxFileName::DirExists()
161 return wxFileName::DirExists( GetFullPath() );
164 bool wxFileName::DirExists( const wxString
&dir
)
166 return ::wxDirExists( dir
);
169 void wxFileName::AssignCwd()
171 Assign( wxFileName::GetCwd(), TRUE
);
174 wxString
wxFileName::GetCwd()
179 bool wxFileName::SetCwd()
181 return wxFileName::SetCwd( GetFullPath() );
184 bool wxFileName::SetCwd( const wxString
&cwd
)
186 return ::wxSetWorkingDirectory( cwd
);
189 void wxFileName::AssignHomeDir()
191 Assign( wxFileName::GetHomeDir(), TRUE
);
194 wxString
wxFileName::GetHomeDir()
196 return ::wxGetHomeDir();
199 void wxFileName::AssignTempFileName( const wxString
&prefix
)
203 bool wxFileName::Mkdir( int perm
)
205 return wxFileName::Mkdir( GetFullPath(), perm
);
208 bool wxFileName::Mkdir( const wxString
&dir
, int perm
)
210 return ::wxMkdir( dir
, perm
);
213 bool wxFileName::Rmdir()
215 return wxFileName::Rmdir( GetFullPath() );
218 bool wxFileName::Rmdir( const wxString
&dir
)
220 return ::wxRmdir( dir
);
223 bool wxFileName::Normalize( const wxString
&cwd
, const wxString
&home
)
225 wxFileName
tmp( *this );
227 const wxArrayString
&dirs
= tmp
.GetDirs();
229 if (dirs
.GetCount() == 0) return FALSE
;
233 if (dirs
[0] == wxT("."))
235 if (cwd
== wxEmptyString
)
236 Assign( wxFileName::GetCwd(), TRUE
);
242 if (dirs
[0] == wxT(".."))
244 if (cwd
== wxEmptyString
)
245 Assign( wxFileName::GetCwd(), TRUE
);
248 m_dirs
.Remove( m_dirs
.GetCount()-1 );
252 if (dirs
[0] == wxT("~"))
254 if (home
== wxEmptyString
)
255 Assign( wxFileName::GetHomeDir(), TRUE
);
261 for (size_t i
= start
; i
< dirs
.GetCount(); i
++)
263 if (dirs
[i
] == wxT(".")) continue;
265 if (dirs
[i
] == wxT(".."))
267 m_dirs
.Remove( m_dirs
.GetCount()-1 );
271 // expand env vars here ?
273 m_dirs
.Add( dirs
[i
] );
276 m_name
= tmp
.GetName();
277 m_ext
= tmp
.GetExt();
282 bool wxFileName::SameAs( const wxFileName
&filepath
, bool upper_case
)
284 wxString
file1( GetFullPath() );
285 wxString
file2( filepath
.GetFullPath() );
289 file1
.MakeUpper(); // what does MSW do to non-ascii chars etc? native funcs?
293 return (file1
== file2
);
296 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
298 format
= GetFormat( format
);
300 return (format
!= wxPATH_DOS
);
303 bool wxFileName::IsRelative( wxPathFormat format
)
305 format
= GetFormat( format
);
307 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
309 if ((format
== wxPATH_UNIX
) && (i
== 0) && (m_dirs
[0] == wxT("~"))) return TRUE
;
311 if (m_dirs
[i
] == wxT(".")) return TRUE
;
312 if (m_dirs
[i
] == wxT("..")) return TRUE
;
318 bool wxFileName::IsAbsolute( wxPathFormat format
)
320 return (!IsRelative(format
));
323 bool wxFileName::IsWild( wxPathFormat format
)
325 format
= GetFormat( format
);
327 if (format
== wxPATH_DOS
)
329 if (m_name
.Find( wxT('*') ) != -1) return TRUE
;
330 if (m_name
.Find( wxT('?') ) != -1) return TRUE
;
334 if (m_name
.Find( wxT('*') ) != -1) return TRUE
;
340 void wxFileName::AppendDir( const wxString
&dir
)
345 void wxFileName::PrependDir( const wxString
&dir
)
347 m_dirs
.Insert( dir
, 0 );
350 void wxFileName::InsertDir( int before
, const wxString
&dir
)
352 m_dirs
.Insert( dir
, before
);
355 void wxFileName::RemoveDir( int pos
)
357 m_dirs
.Remove( (size_t)pos
);
360 void wxFileName::SetFullName( const wxString name
, wxPathFormat format
)
362 format
= GetFormat( format
);
365 m_ext
= wxEmptyString
;
367 if (m_name
== wxT(".")) return;
368 if (m_name
== wxT("..")) return;
371 int pos
= m_name
.Find( wxT('.') );
372 if (pos
== -1) return;
374 bool has_starting_dot
= (pos
== 0);
375 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
381 pos
= m_name
.Find( wxT('.') );
385 m_name
.Prepend( _T(".") );
391 m_ext
.Remove( 0, pos
+1 );
393 m_name
.Remove( pos
, m_name
.Len()-pos
);
395 if (has_starting_dot
&& (format
== wxPATH_UNIX
))
398 m_name
.Prepend( _T(".") );
403 wxString
wxFileName::GetFullName()
405 wxString
ret( m_name
);
406 if (!m_ext
.IsEmpty())
414 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
416 format
= GetFormat( format
);
419 if (format
== wxPATH_DOS
)
421 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
424 if (add_separator
|| (i
!= m_dirs
.GetCount()-1))
429 if (format
== wxPATH_UNIX
)
431 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
434 if (add_separator
|| (i
!= m_dirs
.GetCount()-1))
440 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
443 if (add_separator
|| (i
!= m_dirs
.GetCount()-1))
451 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
453 format
= GetFormat( format
);
456 if (format
== wxPATH_DOS
)
458 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
465 if (format
== wxPATH_UNIX
)
467 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
475 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
484 if (!m_ext
.IsEmpty())
493 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
495 if (format
== wxPATH_NATIVE
)
497 #if defined(__WXMSW__) || defined(__WXPM__)
500 #if defined(__WXMAC__)
503 #if !defined(__WXMSW__) && !defined(__WXPM__) && !defined(__WXMAC__)
504 format
= wxPATH_UNIX
;