]>
Commit | Line | Data |
---|---|---|
df5ddbca | 1 | ///////////////////////////////////////////////////////////////////////////// |
097ead30 VZ |
2 | // Name: src/common/filename.cpp |
3 | // Purpose: wxFileName - encapsulates a file path | |
844f90fb | 4 | // Author: Robert Roebling, Vadim Zeitlin |
df5ddbca RR |
5 | // Modified by: |
6 | // Created: 28.12.2000 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2000 Robert Roebling | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
097ead30 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
df5ddbca RR |
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" | |
844f90fb | 38 | #include "wx/config.h" // for wxExpandEnvVars |
a35b27b1 | 39 | #include "wx/utils.h" |
5d978d07 JS |
40 | |
41 | #if wxUSE_DYNLIB_CLASS | |
951cd180 | 42 | #include "wx/dynlib.h" |
5d978d07 | 43 | #endif |
df5ddbca | 44 | |
9e9b65c1 JS |
45 | // For GetShort/LongPathName |
46 | #ifdef __WIN32__ | |
951cd180 VZ |
47 | #include <windows.h> |
48 | ||
49 | #include "wx/msw/winundef.h" | |
50 | #endif | |
51 | ||
52 | // utime() is POSIX so should normally be available on all Unices | |
53 | #ifdef __UNIX_LIKE__ | |
54 | #include <sys/types.h> | |
55 | #include <utime.h> | |
4f2570aa VZ |
56 | #include <sys/stat.h> |
57 | #include <unistd.h> | |
9e9b65c1 JS |
58 | #endif |
59 | ||
01d981ec RR |
60 | #ifdef __MWERKS__ |
61 | #include <stat.h> | |
62 | #include <unistd.h> | |
63 | #include <unix.h> | |
64 | #endif | |
65 | ||
951cd180 VZ |
66 | // ---------------------------------------------------------------------------- |
67 | // private classes | |
68 | // ---------------------------------------------------------------------------- | |
69 | ||
70 | // small helper class which opens and closes the file - we use it just to get | |
71 | // a file handle for the given file name to pass it to some Win32 API function | |
72 | #ifdef __WIN32__ | |
73 | ||
74 | class wxFileHandle | |
75 | { | |
76 | public: | |
77 | wxFileHandle(const wxString& filename) | |
78 | { | |
79 | m_hFile = ::CreateFile | |
80 | ( | |
81 | filename, // name | |
82 | GENERIC_READ, // access mask | |
83 | 0, // no sharing | |
84 | NULL, // no secutity attr | |
85 | OPEN_EXISTING, // creation disposition | |
86 | 0, // no flags | |
87 | NULL // no template file | |
88 | ); | |
89 | ||
90 | if ( m_hFile == INVALID_HANDLE_VALUE ) | |
91 | { | |
92 | wxLogSysError(_("Failed to open '%s' for reading"), | |
93 | filename.c_str()); | |
94 | } | |
95 | } | |
96 | ||
97 | ~wxFileHandle() | |
98 | { | |
99 | if ( m_hFile != INVALID_HANDLE_VALUE ) | |
100 | { | |
101 | if ( !::CloseHandle(m_hFile) ) | |
102 | { | |
103 | wxLogSysError(_("Failed to close file handle")); | |
104 | } | |
105 | } | |
106 | } | |
107 | ||
108 | // return TRUE only if the file could be opened successfully | |
109 | bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; } | |
110 | ||
111 | // get the handle | |
112 | operator HANDLE() const { return m_hFile; } | |
113 | ||
114 | private: | |
115 | HANDLE m_hFile; | |
116 | }; | |
117 | ||
118 | #endif // __WIN32__ | |
119 | ||
120 | // ---------------------------------------------------------------------------- | |
121 | // private functions | |
122 | // ---------------------------------------------------------------------------- | |
123 | ||
124 | #ifdef __WIN32__ | |
125 | ||
126 | // convert between wxDateTime and FILETIME which is a 64-bit value representing | |
127 | // the number of 100-nanosecond intervals since January 1, 1601. | |
128 | ||
d56e2b97 VZ |
129 | // the number of milliseconds between the Unix Epoch (January 1, 1970) and the |
130 | // FILETIME reference point (January 1, 1601) | |
131 | static const wxLongLong FILETIME_EPOCH_OFFSET = wxLongLong(0xa97, 0x30b66800); | |
132 | ||
133 | static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft) | |
951cd180 | 134 | { |
d56e2b97 VZ |
135 | wxLongLong ll(ft.dwHighDateTime, ft.dwLowDateTime); |
136 | ||
137 | // convert 100ns to ms | |
138 | ll /= 10000; | |
139 | ||
140 | // move it to our Epoch | |
141 | ll -= FILETIME_EPOCH_OFFSET; | |
142 | ||
143 | *dt = wxDateTime(ll); | |
951cd180 VZ |
144 | } |
145 | ||
d56e2b97 | 146 | static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt) |
951cd180 | 147 | { |
d56e2b97 VZ |
148 | // do the reverse of ConvertFileTimeToWx() |
149 | wxLongLong ll = dt.GetValue(); | |
150 | ll *= 10000; | |
151 | ll += FILETIME_EPOCH_OFFSET; | |
152 | ||
153 | ft->dwHighDateTime = ll.GetHi(); | |
154 | ft->dwLowDateTime = ll.GetLo(); | |
951cd180 VZ |
155 | } |
156 | ||
157 | #endif // __WIN32__ | |
158 | ||
097ead30 VZ |
159 | // ============================================================================ |
160 | // implementation | |
161 | // ============================================================================ | |
162 | ||
163 | // ---------------------------------------------------------------------------- | |
844f90fb | 164 | // wxFileName construction |
097ead30 | 165 | // ---------------------------------------------------------------------------- |
df5ddbca | 166 | |
a35b27b1 RR |
167 | void wxFileName::Assign( const wxFileName &filepath ) |
168 | { | |
a35b27b1 RR |
169 | m_ext = filepath.GetExt(); |
170 | m_name = filepath.GetName(); | |
844f90fb | 171 | m_dirs = filepath.GetDirs(); |
df5ddbca RR |
172 | } |
173 | ||
844f90fb VZ |
174 | void wxFileName::Assign( const wxString& path, |
175 | const wxString& name, | |
176 | const wxString& ext, | |
177 | wxPathFormat format ) | |
df5ddbca | 178 | { |
844f90fb VZ |
179 | wxStringTokenizer tn(path, GetPathSeparators(format), |
180 | wxTOKEN_RET_EMPTY_ALL); | |
25db1b74 | 181 | int i = 0; |
844f90fb VZ |
182 | m_dirs.Clear(); |
183 | while ( tn.HasMoreTokens() ) | |
df5ddbca | 184 | { |
844f90fb VZ |
185 | wxString token = tn.GetNextToken(); |
186 | ||
25db1b74 JS |
187 | // If the path starts with a slash (or two for a network path), |
188 | // we need the first dir entry to be an empty for later reassembly. | |
189 | if ((i < 2) || !token.IsEmpty()) | |
df5ddbca | 190 | m_dirs.Add( token ); |
844f90fb | 191 | |
25db1b74 | 192 | i ++; |
df5ddbca | 193 | } |
844f90fb VZ |
194 | |
195 | m_ext = ext; | |
196 | m_name = name; | |
197 | } | |
198 | ||
199 | void wxFileName::Assign(const wxString& fullpath, | |
200 | wxPathFormat format) | |
201 | { | |
202 | wxString path, name, ext; | |
9e8d8607 | 203 | SplitPath(fullpath, &path, &name, &ext, format); |
844f90fb VZ |
204 | |
205 | Assign(path, name, ext, format); | |
206 | } | |
207 | ||
208 | void wxFileName::Assign(const wxString& path, | |
209 | const wxString& fullname, | |
210 | wxPathFormat format) | |
211 | { | |
212 | wxString name, ext; | |
9e8d8607 | 213 | SplitPath(fullname, NULL /* no path */, &name, &ext, format); |
844f90fb VZ |
214 | |
215 | Assign(path, name, ext, format); | |
216 | } | |
217 | ||
218 | void wxFileName::Clear() | |
219 | { | |
220 | m_dirs.Clear(); | |
221 | m_name = | |
222 | m_ext = wxEmptyString; | |
223 | } | |
224 | ||
225 | /* static */ | |
226 | wxFileName wxFileName::FileName(const wxString& file) | |
227 | { | |
228 | return wxFileName(file); | |
229 | } | |
230 | ||
231 | /* static */ | |
232 | wxFileName wxFileName::DirName(const wxString& dir) | |
233 | { | |
234 | wxFileName fn; | |
235 | fn.AssignDir(dir); | |
236 | return fn; | |
df5ddbca RR |
237 | } |
238 | ||
844f90fb VZ |
239 | // ---------------------------------------------------------------------------- |
240 | // existence tests | |
241 | // ---------------------------------------------------------------------------- | |
242 | ||
df5ddbca RR |
243 | bool wxFileName::FileExists() |
244 | { | |
a35b27b1 RR |
245 | return wxFileName::FileExists( GetFullPath() ); |
246 | } | |
247 | ||
248 | bool wxFileName::FileExists( const wxString &file ) | |
249 | { | |
250 | return ::wxFileExists( file ); | |
df5ddbca RR |
251 | } |
252 | ||
253 | bool wxFileName::DirExists() | |
254 | { | |
a35b27b1 RR |
255 | return wxFileName::DirExists( GetFullPath() ); |
256 | } | |
257 | ||
258 | bool wxFileName::DirExists( const wxString &dir ) | |
259 | { | |
260 | return ::wxDirExists( dir ); | |
df5ddbca RR |
261 | } |
262 | ||
844f90fb VZ |
263 | // ---------------------------------------------------------------------------- |
264 | // CWD and HOME stuff | |
265 | // ---------------------------------------------------------------------------- | |
266 | ||
df5ddbca RR |
267 | void wxFileName::AssignCwd() |
268 | { | |
844f90fb | 269 | AssignDir(wxFileName::GetCwd()); |
a35b27b1 RR |
270 | } |
271 | ||
844f90fb | 272 | /* static */ |
a35b27b1 RR |
273 | wxString wxFileName::GetCwd() |
274 | { | |
275 | return ::wxGetCwd(); | |
276 | } | |
277 | ||
278 | bool wxFileName::SetCwd() | |
279 | { | |
280 | return wxFileName::SetCwd( GetFullPath() ); | |
df5ddbca RR |
281 | } |
282 | ||
a35b27b1 | 283 | bool wxFileName::SetCwd( const wxString &cwd ) |
df5ddbca | 284 | { |
a35b27b1 | 285 | return ::wxSetWorkingDirectory( cwd ); |
df5ddbca RR |
286 | } |
287 | ||
a35b27b1 RR |
288 | void wxFileName::AssignHomeDir() |
289 | { | |
844f90fb | 290 | AssignDir(wxFileName::GetHomeDir()); |
a35b27b1 | 291 | } |
844f90fb | 292 | |
a35b27b1 RR |
293 | wxString wxFileName::GetHomeDir() |
294 | { | |
295 | return ::wxGetHomeDir(); | |
296 | } | |
844f90fb | 297 | |
df5ddbca RR |
298 | void wxFileName::AssignTempFileName( const wxString &prefix ) |
299 | { | |
844f90fb VZ |
300 | wxString fullname; |
301 | if ( wxGetTempFileName(prefix, fullname) ) | |
302 | { | |
303 | Assign(fullname); | |
304 | } | |
305 | else // error | |
306 | { | |
307 | Clear(); | |
308 | } | |
df5ddbca RR |
309 | } |
310 | ||
844f90fb VZ |
311 | // ---------------------------------------------------------------------------- |
312 | // directory operations | |
313 | // ---------------------------------------------------------------------------- | |
314 | ||
f0ce3409 | 315 | bool wxFileName::Mkdir( int perm, bool full ) |
a35b27b1 | 316 | { |
f0ce3409 | 317 | return wxFileName::Mkdir( GetFullPath(), perm, full ); |
a35b27b1 RR |
318 | } |
319 | ||
f0ce3409 | 320 | bool wxFileName::Mkdir( const wxString &dir, int perm, bool full ) |
df5ddbca | 321 | { |
f0ce3409 JS |
322 | if (full) |
323 | { | |
324 | wxFileName filename(dir); | |
325 | wxArrayString dirs = filename.GetDirs(); | |
77fe02a8 | 326 | dirs.Add(filename.GetName()); |
f0ce3409 JS |
327 | |
328 | size_t count = dirs.GetCount(); | |
329 | size_t i; | |
330 | wxString currPath; | |
331 | int noErrors = 0; | |
332 | for ( i = 0; i < count; i++ ) | |
333 | { | |
334 | currPath += dirs[i]; | |
335 | ||
336 | if (currPath.Last() == wxT(':')) | |
337 | { | |
338 | // Can't create a root directory so continue to next dir | |
339 | currPath += wxFILE_SEP_PATH; | |
340 | continue; | |
341 | } | |
342 | ||
343 | if (!DirExists(currPath)) | |
344 | if (!wxMkdir(currPath, perm)) | |
345 | noErrors ++; | |
346 | ||
347 | if ( (i < (count-1)) ) | |
348 | currPath += wxFILE_SEP_PATH; | |
349 | } | |
350 | ||
351 | return (noErrors == 0); | |
352 | ||
353 | } | |
354 | else | |
355 | return ::wxMkdir( dir, perm ); | |
df5ddbca RR |
356 | } |
357 | ||
a35b27b1 | 358 | bool wxFileName::Rmdir() |
df5ddbca | 359 | { |
a35b27b1 | 360 | return wxFileName::Rmdir( GetFullPath() ); |
df5ddbca RR |
361 | } |
362 | ||
a35b27b1 | 363 | bool wxFileName::Rmdir( const wxString &dir ) |
df5ddbca | 364 | { |
a35b27b1 | 365 | return ::wxRmdir( dir ); |
df5ddbca RR |
366 | } |
367 | ||
844f90fb VZ |
368 | // ---------------------------------------------------------------------------- |
369 | // path normalization | |
370 | // ---------------------------------------------------------------------------- | |
371 | ||
372 | bool wxFileName::Normalize(wxPathNormalize flags, | |
373 | const wxString& cwd, | |
374 | wxPathFormat format) | |
a35b27b1 | 375 | { |
844f90fb VZ |
376 | // the existing path components |
377 | wxArrayString dirs = GetDirs(); | |
378 | ||
379 | // the path to prepend in front to make the path absolute | |
380 | wxFileName curDir; | |
381 | ||
382 | format = GetFormat(format); | |
383 | ||
384 | // make the path absolute | |
385 | if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute() ) | |
a35b27b1 | 386 | { |
844f90fb VZ |
387 | if ( cwd.empty() ) |
388 | curDir.AssignCwd(); | |
a35b27b1 | 389 | else |
844f90fb | 390 | curDir.AssignDir(cwd); |
a35b27b1 | 391 | } |
844f90fb VZ |
392 | |
393 | // handle ~ stuff under Unix only | |
394 | if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) ) | |
a35b27b1 | 395 | { |
844f90fb VZ |
396 | if ( !dirs.IsEmpty() ) |
397 | { | |
398 | wxString dir = dirs[0u]; | |
399 | if ( !dir.empty() && dir[0u] == _T('~') ) | |
400 | { | |
401 | curDir.AssignDir(wxGetUserHome(dir.c_str() + 1)); | |
402 | ||
da2fd5ac | 403 | dirs.RemoveAt(0u); |
844f90fb VZ |
404 | } |
405 | } | |
a35b27b1 | 406 | } |
844f90fb VZ |
407 | |
408 | if ( curDir.IsOk() ) | |
a35b27b1 | 409 | { |
844f90fb VZ |
410 | wxArrayString dirsNew = curDir.GetDirs(); |
411 | size_t count = dirs.GetCount(); | |
412 | for ( size_t n = 0; n < count; n++ ) | |
413 | { | |
414 | dirsNew.Add(dirs[n]); | |
415 | } | |
416 | ||
417 | dirs = dirsNew; | |
a35b27b1 | 418 | } |
844f90fb VZ |
419 | |
420 | // now deal with ".", ".." and the rest | |
421 | m_dirs.Empty(); | |
422 | size_t count = dirs.GetCount(); | |
423 | for ( size_t n = 0; n < count; n++ ) | |
a35b27b1 | 424 | { |
844f90fb VZ |
425 | wxString dir = dirs[n]; |
426 | ||
427 | if ( flags && wxPATH_NORM_DOTS ) | |
428 | { | |
429 | if ( dir == wxT(".") ) | |
430 | { | |
431 | // just ignore | |
432 | continue; | |
433 | } | |
434 | ||
435 | if ( dir == wxT("..") ) | |
436 | { | |
437 | if ( m_dirs.IsEmpty() ) | |
438 | { | |
439 | wxLogError(_("The path '%s' contains too many \"..\"!"), | |
440 | GetFullPath().c_str()); | |
441 | return FALSE; | |
442 | } | |
443 | ||
444 | m_dirs.Remove(m_dirs.GetCount() - 1); | |
445 | continue; | |
446 | } | |
447 | } | |
448 | ||
449 | if ( flags & wxPATH_NORM_ENV_VARS ) | |
a35b27b1 | 450 | { |
844f90fb | 451 | dir = wxExpandEnvVars(dir); |
a35b27b1 | 452 | } |
844f90fb VZ |
453 | |
454 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
455 | { | |
456 | dir.MakeLower(); | |
457 | } | |
458 | ||
459 | m_dirs.Add(dir); | |
a35b27b1 | 460 | } |
844f90fb VZ |
461 | |
462 | if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) ) | |
463 | { | |
464 | // VZ: expand env vars here too? | |
465 | ||
466 | m_name.MakeLower(); | |
467 | m_ext.MakeLower(); | |
468 | } | |
469 | ||
9e9b65c1 JS |
470 | #if defined(__WXMSW__) && defined(__WIN32__) |
471 | if (flags & wxPATH_NORM_LONG) | |
472 | { | |
473 | Assign(GetLongPath()); | |
474 | } | |
475 | #endif | |
476 | ||
a35b27b1 RR |
477 | return TRUE; |
478 | } | |
479 | ||
844f90fb VZ |
480 | // ---------------------------------------------------------------------------- |
481 | // filename kind tests | |
482 | // ---------------------------------------------------------------------------- | |
483 | ||
484 | bool wxFileName::SameAs( const wxFileName &filepath, wxPathFormat format) | |
df5ddbca | 485 | { |
844f90fb VZ |
486 | wxFileName fn1 = *this, |
487 | fn2 = filepath; | |
488 | ||
489 | // get cwd only once - small time saving | |
490 | wxString cwd = wxGetCwd(); | |
491 | fn1.Normalize(wxPATH_NORM_ALL, cwd, format); | |
492 | fn2.Normalize(wxPATH_NORM_ALL, cwd, format); | |
493 | ||
494 | if ( fn1.GetFullPath() == fn2.GetFullPath() ) | |
495 | return TRUE; | |
496 | ||
497 | // TODO: compare inodes for Unix, this works even when filenames are | |
498 | // different but files are the same (symlinks) (VZ) | |
499 | ||
500 | return FALSE; | |
df5ddbca RR |
501 | } |
502 | ||
844f90fb | 503 | /* static */ |
df5ddbca RR |
504 | bool wxFileName::IsCaseSensitive( wxPathFormat format ) |
505 | { | |
3c621059 | 506 | // only DOS and OpenVMS filenames are case-sensitive |
9a9045a9 | 507 | return GetFormat(format) != wxPATH_DOS && GetFormat(format) != wxPATH_VMS; |
df5ddbca RR |
508 | } |
509 | ||
510 | bool wxFileName::IsRelative( wxPathFormat format ) | |
511 | { | |
844f90fb VZ |
512 | return !IsAbsolute(format); |
513 | } | |
514 | ||
515 | bool wxFileName::IsAbsolute( wxPathFormat format ) | |
516 | { | |
517 | wxChar ch = m_dirs.IsEmpty() ? _T('\0') : m_dirs[0u][0u]; | |
518 | ||
9e9b65c1 JS |
519 | // Hack to cope with e.g. c:\thing - need something better |
520 | wxChar driveSep = _T('\0'); | |
521 | if (!m_dirs.IsEmpty() && m_dirs[0].Length() > 1) | |
522 | driveSep = m_dirs[0u][1u]; | |
523 | ||
844f90fb VZ |
524 | // the path is absolute if it starts with a path separator or, only for |
525 | // Unix filenames, with "~" or "~user" | |
526 | return IsPathSeparator(ch, format) || | |
9e9b65c1 | 527 | driveSep == _T(':') || |
844f90fb VZ |
528 | (GetFormat(format) == wxPATH_UNIX && ch == _T('~') ); |
529 | } | |
530 | ||
531 | /* static */ | |
532 | wxString wxFileName::GetPathSeparators(wxPathFormat format) | |
533 | { | |
534 | wxString seps; | |
535 | switch ( GetFormat(format) ) | |
df5ddbca | 536 | { |
844f90fb VZ |
537 | case wxPATH_DOS: |
538 | // accept both as native APIs do | |
9e8d8607 | 539 | seps << wxFILE_SEP_PATH_UNIX << wxFILE_SEP_PATH_DOS; |
844f90fb VZ |
540 | break; |
541 | ||
542 | default: | |
543 | wxFAIL_MSG( _T("unknown wxPATH_XXX style") ); | |
544 | // fall through | |
545 | ||
546 | case wxPATH_UNIX: | |
9e8d8607 | 547 | seps = wxFILE_SEP_PATH_UNIX; |
844f90fb VZ |
548 | break; |
549 | ||
550 | case wxPATH_MAC: | |
9e8d8607 | 551 | seps = wxFILE_SEP_PATH_MAC; |
844f90fb | 552 | break; |
3c621059 JJ |
553 | |
554 | case wxPATH_VMS: | |
555 | seps = wxFILE_SEP_PATH_VMS; | |
556 | break; | |
df5ddbca RR |
557 | } |
558 | ||
844f90fb | 559 | return seps; |
df5ddbca RR |
560 | } |
561 | ||
844f90fb VZ |
562 | /* static */ |
563 | bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format) | |
df5ddbca | 564 | { |
844f90fb | 565 | return GetPathSeparators(format).Find(ch) != wxNOT_FOUND; |
df5ddbca RR |
566 | } |
567 | ||
568 | bool wxFileName::IsWild( wxPathFormat format ) | |
569 | { | |
844f90fb VZ |
570 | // FIXME: this is probably false for Mac and this is surely wrong for most |
571 | // of Unix shells (think about "[...]") | |
33ac7e6f | 572 | (void)format; |
844f90fb | 573 | return m_name.find_first_of(_T("*?")) != wxString::npos; |
df5ddbca RR |
574 | } |
575 | ||
844f90fb VZ |
576 | // ---------------------------------------------------------------------------- |
577 | // path components manipulation | |
578 | // ---------------------------------------------------------------------------- | |
579 | ||
df5ddbca RR |
580 | void wxFileName::AppendDir( const wxString &dir ) |
581 | { | |
582 | m_dirs.Add( dir ); | |
583 | } | |
584 | ||
585 | void wxFileName::PrependDir( const wxString &dir ) | |
586 | { | |
587 | m_dirs.Insert( dir, 0 ); | |
588 | } | |
589 | ||
590 | void wxFileName::InsertDir( int before, const wxString &dir ) | |
591 | { | |
592 | m_dirs.Insert( dir, before ); | |
593 | } | |
594 | ||
595 | void wxFileName::RemoveDir( int pos ) | |
596 | { | |
597 | m_dirs.Remove( (size_t)pos ); | |
598 | } | |
599 | ||
844f90fb VZ |
600 | // ---------------------------------------------------------------------------- |
601 | // accessors | |
602 | // ---------------------------------------------------------------------------- | |
603 | ||
7124df9b VZ |
604 | void wxFileName::SetFullName(const wxString& fullname) |
605 | { | |
606 | SplitPath(fullname, NULL /* no path */, &m_name, &m_ext); | |
607 | } | |
608 | ||
844f90fb | 609 | wxString wxFileName::GetFullName() const |
a35b27b1 | 610 | { |
844f90fb VZ |
611 | wxString fullname = m_name; |
612 | if ( !m_ext.empty() ) | |
a35b27b1 | 613 | { |
9e8d8607 | 614 | fullname << wxFILE_SEP_EXT << m_ext; |
a35b27b1 | 615 | } |
a35b27b1 | 616 | |
844f90fb | 617 | return fullname; |
a35b27b1 RR |
618 | } |
619 | ||
620 | wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const | |
df5ddbca RR |
621 | { |
622 | format = GetFormat( format ); | |
844f90fb | 623 | |
df5ddbca | 624 | wxString ret; |
844f90fb VZ |
625 | size_t count = m_dirs.GetCount(); |
626 | for ( size_t i = 0; i < count; i++ ) | |
df5ddbca | 627 | { |
844f90fb VZ |
628 | ret += m_dirs[i]; |
629 | if ( add_separator || (i < count) ) | |
630 | ret += wxFILE_SEP_PATH; | |
df5ddbca | 631 | } |
844f90fb | 632 | |
df5ddbca RR |
633 | return ret; |
634 | } | |
635 | ||
636 | wxString wxFileName::GetFullPath( wxPathFormat format ) const | |
637 | { | |
dcf0fce4 RR |
638 | format = GetFormat( format ); |
639 | ||
640 | wxString ret; | |
641 | if (format == wxPATH_DOS) | |
642 | { | |
643 | for (size_t i = 0; i < m_dirs.GetCount(); i++) | |
644 | { | |
645 | ret += m_dirs[i]; | |
646 | ret += '\\'; | |
647 | } | |
648 | } | |
649 | else | |
650 | if (format == wxPATH_UNIX) | |
651 | { | |
652 | for (size_t i = 0; i < m_dirs.GetCount(); i++) | |
653 | { | |
654 | ret += m_dirs[i]; | |
655 | ret += '/'; | |
656 | } | |
657 | } | |
658 | else | |
3c621059 JJ |
659 | if (format == wxPATH_VMS) |
660 | { | |
661 | ret += '['; | |
662 | for (size_t i = 0; i < m_dirs.GetCount(); i++) | |
663 | { | |
664 | ret += '.'; | |
665 | ret += m_dirs[i]; | |
666 | } | |
667 | ret += ']'; | |
668 | } | |
669 | else | |
dcf0fce4 RR |
670 | { |
671 | for (size_t i = 0; i < m_dirs.GetCount(); i++) | |
672 | { | |
673 | ret += m_dirs[i]; | |
674 | ret += ':'; | |
675 | } | |
676 | } | |
677 | ||
678 | ret += m_name; | |
679 | ||
680 | if (!m_ext.IsEmpty()) | |
681 | { | |
682 | ret += '.'; | |
683 | ret += m_ext; | |
684 | } | |
685 | ||
686 | return ret; | |
df5ddbca RR |
687 | } |
688 | ||
9e9b65c1 JS |
689 | // Return the short form of the path (returns identity on non-Windows platforms) |
690 | wxString wxFileName::GetShortPath() const | |
691 | { | |
8cb172b4 | 692 | #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) |
9e9b65c1 | 693 | wxString path(GetFullPath()); |
75ef5722 JS |
694 | wxString pathOut; |
695 | DWORD sz = ::GetShortPathName(path, NULL, 0); | |
696 | bool ok = sz != 0; | |
697 | if ( ok ) | |
9e9b65c1 | 698 | { |
75ef5722 JS |
699 | ok = ::GetShortPathName |
700 | ( | |
701 | path, | |
702 | pathOut.GetWriteBuf(sz), | |
703 | sz | |
704 | ) != 0; | |
705 | pathOut.UngetWriteBuf(); | |
9e9b65c1 | 706 | } |
75ef5722 JS |
707 | if (ok) |
708 | return pathOut; | |
5716a1ab VZ |
709 | |
710 | return path; | |
9e9b65c1 JS |
711 | #else |
712 | return GetFullPath(); | |
713 | #endif | |
714 | } | |
715 | ||
716 | // Return the long form of the path (returns identity on non-Windows platforms) | |
717 | wxString wxFileName::GetLongPath() const | |
718 | { | |
8cb172b4 | 719 | #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) |
9e9b65c1 | 720 | wxString path(GetFullPath()); |
75ef5722 | 721 | wxString pathOut; |
05e7001c JS |
722 | bool success = FALSE; |
723 | ||
5716a1ab VZ |
724 | // VZ: this code was disabled, why? |
725 | #if 0 // wxUSE_DYNLIB_CLASS | |
05e7001c JS |
726 | typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); |
727 | ||
728 | static bool s_triedToLoad = FALSE; | |
05e7001c JS |
729 | |
730 | if ( !s_triedToLoad ) | |
9e9b65c1 | 731 | { |
05e7001c | 732 | s_triedToLoad = TRUE; |
05e7001c | 733 | wxDllType dllKernel = wxDllLoader::LoadLibrary(_T("kernel32")); |
33ac7e6f | 734 | if ( dllKernel ) |
05e7001c JS |
735 | { |
736 | // may succeed or fail depending on the Windows version | |
33ac7e6f | 737 | static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL; |
5d978d07 JS |
738 | #ifdef _UNICODE |
739 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameW")); | |
740 | #else | |
741 | s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameA")); | |
742 | #endif | |
05e7001c JS |
743 | |
744 | wxDllLoader::UnloadLibrary(dllKernel); | |
745 | ||
746 | if ( s_pfnGetLongPathName ) | |
747 | { | |
748 | DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0); | |
749 | bool ok = dwSize > 0; | |
750 | ||
751 | if ( ok ) | |
752 | { | |
753 | DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0); | |
754 | ok = sz != 0; | |
755 | if ( ok ) | |
756 | { | |
757 | ok = (*s_pfnGetLongPathName) | |
758 | ( | |
759 | path, | |
760 | pathOut.GetWriteBuf(sz), | |
761 | sz | |
762 | ) != 0; | |
763 | pathOut.UngetWriteBuf(); | |
764 | ||
765 | success = TRUE; | |
766 | } | |
767 | } | |
768 | } | |
769 | } | |
9e9b65c1 | 770 | } |
05e7001c | 771 | if (success) |
75ef5722 | 772 | return pathOut; |
5716a1ab | 773 | #endif // wxUSE_DYNLIB_CLASS |
05e7001c JS |
774 | |
775 | if (!success) | |
776 | { | |
777 | // The OS didn't support GetLongPathName, or some other error. | |
778 | // We need to call FindFirstFile on each component in turn. | |
779 | ||
780 | WIN32_FIND_DATA findFileData; | |
781 | HANDLE hFind; | |
782 | pathOut = wxEmptyString; | |
783 | ||
77fe02a8 | 784 | wxArrayString dirs = GetDirs(); |
bcbe86e5 | 785 | dirs.Add(GetFullName()); |
77fe02a8 JS |
786 | |
787 | size_t count = dirs.GetCount(); | |
05e7001c JS |
788 | size_t i; |
789 | wxString tmpPath; | |
5d978d07 | 790 | |
05e7001c JS |
791 | for ( i = 0; i < count; i++ ) |
792 | { | |
793 | // We're using pathOut to collect the long-name path, | |
794 | // but using a temporary for appending the last path component which may be short-name | |
77fe02a8 | 795 | tmpPath = pathOut + dirs[i]; |
05e7001c JS |
796 | |
797 | if (tmpPath.Last() == wxT(':')) | |
5d978d07 JS |
798 | { |
799 | // Can't pass a drive and root dir to FindFirstFile, | |
800 | // so continue to next dir | |
05e7001c | 801 | tmpPath += wxFILE_SEP_PATH; |
5d978d07 JS |
802 | pathOut = tmpPath; |
803 | continue; | |
804 | } | |
05e7001c JS |
805 | |
806 | hFind = ::FindFirstFile(tmpPath, &findFileData); | |
807 | if (hFind == INVALID_HANDLE_VALUE) | |
808 | { | |
809 | // Error: return immediately with the original path | |
810 | return path; | |
811 | } | |
812 | else | |
813 | { | |
814 | pathOut += findFileData.cFileName; | |
5d978d07 | 815 | if ( (i < (count-1)) ) |
05e7001c JS |
816 | pathOut += wxFILE_SEP_PATH; |
817 | ||
818 | ::FindClose(hFind); | |
819 | } | |
820 | } | |
821 | } | |
5716a1ab | 822 | |
05e7001c | 823 | return pathOut; |
9e9b65c1 JS |
824 | #else |
825 | return GetFullPath(); | |
826 | #endif | |
827 | } | |
828 | ||
df5ddbca RR |
829 | wxPathFormat wxFileName::GetFormat( wxPathFormat format ) |
830 | { | |
831 | if (format == wxPATH_NATIVE) | |
832 | { | |
833 | #if defined(__WXMSW__) || defined(__WXPM__) | |
834 | format = wxPATH_DOS; | |
4d293f8e | 835 | #elif defined(__WXMAC__) && !defined(__DARWIN__) |
f72124db | 836 | format = wxPATH_MAC; |
3c621059 JJ |
837 | #elif defined(__VMS) |
838 | format = wxPATH_VMS; | |
844f90fb | 839 | #else |
df5ddbca RR |
840 | format = wxPATH_UNIX; |
841 | #endif | |
842 | } | |
843 | return format; | |
844 | } | |
a35b27b1 | 845 | |
9e8d8607 VZ |
846 | // ---------------------------------------------------------------------------- |
847 | // path splitting function | |
848 | // ---------------------------------------------------------------------------- | |
849 | ||
850 | void wxFileName::SplitPath(const wxString& fullpath, | |
851 | wxString *pstrPath, | |
852 | wxString *pstrName, | |
853 | wxString *pstrExt, | |
854 | wxPathFormat format) | |
855 | { | |
856 | format = GetFormat(format); | |
857 | ||
858 | // find the positions of the last dot and last path separator in the path | |
859 | size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT); | |
860 | size_t posLastSlash = fullpath.find_last_of(GetPathSeparators(format)); | |
861 | ||
862 | if ( (posLastDot != wxString::npos) && (format == wxPATH_UNIX) ) | |
863 | { | |
864 | if ( (posLastDot == 0) || | |
865 | (fullpath[posLastDot - 1] == wxFILE_SEP_PATH_UNIX) ) | |
866 | { | |
867 | // under Unix, dot may be (and commonly is) the first character of | |
868 | // the filename, don't treat the entire filename as extension in | |
3c621059 JJ |
869 | // this case |
870 | posLastDot = wxString::npos; | |
871 | } | |
872 | } | |
873 | else | |
874 | if ( (posLastDot != wxString::npos) && (format == wxPATH_VMS) ) | |
875 | { | |
876 | if ( (posLastDot == 0) || | |
877 | (fullpath[posLastDot - 1] == ']' ) ) | |
878 | { | |
879 | // under OpenVMS, dot may be (and commonly is) the first character of | |
880 | // the filename, don't treat the entire filename as extension in | |
9e8d8607 VZ |
881 | // this case |
882 | posLastDot = wxString::npos; | |
883 | } | |
884 | } | |
885 | ||
8e7dda21 VZ |
886 | // if we do have a dot and a slash, check that the dot is in the name part |
887 | if ( (posLastDot != wxString::npos) && | |
888 | (posLastSlash != wxString::npos) && | |
889 | (posLastDot < posLastSlash) ) | |
9e8d8607 VZ |
890 | { |
891 | // the dot is part of the path, not the start of the extension | |
892 | posLastDot = wxString::npos; | |
893 | } | |
894 | ||
895 | // now fill in the variables provided by user | |
896 | if ( pstrPath ) | |
897 | { | |
898 | if ( posLastSlash == wxString::npos ) | |
899 | { | |
900 | // no path at all | |
901 | pstrPath->Empty(); | |
902 | } | |
903 | else | |
904 | { | |
905 | // take all until the separator | |
906 | *pstrPath = fullpath.Left(posLastSlash); | |
907 | } | |
908 | } | |
909 | ||
910 | if ( pstrName ) | |
911 | { | |
42b1f941 VZ |
912 | // take all characters starting from the one after the last slash and |
913 | // up to, but excluding, the last dot | |
9e8d8607 | 914 | size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1; |
8e7dda21 VZ |
915 | size_t count; |
916 | if ( posLastDot == wxString::npos ) | |
917 | { | |
918 | // take all until the end | |
919 | count = wxString::npos; | |
920 | } | |
921 | else if ( posLastSlash == wxString::npos ) | |
922 | { | |
923 | count = posLastDot; | |
924 | } | |
925 | else // have both dot and slash | |
926 | { | |
927 | count = posLastDot - posLastSlash - 1; | |
928 | } | |
9e8d8607 VZ |
929 | |
930 | *pstrName = fullpath.Mid(nStart, count); | |
931 | } | |
932 | ||
933 | if ( pstrExt ) | |
934 | { | |
935 | if ( posLastDot == wxString::npos ) | |
936 | { | |
937 | // no extension | |
938 | pstrExt->Empty(); | |
939 | } | |
940 | else | |
941 | { | |
942 | // take everything after the dot | |
943 | *pstrExt = fullpath.Mid(posLastDot + 1); | |
944 | } | |
945 | } | |
946 | } | |
951cd180 VZ |
947 | |
948 | // ---------------------------------------------------------------------------- | |
949 | // time functions | |
950 | // ---------------------------------------------------------------------------- | |
951 | ||
952 | bool wxFileName::SetTimes(const wxDateTime *dtCreate, | |
953 | const wxDateTime *dtAccess, | |
954 | const wxDateTime *dtMod) | |
955 | { | |
956 | #if defined(__UNIX_LIKE__) | |
246c704f VZ |
957 | if ( !dtAccess && !dtMod ) |
958 | { | |
959 | // can't modify the creation time anyhow, don't try | |
960 | return TRUE; | |
961 | } | |
962 | ||
963 | // if dtAccess or dtMod is not specified, use the other one (which must be | |
964 | // non NULL because of the test above) for both times | |
951cd180 | 965 | utimbuf utm; |
246c704f VZ |
966 | utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks(); |
967 | utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks(); | |
951cd180 VZ |
968 | if ( utime(GetFullPath(), &utm) == 0 ) |
969 | { | |
970 | return TRUE; | |
971 | } | |
972 | #elif defined(__WIN32__) | |
973 | wxFileHandle fh(GetFullPath()); | |
974 | if ( fh.IsOk() ) | |
975 | { | |
976 | FILETIME ftAccess, ftCreate, ftWrite; | |
977 | ||
978 | if ( dtCreate ) | |
979 | ConvertWxToFileTime(&ftCreate, *dtCreate); | |
980 | if ( dtAccess ) | |
981 | ConvertWxToFileTime(&ftAccess, *dtAccess); | |
982 | if ( dtMod ) | |
983 | ConvertWxToFileTime(&ftWrite, *dtMod); | |
984 | ||
985 | if ( ::SetFileTime(fh, | |
986 | dtCreate ? &ftCreate : NULL, | |
987 | dtAccess ? &ftAccess : NULL, | |
988 | dtMod ? &ftWrite : NULL) ) | |
989 | { | |
990 | return TRUE; | |
991 | } | |
992 | } | |
993 | #else // other platform | |
994 | #endif // platforms | |
995 | ||
996 | wxLogSysError(_("Failed to modify file times for '%s'"), | |
997 | GetFullPath().c_str()); | |
998 | ||
999 | return FALSE; | |
1000 | } | |
1001 | ||
1002 | bool wxFileName::Touch() | |
1003 | { | |
1004 | #if defined(__UNIX_LIKE__) | |
1005 | // under Unix touching file is simple: just pass NULL to utime() | |
1006 | if ( utime(GetFullPath(), NULL) == 0 ) | |
1007 | { | |
1008 | return TRUE; | |
1009 | } | |
1010 | ||
1011 | wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str()); | |
1012 | ||
1013 | return FALSE; | |
1014 | #else // other platform | |
1015 | wxDateTime dtNow = wxDateTime::Now(); | |
1016 | ||
1017 | return SetTimes(NULL /* don't change create time */, &dtNow, &dtNow); | |
1018 | #endif // platforms | |
1019 | } | |
1020 | ||
1021 | bool wxFileName::GetTimes(wxDateTime *dtAccess, | |
1022 | wxDateTime *dtMod, | |
1023 | wxDateTime *dtChange) const | |
1024 | { | |
01d981ec RR |
1025 | #if defined(__UNIX_LIKE__) |
1026 | wxStructStat stBuf; | |
1027 | if ( wxStat(GetFullPath(), &stBuf) == 0 ) | |
1028 | { | |
1029 | if ( dtAccess ) | |
1030 | dtAccess->Set(stBuf.st_atime); | |
1031 | if ( dtMod ) | |
1032 | dtMod->Set(stBuf.st_mtime); | |
1033 | if ( dtChange ) | |
1034 | dtChange->Set(stBuf.st_ctime); | |
1035 | ||
1036 | return TRUE; | |
1037 | } | |
1038 | #elif defined(__WXMAC__) | |
951cd180 VZ |
1039 | wxStructStat stBuf; |
1040 | if ( wxStat(GetFullPath(), &stBuf) == 0 ) | |
1041 | { | |
1042 | if ( dtAccess ) | |
1043 | dtAccess->Set(stBuf.st_atime); | |
1044 | if ( dtMod ) | |
1045 | dtMod->Set(stBuf.st_mtime); | |
1046 | if ( dtChange ) | |
1047 | dtChange->Set(stBuf.st_ctime); | |
1048 | ||
1049 | return TRUE; | |
1050 | } | |
1051 | #elif defined(__WIN32__) | |
1052 | wxFileHandle fh(GetFullPath()); | |
1053 | if ( fh.IsOk() ) | |
1054 | { | |
1055 | FILETIME ftAccess, ftCreate, ftWrite; | |
1056 | ||
1057 | if ( ::GetFileTime(fh, | |
1058 | dtMod ? &ftCreate : NULL, | |
1059 | dtAccess ? &ftAccess : NULL, | |
1060 | dtChange ? &ftWrite : NULL) ) | |
1061 | { | |
1062 | if ( dtMod ) | |
1063 | ConvertFileTimeToWx(dtMod, ftCreate); | |
1064 | if ( dtAccess ) | |
1065 | ConvertFileTimeToWx(dtAccess, ftAccess); | |
1066 | if ( dtChange ) | |
1067 | ConvertFileTimeToWx(dtChange, ftWrite); | |
1068 | ||
1069 | return TRUE; | |
1070 | } | |
1071 | } | |
1072 | #else // other platform | |
1073 | #endif // platforms | |
1074 | ||
1075 | wxLogSysError(_("Failed to retrieve file times for '%s'"), | |
1076 | GetFullPath().c_str()); | |
1077 | ||
1078 | return FALSE; | |
1079 | } | |
1080 |