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