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