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