]>
git.saurik.com Git - wxWidgets.git/blob - src/common/filename.cpp
99dce6eb04fd57548ac7a96675f0ccc091fbadad
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"
30 #include "wx/filefn.h"
32 //----------------------------------------------------------------------------
34 //----------------------------------------------------------------------------
36 wxFileName::wxFileName( const wxFileName
&filename
)
38 m_ext
= filename
.GetExt();
39 m_name
= filename
.GetName();
40 const wxArrayString
&dirs
= filename
.GetDirs();
41 for (size_t i
= 0; i
< dirs
.GetCount(); i
++)
43 m_dirs
.Add( dirs
[i
] );
47 void wxFileName::Assign( const wxString
&path
, bool dir_only
, wxPathFormat format
)
49 m_ext
= wxEmptyString
;
50 m_name
= wxEmptyString
;
53 format
= GetFormat( format
);
56 if (format
== wxPATH_DOS
)
61 if (format
== wxPATH_UNIX
)
67 seps
= "/"; // or maybe ":" or both ?
70 wxStringTokenizer
tn( path
, seps
);
71 while (tn
.HasMoreTokens())
73 wxString
token( tn
.GetNextToken() );
80 // make last m_dir -> m_name
81 size_t last
= m_dirs
.GetCount();
82 if (last
== 0) return;
84 m_name
= m_dirs
[last
];
85 m_dirs
.Remove( last
);
87 if (m_name
== wxT(".")) return;
88 if (m_name
== wxT("..")) return;
91 int pos
= m_name
.Find( wxT('.') );
92 if (pos
== -1) return;
94 bool has_starting_dot
= (pos
== 0);
101 pos
= m_name
.Find( wxT('.') );
105 m_name
.Prepend( "." );
110 m_ext
.Remove( 0, pos
+1 );
112 m_name
.Remove( pos
, m_name
.Len()-pos
);
114 if (has_starting_dot
)
117 m_name
.Prepend( "." );
123 bool wxFileName::FileExists()
125 return ::wxFileExists( GetFullPath() );
128 bool wxFileName::DirExists()
130 return ::wxDirExists( GetFullPath() );
133 void wxFileName::AssignCwd()
135 Assign( wxGetCwd(), TRUE
);
138 void wxFileName::SetCwd()
140 wxSetWorkingDirectory( GetFullPath() );
143 void wxFileName::AssignTempFileName( const wxString
&prefix
)
147 void wxFileName::Mkdir( int perm
)
149 wxMkdir( GetFullPath(), perm
);
152 void wxFileName::Rmdir()
154 wxRmdir( GetFullPath() );
157 void wxFileName::MakeAbsolute()
161 bool wxFileName::SameAs( const wxFileName
&filename
, bool upper_on_dos
)
163 wxString
file1( GetFullPath() );
164 wxString
file2( filename
.GetFullPath() );
174 return (file1
== file2
);
177 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
179 format
= GetFormat( format
);
181 return (format
!= wxPATH_DOS
);
184 bool wxFileName::IsRelative( wxPathFormat format
)
186 format
= GetFormat( format
);
188 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
190 if ((format
== wxPATH_UNIX
) && (i
== 0) && (m_dirs
[0] == wxT("~"))) return TRUE
;
192 if (m_dirs
[i
] == wxT(".")) return TRUE
;
193 if (m_dirs
[i
] == wxT("..")) return TRUE
;
199 bool wxFileName::IsAbsolute( wxPathFormat format
)
201 return (!IsRelative(format
));
204 bool wxFileName::IsWild( wxPathFormat format
)
206 format
= GetFormat( format
);
208 if (format
== wxPATH_DOS
)
210 if (m_name
.Find( wxT('*') ) != -1) return TRUE
;
211 if (m_name
.Find( wxT('?') ) != -1) return TRUE
;
215 if (m_name
.Find( wxT('*') ) != -1) return TRUE
;
221 void wxFileName::AppendDir( const wxString
&dir
)
226 void wxFileName::PrependDir( const wxString
&dir
)
228 m_dirs
.Insert( dir
, 0 );
231 void wxFileName::InsertDir( int before
, const wxString
&dir
)
233 m_dirs
.Insert( dir
, before
);
236 void wxFileName::RemoveDir( int pos
)
238 m_dirs
.Remove( (size_t)pos
);
241 wxString
wxFileName::GetPath( wxPathFormat format
) const
243 format
= GetFormat( format
);
246 if (format
== wxPATH_DOS
)
248 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
251 if (i
!= m_dirs
.GetCount()-1) ret
+= '\\';
255 if (format
== wxPATH_DOS
)
257 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
260 if (i
!= m_dirs
.GetCount()-1) ret
+= '/';
265 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
268 if (i
!= m_dirs
.GetCount()-1) ret
+= "//"; // or maybe ":" ?
275 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
277 format
= GetFormat( format
);
280 if (format
== wxPATH_DOS
)
282 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
289 if (format
== wxPATH_DOS
)
291 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
299 for (size_t i
= 0; i
< m_dirs
.GetCount(); i
++)
302 ret
+= '/'; // or maybe ":" ?
308 if (!m_ext
.IsEmpty())
317 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
319 if (format
== wxPATH_NATIVE
)
321 #if defined(__WXMSW__) || defined(__WXPM__)
324 #if defined(__WXMAC__)
327 #if !defined(__WXMSW__) && !defined(__WXPM__) && !defined(__WXMAC__)
328 format
= wxPATH_UNIX
;