]> git.saurik.com Git - wxWidgets.git/blame - src/common/filename.cpp
wxMac uses wxStandardPathsCF to mean wxStandardPaths in its wxBase
[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
65571936 9// Licence: wxWindows licence
df5ddbca
RR
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.
ba623ba2 50 [ start of directory specification
04c943b1
VZ
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
14f355c2 63#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
35a6691a 85#include "wx/dynlib.h"
df5ddbca 86
9e9b65c1
JS
87// For GetShort/LongPathName
88#ifdef __WIN32__
9ed0d735 89#include "wx/msw/wrapwin.h"
59ba321b
JS
90#if defined(__MINGW32__)
91#include "wx/msw/gccpriv.h"
92#endif
951cd180
VZ
93#endif
94
1c193821
JS
95#ifdef __WXWINCE__
96#include "wx/msw/private.h"
97#endif
98
76a5e5d2
SC
99#if defined(__WXMAC__)
100 #include "wx/mac/private.h" // includes mac headers
101#endif
102
951cd180
VZ
103// utime() is POSIX so should normally be available on all Unices
104#ifdef __UNIX_LIKE__
0b9ab0bd
RL
105#include <sys/types.h>
106#include <utime.h>
107#include <sys/stat.h>
108#include <unistd.h>
9e9b65c1
JS
109#endif
110
444d61ba
VS
111#ifdef __DJGPP__
112#include <unistd.h>
113#endif
114
01d981ec 115#ifdef __MWERKS__
31907d03
SC
116#ifdef __MACH__
117#include <sys/types.h>
118#include <utime.h>
119#include <sys/stat.h>
120#include <unistd.h>
121#else
0b9ab0bd
RL
122#include <stat.h>
123#include <unistd.h>
124#include <unix.h>
01d981ec 125#endif
31907d03 126#endif
01d981ec 127
d9f54bb0 128#ifdef __WATCOMC__
0b9ab0bd
RL
129#include <io.h>
130#include <sys/utime.h>
131#include <sys/stat.h>
d9f54bb0
VS
132#endif
133
24eb81cb
DW
134#ifdef __VISAGECPP__
135#ifndef MAX_PATH
136#define MAX_PATH 256
137#endif
138#endif
139
01c9a906 140#ifdef __EMX__
5d66debf 141#include <os2.h>
01c9a906
SN
142#define MAX_PATH _MAX_PATH
143#endif
144
951cd180
VZ
145// ----------------------------------------------------------------------------
146// private classes
147// ----------------------------------------------------------------------------
148
149// small helper class which opens and closes the file - we use it just to get
150// a file handle for the given file name to pass it to some Win32 API function
c67d6888 151#if defined(__WIN32__) && !defined(__WXMICROWIN__)
951cd180
VZ
152
153class wxFileHandle
154{
155public:
6dbb903b
VZ
156 enum OpenMode
157 {
158 Read,
159 Write
160 };
161
162 wxFileHandle(const wxString& filename, OpenMode mode)
951cd180
VZ
163 {
164 m_hFile = ::CreateFile
165 (
6dbb903b 166 filename, // name
0ea621cc 167 mode == Read ? GENERIC_READ // access mask
6dbb903b 168 : GENERIC_WRITE,
54bcff35
VZ
169 FILE_SHARE_READ | // sharing mode
170 FILE_SHARE_WRITE, // (allow everything)
6dbb903b
VZ
171 NULL, // no secutity attr
172 OPEN_EXISTING, // creation disposition
173 0, // no flags
174 NULL // no template file
951cd180
VZ
175 );
176
177 if ( m_hFile == INVALID_HANDLE_VALUE )
178 {
6dbb903b
VZ
179 wxLogSysError(_("Failed to open '%s' for %s"),
180 filename.c_str(),
181 mode == Read ? _("reading") : _("writing"));
951cd180
VZ
182 }
183 }
184
185 ~wxFileHandle()
186 {
187 if ( m_hFile != INVALID_HANDLE_VALUE )
188 {
189 if ( !::CloseHandle(m_hFile) )
190 {
191 wxLogSysError(_("Failed to close file handle"));
192 }
193 }
194 }
195
f363e05c 196 // return true only if the file could be opened successfully
951cd180
VZ
197 bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; }
198
199 // get the handle
200 operator HANDLE() const { return m_hFile; }
201
202private:
203 HANDLE m_hFile;
204};
205
206#endif // __WIN32__
207
208// ----------------------------------------------------------------------------
209// private functions
210// ----------------------------------------------------------------------------
211
e2b87f38 212#if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
951cd180
VZ
213
214// convert between wxDateTime and FILETIME which is a 64-bit value representing
215// the number of 100-nanosecond intervals since January 1, 1601.
216
d56e2b97 217static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft)
951cd180 218{
752f10a8 219 FILETIME ftcopy = ft;
6dbb903b 220 FILETIME ftLocal;
752f10a8 221 if ( !::FileTimeToLocalFileTime(&ftcopy, &ftLocal) )
6dbb903b
VZ
222 {
223 wxLogLastError(_T("FileTimeToLocalFileTime"));
224 }
d56e2b97 225
6dbb903b
VZ
226 SYSTEMTIME st;
227 if ( !::FileTimeToSystemTime(&ftLocal, &st) )
228 {
229 wxLogLastError(_T("FileTimeToSystemTime"));
230 }
d56e2b97 231
6dbb903b
VZ
232 dt->Set(st.wDay, wxDateTime::Month(st.wMonth - 1), st.wYear,
233 st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
951cd180
VZ
234}
235
d56e2b97 236static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt)
951cd180 237{
6dbb903b
VZ
238 SYSTEMTIME st;
239 st.wDay = dt.GetDay();
ad816476
WS
240 st.wMonth = (WORD)(dt.GetMonth() + 1);
241 st.wYear = (WORD)dt.GetYear();
6dbb903b
VZ
242 st.wHour = dt.GetHour();
243 st.wMinute = dt.GetMinute();
244 st.wSecond = dt.GetSecond();
245 st.wMilliseconds = dt.GetMillisecond();
246
247 FILETIME ftLocal;
248 if ( !::SystemTimeToFileTime(&st, &ftLocal) )
249 {
250 wxLogLastError(_T("SystemTimeToFileTime"));
251 }
d56e2b97 252
6dbb903b
VZ
253 if ( !::LocalFileTimeToFileTime(&ftLocal, ft) )
254 {
255 wxLogLastError(_T("LocalFileTimeToFileTime"));
256 }
951cd180
VZ
257}
258
e2b87f38 259#endif // wxUSE_DATETIME && __WIN32__
951cd180 260
67c34f64
VZ
261// return a string with the volume par
262static wxString wxGetVolumeString(const wxString& volume, wxPathFormat format)
263{
264 wxString path;
265
266 if ( !volume.empty() )
267 {
6ce27aa2
VZ
268 format = wxFileName::GetFormat(format);
269
67c34f64
VZ
270 // Special Windows UNC paths hack, part 2: undo what we did in
271 // SplitPath() and make an UNC path if we have a drive which is not a
272 // single letter (hopefully the network shares can't be one letter only
273 // although I didn't find any authoritative docs on this)
274 if ( format == wxPATH_DOS && volume.length() > 1 )
275 {
276 path << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << volume;
277 }
278 else if ( format == wxPATH_DOS || format == wxPATH_VMS )
279 {
280 path << volume << wxFileName::GetVolumeSeparator(format);
281 }
282 // else ignore
283 }
284
285 return path;
286}
287
097ead30
VZ
288// ============================================================================
289// implementation
290// ============================================================================
291
292// ----------------------------------------------------------------------------
844f90fb 293// wxFileName construction
097ead30 294// ----------------------------------------------------------------------------
df5ddbca 295
a35b27b1
RR
296void wxFileName::Assign( const wxFileName &filepath )
297{
04c943b1 298 m_volume = filepath.GetVolume();
844f90fb 299 m_dirs = filepath.GetDirs();
04c943b1
VZ
300 m_name = filepath.GetName();
301 m_ext = filepath.GetExt();
a2fa5040 302 m_relative = filepath.m_relative;
df5ddbca
RR
303}
304
04c943b1
VZ
305void wxFileName::Assign(const wxString& volume,
306 const wxString& path,
307 const wxString& name,
308 const wxString& ext,
309 wxPathFormat format )
a7b51bc8
RR
310{
311 SetPath( path, format );
312
313 m_volume = volume;
314 m_ext = ext;
315 m_name = name;
316}
317
f1e77933 318void wxFileName::SetPath( const wxString& pathOrig, wxPathFormat format )
df5ddbca 319{
844f90fb 320 m_dirs.Clear();
90a68369 321
f1e77933 322 if ( pathOrig.empty() )
df5ddbca 323 {
f1e77933
VZ
324 // no path at all
325 m_relative = true;
a2fa5040 326
f1e77933
VZ
327 return;
328 }
90a68369 329
f1e77933 330 format = GetFormat( format );
a2fa5040 331
f1e77933
VZ
332 // 0) deal with possible volume part first
333 wxString volume,
334 path;
335 SplitVolume(pathOrig, &volume, &path, format);
336 if ( !volume.empty() )
337 {
338 m_relative = false;
a2fa5040 339
f1e77933
VZ
340 SetVolume(volume);
341 }
f363e05c 342
f1e77933
VZ
343 // 1) Determine if the path is relative or absolute.
344 wxChar leadingChar = path[0u];
a2fa5040 345
f1e77933
VZ
346 switch (format)
347 {
348 case wxPATH_MAC:
349 m_relative = leadingChar == wxT(':');
350
351 // We then remove a leading ":". The reason is in our
352 // storage form for relative paths:
353 // ":dir:file.txt" actually means "./dir/file.txt" in
354 // DOS notation and should get stored as
355 // (relative) (dir) (file.txt)
356 // "::dir:file.txt" actually means "../dir/file.txt"
357 // stored as (relative) (..) (dir) (file.txt)
358 // This is important only for the Mac as an empty dir
359 // actually means <UP>, whereas under DOS, double
360 // slashes can be ignored: "\\\\" is the same as "\\".
361 if (m_relative)
362 path.erase( 0, 1 );
363 break;
a2fa5040 364
f1e77933
VZ
365 case wxPATH_VMS:
366 // TODO: what is the relative path format here?
367 m_relative = false;
368 break;
90a68369 369
f1e77933
VZ
370 default:
371 wxFAIL_MSG( _T("Unknown path format") );
372 // !! Fall through !!
90a68369 373
f1e77933
VZ
374 case wxPATH_UNIX:
375 // the paths of the form "~" or "~username" are absolute
376 m_relative = leadingChar != wxT('/') && leadingChar != _T('~');
377 break;
353f41cb 378
f1e77933
VZ
379 case wxPATH_DOS:
380 m_relative = !IsPathSeparator(leadingChar, format);
381 break;
844f90fb 382
df5ddbca 383 }
f1e77933
VZ
384
385 // 2) Break up the path into its members. If the original path
386 // was just "/" or "\\", m_dirs will be empty. We know from
387 // the m_relative field, if this means "nothing" or "root dir".
388
389 wxStringTokenizer tn( path, GetPathSeparators(format) );
390
391 while ( tn.HasMoreTokens() )
a7b51bc8 392 {
f1e77933
VZ
393 wxString token = tn.GetNextToken();
394
395 // Remove empty token under DOS and Unix, interpret them
396 // as .. under Mac.
397 if (token.empty())
398 {
399 if (format == wxPATH_MAC)
400 m_dirs.Add( wxT("..") );
401 // else ignore
402 }
403 else
404 {
405 m_dirs.Add( token );
406 }
a7b51bc8 407 }
844f90fb
VZ
408}
409
410void wxFileName::Assign(const wxString& fullpath,
411 wxPathFormat format)
412{
04c943b1
VZ
413 wxString volume, path, name, ext;
414 SplitPath(fullpath, &volume, &path, &name, &ext, format);
844f90fb 415
04c943b1 416 Assign(volume, path, name, ext, format);
844f90fb
VZ
417}
418
81f25632 419void wxFileName::Assign(const wxString& fullpathOrig,
844f90fb
VZ
420 const wxString& fullname,
421 wxPathFormat format)
422{
81f25632
VZ
423 // always recognize fullpath as directory, even if it doesn't end with a
424 // slash
425 wxString fullpath = fullpathOrig;
426 if ( !wxEndsWithPathSeparator(fullpath) )
427 {
33b97389 428 fullpath += GetPathSeparator(format);
81f25632
VZ
429 }
430
52dbd056 431 wxString volume, path, name, ext;
81f25632
VZ
432
433 // do some consistency checks in debug mode: the name should be really just
353f41cb 434 // the filename and the path should be really just a path
81f25632
VZ
435#ifdef __WXDEBUG__
436 wxString pathDummy, nameDummy, extDummy;
437
438 SplitPath(fullname, &pathDummy, &name, &ext, format);
439
440 wxASSERT_MSG( pathDummy.empty(),
441 _T("the file name shouldn't contain the path") );
442
443 SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format);
444
445 wxASSERT_MSG( nameDummy.empty() && extDummy.empty(),
446 _T("the path shouldn't contain file name nor extension") );
447
448#else // !__WXDEBUG__
0ea621cc 449 SplitPath(fullname, NULL /* no path */, &name, &ext, format);
52dbd056 450 SplitPath(fullpath, &volume, &path, NULL, NULL, format);
81f25632 451#endif // __WXDEBUG__/!__WXDEBUG__
844f90fb 452
52dbd056
VZ
453 Assign(volume, path, name, ext, format);
454}
455
456void wxFileName::AssignDir(const wxString& dir, wxPathFormat format)
457{
81f25632 458 Assign(dir, _T(""), format);
844f90fb
VZ
459}
460
461void wxFileName::Clear()
462{
463 m_dirs.Clear();
04c943b1
VZ
464
465 m_volume =
844f90fb
VZ
466 m_name =
467 m_ext = wxEmptyString;
fb969475
VZ
468
469 // we don't have any absolute path for now
f363e05c 470 m_relative = true;
844f90fb
VZ
471}
472
473/* static */
520200fd 474wxFileName wxFileName::FileName(const wxString& file, wxPathFormat format)
844f90fb 475{
520200fd 476 return wxFileName(file, format);
844f90fb
VZ
477}
478
479/* static */
520200fd 480wxFileName wxFileName::DirName(const wxString& dir, wxPathFormat format)
844f90fb
VZ
481{
482 wxFileName fn;
520200fd 483 fn.AssignDir(dir, format);
844f90fb 484 return fn;
df5ddbca
RR
485}
486
844f90fb
VZ
487// ----------------------------------------------------------------------------
488// existence tests
489// ----------------------------------------------------------------------------
490
8e41796c 491bool wxFileName::FileExists() const
df5ddbca 492{
a35b27b1
RR
493 return wxFileName::FileExists( GetFullPath() );
494}
495
496bool wxFileName::FileExists( const wxString &file )
497{
498 return ::wxFileExists( file );
df5ddbca
RR
499}
500
8e41796c 501bool wxFileName::DirExists() const
df5ddbca 502{
a35b27b1
RR
503 return wxFileName::DirExists( GetFullPath() );
504}
505
506bool wxFileName::DirExists( const wxString &dir )
507{
508 return ::wxDirExists( dir );
df5ddbca
RR
509}
510
844f90fb
VZ
511// ----------------------------------------------------------------------------
512// CWD and HOME stuff
513// ----------------------------------------------------------------------------
514
6f91bc33 515void wxFileName::AssignCwd(const wxString& volume)
df5ddbca 516{
6f91bc33 517 AssignDir(wxFileName::GetCwd(volume));
a35b27b1
RR
518}
519
844f90fb 520/* static */
6f91bc33 521wxString wxFileName::GetCwd(const wxString& volume)
a35b27b1 522{
6f91bc33
VZ
523 // if we have the volume, we must get the current directory on this drive
524 // and to do this we have to chdir to this volume - at least under Windows,
525 // I don't know how to get the current drive on another volume elsewhere
526 // (TODO)
527 wxString cwdOld;
528 if ( !volume.empty() )
529 {
530 cwdOld = wxGetCwd();
531 SetCwd(volume + GetVolumeSeparator());
532 }
533
534 wxString cwd = ::wxGetCwd();
535
536 if ( !volume.empty() )
537 {
538 SetCwd(cwdOld);
539 }
ae4d7004 540
6f91bc33 541 return cwd;
a35b27b1
RR
542}
543
544bool wxFileName::SetCwd()
545{
546 return wxFileName::SetCwd( GetFullPath() );
df5ddbca
RR
547}
548
a35b27b1 549bool wxFileName::SetCwd( const wxString &cwd )
df5ddbca 550{
a35b27b1 551 return ::wxSetWorkingDirectory( cwd );
df5ddbca
RR
552}
553
a35b27b1
RR
554void wxFileName::AssignHomeDir()
555{
844f90fb 556 AssignDir(wxFileName::GetHomeDir());
a35b27b1 557}
844f90fb 558
a35b27b1
RR
559wxString wxFileName::GetHomeDir()
560{
561 return ::wxGetHomeDir();
562}
844f90fb 563
df22f860 564void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp)
df5ddbca 565{
df22f860 566 wxString tempname = CreateTempFileName(prefix, fileTemp);
ade35f11 567 if ( tempname.empty() )
844f90fb 568 {
ade35f11
VZ
569 // error, failed to get temp file name
570 Clear();
844f90fb 571 }
ade35f11 572 else // ok
844f90fb 573 {
ade35f11
VZ
574 Assign(tempname);
575 }
576}
577
578/* static */
df22f860
VZ
579wxString
580wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
ade35f11
VZ
581{
582 wxString path, dir, name;
583
584 // use the directory specified by the prefix
585 SplitPath(prefix, &dir, &name, NULL /* extension */);
586
1c193821
JS
587#if defined(__WXWINCE__)
588 if (dir.empty())
589 {
590 // FIXME. Create \temp dir?
591 dir = wxT("\\");
592 }
593 path = dir + wxT("\\") + prefix;
594 int i = 1;
595 while (wxFileExists(path))
596 {
597 path = dir + wxT("\\") + prefix ;
598 path << i;
599 i ++;
600 }
ade35f11 601
1c193821 602#elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
3a5bcc4d 603
ade35f11
VZ
604 if ( dir.empty() )
605 {
606 if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) )
607 {
608 wxLogLastError(_T("GetTempPath"));
609 }
610
611 if ( dir.empty() )
612 {
613 // GetTempFileName() fails if we pass it an empty string
614 dir = _T('.');
615 }
616 }
a2fa5040
VZ
617 else // we have a dir to create the file in
618 {
619 // ensure we use only the back slashes as GetTempFileName(), unlike all
620 // the other APIs, is picky and doesn't accept the forward ones
621 dir.Replace(_T("/"), _T("\\"));
622 }
ade35f11
VZ
623
624 if ( !::GetTempFileName(dir, name, 0, wxStringBuffer(path, MAX_PATH + 1)) )
625 {
626 wxLogLastError(_T("GetTempFileName"));
627
628 path.clear();
629 }
ade35f11 630
5a3912f2 631#else // !Windows
ade35f11
VZ
632 if ( dir.empty() )
633 {
3752e0ab 634#if defined(__WXMAC__) && !defined(__DARWIN__)
263a72f3 635 dir = wxMacFindFolder( (short) kOnSystemDisk, kTemporaryFolderType, kCreateFolder ) ;
3752e0ab 636#else // !Mac
ade35f11 637 dir = wxGetenv(_T("TMP"));
3752e0ab 638 if ( dir.empty() )
ade35f11
VZ
639 {
640 dir = wxGetenv(_T("TEMP"));
641 }
642
643 if ( dir.empty() )
644 {
645 // default
5a3912f2 646 #if defined(__DOS__) || defined(__OS2__)
903b61cc 647 dir = _T(".");
d9f54bb0 648 #else
903b61cc 649 dir = _T("/tmp");
d9f54bb0 650 #endif
ade35f11 651 }
3752e0ab 652#endif // Mac/!Mac
844f90fb 653 }
ade35f11
VZ
654
655 path = dir;
656
657 if ( !wxEndsWithPathSeparator(dir) &&
658 (name.empty() || !wxIsPathSeparator(name[0u])) )
659 {
d9f54bb0 660 path += wxFILE_SEP_PATH;
ade35f11
VZ
661 }
662
663 path += name;
664
7070f55b 665#if defined(HAVE_MKSTEMP)
ade35f11
VZ
666 // scratch space for mkstemp()
667 path += _T("XXXXXX");
668
74cf9763 669 // we need to copy the path to the buffer in which mkstemp() can modify it
2b5f62a0 670 wxCharBuffer buf( wxConvFile.cWX2MB( path ) );
74cf9763
VZ
671
672 // cast is safe because the string length doesn't change
ca11abde 673 int fdTemp = mkstemp( (char*)(const char*) buf );
df22f860 674 if ( fdTemp == -1 )
ade35f11
VZ
675 {
676 // this might be not necessary as mkstemp() on most systems should have
677 // already done it but it doesn't hurt neither...
678 path.clear();
679 }
df22f860
VZ
680 else // mkstemp() succeeded
681 {
ca11abde 682 path = wxConvFile.cMB2WX( (const char*) buf );
a62848fd 683
df22f860
VZ
684 // avoid leaking the fd
685 if ( fileTemp )
686 {
687 fileTemp->Attach(fdTemp);
688 }
689 else
690 {
691 close(fdTemp);
692 }
693 }
ade35f11
VZ
694#else // !HAVE_MKSTEMP
695
696#ifdef HAVE_MKTEMP
697 // same as above
698 path += _T("XXXXXX");
699
ca11abde
RR
700 wxCharBuffer buf = wxConvFile.cWX2MB( path );
701 if ( !mktemp( (const char*) buf ) )
ade35f11
VZ
702 {
703 path.clear();
704 }
aed08d79
RR
705 else
706 {
ca11abde 707 path = wxConvFile.cMB2WX( (const char*) buf );
aed08d79 708 }
7070f55b 709#else // !HAVE_MKTEMP (includes __DOS__)
ade35f11 710 // generate the unique file name ourselves
e0ebbbd9 711 #if !defined(__DOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
ade35f11 712 path << (unsigned int)getpid();
7070f55b 713 #endif
ade35f11
VZ
714
715 wxString pathTry;
716
717 static const size_t numTries = 1000;
718 for ( size_t n = 0; n < numTries; n++ )
719 {
720 // 3 hex digits is enough for numTries == 1000 < 4096
3e778e3b 721 pathTry = path + wxString::Format(_T("%.03x"), (unsigned int) n);
ade35f11
VZ
722 if ( !wxFile::Exists(pathTry) )
723 {
724 break;
725 }
726
727 pathTry.clear();
728 }
729
730 path = pathTry;
731#endif // HAVE_MKTEMP/!HAVE_MKTEMP
732
733 if ( !path.empty() )
734 {
df22f860
VZ
735 }
736#endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
737
738#endif // Windows/!Windows
739
740 if ( path.empty() )
741 {
742 wxLogSysError(_("Failed to create a temporary file name"));
743 }
744 else if ( fileTemp && !fileTemp->IsOpened() )
745 {
746 // open the file - of course, there is a race condition here, this is
ade35f11 747 // why we always prefer using mkstemp()...
90a68369
VZ
748 //
749 // NB: GetTempFileName() under Windows creates the file, so using
750 // write_excl there would fail
751 if ( !fileTemp->Open(path,
752#if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
753 wxFile::write,
754#else
755 wxFile::write_excl,
756#endif
757 wxS_IRUSR | wxS_IWUSR) )
ade35f11
VZ
758 {
759 // FIXME: If !ok here should we loop and try again with another
760 // file name? That is the standard recourse if open(O_EXCL)
761 // fails, though of course it should be protected against
762 // possible infinite looping too.
763
764 wxLogError(_("Failed to open temporary file."));
765
766 path.clear();
767 }
768 }
ade35f11
VZ
769
770 return path;
df5ddbca
RR
771}
772
844f90fb
VZ
773// ----------------------------------------------------------------------------
774// directory operations
775// ----------------------------------------------------------------------------
776
1527281e 777bool wxFileName::Mkdir( int perm, int flags )
a35b27b1 778{
1527281e 779 return wxFileName::Mkdir( GetFullPath(), perm, flags );
a35b27b1
RR
780}
781
1527281e 782bool wxFileName::Mkdir( const wxString& dir, int perm, int flags )
df5ddbca 783{
1527281e 784 if ( flags & wxPATH_MKDIR_FULL )
f0ce3409 785 {
1527281e
VZ
786 // split the path in components
787 wxFileName filename;
788 filename.AssignDir(dir);
f0ce3409 789
f0ce3409 790 wxString currPath;
0ea621cc
VZ
791 if ( filename.HasVolume())
792 {
1527281e 793 currPath << wxGetVolumeString(filename.GetVolume(), wxPATH_NATIVE);
0ea621cc 794 }
f0ce3409 795
1527281e
VZ
796 wxArrayString dirs = filename.GetDirs();
797 size_t count = dirs.GetCount();
798 for ( size_t i = 0; i < count; i++ )
799 {
a62848fd 800 if ( i > 0 ||
6b9eeef2 801#if defined(__WXMAC__) && !defined(__DARWIN__)
a62848fd
WS
802 // relative pathnames are exactely the other way round under mac...
803 !filename.IsAbsolute()
6b9eeef2 804#else
a62848fd 805 filename.IsAbsolute()
6b9eeef2
SC
806#endif
807 )
f0ce3409 808 currPath += wxFILE_SEP_PATH;
1527281e 809 currPath += dirs[i];
f0ce3409
JS
810
811 if (!DirExists(currPath))
1527281e 812 {
f0ce3409 813 if (!wxMkdir(currPath, perm))
1527281e
VZ
814 {
815 // no need to try creating further directories
f363e05c 816 return false;
1527281e
VZ
817 }
818 }
f0ce3409
JS
819 }
820
f363e05c 821 return true;
f0ce3409
JS
822
823 }
1527281e
VZ
824
825 return ::wxMkdir( dir, perm );
df5ddbca
RR
826}
827
a35b27b1 828bool wxFileName::Rmdir()
df5ddbca 829{
a35b27b1 830 return wxFileName::Rmdir( GetFullPath() );
df5ddbca
RR
831}
832
a35b27b1 833bool wxFileName::Rmdir( const wxString &dir )
df5ddbca 834{
a35b27b1 835 return ::wxRmdir( dir );
df5ddbca
RR
836}
837
844f90fb
VZ
838// ----------------------------------------------------------------------------
839// path normalization
840// ----------------------------------------------------------------------------
841
32a0d013 842bool wxFileName::Normalize(int flags,
844f90fb
VZ
843 const wxString& cwd,
844 wxPathFormat format)
a35b27b1 845{
05e1201c
VZ
846 // deal with env vars renaming first as this may seriously change the path
847 if ( flags & wxPATH_NORM_ENV_VARS )
848 {
849 wxString pathOrig = GetFullPath(format);
850 wxString path = wxExpandEnvVars(pathOrig);
851 if ( path != pathOrig )
852 {
853 Assign(path);
854 }
855 }
856
857
844f90fb
VZ
858 // the existing path components
859 wxArrayString dirs = GetDirs();
860
861 // the path to prepend in front to make the path absolute
862 wxFileName curDir;
863
864 format = GetFormat(format);
ae4d7004 865
844f90fb 866 // make the path absolute
a2fa5040 867 if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute(format) )
a35b27b1 868 {
844f90fb 869 if ( cwd.empty() )
6f91bc33
VZ
870 {
871 curDir.AssignCwd(GetVolume());
872 }
873 else // cwd provided
874 {
844f90fb 875 curDir.AssignDir(cwd);
6f91bc33 876 }
52dbd056
VZ
877
878 // the path may be not absolute because it doesn't have the volume name
879 // but in this case we shouldn't modify the directory components of it
880 // but just set the current volume
881 if ( !HasVolume() && curDir.HasVolume() )
882 {
883 SetVolume(curDir.GetVolume());
884
a2fa5040 885 if ( !m_relative )
52dbd056
VZ
886 {
887 // yes, it was the case - we don't need curDir then
888 curDir.Clear();
889 }
890 }
a35b27b1 891 }
ae4d7004 892
844f90fb
VZ
893 // handle ~ stuff under Unix only
894 if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
a35b27b1 895 {
844f90fb
VZ
896 if ( !dirs.IsEmpty() )
897 {
898 wxString dir = dirs[0u];
899 if ( !dir.empty() && dir[0u] == _T('~') )
900 {
901 curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
902
da2fd5ac 903 dirs.RemoveAt(0u);
844f90fb
VZ
904 }
905 }
a35b27b1 906 }
844f90fb 907
52dbd056 908 // transform relative path into abs one
844f90fb 909 if ( curDir.IsOk() )
a35b27b1 910 {
844f90fb
VZ
911 wxArrayString dirsNew = curDir.GetDirs();
912 size_t count = dirs.GetCount();
913 for ( size_t n = 0; n < count; n++ )
914 {
915 dirsNew.Add(dirs[n]);
916 }
917
918 dirs = dirsNew;
a35b27b1 919 }
844f90fb
VZ
920
921 // now deal with ".", ".." and the rest
922 m_dirs.Empty();
923 size_t count = dirs.GetCount();
924 for ( size_t n = 0; n < count; n++ )
a35b27b1 925 {
844f90fb
VZ
926 wxString dir = dirs[n];
927
2bc60417 928 if ( flags & wxPATH_NORM_DOTS )
844f90fb
VZ
929 {
930 if ( dir == wxT(".") )
931 {
932 // just ignore
933 continue;
934 }
935
936 if ( dir == wxT("..") )
937 {
938 if ( m_dirs.IsEmpty() )
939 {
940 wxLogError(_("The path '%s' contains too many \"..\"!"),
941 GetFullPath().c_str());
f363e05c 942 return false;
844f90fb
VZ
943 }
944
ae4d7004 945 m_dirs.RemoveAt(m_dirs.GetCount() - 1);
844f90fb
VZ
946 continue;
947 }
948 }
949
844f90fb
VZ
950 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
951 {
952 dir.MakeLower();
953 }
954
955 m_dirs.Add(dir);
a35b27b1 956 }
a62848fd 957
6bda7391 958#if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
21f60945
JS
959 if ( (flags & wxPATH_NORM_SHORTCUT) )
960 {
961 wxString filename;
962 if (GetShortcutTarget(GetFullPath(format), filename))
963 {
964 // Repeat this since we may now have a new path
965 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
966 {
967 filename.MakeLower();
968 }
969 m_relative = false;
970 Assign(filename);
971 }
972 }
973#endif
844f90fb
VZ
974
975 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
976 {
977 // VZ: expand env vars here too?
978
55268a3a 979 m_volume.MakeLower();
844f90fb
VZ
980 m_name.MakeLower();
981 m_ext.MakeLower();
982 }
983
e83ecba9
VZ
984 // we do have the path now
985 //
986 // NB: need to do this before (maybe) calling Assign() below
f363e05c 987 m_relative = false;
e83ecba9 988
52dbd056
VZ
989#if defined(__WIN32__)
990 if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) )
9e9b65c1
JS
991 {
992 Assign(GetLongPath());
993 }
52dbd056 994#endif // Win32
9e9b65c1 995
f363e05c 996 return true;
a2fa5040
VZ
997}
998
21f60945
JS
999// ----------------------------------------------------------------------------
1000// get the shortcut target
1001// ----------------------------------------------------------------------------
1002
5671b3b6
JS
1003// WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions.
1004// The .lnk file is a plain text file so it should be easy to
1005// make it work. Hint from Google Groups:
1006// "If you open up a lnk file, you'll see a
1007// number, followed by a pound sign (#), followed by more text. The
1008// number is the number of characters that follows the pound sign. The
1009// characters after the pound sign are the command line (which _can_
1010// include arguments) to be executed. Any path (e.g. \windows\program
1011// files\myapp.exe) that includes spaces needs to be enclosed in
1012// quotation marks."
1013
6bda7391 1014#if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
5671b3b6
JS
1015// The following lines are necessary under WinCE
1016// #include "wx/msw/private.h"
1017// #include <ole2.h>
21f60945 1018#include <shlobj.h>
5671b3b6
JS
1019#if defined(__WXWINCE__)
1020#include <shlguid.h>
1021#endif
21f60945 1022
21f60945
JS
1023bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, wxString& targetFilename, wxString* arguments)
1024{
21f60945
JS
1025 wxString path, file, ext;
1026 wxSplitPath(shortcutPath, & path, & file, & ext);
a62848fd
WS
1027
1028 HRESULT hres;
1029 IShellLink* psl;
1030 bool success = false;
21f60945
JS
1031
1032 // Assume it's not a shortcut if it doesn't end with lnk
1033 if (ext.Lower() != wxT("lnk"))
a62848fd
WS
1034 return false;
1035
1036 // create a ShellLink object
1037 hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1038 IID_IShellLink, (LPVOID*) &psl);
1039
1040 if (SUCCEEDED(hres))
1041 {
1042 IPersistFile* ppf;
1043 hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf);
1044 if (SUCCEEDED(hres))
1045 {
1046 WCHAR wsz[MAX_PATH];
1047
1048 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, shortcutPath.mb_str(), -1, wsz,
1049 MAX_PATH);
1050
1051 hres = ppf->Load(wsz, 0);
1052 if (SUCCEEDED(hres))
1053 {
21f60945 1054 wxChar buf[2048];
59ba321b
JS
1055 // Wrong prototype in early versions
1056#if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2)
1057 psl->GetPath((CHAR*) buf, 2048, NULL, SLGP_UNCPRIORITY);
1058#else
a62848fd 1059 psl->GetPath(buf, 2048, NULL, SLGP_UNCPRIORITY);
59ba321b 1060#endif
a62848fd 1061 targetFilename = wxString(buf);
21f60945
JS
1062 success = (shortcutPath != targetFilename);
1063
a62848fd 1064 psl->GetArguments(buf, 2048);
21f60945
JS
1065 wxString args(buf);
1066 if (!args.IsEmpty() && arguments)
1067 {
1068 *arguments = args;
a62848fd
WS
1069 }
1070 }
1071 }
1072 }
1073 psl->Release();
1074 return success;
21f60945
JS
1075}
1076#endif
1077
1078
a2fa5040
VZ
1079// ----------------------------------------------------------------------------
1080// absolute/relative paths
1081// ----------------------------------------------------------------------------
1082
1083bool wxFileName::IsAbsolute(wxPathFormat format) const
1084{
1085 // if our path doesn't start with a path separator, it's not an absolute
1086 // path
1087 if ( m_relative )
f363e05c 1088 return false;
a2fa5040
VZ
1089
1090 if ( !GetVolumeSeparator(format).empty() )
1091 {
1092 // this format has volumes and an absolute path must have one, it's not
1093 // enough to have the full path to bean absolute file under Windows
1094 if ( GetVolume().empty() )
f363e05c 1095 return false;
a2fa5040
VZ
1096 }
1097
f363e05c 1098 return true;
a35b27b1
RR
1099}
1100
f7d886af
VZ
1101bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format)
1102{
cef9731a 1103 wxFileName fnBase = wxFileName::DirName(pathBase, format);
f7d886af
VZ
1104
1105 // get cwd only once - small time saving
1106 wxString cwd = wxGetCwd();
b5b62eea
JS
1107 Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format);
1108 fnBase.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format);
f7d886af
VZ
1109
1110 bool withCase = IsCaseSensitive(format);
1111
1112 // we can't do anything if the files live on different volumes
1113 if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) )
1114 {
1115 // nothing done
f363e05c 1116 return false;
f7d886af
VZ
1117 }
1118
1119 // same drive, so we don't need our volume
1120 m_volume.clear();
1121
1122 // remove common directories starting at the top
1123 while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() &&
1124 m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) )
1125 {
ae4d7004
VZ
1126 m_dirs.RemoveAt(0);
1127 fnBase.m_dirs.RemoveAt(0);
f7d886af
VZ
1128 }
1129
1130 // add as many ".." as needed
1131 size_t count = fnBase.m_dirs.GetCount();
1132 for ( size_t i = 0; i < count; i++ )
1133 {
1134 m_dirs.Insert(wxT(".."), 0u);
1135 }
90a68369 1136
2db991f4
VZ
1137 if ( format == wxPATH_UNIX || format == wxPATH_DOS )
1138 {
1139 // a directory made relative with respect to itself is '.' under Unix
1140 // and DOS, by definition (but we don't have to insert "./" for the
1141 // files)
1142 if ( m_dirs.IsEmpty() && IsDir() )
1143 {
1144 m_dirs.Add(_T('.'));
0ea621cc 1145 }
2db991f4
VZ
1146 }
1147
f363e05c 1148 m_relative = true;
f7d886af
VZ
1149
1150 // we were modified
f363e05c 1151 return true;
f7d886af
VZ
1152}
1153
844f90fb
VZ
1154// ----------------------------------------------------------------------------
1155// filename kind tests
1156// ----------------------------------------------------------------------------
1157
2b5f62a0 1158bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const
df5ddbca 1159{
844f90fb
VZ
1160 wxFileName fn1 = *this,
1161 fn2 = filepath;
1162
1163 // get cwd only once - small time saving
1164 wxString cwd = wxGetCwd();
55268a3a
VZ
1165 fn1.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format);
1166 fn2.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format);
844f90fb
VZ
1167
1168 if ( fn1.GetFullPath() == fn2.GetFullPath() )
f363e05c 1169 return true;
844f90fb
VZ
1170
1171 // TODO: compare inodes for Unix, this works even when filenames are
1172 // different but files are the same (symlinks) (VZ)
1173
f363e05c 1174 return false;
df5ddbca
RR
1175}
1176
844f90fb 1177/* static */
df5ddbca
RR
1178bool wxFileName::IsCaseSensitive( wxPathFormat format )
1179{
04c943b1
VZ
1180 // only Unix filenames are truely case-sensitive
1181 return GetFormat(format) == wxPATH_UNIX;
df5ddbca
RR
1182}
1183
f363e05c
VZ
1184/* static */
1185wxString wxFileName::GetForbiddenChars(wxPathFormat format)
1186{
1187 // Inits to forbidden characters that are common to (almost) all platforms.
1188 wxString strForbiddenChars = wxT("*?");
1189
1c6f2414 1190 // If asserts, wxPathFormat has been changed. In case of a new path format
f363e05c 1191 // addition, the following code might have to be updated.
1c6f2414 1192 wxCOMPILE_TIME_ASSERT(wxPATH_MAX == 5, wxPathFormatChanged);
f363e05c
VZ
1193 switch ( GetFormat(format) )
1194 {
1195 default :
1196 wxFAIL_MSG( wxT("Unknown path format") );
1197 // !! Fall through !!
1198
1199 case wxPATH_UNIX:
1200 break;
1201
1202 case wxPATH_MAC:
1203 // On a Mac even names with * and ? are allowed (Tested with OS
1204 // 9.2.1 and OS X 10.2.5)
1205 strForbiddenChars = wxEmptyString;
1206 break;
1207
1208 case wxPATH_DOS:
1209 strForbiddenChars += wxT("\\/:\"<>|");
1210 break;
1211
1212 case wxPATH_VMS:
1213 break;
1214 }
1215
1216 return strForbiddenChars;
1217}
1218
04c943b1
VZ
1219/* static */
1220wxString wxFileName::GetVolumeSeparator(wxPathFormat format)
844f90fb 1221{
04c943b1
VZ
1222 wxString sepVol;
1223
a385b5df
RR
1224 if ( (GetFormat(format) == wxPATH_DOS) ||
1225 (GetFormat(format) == wxPATH_VMS) )
04c943b1 1226 {
04c943b1
VZ
1227 sepVol = wxFILE_SEP_DSK;
1228 }
a385b5df 1229 //else: leave empty
04c943b1
VZ
1230
1231 return sepVol;
844f90fb
VZ
1232}
1233
1234/* static */
1235wxString wxFileName::GetPathSeparators(wxPathFormat format)
1236{
1237 wxString seps;
1238 switch ( GetFormat(format) )
df5ddbca 1239 {
844f90fb 1240 case wxPATH_DOS:
04c943b1
VZ
1241 // accept both as native APIs do but put the native one first as
1242 // this is the one we use in GetFullPath()
1243 seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX;
844f90fb
VZ
1244 break;
1245
1246 default:
f363e05c 1247 wxFAIL_MSG( _T("Unknown wxPATH_XXX style") );
844f90fb
VZ
1248 // fall through
1249
1250 case wxPATH_UNIX:
9e8d8607 1251 seps = wxFILE_SEP_PATH_UNIX;
844f90fb
VZ
1252 break;
1253
1254 case wxPATH_MAC:
9e8d8607 1255 seps = wxFILE_SEP_PATH_MAC;
844f90fb 1256 break;
04c943b1 1257
3c621059
JJ
1258 case wxPATH_VMS:
1259 seps = wxFILE_SEP_PATH_VMS;
1260 break;
df5ddbca
RR
1261 }
1262
844f90fb 1263 return seps;
df5ddbca
RR
1264}
1265
f1e77933
VZ
1266/* static */
1267wxString wxFileName::GetPathTerminators(wxPathFormat format)
1268{
1269 format = GetFormat(format);
1270
1271 // under VMS the end of the path is ']', not the path separator used to
1272 // separate the components
1273 return format == wxPATH_VMS ? wxString(_T(']')) : GetPathSeparators(format);
1274}
1275
844f90fb
VZ
1276/* static */
1277bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format)
df5ddbca 1278{
04c943b1
VZ
1279 // wxString::Find() doesn't work as expected with NUL - it will always find
1280 // it, so it is almost surely a bug if this function is called with NUL arg
1281 wxASSERT_MSG( ch != _T('\0'), _T("shouldn't be called with NUL") );
1282
844f90fb 1283 return GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
df5ddbca
RR
1284}
1285
844f90fb
VZ
1286// ----------------------------------------------------------------------------
1287// path components manipulation
1288// ----------------------------------------------------------------------------
1289
5bb9aeb2
VZ
1290/* static */ bool wxFileName::IsValidDirComponent(const wxString& dir)
1291{
1292 if ( dir.empty() )
1293 {
1294 wxFAIL_MSG( _T("empty directory passed to wxFileName::InsertDir()") );
1295
1296 return false;
1297 }
1298
1299 const size_t len = dir.length();
1300 for ( size_t n = 0; n < len; n++ )
1301 {
1302 if ( dir[n] == GetVolumeSeparator() || IsPathSeparator(dir[n]) )
1303 {
1304 wxFAIL_MSG( _T("invalid directory component in wxFileName") );
1305
1306 return false;
1307 }
1308 }
1309
1310 return true;
1311}
1312
df5ddbca
RR
1313void wxFileName::AppendDir( const wxString &dir )
1314{
5bb9aeb2
VZ
1315 if ( IsValidDirComponent(dir) )
1316 m_dirs.Add( dir );
df5ddbca
RR
1317}
1318
1319void wxFileName::PrependDir( const wxString &dir )
1320{
5bb9aeb2 1321 InsertDir(0, dir);
df5ddbca
RR
1322}
1323
1324void wxFileName::InsertDir( int before, const wxString &dir )
1325{
5bb9aeb2
VZ
1326 if ( IsValidDirComponent(dir) )
1327 m_dirs.Insert( dir, before );
df5ddbca
RR
1328}
1329
1330void wxFileName::RemoveDir( int pos )
1331{
ba8c1601 1332 m_dirs.RemoveAt( (size_t)pos );
df5ddbca
RR
1333}
1334
844f90fb
VZ
1335// ----------------------------------------------------------------------------
1336// accessors
1337// ----------------------------------------------------------------------------
1338
7124df9b
VZ
1339void wxFileName::SetFullName(const wxString& fullname)
1340{
1341 SplitPath(fullname, NULL /* no path */, &m_name, &m_ext);
1342}
1343
844f90fb 1344wxString wxFileName::GetFullName() const
a35b27b1 1345{
844f90fb
VZ
1346 wxString fullname = m_name;
1347 if ( !m_ext.empty() )
a35b27b1 1348 {
9e8d8607 1349 fullname << wxFILE_SEP_EXT << m_ext;
a35b27b1 1350 }
a35b27b1 1351
844f90fb 1352 return fullname;
a35b27b1
RR
1353}
1354
33b97389 1355wxString wxFileName::GetPath( int flags, wxPathFormat format ) const
df5ddbca
RR
1356{
1357 format = GetFormat( format );
844f90fb 1358
353f41cb
RR
1359 wxString fullpath;
1360
33b97389
VZ
1361 // return the volume with the path as well if requested
1362 if ( flags & wxPATH_GET_VOLUME )
1363 {
1364 fullpath += wxGetVolumeString(GetVolume(), format);
1365 }
1366
034742fc
VZ
1367 // the leading character
1368 switch ( format )
1369 {
1370 case wxPATH_MAC:
1371 if ( m_relative )
1372 fullpath += wxFILE_SEP_PATH_MAC;
1373 break;
1374
1375 case wxPATH_DOS:
1376 if ( !m_relative )
1377 fullpath += wxFILE_SEP_PATH_DOS;
1378 break;
1379
1380 default:
1381 wxFAIL_MSG( wxT("Unknown path format") );
1382 // fall through
1383
1384 case wxPATH_UNIX:
1385 if ( !m_relative )
1386 {
1387 // normally the absolute file names start with a slash
1388 // with one exception: the ones like "~/foo.bar" don't
1389 // have it
9b3a3820 1390 if ( m_dirs.IsEmpty() || m_dirs[0u] != _T('~') )
034742fc
VZ
1391 {
1392 fullpath += wxFILE_SEP_PATH_UNIX;
1393 }
1394 }
1395 break;
1396
1397 case wxPATH_VMS:
1398 // no leading character here but use this place to unset
1399 // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense
1400 // as, if I understand correctly, there should never be a dot
1401 // before the closing bracket
1402 flags &= ~wxPATH_GET_SEPARATOR;
1403 }
1404
1405 if ( m_dirs.empty() )
1406 {
1407 // there is nothing more
1408 return fullpath;
1409 }
1410
1411 // then concatenate all the path components using the path separator
1412 if ( format == wxPATH_VMS )
1413 {
1414 fullpath += wxT('[');
1415 }
1416
5bb9aeb2 1417 const size_t dirCount = m_dirs.GetCount();
034742fc 1418 for ( size_t i = 0; i < dirCount; i++ )
353f41cb 1419 {
034742fc 1420 switch (format)
5bb9aeb2
VZ
1421 {
1422 case wxPATH_MAC:
034742fc
VZ
1423 if ( m_dirs[i] == wxT(".") )
1424 {
1425 // skip appending ':', this shouldn't be done in this
1426 // case as "::" is interpreted as ".." under Unix
1427 continue;
1428 }
2361ce82 1429
034742fc
VZ
1430 // convert back from ".." to nothing
1431 if ( m_dirs[i] != wxT("..") )
1432 fullpath += m_dirs[i];
5bb9aeb2 1433 break;
2361ce82 1434
5bb9aeb2 1435 default:
034742fc
VZ
1436 wxFAIL_MSG( wxT("Unexpected path format") );
1437 // still fall through
2361ce82 1438
034742fc 1439 case wxPATH_DOS:
5bb9aeb2 1440 case wxPATH_UNIX:
034742fc 1441 fullpath += m_dirs[i];
5bb9aeb2 1442 break;
2361ce82 1443
5bb9aeb2 1444 case wxPATH_VMS:
034742fc 1445 // TODO: What to do with ".." under VMS
353f41cb 1446
034742fc
VZ
1447 // convert back from ".." to nothing
1448 if ( m_dirs[i] != wxT("..") )
353f41cb 1449 fullpath += m_dirs[i];
034742fc 1450 break;
4175794e
VZ
1451 }
1452
034742fc
VZ
1453 if ( (flags & wxPATH_GET_SEPARATOR) || (i != dirCount - 1) )
1454 fullpath += GetPathSeparator(format);
df5ddbca 1455 }
034742fc
VZ
1456
1457 if ( format == wxPATH_VMS )
5bb9aeb2 1458 {
034742fc 1459 fullpath += wxT(']');
5bb9aeb2 1460 }
844f90fb 1461
353f41cb 1462 return fullpath;
df5ddbca
RR
1463}
1464
1465wxString wxFileName::GetFullPath( wxPathFormat format ) const
1466{
4175794e
VZ
1467 // we already have a function to get the path
1468 wxString fullpath = GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR,
1469 format);
04c943b1 1470
4175794e 1471 // now just add the file name and extension to it
04c943b1
VZ
1472 fullpath += GetFullName();
1473
1474 return fullpath;
df5ddbca
RR
1475}
1476
9e9b65c1
JS
1477// Return the short form of the path (returns identity on non-Windows platforms)
1478wxString wxFileName::GetShortPath() const
1479{
1c193821 1480#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
9e9b65c1 1481 wxString path(GetFullPath());
75ef5722
JS
1482 wxString pathOut;
1483 DWORD sz = ::GetShortPathName(path, NULL, 0);
1484 bool ok = sz != 0;
1485 if ( ok )
9e9b65c1 1486 {
75ef5722
JS
1487 ok = ::GetShortPathName
1488 (
1489 path,
de564874 1490 wxStringBuffer(pathOut, sz),
75ef5722
JS
1491 sz
1492 ) != 0;
9e9b65c1 1493 }
75ef5722
JS
1494 if (ok)
1495 return pathOut;
5716a1ab
VZ
1496
1497 return path;
9e9b65c1
JS
1498#else
1499 return GetFullPath();
1500#endif
1501}
1502
1503// Return the long form of the path (returns identity on non-Windows platforms)
1504wxString wxFileName::GetLongPath() const
1505{
52dbd056
VZ
1506 wxString pathOut,
1507 path = GetFullPath();
1508
1509#if defined(__WIN32__) && !defined(__WXMICROWIN__)
f363e05c 1510 bool success = false;
05e7001c 1511
35a6691a 1512#if wxUSE_DYNAMIC_LOADER
62dcaed6 1513 typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
05e7001c 1514
f363e05c 1515 static bool s_triedToLoad = false;
05e7001c
JS
1516
1517 if ( !s_triedToLoad )
9e9b65c1 1518 {
2361ce82
VZ
1519 // suppress the errors about missing GetLongPathName[AW]
1520 wxLogNull noLog;
1521
f363e05c 1522 s_triedToLoad = true;
4f89dbc4
RL
1523 wxDynamicLibrary dllKernel(_T("kernel32"));
1524 if ( dllKernel.IsLoaded() )
05e7001c
JS
1525 {
1526 // may succeed or fail depending on the Windows version
04c943b1 1527 static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
5d978d07 1528#ifdef _UNICODE
4f89dbc4 1529 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameW"));
5d978d07 1530#else
4f89dbc4 1531 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameA"));
5d978d07 1532#endif
05e7001c 1533
05e7001c
JS
1534 if ( s_pfnGetLongPathName )
1535 {
1536 DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0);
1537 bool ok = dwSize > 0;
1538
1539 if ( ok )
1540 {
1541 DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0);
1542 ok = sz != 0;
1543 if ( ok )
1544 {
1545 ok = (*s_pfnGetLongPathName)
1546 (
1547 path,
de564874 1548 wxStringBuffer(pathOut, sz),
05e7001c
JS
1549 sz
1550 ) != 0;
f363e05c 1551 success = true;
05e7001c
JS
1552 }
1553 }
1554 }
1555 }
9e9b65c1 1556 }
2361ce82 1557
05e7001c 1558 if (success)
75ef5722 1559 return pathOut;
0b9ab0bd 1560#endif // wxUSE_DYNAMIC_LOADER
05e7001c
JS
1561
1562 if (!success)
1563 {
1564 // The OS didn't support GetLongPathName, or some other error.
1565 // We need to call FindFirstFile on each component in turn.
1566
1567 WIN32_FIND_DATA findFileData;
1568 HANDLE hFind;
b5b62eea
JS
1569
1570 if ( HasVolume() )
1571 pathOut = GetVolume() +
1572 GetVolumeSeparator(wxPATH_DOS) +
1573 GetPathSeparator(wxPATH_DOS);
1574 else
1575 pathOut = wxEmptyString;
05e7001c 1576
77fe02a8 1577 wxArrayString dirs = GetDirs();
bcbe86e5 1578 dirs.Add(GetFullName());
77fe02a8 1579
05e7001c 1580 wxString tmpPath;
5d978d07 1581
52dbd056
VZ
1582 size_t count = dirs.GetCount();
1583 for ( size_t i = 0; i < count; i++ )
05e7001c 1584 {
52dbd056
VZ
1585 // We're using pathOut to collect the long-name path, but using a
1586 // temporary for appending the last path component which may be
1587 // short-name
77fe02a8 1588 tmpPath = pathOut + dirs[i];
05e7001c 1589
52dbd056
VZ
1590 if ( tmpPath.empty() )
1591 continue;
1592
b5b62eea
JS
1593 // can't see this being necessary? MF
1594 if ( tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) )
5d978d07
JS
1595 {
1596 // Can't pass a drive and root dir to FindFirstFile,
1597 // so continue to next dir
05e7001c 1598 tmpPath += wxFILE_SEP_PATH;
5d978d07
JS
1599 pathOut = tmpPath;
1600 continue;
1601 }
05e7001c
JS
1602
1603 hFind = ::FindFirstFile(tmpPath, &findFileData);
1604 if (hFind == INVALID_HANDLE_VALUE)
1605 {
b5b62eea
JS
1606 // Error: most likely reason is that path doesn't exist, so
1607 // append any unprocessed parts and return
1608 for ( i += 1; i < count; i++ )
1609 tmpPath += wxFILE_SEP_PATH + dirs[i];
1610
1611 return tmpPath;
05e7001c 1612 }
05e7001c 1613
52dbd056
VZ
1614 pathOut += findFileData.cFileName;
1615 if ( (i < (count-1)) )
1616 pathOut += wxFILE_SEP_PATH;
1617
1618 ::FindClose(hFind);
05e7001c
JS
1619 }
1620 }
52dbd056
VZ
1621#else // !Win32
1622 pathOut = path;
1623#endif // Win32/!Win32
5716a1ab 1624
05e7001c 1625 return pathOut;
9e9b65c1
JS
1626}
1627
df5ddbca
RR
1628wxPathFormat wxFileName::GetFormat( wxPathFormat format )
1629{
1630 if (format == wxPATH_NATIVE)
1631 {
5a3912f2 1632#if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__)
df5ddbca 1633 format = wxPATH_DOS;
4d293f8e 1634#elif defined(__WXMAC__) && !defined(__DARWIN__)
04c943b1 1635 format = wxPATH_MAC;
3c621059 1636#elif defined(__VMS)
04c943b1 1637 format = wxPATH_VMS;
844f90fb 1638#else
df5ddbca
RR
1639 format = wxPATH_UNIX;
1640#endif
1641 }
1642 return format;
1643}
a35b27b1 1644
9e8d8607
VZ
1645// ----------------------------------------------------------------------------
1646// path splitting function
1647// ----------------------------------------------------------------------------
1648
6f91bc33 1649/* static */
f1e77933
VZ
1650void
1651wxFileName::SplitVolume(const wxString& fullpathWithVolume,
1652 wxString *pstrVolume,
1653 wxString *pstrPath,
1654 wxPathFormat format)
9e8d8607
VZ
1655{
1656 format = GetFormat(format);
1657
04c943b1
VZ
1658 wxString fullpath = fullpathWithVolume;
1659
04c943b1
VZ
1660 // special Windows UNC paths hack: transform \\share\path into share:path
1661 if ( format == wxPATH_DOS )
9e8d8607 1662 {
04c943b1
VZ
1663 if ( fullpath.length() >= 4 &&
1664 fullpath[0u] == wxFILE_SEP_PATH_DOS &&
1665 fullpath[1u] == wxFILE_SEP_PATH_DOS )
9e8d8607 1666 {
04c943b1
VZ
1667 fullpath.erase(0, 2);
1668
f1e77933
VZ
1669 size_t posFirstSlash =
1670 fullpath.find_first_of(GetPathTerminators(format));
04c943b1
VZ
1671 if ( posFirstSlash != wxString::npos )
1672 {
1673 fullpath[posFirstSlash] = wxFILE_SEP_DSK;
1674
1675 // UNC paths are always absolute, right? (FIXME)
de564874 1676 fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS);
04c943b1 1677 }
3c621059
JJ
1678 }
1679 }
04c943b1 1680
a385b5df
RR
1681 // We separate the volume here
1682 if ( format == wxPATH_DOS || format == wxPATH_VMS )
04c943b1 1683 {
a385b5df 1684 wxString sepVol = GetVolumeSeparator(format);
24eb81cb 1685
04c943b1
VZ
1686 size_t posFirstColon = fullpath.find_first_of(sepVol);
1687 if ( posFirstColon != wxString::npos )
1688 {
1689 if ( pstrVolume )
1690 {
1691 *pstrVolume = fullpath.Left(posFirstColon);
1692 }
1693
1694 // remove the volume name and the separator from the full path
1695 fullpath.erase(0, posFirstColon + sepVol.length());
1696 }
1697 }
1698
f1e77933
VZ
1699 if ( pstrPath )
1700 *pstrPath = fullpath;
1701}
1702
1703/* static */
1704void wxFileName::SplitPath(const wxString& fullpathWithVolume,
1705 wxString *pstrVolume,
1706 wxString *pstrPath,
1707 wxString *pstrName,
1708 wxString *pstrExt,
1709 wxPathFormat format)
1710{
1711 format = GetFormat(format);
1712
1713 wxString fullpath;
1714 SplitVolume(fullpathWithVolume, pstrVolume, &fullpath, format);
1715
04c943b1
VZ
1716 // find the positions of the last dot and last path separator in the path
1717 size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT);
f1e77933 1718 size_t posLastSlash = fullpath.find_last_of(GetPathTerminators(format));
04c943b1 1719
e5a573a2 1720 // check whether this dot occurs at the very beginning of a path component
04c943b1 1721 if ( (posLastDot != wxString::npos) &&
e5a573a2
VZ
1722 (posLastDot == 0 ||
1723 IsPathSeparator(fullpath[posLastDot - 1]) ||
1724 (format == wxPATH_VMS && fullpath[posLastDot - 1] == _T(']'))) )
f1e77933
VZ
1725 {
1726 // dot may be (and commonly -- at least under Unix -- is) the first
1727 // character of the filename, don't treat the entire filename as
1728 // extension in this case
1729 posLastDot = wxString::npos;
1730 }
9e8d8607 1731
8e7dda21
VZ
1732 // if we do have a dot and a slash, check that the dot is in the name part
1733 if ( (posLastDot != wxString::npos) &&
1734 (posLastSlash != wxString::npos) &&
1735 (posLastDot < posLastSlash) )
9e8d8607
VZ
1736 {
1737 // the dot is part of the path, not the start of the extension
1738 posLastDot = wxString::npos;
1739 }
1740
1741 // now fill in the variables provided by user
1742 if ( pstrPath )
1743 {
1744 if ( posLastSlash == wxString::npos )
1745 {
1746 // no path at all
1747 pstrPath->Empty();
1748 }
1749 else
1750 {
04c943b1 1751 // take everything up to the path separator but take care to make
353f41cb 1752 // the path equal to something like '/', not empty, for the files
04c943b1
VZ
1753 // immediately under root directory
1754 size_t len = posLastSlash;
91b4bd63
SC
1755
1756 // this rule does not apply to mac since we do not start with colons (sep)
1757 // except for relative paths
1758 if ( !len && format != wxPATH_MAC)
04c943b1
VZ
1759 len++;
1760
1761 *pstrPath = fullpath.Left(len);
1762
1763 // special VMS hack: remove the initial bracket
1764 if ( format == wxPATH_VMS )
1765 {
1766 if ( (*pstrPath)[0u] == _T('[') )
1767 pstrPath->erase(0, 1);
1768 }
9e8d8607
VZ
1769 }
1770 }
1771
1772 if ( pstrName )
1773 {
42b1f941
VZ
1774 // take all characters starting from the one after the last slash and
1775 // up to, but excluding, the last dot
9e8d8607 1776 size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
8e7dda21
VZ
1777 size_t count;
1778 if ( posLastDot == wxString::npos )
1779 {
1780 // take all until the end
1781 count = wxString::npos;
1782 }
1783 else if ( posLastSlash == wxString::npos )
1784 {
1785 count = posLastDot;
1786 }
1787 else // have both dot and slash
1788 {
1789 count = posLastDot - posLastSlash - 1;
1790 }
9e8d8607
VZ
1791
1792 *pstrName = fullpath.Mid(nStart, count);
1793 }
1794
1795 if ( pstrExt )
1796 {
1797 if ( posLastDot == wxString::npos )
1798 {
1799 // no extension
1800 pstrExt->Empty();
1801 }
1802 else
1803 {
1804 // take everything after the dot
1805 *pstrExt = fullpath.Mid(posLastDot + 1);
1806 }
1807 }
1808}
951cd180 1809
6f91bc33
VZ
1810/* static */
1811void wxFileName::SplitPath(const wxString& fullpath,
1812 wxString *path,
1813 wxString *name,
1814 wxString *ext,
1815 wxPathFormat format)
1816{
1817 wxString volume;
1818 SplitPath(fullpath, &volume, path, name, ext, format);
1819
67c34f64 1820 if ( path )
6f91bc33 1821 {
67c34f64 1822 path->Prepend(wxGetVolumeString(volume, format));
6f91bc33
VZ
1823 }
1824}
1825
951cd180
VZ
1826// ----------------------------------------------------------------------------
1827// time functions
1828// ----------------------------------------------------------------------------
1829
e2b87f38
VZ
1830#if wxUSE_DATETIME
1831
6dbb903b
VZ
1832bool wxFileName::SetTimes(const wxDateTime *dtAccess,
1833 const wxDateTime *dtMod,
1834 const wxDateTime *dtCreate)
951cd180 1835{
2b5f62a0
VZ
1836#if defined(__WIN32__)
1837 if ( IsDir() )
1838 {
1839 // VZ: please let me know how to do this if you can
1840 wxFAIL_MSG( _T("SetTimes() not implemented for the directories") );
1841 }
1842 else // file
1843 {
1844 wxFileHandle fh(GetFullPath(), wxFileHandle::Write);
1845 if ( fh.IsOk() )
1846 {
1847 FILETIME ftAccess, ftCreate, ftWrite;
1848
1849 if ( dtCreate )
1850 ConvertWxToFileTime(&ftCreate, *dtCreate);
1851 if ( dtAccess )
1852 ConvertWxToFileTime(&ftAccess, *dtAccess);
1853 if ( dtMod )
1854 ConvertWxToFileTime(&ftWrite, *dtMod);
1855
1856 if ( ::SetFileTime(fh,
1857 dtCreate ? &ftCreate : NULL,
1858 dtAccess ? &ftAccess : NULL,
1859 dtMod ? &ftWrite : NULL) )
1860 {
f363e05c 1861 return true;
2b5f62a0
VZ
1862 }
1863 }
1864 }
1865#elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
246c704f
VZ
1866 if ( !dtAccess && !dtMod )
1867 {
1868 // can't modify the creation time anyhow, don't try
f363e05c 1869 return true;
246c704f
VZ
1870 }
1871
1872 // if dtAccess or dtMod is not specified, use the other one (which must be
1873 // non NULL because of the test above) for both times
951cd180 1874 utimbuf utm;
246c704f
VZ
1875 utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks();
1876 utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks();
401eb3de 1877 if ( utime(GetFullPath().fn_str(), &utm) == 0 )
951cd180 1878 {
f363e05c 1879 return true;
951cd180 1880 }
951cd180
VZ
1881#else // other platform
1882#endif // platforms
1883
1884 wxLogSysError(_("Failed to modify file times for '%s'"),
1885 GetFullPath().c_str());
1886
f363e05c 1887 return false;
951cd180
VZ
1888}
1889
1890bool wxFileName::Touch()
1891{
1892#if defined(__UNIX_LIKE__)
1893 // under Unix touching file is simple: just pass NULL to utime()
401eb3de 1894 if ( utime(GetFullPath().fn_str(), NULL) == 0 )
951cd180 1895 {
f363e05c 1896 return true;
951cd180
VZ
1897 }
1898
1899 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1900
f363e05c 1901 return false;
951cd180
VZ
1902#else // other platform
1903 wxDateTime dtNow = wxDateTime::Now();
1904
6dbb903b 1905 return SetTimes(&dtNow, &dtNow, NULL /* don't change create time */);
951cd180
VZ
1906#endif // platforms
1907}
1908
1909bool wxFileName::GetTimes(wxDateTime *dtAccess,
1910 wxDateTime *dtMod,
6dbb903b 1911 wxDateTime *dtCreate) const
951cd180 1912{
2b5f62a0
VZ
1913#if defined(__WIN32__)
1914 // we must use different methods for the files and directories under
1915 // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and
1916 // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and
1917 // not 9x
1918 bool ok;
1919 FILETIME ftAccess, ftCreate, ftWrite;
1920 if ( IsDir() )
1921 {
1922 // implemented in msw/dir.cpp
1923 extern bool wxGetDirectoryTimes(const wxString& dirname,
1924 FILETIME *, FILETIME *, FILETIME *);
1925
1926 // we should pass the path without the trailing separator to
1927 // wxGetDirectoryTimes()
1928 ok = wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME),
1929 &ftAccess, &ftCreate, &ftWrite);
1930 }
1931 else // file
1932 {
1933 wxFileHandle fh(GetFullPath(), wxFileHandle::Read);
1934 if ( fh.IsOk() )
1935 {
1936 ok = ::GetFileTime(fh,
1937 dtCreate ? &ftCreate : NULL,
1938 dtAccess ? &ftAccess : NULL,
1939 dtMod ? &ftWrite : NULL) != 0;
1940 }
1941 else
1942 {
f363e05c 1943 ok = false;
2b5f62a0
VZ
1944 }
1945 }
1946
1947 if ( ok )
1948 {
1949 if ( dtCreate )
1950 ConvertFileTimeToWx(dtCreate, ftCreate);
1951 if ( dtAccess )
1952 ConvertFileTimeToWx(dtAccess, ftAccess);
1953 if ( dtMod )
1954 ConvertFileTimeToWx(dtMod, ftWrite);
1955
f363e05c 1956 return true;
2b5f62a0
VZ
1957 }
1958#elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
951cd180 1959 wxStructStat stBuf;
92980e90 1960 if ( wxStat( GetFullPath().c_str(), &stBuf) == 0 )
951cd180
VZ
1961 {
1962 if ( dtAccess )
1963 dtAccess->Set(stBuf.st_atime);
1964 if ( dtMod )
1965 dtMod->Set(stBuf.st_mtime);
6dbb903b
VZ
1966 if ( dtCreate )
1967 dtCreate->Set(stBuf.st_ctime);
951cd180 1968
f363e05c 1969 return true;
951cd180 1970 }
951cd180
VZ
1971#else // other platform
1972#endif // platforms
1973
1974 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1975 GetFullPath().c_str());
1976
f363e05c 1977 return false;
951cd180
VZ
1978}
1979
e2b87f38
VZ
1980#endif // wxUSE_DATETIME
1981
4dfbcdd5
SC
1982#ifdef __WXMAC__
1983
1984const short kMacExtensionMaxLength = 16 ;
0ca4ae93 1985class MacDefaultExtensionRecord
4dfbcdd5 1986{
0ca4ae93
SC
1987public :
1988 MacDefaultExtensionRecord()
1989 {
1990 m_ext[0] = 0 ;
1991 m_type = m_creator = NULL ;
1992 }
1993 MacDefaultExtensionRecord( const MacDefaultExtensionRecord& from )
1994 {
44c44c82 1995 wxStrcpy( m_ext , from.m_ext ) ;
0ca4ae93
SC
1996 m_type = from.m_type ;
1997 m_creator = from.m_creator ;
1998 }
44c44c82 1999 MacDefaultExtensionRecord( const wxChar * extension , OSType type , OSType creator )
0ca4ae93 2000 {
44c44c82 2001 wxStrncpy( m_ext , extension , kMacExtensionMaxLength ) ;
0ca4ae93
SC
2002 m_ext[kMacExtensionMaxLength] = 0 ;
2003 m_type = type ;
2004 m_creator = creator ;
2005 }
44c44c82 2006 wxChar m_ext[kMacExtensionMaxLength] ;
4dfbcdd5
SC
2007 OSType m_type ;
2008 OSType m_creator ;
0ca4ae93 2009} ;
4dfbcdd5
SC
2010
2011#include "wx/dynarray.h"
2012WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray) ;
0ca4ae93
SC
2013
2014bool gMacDefaultExtensionsInited = false ;
2015
4dfbcdd5 2016#include "wx/arrimpl.cpp"
0ca4ae93
SC
2017
2018WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray) ;
4dfbcdd5
SC
2019
2020MacDefaultExtensionArray gMacDefaultExtensions ;
4dfbcdd5 2021
5974c3cf
SC
2022// load the default extensions
2023MacDefaultExtensionRecord gDefaults[] =
4dfbcdd5 2024{
5974c3cf
SC
2025 MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) ,
2026 MacDefaultExtensionRecord( wxT("tif") , 'TIFF' , '****' ) ,
2027 MacDefaultExtensionRecord( wxT("jpg") , 'JPEG' , '****' ) ,
2028} ;
0ea621cc 2029
5974c3cf
SC
2030static void MacEnsureDefaultExtensionsLoaded()
2031{
2032 if ( !gMacDefaultExtensionsInited )
4dfbcdd5 2033 {
5974c3cf
SC
2034 // we could load the pc exchange prefs here too
2035 for ( size_t i = 0 ; i < WXSIZEOF( gDefaults ) ; ++i )
2036 {
2037 gMacDefaultExtensions.Add( gDefaults[i] ) ;
2038 }
2039 gMacDefaultExtensionsInited = true ;
0ea621cc 2040 }
4dfbcdd5 2041}
a2b77260 2042
0ea621cc 2043bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator )
4dfbcdd5 2044{
a2b77260
SC
2045 FSRef fsRef ;
2046 FSCatalogInfo catInfo;
2047 FileInfo *finfo ;
4dfbcdd5 2048
a2b77260
SC
2049 if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr )
2050 {
a62848fd
WS
2051 if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr )
2052 {
2053 finfo = (FileInfo*)&catInfo.finderInfo;
2054 finfo->fileType = type ;
2055 finfo->fileCreator = creator ;
2056 FSSetCatalogInfo( &fsRef, kFSCatInfoFinderInfo, &catInfo ) ;
a2b77260 2057 return true ;
a62848fd 2058 }
a2b77260
SC
2059 }
2060 return false ;
4dfbcdd5
SC
2061}
2062
0ea621cc 2063bool wxFileName::MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator )
4dfbcdd5 2064{
a2b77260
SC
2065 FSRef fsRef ;
2066 FSCatalogInfo catInfo;
2067 FileInfo *finfo ;
4dfbcdd5 2068
a2b77260
SC
2069 if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr )
2070 {
a62848fd
WS
2071 if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr )
2072 {
2073 finfo = (FileInfo*)&catInfo.finderInfo;
2074 *type = finfo->fileType ;
2075 *creator = finfo->fileCreator ;
a2b77260 2076 return true ;
a62848fd 2077 }
a2b77260
SC
2078 }
2079 return false ;
4dfbcdd5
SC
2080}
2081
2082bool wxFileName::MacSetDefaultTypeAndCreator()
2083{
2084 wxUint32 type , creator ;
2085 if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type ,
2086 &creator ) )
2087 {
f63fb56d
GD
2088 return MacSetTypeAndCreator( type , creator ) ;
2089 }
2090 return false;
4dfbcdd5
SC
2091}
2092
0ea621cc 2093bool wxFileName::MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator )
4dfbcdd5
SC
2094{
2095 MacEnsureDefaultExtensionsLoaded() ;
2096 wxString extl = ext.Lower() ;
2097 for( int i = gMacDefaultExtensions.Count() - 1 ; i >= 0 ; --i )
2098 {
2099 if ( gMacDefaultExtensions.Item(i).m_ext == extl )
2100 {
2101 *type = gMacDefaultExtensions.Item(i).m_type ;
2102 *creator = gMacDefaultExtensions.Item(i).m_creator ;
2103 return true ;
2104 }
2105 }
2106 return false ;
2107}
2108
2109void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator )
2110{
2111 MacEnsureDefaultExtensionsLoaded() ;
2112 MacDefaultExtensionRecord rec ;
2113 rec.m_type = type ;
2114 rec.m_creator = creator ;
44c44c82 2115 wxStrncpy( rec.m_ext , ext.Lower().c_str() , kMacExtensionMaxLength ) ;
4dfbcdd5
SC
2116 gMacDefaultExtensions.Add( rec ) ;
2117}
2118#endif