]> git.saurik.com Git - wxWidgets.git/blame - src/common/filename.cpp
Minor doc changes
[wxWidgets.git] / src / common / filename.cpp
CommitLineData
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
04c943b1
VZ
12/*
13 Here are brief descriptions of the filename formats supported by this class:
14
2bc60417
RR
15 wxPATH_UNIX: standard Unix format, used under Darwin as well, absolute file
16 names have the form:
04c943b1
VZ
17 /dir1/dir2/.../dirN/filename, "." and ".." stand for the
18 current and parent directory respectively, "~" is parsed as the
19 user HOME and "~username" as the HOME of that user
20
2bc60417 21 wxPATH_DOS: DOS/Windows format, absolute file names have the form:
04c943b1
VZ
22 drive:\dir1\dir2\...\dirN\filename.ext where drive is a single
23 letter. "." and ".." as for Unix but no "~".
24
25 There are also UNC names of the form \\share\fullpath
26
ae4d7004 27 wxPATH_MAC: Mac OS 8/9 and Mac OS X under CodeWarrior 7 format, absolute file
2bc60417 28 names have the form
04c943b1
VZ
29 volume:dir1:...:dirN:filename
30 and the relative file names are either
31 :dir1:...:dirN:filename
32 or just
33 filename
34 (although :filename works as well).
a385b5df 35 Since the volume is just part of the file path, it is not
353f41cb
RR
36 treated like a separate entity as it is done under DOS and
37 VMS, it is just treated as another dir.
04c943b1
VZ
38
39 wxPATH_VMS: VMS native format, absolute file names have the form
40 <device>:[dir1.dir2.dir3]file.txt
41 or
42 <device>:[000000.dir1.dir2.dir3]file.txt
43
44 the <device> is the physical device (i.e. disk). 000000 is the
45 root directory on the device which can be omitted.
46
47 Note that VMS uses different separators unlike Unix:
48 : always after the device. If the path does not contain : than
49 the default (the device of the current directory) is assumed.
50 [ start of directory specyfication
51 . separator between directory and subdirectory
52 ] between directory and file
53 */
54
097ead30
VZ
55// ============================================================================
56// declarations
57// ============================================================================
58
59// ----------------------------------------------------------------------------
60// headers
61// ----------------------------------------------------------------------------
62
df5ddbca
RR
63#ifdef __GNUG__
64 #pragma implementation "filename.h"
65#endif
66
67// For compilers that support precompilation, includes "wx.h".
68#include "wx/wxprec.h"
69
70#ifdef __BORLANDC__
71 #pragma hdrstop
72#endif
73
74#ifndef WX_PRECOMP
75 #include "wx/intl.h"
76 #include "wx/log.h"
77#endif
78
79#include "wx/filename.h"
80#include "wx/tokenzr.h"
844f90fb 81#include "wx/config.h" // for wxExpandEnvVars
a35b27b1 82#include "wx/utils.h"
5d978d07
JS
83
84#if wxUSE_DYNLIB_CLASS
951cd180 85 #include "wx/dynlib.h"
5d978d07 86#endif
df5ddbca 87
9e9b65c1
JS
88// For GetShort/LongPathName
89#ifdef __WIN32__
951cd180
VZ
90 #include <windows.h>
91
92 #include "wx/msw/winundef.h"
93#endif
94
95// utime() is POSIX so should normally be available on all Unices
96#ifdef __UNIX_LIKE__
97 #include <sys/types.h>
98 #include <utime.h>
4f2570aa
VZ
99 #include <sys/stat.h>
100 #include <unistd.h>
9e9b65c1
JS
101#endif
102
01d981ec
RR
103#ifdef __MWERKS__
104 #include <stat.h>
105 #include <unistd.h>
106 #include <unix.h>
107#endif
108
d9f54bb0
VS
109#ifdef __WATCOMC__
110 #include <io.h>
111 #include <sys/utime.h>
112 #include <sys/stat.h>
113#endif
114
24eb81cb
DW
115#ifdef __VISAGECPP__
116#ifndef MAX_PATH
117#define MAX_PATH 256
118#endif
119#endif
120
951cd180
VZ
121// ----------------------------------------------------------------------------
122// private classes
123// ----------------------------------------------------------------------------
124
125// small helper class which opens and closes the file - we use it just to get
126// a file handle for the given file name to pass it to some Win32 API function
c67d6888 127#if defined(__WIN32__) && !defined(__WXMICROWIN__)
951cd180
VZ
128
129class wxFileHandle
130{
131public:
132 wxFileHandle(const wxString& filename)
133 {
134 m_hFile = ::CreateFile
135 (
136 filename, // name
137 GENERIC_READ, // access mask
138 0, // no sharing
139 NULL, // no secutity attr
140 OPEN_EXISTING, // creation disposition
141 0, // no flags
142 NULL // no template file
143 );
144
145 if ( m_hFile == INVALID_HANDLE_VALUE )
146 {
147 wxLogSysError(_("Failed to open '%s' for reading"),
148 filename.c_str());
149 }
150 }
151
152 ~wxFileHandle()
153 {
154 if ( m_hFile != INVALID_HANDLE_VALUE )
155 {
156 if ( !::CloseHandle(m_hFile) )
157 {
158 wxLogSysError(_("Failed to close file handle"));
159 }
160 }
161 }
162
163 // return TRUE only if the file could be opened successfully
164 bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; }
165
166 // get the handle
167 operator HANDLE() const { return m_hFile; }
168
169private:
170 HANDLE m_hFile;
171};
172
173#endif // __WIN32__
174
175// ----------------------------------------------------------------------------
176// private functions
177// ----------------------------------------------------------------------------
178
c67d6888 179#if defined(__WIN32__) && !defined(__WXMICROWIN__)
951cd180
VZ
180
181// convert between wxDateTime and FILETIME which is a 64-bit value representing
182// the number of 100-nanosecond intervals since January 1, 1601.
183
d56e2b97
VZ
184// the number of milliseconds between the Unix Epoch (January 1, 1970) and the
185// FILETIME reference point (January 1, 1601)
186static const wxLongLong FILETIME_EPOCH_OFFSET = wxLongLong(0xa97, 0x30b66800);
187
188static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft)
951cd180 189{
d56e2b97
VZ
190 wxLongLong ll(ft.dwHighDateTime, ft.dwLowDateTime);
191
192 // convert 100ns to ms
193 ll /= 10000;
194
195 // move it to our Epoch
196 ll -= FILETIME_EPOCH_OFFSET;
197
198 *dt = wxDateTime(ll);
951cd180
VZ
199}
200
d56e2b97 201static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt)
951cd180 202{
d56e2b97
VZ
203 // do the reverse of ConvertFileTimeToWx()
204 wxLongLong ll = dt.GetValue();
205 ll *= 10000;
206 ll += FILETIME_EPOCH_OFFSET;
207
208 ft->dwHighDateTime = ll.GetHi();
209 ft->dwLowDateTime = ll.GetLo();
951cd180
VZ
210}
211
212#endif // __WIN32__
213
097ead30
VZ
214// ============================================================================
215// implementation
216// ============================================================================
217
218// ----------------------------------------------------------------------------
844f90fb 219// wxFileName construction
097ead30 220// ----------------------------------------------------------------------------
df5ddbca 221
a35b27b1
RR
222void wxFileName::Assign( const wxFileName &filepath )
223{
04c943b1 224 m_volume = filepath.GetVolume();
844f90fb 225 m_dirs = filepath.GetDirs();
04c943b1
VZ
226 m_name = filepath.GetName();
227 m_ext = filepath.GetExt();
353f41cb 228 m_relative = filepath.IsRelative();
df5ddbca
RR
229}
230
04c943b1
VZ
231void wxFileName::Assign(const wxString& volume,
232 const wxString& path,
233 const wxString& name,
234 const wxString& ext,
235 wxPathFormat format )
df5ddbca 236{
353f41cb
RR
237 wxPathFormat my_format = GetFormat( format );
238 wxString my_path = path;
239
844f90fb 240 m_dirs.Clear();
353f41cb
RR
241
242 if (!my_path.empty())
df5ddbca 243 {
353f41cb
RR
244 // 1) Determine if the path is relative or absolute.
245
246 switch (my_format)
247 {
248 case wxPATH_MAC:
249 m_relative = ( my_path[0u] == wxT(':') );
250 // We then remove a leading ":". The reason is in our
251 // storage form for relative paths:
252 // ":dir:file.txt" actually means "./dir/file.txt" in
253 // DOS notation and should get stored as
254 // (relative) (dir) (file.txt)
255 // "::dir:file.txt" actually means "../dir/file.txt"
256 // stored as (relative) (..) (dir) (file.txt)
257 // This is important only for the Mac as an empty dir
258 // actually means <UP>, whereas under DOS, double
259 // slashes can be ignored: "\\\\" is the same as "\\".
260 if (m_relative)
261 my_path.Remove( 0, 1 );
262 break;
263 case wxPATH_VMS:
264 // TODO: what is the relative path format here?
265 m_relative = FALSE;
266 break;
267 case wxPATH_UNIX:
268 m_relative = ( my_path[0u] != wxT('/') );
269 break;
270 case wxPATH_DOS:
271 m_relative = ( (my_path[0u] != wxT('/')) && (my_path[0u] != wxT('\\')) );
272 break;
273 default:
7613582b 274 wxFAIL_MSG( wxT("error") );
353f41cb
RR
275 break;
276 }
277
278 // 2) Break up the path into its members. If the original path
279 // was just "/" or "\\", m_dirs will be empty. We know from
280 // the m_relative field, if this means "nothing" or "root dir".
281
282 wxStringTokenizer tn( my_path, GetPathSeparators(my_format) );
283
284 while ( tn.HasMoreTokens() )
285 {
286 wxString token = tn.GetNextToken();
844f90fb 287
353f41cb
RR
288 // Remove empty token under DOS and Unix, interpret them
289 // as .. under Mac.
290 if (token.empty())
291 {
292 if (my_format == wxPATH_MAC)
293 m_dirs.Add( wxT("..") );
294 // else ignore
295 }
296 else
297 {
298 m_dirs.Add( token );
299 }
300 }
df5ddbca 301 }
844f90fb 302
04c943b1 303 m_volume = volume;
844f90fb
VZ
304 m_ext = ext;
305 m_name = name;
306}
307
308void wxFileName::Assign(const wxString& fullpath,
309 wxPathFormat format)
310{
04c943b1
VZ
311 wxString volume, path, name, ext;
312 SplitPath(fullpath, &volume, &path, &name, &ext, format);
844f90fb 313
04c943b1 314 Assign(volume, path, name, ext, format);
844f90fb
VZ
315}
316
81f25632 317void wxFileName::Assign(const wxString& fullpathOrig,
844f90fb
VZ
318 const wxString& fullname,
319 wxPathFormat format)
320{
81f25632
VZ
321 // always recognize fullpath as directory, even if it doesn't end with a
322 // slash
323 wxString fullpath = fullpathOrig;
324 if ( !wxEndsWithPathSeparator(fullpath) )
325 {
326 fullpath += GetPathSeparators(format)[0u];
327 }
328
52dbd056 329 wxString volume, path, name, ext;
81f25632
VZ
330
331 // do some consistency checks in debug mode: the name should be really just
353f41cb 332 // the filename and the path should be really just a path
81f25632
VZ
333#ifdef __WXDEBUG__
334 wxString pathDummy, nameDummy, extDummy;
335
336 SplitPath(fullname, &pathDummy, &name, &ext, format);
337
338 wxASSERT_MSG( pathDummy.empty(),
339 _T("the file name shouldn't contain the path") );
340
341 SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format);
342
343 wxASSERT_MSG( nameDummy.empty() && extDummy.empty(),
344 _T("the path shouldn't contain file name nor extension") );
345
346#else // !__WXDEBUG__
9e8d8607 347 SplitPath(fullname, NULL /* no path */, &name, &ext, format);
52dbd056 348 SplitPath(fullpath, &volume, &path, NULL, NULL, format);
81f25632 349#endif // __WXDEBUG__/!__WXDEBUG__
844f90fb 350
52dbd056
VZ
351 Assign(volume, path, name, ext, format);
352}
353
354void wxFileName::AssignDir(const wxString& dir, wxPathFormat format)
355{
81f25632 356 Assign(dir, _T(""), format);
844f90fb
VZ
357}
358
359void wxFileName::Clear()
360{
361 m_dirs.Clear();
04c943b1
VZ
362
363 m_volume =
844f90fb
VZ
364 m_name =
365 m_ext = wxEmptyString;
366}
367
368/* static */
369wxFileName wxFileName::FileName(const wxString& file)
370{
371 return wxFileName(file);
372}
373
374/* static */
375wxFileName wxFileName::DirName(const wxString& dir)
376{
377 wxFileName fn;
378 fn.AssignDir(dir);
379 return fn;
df5ddbca
RR
380}
381
844f90fb
VZ
382// ----------------------------------------------------------------------------
383// existence tests
384// ----------------------------------------------------------------------------
385
df5ddbca
RR
386bool wxFileName::FileExists()
387{
a35b27b1
RR
388 return wxFileName::FileExists( GetFullPath() );
389}
390
391bool wxFileName::FileExists( const wxString &file )
392{
393 return ::wxFileExists( file );
df5ddbca
RR
394}
395
396bool wxFileName::DirExists()
397{
a35b27b1
RR
398 return wxFileName::DirExists( GetFullPath() );
399}
400
401bool wxFileName::DirExists( const wxString &dir )
402{
403 return ::wxDirExists( dir );
df5ddbca
RR
404}
405
844f90fb
VZ
406// ----------------------------------------------------------------------------
407// CWD and HOME stuff
408// ----------------------------------------------------------------------------
409
6f91bc33 410void wxFileName::AssignCwd(const wxString& volume)
df5ddbca 411{
6f91bc33 412 AssignDir(wxFileName::GetCwd(volume));
a35b27b1
RR
413}
414
844f90fb 415/* static */
6f91bc33 416wxString wxFileName::GetCwd(const wxString& volume)
a35b27b1 417{
6f91bc33
VZ
418 // if we have the volume, we must get the current directory on this drive
419 // and to do this we have to chdir to this volume - at least under Windows,
420 // I don't know how to get the current drive on another volume elsewhere
421 // (TODO)
422 wxString cwdOld;
423 if ( !volume.empty() )
424 {
425 cwdOld = wxGetCwd();
426 SetCwd(volume + GetVolumeSeparator());
427 }
428
429 wxString cwd = ::wxGetCwd();
430
431 if ( !volume.empty() )
432 {
433 SetCwd(cwdOld);
434 }
ae4d7004 435
6f91bc33 436 return cwd;
a35b27b1
RR
437}
438
439bool wxFileName::SetCwd()
440{
441 return wxFileName::SetCwd( GetFullPath() );
df5ddbca
RR
442}
443
a35b27b1 444bool wxFileName::SetCwd( const wxString &cwd )
df5ddbca 445{
a35b27b1 446 return ::wxSetWorkingDirectory( cwd );
df5ddbca
RR
447}
448
a35b27b1
RR
449void wxFileName::AssignHomeDir()
450{
844f90fb 451 AssignDir(wxFileName::GetHomeDir());
a35b27b1 452}
844f90fb 453
a35b27b1
RR
454wxString wxFileName::GetHomeDir()
455{
456 return ::wxGetHomeDir();
457}
844f90fb 458
df22f860 459void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp)
df5ddbca 460{
df22f860 461 wxString tempname = CreateTempFileName(prefix, fileTemp);
ade35f11 462 if ( tempname.empty() )
844f90fb 463 {
ade35f11
VZ
464 // error, failed to get temp file name
465 Clear();
844f90fb 466 }
ade35f11 467 else // ok
844f90fb 468 {
ade35f11
VZ
469 Assign(tempname);
470 }
471}
472
473/* static */
df22f860
VZ
474wxString
475wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
ade35f11
VZ
476{
477 wxString path, dir, name;
478
479 // use the directory specified by the prefix
480 SplitPath(prefix, &dir, &name, NULL /* extension */);
481
482#if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
483
484#ifdef __WIN32__
485 if ( dir.empty() )
486 {
487 if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) )
488 {
489 wxLogLastError(_T("GetTempPath"));
490 }
491
492 if ( dir.empty() )
493 {
494 // GetTempFileName() fails if we pass it an empty string
495 dir = _T('.');
496 }
497 }
498
499 if ( !::GetTempFileName(dir, name, 0, wxStringBuffer(path, MAX_PATH + 1)) )
500 {
501 wxLogLastError(_T("GetTempFileName"));
502
503 path.clear();
504 }
505#else // Win16
506 if ( !::GetTempFileName(NULL, prefix, 0, wxStringBuffer(path, 1025)) )
507 {
508 path.clear();
509 }
510#endif // Win32/16
511
512#elif defined(__WXPM__)
513 // for now just create a file
514 //
515 // future enhancements can be to set some extended attributes for file
516 // systems OS/2 supports that have them (HPFS, FAT32) and security
517 // (HPFS386)
518 static const wxChar *szMktempSuffix = wxT("XXX");
519 path << dir << _T('/') << name << szMktempSuffix;
520
521 // Temporarily remove - MN
522 #ifndef __WATCOMC__
24eb81cb 523 ::DosCreateDir(wxStringBuffer(path, MAX_PATH), NULL);
ade35f11 524 #endif
7070f55b
VS
525
526#else // !Windows, !OS/2
ade35f11
VZ
527 if ( dir.empty() )
528 {
529 dir = wxGetenv(_T("TMP"));
530 if ( path.empty() )
531 {
532 dir = wxGetenv(_T("TEMP"));
533 }
534
535 if ( dir.empty() )
536 {
537 // default
d9f54bb0
VS
538 #ifdef __DOS__
539 dir = _T(".");
540 #else
ade35f11 541 dir = _T("/tmp");
d9f54bb0 542 #endif
ade35f11 543 }
844f90fb 544 }
ade35f11
VZ
545
546 path = dir;
547
548 if ( !wxEndsWithPathSeparator(dir) &&
549 (name.empty() || !wxIsPathSeparator(name[0u])) )
550 {
d9f54bb0 551 path += wxFILE_SEP_PATH;
ade35f11
VZ
552 }
553
554 path += name;
555
7070f55b 556#if defined(HAVE_MKSTEMP)
ade35f11
VZ
557 // scratch space for mkstemp()
558 path += _T("XXXXXX");
559
560 // can use the cast here because the length doesn't change and the string
561 // is not shared
df22f860
VZ
562 int fdTemp = mkstemp((char *)path.mb_str());
563 if ( fdTemp == -1 )
ade35f11
VZ
564 {
565 // this might be not necessary as mkstemp() on most systems should have
566 // already done it but it doesn't hurt neither...
567 path.clear();
568 }
df22f860
VZ
569 else // mkstemp() succeeded
570 {
571 // avoid leaking the fd
572 if ( fileTemp )
573 {
574 fileTemp->Attach(fdTemp);
575 }
576 else
577 {
578 close(fdTemp);
579 }
580 }
ade35f11
VZ
581#else // !HAVE_MKSTEMP
582
583#ifdef HAVE_MKTEMP
584 // same as above
585 path += _T("XXXXXX");
586
587 if ( !mktemp((char *)path.mb_str()) )
588 {
589 path.clear();
590 }
7070f55b 591#else // !HAVE_MKTEMP (includes __DOS__)
ade35f11 592 // generate the unique file name ourselves
7070f55b 593 #ifndef __DOS__
ade35f11 594 path << (unsigned int)getpid();
7070f55b 595 #endif
ade35f11
VZ
596
597 wxString pathTry;
598
599 static const size_t numTries = 1000;
600 for ( size_t n = 0; n < numTries; n++ )
601 {
602 // 3 hex digits is enough for numTries == 1000 < 4096
603 pathTry = path + wxString::Format(_T("%.03x"), n);
604 if ( !wxFile::Exists(pathTry) )
605 {
606 break;
607 }
608
609 pathTry.clear();
610 }
611
612 path = pathTry;
613#endif // HAVE_MKTEMP/!HAVE_MKTEMP
614
615 if ( !path.empty() )
616 {
df22f860
VZ
617 }
618#endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
619
620#endif // Windows/!Windows
621
622 if ( path.empty() )
623 {
624 wxLogSysError(_("Failed to create a temporary file name"));
625 }
626 else if ( fileTemp && !fileTemp->IsOpened() )
627 {
628 // open the file - of course, there is a race condition here, this is
ade35f11 629 // why we always prefer using mkstemp()...
df22f860 630 if ( !fileTemp->Open(path, wxFile::write_excl, wxS_IRUSR | wxS_IWUSR) )
ade35f11
VZ
631 {
632 // FIXME: If !ok here should we loop and try again with another
633 // file name? That is the standard recourse if open(O_EXCL)
634 // fails, though of course it should be protected against
635 // possible infinite looping too.
636
637 wxLogError(_("Failed to open temporary file."));
638
639 path.clear();
640 }
641 }
ade35f11
VZ
642
643 return path;
df5ddbca
RR
644}
645
844f90fb
VZ
646// ----------------------------------------------------------------------------
647// directory operations
648// ----------------------------------------------------------------------------
649
f0ce3409 650bool wxFileName::Mkdir( int perm, bool full )
a35b27b1 651{
f0ce3409 652 return wxFileName::Mkdir( GetFullPath(), perm, full );
a35b27b1
RR
653}
654
f0ce3409 655bool wxFileName::Mkdir( const wxString &dir, int perm, bool full )
df5ddbca 656{
f0ce3409
JS
657 if (full)
658 {
659 wxFileName filename(dir);
660 wxArrayString dirs = filename.GetDirs();
77fe02a8 661 dirs.Add(filename.GetName());
f0ce3409
JS
662
663 size_t count = dirs.GetCount();
664 size_t i;
665 wxString currPath;
666 int noErrors = 0;
667 for ( i = 0; i < count; i++ )
668 {
669 currPath += dirs[i];
670
671 if (currPath.Last() == wxT(':'))
672 {
673 // Can't create a root directory so continue to next dir
674 currPath += wxFILE_SEP_PATH;
675 continue;
676 }
677
678 if (!DirExists(currPath))
679 if (!wxMkdir(currPath, perm))
680 noErrors ++;
681
682 if ( (i < (count-1)) )
683 currPath += wxFILE_SEP_PATH;
684 }
685
686 return (noErrors == 0);
687
688 }
689 else
690 return ::wxMkdir( dir, perm );
df5ddbca
RR
691}
692
a35b27b1 693bool wxFileName::Rmdir()
df5ddbca 694{
a35b27b1 695 return wxFileName::Rmdir( GetFullPath() );
df5ddbca
RR
696}
697
a35b27b1 698bool wxFileName::Rmdir( const wxString &dir )
df5ddbca 699{
a35b27b1 700 return ::wxRmdir( dir );
df5ddbca
RR
701}
702
844f90fb
VZ
703// ----------------------------------------------------------------------------
704// path normalization
705// ----------------------------------------------------------------------------
706
707bool wxFileName::Normalize(wxPathNormalize flags,
708 const wxString& cwd,
709 wxPathFormat format)
a35b27b1 710{
844f90fb
VZ
711 // the existing path components
712 wxArrayString dirs = GetDirs();
713
714 // the path to prepend in front to make the path absolute
715 wxFileName curDir;
716
717 format = GetFormat(format);
ae4d7004 718
844f90fb 719 // make the path absolute
353f41cb 720 if ( (flags & wxPATH_NORM_ABSOLUTE) && m_relative )
a35b27b1 721 {
844f90fb 722 if ( cwd.empty() )
6f91bc33
VZ
723 {
724 curDir.AssignCwd(GetVolume());
725 }
726 else // cwd provided
727 {
844f90fb 728 curDir.AssignDir(cwd);
6f91bc33 729 }
52dbd056 730
353f41cb 731#if 0
52dbd056
VZ
732 // the path may be not absolute because it doesn't have the volume name
733 // but in this case we shouldn't modify the directory components of it
734 // but just set the current volume
735 if ( !HasVolume() && curDir.HasVolume() )
736 {
737 SetVolume(curDir.GetVolume());
738
739 if ( IsAbsolute() )
740 {
741 // yes, it was the case - we don't need curDir then
742 curDir.Clear();
743 }
744 }
353f41cb 745#endif
5fc67e5c 746 m_relative = FALSE;
a35b27b1 747 }
ae4d7004 748
844f90fb
VZ
749 // handle ~ stuff under Unix only
750 if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
a35b27b1 751 {
844f90fb
VZ
752 if ( !dirs.IsEmpty() )
753 {
754 wxString dir = dirs[0u];
755 if ( !dir.empty() && dir[0u] == _T('~') )
756 {
757 curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
758
da2fd5ac 759 dirs.RemoveAt(0u);
844f90fb
VZ
760 }
761 }
a35b27b1 762 }
844f90fb 763
52dbd056 764 // transform relative path into abs one
844f90fb 765 if ( curDir.IsOk() )
a35b27b1 766 {
844f90fb
VZ
767 wxArrayString dirsNew = curDir.GetDirs();
768 size_t count = dirs.GetCount();
769 for ( size_t n = 0; n < count; n++ )
770 {
771 dirsNew.Add(dirs[n]);
772 }
773
774 dirs = dirsNew;
a35b27b1 775 }
844f90fb
VZ
776
777 // now deal with ".", ".." and the rest
778 m_dirs.Empty();
779 size_t count = dirs.GetCount();
780 for ( size_t n = 0; n < count; n++ )
a35b27b1 781 {
844f90fb
VZ
782 wxString dir = dirs[n];
783
2bc60417 784 if ( flags & wxPATH_NORM_DOTS )
844f90fb
VZ
785 {
786 if ( dir == wxT(".") )
787 {
788 // just ignore
789 continue;
790 }
791
792 if ( dir == wxT("..") )
793 {
794 if ( m_dirs.IsEmpty() )
795 {
796 wxLogError(_("The path '%s' contains too many \"..\"!"),
797 GetFullPath().c_str());
798 return FALSE;
799 }
800
ae4d7004 801 m_dirs.RemoveAt(m_dirs.GetCount() - 1);
844f90fb
VZ
802 continue;
803 }
804 }
805
806 if ( flags & wxPATH_NORM_ENV_VARS )
a35b27b1 807 {
844f90fb 808 dir = wxExpandEnvVars(dir);
a35b27b1 809 }
844f90fb
VZ
810
811 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
812 {
813 dir.MakeLower();
814 }
815
816 m_dirs.Add(dir);
a35b27b1 817 }
844f90fb
VZ
818
819 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
820 {
821 // VZ: expand env vars here too?
822
823 m_name.MakeLower();
824 m_ext.MakeLower();
825 }
826
52dbd056
VZ
827#if defined(__WIN32__)
828 if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) )
9e9b65c1
JS
829 {
830 Assign(GetLongPath());
831 }
52dbd056 832#endif // Win32
9e9b65c1 833
a35b27b1
RR
834 return TRUE;
835}
836
f7d886af
VZ
837bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format)
838{
839 wxFileName fnBase(pathBase, format);
840
841 // get cwd only once - small time saving
842 wxString cwd = wxGetCwd();
843 Normalize(wxPATH_NORM_ALL, cwd, format);
844 fnBase.Normalize(wxPATH_NORM_ALL, cwd, format);
845
846 bool withCase = IsCaseSensitive(format);
847
848 // we can't do anything if the files live on different volumes
849 if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) )
850 {
851 // nothing done
852 return FALSE;
853 }
854
855 // same drive, so we don't need our volume
856 m_volume.clear();
857
858 // remove common directories starting at the top
859 while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() &&
860 m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) )
861 {
ae4d7004
VZ
862 m_dirs.RemoveAt(0);
863 fnBase.m_dirs.RemoveAt(0);
f7d886af
VZ
864 }
865
866 // add as many ".." as needed
867 size_t count = fnBase.m_dirs.GetCount();
868 for ( size_t i = 0; i < count; i++ )
869 {
870 m_dirs.Insert(wxT(".."), 0u);
871 }
353f41cb
RR
872
873 m_relative = TRUE;
f7d886af
VZ
874
875 // we were modified
876 return TRUE;
877}
878
844f90fb
VZ
879// ----------------------------------------------------------------------------
880// filename kind tests
881// ----------------------------------------------------------------------------
882
f7d886af 883bool wxFileName::SameAs(const wxFileName &filepath, wxPathFormat format)
df5ddbca 884{
844f90fb
VZ
885 wxFileName fn1 = *this,
886 fn2 = filepath;
887
888 // get cwd only once - small time saving
889 wxString cwd = wxGetCwd();
890 fn1.Normalize(wxPATH_NORM_ALL, cwd, format);
891 fn2.Normalize(wxPATH_NORM_ALL, cwd, format);
892
893 if ( fn1.GetFullPath() == fn2.GetFullPath() )
894 return TRUE;
895
896 // TODO: compare inodes for Unix, this works even when filenames are
897 // different but files are the same (symlinks) (VZ)
898
899 return FALSE;
df5ddbca
RR
900}
901
844f90fb 902/* static */
df5ddbca
RR
903bool wxFileName::IsCaseSensitive( wxPathFormat format )
904{
04c943b1
VZ
905 // only Unix filenames are truely case-sensitive
906 return GetFormat(format) == wxPATH_UNIX;
df5ddbca
RR
907}
908
04c943b1
VZ
909/* static */
910wxString wxFileName::GetVolumeSeparator(wxPathFormat format)
844f90fb 911{
04c943b1
VZ
912 wxString sepVol;
913
a385b5df
RR
914 if ( (GetFormat(format) == wxPATH_DOS) ||
915 (GetFormat(format) == wxPATH_VMS) )
04c943b1 916 {
04c943b1
VZ
917 sepVol = wxFILE_SEP_DSK;
918 }
a385b5df 919 //else: leave empty
04c943b1
VZ
920
921 return sepVol;
844f90fb
VZ
922}
923
924/* static */
925wxString wxFileName::GetPathSeparators(wxPathFormat format)
926{
927 wxString seps;
928 switch ( GetFormat(format) )
df5ddbca 929 {
844f90fb 930 case wxPATH_DOS:
04c943b1
VZ
931 // accept both as native APIs do but put the native one first as
932 // this is the one we use in GetFullPath()
933 seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX;
844f90fb
VZ
934 break;
935
936 default:
937 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
938 // fall through
939
940 case wxPATH_UNIX:
9e8d8607 941 seps = wxFILE_SEP_PATH_UNIX;
844f90fb
VZ
942 break;
943
944 case wxPATH_MAC:
9e8d8607 945 seps = wxFILE_SEP_PATH_MAC;
844f90fb 946 break;
04c943b1 947
3c621059
JJ
948 case wxPATH_VMS:
949 seps = wxFILE_SEP_PATH_VMS;
950 break;
df5ddbca
RR
951 }
952
844f90fb 953 return seps;
df5ddbca
RR
954}
955
844f90fb
VZ
956/* static */
957bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format)
df5ddbca 958{
04c943b1
VZ
959 // wxString::Find() doesn't work as expected with NUL - it will always find
960 // it, so it is almost surely a bug if this function is called with NUL arg
961 wxASSERT_MSG( ch != _T('\0'), _T("shouldn't be called with NUL") );
962
844f90fb 963 return GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
df5ddbca
RR
964}
965
966bool wxFileName::IsWild( wxPathFormat format )
967{
844f90fb
VZ
968 // FIXME: this is probably false for Mac and this is surely wrong for most
969 // of Unix shells (think about "[...]")
04c943b1 970 (void)format;
844f90fb 971 return m_name.find_first_of(_T("*?")) != wxString::npos;
df5ddbca
RR
972}
973
844f90fb
VZ
974// ----------------------------------------------------------------------------
975// path components manipulation
976// ----------------------------------------------------------------------------
977
df5ddbca
RR
978void wxFileName::AppendDir( const wxString &dir )
979{
980 m_dirs.Add( dir );
981}
982
983void wxFileName::PrependDir( const wxString &dir )
984{
985 m_dirs.Insert( dir, 0 );
986}
987
988void wxFileName::InsertDir( int before, const wxString &dir )
989{
990 m_dirs.Insert( dir, before );
991}
992
993void wxFileName::RemoveDir( int pos )
994{
995 m_dirs.Remove( (size_t)pos );
996}
997
844f90fb
VZ
998// ----------------------------------------------------------------------------
999// accessors
1000// ----------------------------------------------------------------------------
1001
7124df9b
VZ
1002void wxFileName::SetFullName(const wxString& fullname)
1003{
1004 SplitPath(fullname, NULL /* no path */, &m_name, &m_ext);
1005}
1006
844f90fb 1007wxString wxFileName::GetFullName() const
a35b27b1 1008{
844f90fb
VZ
1009 wxString fullname = m_name;
1010 if ( !m_ext.empty() )
a35b27b1 1011 {
9e8d8607 1012 fullname << wxFILE_SEP_EXT << m_ext;
a35b27b1 1013 }
a35b27b1 1014
844f90fb 1015 return fullname;
a35b27b1
RR
1016}
1017
1018wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const
df5ddbca
RR
1019{
1020 format = GetFormat( format );
844f90fb 1021
353f41cb
RR
1022 wxString fullpath;
1023
1024 // the leading character
1025 if ( format == wxPATH_MAC && m_relative )
1026 {
1027 fullpath += wxFILE_SEP_PATH_MAC;
1028 }
1029 else if ( format == wxPATH_DOS )
1030 {
1031 if (!m_relative)
1032 fullpath += wxFILE_SEP_PATH_DOS;
1033 }
1034 else if ( format == wxPATH_UNIX )
1035 {
1036 if (!m_relative)
1037 fullpath += wxFILE_SEP_PATH_UNIX;
1038 }
1039
1040 // then concatenate all the path components using the path separator
1041 size_t dirCount = m_dirs.GetCount();
1042 if ( dirCount )
df5ddbca 1043 {
353f41cb
RR
1044 if ( format == wxPATH_VMS )
1045 {
1046 fullpath += wxT('[');
1047 }
1048
1049
1050 for ( size_t i = 0; i < dirCount; i++ )
1051 {
1052 // TODO: What to do with ".." under VMS
1053
1054 switch (format)
1055 {
1056 case wxPATH_MAC:
1057 {
1058 if (m_dirs[i] == wxT("."))
1059 break;
1060 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1061 fullpath += m_dirs[i];
1062 fullpath += wxT(':');
1063 break;
1064 }
1065 case wxPATH_DOS:
1066 {
1067 fullpath += m_dirs[i];
1068 fullpath += wxT('\\');
1069 break;
1070 }
1071 case wxPATH_UNIX:
1072 {
1073 fullpath += m_dirs[i];
1074 fullpath += wxT('/');
1075 break;
1076 }
1077 case wxPATH_VMS:
1078 {
1079 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1080 fullpath += m_dirs[i];
1081 if (i == dirCount-1)
1082 fullpath += wxT(']');
1083 else
1084 fullpath += wxT('.');
1085 break;
1086 }
1087 default:
1088 {
7613582b 1089 wxFAIL_MSG( wxT("error") );
353f41cb
RR
1090 }
1091 }
1092 }
df5ddbca 1093 }
844f90fb 1094
353f41cb
RR
1095
1096
1097 return fullpath;
df5ddbca
RR
1098}
1099
1100wxString wxFileName::GetFullPath( wxPathFormat format ) const
1101{
04c943b1
VZ
1102 format = GetFormat(format);
1103
1104 wxString fullpath;
1105
1106 // first put the volume
1107 if ( !m_volume.empty() )
dcf0fce4 1108 {
5fc67e5c
RR
1109 {
1110 // Special Windows UNC paths hack, part 2: undo what we did in
1111 // SplitPath() and make an UNC path if we have a drive which is not a
1112 // single letter (hopefully the network shares can't be one letter only
1113 // although I didn't find any authoritative docs on this)
1114 if ( format == wxPATH_DOS && m_volume.length() > 1 )
1115 {
1116 fullpath << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << m_volume;
1117 }
1118 else if ( format == wxPATH_DOS || format == wxPATH_VMS )
1119 {
1120 fullpath << m_volume << GetVolumeSeparator(format);
a385b5df 1121 }
353f41cb 1122 // else ignore
dcf0fce4
RR
1123 }
1124 }
353f41cb
RR
1125
1126 // the leading character
1127 if ( format == wxPATH_MAC && m_relative )
1128 {
1129 fullpath += wxFILE_SEP_PATH_MAC;
1130 }
1131 else if ( format == wxPATH_DOS )
1132 {
1133 if (!m_relative)
1134 fullpath += wxFILE_SEP_PATH_DOS;
1135 }
1136 else if ( format == wxPATH_UNIX )
1137 {
1138 if (!m_relative)
1139 fullpath += wxFILE_SEP_PATH_UNIX;
1140 }
1141
04c943b1
VZ
1142 // then concatenate all the path components using the path separator
1143 size_t dirCount = m_dirs.GetCount();
1144 if ( dirCount )
3c621059 1145 {
04c943b1 1146 if ( format == wxPATH_VMS )
dcf0fce4 1147 {
353f41cb 1148 fullpath += wxT('[');
04c943b1
VZ
1149 }
1150
353f41cb 1151
04c943b1
VZ
1152 for ( size_t i = 0; i < dirCount; i++ )
1153 {
353f41cb 1154 // TODO: What to do with ".." under VMS
04c943b1 1155
353f41cb
RR
1156 switch (format)
1157 {
1158 case wxPATH_MAC:
1159 {
1160 if (m_dirs[i] == wxT("."))
1161 break;
1162 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1163 fullpath += m_dirs[i];
1164 fullpath += wxT(':');
1165 break;
1166 }
1167 case wxPATH_DOS:
1168 {
1169 fullpath += m_dirs[i];
1170 fullpath += wxT('\\');
1171 break;
1172 }
1173 case wxPATH_UNIX:
1174 {
1175 fullpath += m_dirs[i];
1176 fullpath += wxT('/');
1177 break;
1178 }
1179 case wxPATH_VMS:
1180 {
1181 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1182 fullpath += m_dirs[i];
1183 if (i == dirCount-1)
1184 fullpath += wxT(']');
1185 else
1186 fullpath += wxT('.');
1187 break;
1188 }
1189 default:
1190 {
7613582b 1191 wxFAIL_MSG( wxT("error") );
353f41cb
RR
1192 }
1193 }
dcf0fce4
RR
1194 }
1195 }
04c943b1
VZ
1196
1197 // finally add the file name and extension
1198 fullpath += GetFullName();
1199
1200 return fullpath;
df5ddbca
RR
1201}
1202
9e9b65c1
JS
1203// Return the short form of the path (returns identity on non-Windows platforms)
1204wxString wxFileName::GetShortPath() const
1205{
8cb172b4 1206#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
9e9b65c1 1207 wxString path(GetFullPath());
75ef5722
JS
1208 wxString pathOut;
1209 DWORD sz = ::GetShortPathName(path, NULL, 0);
1210 bool ok = sz != 0;
1211 if ( ok )
9e9b65c1 1212 {
75ef5722
JS
1213 ok = ::GetShortPathName
1214 (
1215 path,
1216 pathOut.GetWriteBuf(sz),
1217 sz
1218 ) != 0;
1219 pathOut.UngetWriteBuf();
9e9b65c1 1220 }
75ef5722
JS
1221 if (ok)
1222 return pathOut;
5716a1ab
VZ
1223
1224 return path;
9e9b65c1
JS
1225#else
1226 return GetFullPath();
1227#endif
1228}
1229
1230// Return the long form of the path (returns identity on non-Windows platforms)
1231wxString wxFileName::GetLongPath() const
1232{
52dbd056
VZ
1233 wxString pathOut,
1234 path = GetFullPath();
1235
1236#if defined(__WIN32__) && !defined(__WXMICROWIN__)
05e7001c
JS
1237 bool success = FALSE;
1238
5716a1ab
VZ
1239 // VZ: this code was disabled, why?
1240#if 0 // wxUSE_DYNLIB_CLASS
05e7001c
JS
1241 typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
1242
1243 static bool s_triedToLoad = FALSE;
05e7001c
JS
1244
1245 if ( !s_triedToLoad )
9e9b65c1 1246 {
05e7001c 1247 s_triedToLoad = TRUE;
05e7001c 1248 wxDllType dllKernel = wxDllLoader::LoadLibrary(_T("kernel32"));
33ac7e6f 1249 if ( dllKernel )
05e7001c
JS
1250 {
1251 // may succeed or fail depending on the Windows version
04c943b1 1252 static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
5d978d07
JS
1253#ifdef _UNICODE
1254 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameW"));
1255#else
1256 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameA"));
1257#endif
05e7001c
JS
1258
1259 wxDllLoader::UnloadLibrary(dllKernel);
1260
1261 if ( s_pfnGetLongPathName )
1262 {
1263 DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0);
1264 bool ok = dwSize > 0;
1265
1266 if ( ok )
1267 {
1268 DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0);
1269 ok = sz != 0;
1270 if ( ok )
1271 {
1272 ok = (*s_pfnGetLongPathName)
1273 (
1274 path,
1275 pathOut.GetWriteBuf(sz),
1276 sz
1277 ) != 0;
1278 pathOut.UngetWriteBuf();
1279
1280 success = TRUE;
1281 }
1282 }
1283 }
1284 }
9e9b65c1 1285 }
05e7001c 1286 if (success)
75ef5722 1287 return pathOut;
5716a1ab 1288#endif // wxUSE_DYNLIB_CLASS
05e7001c
JS
1289
1290 if (!success)
1291 {
1292 // The OS didn't support GetLongPathName, or some other error.
1293 // We need to call FindFirstFile on each component in turn.
1294
1295 WIN32_FIND_DATA findFileData;
1296 HANDLE hFind;
1297 pathOut = wxEmptyString;
1298
77fe02a8 1299 wxArrayString dirs = GetDirs();
bcbe86e5 1300 dirs.Add(GetFullName());
77fe02a8 1301
05e7001c 1302 wxString tmpPath;
5d978d07 1303
52dbd056
VZ
1304 size_t count = dirs.GetCount();
1305 for ( size_t i = 0; i < count; i++ )
05e7001c 1306 {
52dbd056
VZ
1307 // We're using pathOut to collect the long-name path, but using a
1308 // temporary for appending the last path component which may be
1309 // short-name
77fe02a8 1310 tmpPath = pathOut + dirs[i];
05e7001c 1311
52dbd056
VZ
1312 if ( tmpPath.empty() )
1313 continue;
1314
1315 if ( tmpPath.Last() == wxT(':') )
5d978d07
JS
1316 {
1317 // Can't pass a drive and root dir to FindFirstFile,
1318 // so continue to next dir
05e7001c 1319 tmpPath += wxFILE_SEP_PATH;
5d978d07
JS
1320 pathOut = tmpPath;
1321 continue;
1322 }
05e7001c
JS
1323
1324 hFind = ::FindFirstFile(tmpPath, &findFileData);
1325 if (hFind == INVALID_HANDLE_VALUE)
1326 {
1327 // Error: return immediately with the original path
1328 return path;
1329 }
05e7001c 1330
52dbd056
VZ
1331 pathOut += findFileData.cFileName;
1332 if ( (i < (count-1)) )
1333 pathOut += wxFILE_SEP_PATH;
1334
1335 ::FindClose(hFind);
05e7001c
JS
1336 }
1337 }
52dbd056
VZ
1338#else // !Win32
1339 pathOut = path;
1340#endif // Win32/!Win32
5716a1ab 1341
05e7001c 1342 return pathOut;
9e9b65c1
JS
1343}
1344
df5ddbca
RR
1345wxPathFormat wxFileName::GetFormat( wxPathFormat format )
1346{
1347 if (format == wxPATH_NATIVE)
1348 {
d9f54bb0 1349#if defined(__WXMSW__) || defined(__WXPM__) || defined(__DOS__)
df5ddbca 1350 format = wxPATH_DOS;
4d293f8e 1351#elif defined(__WXMAC__) && !defined(__DARWIN__)
04c943b1 1352 format = wxPATH_MAC;
3c621059 1353#elif defined(__VMS)
04c943b1 1354 format = wxPATH_VMS;
844f90fb 1355#else
df5ddbca
RR
1356 format = wxPATH_UNIX;
1357#endif
1358 }
1359 return format;
1360}
a35b27b1 1361
9e8d8607
VZ
1362// ----------------------------------------------------------------------------
1363// path splitting function
1364// ----------------------------------------------------------------------------
1365
6f91bc33 1366/* static */
04c943b1
VZ
1367void wxFileName::SplitPath(const wxString& fullpathWithVolume,
1368 wxString *pstrVolume,
9e8d8607
VZ
1369 wxString *pstrPath,
1370 wxString *pstrName,
1371 wxString *pstrExt,
1372 wxPathFormat format)
1373{
1374 format = GetFormat(format);
1375
04c943b1
VZ
1376 wxString fullpath = fullpathWithVolume;
1377
1378 // under VMS the end of the path is ']', not the path separator used to
1379 // separate the components
ab3edace 1380 wxString sepPath = format == wxPATH_VMS ? wxString(_T(']'))
04c943b1 1381 : GetPathSeparators(format);
9e8d8607 1382
04c943b1
VZ
1383 // special Windows UNC paths hack: transform \\share\path into share:path
1384 if ( format == wxPATH_DOS )
9e8d8607 1385 {
04c943b1
VZ
1386 if ( fullpath.length() >= 4 &&
1387 fullpath[0u] == wxFILE_SEP_PATH_DOS &&
1388 fullpath[1u] == wxFILE_SEP_PATH_DOS )
9e8d8607 1389 {
04c943b1
VZ
1390 fullpath.erase(0, 2);
1391
1392 size_t posFirstSlash = fullpath.find_first_of(sepPath);
1393 if ( posFirstSlash != wxString::npos )
1394 {
1395 fullpath[posFirstSlash] = wxFILE_SEP_DSK;
1396
1397 // UNC paths are always absolute, right? (FIXME)
1398 fullpath.insert(posFirstSlash + 1, wxFILE_SEP_PATH_DOS);
1399 }
3c621059
JJ
1400 }
1401 }
04c943b1 1402
a385b5df
RR
1403 // We separate the volume here
1404 if ( format == wxPATH_DOS || format == wxPATH_VMS )
04c943b1 1405 {
a385b5df 1406 wxString sepVol = GetVolumeSeparator(format);
24eb81cb 1407
04c943b1
VZ
1408 size_t posFirstColon = fullpath.find_first_of(sepVol);
1409 if ( posFirstColon != wxString::npos )
1410 {
1411 if ( pstrVolume )
1412 {
1413 *pstrVolume = fullpath.Left(posFirstColon);
1414 }
1415
1416 // remove the volume name and the separator from the full path
1417 fullpath.erase(0, posFirstColon + sepVol.length());
1418 }
1419 }
1420
1421 // find the positions of the last dot and last path separator in the path
1422 size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT);
1423 size_t posLastSlash = fullpath.find_last_of(sepPath);
1424
1425 if ( (posLastDot != wxString::npos) &&
1426 ((format == wxPATH_UNIX) || (format == wxPATH_VMS)) )
3c621059
JJ
1427 {
1428 if ( (posLastDot == 0) ||
04c943b1 1429 (fullpath[posLastDot - 1] == sepPath[0u] ) )
3c621059 1430 {
04c943b1
VZ
1431 // under Unix and VMS, dot may be (and commonly is) the first
1432 // character of the filename, don't treat the entire filename as
1433 // extension in this case
9e8d8607
VZ
1434 posLastDot = wxString::npos;
1435 }
1436 }
1437
8e7dda21
VZ
1438 // if we do have a dot and a slash, check that the dot is in the name part
1439 if ( (posLastDot != wxString::npos) &&
1440 (posLastSlash != wxString::npos) &&
1441 (posLastDot < posLastSlash) )
9e8d8607
VZ
1442 {
1443 // the dot is part of the path, not the start of the extension
1444 posLastDot = wxString::npos;
1445 }
1446
1447 // now fill in the variables provided by user
1448 if ( pstrPath )
1449 {
1450 if ( posLastSlash == wxString::npos )
1451 {
1452 // no path at all
1453 pstrPath->Empty();
1454 }
1455 else
1456 {
04c943b1 1457 // take everything up to the path separator but take care to make
353f41cb 1458 // the path equal to something like '/', not empty, for the files
04c943b1
VZ
1459 // immediately under root directory
1460 size_t len = posLastSlash;
1461 if ( !len )
1462 len++;
1463
1464 *pstrPath = fullpath.Left(len);
1465
1466 // special VMS hack: remove the initial bracket
1467 if ( format == wxPATH_VMS )
1468 {
1469 if ( (*pstrPath)[0u] == _T('[') )
1470 pstrPath->erase(0, 1);
1471 }
9e8d8607
VZ
1472 }
1473 }
1474
1475 if ( pstrName )
1476 {
42b1f941
VZ
1477 // take all characters starting from the one after the last slash and
1478 // up to, but excluding, the last dot
9e8d8607 1479 size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
8e7dda21
VZ
1480 size_t count;
1481 if ( posLastDot == wxString::npos )
1482 {
1483 // take all until the end
1484 count = wxString::npos;
1485 }
1486 else if ( posLastSlash == wxString::npos )
1487 {
1488 count = posLastDot;
1489 }
1490 else // have both dot and slash
1491 {
1492 count = posLastDot - posLastSlash - 1;
1493 }
9e8d8607
VZ
1494
1495 *pstrName = fullpath.Mid(nStart, count);
1496 }
1497
1498 if ( pstrExt )
1499 {
1500 if ( posLastDot == wxString::npos )
1501 {
1502 // no extension
1503 pstrExt->Empty();
1504 }
1505 else
1506 {
1507 // take everything after the dot
1508 *pstrExt = fullpath.Mid(posLastDot + 1);
1509 }
1510 }
1511}
951cd180 1512
6f91bc33
VZ
1513/* static */
1514void wxFileName::SplitPath(const wxString& fullpath,
1515 wxString *path,
1516 wxString *name,
1517 wxString *ext,
1518 wxPathFormat format)
1519{
1520 wxString volume;
1521 SplitPath(fullpath, &volume, path, name, ext, format);
1522
1523 if ( path && !volume.empty() )
1524 {
1525 path->Prepend(volume + GetVolumeSeparator(format));
1526 }
1527}
1528
951cd180
VZ
1529// ----------------------------------------------------------------------------
1530// time functions
1531// ----------------------------------------------------------------------------
1532
1533bool wxFileName::SetTimes(const wxDateTime *dtCreate,
1534 const wxDateTime *dtAccess,
1535 const wxDateTime *dtMod)
1536{
d9f54bb0 1537#if defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
246c704f
VZ
1538 if ( !dtAccess && !dtMod )
1539 {
1540 // can't modify the creation time anyhow, don't try
1541 return TRUE;
1542 }
1543
1544 // if dtAccess or dtMod is not specified, use the other one (which must be
1545 // non NULL because of the test above) for both times
951cd180 1546 utimbuf utm;
246c704f
VZ
1547 utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks();
1548 utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks();
951cd180
VZ
1549 if ( utime(GetFullPath(), &utm) == 0 )
1550 {
1551 return TRUE;
1552 }
1553#elif defined(__WIN32__)
1554 wxFileHandle fh(GetFullPath());
1555 if ( fh.IsOk() )
1556 {
1557 FILETIME ftAccess, ftCreate, ftWrite;
1558
1559 if ( dtCreate )
1560 ConvertWxToFileTime(&ftCreate, *dtCreate);
1561 if ( dtAccess )
1562 ConvertWxToFileTime(&ftAccess, *dtAccess);
1563 if ( dtMod )
1564 ConvertWxToFileTime(&ftWrite, *dtMod);
1565
1566 if ( ::SetFileTime(fh,
1567 dtCreate ? &ftCreate : NULL,
1568 dtAccess ? &ftAccess : NULL,
1569 dtMod ? &ftWrite : NULL) )
1570 {
1571 return TRUE;
1572 }
1573 }
1574#else // other platform
1575#endif // platforms
1576
1577 wxLogSysError(_("Failed to modify file times for '%s'"),
1578 GetFullPath().c_str());
1579
1580 return FALSE;
1581}
1582
1583bool wxFileName::Touch()
1584{
1585#if defined(__UNIX_LIKE__)
1586 // under Unix touching file is simple: just pass NULL to utime()
1587 if ( utime(GetFullPath(), NULL) == 0 )
1588 {
1589 return TRUE;
1590 }
1591
1592 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1593
1594 return FALSE;
1595#else // other platform
1596 wxDateTime dtNow = wxDateTime::Now();
1597
1598 return SetTimes(NULL /* don't change create time */, &dtNow, &dtNow);
1599#endif // platforms
1600}
1601
1602bool wxFileName::GetTimes(wxDateTime *dtAccess,
1603 wxDateTime *dtMod,
1604 wxDateTime *dtChange) const
1605{
d9f54bb0 1606#if defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
951cd180
VZ
1607 wxStructStat stBuf;
1608 if ( wxStat(GetFullPath(), &stBuf) == 0 )
1609 {
1610 if ( dtAccess )
1611 dtAccess->Set(stBuf.st_atime);
1612 if ( dtMod )
1613 dtMod->Set(stBuf.st_mtime);
1614 if ( dtChange )
1615 dtChange->Set(stBuf.st_ctime);
1616
1617 return TRUE;
1618 }
1619#elif defined(__WIN32__)
1620 wxFileHandle fh(GetFullPath());
1621 if ( fh.IsOk() )
1622 {
1623 FILETIME ftAccess, ftCreate, ftWrite;
1624
1625 if ( ::GetFileTime(fh,
1626 dtMod ? &ftCreate : NULL,
1627 dtAccess ? &ftAccess : NULL,
1628 dtChange ? &ftWrite : NULL) )
1629 {
1630 if ( dtMod )
1631 ConvertFileTimeToWx(dtMod, ftCreate);
1632 if ( dtAccess )
1633 ConvertFileTimeToWx(dtAccess, ftAccess);
1634 if ( dtChange )
1635 ConvertFileTimeToWx(dtChange, ftWrite);
1636
1637 return TRUE;
1638 }
1639 }
1640#else // other platform
1641#endif // platforms
1642
1643 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1644 GetFullPath().c_str());
1645
1646 return FALSE;
1647}
1648