]> git.saurik.com Git - wxWidgets.git/blame - src/common/filename.cpp
Smooth Aqua buttons in wxTreeCtrl.
[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
ade35f11 459void wxFileName::AssignTempFileName( const wxString& prefix )
df5ddbca 460{
ade35f11
VZ
461 wxString tempname = CreateTempFileName(prefix);
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 */
474wxString wxFileName::CreateTempFileName(const wxString& prefix)
475{
476 wxString path, dir, name;
477
478 // use the directory specified by the prefix
479 SplitPath(prefix, &dir, &name, NULL /* extension */);
480
481#if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
482
483#ifdef __WIN32__
484 if ( dir.empty() )
485 {
486 if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) )
487 {
488 wxLogLastError(_T("GetTempPath"));
489 }
490
491 if ( dir.empty() )
492 {
493 // GetTempFileName() fails if we pass it an empty string
494 dir = _T('.');
495 }
496 }
497
498 if ( !::GetTempFileName(dir, name, 0, wxStringBuffer(path, MAX_PATH + 1)) )
499 {
500 wxLogLastError(_T("GetTempFileName"));
501
502 path.clear();
503 }
504#else // Win16
505 if ( !::GetTempFileName(NULL, prefix, 0, wxStringBuffer(path, 1025)) )
506 {
507 path.clear();
508 }
509#endif // Win32/16
510
511#elif defined(__WXPM__)
512 // for now just create a file
513 //
514 // future enhancements can be to set some extended attributes for file
515 // systems OS/2 supports that have them (HPFS, FAT32) and security
516 // (HPFS386)
517 static const wxChar *szMktempSuffix = wxT("XXX");
518 path << dir << _T('/') << name << szMktempSuffix;
519
520 // Temporarily remove - MN
521 #ifndef __WATCOMC__
24eb81cb 522 ::DosCreateDir(wxStringBuffer(path, MAX_PATH), NULL);
ade35f11 523 #endif
7070f55b
VS
524
525#else // !Windows, !OS/2
ade35f11
VZ
526 if ( dir.empty() )
527 {
528 dir = wxGetenv(_T("TMP"));
529 if ( path.empty() )
530 {
531 dir = wxGetenv(_T("TEMP"));
532 }
533
534 if ( dir.empty() )
535 {
536 // default
d9f54bb0
VS
537 #ifdef __DOS__
538 dir = _T(".");
539 #else
ade35f11 540 dir = _T("/tmp");
d9f54bb0 541 #endif
ade35f11 542 }
844f90fb 543 }
ade35f11
VZ
544
545 path = dir;
546
547 if ( !wxEndsWithPathSeparator(dir) &&
548 (name.empty() || !wxIsPathSeparator(name[0u])) )
549 {
d9f54bb0 550 path += wxFILE_SEP_PATH;
ade35f11
VZ
551 }
552
553 path += name;
554
7070f55b 555#if defined(HAVE_MKSTEMP)
ade35f11
VZ
556 // scratch space for mkstemp()
557 path += _T("XXXXXX");
558
559 // can use the cast here because the length doesn't change and the string
560 // is not shared
561 if ( mkstemp((char *)path.mb_str()) == -1 )
562 {
563 // this might be not necessary as mkstemp() on most systems should have
564 // already done it but it doesn't hurt neither...
565 path.clear();
566 }
567 //else: file already created
568#else // !HAVE_MKSTEMP
569
570#ifdef HAVE_MKTEMP
571 // same as above
572 path += _T("XXXXXX");
573
574 if ( !mktemp((char *)path.mb_str()) )
575 {
576 path.clear();
577 }
7070f55b 578#else // !HAVE_MKTEMP (includes __DOS__)
ade35f11 579 // generate the unique file name ourselves
7070f55b 580 #ifndef __DOS__
ade35f11 581 path << (unsigned int)getpid();
7070f55b 582 #endif
ade35f11
VZ
583
584 wxString pathTry;
585
586 static const size_t numTries = 1000;
587 for ( size_t n = 0; n < numTries; n++ )
588 {
589 // 3 hex digits is enough for numTries == 1000 < 4096
590 pathTry = path + wxString::Format(_T("%.03x"), n);
591 if ( !wxFile::Exists(pathTry) )
592 {
593 break;
594 }
595
596 pathTry.clear();
597 }
598
599 path = pathTry;
600#endif // HAVE_MKTEMP/!HAVE_MKTEMP
601
602 if ( !path.empty() )
603 {
604 // create the file - of course, there is a race condition here, this is
605 // why we always prefer using mkstemp()...
606 wxFile file;
607 if ( !file.Open(path, wxFile::write_excl, wxS_IRUSR | wxS_IWUSR) )
608 {
609 // FIXME: If !ok here should we loop and try again with another
610 // file name? That is the standard recourse if open(O_EXCL)
611 // fails, though of course it should be protected against
612 // possible infinite looping too.
613
614 wxLogError(_("Failed to open temporary file."));
615
616 path.clear();
617 }
618 }
619#endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
620
621#endif // Windows/!Windows
622
623 if ( path.empty() )
624 {
625 wxLogSysError(_("Failed to create a temporary file name"));
626 }
627
628 return path;
df5ddbca
RR
629}
630
844f90fb
VZ
631// ----------------------------------------------------------------------------
632// directory operations
633// ----------------------------------------------------------------------------
634
f0ce3409 635bool wxFileName::Mkdir( int perm, bool full )
a35b27b1 636{
f0ce3409 637 return wxFileName::Mkdir( GetFullPath(), perm, full );
a35b27b1
RR
638}
639
f0ce3409 640bool wxFileName::Mkdir( const wxString &dir, int perm, bool full )
df5ddbca 641{
f0ce3409
JS
642 if (full)
643 {
644 wxFileName filename(dir);
645 wxArrayString dirs = filename.GetDirs();
77fe02a8 646 dirs.Add(filename.GetName());
f0ce3409
JS
647
648 size_t count = dirs.GetCount();
649 size_t i;
650 wxString currPath;
651 int noErrors = 0;
652 for ( i = 0; i < count; i++ )
653 {
654 currPath += dirs[i];
655
656 if (currPath.Last() == wxT(':'))
657 {
658 // Can't create a root directory so continue to next dir
659 currPath += wxFILE_SEP_PATH;
660 continue;
661 }
662
663 if (!DirExists(currPath))
664 if (!wxMkdir(currPath, perm))
665 noErrors ++;
666
667 if ( (i < (count-1)) )
668 currPath += wxFILE_SEP_PATH;
669 }
670
671 return (noErrors == 0);
672
673 }
674 else
675 return ::wxMkdir( dir, perm );
df5ddbca
RR
676}
677
a35b27b1 678bool wxFileName::Rmdir()
df5ddbca 679{
a35b27b1 680 return wxFileName::Rmdir( GetFullPath() );
df5ddbca
RR
681}
682
a35b27b1 683bool wxFileName::Rmdir( const wxString &dir )
df5ddbca 684{
a35b27b1 685 return ::wxRmdir( dir );
df5ddbca
RR
686}
687
844f90fb
VZ
688// ----------------------------------------------------------------------------
689// path normalization
690// ----------------------------------------------------------------------------
691
692bool wxFileName::Normalize(wxPathNormalize flags,
693 const wxString& cwd,
694 wxPathFormat format)
a35b27b1 695{
844f90fb
VZ
696 // the existing path components
697 wxArrayString dirs = GetDirs();
698
699 // the path to prepend in front to make the path absolute
700 wxFileName curDir;
701
702 format = GetFormat(format);
ae4d7004 703
844f90fb 704 // make the path absolute
353f41cb 705 if ( (flags & wxPATH_NORM_ABSOLUTE) && m_relative )
a35b27b1 706 {
844f90fb 707 if ( cwd.empty() )
6f91bc33
VZ
708 {
709 curDir.AssignCwd(GetVolume());
710 }
711 else // cwd provided
712 {
844f90fb 713 curDir.AssignDir(cwd);
6f91bc33 714 }
52dbd056 715
353f41cb 716#if 0
52dbd056
VZ
717 // the path may be not absolute because it doesn't have the volume name
718 // but in this case we shouldn't modify the directory components of it
719 // but just set the current volume
720 if ( !HasVolume() && curDir.HasVolume() )
721 {
722 SetVolume(curDir.GetVolume());
723
724 if ( IsAbsolute() )
725 {
726 // yes, it was the case - we don't need curDir then
727 curDir.Clear();
728 }
729 }
353f41cb 730#endif
5fc67e5c 731 m_relative = FALSE;
a35b27b1 732 }
ae4d7004 733
844f90fb
VZ
734 // handle ~ stuff under Unix only
735 if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
a35b27b1 736 {
844f90fb
VZ
737 if ( !dirs.IsEmpty() )
738 {
739 wxString dir = dirs[0u];
740 if ( !dir.empty() && dir[0u] == _T('~') )
741 {
742 curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
743
da2fd5ac 744 dirs.RemoveAt(0u);
844f90fb
VZ
745 }
746 }
a35b27b1 747 }
844f90fb 748
52dbd056 749 // transform relative path into abs one
844f90fb 750 if ( curDir.IsOk() )
a35b27b1 751 {
844f90fb
VZ
752 wxArrayString dirsNew = curDir.GetDirs();
753 size_t count = dirs.GetCount();
754 for ( size_t n = 0; n < count; n++ )
755 {
756 dirsNew.Add(dirs[n]);
757 }
758
759 dirs = dirsNew;
a35b27b1 760 }
844f90fb
VZ
761
762 // now deal with ".", ".." and the rest
763 m_dirs.Empty();
764 size_t count = dirs.GetCount();
765 for ( size_t n = 0; n < count; n++ )
a35b27b1 766 {
844f90fb
VZ
767 wxString dir = dirs[n];
768
2bc60417 769 if ( flags & wxPATH_NORM_DOTS )
844f90fb
VZ
770 {
771 if ( dir == wxT(".") )
772 {
773 // just ignore
774 continue;
775 }
776
777 if ( dir == wxT("..") )
778 {
779 if ( m_dirs.IsEmpty() )
780 {
781 wxLogError(_("The path '%s' contains too many \"..\"!"),
782 GetFullPath().c_str());
783 return FALSE;
784 }
785
ae4d7004 786 m_dirs.RemoveAt(m_dirs.GetCount() - 1);
844f90fb
VZ
787 continue;
788 }
789 }
790
791 if ( flags & wxPATH_NORM_ENV_VARS )
a35b27b1 792 {
844f90fb 793 dir = wxExpandEnvVars(dir);
a35b27b1 794 }
844f90fb
VZ
795
796 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
797 {
798 dir.MakeLower();
799 }
800
801 m_dirs.Add(dir);
a35b27b1 802 }
844f90fb
VZ
803
804 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
805 {
806 // VZ: expand env vars here too?
807
808 m_name.MakeLower();
809 m_ext.MakeLower();
810 }
811
52dbd056
VZ
812#if defined(__WIN32__)
813 if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) )
9e9b65c1
JS
814 {
815 Assign(GetLongPath());
816 }
52dbd056 817#endif // Win32
9e9b65c1 818
a35b27b1
RR
819 return TRUE;
820}
821
f7d886af
VZ
822bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format)
823{
824 wxFileName fnBase(pathBase, format);
825
826 // get cwd only once - small time saving
827 wxString cwd = wxGetCwd();
828 Normalize(wxPATH_NORM_ALL, cwd, format);
829 fnBase.Normalize(wxPATH_NORM_ALL, cwd, format);
830
831 bool withCase = IsCaseSensitive(format);
832
833 // we can't do anything if the files live on different volumes
834 if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) )
835 {
836 // nothing done
837 return FALSE;
838 }
839
840 // same drive, so we don't need our volume
841 m_volume.clear();
842
843 // remove common directories starting at the top
844 while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() &&
845 m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) )
846 {
ae4d7004
VZ
847 m_dirs.RemoveAt(0);
848 fnBase.m_dirs.RemoveAt(0);
f7d886af
VZ
849 }
850
851 // add as many ".." as needed
852 size_t count = fnBase.m_dirs.GetCount();
853 for ( size_t i = 0; i < count; i++ )
854 {
855 m_dirs.Insert(wxT(".."), 0u);
856 }
353f41cb
RR
857
858 m_relative = TRUE;
f7d886af
VZ
859
860 // we were modified
861 return TRUE;
862}
863
844f90fb
VZ
864// ----------------------------------------------------------------------------
865// filename kind tests
866// ----------------------------------------------------------------------------
867
f7d886af 868bool wxFileName::SameAs(const wxFileName &filepath, wxPathFormat format)
df5ddbca 869{
844f90fb
VZ
870 wxFileName fn1 = *this,
871 fn2 = filepath;
872
873 // get cwd only once - small time saving
874 wxString cwd = wxGetCwd();
875 fn1.Normalize(wxPATH_NORM_ALL, cwd, format);
876 fn2.Normalize(wxPATH_NORM_ALL, cwd, format);
877
878 if ( fn1.GetFullPath() == fn2.GetFullPath() )
879 return TRUE;
880
881 // TODO: compare inodes for Unix, this works even when filenames are
882 // different but files are the same (symlinks) (VZ)
883
884 return FALSE;
df5ddbca
RR
885}
886
844f90fb 887/* static */
df5ddbca
RR
888bool wxFileName::IsCaseSensitive( wxPathFormat format )
889{
04c943b1
VZ
890 // only Unix filenames are truely case-sensitive
891 return GetFormat(format) == wxPATH_UNIX;
df5ddbca
RR
892}
893
04c943b1
VZ
894/* static */
895wxString wxFileName::GetVolumeSeparator(wxPathFormat format)
844f90fb 896{
04c943b1
VZ
897 wxString sepVol;
898
a385b5df
RR
899 if ( (GetFormat(format) == wxPATH_DOS) ||
900 (GetFormat(format) == wxPATH_VMS) )
04c943b1 901 {
04c943b1
VZ
902 sepVol = wxFILE_SEP_DSK;
903 }
a385b5df 904 //else: leave empty
04c943b1
VZ
905
906 return sepVol;
844f90fb
VZ
907}
908
909/* static */
910wxString wxFileName::GetPathSeparators(wxPathFormat format)
911{
912 wxString seps;
913 switch ( GetFormat(format) )
df5ddbca 914 {
844f90fb 915 case wxPATH_DOS:
04c943b1
VZ
916 // accept both as native APIs do but put the native one first as
917 // this is the one we use in GetFullPath()
918 seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX;
844f90fb
VZ
919 break;
920
921 default:
922 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
923 // fall through
924
925 case wxPATH_UNIX:
9e8d8607 926 seps = wxFILE_SEP_PATH_UNIX;
844f90fb
VZ
927 break;
928
929 case wxPATH_MAC:
9e8d8607 930 seps = wxFILE_SEP_PATH_MAC;
844f90fb 931 break;
04c943b1 932
3c621059
JJ
933 case wxPATH_VMS:
934 seps = wxFILE_SEP_PATH_VMS;
935 break;
df5ddbca
RR
936 }
937
844f90fb 938 return seps;
df5ddbca
RR
939}
940
844f90fb
VZ
941/* static */
942bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format)
df5ddbca 943{
04c943b1
VZ
944 // wxString::Find() doesn't work as expected with NUL - it will always find
945 // it, so it is almost surely a bug if this function is called with NUL arg
946 wxASSERT_MSG( ch != _T('\0'), _T("shouldn't be called with NUL") );
947
844f90fb 948 return GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
df5ddbca
RR
949}
950
951bool wxFileName::IsWild( wxPathFormat format )
952{
844f90fb
VZ
953 // FIXME: this is probably false for Mac and this is surely wrong for most
954 // of Unix shells (think about "[...]")
04c943b1 955 (void)format;
844f90fb 956 return m_name.find_first_of(_T("*?")) != wxString::npos;
df5ddbca
RR
957}
958
844f90fb
VZ
959// ----------------------------------------------------------------------------
960// path components manipulation
961// ----------------------------------------------------------------------------
962
df5ddbca
RR
963void wxFileName::AppendDir( const wxString &dir )
964{
965 m_dirs.Add( dir );
966}
967
968void wxFileName::PrependDir( const wxString &dir )
969{
970 m_dirs.Insert( dir, 0 );
971}
972
973void wxFileName::InsertDir( int before, const wxString &dir )
974{
975 m_dirs.Insert( dir, before );
976}
977
978void wxFileName::RemoveDir( int pos )
979{
980 m_dirs.Remove( (size_t)pos );
981}
982
844f90fb
VZ
983// ----------------------------------------------------------------------------
984// accessors
985// ----------------------------------------------------------------------------
986
7124df9b
VZ
987void wxFileName::SetFullName(const wxString& fullname)
988{
989 SplitPath(fullname, NULL /* no path */, &m_name, &m_ext);
990}
991
844f90fb 992wxString wxFileName::GetFullName() const
a35b27b1 993{
844f90fb
VZ
994 wxString fullname = m_name;
995 if ( !m_ext.empty() )
a35b27b1 996 {
9e8d8607 997 fullname << wxFILE_SEP_EXT << m_ext;
a35b27b1 998 }
a35b27b1 999
844f90fb 1000 return fullname;
a35b27b1
RR
1001}
1002
1003wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const
df5ddbca
RR
1004{
1005 format = GetFormat( format );
844f90fb 1006
353f41cb
RR
1007 wxString fullpath;
1008
1009 // the leading character
1010 if ( format == wxPATH_MAC && m_relative )
1011 {
1012 fullpath += wxFILE_SEP_PATH_MAC;
1013 }
1014 else if ( format == wxPATH_DOS )
1015 {
1016 if (!m_relative)
1017 fullpath += wxFILE_SEP_PATH_DOS;
1018 }
1019 else if ( format == wxPATH_UNIX )
1020 {
1021 if (!m_relative)
1022 fullpath += wxFILE_SEP_PATH_UNIX;
1023 }
1024
1025 // then concatenate all the path components using the path separator
1026 size_t dirCount = m_dirs.GetCount();
1027 if ( dirCount )
df5ddbca 1028 {
353f41cb
RR
1029 if ( format == wxPATH_VMS )
1030 {
1031 fullpath += wxT('[');
1032 }
1033
1034
1035 for ( size_t i = 0; i < dirCount; i++ )
1036 {
1037 // TODO: What to do with ".." under VMS
1038
1039 switch (format)
1040 {
1041 case wxPATH_MAC:
1042 {
1043 if (m_dirs[i] == wxT("."))
1044 break;
1045 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1046 fullpath += m_dirs[i];
1047 fullpath += wxT(':');
1048 break;
1049 }
1050 case wxPATH_DOS:
1051 {
1052 fullpath += m_dirs[i];
1053 fullpath += wxT('\\');
1054 break;
1055 }
1056 case wxPATH_UNIX:
1057 {
1058 fullpath += m_dirs[i];
1059 fullpath += wxT('/');
1060 break;
1061 }
1062 case wxPATH_VMS:
1063 {
1064 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1065 fullpath += m_dirs[i];
1066 if (i == dirCount-1)
1067 fullpath += wxT(']');
1068 else
1069 fullpath += wxT('.');
1070 break;
1071 }
1072 default:
1073 {
7613582b 1074 wxFAIL_MSG( wxT("error") );
353f41cb
RR
1075 }
1076 }
1077 }
df5ddbca 1078 }
844f90fb 1079
353f41cb
RR
1080
1081
1082 return fullpath;
df5ddbca
RR
1083}
1084
1085wxString wxFileName::GetFullPath( wxPathFormat format ) const
1086{
04c943b1
VZ
1087 format = GetFormat(format);
1088
1089 wxString fullpath;
1090
1091 // first put the volume
1092 if ( !m_volume.empty() )
dcf0fce4 1093 {
5fc67e5c
RR
1094 {
1095 // Special Windows UNC paths hack, part 2: undo what we did in
1096 // SplitPath() and make an UNC path if we have a drive which is not a
1097 // single letter (hopefully the network shares can't be one letter only
1098 // although I didn't find any authoritative docs on this)
1099 if ( format == wxPATH_DOS && m_volume.length() > 1 )
1100 {
1101 fullpath << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << m_volume;
1102 }
1103 else if ( format == wxPATH_DOS || format == wxPATH_VMS )
1104 {
1105 fullpath << m_volume << GetVolumeSeparator(format);
a385b5df 1106 }
353f41cb 1107 // else ignore
dcf0fce4
RR
1108 }
1109 }
353f41cb
RR
1110
1111 // the leading character
1112 if ( format == wxPATH_MAC && m_relative )
1113 {
1114 fullpath += wxFILE_SEP_PATH_MAC;
1115 }
1116 else if ( format == wxPATH_DOS )
1117 {
1118 if (!m_relative)
1119 fullpath += wxFILE_SEP_PATH_DOS;
1120 }
1121 else if ( format == wxPATH_UNIX )
1122 {
1123 if (!m_relative)
1124 fullpath += wxFILE_SEP_PATH_UNIX;
1125 }
1126
04c943b1
VZ
1127 // then concatenate all the path components using the path separator
1128 size_t dirCount = m_dirs.GetCount();
1129 if ( dirCount )
3c621059 1130 {
04c943b1 1131 if ( format == wxPATH_VMS )
dcf0fce4 1132 {
353f41cb 1133 fullpath += wxT('[');
04c943b1
VZ
1134 }
1135
353f41cb 1136
04c943b1
VZ
1137 for ( size_t i = 0; i < dirCount; i++ )
1138 {
353f41cb 1139 // TODO: What to do with ".." under VMS
04c943b1 1140
353f41cb
RR
1141 switch (format)
1142 {
1143 case wxPATH_MAC:
1144 {
1145 if (m_dirs[i] == wxT("."))
1146 break;
1147 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1148 fullpath += m_dirs[i];
1149 fullpath += wxT(':');
1150 break;
1151 }
1152 case wxPATH_DOS:
1153 {
1154 fullpath += m_dirs[i];
1155 fullpath += wxT('\\');
1156 break;
1157 }
1158 case wxPATH_UNIX:
1159 {
1160 fullpath += m_dirs[i];
1161 fullpath += wxT('/');
1162 break;
1163 }
1164 case wxPATH_VMS:
1165 {
1166 if (m_dirs[i] != wxT("..")) // convert back from ".." to nothing
1167 fullpath += m_dirs[i];
1168 if (i == dirCount-1)
1169 fullpath += wxT(']');
1170 else
1171 fullpath += wxT('.');
1172 break;
1173 }
1174 default:
1175 {
7613582b 1176 wxFAIL_MSG( wxT("error") );
353f41cb
RR
1177 }
1178 }
dcf0fce4
RR
1179 }
1180 }
04c943b1
VZ
1181
1182 // finally add the file name and extension
1183 fullpath += GetFullName();
1184
1185 return fullpath;
df5ddbca
RR
1186}
1187
9e9b65c1
JS
1188// Return the short form of the path (returns identity on non-Windows platforms)
1189wxString wxFileName::GetShortPath() const
1190{
8cb172b4 1191#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
9e9b65c1 1192 wxString path(GetFullPath());
75ef5722
JS
1193 wxString pathOut;
1194 DWORD sz = ::GetShortPathName(path, NULL, 0);
1195 bool ok = sz != 0;
1196 if ( ok )
9e9b65c1 1197 {
75ef5722
JS
1198 ok = ::GetShortPathName
1199 (
1200 path,
1201 pathOut.GetWriteBuf(sz),
1202 sz
1203 ) != 0;
1204 pathOut.UngetWriteBuf();
9e9b65c1 1205 }
75ef5722
JS
1206 if (ok)
1207 return pathOut;
5716a1ab
VZ
1208
1209 return path;
9e9b65c1
JS
1210#else
1211 return GetFullPath();
1212#endif
1213}
1214
1215// Return the long form of the path (returns identity on non-Windows platforms)
1216wxString wxFileName::GetLongPath() const
1217{
52dbd056
VZ
1218 wxString pathOut,
1219 path = GetFullPath();
1220
1221#if defined(__WIN32__) && !defined(__WXMICROWIN__)
05e7001c
JS
1222 bool success = FALSE;
1223
5716a1ab
VZ
1224 // VZ: this code was disabled, why?
1225#if 0 // wxUSE_DYNLIB_CLASS
05e7001c
JS
1226 typedef DWORD (*GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
1227
1228 static bool s_triedToLoad = FALSE;
05e7001c
JS
1229
1230 if ( !s_triedToLoad )
9e9b65c1 1231 {
05e7001c 1232 s_triedToLoad = TRUE;
05e7001c 1233 wxDllType dllKernel = wxDllLoader::LoadLibrary(_T("kernel32"));
33ac7e6f 1234 if ( dllKernel )
05e7001c
JS
1235 {
1236 // may succeed or fail depending on the Windows version
04c943b1 1237 static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
5d978d07
JS
1238#ifdef _UNICODE
1239 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameW"));
1240#else
1241 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) wxDllLoader::GetSymbol(dllKernel, _T("GetLongPathNameA"));
1242#endif
05e7001c
JS
1243
1244 wxDllLoader::UnloadLibrary(dllKernel);
1245
1246 if ( s_pfnGetLongPathName )
1247 {
1248 DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0);
1249 bool ok = dwSize > 0;
1250
1251 if ( ok )
1252 {
1253 DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0);
1254 ok = sz != 0;
1255 if ( ok )
1256 {
1257 ok = (*s_pfnGetLongPathName)
1258 (
1259 path,
1260 pathOut.GetWriteBuf(sz),
1261 sz
1262 ) != 0;
1263 pathOut.UngetWriteBuf();
1264
1265 success = TRUE;
1266 }
1267 }
1268 }
1269 }
9e9b65c1 1270 }
05e7001c 1271 if (success)
75ef5722 1272 return pathOut;
5716a1ab 1273#endif // wxUSE_DYNLIB_CLASS
05e7001c
JS
1274
1275 if (!success)
1276 {
1277 // The OS didn't support GetLongPathName, or some other error.
1278 // We need to call FindFirstFile on each component in turn.
1279
1280 WIN32_FIND_DATA findFileData;
1281 HANDLE hFind;
1282 pathOut = wxEmptyString;
1283
77fe02a8 1284 wxArrayString dirs = GetDirs();
bcbe86e5 1285 dirs.Add(GetFullName());
77fe02a8 1286
05e7001c 1287 wxString tmpPath;
5d978d07 1288
52dbd056
VZ
1289 size_t count = dirs.GetCount();
1290 for ( size_t i = 0; i < count; i++ )
05e7001c 1291 {
52dbd056
VZ
1292 // We're using pathOut to collect the long-name path, but using a
1293 // temporary for appending the last path component which may be
1294 // short-name
77fe02a8 1295 tmpPath = pathOut + dirs[i];
05e7001c 1296
52dbd056
VZ
1297 if ( tmpPath.empty() )
1298 continue;
1299
1300 if ( tmpPath.Last() == wxT(':') )
5d978d07
JS
1301 {
1302 // Can't pass a drive and root dir to FindFirstFile,
1303 // so continue to next dir
05e7001c 1304 tmpPath += wxFILE_SEP_PATH;
5d978d07
JS
1305 pathOut = tmpPath;
1306 continue;
1307 }
05e7001c
JS
1308
1309 hFind = ::FindFirstFile(tmpPath, &findFileData);
1310 if (hFind == INVALID_HANDLE_VALUE)
1311 {
1312 // Error: return immediately with the original path
1313 return path;
1314 }
05e7001c 1315
52dbd056
VZ
1316 pathOut += findFileData.cFileName;
1317 if ( (i < (count-1)) )
1318 pathOut += wxFILE_SEP_PATH;
1319
1320 ::FindClose(hFind);
05e7001c
JS
1321 }
1322 }
52dbd056
VZ
1323#else // !Win32
1324 pathOut = path;
1325#endif // Win32/!Win32
5716a1ab 1326
05e7001c 1327 return pathOut;
9e9b65c1
JS
1328}
1329
df5ddbca
RR
1330wxPathFormat wxFileName::GetFormat( wxPathFormat format )
1331{
1332 if (format == wxPATH_NATIVE)
1333 {
d9f54bb0 1334#if defined(__WXMSW__) || defined(__WXPM__) || defined(__DOS__)
df5ddbca 1335 format = wxPATH_DOS;
4d293f8e 1336#elif defined(__WXMAC__) && !defined(__DARWIN__)
04c943b1 1337 format = wxPATH_MAC;
3c621059 1338#elif defined(__VMS)
04c943b1 1339 format = wxPATH_VMS;
844f90fb 1340#else
df5ddbca
RR
1341 format = wxPATH_UNIX;
1342#endif
1343 }
1344 return format;
1345}
a35b27b1 1346
9e8d8607
VZ
1347// ----------------------------------------------------------------------------
1348// path splitting function
1349// ----------------------------------------------------------------------------
1350
6f91bc33 1351/* static */
04c943b1
VZ
1352void wxFileName::SplitPath(const wxString& fullpathWithVolume,
1353 wxString *pstrVolume,
9e8d8607
VZ
1354 wxString *pstrPath,
1355 wxString *pstrName,
1356 wxString *pstrExt,
1357 wxPathFormat format)
1358{
1359 format = GetFormat(format);
1360
04c943b1
VZ
1361 wxString fullpath = fullpathWithVolume;
1362
1363 // under VMS the end of the path is ']', not the path separator used to
1364 // separate the components
ab3edace 1365 wxString sepPath = format == wxPATH_VMS ? wxString(_T(']'))
04c943b1 1366 : GetPathSeparators(format);
9e8d8607 1367
04c943b1
VZ
1368 // special Windows UNC paths hack: transform \\share\path into share:path
1369 if ( format == wxPATH_DOS )
9e8d8607 1370 {
04c943b1
VZ
1371 if ( fullpath.length() >= 4 &&
1372 fullpath[0u] == wxFILE_SEP_PATH_DOS &&
1373 fullpath[1u] == wxFILE_SEP_PATH_DOS )
9e8d8607 1374 {
04c943b1
VZ
1375 fullpath.erase(0, 2);
1376
1377 size_t posFirstSlash = fullpath.find_first_of(sepPath);
1378 if ( posFirstSlash != wxString::npos )
1379 {
1380 fullpath[posFirstSlash] = wxFILE_SEP_DSK;
1381
1382 // UNC paths are always absolute, right? (FIXME)
1383 fullpath.insert(posFirstSlash + 1, wxFILE_SEP_PATH_DOS);
1384 }
3c621059
JJ
1385 }
1386 }
04c943b1 1387
a385b5df
RR
1388 // We separate the volume here
1389 if ( format == wxPATH_DOS || format == wxPATH_VMS )
04c943b1 1390 {
a385b5df 1391 wxString sepVol = GetVolumeSeparator(format);
24eb81cb 1392
04c943b1
VZ
1393 size_t posFirstColon = fullpath.find_first_of(sepVol);
1394 if ( posFirstColon != wxString::npos )
1395 {
1396 if ( pstrVolume )
1397 {
1398 *pstrVolume = fullpath.Left(posFirstColon);
1399 }
1400
1401 // remove the volume name and the separator from the full path
1402 fullpath.erase(0, posFirstColon + sepVol.length());
1403 }
1404 }
1405
1406 // find the positions of the last dot and last path separator in the path
1407 size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT);
1408 size_t posLastSlash = fullpath.find_last_of(sepPath);
1409
1410 if ( (posLastDot != wxString::npos) &&
1411 ((format == wxPATH_UNIX) || (format == wxPATH_VMS)) )
3c621059
JJ
1412 {
1413 if ( (posLastDot == 0) ||
04c943b1 1414 (fullpath[posLastDot - 1] == sepPath[0u] ) )
3c621059 1415 {
04c943b1
VZ
1416 // under Unix and VMS, dot may be (and commonly is) the first
1417 // character of the filename, don't treat the entire filename as
1418 // extension in this case
9e8d8607
VZ
1419 posLastDot = wxString::npos;
1420 }
1421 }
1422
8e7dda21
VZ
1423 // if we do have a dot and a slash, check that the dot is in the name part
1424 if ( (posLastDot != wxString::npos) &&
1425 (posLastSlash != wxString::npos) &&
1426 (posLastDot < posLastSlash) )
9e8d8607
VZ
1427 {
1428 // the dot is part of the path, not the start of the extension
1429 posLastDot = wxString::npos;
1430 }
1431
1432 // now fill in the variables provided by user
1433 if ( pstrPath )
1434 {
1435 if ( posLastSlash == wxString::npos )
1436 {
1437 // no path at all
1438 pstrPath->Empty();
1439 }
1440 else
1441 {
04c943b1 1442 // take everything up to the path separator but take care to make
353f41cb 1443 // the path equal to something like '/', not empty, for the files
04c943b1
VZ
1444 // immediately under root directory
1445 size_t len = posLastSlash;
1446 if ( !len )
1447 len++;
1448
1449 *pstrPath = fullpath.Left(len);
1450
1451 // special VMS hack: remove the initial bracket
1452 if ( format == wxPATH_VMS )
1453 {
1454 if ( (*pstrPath)[0u] == _T('[') )
1455 pstrPath->erase(0, 1);
1456 }
9e8d8607
VZ
1457 }
1458 }
1459
1460 if ( pstrName )
1461 {
42b1f941
VZ
1462 // take all characters starting from the one after the last slash and
1463 // up to, but excluding, the last dot
9e8d8607 1464 size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
8e7dda21
VZ
1465 size_t count;
1466 if ( posLastDot == wxString::npos )
1467 {
1468 // take all until the end
1469 count = wxString::npos;
1470 }
1471 else if ( posLastSlash == wxString::npos )
1472 {
1473 count = posLastDot;
1474 }
1475 else // have both dot and slash
1476 {
1477 count = posLastDot - posLastSlash - 1;
1478 }
9e8d8607
VZ
1479
1480 *pstrName = fullpath.Mid(nStart, count);
1481 }
1482
1483 if ( pstrExt )
1484 {
1485 if ( posLastDot == wxString::npos )
1486 {
1487 // no extension
1488 pstrExt->Empty();
1489 }
1490 else
1491 {
1492 // take everything after the dot
1493 *pstrExt = fullpath.Mid(posLastDot + 1);
1494 }
1495 }
1496}
951cd180 1497
6f91bc33
VZ
1498/* static */
1499void wxFileName::SplitPath(const wxString& fullpath,
1500 wxString *path,
1501 wxString *name,
1502 wxString *ext,
1503 wxPathFormat format)
1504{
1505 wxString volume;
1506 SplitPath(fullpath, &volume, path, name, ext, format);
1507
1508 if ( path && !volume.empty() )
1509 {
1510 path->Prepend(volume + GetVolumeSeparator(format));
1511 }
1512}
1513
951cd180
VZ
1514// ----------------------------------------------------------------------------
1515// time functions
1516// ----------------------------------------------------------------------------
1517
1518bool wxFileName::SetTimes(const wxDateTime *dtCreate,
1519 const wxDateTime *dtAccess,
1520 const wxDateTime *dtMod)
1521{
d9f54bb0 1522#if defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
246c704f
VZ
1523 if ( !dtAccess && !dtMod )
1524 {
1525 // can't modify the creation time anyhow, don't try
1526 return TRUE;
1527 }
1528
1529 // if dtAccess or dtMod is not specified, use the other one (which must be
1530 // non NULL because of the test above) for both times
951cd180 1531 utimbuf utm;
246c704f
VZ
1532 utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks();
1533 utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks();
951cd180
VZ
1534 if ( utime(GetFullPath(), &utm) == 0 )
1535 {
1536 return TRUE;
1537 }
1538#elif defined(__WIN32__)
1539 wxFileHandle fh(GetFullPath());
1540 if ( fh.IsOk() )
1541 {
1542 FILETIME ftAccess, ftCreate, ftWrite;
1543
1544 if ( dtCreate )
1545 ConvertWxToFileTime(&ftCreate, *dtCreate);
1546 if ( dtAccess )
1547 ConvertWxToFileTime(&ftAccess, *dtAccess);
1548 if ( dtMod )
1549 ConvertWxToFileTime(&ftWrite, *dtMod);
1550
1551 if ( ::SetFileTime(fh,
1552 dtCreate ? &ftCreate : NULL,
1553 dtAccess ? &ftAccess : NULL,
1554 dtMod ? &ftWrite : NULL) )
1555 {
1556 return TRUE;
1557 }
1558 }
1559#else // other platform
1560#endif // platforms
1561
1562 wxLogSysError(_("Failed to modify file times for '%s'"),
1563 GetFullPath().c_str());
1564
1565 return FALSE;
1566}
1567
1568bool wxFileName::Touch()
1569{
1570#if defined(__UNIX_LIKE__)
1571 // under Unix touching file is simple: just pass NULL to utime()
1572 if ( utime(GetFullPath(), NULL) == 0 )
1573 {
1574 return TRUE;
1575 }
1576
1577 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1578
1579 return FALSE;
1580#else // other platform
1581 wxDateTime dtNow = wxDateTime::Now();
1582
1583 return SetTimes(NULL /* don't change create time */, &dtNow, &dtNow);
1584#endif // platforms
1585}
1586
1587bool wxFileName::GetTimes(wxDateTime *dtAccess,
1588 wxDateTime *dtMod,
1589 wxDateTime *dtChange) const
1590{
d9f54bb0 1591#if defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
951cd180
VZ
1592 wxStructStat stBuf;
1593 if ( wxStat(GetFullPath(), &stBuf) == 0 )
1594 {
1595 if ( dtAccess )
1596 dtAccess->Set(stBuf.st_atime);
1597 if ( dtMod )
1598 dtMod->Set(stBuf.st_mtime);
1599 if ( dtChange )
1600 dtChange->Set(stBuf.st_ctime);
1601
1602 return TRUE;
1603 }
1604#elif defined(__WIN32__)
1605 wxFileHandle fh(GetFullPath());
1606 if ( fh.IsOk() )
1607 {
1608 FILETIME ftAccess, ftCreate, ftWrite;
1609
1610 if ( ::GetFileTime(fh,
1611 dtMod ? &ftCreate : NULL,
1612 dtAccess ? &ftAccess : NULL,
1613 dtChange ? &ftWrite : NULL) )
1614 {
1615 if ( dtMod )
1616 ConvertFileTimeToWx(dtMod, ftCreate);
1617 if ( dtAccess )
1618 ConvertFileTimeToWx(dtAccess, ftAccess);
1619 if ( dtChange )
1620 ConvertFileTimeToWx(dtChange, ftWrite);
1621
1622 return TRUE;
1623 }
1624 }
1625#else // other platform
1626#endif // platforms
1627
1628 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1629 GetFullPath().c_str());
1630
1631 return FALSE;
1632}
1633