]> git.saurik.com Git - wxWidgets.git/blob - src/common/filename.cpp
7fa61c0a556bcd949cd09acf2a920c78fe5d808f
[wxWidgets.git] / src / common / filename.cpp
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/utils.h"
31
32 //----------------------------------------------------------------------------
33 // wxFileName
34 //----------------------------------------------------------------------------
35
36 wxFileName::wxFileName( const wxFileName &filepath )
37 {
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] );
43 }
44
45 void wxFileName::Assign( const wxFileName &filepath )
46 {
47 m_dirs.Clear();
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] );
53 }
54
55 void wxFileName::Assign( const wxString &path, bool dir_only, wxPathFormat format )
56 {
57 m_ext = wxEmptyString;
58 m_name = wxEmptyString;
59 m_dirs.Clear();
60
61 format = GetFormat( format );
62
63 wxString seps;
64 if (format == wxPATH_DOS)
65 {
66 seps = "/\\";
67 }
68 else
69 if (format == wxPATH_UNIX)
70 {
71 seps = "/";
72 }
73 else
74 {
75 seps = ":";
76 }
77
78 wxStringTokenizer tn( path, seps );
79 while (tn.HasMoreTokens())
80 {
81 wxString token( tn.GetNextToken() );
82 if (!token.IsEmpty())
83 m_dirs.Add( token );
84 }
85
86 if (!dir_only)
87 {
88 // make last m_dir -> m_name
89 size_t last = m_dirs.GetCount();
90 if (last == 0) return;
91 last--;
92 m_name = m_dirs[last];
93 m_dirs.Remove( last );
94
95 if (m_name == wxT(".")) return;
96 if (m_name == wxT("..")) return;
97
98 // ext?
99 int pos = m_name.Find( wxT('.') );
100 if (pos == -1) return;
101
102 bool has_starting_dot = (pos == 0);
103 if (has_starting_dot && (format == wxPATH_UNIX))
104 {
105 // remove dot
106 m_name.Remove(0,1);
107
108 // search again
109 pos = m_name.Find( wxT('.') );
110 if (pos == -1)
111 {
112 // add dot back
113 m_name.Prepend( "." );
114 return;
115 }
116 }
117 m_ext = m_name;
118 m_ext.Remove( 0, pos+1 );
119
120 m_name.Remove( pos, m_name.Len()-pos );
121
122 if (has_starting_dot && (format == wxPATH_UNIX))
123 {
124 // add dot back
125 m_name.Prepend( "." );
126 return;
127 }
128 }
129 }
130
131 bool wxFileName::FileExists()
132 {
133 return wxFileName::FileExists( GetFullPath() );
134 }
135
136 bool wxFileName::FileExists( const wxString &file )
137 {
138 return ::wxFileExists( file );
139 }
140
141 bool wxFileName::DirExists()
142 {
143 return wxFileName::DirExists( GetFullPath() );
144 }
145
146 bool wxFileName::DirExists( const wxString &dir )
147 {
148 return ::wxDirExists( dir );
149 }
150
151 void wxFileName::AssignCwd()
152 {
153 Assign( wxFileName::GetCwd(), TRUE );
154 }
155
156 wxString wxFileName::GetCwd()
157 {
158 return ::wxGetCwd();
159 }
160
161 bool wxFileName::SetCwd()
162 {
163 return wxFileName::SetCwd( GetFullPath() );
164 }
165
166 bool wxFileName::SetCwd( const wxString &cwd )
167 {
168 return ::wxSetWorkingDirectory( cwd );
169 }
170
171 void wxFileName::AssignHomeDir()
172 {
173 Assign( wxFileName::GetHomeDir(), TRUE );
174 }
175
176 wxString wxFileName::GetHomeDir()
177 {
178 return ::wxGetHomeDir();
179 }
180
181 void wxFileName::AssignTempFileName( const wxString &prefix )
182 {
183 }
184
185 bool wxFileName::Mkdir( int perm )
186 {
187 return wxFileName::Mkdir( GetFullPath(), perm );
188 }
189
190 bool wxFileName::Mkdir( const wxString &dir, int perm )
191 {
192 return ::wxMkdir( dir, perm );
193 }
194
195 bool wxFileName::Rmdir()
196 {
197 return wxFileName::Rmdir( GetFullPath() );
198 }
199
200 bool wxFileName::Rmdir( const wxString &dir )
201 {
202 return ::wxRmdir( dir );
203 }
204
205 bool wxFileName::Normalize( const wxString &cwd, const wxString &home )
206 {
207 wxFileName tmp( *this );
208 m_dirs.Clear();
209 const wxArrayString &dirs = tmp.GetDirs();
210
211 if (dirs.GetCount() == 0) return FALSE;
212
213 size_t start = 0;
214
215 if (dirs[0] == wxT("."))
216 {
217 if (cwd == wxEmptyString)
218 Assign( wxFileName::GetCwd(), TRUE );
219 else
220 Assign( cwd );
221 start = 1;
222 }
223 else
224 if (dirs[0] == wxT(".."))
225 {
226 if (cwd == wxEmptyString)
227 Assign( wxFileName::GetCwd(), TRUE );
228 else
229 Assign( cwd );
230 m_dirs.Remove( m_dirs.GetCount()-1 );
231 start = 1;
232 }
233 else
234 if (dirs[0] == wxT("~"))
235 {
236 if (home == wxEmptyString)
237 Assign( wxFileName::GetHomeDir(), TRUE );
238 else
239 Assign( home );
240 start = 1;
241 }
242
243 for (size_t i = start; i < dirs.GetCount(); i++)
244 {
245 if (dirs[i] == wxT(".")) continue;
246
247 if (dirs[i] == wxT(".."))
248 {
249 m_dirs.Remove( m_dirs.GetCount()-1 );
250 continue;
251 }
252
253 // expand env vars here ?
254
255 m_dirs.Add( dirs[i] );
256 }
257
258 m_name = tmp.GetName();
259 m_ext = tmp.GetExt();
260
261 return TRUE;
262 }
263
264 bool wxFileName::SameAs( const wxFileName &filepath, bool upper_case )
265 {
266 wxString file1( GetFullPath() );
267 wxString file2( filepath.GetFullPath() );
268
269 if (upper_case)
270 {
271 file1.MakeUpper(); // what does MSW do to non-ascii chars etc? native funcs?
272 file2.MakeUpper();
273 }
274
275 return (file1 == file2);
276 }
277
278 bool wxFileName::IsCaseSensitive( wxPathFormat format )
279 {
280 format = GetFormat( format );
281
282 return (format != wxPATH_DOS);
283 }
284
285 bool wxFileName::IsRelative( wxPathFormat format )
286 {
287 format = GetFormat( format );
288
289 for (size_t i = 0; i < m_dirs.GetCount(); i++)
290 {
291 if ((format == wxPATH_UNIX) && (i == 0) && (m_dirs[0] == wxT("~"))) return TRUE;
292
293 if (m_dirs[i] == wxT(".")) return TRUE;
294 if (m_dirs[i] == wxT("..")) return TRUE;
295 }
296
297 return FALSE;
298 }
299
300 bool wxFileName::IsAbsolute( wxPathFormat format )
301 {
302 return (!IsRelative(format));
303 }
304
305 bool wxFileName::IsWild( wxPathFormat format )
306 {
307 format = GetFormat( format );
308
309 if (format == wxPATH_DOS)
310 {
311 if (m_name.Find( wxT('*') ) != -1) return TRUE;
312 if (m_name.Find( wxT('?') ) != -1) return TRUE;
313 }
314 else
315 {
316 if (m_name.Find( wxT('*') ) != -1) return TRUE;
317 }
318
319 return FALSE;
320 }
321
322 void wxFileName::AppendDir( const wxString &dir )
323 {
324 m_dirs.Add( dir );
325 }
326
327 void wxFileName::PrependDir( const wxString &dir )
328 {
329 m_dirs.Insert( dir, 0 );
330 }
331
332 void wxFileName::InsertDir( int before, const wxString &dir )
333 {
334 m_dirs.Insert( dir, before );
335 }
336
337 void wxFileName::RemoveDir( int pos )
338 {
339 m_dirs.Remove( (size_t)pos );
340 }
341
342 void wxFileName::SetFullName( const wxString name, wxPathFormat format )
343 {
344 format = GetFormat( format );
345
346 m_name = name;
347
348 if (m_name == wxT(".")) return;
349 if (m_name == wxT("..")) return;
350
351 // ext?
352 int pos = m_name.Find( wxT('.') );
353 if (pos == -1) return;
354
355 bool has_starting_dot = (pos == 0);
356 if (has_starting_dot && (format == wxPATH_UNIX))
357 {
358 // remove dot
359 m_name.Remove(0,1);
360
361 // search again
362 pos = m_name.Find( wxT('.') );
363 if (pos == -1)
364 {
365 // add dot back
366 m_name.Prepend( "." );
367 return;
368 }
369 }
370
371 m_ext = m_name;
372 m_ext.Remove( 0, pos+1 );
373
374 m_name.Remove( pos, m_name.Len()-pos );
375
376 if (has_starting_dot && (format == wxPATH_UNIX))
377 {
378 // add dot back
379 m_name.Prepend( "." );
380 return;
381 }
382 }
383
384 wxString wxFileName::GetFullName()
385 {
386 wxString ret( m_name );
387 if (!m_ext.IsEmpty())
388 {
389 ret += '.';
390 ret += m_ext;
391 }
392 return ret;
393 }
394
395 wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const
396 {
397 format = GetFormat( format );
398
399 wxString ret;
400 if (format == wxPATH_DOS)
401 {
402 for (size_t i = 0; i < m_dirs.GetCount(); i++)
403 {
404 ret += m_dirs[i];
405 if (add_separator || (i != m_dirs.GetCount()-1))
406 ret += '\\';
407 }
408 }
409 else
410 if (format == wxPATH_UNIX)
411 {
412 ret = '/'; // FIXME: must be absolute
413 for (size_t i = 0; i < m_dirs.GetCount(); i++)
414 {
415 ret += m_dirs[i];
416 if (add_separator || (i != m_dirs.GetCount()-1))
417 ret += '/';
418 }
419 }
420 else
421 {
422 for (size_t i = 0; i < m_dirs.GetCount(); i++)
423 {
424 ret += m_dirs[i];
425 if (add_separator || (i != m_dirs.GetCount()-1))
426 ret += ":";
427 }
428 }
429
430 return ret;
431 }
432
433 wxString wxFileName::GetFullPath( wxPathFormat format ) const
434 {
435 format = GetFormat( format );
436
437 wxString ret;
438 if (format == wxPATH_DOS)
439 {
440 for (size_t i = 0; i < m_dirs.GetCount(); i++)
441 {
442 ret += m_dirs[i];
443 ret += '\\';
444 }
445 }
446 else
447 if (format == wxPATH_UNIX)
448 {
449 ret = '/'; // FIXME: must be absolute
450 for (size_t i = 0; i < m_dirs.GetCount(); i++)
451 {
452 ret += m_dirs[i];
453 ret += '/';
454 }
455 }
456 else
457 {
458 for (size_t i = 0; i < m_dirs.GetCount(); i++)
459 {
460 ret += m_dirs[i];
461 ret += ':';
462 }
463 }
464
465 ret += m_name;
466
467 if (!m_ext.IsEmpty())
468 {
469 ret += '.';
470 ret += m_ext;
471 }
472
473 return ret;
474 }
475
476 wxPathFormat wxFileName::GetFormat( wxPathFormat format )
477 {
478 if (format == wxPATH_NATIVE)
479 {
480 #if defined(__WXMSW__) || defined(__WXPM__)
481 format = wxPATH_DOS;
482 #endif
483 #if defined(__WXMAC__)
484 format = wxPATH_MAC;
485 #endif
486 #if !defined(__WXMSW__) && !defined(__WXPM__) && !defined(__WXMAC__)
487 format = wxPATH_UNIX;
488 #endif
489 }
490 return format;
491 }
492