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