]> git.saurik.com Git - wxWidgets.git/blame - src/common/filename.cpp
merged wxStaticLine ctor fix
[wxWidgets.git] / src / common / filename.cpp
CommitLineData
df5ddbca
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: filename.cpp
3// Purpose: wxFileName - encapsulates candy
4// Author: Robert Roebling
5// Modified by:
6// Created: 28.12.2000
7// RCS-ID: $Id$
8// Copyright: (c) 2000 Robert Roebling
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13 #pragma implementation "filename.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24 #include "wx/intl.h"
25 #include "wx/log.h"
26#endif
27
28#include "wx/filename.h"
29#include "wx/tokenzr.h"
30#include "wx/filefn.h"
31
32//----------------------------------------------------------------------------
33// wxFileName
34//----------------------------------------------------------------------------
35
36wxFileName::wxFileName( const wxFileName &filename )
37{
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++)
42 {
43 m_dirs.Add( dirs[i] );
44 }
45}
46
47void wxFileName::Assign( const wxString &path, bool dir_only, wxPathFormat format )
48{
49 m_ext = wxEmptyString;
50 m_name = wxEmptyString;
51 m_dirs.Clear();
52
53 format = GetFormat( format );
54
55 wxString seps;
56 if (format == wxPATH_DOS)
57 {
58 seps = "/\\";
59 }
60 else
61 if (format == wxPATH_UNIX)
62 {
63 seps = "/";
64 }
65 else
66 {
67 seps = "/"; // or maybe ":" or both ?
68 }
69
70 wxStringTokenizer tn( path, seps );
71 while (tn.HasMoreTokens())
72 {
73 wxString token( tn.GetNextToken() );
74 if (!token.IsEmpty())
75 m_dirs.Add( token );
76 }
77
78 if (!dir_only)
79 {
80 // make last m_dir -> m_name
81 size_t last = m_dirs.GetCount();
82 if (last == 0) return;
83 last--;
84 m_name = m_dirs[last];
85 m_dirs.Remove( last );
86
87 if (m_name == wxT(".")) return;
88 if (m_name == wxT("..")) return;
89
90 // ext?
91 int pos = m_name.Find( wxT('.') );
92 if (pos == -1) return;
93
94 bool has_starting_dot = (pos == 0);
95 if (has_starting_dot)
96 {
97 // remove dot
98 m_name.Remove(0,1);
99
100 // search again
101 pos = m_name.Find( wxT('.') );
102 if (pos == -1)
103 {
104 // add dot back
105 m_name.Prepend( "." );
106 return;
107 }
108 }
109 m_ext = m_name;
110 m_ext.Remove( 0, pos+1 );
111
112 m_name.Remove( pos, m_name.Len()-pos );
113
114 if (has_starting_dot)
115 {
116 // add dot back
117 m_name.Prepend( "." );
118 return;
119 }
120 }
121}
122
123bool wxFileName::FileExists()
124{
125 return ::wxFileExists( GetFullPath() );
126}
127
128bool wxFileName::DirExists()
129{
130 return ::wxDirExists( GetFullPath() );
131}
132
133void wxFileName::AssignCwd()
134{
135 Assign( wxGetCwd(), TRUE );
136}
137
138void wxFileName::SetCwd()
139{
140 wxSetWorkingDirectory( GetFullPath() );
141}
142
143void wxFileName::AssignTempFileName( const wxString &prefix )
144{
145}
146
147void wxFileName::Mkdir( int perm )
148{
149 wxMkdir( GetFullPath(), perm );
150}
151
152void wxFileName::Rmdir()
153{
154 wxRmdir( GetFullPath() );
155}
156
157void wxFileName::MakeAbsolute()
158{
159}
160
161bool wxFileName::SameAs( const wxFileName &filename, bool upper_on_dos )
162{
163 wxString file1( GetFullPath() );
164 wxString file2( filename.GetFullPath() );
165
166#ifdef __WXMSW__
167 if (upper_on_dos)
168 {
169 file1.MakeUpper();
170 file2.MakeUpper();
171 }
172#endif
173
174 return (file1 == file2);
175}
176
177bool wxFileName::IsCaseSensitive( wxPathFormat format )
178{
179 format = GetFormat( format );
180
181 return (format != wxPATH_DOS);
182}
183
184bool wxFileName::IsRelative( wxPathFormat format )
185{
186 format = GetFormat( format );
187
188 for (size_t i = 0; i < m_dirs.GetCount(); i++)
189 {
190 if ((format == wxPATH_UNIX) && (i == 0) && (m_dirs[0] == wxT("~"))) return TRUE;
191
192 if (m_dirs[i] == wxT(".")) return TRUE;
193 if (m_dirs[i] == wxT("..")) return TRUE;
194 }
195
196 return FALSE;
197}
198
199bool wxFileName::IsAbsolute( wxPathFormat format )
200{
201 return (!IsRelative(format));
202}
203
204bool wxFileName::IsWild( wxPathFormat format )
205{
206 format = GetFormat( format );
207
208 if (format == wxPATH_DOS)
209 {
210 if (m_name.Find( wxT('*') ) != -1) return TRUE;
211 if (m_name.Find( wxT('?') ) != -1) return TRUE;
212 }
213 else
214 {
215 if (m_name.Find( wxT('*') ) != -1) return TRUE;
216 }
217
218 return FALSE;
219}
220
221void wxFileName::AppendDir( const wxString &dir )
222{
223 m_dirs.Add( dir );
224}
225
226void wxFileName::PrependDir( const wxString &dir )
227{
228 m_dirs.Insert( dir, 0 );
229}
230
231void wxFileName::InsertDir( int before, const wxString &dir )
232{
233 m_dirs.Insert( dir, before );
234}
235
236void wxFileName::RemoveDir( int pos )
237{
238 m_dirs.Remove( (size_t)pos );
239}
240
241wxString wxFileName::GetPath( wxPathFormat format ) const
242{
243 format = GetFormat( format );
244
245 wxString ret;
246 if (format == wxPATH_DOS)
247 {
248 for (size_t i = 0; i < m_dirs.GetCount(); i++)
249 {
250 ret += m_dirs[i];
251 if (i != m_dirs.GetCount()-1) ret += '\\';
252 }
253 }
254 else
255 if (format == wxPATH_DOS)
256 {
257 for (size_t i = 0; i < m_dirs.GetCount(); i++)
258 {
259 ret += m_dirs[i];
260 if (i != m_dirs.GetCount()-1) ret += '/';
261 }
262 }
263 else
264 {
265 for (size_t i = 0; i < m_dirs.GetCount(); i++)
266 {
267 ret += m_dirs[i];
268 if (i != m_dirs.GetCount()-1) ret += "//"; // or maybe ":" ?
269 }
270 }
271
272 return ret;
273}
274
275wxString wxFileName::GetFullPath( wxPathFormat format ) const
276{
277 format = GetFormat( format );
278
279 wxString ret;
280 if (format == wxPATH_DOS)
281 {
282 for (size_t i = 0; i < m_dirs.GetCount(); i++)
283 {
284 ret += m_dirs[i];
285 ret += '\\';
286 }
287 }
288 else
289 if (format == wxPATH_DOS)
290 {
291 for (size_t i = 0; i < m_dirs.GetCount(); i++)
292 {
293 ret += m_dirs[i];
294 ret += '/';
295 }
296 }
297 else
298 {
299 for (size_t i = 0; i < m_dirs.GetCount(); i++)
300 {
301 ret += m_dirs[i];
302 ret += '/'; // or maybe ":" ?
303 }
304 }
305
306 ret += m_name;
307
308 if (!m_ext.IsEmpty())
309 {
310 ret += '.';
311 ret += m_ext;
312 }
313
314 return ret;
315}
316
317wxPathFormat wxFileName::GetFormat( wxPathFormat format )
318{
319 if (format == wxPATH_NATIVE)
320 {
321#if defined(__WXMSW__) || defined(__WXPM__)
322 format = wxPATH_DOS;
323#endif
324#if defined(__WXMAC__)
325 format = wxPATH_MAC;
326#endif
327#if !defined(__WXMSW__) && !defined(__WXPM__) && !defined(__WXMAC__)
328 format = wxPATH_UNIX;
329#endif
330 }
331 return format;
332}