]> git.saurik.com Git - wxWidgets.git/blame - src/common/filename.cpp
really show the window after creation (calling Show(TRUE) doesn't do it any more)
[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__)
3a5bcc4d 588
ade35f11
VZ
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 }
ade35f11 615
5a3912f2 616#else // !Windows
ade35f11
VZ
617 if ( dir.empty() )
618 {
3752e0ab 619#if defined(__WXMAC__) && !defined(__DARWIN__)
263a72f3 620 dir = wxMacFindFolder( (short) kOnSystemDisk, kTemporaryFolderType, kCreateFolder ) ;
3752e0ab 621#else // !Mac
ade35f11 622 dir = wxGetenv(_T("TMP"));
3752e0ab 623 if ( dir.empty() )
ade35f11
VZ
624 {
625 dir = wxGetenv(_T("TEMP"));
626 }
627
628 if ( dir.empty() )
629 {
630 // default
5a3912f2 631 #if defined(__DOS__) || defined(__OS2__)
903b61cc 632 dir = _T(".");
d9f54bb0 633 #else
903b61cc 634 dir = _T("/tmp");
d9f54bb0 635 #endif
ade35f11 636 }
3752e0ab 637#endif // Mac/!Mac
844f90fb 638 }
ade35f11
VZ
639
640 path = dir;
641
642 if ( !wxEndsWithPathSeparator(dir) &&
643 (name.empty() || !wxIsPathSeparator(name[0u])) )
644 {
d9f54bb0 645 path += wxFILE_SEP_PATH;
ade35f11
VZ
646 }
647
648 path += name;
649
7070f55b 650#if defined(HAVE_MKSTEMP)
ade35f11
VZ
651 // scratch space for mkstemp()
652 path += _T("XXXXXX");
653
74cf9763 654 // we need to copy the path to the buffer in which mkstemp() can modify it
2b5f62a0 655 wxCharBuffer buf( wxConvFile.cWX2MB( path ) );
74cf9763
VZ
656
657 // cast is safe because the string length doesn't change
ca11abde 658 int fdTemp = mkstemp( (char*)(const char*) buf );
df22f860 659 if ( fdTemp == -1 )
ade35f11
VZ
660 {
661 // this might be not necessary as mkstemp() on most systems should have
662 // already done it but it doesn't hurt neither...
663 path.clear();
664 }
df22f860
VZ
665 else // mkstemp() succeeded
666 {
ca11abde
RR
667 path = wxConvFile.cMB2WX( (const char*) buf );
668
df22f860
VZ
669 // avoid leaking the fd
670 if ( fileTemp )
671 {
672 fileTemp->Attach(fdTemp);
673 }
674 else
675 {
676 close(fdTemp);
677 }
678 }
ade35f11
VZ
679#else // !HAVE_MKSTEMP
680
681#ifdef HAVE_MKTEMP
682 // same as above
683 path += _T("XXXXXX");
684
ca11abde
RR
685 wxCharBuffer buf = wxConvFile.cWX2MB( path );
686 if ( !mktemp( (const char*) buf ) )
ade35f11
VZ
687 {
688 path.clear();
689 }
aed08d79
RR
690 else
691 {
ca11abde 692 path = wxConvFile.cMB2WX( (const char*) buf );
aed08d79 693 }
7070f55b 694#else // !HAVE_MKTEMP (includes __DOS__)
ade35f11 695 // generate the unique file name ourselves
e0ebbbd9 696 #if !defined(__DOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
ade35f11 697 path << (unsigned int)getpid();
7070f55b 698 #endif
ade35f11
VZ
699
700 wxString pathTry;
701
702 static const size_t numTries = 1000;
703 for ( size_t n = 0; n < numTries; n++ )
704 {
705 // 3 hex digits is enough for numTries == 1000 < 4096
3e778e3b 706 pathTry = path + wxString::Format(_T("%.03x"), (unsigned int) n);
ade35f11
VZ
707 if ( !wxFile::Exists(pathTry) )
708 {
709 break;
710 }
711
712 pathTry.clear();
713 }
714
715 path = pathTry;
716#endif // HAVE_MKTEMP/!HAVE_MKTEMP
717
718 if ( !path.empty() )
719 {
df22f860
VZ
720 }
721#endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
722
723#endif // Windows/!Windows
724
725 if ( path.empty() )
726 {
727 wxLogSysError(_("Failed to create a temporary file name"));
728 }
729 else if ( fileTemp && !fileTemp->IsOpened() )
730 {
731 // open the file - of course, there is a race condition here, this is
ade35f11 732 // why we always prefer using mkstemp()...
90a68369
VZ
733 //
734 // NB: GetTempFileName() under Windows creates the file, so using
735 // write_excl there would fail
736 if ( !fileTemp->Open(path,
737#if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
738 wxFile::write,
739#else
740 wxFile::write_excl,
741#endif
742 wxS_IRUSR | wxS_IWUSR) )
ade35f11
VZ
743 {
744 // FIXME: If !ok here should we loop and try again with another
745 // file name? That is the standard recourse if open(O_EXCL)
746 // fails, though of course it should be protected against
747 // possible infinite looping too.
748
749 wxLogError(_("Failed to open temporary file."));
750
751 path.clear();
752 }
753 }
ade35f11
VZ
754
755 return path;
df5ddbca
RR
756}
757
844f90fb
VZ
758// ----------------------------------------------------------------------------
759// directory operations
760// ----------------------------------------------------------------------------
761
1527281e 762bool wxFileName::Mkdir( int perm, int flags )
a35b27b1 763{
1527281e 764 return wxFileName::Mkdir( GetFullPath(), perm, flags );
a35b27b1
RR
765}
766
1527281e 767bool wxFileName::Mkdir( const wxString& dir, int perm, int flags )
df5ddbca 768{
1527281e 769 if ( flags & wxPATH_MKDIR_FULL )
f0ce3409 770 {
1527281e
VZ
771 // split the path in components
772 wxFileName filename;
773 filename.AssignDir(dir);
f0ce3409 774
f0ce3409 775 wxString currPath;
0ea621cc
VZ
776 if ( filename.HasVolume())
777 {
1527281e 778 currPath << wxGetVolumeString(filename.GetVolume(), wxPATH_NATIVE);
0ea621cc 779 }
f0ce3409 780
1527281e
VZ
781 wxArrayString dirs = filename.GetDirs();
782 size_t count = dirs.GetCount();
783 for ( size_t i = 0; i < count; i++ )
784 {
6b9eeef2
SC
785 if ( i > 0 ||
786#if defined(__WXMAC__) && !defined(__DARWIN__)
787 // relative pathnames are exactely the other way round under mac...
788 !filename.IsAbsolute()
789#else
790 filename.IsAbsolute()
791#endif
792 )
f0ce3409 793 currPath += wxFILE_SEP_PATH;
1527281e 794 currPath += dirs[i];
f0ce3409
JS
795
796 if (!DirExists(currPath))
1527281e 797 {
f0ce3409 798 if (!wxMkdir(currPath, perm))
1527281e
VZ
799 {
800 // no need to try creating further directories
f363e05c 801 return false;
1527281e
VZ
802 }
803 }
f0ce3409
JS
804 }
805
f363e05c 806 return true;
f0ce3409
JS
807
808 }
1527281e
VZ
809
810 return ::wxMkdir( dir, perm );
df5ddbca
RR
811}
812
a35b27b1 813bool wxFileName::Rmdir()
df5ddbca 814{
a35b27b1 815 return wxFileName::Rmdir( GetFullPath() );
df5ddbca
RR
816}
817
a35b27b1 818bool wxFileName::Rmdir( const wxString &dir )
df5ddbca 819{
a35b27b1 820 return ::wxRmdir( dir );
df5ddbca
RR
821}
822
844f90fb
VZ
823// ----------------------------------------------------------------------------
824// path normalization
825// ----------------------------------------------------------------------------
826
32a0d013 827bool wxFileName::Normalize(int flags,
844f90fb
VZ
828 const wxString& cwd,
829 wxPathFormat format)
a35b27b1 830{
05e1201c
VZ
831 // deal with env vars renaming first as this may seriously change the path
832 if ( flags & wxPATH_NORM_ENV_VARS )
833 {
834 wxString pathOrig = GetFullPath(format);
835 wxString path = wxExpandEnvVars(pathOrig);
836 if ( path != pathOrig )
837 {
838 Assign(path);
839 }
840 }
841
842
844f90fb
VZ
843 // the existing path components
844 wxArrayString dirs = GetDirs();
845
846 // the path to prepend in front to make the path absolute
847 wxFileName curDir;
848
849 format = GetFormat(format);
ae4d7004 850
844f90fb 851 // make the path absolute
a2fa5040 852 if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute(format) )
a35b27b1 853 {
844f90fb 854 if ( cwd.empty() )
6f91bc33
VZ
855 {
856 curDir.AssignCwd(GetVolume());
857 }
858 else // cwd provided
859 {
844f90fb 860 curDir.AssignDir(cwd);
6f91bc33 861 }
52dbd056
VZ
862
863 // the path may be not absolute because it doesn't have the volume name
864 // but in this case we shouldn't modify the directory components of it
865 // but just set the current volume
866 if ( !HasVolume() && curDir.HasVolume() )
867 {
868 SetVolume(curDir.GetVolume());
869
a2fa5040 870 if ( !m_relative )
52dbd056
VZ
871 {
872 // yes, it was the case - we don't need curDir then
873 curDir.Clear();
874 }
875 }
a35b27b1 876 }
ae4d7004 877
844f90fb
VZ
878 // handle ~ stuff under Unix only
879 if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
a35b27b1 880 {
844f90fb
VZ
881 if ( !dirs.IsEmpty() )
882 {
883 wxString dir = dirs[0u];
884 if ( !dir.empty() && dir[0u] == _T('~') )
885 {
886 curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
887
da2fd5ac 888 dirs.RemoveAt(0u);
844f90fb
VZ
889 }
890 }
a35b27b1 891 }
844f90fb 892
52dbd056 893 // transform relative path into abs one
844f90fb 894 if ( curDir.IsOk() )
a35b27b1 895 {
844f90fb
VZ
896 wxArrayString dirsNew = curDir.GetDirs();
897 size_t count = dirs.GetCount();
898 for ( size_t n = 0; n < count; n++ )
899 {
900 dirsNew.Add(dirs[n]);
901 }
902
903 dirs = dirsNew;
a35b27b1 904 }
844f90fb
VZ
905
906 // now deal with ".", ".." and the rest
907 m_dirs.Empty();
908 size_t count = dirs.GetCount();
909 for ( size_t n = 0; n < count; n++ )
a35b27b1 910 {
844f90fb
VZ
911 wxString dir = dirs[n];
912
2bc60417 913 if ( flags & wxPATH_NORM_DOTS )
844f90fb
VZ
914 {
915 if ( dir == wxT(".") )
916 {
917 // just ignore
918 continue;
919 }
920
921 if ( dir == wxT("..") )
922 {
923 if ( m_dirs.IsEmpty() )
924 {
925 wxLogError(_("The path '%s' contains too many \"..\"!"),
926 GetFullPath().c_str());
f363e05c 927 return false;
844f90fb
VZ
928 }
929
ae4d7004 930 m_dirs.RemoveAt(m_dirs.GetCount() - 1);
844f90fb
VZ
931 continue;
932 }
933 }
934
844f90fb
VZ
935 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
936 {
937 dir.MakeLower();
938 }
939
940 m_dirs.Add(dir);
a35b27b1 941 }
21f60945 942
6bda7391 943#if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
21f60945
JS
944 if ( (flags & wxPATH_NORM_SHORTCUT) )
945 {
946 wxString filename;
947 if (GetShortcutTarget(GetFullPath(format), filename))
948 {
949 // Repeat this since we may now have a new path
950 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
951 {
952 filename.MakeLower();
953 }
954 m_relative = false;
955 Assign(filename);
956 }
957 }
958#endif
844f90fb
VZ
959
960 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
961 {
962 // VZ: expand env vars here too?
963
964 m_name.MakeLower();
965 m_ext.MakeLower();
966 }
967
e83ecba9
VZ
968 // we do have the path now
969 //
970 // NB: need to do this before (maybe) calling Assign() below
f363e05c 971 m_relative = false;
e83ecba9 972
52dbd056
VZ
973#if defined(__WIN32__)
974 if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) )
9e9b65c1
JS
975 {
976 Assign(GetLongPath());
977 }
52dbd056 978#endif // Win32
9e9b65c1 979
f363e05c 980 return true;
a2fa5040
VZ
981}
982
21f60945
JS
983// ----------------------------------------------------------------------------
984// get the shortcut target
985// ----------------------------------------------------------------------------
986
5671b3b6
JS
987// WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions.
988// The .lnk file is a plain text file so it should be easy to
989// make it work. Hint from Google Groups:
990// "If you open up a lnk file, you'll see a
991// number, followed by a pound sign (#), followed by more text. The
992// number is the number of characters that follows the pound sign. The
993// characters after the pound sign are the command line (which _can_
994// include arguments) to be executed. Any path (e.g. \windows\program
995// files\myapp.exe) that includes spaces needs to be enclosed in
996// quotation marks."
997
6bda7391 998#if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
5671b3b6
JS
999// The following lines are necessary under WinCE
1000// #include "wx/msw/private.h"
1001// #include <ole2.h>
21f60945 1002#include <shlobj.h>
5671b3b6
JS
1003#if defined(__WXWINCE__)
1004#include <shlguid.h>
1005#endif
21f60945 1006
21f60945
JS
1007bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, wxString& targetFilename, wxString* arguments)
1008{
21f60945
JS
1009 wxString path, file, ext;
1010 wxSplitPath(shortcutPath, & path, & file, & ext);
1011
1012 HRESULT hres;
1013 IShellLink* psl;
1014 bool success = FALSE;
1015
1016 // Assume it's not a shortcut if it doesn't end with lnk
1017 if (ext.Lower() != wxT("lnk"))
1018 return FALSE;
1019
1020 // create a ShellLink object
1021 hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1022 IID_IShellLink, (LPVOID*) &psl);
1023
1024 if (SUCCEEDED(hres))
1025 {
1026 IPersistFile* ppf;
1027 hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf);
1028 if (SUCCEEDED(hres))
1029 {
00cb0756 1030 WCHAR wsz[MAX_PATH];
21f60945
JS
1031
1032 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, shortcutPath.mb_str(), -1, wsz,
1033 MAX_PATH);
1034
1035 hres = ppf->Load(wsz, 0);
1036 if (SUCCEEDED(hres))
1037 {
1038 wxChar buf[2048];
1039 psl->GetPath(buf, 2048, NULL, SLGP_UNCPRIORITY);
1040 targetFilename = wxString(buf);
1041 success = (shortcutPath != targetFilename);
1042
1043 psl->GetArguments(buf, 2048);
1044 wxString args(buf);
1045 if (!args.IsEmpty() && arguments)
1046 {
1047 *arguments = args;
1048 }
1049 }
1050 }
1051 }
1052 psl->Release();
1053 return success;
21f60945
JS
1054}
1055#endif
1056
1057
a2fa5040
VZ
1058// ----------------------------------------------------------------------------
1059// absolute/relative paths
1060// ----------------------------------------------------------------------------
1061
1062bool wxFileName::IsAbsolute(wxPathFormat format) const
1063{
1064 // if our path doesn't start with a path separator, it's not an absolute
1065 // path
1066 if ( m_relative )
f363e05c 1067 return false;
a2fa5040
VZ
1068
1069 if ( !GetVolumeSeparator(format).empty() )
1070 {
1071 // this format has volumes and an absolute path must have one, it's not
1072 // enough to have the full path to bean absolute file under Windows
1073 if ( GetVolume().empty() )
f363e05c 1074 return false;
a2fa5040
VZ
1075 }
1076
f363e05c 1077 return true;
a35b27b1
RR
1078}
1079
f7d886af
VZ
1080bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format)
1081{
cef9731a 1082 wxFileName fnBase = wxFileName::DirName(pathBase, format);
f7d886af
VZ
1083
1084 // get cwd only once - small time saving
1085 wxString cwd = wxGetCwd();
b5b62eea
JS
1086 Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format);
1087 fnBase.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format);
f7d886af
VZ
1088
1089 bool withCase = IsCaseSensitive(format);
1090
1091 // we can't do anything if the files live on different volumes
1092 if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) )
1093 {
1094 // nothing done
f363e05c 1095 return false;
f7d886af
VZ
1096 }
1097
1098 // same drive, so we don't need our volume
1099 m_volume.clear();
1100
1101 // remove common directories starting at the top
1102 while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() &&
1103 m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) )
1104 {
ae4d7004
VZ
1105 m_dirs.RemoveAt(0);
1106 fnBase.m_dirs.RemoveAt(0);
f7d886af
VZ
1107 }
1108
1109 // add as many ".." as needed
1110 size_t count = fnBase.m_dirs.GetCount();
1111 for ( size_t i = 0; i < count; i++ )
1112 {
1113 m_dirs.Insert(wxT(".."), 0u);
1114 }
90a68369 1115
2db991f4
VZ
1116 if ( format == wxPATH_UNIX || format == wxPATH_DOS )
1117 {
1118 // a directory made relative with respect to itself is '.' under Unix
1119 // and DOS, by definition (but we don't have to insert "./" for the
1120 // files)
1121 if ( m_dirs.IsEmpty() && IsDir() )
1122 {
1123 m_dirs.Add(_T('.'));
0ea621cc 1124 }
2db991f4
VZ
1125 }
1126
f363e05c 1127 m_relative = true;
f7d886af
VZ
1128
1129 // we were modified
f363e05c 1130 return true;
f7d886af
VZ
1131}
1132
844f90fb
VZ
1133// ----------------------------------------------------------------------------
1134// filename kind tests
1135// ----------------------------------------------------------------------------
1136
2b5f62a0 1137bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const
df5ddbca 1138{
844f90fb
VZ
1139 wxFileName fn1 = *this,
1140 fn2 = filepath;
1141
1142 // get cwd only once - small time saving
1143 wxString cwd = wxGetCwd();
b5b62eea
JS
1144 fn1.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format);
1145 fn2.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format);
844f90fb
VZ
1146
1147 if ( fn1.GetFullPath() == fn2.GetFullPath() )
f363e05c 1148 return true;
844f90fb
VZ
1149
1150 // TODO: compare inodes for Unix, this works even when filenames are
1151 // different but files are the same (symlinks) (VZ)
1152
f363e05c 1153 return false;
df5ddbca
RR
1154}
1155
844f90fb 1156/* static */
df5ddbca
RR
1157bool wxFileName::IsCaseSensitive( wxPathFormat format )
1158{
04c943b1
VZ
1159 // only Unix filenames are truely case-sensitive
1160 return GetFormat(format) == wxPATH_UNIX;
df5ddbca
RR
1161}
1162
f363e05c
VZ
1163/* static */
1164wxString wxFileName::GetForbiddenChars(wxPathFormat format)
1165{
1166 // Inits to forbidden characters that are common to (almost) all platforms.
1167 wxString strForbiddenChars = wxT("*?");
1168
1169 // If asserts, wxPathFormat has been changed. In case of a new path format
1170 // addition, the following code might have to be updated.
1171 wxCOMPILE_TIME_ASSERT(wxPATH_MAX == 5, wxPathFormatChanged);
1172 switch ( GetFormat(format) )
1173 {
1174 default :
1175 wxFAIL_MSG( wxT("Unknown path format") );
1176 // !! Fall through !!
1177
1178 case wxPATH_UNIX:
1179 break;
1180
1181 case wxPATH_MAC:
1182 // On a Mac even names with * and ? are allowed (Tested with OS
1183 // 9.2.1 and OS X 10.2.5)
1184 strForbiddenChars = wxEmptyString;
1185 break;
1186
1187 case wxPATH_DOS:
1188 strForbiddenChars += wxT("\\/:\"<>|");
1189 break;
1190
1191 case wxPATH_VMS:
1192 break;
1193 }
1194
1195 return strForbiddenChars;
1196}
1197
04c943b1
VZ
1198/* static */
1199wxString wxFileName::GetVolumeSeparator(wxPathFormat format)
844f90fb 1200{
04c943b1
VZ
1201 wxString sepVol;
1202
a385b5df
RR
1203 if ( (GetFormat(format) == wxPATH_DOS) ||
1204 (GetFormat(format) == wxPATH_VMS) )
04c943b1 1205 {
04c943b1
VZ
1206 sepVol = wxFILE_SEP_DSK;
1207 }
a385b5df 1208 //else: leave empty
04c943b1
VZ
1209
1210 return sepVol;
844f90fb
VZ
1211}
1212
1213/* static */
1214wxString wxFileName::GetPathSeparators(wxPathFormat format)
1215{
1216 wxString seps;
1217 switch ( GetFormat(format) )
df5ddbca 1218 {
844f90fb 1219 case wxPATH_DOS:
04c943b1
VZ
1220 // accept both as native APIs do but put the native one first as
1221 // this is the one we use in GetFullPath()
1222 seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX;
844f90fb
VZ
1223 break;
1224
1225 default:
f363e05c 1226 wxFAIL_MSG( _T("Unknown wxPATH_XXX style") );
844f90fb
VZ
1227 // fall through
1228
1229 case wxPATH_UNIX:
9e8d8607 1230 seps = wxFILE_SEP_PATH_UNIX;
844f90fb
VZ
1231 break;
1232
1233 case wxPATH_MAC:
9e8d8607 1234 seps = wxFILE_SEP_PATH_MAC;
844f90fb 1235 break;
04c943b1 1236
3c621059
JJ
1237 case wxPATH_VMS:
1238 seps = wxFILE_SEP_PATH_VMS;
1239 break;
df5ddbca
RR
1240 }
1241
844f90fb 1242 return seps;
df5ddbca
RR
1243}
1244
844f90fb
VZ
1245/* static */
1246bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format)
df5ddbca 1247{
04c943b1
VZ
1248 // wxString::Find() doesn't work as expected with NUL - it will always find
1249 // it, so it is almost surely a bug if this function is called with NUL arg
1250 wxASSERT_MSG( ch != _T('\0'), _T("shouldn't be called with NUL") );
1251
844f90fb 1252 return GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
df5ddbca
RR
1253}
1254
844f90fb
VZ
1255// ----------------------------------------------------------------------------
1256// path components manipulation
1257// ----------------------------------------------------------------------------
1258
5bb9aeb2
VZ
1259/* static */ bool wxFileName::IsValidDirComponent(const wxString& dir)
1260{
1261 if ( dir.empty() )
1262 {
1263 wxFAIL_MSG( _T("empty directory passed to wxFileName::InsertDir()") );
1264
1265 return false;
1266 }
1267
1268 const size_t len = dir.length();
1269 for ( size_t n = 0; n < len; n++ )
1270 {
1271 if ( dir[n] == GetVolumeSeparator() || IsPathSeparator(dir[n]) )
1272 {
1273 wxFAIL_MSG( _T("invalid directory component in wxFileName") );
1274
1275 return false;
1276 }
1277 }
1278
1279 return true;
1280}
1281
df5ddbca
RR
1282void wxFileName::AppendDir( const wxString &dir )
1283{
5bb9aeb2
VZ
1284 if ( IsValidDirComponent(dir) )
1285 m_dirs.Add( dir );
df5ddbca
RR
1286}
1287
1288void wxFileName::PrependDir( const wxString &dir )
1289{
5bb9aeb2 1290 InsertDir(0, dir);
df5ddbca
RR
1291}
1292
1293void wxFileName::InsertDir( int before, const wxString &dir )
1294{
5bb9aeb2
VZ
1295 if ( IsValidDirComponent(dir) )
1296 m_dirs.Insert( dir, before );
df5ddbca
RR
1297}
1298
1299void wxFileName::RemoveDir( int pos )
1300{
ba8c1601 1301 m_dirs.RemoveAt( (size_t)pos );
df5ddbca
RR
1302}
1303
844f90fb
VZ
1304// ----------------------------------------------------------------------------
1305// accessors
1306// ----------------------------------------------------------------------------
1307
7124df9b
VZ
1308void wxFileName::SetFullName(const wxString& fullname)
1309{
1310 SplitPath(fullname, NULL /* no path */, &m_name, &m_ext);
1311}
1312
844f90fb 1313wxString wxFileName::GetFullName() const
a35b27b1 1314{
844f90fb
VZ
1315 wxString fullname = m_name;
1316 if ( !m_ext.empty() )
a35b27b1 1317 {
9e8d8607 1318 fullname << wxFILE_SEP_EXT << m_ext;
a35b27b1 1319 }
a35b27b1 1320
844f90fb 1321 return fullname;
a35b27b1
RR
1322}
1323
33b97389 1324wxString wxFileName::GetPath( int flags, wxPathFormat format ) const
df5ddbca
RR
1325{
1326 format = GetFormat( format );
844f90fb 1327
353f41cb
RR
1328 wxString fullpath;
1329
33b97389
VZ
1330 // return the volume with the path as well if requested
1331 if ( flags & wxPATH_GET_VOLUME )
1332 {
1333 fullpath += wxGetVolumeString(GetVolume(), format);
1334 }
1335
034742fc
VZ
1336 // the leading character
1337 switch ( format )
1338 {
1339 case wxPATH_MAC:
1340 if ( m_relative )
1341 fullpath += wxFILE_SEP_PATH_MAC;
1342 break;
1343
1344 case wxPATH_DOS:
1345 if ( !m_relative )
1346 fullpath += wxFILE_SEP_PATH_DOS;
1347 break;
1348
1349 default:
1350 wxFAIL_MSG( wxT("Unknown path format") );
1351 // fall through
1352
1353 case wxPATH_UNIX:
1354 if ( !m_relative )
1355 {
1356 // normally the absolute file names start with a slash
1357 // with one exception: the ones like "~/foo.bar" don't
1358 // have it
9b3a3820 1359 if ( m_dirs.IsEmpty() || m_dirs[0u] != _T('~') )
034742fc
VZ
1360 {
1361 fullpath += wxFILE_SEP_PATH_UNIX;
1362 }
1363 }
1364 break;
1365
1366 case wxPATH_VMS:
1367 // no leading character here but use this place to unset
1368 // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense
1369 // as, if I understand correctly, there should never be a dot
1370 // before the closing bracket
1371 flags &= ~wxPATH_GET_SEPARATOR;
1372 }
1373
1374 if ( m_dirs.empty() )
1375 {
1376 // there is nothing more
1377 return fullpath;
1378 }
1379
1380 // then concatenate all the path components using the path separator
1381 if ( format == wxPATH_VMS )
1382 {
1383 fullpath += wxT('[');
1384 }
1385
5bb9aeb2 1386 const size_t dirCount = m_dirs.GetCount();
034742fc 1387 for ( size_t i = 0; i < dirCount; i++ )
353f41cb 1388 {
034742fc 1389 switch (format)
5bb9aeb2
VZ
1390 {
1391 case wxPATH_MAC:
034742fc
VZ
1392 if ( m_dirs[i] == wxT(".") )
1393 {
1394 // skip appending ':', this shouldn't be done in this
1395 // case as "::" is interpreted as ".." under Unix
1396 continue;
1397 }
2361ce82 1398
034742fc
VZ
1399 // convert back from ".." to nothing
1400 if ( m_dirs[i] != wxT("..") )
1401 fullpath += m_dirs[i];
5bb9aeb2 1402 break;
2361ce82 1403
5bb9aeb2 1404 default:
034742fc
VZ
1405 wxFAIL_MSG( wxT("Unexpected path format") );
1406 // still fall through
2361ce82 1407
034742fc 1408 case wxPATH_DOS:
5bb9aeb2 1409 case wxPATH_UNIX:
034742fc 1410 fullpath += m_dirs[i];
5bb9aeb2 1411 break;
2361ce82 1412
5bb9aeb2 1413 case wxPATH_VMS:
034742fc 1414 // TODO: What to do with ".." under VMS
353f41cb 1415
034742fc
VZ
1416 // convert back from ".." to nothing
1417 if ( m_dirs[i] != wxT("..") )
353f41cb 1418 fullpath += m_dirs[i];
034742fc 1419 break;
4175794e
VZ
1420 }
1421
034742fc
VZ
1422 if ( (flags & wxPATH_GET_SEPARATOR) || (i != dirCount - 1) )
1423 fullpath += GetPathSeparator(format);
df5ddbca 1424 }
034742fc
VZ
1425
1426 if ( format == wxPATH_VMS )
5bb9aeb2 1427 {
034742fc 1428 fullpath += wxT(']');
5bb9aeb2 1429 }
844f90fb 1430
353f41cb 1431 return fullpath;
df5ddbca
RR
1432}
1433
1434wxString wxFileName::GetFullPath( wxPathFormat format ) const
1435{
4175794e
VZ
1436 // we already have a function to get the path
1437 wxString fullpath = GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR,
1438 format);
04c943b1 1439
4175794e 1440 // now just add the file name and extension to it
04c943b1
VZ
1441 fullpath += GetFullName();
1442
1443 return fullpath;
df5ddbca
RR
1444}
1445
9e9b65c1
JS
1446// Return the short form of the path (returns identity on non-Windows platforms)
1447wxString wxFileName::GetShortPath() const
1448{
1c193821 1449#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
9e9b65c1 1450 wxString path(GetFullPath());
75ef5722
JS
1451 wxString pathOut;
1452 DWORD sz = ::GetShortPathName(path, NULL, 0);
1453 bool ok = sz != 0;
1454 if ( ok )
9e9b65c1 1455 {
75ef5722
JS
1456 ok = ::GetShortPathName
1457 (
1458 path,
de564874 1459 wxStringBuffer(pathOut, sz),
75ef5722
JS
1460 sz
1461 ) != 0;
9e9b65c1 1462 }
75ef5722
JS
1463 if (ok)
1464 return pathOut;
5716a1ab
VZ
1465
1466 return path;
9e9b65c1
JS
1467#else
1468 return GetFullPath();
1469#endif
1470}
1471
1472// Return the long form of the path (returns identity on non-Windows platforms)
1473wxString wxFileName::GetLongPath() const
1474{
52dbd056
VZ
1475 wxString pathOut,
1476 path = GetFullPath();
1477
1478#if defined(__WIN32__) && !defined(__WXMICROWIN__)
f363e05c 1479 bool success = false;
05e7001c 1480
35a6691a 1481#if wxUSE_DYNAMIC_LOADER
62dcaed6 1482 typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
05e7001c 1483
f363e05c 1484 static bool s_triedToLoad = false;
05e7001c
JS
1485
1486 if ( !s_triedToLoad )
9e9b65c1 1487 {
2361ce82
VZ
1488 // suppress the errors about missing GetLongPathName[AW]
1489 wxLogNull noLog;
1490
f363e05c 1491 s_triedToLoad = true;
4f89dbc4
RL
1492 wxDynamicLibrary dllKernel(_T("kernel32"));
1493 if ( dllKernel.IsLoaded() )
05e7001c
JS
1494 {
1495 // may succeed or fail depending on the Windows version
04c943b1 1496 static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
5d978d07 1497#ifdef _UNICODE
4f89dbc4 1498 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameW"));
5d978d07 1499#else
4f89dbc4 1500 s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameA"));
5d978d07 1501#endif
05e7001c 1502
05e7001c
JS
1503 if ( s_pfnGetLongPathName )
1504 {
1505 DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0);
1506 bool ok = dwSize > 0;
1507
1508 if ( ok )
1509 {
1510 DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0);
1511 ok = sz != 0;
1512 if ( ok )
1513 {
1514 ok = (*s_pfnGetLongPathName)
1515 (
1516 path,
de564874 1517 wxStringBuffer(pathOut, sz),
05e7001c
JS
1518 sz
1519 ) != 0;
f363e05c 1520 success = true;
05e7001c
JS
1521 }
1522 }
1523 }
1524 }
9e9b65c1 1525 }
2361ce82 1526
05e7001c 1527 if (success)
75ef5722 1528 return pathOut;
0b9ab0bd 1529#endif // wxUSE_DYNAMIC_LOADER
05e7001c
JS
1530
1531 if (!success)
1532 {
1533 // The OS didn't support GetLongPathName, or some other error.
1534 // We need to call FindFirstFile on each component in turn.
1535
1536 WIN32_FIND_DATA findFileData;
1537 HANDLE hFind;
b5b62eea
JS
1538
1539 if ( HasVolume() )
1540 pathOut = GetVolume() +
1541 GetVolumeSeparator(wxPATH_DOS) +
1542 GetPathSeparator(wxPATH_DOS);
1543 else
1544 pathOut = wxEmptyString;
05e7001c 1545
77fe02a8 1546 wxArrayString dirs = GetDirs();
bcbe86e5 1547 dirs.Add(GetFullName());
77fe02a8 1548
05e7001c 1549 wxString tmpPath;
5d978d07 1550
52dbd056
VZ
1551 size_t count = dirs.GetCount();
1552 for ( size_t i = 0; i < count; i++ )
05e7001c 1553 {
52dbd056
VZ
1554 // We're using pathOut to collect the long-name path, but using a
1555 // temporary for appending the last path component which may be
1556 // short-name
77fe02a8 1557 tmpPath = pathOut + dirs[i];
05e7001c 1558
52dbd056
VZ
1559 if ( tmpPath.empty() )
1560 continue;
1561
b5b62eea
JS
1562 // can't see this being necessary? MF
1563 if ( tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) )
5d978d07
JS
1564 {
1565 // Can't pass a drive and root dir to FindFirstFile,
1566 // so continue to next dir
05e7001c 1567 tmpPath += wxFILE_SEP_PATH;
5d978d07
JS
1568 pathOut = tmpPath;
1569 continue;
1570 }
05e7001c
JS
1571
1572 hFind = ::FindFirstFile(tmpPath, &findFileData);
1573 if (hFind == INVALID_HANDLE_VALUE)
1574 {
b5b62eea
JS
1575 // Error: most likely reason is that path doesn't exist, so
1576 // append any unprocessed parts and return
1577 for ( i += 1; i < count; i++ )
1578 tmpPath += wxFILE_SEP_PATH + dirs[i];
1579
1580 return tmpPath;
05e7001c 1581 }
05e7001c 1582
52dbd056
VZ
1583 pathOut += findFileData.cFileName;
1584 if ( (i < (count-1)) )
1585 pathOut += wxFILE_SEP_PATH;
1586
1587 ::FindClose(hFind);
05e7001c
JS
1588 }
1589 }
52dbd056
VZ
1590#else // !Win32
1591 pathOut = path;
1592#endif // Win32/!Win32
5716a1ab 1593
05e7001c 1594 return pathOut;
9e9b65c1
JS
1595}
1596
df5ddbca
RR
1597wxPathFormat wxFileName::GetFormat( wxPathFormat format )
1598{
1599 if (format == wxPATH_NATIVE)
1600 {
5a3912f2 1601#if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__)
df5ddbca 1602 format = wxPATH_DOS;
4d293f8e 1603#elif defined(__WXMAC__) && !defined(__DARWIN__)
04c943b1 1604 format = wxPATH_MAC;
3c621059 1605#elif defined(__VMS)
04c943b1 1606 format = wxPATH_VMS;
844f90fb 1607#else
df5ddbca
RR
1608 format = wxPATH_UNIX;
1609#endif
1610 }
1611 return format;
1612}
a35b27b1 1613
9e8d8607
VZ
1614// ----------------------------------------------------------------------------
1615// path splitting function
1616// ----------------------------------------------------------------------------
1617
6f91bc33 1618/* static */
04c943b1
VZ
1619void wxFileName::SplitPath(const wxString& fullpathWithVolume,
1620 wxString *pstrVolume,
9e8d8607
VZ
1621 wxString *pstrPath,
1622 wxString *pstrName,
1623 wxString *pstrExt,
1624 wxPathFormat format)
1625{
1626 format = GetFormat(format);
1627
04c943b1
VZ
1628 wxString fullpath = fullpathWithVolume;
1629
1630 // under VMS the end of the path is ']', not the path separator used to
1631 // separate the components
ab3edace 1632 wxString sepPath = format == wxPATH_VMS ? wxString(_T(']'))
04c943b1 1633 : GetPathSeparators(format);
9e8d8607 1634
04c943b1
VZ
1635 // special Windows UNC paths hack: transform \\share\path into share:path
1636 if ( format == wxPATH_DOS )
9e8d8607 1637 {
04c943b1
VZ
1638 if ( fullpath.length() >= 4 &&
1639 fullpath[0u] == wxFILE_SEP_PATH_DOS &&
1640 fullpath[1u] == wxFILE_SEP_PATH_DOS )
9e8d8607 1641 {
04c943b1
VZ
1642 fullpath.erase(0, 2);
1643
1644 size_t posFirstSlash = fullpath.find_first_of(sepPath);
1645 if ( posFirstSlash != wxString::npos )
1646 {
1647 fullpath[posFirstSlash] = wxFILE_SEP_DSK;
1648
1649 // UNC paths are always absolute, right? (FIXME)
de564874 1650 fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS);
04c943b1 1651 }
3c621059
JJ
1652 }
1653 }
04c943b1 1654
a385b5df
RR
1655 // We separate the volume here
1656 if ( format == wxPATH_DOS || format == wxPATH_VMS )
04c943b1 1657 {
a385b5df 1658 wxString sepVol = GetVolumeSeparator(format);
24eb81cb 1659
04c943b1
VZ
1660 size_t posFirstColon = fullpath.find_first_of(sepVol);
1661 if ( posFirstColon != wxString::npos )
1662 {
1663 if ( pstrVolume )
1664 {
1665 *pstrVolume = fullpath.Left(posFirstColon);
1666 }
1667
1668 // remove the volume name and the separator from the full path
1669 fullpath.erase(0, posFirstColon + sepVol.length());
1670 }
1671 }
1672
1673 // find the positions of the last dot and last path separator in the path
1674 size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT);
1675 size_t posLastSlash = fullpath.find_last_of(sepPath);
1676
1677 if ( (posLastDot != wxString::npos) &&
1678 ((format == wxPATH_UNIX) || (format == wxPATH_VMS)) )
3c621059
JJ
1679 {
1680 if ( (posLastDot == 0) ||
04c943b1 1681 (fullpath[posLastDot - 1] == sepPath[0u] ) )
3c621059 1682 {
04c943b1
VZ
1683 // under Unix and VMS, dot may be (and commonly is) the first
1684 // character of the filename, don't treat the entire filename as
1685 // extension in this case
9e8d8607
VZ
1686 posLastDot = wxString::npos;
1687 }
1688 }
1689
8e7dda21
VZ
1690 // if we do have a dot and a slash, check that the dot is in the name part
1691 if ( (posLastDot != wxString::npos) &&
1692 (posLastSlash != wxString::npos) &&
1693 (posLastDot < posLastSlash) )
9e8d8607
VZ
1694 {
1695 // the dot is part of the path, not the start of the extension
1696 posLastDot = wxString::npos;
1697 }
1698
1699 // now fill in the variables provided by user
1700 if ( pstrPath )
1701 {
1702 if ( posLastSlash == wxString::npos )
1703 {
1704 // no path at all
1705 pstrPath->Empty();
1706 }
1707 else
1708 {
04c943b1 1709 // take everything up to the path separator but take care to make
353f41cb 1710 // the path equal to something like '/', not empty, for the files
04c943b1
VZ
1711 // immediately under root directory
1712 size_t len = posLastSlash;
91b4bd63
SC
1713
1714 // this rule does not apply to mac since we do not start with colons (sep)
1715 // except for relative paths
1716 if ( !len && format != wxPATH_MAC)
04c943b1
VZ
1717 len++;
1718
1719 *pstrPath = fullpath.Left(len);
1720
1721 // special VMS hack: remove the initial bracket
1722 if ( format == wxPATH_VMS )
1723 {
1724 if ( (*pstrPath)[0u] == _T('[') )
1725 pstrPath->erase(0, 1);
1726 }
9e8d8607
VZ
1727 }
1728 }
1729
1730 if ( pstrName )
1731 {
42b1f941
VZ
1732 // take all characters starting from the one after the last slash and
1733 // up to, but excluding, the last dot
9e8d8607 1734 size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
8e7dda21
VZ
1735 size_t count;
1736 if ( posLastDot == wxString::npos )
1737 {
1738 // take all until the end
1739 count = wxString::npos;
1740 }
1741 else if ( posLastSlash == wxString::npos )
1742 {
1743 count = posLastDot;
1744 }
1745 else // have both dot and slash
1746 {
1747 count = posLastDot - posLastSlash - 1;
1748 }
9e8d8607
VZ
1749
1750 *pstrName = fullpath.Mid(nStart, count);
1751 }
1752
1753 if ( pstrExt )
1754 {
1755 if ( posLastDot == wxString::npos )
1756 {
1757 // no extension
1758 pstrExt->Empty();
1759 }
1760 else
1761 {
1762 // take everything after the dot
1763 *pstrExt = fullpath.Mid(posLastDot + 1);
1764 }
1765 }
1766}
951cd180 1767
6f91bc33
VZ
1768/* static */
1769void wxFileName::SplitPath(const wxString& fullpath,
1770 wxString *path,
1771 wxString *name,
1772 wxString *ext,
1773 wxPathFormat format)
1774{
1775 wxString volume;
1776 SplitPath(fullpath, &volume, path, name, ext, format);
1777
67c34f64 1778 if ( path )
6f91bc33 1779 {
67c34f64 1780 path->Prepend(wxGetVolumeString(volume, format));
6f91bc33
VZ
1781 }
1782}
1783
951cd180
VZ
1784// ----------------------------------------------------------------------------
1785// time functions
1786// ----------------------------------------------------------------------------
1787
e2b87f38
VZ
1788#if wxUSE_DATETIME
1789
6dbb903b
VZ
1790bool wxFileName::SetTimes(const wxDateTime *dtAccess,
1791 const wxDateTime *dtMod,
1792 const wxDateTime *dtCreate)
951cd180 1793{
2b5f62a0
VZ
1794#if defined(__WIN32__)
1795 if ( IsDir() )
1796 {
1797 // VZ: please let me know how to do this if you can
1798 wxFAIL_MSG( _T("SetTimes() not implemented for the directories") );
1799 }
1800 else // file
1801 {
1802 wxFileHandle fh(GetFullPath(), wxFileHandle::Write);
1803 if ( fh.IsOk() )
1804 {
1805 FILETIME ftAccess, ftCreate, ftWrite;
1806
1807 if ( dtCreate )
1808 ConvertWxToFileTime(&ftCreate, *dtCreate);
1809 if ( dtAccess )
1810 ConvertWxToFileTime(&ftAccess, *dtAccess);
1811 if ( dtMod )
1812 ConvertWxToFileTime(&ftWrite, *dtMod);
1813
1814 if ( ::SetFileTime(fh,
1815 dtCreate ? &ftCreate : NULL,
1816 dtAccess ? &ftAccess : NULL,
1817 dtMod ? &ftWrite : NULL) )
1818 {
f363e05c 1819 return true;
2b5f62a0
VZ
1820 }
1821 }
1822 }
1823#elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
246c704f
VZ
1824 if ( !dtAccess && !dtMod )
1825 {
1826 // can't modify the creation time anyhow, don't try
f363e05c 1827 return true;
246c704f
VZ
1828 }
1829
1830 // if dtAccess or dtMod is not specified, use the other one (which must be
1831 // non NULL because of the test above) for both times
951cd180 1832 utimbuf utm;
246c704f
VZ
1833 utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks();
1834 utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks();
401eb3de 1835 if ( utime(GetFullPath().fn_str(), &utm) == 0 )
951cd180 1836 {
f363e05c 1837 return true;
951cd180 1838 }
951cd180
VZ
1839#else // other platform
1840#endif // platforms
1841
1842 wxLogSysError(_("Failed to modify file times for '%s'"),
1843 GetFullPath().c_str());
1844
f363e05c 1845 return false;
951cd180
VZ
1846}
1847
1848bool wxFileName::Touch()
1849{
1850#if defined(__UNIX_LIKE__)
1851 // under Unix touching file is simple: just pass NULL to utime()
401eb3de 1852 if ( utime(GetFullPath().fn_str(), NULL) == 0 )
951cd180 1853 {
f363e05c 1854 return true;
951cd180
VZ
1855 }
1856
1857 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1858
f363e05c 1859 return false;
951cd180
VZ
1860#else // other platform
1861 wxDateTime dtNow = wxDateTime::Now();
1862
6dbb903b 1863 return SetTimes(&dtNow, &dtNow, NULL /* don't change create time */);
951cd180
VZ
1864#endif // platforms
1865}
1866
1867bool wxFileName::GetTimes(wxDateTime *dtAccess,
1868 wxDateTime *dtMod,
6dbb903b 1869 wxDateTime *dtCreate) const
951cd180 1870{
2b5f62a0
VZ
1871#if defined(__WIN32__)
1872 // we must use different methods for the files and directories under
1873 // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and
1874 // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and
1875 // not 9x
1876 bool ok;
1877 FILETIME ftAccess, ftCreate, ftWrite;
1878 if ( IsDir() )
1879 {
1880 // implemented in msw/dir.cpp
1881 extern bool wxGetDirectoryTimes(const wxString& dirname,
1882 FILETIME *, FILETIME *, FILETIME *);
1883
1884 // we should pass the path without the trailing separator to
1885 // wxGetDirectoryTimes()
1886 ok = wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME),
1887 &ftAccess, &ftCreate, &ftWrite);
1888 }
1889 else // file
1890 {
1891 wxFileHandle fh(GetFullPath(), wxFileHandle::Read);
1892 if ( fh.IsOk() )
1893 {
1894 ok = ::GetFileTime(fh,
1895 dtCreate ? &ftCreate : NULL,
1896 dtAccess ? &ftAccess : NULL,
1897 dtMod ? &ftWrite : NULL) != 0;
1898 }
1899 else
1900 {
f363e05c 1901 ok = false;
2b5f62a0
VZ
1902 }
1903 }
1904
1905 if ( ok )
1906 {
1907 if ( dtCreate )
1908 ConvertFileTimeToWx(dtCreate, ftCreate);
1909 if ( dtAccess )
1910 ConvertFileTimeToWx(dtAccess, ftAccess);
1911 if ( dtMod )
1912 ConvertFileTimeToWx(dtMod, ftWrite);
1913
f363e05c 1914 return true;
2b5f62a0
VZ
1915 }
1916#elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
951cd180 1917 wxStructStat stBuf;
92980e90 1918 if ( wxStat( GetFullPath().c_str(), &stBuf) == 0 )
951cd180
VZ
1919 {
1920 if ( dtAccess )
1921 dtAccess->Set(stBuf.st_atime);
1922 if ( dtMod )
1923 dtMod->Set(stBuf.st_mtime);
6dbb903b
VZ
1924 if ( dtCreate )
1925 dtCreate->Set(stBuf.st_ctime);
951cd180 1926
f363e05c 1927 return true;
951cd180 1928 }
951cd180
VZ
1929#else // other platform
1930#endif // platforms
1931
1932 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1933 GetFullPath().c_str());
1934
f363e05c 1935 return false;
951cd180
VZ
1936}
1937
e2b87f38
VZ
1938#endif // wxUSE_DATETIME
1939
4dfbcdd5
SC
1940#ifdef __WXMAC__
1941
1942const short kMacExtensionMaxLength = 16 ;
0ca4ae93 1943class MacDefaultExtensionRecord
4dfbcdd5 1944{
0ca4ae93
SC
1945public :
1946 MacDefaultExtensionRecord()
1947 {
1948 m_ext[0] = 0 ;
1949 m_type = m_creator = NULL ;
1950 }
1951 MacDefaultExtensionRecord( const MacDefaultExtensionRecord& from )
1952 {
44c44c82 1953 wxStrcpy( m_ext , from.m_ext ) ;
0ca4ae93
SC
1954 m_type = from.m_type ;
1955 m_creator = from.m_creator ;
1956 }
44c44c82 1957 MacDefaultExtensionRecord( const wxChar * extension , OSType type , OSType creator )
0ca4ae93 1958 {
44c44c82 1959 wxStrncpy( m_ext , extension , kMacExtensionMaxLength ) ;
0ca4ae93
SC
1960 m_ext[kMacExtensionMaxLength] = 0 ;
1961 m_type = type ;
1962 m_creator = creator ;
1963 }
44c44c82 1964 wxChar m_ext[kMacExtensionMaxLength] ;
4dfbcdd5
SC
1965 OSType m_type ;
1966 OSType m_creator ;
0ca4ae93 1967} ;
4dfbcdd5
SC
1968
1969#include "wx/dynarray.h"
1970WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray) ;
0ca4ae93
SC
1971
1972bool gMacDefaultExtensionsInited = false ;
1973
4dfbcdd5 1974#include "wx/arrimpl.cpp"
0ca4ae93
SC
1975
1976WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray) ;
4dfbcdd5
SC
1977
1978MacDefaultExtensionArray gMacDefaultExtensions ;
4dfbcdd5 1979
5974c3cf
SC
1980// load the default extensions
1981MacDefaultExtensionRecord gDefaults[] =
4dfbcdd5 1982{
5974c3cf
SC
1983 MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) ,
1984 MacDefaultExtensionRecord( wxT("tif") , 'TIFF' , '****' ) ,
1985 MacDefaultExtensionRecord( wxT("jpg") , 'JPEG' , '****' ) ,
1986} ;
0ea621cc 1987
5974c3cf
SC
1988static void MacEnsureDefaultExtensionsLoaded()
1989{
1990 if ( !gMacDefaultExtensionsInited )
4dfbcdd5 1991 {
5974c3cf
SC
1992 // we could load the pc exchange prefs here too
1993 for ( size_t i = 0 ; i < WXSIZEOF( gDefaults ) ; ++i )
1994 {
1995 gMacDefaultExtensions.Add( gDefaults[i] ) ;
1996 }
1997 gMacDefaultExtensionsInited = true ;
0ea621cc 1998 }
4dfbcdd5 1999}
0ea621cc 2000bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator )
4dfbcdd5
SC
2001{
2002 FInfo fndrInfo ;
2003 FSSpec spec ;
2004 wxMacFilename2FSSpec(GetFullPath(),&spec) ;
2005 OSErr err = FSpGetFInfo( &spec , &fndrInfo ) ;
2006 wxCHECK( err == noErr , false ) ;
2007
2008 fndrInfo.fdType = type ;
2009 fndrInfo.fdCreator = creator ;
2010 FSpSetFInfo( &spec , &fndrInfo ) ;
2011 return true ;
2012}
2013
0ea621cc 2014bool wxFileName::MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator )
4dfbcdd5
SC
2015{
2016 FInfo fndrInfo ;
2017 FSSpec spec ;
2018 wxMacFilename2FSSpec(GetFullPath(),&spec) ;
2019 OSErr err = FSpGetFInfo( &spec , &fndrInfo ) ;
2020 wxCHECK( err == noErr , false ) ;
2021
2022 *type = fndrInfo.fdType ;
2023 *creator = fndrInfo.fdCreator ;
2024 return true ;
2025}
2026
2027bool wxFileName::MacSetDefaultTypeAndCreator()
2028{
2029 wxUint32 type , creator ;
2030 if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type ,
2031 &creator ) )
2032 {
f63fb56d
GD
2033 return MacSetTypeAndCreator( type , creator ) ;
2034 }
2035 return false;
4dfbcdd5
SC
2036}
2037
0ea621cc 2038bool wxFileName::MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator )
4dfbcdd5
SC
2039{
2040 MacEnsureDefaultExtensionsLoaded() ;
2041 wxString extl = ext.Lower() ;
2042 for( int i = gMacDefaultExtensions.Count() - 1 ; i >= 0 ; --i )
2043 {
2044 if ( gMacDefaultExtensions.Item(i).m_ext == extl )
2045 {
2046 *type = gMacDefaultExtensions.Item(i).m_type ;
2047 *creator = gMacDefaultExtensions.Item(i).m_creator ;
2048 return true ;
2049 }
2050 }
2051 return false ;
2052}
2053
2054void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator )
2055{
2056 MacEnsureDefaultExtensionsLoaded() ;
2057 MacDefaultExtensionRecord rec ;
2058 rec.m_type = type ;
2059 rec.m_creator = creator ;
44c44c82 2060 wxStrncpy( rec.m_ext , ext.Lower().c_str() , kMacExtensionMaxLength ) ;
4dfbcdd5
SC
2061 gMacDefaultExtensions.Add( rec ) ;
2062}
2063#endif