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