]> git.saurik.com Git - wxWidgets.git/blame - src/common/filename.cpp
fix the definition of many static functions marked as 'const' or functions which...
[wxWidgets.git] / src / common / filename.cpp
CommitLineData
df5ddbca 1/////////////////////////////////////////////////////////////////////////////
097ead30
VZ
2// Name: src/common/filename.cpp
3// Purpose: wxFileName - encapsulates a file path
844f90fb 4// Author: Robert Roebling, Vadim Zeitlin
df5ddbca
RR
5// Modified by:
6// Created: 28.12.2000
7// RCS-ID: $Id$
8// Copyright: (c) 2000 Robert Roebling
65571936 9// Licence: wxWindows licence
df5ddbca
RR
10/////////////////////////////////////////////////////////////////////////////
11
04c943b1
VZ
12/*
13 Here are brief descriptions of the filename formats supported by this class:
14
2bc60417
RR
15 wxPATH_UNIX: standard Unix format, used under Darwin as well, absolute file
16 names have the form:
04c943b1
VZ
17 /dir1/dir2/.../dirN/filename, "." and ".." stand for the
18 current and parent directory respectively, "~" is parsed as the
19 user HOME and "~username" as the HOME of that user
20
2bc60417 21 wxPATH_DOS: DOS/Windows format, absolute file names have the form:
04c943b1
VZ
22 drive:\dir1\dir2\...\dirN\filename.ext where drive is a single
23 letter. "." and ".." as for Unix but no "~".
24
25 There are also UNC names of the form \\share\fullpath
26
ae4d7004 27 wxPATH_MAC: Mac OS 8/9 and Mac OS X under CodeWarrior 7 format, absolute file
2bc60417 28 names have the form
04c943b1
VZ
29 volume:dir1:...:dirN:filename
30 and the relative file names are either
31 :dir1:...:dirN:filename
32 or just
33 filename
34 (although :filename works as well).
a385b5df 35 Since the volume is just part of the file path, it is not
353f41cb 36 treated like a separate entity as it is done under DOS and
90a68369 37 VMS, it is just treated as another dir.
04c943b1
VZ
38
39 wxPATH_VMS: VMS native format, absolute file names have the form
40 <device>:[dir1.dir2.dir3]file.txt
41 or
42 <device>:[000000.dir1.dir2.dir3]file.txt
43
44 the <device> is the physical device (i.e. disk). 000000 is the
45 root directory on the device which can be omitted.
46
47 Note that VMS uses different separators unlike Unix:
48 : always after the device. If the path does not contain : than
49 the default (the device of the current directory) is assumed.
ba623ba2 50 [ start of directory specification
04c943b1
VZ
51 . separator between directory and subdirectory
52 ] between directory and file
53 */
54
097ead30
VZ
55// ============================================================================
56// declarations
57// ============================================================================
58
59// ----------------------------------------------------------------------------
60// headers
61// ----------------------------------------------------------------------------
62
df5ddbca
RR
63// For compilers that support precompilation, includes "wx.h".
64#include "wx/wxprec.h"
65
66#ifdef __BORLANDC__
0b9ab0bd 67#pragma hdrstop
df5ddbca
RR
68#endif
69
70#ifndef WX_PRECOMP
57bd4c60
WS
71 #ifdef __WXMSW__
72 #include "wx/msw/wrapwin.h" // For GetShort/LongPathName
73 #endif
ad9835c9
WS
74 #include "wx/dynarray.h"
75 #include "wx/intl.h"
76 #include "wx/log.h"
de6185e2 77 #include "wx/utils.h"
0bf751e7 78 #include "wx/crt.h"
df5ddbca
RR
79#endif
80
81#include "wx/filename.h"
b70a2866 82#include "wx/private/filename.h"
df5ddbca 83#include "wx/tokenzr.h"
844f90fb 84#include "wx/config.h" // for wxExpandEnvVars
35a6691a 85#include "wx/dynlib.h"
df5ddbca 86
57bd4c60
WS
87#if defined(__WIN32__) && defined(__MINGW32__)
88 #include "wx/msw/gccpriv.h"
951cd180
VZ
89#endif
90
1c193821
JS
91#ifdef __WXWINCE__
92#include "wx/msw/private.h"
93#endif
94
76a5e5d2 95#if defined(__WXMAC__)
c933e267 96 #include "wx/osx/private.h" // includes mac headers
76a5e5d2
SC
97#endif
98
951cd180
VZ
99// utime() is POSIX so should normally be available on all Unices
100#ifdef __UNIX_LIKE__
0b9ab0bd
RL
101#include <sys/types.h>
102#include <utime.h>
103#include <sys/stat.h>
104#include <unistd.h>
9e9b65c1
JS
105#endif
106
444d61ba
VS
107#ifdef __DJGPP__
108#include <unistd.h>
109#endif
110
01d981ec 111#ifdef __MWERKS__
31907d03
SC
112#ifdef __MACH__
113#include <sys/types.h>
114#include <utime.h>
115#include <sys/stat.h>
116#include <unistd.h>
117#else
0b9ab0bd
RL
118#include <stat.h>
119#include <unistd.h>
120#include <unix.h>
01d981ec 121#endif
31907d03 122#endif
01d981ec 123
d9f54bb0 124#ifdef __WATCOMC__
0b9ab0bd
RL
125#include <io.h>
126#include <sys/utime.h>
127#include <sys/stat.h>
d9f54bb0
VS
128#endif
129
24eb81cb
DW
130#ifdef __VISAGECPP__
131#ifndef MAX_PATH
132#define MAX_PATH 256
133#endif
134#endif
135
01c9a906 136#ifdef __EMX__
5d66debf 137#include <os2.h>
01c9a906
SN
138#define MAX_PATH _MAX_PATH
139#endif
140
23b8a262 141
bd08f2f7 142#if wxUSE_LONGLONG
060668a1 143extern const wxULongLong wxInvalidSize = (unsigned)-1;
bd08f2f7 144#endif // wxUSE_LONGLONG
23b8a262
JS
145
146
951cd180
VZ
147// ----------------------------------------------------------------------------
148// private classes
149// ----------------------------------------------------------------------------
150
151// small helper class which opens and closes the file - we use it just to get
152// a file handle for the given file name to pass it to some Win32 API function
c67d6888 153#if defined(__WIN32__) && !defined(__WXMICROWIN__)
951cd180
VZ
154
155class wxFileHandle
156{
157public:
6dbb903b
VZ
158 enum OpenMode
159 {
160 Read,
161 Write
162 };
163
164 wxFileHandle(const wxString& filename, OpenMode mode)
951cd180
VZ
165 {
166 m_hFile = ::CreateFile
167 (
e0a050e3 168 filename.fn_str(), // name
0ea621cc 169 mode == Read ? GENERIC_READ // access mask
6dbb903b 170 : GENERIC_WRITE,
54bcff35
VZ
171 FILE_SHARE_READ | // sharing mode
172 FILE_SHARE_WRITE, // (allow everything)
6dbb903b
VZ
173 NULL, // no secutity attr
174 OPEN_EXISTING, // creation disposition
175 0, // no flags
176 NULL // no template file
951cd180
VZ
177 );
178
179 if ( m_hFile == INVALID_HANDLE_VALUE )
180 {
49a70977
VS
181 if ( mode == Read )
182 wxLogSysError(_("Failed to open '%s' for reading"),
183 filename.c_str());
184 else
185 wxLogSysError(_("Failed to open '%s' for writing"),
186 filename.c_str());
951cd180
VZ
187 }
188 }
189
190 ~wxFileHandle()
191 {
192 if ( m_hFile != INVALID_HANDLE_VALUE )
193 {
194 if ( !::CloseHandle(m_hFile) )
195 {
196 wxLogSysError(_("Failed to close file handle"));
197 }
198 }
199 }
200
f363e05c 201 // return true only if the file could be opened successfully
951cd180
VZ
202 bool IsOk() const { return m_hFile != INVALID_HANDLE_VALUE; }
203
204 // get the handle
205 operator HANDLE() const { return m_hFile; }
206
207private:
208 HANDLE m_hFile;
209};
210
211#endif // __WIN32__
212
213// ----------------------------------------------------------------------------
214// private functions
215// ----------------------------------------------------------------------------
216
e2b87f38 217#if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
951cd180
VZ
218
219// convert between wxDateTime and FILETIME which is a 64-bit value representing
220// the number of 100-nanosecond intervals since January 1, 1601.
221
d56e2b97 222static void ConvertFileTimeToWx(wxDateTime *dt, const FILETIME &ft)
951cd180 223{
752f10a8 224 FILETIME ftcopy = ft;
6dbb903b 225 FILETIME ftLocal;
752f10a8 226 if ( !::FileTimeToLocalFileTime(&ftcopy, &ftLocal) )
6dbb903b
VZ
227 {
228 wxLogLastError(_T("FileTimeToLocalFileTime"));
229 }
d56e2b97 230
6dbb903b
VZ
231 SYSTEMTIME st;
232 if ( !::FileTimeToSystemTime(&ftLocal, &st) )
233 {
234 wxLogLastError(_T("FileTimeToSystemTime"));
235 }
d56e2b97 236
6dbb903b
VZ
237 dt->Set(st.wDay, wxDateTime::Month(st.wMonth - 1), st.wYear,
238 st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
951cd180
VZ
239}
240
d56e2b97 241static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt)
951cd180 242{
6dbb903b
VZ
243 SYSTEMTIME st;
244 st.wDay = dt.GetDay();
ad816476
WS
245 st.wMonth = (WORD)(dt.GetMonth() + 1);
246 st.wYear = (WORD)dt.GetYear();
6dbb903b
VZ
247 st.wHour = dt.GetHour();
248 st.wMinute = dt.GetMinute();
249 st.wSecond = dt.GetSecond();
250 st.wMilliseconds = dt.GetMillisecond();
251
252 FILETIME ftLocal;
253 if ( !::SystemTimeToFileTime(&st, &ftLocal) )
254 {
255 wxLogLastError(_T("SystemTimeToFileTime"));
256 }
d56e2b97 257
6dbb903b
VZ
258 if ( !::LocalFileTimeToFileTime(&ftLocal, ft) )
259 {
260 wxLogLastError(_T("LocalFileTimeToFileTime"));
261 }
951cd180
VZ
262}
263
e2b87f38 264#endif // wxUSE_DATETIME && __WIN32__
951cd180 265
67c34f64
VZ
266// return a string with the volume par
267static wxString wxGetVolumeString(const wxString& volume, wxPathFormat format)
268{
269 wxString path;
270
271 if ( !volume.empty() )
272 {
6ce27aa2
VZ
273 format = wxFileName::GetFormat(format);
274
67c34f64
VZ
275 // Special Windows UNC paths hack, part 2: undo what we did in
276 // SplitPath() and make an UNC path if we have a drive which is not a
277 // single letter (hopefully the network shares can't be one letter only
278 // although I didn't find any authoritative docs on this)
279 if ( format == wxPATH_DOS && volume.length() > 1 )
280 {
281 path << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_DOS << volume;
282 }
283 else if ( format == wxPATH_DOS || format == wxPATH_VMS )
284 {
285 path << volume << wxFileName::GetVolumeSeparator(format);
286 }
287 // else ignore
288 }
289
290 return path;
291}
292
9e1c7236
VZ
293// return true if the format used is the DOS/Windows one and the string looks
294// like a UNC path
295static bool IsUNCPath(const wxString& path, wxPathFormat format)
296{
297 return format == wxPATH_DOS &&
298 path.length() >= 4 && // "\\a" can't be a UNC path
299 path[0u] == wxFILE_SEP_PATH_DOS &&
300 path[1u] == wxFILE_SEP_PATH_DOS &&
301 path[2u] != wxFILE_SEP_PATH_DOS;
302}
303
097ead30
VZ
304// ============================================================================
305// implementation
306// ============================================================================
307
308// ----------------------------------------------------------------------------
844f90fb 309// wxFileName construction
097ead30 310// ----------------------------------------------------------------------------
df5ddbca 311
a35b27b1
RR
312void wxFileName::Assign( const wxFileName &filepath )
313{
04c943b1 314 m_volume = filepath.GetVolume();
844f90fb 315 m_dirs = filepath.GetDirs();
04c943b1
VZ
316 m_name = filepath.GetName();
317 m_ext = filepath.GetExt();
a2fa5040 318 m_relative = filepath.m_relative;
dfecbee5 319 m_hasExt = filepath.m_hasExt;
df5ddbca
RR
320}
321
04c943b1
VZ
322void wxFileName::Assign(const wxString& volume,
323 const wxString& path,
324 const wxString& name,
325 const wxString& ext,
dfecbee5 326 bool hasExt,
9e1c7236 327 wxPathFormat format)
a7b51bc8 328{
9e1c7236
VZ
329 // we should ignore paths which look like UNC shares because we already
330 // have the volume here and the UNC notation (\\server\path) is only valid
331 // for paths which don't start with a volume, so prevent SetPath() from
332 // recognizing "\\foo\bar" in "c:\\foo\bar" as an UNC path
333 //
334 // note also that this is a rather ugly way to do what we want (passing
335 // some kind of flag telling to ignore UNC paths to SetPath() would be
336 // better) but this is the safest thing to do to avoid breaking backwards
337 // compatibility in 2.8
338 if ( IsUNCPath(path, format) )
339 {
340 // remove one of the 2 leading backslashes to ensure that it's not
341 // recognized as an UNC path by SetPath()
342 wxString pathNonUNC(path, 1, wxString::npos);
343 SetPath(pathNonUNC, format);
344 }
345 else // no UNC complications
346 {
347 SetPath(path, format);
348 }
a7b51bc8
RR
349
350 m_volume = volume;
351 m_ext = ext;
352 m_name = name;
dfecbee5
VZ
353
354 m_hasExt = hasExt;
a7b51bc8
RR
355}
356
f1e77933 357void wxFileName::SetPath( const wxString& pathOrig, wxPathFormat format )
df5ddbca 358{
844f90fb 359 m_dirs.Clear();
90a68369 360
f1e77933 361 if ( pathOrig.empty() )
df5ddbca 362 {
f1e77933
VZ
363 // no path at all
364 m_relative = true;
a2fa5040 365
f1e77933
VZ
366 return;
367 }
90a68369 368
f1e77933 369 format = GetFormat( format );
a2fa5040 370
f1e77933
VZ
371 // 0) deal with possible volume part first
372 wxString volume,
373 path;
374 SplitVolume(pathOrig, &volume, &path, format);
375 if ( !volume.empty() )
376 {
377 m_relative = false;
a2fa5040 378
f1e77933
VZ
379 SetVolume(volume);
380 }
f363e05c 381
f1e77933
VZ
382 // 1) Determine if the path is relative or absolute.
383 wxChar leadingChar = path[0u];
a2fa5040 384
f1e77933
VZ
385 switch (format)
386 {
387 case wxPATH_MAC:
388 m_relative = leadingChar == wxT(':');
389
390 // We then remove a leading ":". The reason is in our
391 // storage form for relative paths:
392 // ":dir:file.txt" actually means "./dir/file.txt" in
393 // DOS notation and should get stored as
394 // (relative) (dir) (file.txt)
395 // "::dir:file.txt" actually means "../dir/file.txt"
396 // stored as (relative) (..) (dir) (file.txt)
397 // This is important only for the Mac as an empty dir
398 // actually means <UP>, whereas under DOS, double
399 // slashes can be ignored: "\\\\" is the same as "\\".
400 if (m_relative)
401 path.erase( 0, 1 );
402 break;
a2fa5040 403
f1e77933
VZ
404 case wxPATH_VMS:
405 // TODO: what is the relative path format here?
406 m_relative = false;
407 break;
90a68369 408
f1e77933
VZ
409 default:
410 wxFAIL_MSG( _T("Unknown path format") );
411 // !! Fall through !!
90a68369 412
f1e77933
VZ
413 case wxPATH_UNIX:
414 // the paths of the form "~" or "~username" are absolute
415 m_relative = leadingChar != wxT('/') && leadingChar != _T('~');
416 break;
353f41cb 417
f1e77933
VZ
418 case wxPATH_DOS:
419 m_relative = !IsPathSeparator(leadingChar, format);
420 break;
844f90fb 421
df5ddbca 422 }
f1e77933
VZ
423
424 // 2) Break up the path into its members. If the original path
425 // was just "/" or "\\", m_dirs will be empty. We know from
426 // the m_relative field, if this means "nothing" or "root dir".
427
428 wxStringTokenizer tn( path, GetPathSeparators(format) );
429
430 while ( tn.HasMoreTokens() )
a7b51bc8 431 {
f1e77933
VZ
432 wxString token = tn.GetNextToken();
433
434 // Remove empty token under DOS and Unix, interpret them
435 // as .. under Mac.
436 if (token.empty())
437 {
438 if (format == wxPATH_MAC)
439 m_dirs.Add( wxT("..") );
440 // else ignore
441 }
442 else
443 {
444 m_dirs.Add( token );
445 }
a7b51bc8 446 }
844f90fb
VZ
447}
448
449void wxFileName::Assign(const wxString& fullpath,
450 wxPathFormat format)
451{
04c943b1 452 wxString volume, path, name, ext;
dfecbee5
VZ
453 bool hasExt;
454 SplitPath(fullpath, &volume, &path, &name, &ext, &hasExt, format);
844f90fb 455
dfecbee5 456 Assign(volume, path, name, ext, hasExt, format);
844f90fb
VZ
457}
458
81f25632 459void wxFileName::Assign(const wxString& fullpathOrig,
844f90fb
VZ
460 const wxString& fullname,
461 wxPathFormat format)
462{
81f25632
VZ
463 // always recognize fullpath as directory, even if it doesn't end with a
464 // slash
465 wxString fullpath = fullpathOrig;
69858116 466 if ( !fullpath.empty() && !wxEndsWithPathSeparator(fullpath) )
81f25632 467 {
33b97389 468 fullpath += GetPathSeparator(format);
81f25632
VZ
469 }
470
52dbd056 471 wxString volume, path, name, ext;
dfecbee5 472 bool hasExt;
81f25632
VZ
473
474 // do some consistency checks in debug mode: the name should be really just
353f41cb 475 // the filename and the path should be really just a path
81f25632 476#ifdef __WXDEBUG__
dfecbee5 477 wxString volDummy, pathDummy, nameDummy, extDummy;
81f25632 478
dfecbee5 479 SplitPath(fullname, &volDummy, &pathDummy, &name, &ext, &hasExt, format);
81f25632 480
dfecbee5 481 wxASSERT_MSG( volDummy.empty() && pathDummy.empty(),
81f25632
VZ
482 _T("the file name shouldn't contain the path") );
483
484 SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format);
485
486 wxASSERT_MSG( nameDummy.empty() && extDummy.empty(),
487 _T("the path shouldn't contain file name nor extension") );
488
489#else // !__WXDEBUG__
284be59b
VZ
490 SplitPath(fullname, NULL /* no volume */, NULL /* no path */,
491 &name, &ext, &hasExt, format);
52dbd056 492 SplitPath(fullpath, &volume, &path, NULL, NULL, format);
81f25632 493#endif // __WXDEBUG__/!__WXDEBUG__
844f90fb 494
dfecbee5 495 Assign(volume, path, name, ext, hasExt, format);
52dbd056
VZ
496}
497
4c2deb19
VZ
498void wxFileName::Assign(const wxString& pathOrig,
499 const wxString& name,
500 const wxString& ext,
501 wxPathFormat format)
502{
503 wxString volume,
504 path;
505 SplitVolume(pathOrig, &volume, &path, format);
506
507 Assign(volume, path, name, ext, format);
508}
509
52dbd056
VZ
510void wxFileName::AssignDir(const wxString& dir, wxPathFormat format)
511{
b494c48b 512 Assign(dir, wxEmptyString, format);
844f90fb
VZ
513}
514
515void wxFileName::Clear()
516{
517 m_dirs.Clear();
04c943b1
VZ
518
519 m_volume =
844f90fb
VZ
520 m_name =
521 m_ext = wxEmptyString;
fb969475
VZ
522
523 // we don't have any absolute path for now
f363e05c 524 m_relative = true;
dfecbee5
VZ
525
526 // nor any extension
527 m_hasExt = false;
844f90fb
VZ
528}
529
530/* static */
520200fd 531wxFileName wxFileName::FileName(const wxString& file, wxPathFormat format)
844f90fb 532{
520200fd 533 return wxFileName(file, format);
844f90fb
VZ
534}
535
536/* static */
520200fd 537wxFileName wxFileName::DirName(const wxString& dir, wxPathFormat format)
844f90fb
VZ
538{
539 wxFileName fn;
520200fd 540 fn.AssignDir(dir, format);
844f90fb 541 return fn;
df5ddbca
RR
542}
543
844f90fb
VZ
544// ----------------------------------------------------------------------------
545// existence tests
546// ----------------------------------------------------------------------------
547
8e41796c 548bool wxFileName::FileExists() const
df5ddbca 549{
a35b27b1
RR
550 return wxFileName::FileExists( GetFullPath() );
551}
552
553bool wxFileName::FileExists( const wxString &file )
554{
555 return ::wxFileExists( file );
df5ddbca
RR
556}
557
8e41796c 558bool wxFileName::DirExists() const
df5ddbca 559{
db759dde 560 return wxFileName::DirExists( GetPath() );
a35b27b1
RR
561}
562
563bool wxFileName::DirExists( const wxString &dir )
564{
da865fdd 565 return ::wxDirExists( dir );
df5ddbca
RR
566}
567
844f90fb
VZ
568// ----------------------------------------------------------------------------
569// CWD and HOME stuff
570// ----------------------------------------------------------------------------
571
6f91bc33 572void wxFileName::AssignCwd(const wxString& volume)
df5ddbca 573{
6f91bc33 574 AssignDir(wxFileName::GetCwd(volume));
a35b27b1
RR
575}
576
844f90fb 577/* static */
6f91bc33 578wxString wxFileName::GetCwd(const wxString& volume)
a35b27b1 579{
6f91bc33
VZ
580 // if we have the volume, we must get the current directory on this drive
581 // and to do this we have to chdir to this volume - at least under Windows,
582 // I don't know how to get the current drive on another volume elsewhere
583 // (TODO)
584 wxString cwdOld;
585 if ( !volume.empty() )
586 {
587 cwdOld = wxGetCwd();
588 SetCwd(volume + GetVolumeSeparator());
589 }
590
591 wxString cwd = ::wxGetCwd();
592
593 if ( !volume.empty() )
594 {
595 SetCwd(cwdOld);
596 }
ae4d7004 597
6f91bc33 598 return cwd;
a35b27b1
RR
599}
600
601bool wxFileName::SetCwd()
602{
db759dde 603 return wxFileName::SetCwd( GetPath() );
df5ddbca
RR
604}
605
a35b27b1 606bool wxFileName::SetCwd( const wxString &cwd )
df5ddbca 607{
a35b27b1 608 return ::wxSetWorkingDirectory( cwd );
df5ddbca
RR
609}
610
a35b27b1
RR
611void wxFileName::AssignHomeDir()
612{
844f90fb 613 AssignDir(wxFileName::GetHomeDir());
a35b27b1 614}
844f90fb 615
a35b27b1
RR
616wxString wxFileName::GetHomeDir()
617{
618 return ::wxGetHomeDir();
619}
844f90fb 620
68351053 621
b70a2866
MW
622// ----------------------------------------------------------------------------
623// CreateTempFileName
624// ----------------------------------------------------------------------------
625
626#if wxUSE_FILE || wxUSE_FFILE
627
628
629#if !defined wx_fdopen && defined HAVE_FDOPEN
630 #define wx_fdopen fdopen
631#endif
632
633// NB: GetTempFileName() under Windows creates the file, so using
634// O_EXCL there would fail
635#ifdef __WINDOWS__
636 #define wxOPEN_EXCL 0
637#else
638 #define wxOPEN_EXCL O_EXCL
639#endif
640
641
642#ifdef wxOpenOSFHandle
643#define WX_HAVE_DELETE_ON_CLOSE
644// On Windows create a file with the FILE_FLAGS_DELETE_ON_CLOSE flags.
645//
646static int wxOpenWithDeleteOnClose(const wxString& filename)
df5ddbca 647{
b70a2866
MW
648 DWORD access = GENERIC_READ | GENERIC_WRITE;
649
650 DWORD disposition = OPEN_ALWAYS;
651
652 DWORD attributes = FILE_ATTRIBUTE_TEMPORARY |
653 FILE_FLAG_DELETE_ON_CLOSE;
654
e0a050e3 655 HANDLE h = ::CreateFile(filename.fn_str(), access, 0, NULL,
b70a2866
MW
656 disposition, attributes, NULL);
657
693bfcaf 658 return wxOpenOSFHandle(h, wxO_BINARY);
ade35f11 659}
b70a2866 660#endif // wxOpenOSFHandle
ade35f11 661
b70a2866
MW
662
663// Helper to open the file
664//
665static int wxTempOpen(const wxString& path, bool *deleteOnClose)
666{
667#ifdef WX_HAVE_DELETE_ON_CLOSE
668 if (*deleteOnClose)
669 return wxOpenWithDeleteOnClose(path);
670#endif
671
672 *deleteOnClose = false;
673
674 return wxOpen(path, wxO_BINARY | O_RDWR | O_CREAT | wxOPEN_EXCL, 0600);
675}
676
677
678#if wxUSE_FFILE
679// Helper to open the file and attach it to the wxFFile
680//
681static bool wxTempOpen(wxFFile *file, const wxString& path, bool *deleteOnClose)
ade35f11 682{
b70a2866
MW
683#ifndef wx_fdopen
684 *deleteOnClose = false;
685 return file->Open(path, _T("w+b"));
686#else // wx_fdopen
687 int fd = wxTempOpen(path, deleteOnClose);
693bfcaf 688 if (fd == -1)
b70a2866
MW
689 return false;
690 file->Attach(wx_fdopen(fd, "w+b"));
691 return file->IsOpened();
692#endif // wx_fdopen
693}
694#endif // wxUSE_FFILE
695
696
697#if !wxUSE_FILE
698 #define WXFILEARGS(x, y) y
699#elif !wxUSE_FFILE
700 #define WXFILEARGS(x, y) x
701#else
702 #define WXFILEARGS(x, y) x, y
703#endif
704
705
706// Implementation of wxFileName::CreateTempFileName().
707//
708static wxString wxCreateTempImpl(
709 const wxString& prefix,
710 WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp),
711 bool *deleteOnClose = NULL)
712{
713#if wxUSE_FILE && wxUSE_FFILE
714 wxASSERT(fileTemp == NULL || ffileTemp == NULL);
715#endif
ade35f11 716 wxString path, dir, name;
b70a2866
MW
717 bool wantDeleteOnClose = false;
718
719 if (deleteOnClose)
720 {
721 // set the result to false initially
722 wantDeleteOnClose = *deleteOnClose;
723 *deleteOnClose = false;
724 }
725 else
726 {
727 // easier if it alwasys points to something
728 deleteOnClose = &wantDeleteOnClose;
729 }
ade35f11
VZ
730
731 // use the directory specified by the prefix
b70a2866 732 wxFileName::SplitPath(prefix, &dir, &name, NULL /* extension */);
ade35f11 733
6091caf0
JS
734 if (dir.empty())
735 {
8d7d6dea 736 dir = wxFileName::GetTempDir();
6091caf0
JS
737 }
738
1c193821 739#if defined(__WXWINCE__)
f05ba7ff 740 path = dir + wxT("\\") + name;
1c193821 741 int i = 1;
b70a2866 742 while (wxFileName::FileExists(path))
1c193821 743 {
f05ba7ff 744 path = dir + wxT("\\") + name ;
1c193821
JS
745 path << i;
746 i ++;
747 }
ade35f11 748
1c193821 749#elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
e0a050e3
VS
750 if ( !::GetTempFileName(dir.fn_str(), name.fn_str(), 0,
751 wxStringBuffer(path, MAX_PATH + 1)) )
ade35f11
VZ
752 {
753 wxLogLastError(_T("GetTempFileName"));
754
755 path.clear();
756 }
ade35f11 757
5a3912f2 758#else // !Windows
ade35f11
VZ
759 path = dir;
760
761 if ( !wxEndsWithPathSeparator(dir) &&
762 (name.empty() || !wxIsPathSeparator(name[0u])) )
763 {
d9f54bb0 764 path += wxFILE_SEP_PATH;
ade35f11
VZ
765 }
766
767 path += name;
768
7070f55b 769#if defined(HAVE_MKSTEMP)
ade35f11
VZ
770 // scratch space for mkstemp()
771 path += _T("XXXXXX");
772
74cf9763 773 // we need to copy the path to the buffer in which mkstemp() can modify it
86501081 774 wxCharBuffer buf(path.fn_str());
74cf9763
VZ
775
776 // cast is safe because the string length doesn't change
ca11abde 777 int fdTemp = mkstemp( (char*)(const char*) buf );
df22f860 778 if ( fdTemp == -1 )
ade35f11
VZ
779 {
780 // this might be not necessary as mkstemp() on most systems should have
781 // already done it but it doesn't hurt neither...
782 path.clear();
783 }
df22f860
VZ
784 else // mkstemp() succeeded
785 {
ca11abde 786 path = wxConvFile.cMB2WX( (const char*) buf );
a62848fd 787
b70a2866 788 #if wxUSE_FILE
df22f860
VZ
789 // avoid leaking the fd
790 if ( fileTemp )
791 {
792 fileTemp->Attach(fdTemp);
793 }
794 else
b70a2866
MW
795 #endif
796
797 #if wxUSE_FFILE
798 if ( ffileTemp )
799 {
800 #ifdef wx_fdopen
801 ffileTemp->Attach(wx_fdopen(fdTemp, "r+b"));
802 #else
803 ffileTemp->Open(path, _T("r+b"));
804 close(fdTemp);
805 #endif
806 }
807 else
808 #endif
809
df22f860
VZ
810 {
811 close(fdTemp);
812 }
813 }
ade35f11
VZ
814#else // !HAVE_MKSTEMP
815
816#ifdef HAVE_MKTEMP
817 // same as above
818 path += _T("XXXXXX");
819
ca11abde 820 wxCharBuffer buf = wxConvFile.cWX2MB( path );
b70a2866 821 if ( !mktemp( (char*)(const char*) buf ) )
ade35f11
VZ
822 {
823 path.clear();
824 }
aed08d79
RR
825 else
826 {
ca11abde 827 path = wxConvFile.cMB2WX( (const char*) buf );
aed08d79 828 }
7070f55b 829#else // !HAVE_MKTEMP (includes __DOS__)
ade35f11 830 // generate the unique file name ourselves
68351053 831 #if !defined(__DOS__) && !defined(__PALMOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
ade35f11 832 path << (unsigned int)getpid();
7070f55b 833 #endif
ade35f11
VZ
834
835 wxString pathTry;
836
837 static const size_t numTries = 1000;
838 for ( size_t n = 0; n < numTries; n++ )
839 {
840 // 3 hex digits is enough for numTries == 1000 < 4096
3e778e3b 841 pathTry = path + wxString::Format(_T("%.03x"), (unsigned int) n);
b70a2866 842 if ( !wxFileName::FileExists(pathTry) )
ade35f11
VZ
843 {
844 break;
845 }
846
847 pathTry.clear();
848 }
849
850 path = pathTry;
851#endif // HAVE_MKTEMP/!HAVE_MKTEMP
852
df22f860
VZ
853#endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
854
855#endif // Windows/!Windows
856
857 if ( path.empty() )
858 {
859 wxLogSysError(_("Failed to create a temporary file name"));
860 }
b70a2866 861 else
df22f860 862 {
b70a2866
MW
863 bool ok = true;
864
df22f860 865 // open the file - of course, there is a race condition here, this is
ade35f11 866 // why we always prefer using mkstemp()...
b70a2866
MW
867 #if wxUSE_FILE
868 if ( fileTemp && !fileTemp->IsOpened() )
869 {
870 *deleteOnClose = wantDeleteOnClose;
871 int fd = wxTempOpen(path, deleteOnClose);
872 if (fd != -1)
873 fileTemp->Attach(fd);
874 else
875 ok = false;
876 }
877 #endif
878
879 #if wxUSE_FFILE
880 if ( ffileTemp && !ffileTemp->IsOpened() )
881 {
882 *deleteOnClose = wantDeleteOnClose;
883 ok = wxTempOpen(ffileTemp, path, deleteOnClose);
884 }
885 #endif
886
887 if ( !ok )
ade35f11
VZ
888 {
889 // FIXME: If !ok here should we loop and try again with another
890 // file name? That is the standard recourse if open(O_EXCL)
891 // fails, though of course it should be protected against
892 // possible infinite looping too.
893
894 wxLogError(_("Failed to open temporary file."));
895
896 path.clear();
897 }
898 }
ade35f11
VZ
899
900 return path;
df5ddbca
RR
901}
902
b70a2866
MW
903
904static bool wxCreateTempImpl(
905 const wxString& prefix,
906 WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp),
907 wxString *name)
908{
909 bool deleteOnClose = true;
910
911 *name = wxCreateTempImpl(prefix,
912 WXFILEARGS(fileTemp, ffileTemp),
913 &deleteOnClose);
914
915 bool ok = !name->empty();
916
917 if (deleteOnClose)
918 name->clear();
919#ifdef __UNIX__
920 else if (ok && wxRemoveFile(*name))
921 name->clear();
922#endif
923
924 return ok;
925}
926
927
928static void wxAssignTempImpl(
929 wxFileName *fn,
930 const wxString& prefix,
931 WXFILEARGS(wxFile *fileTemp, wxFFile *ffileTemp))
932{
933 wxString tempname;
934 tempname = wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, ffileTemp));
935
936 if ( tempname.empty() )
937 {
938 // error, failed to get temp file name
939 fn->Clear();
940 }
941 else // ok
942 {
943 fn->Assign(tempname);
944 }
945}
946
947
948void wxFileName::AssignTempFileName(const wxString& prefix)
949{
950 wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, NULL));
951}
952
953/* static */
954wxString wxFileName::CreateTempFileName(const wxString& prefix)
955{
956 return wxCreateTempImpl(prefix, WXFILEARGS(NULL, NULL));
957}
958
959#endif // wxUSE_FILE || wxUSE_FFILE
960
961
962#if wxUSE_FILE
963
964wxString wxCreateTempFileName(const wxString& prefix,
965 wxFile *fileTemp,
966 bool *deleteOnClose)
967{
968 return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), deleteOnClose);
969}
970
971bool wxCreateTempFile(const wxString& prefix,
972 wxFile *fileTemp,
973 wxString *name)
974{
975 return wxCreateTempImpl(prefix, WXFILEARGS(fileTemp, NULL), name);
976}
977
978void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp)
979{
980 wxAssignTempImpl(this, prefix, WXFILEARGS(fileTemp, NULL));
981}
982
983/* static */
984wxString
985wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp)
986{
987 return wxCreateTempFileName(prefix, fileTemp);
988}
989
68351053
WS
990#endif // wxUSE_FILE
991
b70a2866
MW
992
993#if wxUSE_FFILE
994
995wxString wxCreateTempFileName(const wxString& prefix,
996 wxFFile *fileTemp,
997 bool *deleteOnClose)
998{
999 return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), deleteOnClose);
1000}
1001
1002bool wxCreateTempFile(const wxString& prefix,
1003 wxFFile *fileTemp,
1004 wxString *name)
1005{
1006 return wxCreateTempImpl(prefix, WXFILEARGS(NULL, fileTemp), name);
1007
1008}
1009
1010void wxFileName::AssignTempFileName(const wxString& prefix, wxFFile *fileTemp)
1011{
1012 wxAssignTempImpl(this, prefix, WXFILEARGS(NULL, fileTemp));
1013}
1014
1015/* static */
1016wxString
1017wxFileName::CreateTempFileName(const wxString& prefix, wxFFile *fileTemp)
1018{
1019 return wxCreateTempFileName(prefix, fileTemp);
1020}
1021
1022#endif // wxUSE_FFILE
1023
1024
844f90fb
VZ
1025// ----------------------------------------------------------------------------
1026// directory operations
1027// ----------------------------------------------------------------------------
1028
8758875e
VZ
1029// helper of GetTempDir(): check if the given directory exists and return it if
1030// it does or an empty string otherwise
1031namespace
8d7d6dea 1032{
8d7d6dea 1033
8758875e
VZ
1034wxString CheckIfDirExists(const wxString& dir)
1035{
1036 return wxFileName::DirExists(dir) ? dir : wxString();
1037}
1038
1039} // anonymous namespace
1040
1041wxString wxFileName::GetTempDir()
1042{
1043 // first try getting it from environment: this allows overriding the values
1044 // used by default if the user wants to create temporary files in another
1045 // directory
1046 wxString dir = CheckIfDirExists(wxGetenv("TMPDIR"));
1047 if ( dir.empty() )
8d7d6dea 1048 {
8758875e
VZ
1049 dir = CheckIfDirExists(wxGetenv("TMP"));
1050 if ( dir.empty() )
1051 dir = CheckIfDirExists(wxGetenv("TEMP"));
8d7d6dea 1052 }
8d7d6dea 1053
8758875e 1054 // if no environment variables are set, use the system default
8d7d6dea
JS
1055 if ( dir.empty() )
1056 {
8758875e
VZ
1057#if defined(__WXWINCE__)
1058 dir = CheckIfDirExists(wxT("\\temp"));
1059#elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
8d7d6dea
JS
1060 if ( !::GetTempPath(MAX_PATH, wxStringBuffer(dir, MAX_PATH + 1)) )
1061 {
1062 wxLogLastError(_T("GetTempPath"));
1063 }
8758875e
VZ
1064#elif defined(__WXMAC__) && wxOSX_USE_CARBON
1065 dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder);
1066#endif // systems with native way
8d7d6dea 1067 }
8d7d6dea 1068
8758875e 1069 // fall back to hard coded value
8d7d6dea
JS
1070 if ( dir.empty() )
1071 {
8758875e
VZ
1072#ifdef __UNIX_LIKE__
1073 dir = CheckIfDirExists("/tmp");
1074 if ( dir.empty() )
1075#endif // __UNIX_LIKE__
1076 dir = ".";
8d7d6dea 1077 }
8d7d6dea
JS
1078
1079 return dir;
1080}
1081
1527281e 1082bool wxFileName::Mkdir( int perm, int flags )
a35b27b1 1083{
db759dde 1084 return wxFileName::Mkdir(GetPath(), perm, flags);
a35b27b1
RR
1085}
1086
1527281e 1087bool wxFileName::Mkdir( const wxString& dir, int perm, int flags )
df5ddbca 1088{
1527281e 1089 if ( flags & wxPATH_MKDIR_FULL )
f0ce3409 1090 {
1527281e
VZ
1091 // split the path in components
1092 wxFileName filename;
1093 filename.AssignDir(dir);
f0ce3409 1094
f0ce3409 1095 wxString currPath;
0ea621cc
VZ
1096 if ( filename.HasVolume())
1097 {
1527281e 1098 currPath << wxGetVolumeString(filename.GetVolume(), wxPATH_NATIVE);
0ea621cc 1099 }
f0ce3409 1100
1527281e
VZ
1101 wxArrayString dirs = filename.GetDirs();
1102 size_t count = dirs.GetCount();
1103 for ( size_t i = 0; i < count; i++ )
1104 {
e0d81eac 1105 if ( i > 0 || filename.IsAbsolute() )
f0ce3409 1106 currPath += wxFILE_SEP_PATH;
1527281e 1107 currPath += dirs[i];
f0ce3409
JS
1108
1109 if (!DirExists(currPath))
1527281e 1110 {
f0ce3409 1111 if (!wxMkdir(currPath, perm))
1527281e
VZ
1112 {
1113 // no need to try creating further directories
f363e05c 1114 return false;
1527281e
VZ
1115 }
1116 }
f0ce3409
JS
1117 }
1118
f363e05c 1119 return true;
f0ce3409
JS
1120
1121 }
1527281e
VZ
1122
1123 return ::wxMkdir( dir, perm );
df5ddbca
RR
1124}
1125
a35b27b1 1126bool wxFileName::Rmdir()
df5ddbca 1127{
db759dde 1128 return wxFileName::Rmdir( GetPath() );
df5ddbca
RR
1129}
1130
a35b27b1 1131bool wxFileName::Rmdir( const wxString &dir )
df5ddbca 1132{
a35b27b1 1133 return ::wxRmdir( dir );
df5ddbca
RR
1134}
1135
844f90fb
VZ
1136// ----------------------------------------------------------------------------
1137// path normalization
1138// ----------------------------------------------------------------------------
1139
32a0d013 1140bool wxFileName::Normalize(int flags,
844f90fb
VZ
1141 const wxString& cwd,
1142 wxPathFormat format)
a35b27b1 1143{
05e1201c
VZ
1144 // deal with env vars renaming first as this may seriously change the path
1145 if ( flags & wxPATH_NORM_ENV_VARS )
1146 {
1147 wxString pathOrig = GetFullPath(format);
1148 wxString path = wxExpandEnvVars(pathOrig);
1149 if ( path != pathOrig )
1150 {
1151 Assign(path);
1152 }
1153 }
1154
844f90fb
VZ
1155 // the existing path components
1156 wxArrayString dirs = GetDirs();
1157
1158 // the path to prepend in front to make the path absolute
1159 wxFileName curDir;
1160
1161 format = GetFormat(format);
ae4d7004 1162
bf7f7793 1163 // set up the directory to use for making the path absolute later
a2fa5040 1164 if ( (flags & wxPATH_NORM_ABSOLUTE) && !IsAbsolute(format) )
a35b27b1 1165 {
844f90fb 1166 if ( cwd.empty() )
6f91bc33
VZ
1167 {
1168 curDir.AssignCwd(GetVolume());
1169 }
1170 else // cwd provided
1171 {
844f90fb 1172 curDir.AssignDir(cwd);
6f91bc33 1173 }
a35b27b1 1174 }
ae4d7004 1175
844f90fb
VZ
1176 // handle ~ stuff under Unix only
1177 if ( (format == wxPATH_UNIX) && (flags & wxPATH_NORM_TILDE) )
a35b27b1 1178 {
844f90fb
VZ
1179 if ( !dirs.IsEmpty() )
1180 {
1181 wxString dir = dirs[0u];
1182 if ( !dir.empty() && dir[0u] == _T('~') )
1183 {
bf7f7793 1184 // to make the path absolute use the home directory
844f90fb
VZ
1185 curDir.AssignDir(wxGetUserHome(dir.c_str() + 1));
1186
bf7f7793
RR
1187 // if we are expanding the tilde, then this path
1188 // *should* be already relative (since we checked for
1189 // the tilde only in the first char of the first dir);
1190 // if m_relative==false, it's because it was initialized
1191 // from a string which started with /~; in that case
1192 // we reach this point but then need m_relative=true
1193 // for relative->absolute expansion later
1194 m_relative = true;
1195
da2fd5ac 1196 dirs.RemoveAt(0u);
844f90fb
VZ
1197 }
1198 }
a35b27b1 1199 }
844f90fb 1200
52dbd056 1201 // transform relative path into abs one
844f90fb 1202 if ( curDir.IsOk() )
a35b27b1 1203 {
bf7f7793
RR
1204 // this path may be relative because it doesn't have the volume name
1205 // and still have m_relative=true; in this case we shouldn't modify
1206 // our directory components but just set the current volume
1207 if ( !HasVolume() && curDir.HasVolume() )
844f90fb 1208 {
bf7f7793
RR
1209 SetVolume(curDir.GetVolume());
1210
1211 if ( !m_relative )
1212 {
1213 // yes, it was the case - we don't need curDir then
1214 curDir.Clear();
1215 }
844f90fb
VZ
1216 }
1217
bf7f7793
RR
1218 // finally, prepend curDir to the dirs array
1219 wxArrayString dirsNew = curDir.GetDirs();
1220 WX_PREPEND_ARRAY(dirs, dirsNew);
1221
1222 // if we used e.g. tilde expansion previously and wxGetUserHome didn't
1223 // return for some reason an absolute path, then curDir maybe not be absolute!
1224 if ( curDir.IsAbsolute(format) )
1225 {
1226 // we have prepended an absolute path and thus we are now an absolute
1227 // file name too
1228 m_relative = false;
1229 }
1230 // else if (flags & wxPATH_NORM_ABSOLUTE):
1231 // should we warn the user that we didn't manage to make the path absolute?
a35b27b1 1232 }
844f90fb
VZ
1233
1234 // now deal with ".", ".." and the rest
1235 m_dirs.Empty();
1236 size_t count = dirs.GetCount();
1237 for ( size_t n = 0; n < count; n++ )
a35b27b1 1238 {
844f90fb
VZ
1239 wxString dir = dirs[n];
1240
2bc60417 1241 if ( flags & wxPATH_NORM_DOTS )
844f90fb
VZ
1242 {
1243 if ( dir == wxT(".") )
1244 {
1245 // just ignore
1246 continue;
1247 }
1248
1249 if ( dir == wxT("..") )
1250 {
1251 if ( m_dirs.IsEmpty() )
1252 {
1253 wxLogError(_("The path '%s' contains too many \"..\"!"),
1254 GetFullPath().c_str());
f363e05c 1255 return false;
844f90fb
VZ
1256 }
1257
ae4d7004 1258 m_dirs.RemoveAt(m_dirs.GetCount() - 1);
844f90fb
VZ
1259 continue;
1260 }
1261 }
1262
844f90fb 1263 m_dirs.Add(dir);
a35b27b1 1264 }
a62848fd 1265
6bda7391 1266#if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
21f60945
JS
1267 if ( (flags & wxPATH_NORM_SHORTCUT) )
1268 {
1269 wxString filename;
1270 if (GetShortcutTarget(GetFullPath(format), filename))
1271 {
21f60945
JS
1272 m_relative = false;
1273 Assign(filename);
1274 }
1275 }
1276#endif
844f90fb 1277
9a0c5c01
VZ
1278#if defined(__WIN32__)
1279 if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) )
844f90fb 1280 {
9a0c5c01
VZ
1281 Assign(GetLongPath());
1282 }
1283#endif // Win32
844f90fb 1284
9a0c5c01
VZ
1285 // Change case (this should be kept at the end of the function, to ensure
1286 // that the path doesn't change any more after we normalize its case)
1287 if ( (flags & wxPATH_NORM_CASE) && !IsCaseSensitive(format) )
1288 {
55268a3a 1289 m_volume.MakeLower();
844f90fb
VZ
1290 m_name.MakeLower();
1291 m_ext.MakeLower();
844f90fb 1292
9a0c5c01
VZ
1293 // directory entries must be made lower case as well
1294 count = m_dirs.GetCount();
1295 for ( size_t i = 0; i < count; i++ )
1296 {
1297 m_dirs[i].MakeLower();
1298 }
9e9b65c1 1299 }
9e9b65c1 1300
f363e05c 1301 return true;
a2fa5040
VZ
1302}
1303
395f3aa8
FM
1304#ifndef __WXWINCE__
1305bool wxFileName::ReplaceEnvVariable(const wxString& envname,
1306 const wxString& replacementFmtString,
1307 wxPathFormat format)
1308{
1309 // look into stringForm for the contents of the given environment variable
1310 wxString val;
1311 if (envname.empty() ||
1312 !wxGetEnv(envname, &val))
1313 return false;
1314 if (val.empty())
1315 return false;
1316
1317 wxString stringForm = GetPath(wxPATH_GET_VOLUME, format);
1318 // do not touch the file name and the extension
1319
1320 wxString replacement = wxString::Format(replacementFmtString, envname);
1321 stringForm.Replace(val, replacement);
1322
1323 // Now assign ourselves the modified path:
1324 Assign(stringForm, GetFullName(), format);
1325
1326 return true;
1327}
1328#endif
1329
1330bool wxFileName::ReplaceHomeDir(wxPathFormat format)
1331{
1332 wxString homedir = wxGetHomeDir();
1333 if (homedir.empty())
1334 return false;
1335
1336 wxString stringForm = GetPath(wxPATH_GET_VOLUME, format);
1337 // do not touch the file name and the extension
1338
1339 stringForm.Replace(homedir, "~");
1340
1341 // Now assign ourselves the modified path:
1342 Assign(stringForm, GetFullName(), format);
1343
1344 return true;
1345}
1346
21f60945
JS
1347// ----------------------------------------------------------------------------
1348// get the shortcut target
1349// ----------------------------------------------------------------------------
1350
5671b3b6
JS
1351// WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions.
1352// The .lnk file is a plain text file so it should be easy to
1353// make it work. Hint from Google Groups:
1354// "If you open up a lnk file, you'll see a
1355// number, followed by a pound sign (#), followed by more text. The
1356// number is the number of characters that follows the pound sign. The
1357// characters after the pound sign are the command line (which _can_
1358// include arguments) to be executed. Any path (e.g. \windows\program
1359// files\myapp.exe) that includes spaces needs to be enclosed in
1360// quotation marks."
1361
6bda7391 1362#if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
5671b3b6
JS
1363// The following lines are necessary under WinCE
1364// #include "wx/msw/private.h"
1365// #include <ole2.h>
21f60945 1366#include <shlobj.h>
5671b3b6
JS
1367#if defined(__WXWINCE__)
1368#include <shlguid.h>
1369#endif
21f60945 1370
cea0869c
VZ
1371bool wxFileName::GetShortcutTarget(const wxString& shortcutPath,
1372 wxString& targetFilename,
1373 wxString* arguments)
21f60945 1374{
21f60945 1375 wxString path, file, ext;
bd365871 1376 wxFileName::SplitPath(shortcutPath, & path, & file, & ext);
a62848fd
WS
1377
1378 HRESULT hres;
1379 IShellLink* psl;
1380 bool success = false;
21f60945
JS
1381
1382 // Assume it's not a shortcut if it doesn't end with lnk
489f6cf7 1383 if (ext.CmpNoCase(wxT("lnk"))!=0)
a62848fd
WS
1384 return false;
1385
1386 // create a ShellLink object
1387 hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1388 IID_IShellLink, (LPVOID*) &psl);
1389
1390 if (SUCCEEDED(hres))
1391 {
1392 IPersistFile* ppf;
1393 hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf);
1394 if (SUCCEEDED(hres))
1395 {
1396 WCHAR wsz[MAX_PATH];
1397
1398 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, shortcutPath.mb_str(), -1, wsz,
1399 MAX_PATH);
1400
1401 hres = ppf->Load(wsz, 0);
cea0869c
VZ
1402 ppf->Release();
1403
a62848fd
WS
1404 if (SUCCEEDED(hres))
1405 {
21f60945 1406 wxChar buf[2048];
59ba321b
JS
1407 // Wrong prototype in early versions
1408#if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2)
1409 psl->GetPath((CHAR*) buf, 2048, NULL, SLGP_UNCPRIORITY);
1410#else
a62848fd 1411 psl->GetPath(buf, 2048, NULL, SLGP_UNCPRIORITY);
59ba321b 1412#endif
a62848fd 1413 targetFilename = wxString(buf);
21f60945
JS
1414 success = (shortcutPath != targetFilename);
1415
a62848fd 1416 psl->GetArguments(buf, 2048);
21f60945 1417 wxString args(buf);
b494c48b 1418 if (!args.empty() && arguments)
21f60945
JS
1419 {
1420 *arguments = args;
a62848fd
WS
1421 }
1422 }
1423 }
cea0869c
VZ
1424
1425 psl->Release();
a62848fd 1426 }
a62848fd 1427 return success;
21f60945 1428}
cea0869c
VZ
1429
1430#endif // __WIN32__ && !__WXWINCE__
21f60945
JS
1431
1432
a2fa5040
VZ
1433// ----------------------------------------------------------------------------
1434// absolute/relative paths
1435// ----------------------------------------------------------------------------
1436
1437bool wxFileName::IsAbsolute(wxPathFormat format) const
1438{
1439 // if our path doesn't start with a path separator, it's not an absolute
1440 // path
1441 if ( m_relative )
f363e05c 1442 return false;
a2fa5040
VZ
1443
1444 if ( !GetVolumeSeparator(format).empty() )
1445 {
1446 // this format has volumes and an absolute path must have one, it's not
1447 // enough to have the full path to bean absolute file under Windows
1448 if ( GetVolume().empty() )
f363e05c 1449 return false;
a2fa5040
VZ
1450 }
1451
f363e05c 1452 return true;
a35b27b1
RR
1453}
1454
f7d886af
VZ
1455bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format)
1456{
cef9731a 1457 wxFileName fnBase = wxFileName::DirName(pathBase, format);
f7d886af
VZ
1458
1459 // get cwd only once - small time saving
1460 wxString cwd = wxGetCwd();
b5b62eea
JS
1461 Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format);
1462 fnBase.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format);
f7d886af
VZ
1463
1464 bool withCase = IsCaseSensitive(format);
1465
1466 // we can't do anything if the files live on different volumes
1467 if ( !GetVolume().IsSameAs(fnBase.GetVolume(), withCase) )
1468 {
1469 // nothing done
f363e05c 1470 return false;
f7d886af
VZ
1471 }
1472
1473 // same drive, so we don't need our volume
1474 m_volume.clear();
1475
1476 // remove common directories starting at the top
1477 while ( !m_dirs.IsEmpty() && !fnBase.m_dirs.IsEmpty() &&
1478 m_dirs[0u].IsSameAs(fnBase.m_dirs[0u], withCase) )
1479 {
ae4d7004
VZ
1480 m_dirs.RemoveAt(0);
1481 fnBase.m_dirs.RemoveAt(0);
f7d886af
VZ
1482 }
1483
1484 // add as many ".." as needed
1485 size_t count = fnBase.m_dirs.GetCount();
1486 for ( size_t i = 0; i < count; i++ )
1487 {
1488 m_dirs.Insert(wxT(".."), 0u);
1489 }
90a68369 1490
2db991f4
VZ
1491 if ( format == wxPATH_UNIX || format == wxPATH_DOS )
1492 {
1493 // a directory made relative with respect to itself is '.' under Unix
1494 // and DOS, by definition (but we don't have to insert "./" for the
1495 // files)
1496 if ( m_dirs.IsEmpty() && IsDir() )
1497 {
1498 m_dirs.Add(_T('.'));
0ea621cc 1499 }
2db991f4
VZ
1500 }
1501
f363e05c 1502 m_relative = true;
f7d886af
VZ
1503
1504 // we were modified
f363e05c 1505 return true;
f7d886af
VZ
1506}
1507
844f90fb
VZ
1508// ----------------------------------------------------------------------------
1509// filename kind tests
1510// ----------------------------------------------------------------------------
1511
2b5f62a0 1512bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const
df5ddbca 1513{
844f90fb
VZ
1514 wxFileName fn1 = *this,
1515 fn2 = filepath;
1516
1517 // get cwd only once - small time saving
1518 wxString cwd = wxGetCwd();
55268a3a
VZ
1519 fn1.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format);
1520 fn2.Normalize(wxPATH_NORM_ALL | wxPATH_NORM_CASE, cwd, format);
844f90fb
VZ
1521
1522 if ( fn1.GetFullPath() == fn2.GetFullPath() )
f363e05c 1523 return true;
844f90fb
VZ
1524
1525 // TODO: compare inodes for Unix, this works even when filenames are
1526 // different but files are the same (symlinks) (VZ)
1527
f363e05c 1528 return false;
df5ddbca
RR
1529}
1530
844f90fb 1531/* static */
df5ddbca
RR
1532bool wxFileName::IsCaseSensitive( wxPathFormat format )
1533{
04c943b1
VZ
1534 // only Unix filenames are truely case-sensitive
1535 return GetFormat(format) == wxPATH_UNIX;
df5ddbca
RR
1536}
1537
f363e05c
VZ
1538/* static */
1539wxString wxFileName::GetForbiddenChars(wxPathFormat format)
1540{
1541 // Inits to forbidden characters that are common to (almost) all platforms.
1542 wxString strForbiddenChars = wxT("*?");
1543
1c6f2414 1544 // If asserts, wxPathFormat has been changed. In case of a new path format
f363e05c 1545 // addition, the following code might have to be updated.
1c6f2414 1546 wxCOMPILE_TIME_ASSERT(wxPATH_MAX == 5, wxPathFormatChanged);
f363e05c
VZ
1547 switch ( GetFormat(format) )
1548 {
1549 default :
1550 wxFAIL_MSG( wxT("Unknown path format") );
1551 // !! Fall through !!
1552
1553 case wxPATH_UNIX:
1554 break;
1555
1556 case wxPATH_MAC:
1557 // On a Mac even names with * and ? are allowed (Tested with OS
1558 // 9.2.1 and OS X 10.2.5)
1559 strForbiddenChars = wxEmptyString;
1560 break;
1561
1562 case wxPATH_DOS:
1563 strForbiddenChars += wxT("\\/:\"<>|");
1564 break;
1565
1566 case wxPATH_VMS:
1567 break;
1568 }
1569
1570 return strForbiddenChars;
1571}
1572
04c943b1 1573/* static */
bcfa2b31 1574wxString wxFileName::GetVolumeSeparator(wxPathFormat WXUNUSED_IN_WINCE(format))
844f90fb 1575{
bcfa2b31
WS
1576#ifdef __WXWINCE__
1577 return wxEmptyString;
1578#else
04c943b1
VZ
1579 wxString sepVol;
1580
a385b5df
RR
1581 if ( (GetFormat(format) == wxPATH_DOS) ||
1582 (GetFormat(format) == wxPATH_VMS) )
04c943b1 1583 {
04c943b1
VZ
1584 sepVol = wxFILE_SEP_DSK;
1585 }
a385b5df 1586 //else: leave empty
04c943b1
VZ
1587
1588 return sepVol;
bcfa2b31 1589#endif
844f90fb
VZ
1590}
1591
1592/* static */
1593wxString wxFileName::GetPathSeparators(wxPathFormat format)
1594{
1595 wxString seps;
1596 switch ( GetFormat(format) )
df5ddbca 1597 {
844f90fb 1598 case wxPATH_DOS:
04c943b1
VZ
1599 // accept both as native APIs do but put the native one first as
1600 // this is the one we use in GetFullPath()
1601 seps << wxFILE_SEP_PATH_DOS << wxFILE_SEP_PATH_UNIX;
844f90fb
VZ
1602 break;
1603
1604 default:
f363e05c 1605 wxFAIL_MSG( _T("Unknown wxPATH_XXX style") );
844f90fb
VZ
1606 // fall through
1607
1608 case wxPATH_UNIX:
9e8d8607 1609 seps = wxFILE_SEP_PATH_UNIX;
844f90fb
VZ
1610 break;
1611
1612 case wxPATH_MAC:
9e8d8607 1613 seps = wxFILE_SEP_PATH_MAC;
844f90fb 1614 break;
04c943b1 1615
3c621059
JJ
1616 case wxPATH_VMS:
1617 seps = wxFILE_SEP_PATH_VMS;
1618 break;
df5ddbca
RR
1619 }
1620
844f90fb 1621 return seps;
df5ddbca
RR
1622}
1623
f1e77933
VZ
1624/* static */
1625wxString wxFileName::GetPathTerminators(wxPathFormat format)
1626{
1627 format = GetFormat(format);
1628
1629 // under VMS the end of the path is ']', not the path separator used to
1630 // separate the components
1631 return format == wxPATH_VMS ? wxString(_T(']')) : GetPathSeparators(format);
1632}
1633
844f90fb
VZ
1634/* static */
1635bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format)
df5ddbca 1636{
04c943b1 1637 // wxString::Find() doesn't work as expected with NUL - it will always find
2fb5a4ce
VZ
1638 // it, so test for it separately
1639 return ch != _T('\0') && GetPathSeparators(format).Find(ch) != wxNOT_FOUND;
df5ddbca
RR
1640}
1641
844f90fb
VZ
1642// ----------------------------------------------------------------------------
1643// path components manipulation
1644// ----------------------------------------------------------------------------
1645
5bb9aeb2
VZ
1646/* static */ bool wxFileName::IsValidDirComponent(const wxString& dir)
1647{
1648 if ( dir.empty() )
1649 {
1650 wxFAIL_MSG( _T("empty directory passed to wxFileName::InsertDir()") );
1651
1652 return false;
1653 }
1654
1655 const size_t len = dir.length();
1656 for ( size_t n = 0; n < len; n++ )
1657 {
1658 if ( dir[n] == GetVolumeSeparator() || IsPathSeparator(dir[n]) )
1659 {
1660 wxFAIL_MSG( _T("invalid directory component in wxFileName") );
1661
1662 return false;
1663 }
1664 }
1665
1666 return true;
1667}
1668
2458d90b 1669void wxFileName::AppendDir( const wxString& dir )
df5ddbca 1670{
5bb9aeb2
VZ
1671 if ( IsValidDirComponent(dir) )
1672 m_dirs.Add( dir );
df5ddbca
RR
1673}
1674
2458d90b 1675void wxFileName::PrependDir( const wxString& dir )
df5ddbca 1676{
5bb9aeb2 1677 InsertDir(0, dir);
df5ddbca
RR
1678}
1679
2458d90b 1680void wxFileName::InsertDir(size_t before, const wxString& dir)
df5ddbca 1681{
5bb9aeb2 1682 if ( IsValidDirComponent(dir) )
2458d90b 1683 m_dirs.Insert(dir, before);
df5ddbca
RR
1684}
1685
2458d90b 1686void wxFileName::RemoveDir(size_t pos)
df5ddbca 1687{
2458d90b 1688 m_dirs.RemoveAt(pos);
df5ddbca
RR
1689}
1690
844f90fb
VZ
1691// ----------------------------------------------------------------------------
1692// accessors
1693// ----------------------------------------------------------------------------
1694
7124df9b
VZ
1695void wxFileName::SetFullName(const wxString& fullname)
1696{
dfecbee5
VZ
1697 SplitPath(fullname, NULL /* no volume */, NULL /* no path */,
1698 &m_name, &m_ext, &m_hasExt);
7124df9b
VZ
1699}
1700
844f90fb 1701wxString wxFileName::GetFullName() const
a35b27b1 1702{
844f90fb 1703 wxString fullname = m_name;
dfecbee5 1704 if ( m_hasExt )
a35b27b1 1705 {
9e8d8607 1706 fullname << wxFILE_SEP_EXT << m_ext;
a35b27b1 1707 }
a35b27b1 1708
844f90fb 1709 return fullname;
a35b27b1
RR
1710}
1711
33b97389 1712wxString wxFileName::GetPath( int flags, wxPathFormat format ) const
df5ddbca
RR
1713{
1714 format = GetFormat( format );
844f90fb 1715
353f41cb
RR
1716 wxString fullpath;
1717
33b97389
VZ
1718 // return the volume with the path as well if requested
1719 if ( flags & wxPATH_GET_VOLUME )
1720 {
1721 fullpath += wxGetVolumeString(GetVolume(), format);
1722 }
1723
034742fc
VZ
1724 // the leading character
1725 switch ( format )
1726 {
1727 case wxPATH_MAC:
1728 if ( m_relative )
1729 fullpath += wxFILE_SEP_PATH_MAC;
1730 break;
1731
1732 case wxPATH_DOS:
1733 if ( !m_relative )
1734 fullpath += wxFILE_SEP_PATH_DOS;
1735 break;
1736
1737 default:
1738 wxFAIL_MSG( wxT("Unknown path format") );
1739 // fall through
1740
1741 case wxPATH_UNIX:
1742 if ( !m_relative )
1743 {
1744 // normally the absolute file names start with a slash
1745 // with one exception: the ones like "~/foo.bar" don't
1746 // have it
9b3a3820 1747 if ( m_dirs.IsEmpty() || m_dirs[0u] != _T('~') )
034742fc
VZ
1748 {
1749 fullpath += wxFILE_SEP_PATH_UNIX;
1750 }
1751 }
1752 break;
1753
1754 case wxPATH_VMS:
1755 // no leading character here but use this place to unset
1756 // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense
1757 // as, if I understand correctly, there should never be a dot
1758 // before the closing bracket
1759 flags &= ~wxPATH_GET_SEPARATOR;
1760 }
1761
1762 if ( m_dirs.empty() )
1763 {
1764 // there is nothing more
1765 return fullpath;
1766 }
1767
1768 // then concatenate all the path components using the path separator
1769 if ( format == wxPATH_VMS )
1770 {
1771 fullpath += wxT('[');
1772 }
1773
5bb9aeb2 1774 const size_t dirCount = m_dirs.GetCount();
034742fc 1775 for ( size_t i = 0; i < dirCount; i++ )
353f41cb 1776 {
034742fc 1777 switch (format)
5bb9aeb2
VZ
1778 {
1779 case wxPATH_MAC:
034742fc
VZ
1780 if ( m_dirs[i] == wxT(".") )
1781 {
1782 // skip appending ':', this shouldn't be done in this
1783 // case as "::" is interpreted as ".." under Unix
1784 continue;
1785 }
2361ce82 1786
034742fc 1787 // convert back from ".." to nothing
489f6cf7 1788 if ( !m_dirs[i].IsSameAs(wxT("..")) )
034742fc 1789 fullpath += m_dirs[i];
5bb9aeb2 1790 break;
2361ce82 1791
5bb9aeb2 1792 default:
034742fc
VZ
1793 wxFAIL_MSG( wxT("Unexpected path format") );
1794 // still fall through
2361ce82 1795
034742fc 1796 case wxPATH_DOS:
5bb9aeb2 1797 case wxPATH_UNIX:
034742fc 1798 fullpath += m_dirs[i];
5bb9aeb2 1799 break;
2361ce82 1800
5bb9aeb2 1801 case wxPATH_VMS:
034742fc 1802 // TODO: What to do with ".." under VMS
353f41cb 1803
034742fc 1804 // convert back from ".." to nothing
489f6cf7 1805 if ( !m_dirs[i].IsSameAs(wxT("..")) )
353f41cb 1806 fullpath += m_dirs[i];
034742fc 1807 break;
4175794e
VZ
1808 }
1809
034742fc
VZ
1810 if ( (flags & wxPATH_GET_SEPARATOR) || (i != dirCount - 1) )
1811 fullpath += GetPathSeparator(format);
df5ddbca 1812 }
034742fc
VZ
1813
1814 if ( format == wxPATH_VMS )
5bb9aeb2 1815 {
034742fc 1816 fullpath += wxT(']');
5bb9aeb2 1817 }
844f90fb 1818
353f41cb 1819 return fullpath;
df5ddbca
RR
1820}
1821
1822wxString wxFileName::GetFullPath( wxPathFormat format ) const
1823{
4175794e
VZ
1824 // we already have a function to get the path
1825 wxString fullpath = GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR,
1826 format);
04c943b1 1827
4175794e 1828 // now just add the file name and extension to it
04c943b1
VZ
1829 fullpath += GetFullName();
1830
1831 return fullpath;
df5ddbca
RR
1832}
1833
9e9b65c1
JS
1834// Return the short form of the path (returns identity on non-Windows platforms)
1835wxString wxFileName::GetShortPath() const
1836{
9e9b65c1 1837 wxString path(GetFullPath());
2f3d9587
VZ
1838
1839#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
e0a050e3 1840 DWORD sz = ::GetShortPathName(path.fn_str(), NULL, 0);
2f3d9587 1841 if ( sz != 0 )
9e9b65c1 1842 {
2f3d9587
VZ
1843 wxString pathOut;
1844 if ( ::GetShortPathName
75ef5722 1845 (
e0a050e3 1846 path.fn_str(),
de564874 1847 wxStringBuffer(pathOut, sz),
75ef5722 1848 sz
2f3d9587
VZ
1849 ) != 0 )
1850 {
1851 return pathOut;
1852 }
9e9b65c1 1853 }
2f3d9587 1854#endif // Windows
5716a1ab
VZ
1855
1856 return path;
9e9b65c1
JS
1857}
1858
1859// Return the long form of the path (returns identity on non-Windows platforms)
1860wxString wxFileName::GetLongPath() const
1861{
52dbd056
VZ
1862 wxString pathOut,
1863 path = GetFullPath();
1864
b517351b 1865#if defined(__WIN32__) && !defined(__WXWINCE__) && !defined(__WXMICROWIN__)
05e7001c 1866
0e207280 1867#if wxUSE_DYNLIB_CLASS
62dcaed6 1868 typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD);
05e7001c 1869
2f3d9587
VZ
1870 // this is MT-safe as in the worst case we're going to resolve the function
1871 // twice -- but as the result is the same in both threads, it's ok
1872 static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL;
1873 if ( !s_pfnGetLongPathName )
9e9b65c1 1874 {
2f3d9587 1875 static bool s_triedToLoad = false;
2361ce82 1876
2f3d9587 1877 if ( !s_triedToLoad )
05e7001c 1878 {
2f3d9587
VZ
1879 s_triedToLoad = true;
1880
1881 wxDynamicLibrary dllKernel(_T("kernel32"));
1882
4377d3ab
WS
1883 const wxChar* GetLongPathName = _T("GetLongPathName")
1884#if wxUSE_UNICODE
1885 _T("W");
1886#else // ANSI
1887 _T("A");
1888#endif // Unicode/ANSI
1889
1890 if ( dllKernel.HasSymbol(GetLongPathName) )
05e7001c 1891 {
2f3d9587 1892 s_pfnGetLongPathName = (GET_LONG_PATH_NAME)
4377d3ab 1893 dllKernel.GetSymbol(GetLongPathName);
05e7001c 1894 }
2f3d9587
VZ
1895
1896 // note that kernel32.dll can be unloaded, it stays in memory
1897 // anyhow as all Win32 programs link to it and so it's safe to call
1898 // GetLongPathName() even after unloading it
05e7001c 1899 }
9e9b65c1 1900 }
2361ce82 1901
2f3d9587
VZ
1902 if ( s_pfnGetLongPathName )
1903 {
e0a050e3 1904 DWORD dwSize = (*s_pfnGetLongPathName)(path.fn_str(), NULL, 0);
2f3d9587
VZ
1905 if ( dwSize > 0 )
1906 {
1907 if ( (*s_pfnGetLongPathName)
1908 (
e0a050e3 1909 path.fn_str(),
2f3d9587
VZ
1910 wxStringBuffer(pathOut, dwSize),
1911 dwSize
1912 ) != 0 )
1913 {
1914 return pathOut;
1915 }
1916 }
1917 }
0e207280 1918#endif // wxUSE_DYNLIB_CLASS
05e7001c 1919
2f3d9587
VZ
1920 // The OS didn't support GetLongPathName, or some other error.
1921 // We need to call FindFirstFile on each component in turn.
05e7001c 1922
2f3d9587
VZ
1923 WIN32_FIND_DATA findFileData;
1924 HANDLE hFind;
b5b62eea 1925
2f3d9587
VZ
1926 if ( HasVolume() )
1927 pathOut = GetVolume() +
1928 GetVolumeSeparator(wxPATH_DOS) +
1929 GetPathSeparator(wxPATH_DOS);
1930 else
1931 pathOut = wxEmptyString;
05e7001c 1932
2f3d9587
VZ
1933 wxArrayString dirs = GetDirs();
1934 dirs.Add(GetFullName());
77fe02a8 1935
2f3d9587 1936 wxString tmpPath;
5d978d07 1937
2f3d9587
VZ
1938 size_t count = dirs.GetCount();
1939 for ( size_t i = 0; i < count; i++ )
1940 {
ea6319cb
VZ
1941 const wxString& dir = dirs[i];
1942
2f3d9587
VZ
1943 // We're using pathOut to collect the long-name path, but using a
1944 // temporary for appending the last path component which may be
1945 // short-name
ea6319cb
VZ
1946 tmpPath = pathOut + dir;
1947
1948 // We must not process "." or ".." here as they would be (unexpectedly)
1949 // replaced by the corresponding directory names so just leave them
1950 // alone
1951 //
1952 // And we can't pass a drive and root dir to FindFirstFile (VZ: why?)
1953 if ( tmpPath.empty() || dir == '.' || dir == ".." ||
1954 tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) )
2f3d9587 1955 {
2f3d9587
VZ
1956 tmpPath += wxFILE_SEP_PATH;
1957 pathOut = tmpPath;
1958 continue;
1959 }
05e7001c 1960
e0a050e3 1961 hFind = ::FindFirstFile(tmpPath.fn_str(), &findFileData);
2f3d9587
VZ
1962 if (hFind == INVALID_HANDLE_VALUE)
1963 {
1964 // Error: most likely reason is that path doesn't exist, so
1965 // append any unprocessed parts and return
1966 for ( i += 1; i < count; i++ )
1967 tmpPath += wxFILE_SEP_PATH + dirs[i];
b5b62eea 1968
2f3d9587
VZ
1969 return tmpPath;
1970 }
05e7001c 1971
2f3d9587
VZ
1972 pathOut += findFileData.cFileName;
1973 if ( (i < (count-1)) )
1974 pathOut += wxFILE_SEP_PATH;
52dbd056 1975
2f3d9587 1976 ::FindClose(hFind);
05e7001c 1977 }
52dbd056
VZ
1978#else // !Win32
1979 pathOut = path;
1980#endif // Win32/!Win32
5716a1ab 1981
05e7001c 1982 return pathOut;
9e9b65c1
JS
1983}
1984
df5ddbca
RR
1985wxPathFormat wxFileName::GetFormat( wxPathFormat format )
1986{
1987 if (format == wxPATH_NATIVE)
1988 {
5a3912f2 1989#if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__)
df5ddbca 1990 format = wxPATH_DOS;
3c621059 1991#elif defined(__VMS)
04c943b1 1992 format = wxPATH_VMS;
844f90fb 1993#else
df5ddbca
RR
1994 format = wxPATH_UNIX;
1995#endif
1996 }
1997 return format;
1998}
a35b27b1 1999
35c2aa4f
VZ
2000#ifdef wxHAS_FILESYSTEM_VOLUMES
2001
2002/* static */
2003wxString wxFileName::GetVolumeString(char drive, int flags)
2004{
2005 wxASSERT_MSG( !(flags & ~wxPATH_GET_SEPARATOR), "invalid flag specified" );
2006
2007 wxString vol(drive);
2008 vol += wxFILE_SEP_DSK;
2009 if ( flags & wxPATH_GET_SEPARATOR )
2010 vol += wxFILE_SEP_PATH;
2011
2012 return vol;
2013}
2014
2015#endif // wxHAS_FILESYSTEM_VOLUMES
2016
9e8d8607
VZ
2017// ----------------------------------------------------------------------------
2018// path splitting function
2019// ----------------------------------------------------------------------------
2020
6f91bc33 2021/* static */
f1e77933
VZ
2022void
2023wxFileName::SplitVolume(const wxString& fullpathWithVolume,
2024 wxString *pstrVolume,
2025 wxString *pstrPath,
2026 wxPathFormat format)
9e8d8607
VZ
2027{
2028 format = GetFormat(format);
2029
04c943b1
VZ
2030 wxString fullpath = fullpathWithVolume;
2031
04c943b1 2032 // special Windows UNC paths hack: transform \\share\path into share:path
9e1c7236 2033 if ( IsUNCPath(fullpath, format) )
9e8d8607 2034 {
9e1c7236 2035 fullpath.erase(0, 2);
04c943b1 2036
9e1c7236
VZ
2037 size_t posFirstSlash =
2038 fullpath.find_first_of(GetPathTerminators(format));
2039 if ( posFirstSlash != wxString::npos )
2040 {
2041 fullpath[posFirstSlash] = wxFILE_SEP_DSK;
04c943b1 2042
9e1c7236
VZ
2043 // UNC paths are always absolute, right? (FIXME)
2044 fullpath.insert(posFirstSlash + 1, 1, wxFILE_SEP_PATH_DOS);
3c621059
JJ
2045 }
2046 }
04c943b1 2047
a385b5df
RR
2048 // We separate the volume here
2049 if ( format == wxPATH_DOS || format == wxPATH_VMS )
04c943b1 2050 {
a385b5df 2051 wxString sepVol = GetVolumeSeparator(format);
24eb81cb 2052
04c943b1
VZ
2053 size_t posFirstColon = fullpath.find_first_of(sepVol);
2054 if ( posFirstColon != wxString::npos )
2055 {
2056 if ( pstrVolume )
2057 {
2058 *pstrVolume = fullpath.Left(posFirstColon);
2059 }
2060
2061 // remove the volume name and the separator from the full path
2062 fullpath.erase(0, posFirstColon + sepVol.length());
2063 }
2064 }
2065
f1e77933
VZ
2066 if ( pstrPath )
2067 *pstrPath = fullpath;
2068}
2069
2070/* static */
2071void wxFileName::SplitPath(const wxString& fullpathWithVolume,
2072 wxString *pstrVolume,
2073 wxString *pstrPath,
2074 wxString *pstrName,
2075 wxString *pstrExt,
dfecbee5 2076 bool *hasExt,
f1e77933
VZ
2077 wxPathFormat format)
2078{
2079 format = GetFormat(format);
2080
2081 wxString fullpath;
2082 SplitVolume(fullpathWithVolume, pstrVolume, &fullpath, format);
2083
04c943b1
VZ
2084 // find the positions of the last dot and last path separator in the path
2085 size_t posLastDot = fullpath.find_last_of(wxFILE_SEP_EXT);
f1e77933 2086 size_t posLastSlash = fullpath.find_last_of(GetPathTerminators(format));
04c943b1 2087
e5a573a2 2088 // check whether this dot occurs at the very beginning of a path component
04c943b1 2089 if ( (posLastDot != wxString::npos) &&
e5a573a2
VZ
2090 (posLastDot == 0 ||
2091 IsPathSeparator(fullpath[posLastDot - 1]) ||
2092 (format == wxPATH_VMS && fullpath[posLastDot - 1] == _T(']'))) )
f1e77933
VZ
2093 {
2094 // dot may be (and commonly -- at least under Unix -- is) the first
2095 // character of the filename, don't treat the entire filename as
2096 // extension in this case
2097 posLastDot = wxString::npos;
2098 }
9e8d8607 2099
8e7dda21
VZ
2100 // if we do have a dot and a slash, check that the dot is in the name part
2101 if ( (posLastDot != wxString::npos) &&
2102 (posLastSlash != wxString::npos) &&
2103 (posLastDot < posLastSlash) )
9e8d8607
VZ
2104 {
2105 // the dot is part of the path, not the start of the extension
2106 posLastDot = wxString::npos;
2107 }
2108
2109 // now fill in the variables provided by user
2110 if ( pstrPath )
2111 {
2112 if ( posLastSlash == wxString::npos )
2113 {
2114 // no path at all
2115 pstrPath->Empty();
2116 }
2117 else
2118 {
04c943b1 2119 // take everything up to the path separator but take care to make
353f41cb 2120 // the path equal to something like '/', not empty, for the files
04c943b1
VZ
2121 // immediately under root directory
2122 size_t len = posLastSlash;
91b4bd63
SC
2123
2124 // this rule does not apply to mac since we do not start with colons (sep)
2125 // except for relative paths
2126 if ( !len && format != wxPATH_MAC)
04c943b1
VZ
2127 len++;
2128
2129 *pstrPath = fullpath.Left(len);
2130
2131 // special VMS hack: remove the initial bracket
2132 if ( format == wxPATH_VMS )
2133 {
2134 if ( (*pstrPath)[0u] == _T('[') )
2135 pstrPath->erase(0, 1);
2136 }
9e8d8607
VZ
2137 }
2138 }
2139
2140 if ( pstrName )
2141 {
42b1f941
VZ
2142 // take all characters starting from the one after the last slash and
2143 // up to, but excluding, the last dot
9e8d8607 2144 size_t nStart = posLastSlash == wxString::npos ? 0 : posLastSlash + 1;
8e7dda21
VZ
2145 size_t count;
2146 if ( posLastDot == wxString::npos )
2147 {
2148 // take all until the end
2149 count = wxString::npos;
2150 }
2151 else if ( posLastSlash == wxString::npos )
2152 {
2153 count = posLastDot;
2154 }
2155 else // have both dot and slash
2156 {
2157 count = posLastDot - posLastSlash - 1;
2158 }
9e8d8607
VZ
2159
2160 *pstrName = fullpath.Mid(nStart, count);
2161 }
2162
dfecbee5
VZ
2163 // finally deal with the extension here: we have an added complication that
2164 // extension may be empty (but present) as in "foo." where trailing dot
2165 // indicates the empty extension at the end -- and hence we must remember
2166 // that we have it independently of pstrExt
2167 if ( posLastDot == wxString::npos )
9e8d8607 2168 {
dfecbee5
VZ
2169 // no extension
2170 if ( pstrExt )
2171 pstrExt->clear();
2172 if ( hasExt )
2173 *hasExt = false;
2174 }
2175 else
2176 {
2177 // take everything after the dot
2178 if ( pstrExt )
9e8d8607 2179 *pstrExt = fullpath.Mid(posLastDot + 1);
dfecbee5
VZ
2180 if ( hasExt )
2181 *hasExt = true;
9e8d8607
VZ
2182 }
2183}
951cd180 2184
6f91bc33
VZ
2185/* static */
2186void wxFileName::SplitPath(const wxString& fullpath,
2187 wxString *path,
2188 wxString *name,
2189 wxString *ext,
2190 wxPathFormat format)
2191{
2192 wxString volume;
2193 SplitPath(fullpath, &volume, path, name, ext, format);
2194
67c34f64 2195 if ( path )
6f91bc33 2196 {
67c34f64 2197 path->Prepend(wxGetVolumeString(volume, format));
6f91bc33
VZ
2198 }
2199}
2200
951cd180
VZ
2201// ----------------------------------------------------------------------------
2202// time functions
2203// ----------------------------------------------------------------------------
2204
e2b87f38
VZ
2205#if wxUSE_DATETIME
2206
6dbb903b
VZ
2207bool wxFileName::SetTimes(const wxDateTime *dtAccess,
2208 const wxDateTime *dtMod,
2209 const wxDateTime *dtCreate)
951cd180 2210{
2b5f62a0
VZ
2211#if defined(__WIN32__)
2212 if ( IsDir() )
2213 {
2214 // VZ: please let me know how to do this if you can
2215 wxFAIL_MSG( _T("SetTimes() not implemented for the directories") );
2216 }
2217 else // file
2218 {
2219 wxFileHandle fh(GetFullPath(), wxFileHandle::Write);
2220 if ( fh.IsOk() )
2221 {
2222 FILETIME ftAccess, ftCreate, ftWrite;
2223
2224 if ( dtCreate )
2225 ConvertWxToFileTime(&ftCreate, *dtCreate);
2226 if ( dtAccess )
2227 ConvertWxToFileTime(&ftAccess, *dtAccess);
2228 if ( dtMod )
2229 ConvertWxToFileTime(&ftWrite, *dtMod);
2230
2231 if ( ::SetFileTime(fh,
2232 dtCreate ? &ftCreate : NULL,
2233 dtAccess ? &ftAccess : NULL,
2234 dtMod ? &ftWrite : NULL) )
2235 {
f363e05c 2236 return true;
2b5f62a0
VZ
2237 }
2238 }
2239 }
2240#elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
17a1ebd1
VZ
2241 wxUnusedVar(dtCreate);
2242
246c704f
VZ
2243 if ( !dtAccess && !dtMod )
2244 {
2245 // can't modify the creation time anyhow, don't try
f363e05c 2246 return true;
246c704f
VZ
2247 }
2248
2249 // if dtAccess or dtMod is not specified, use the other one (which must be
2250 // non NULL because of the test above) for both times
951cd180 2251 utimbuf utm;
246c704f
VZ
2252 utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks();
2253 utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks();
401eb3de 2254 if ( utime(GetFullPath().fn_str(), &utm) == 0 )
951cd180 2255 {
f363e05c 2256 return true;
951cd180 2257 }
951cd180 2258#else // other platform
7a893a31
WS
2259 wxUnusedVar(dtAccess);
2260 wxUnusedVar(dtMod);
2261 wxUnusedVar(dtCreate);
951cd180
VZ
2262#endif // platforms
2263
2264 wxLogSysError(_("Failed to modify file times for '%s'"),
2265 GetFullPath().c_str());
2266
f363e05c 2267 return false;
951cd180
VZ
2268}
2269
2270bool wxFileName::Touch()
2271{
2272#if defined(__UNIX_LIKE__)
2273 // under Unix touching file is simple: just pass NULL to utime()
401eb3de 2274 if ( utime(GetFullPath().fn_str(), NULL) == 0 )
951cd180 2275 {
f363e05c 2276 return true;
951cd180
VZ
2277 }
2278
2279 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
2280
f363e05c 2281 return false;
951cd180
VZ
2282#else // other platform
2283 wxDateTime dtNow = wxDateTime::Now();
2284
6dbb903b 2285 return SetTimes(&dtNow, &dtNow, NULL /* don't change create time */);
951cd180
VZ
2286#endif // platforms
2287}
2288
2289bool wxFileName::GetTimes(wxDateTime *dtAccess,
2290 wxDateTime *dtMod,
6dbb903b 2291 wxDateTime *dtCreate) const
951cd180 2292{
2b5f62a0
VZ
2293#if defined(__WIN32__)
2294 // we must use different methods for the files and directories under
2295 // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and
2296 // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and
2297 // not 9x
2298 bool ok;
2299 FILETIME ftAccess, ftCreate, ftWrite;
9a0c5c01 2300 if ( IsDir() )
2b5f62a0
VZ
2301 {
2302 // implemented in msw/dir.cpp
2303 extern bool wxGetDirectoryTimes(const wxString& dirname,
2304 FILETIME *, FILETIME *, FILETIME *);
2305
2306 // we should pass the path without the trailing separator to
2307 // wxGetDirectoryTimes()
2308 ok = wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME),
2309 &ftAccess, &ftCreate, &ftWrite);
2310 }
2311 else // file
2312 {
2313 wxFileHandle fh(GetFullPath(), wxFileHandle::Read);
2314 if ( fh.IsOk() )
2315 {
2316 ok = ::GetFileTime(fh,
2317 dtCreate ? &ftCreate : NULL,
2318 dtAccess ? &ftAccess : NULL,
2319 dtMod ? &ftWrite : NULL) != 0;
2320 }
2321 else
2322 {
f363e05c 2323 ok = false;
2b5f62a0
VZ
2324 }
2325 }
2326
2327 if ( ok )
2328 {
2329 if ( dtCreate )
2330 ConvertFileTimeToWx(dtCreate, ftCreate);
2331 if ( dtAccess )
2332 ConvertFileTimeToWx(dtAccess, ftAccess);
2333 if ( dtMod )
2334 ConvertFileTimeToWx(dtMod, ftWrite);
2335
f363e05c 2336 return true;
2b5f62a0 2337 }
bf58daba 2338#elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__))
51cb1a65 2339 // no need to test for IsDir() here
951cd180 2340 wxStructStat stBuf;
92980e90 2341 if ( wxStat( GetFullPath().c_str(), &stBuf) == 0 )
951cd180
VZ
2342 {
2343 if ( dtAccess )
2344 dtAccess->Set(stBuf.st_atime);
2345 if ( dtMod )
2346 dtMod->Set(stBuf.st_mtime);
6dbb903b
VZ
2347 if ( dtCreate )
2348 dtCreate->Set(stBuf.st_ctime);
951cd180 2349
f363e05c 2350 return true;
951cd180 2351 }
951cd180 2352#else // other platform
7a893a31
WS
2353 wxUnusedVar(dtAccess);
2354 wxUnusedVar(dtMod);
2355 wxUnusedVar(dtCreate);
951cd180
VZ
2356#endif // platforms
2357
2358 wxLogSysError(_("Failed to retrieve file times for '%s'"),
2359 GetFullPath().c_str());
2360
f363e05c 2361 return false;
951cd180
VZ
2362}
2363
e2b87f38
VZ
2364#endif // wxUSE_DATETIME
2365
23b8a262
JS
2366
2367// ----------------------------------------------------------------------------
2368// file size functions
2369// ----------------------------------------------------------------------------
2370
bd08f2f7
VZ
2371#if wxUSE_LONGLONG
2372
23b8a262
JS
2373/* static */
2374wxULongLong wxFileName::GetSize(const wxString &filename)
2375{
2376 if (!wxFileExists(filename))
2377 return wxInvalidSize;
2378
cdbce971
WS
2379#if defined(__WXPALMOS__)
2380 // TODO
2381 return wxInvalidSize;
2382#elif defined(__WIN32__)
23b8a262
JS
2383 wxFileHandle f(filename, wxFileHandle::Read);
2384 if (!f.IsOk())
2385 return wxInvalidSize;
2386
2387 DWORD lpFileSizeHigh;
2388 DWORD ret = GetFileSize(f, &lpFileSizeHigh);
5ab9534b 2389 if ( ret == INVALID_FILE_SIZE && ::GetLastError() != NO_ERROR )
23b8a262
JS
2390 return wxInvalidSize;
2391
5ab9534b
VZ
2392 return wxULongLong(lpFileSizeHigh, ret);
2393#else // ! __WIN32__
23b8a262
JS
2394 wxStructStat st;
2395#ifndef wxNEED_WX_UNISTD_H
2396 if (wxStat( filename.fn_str() , &st) != 0)
2397#else
2398 if (wxStat( filename, &st) != 0)
2399#endif
2400 return wxInvalidSize;
2401 return wxULongLong(st.st_size);
2402#endif
2403}
2404
2405/* static */
2406wxString wxFileName::GetHumanReadableSize(const wxULongLong &bs,
2407 const wxString &nullsize,
2408 int precision)
2409{
2410 static const double KILOBYTESIZE = 1024.0;
2411 static const double MEGABYTESIZE = 1024.0*KILOBYTESIZE;
2412 static const double GIGABYTESIZE = 1024.0*MEGABYTESIZE;
2413 static const double TERABYTESIZE = 1024.0*GIGABYTESIZE;
2414
2415 if (bs == 0 || bs == wxInvalidSize)
2416 return nullsize;
2417
2418 double bytesize = bs.ToDouble();
2419 if (bytesize < KILOBYTESIZE)
2420 return wxString::Format(_("%s B"), bs.ToString().c_str());
2421 if (bytesize < MEGABYTESIZE)
2422 return wxString::Format(_("%.*f kB"), precision, bytesize/KILOBYTESIZE);
2423 if (bytesize < GIGABYTESIZE)
2424 return wxString::Format(_("%.*f MB"), precision, bytesize/MEGABYTESIZE);
2425 if (bytesize < TERABYTESIZE)
2426 return wxString::Format(_("%.*f GB"), precision, bytesize/GIGABYTESIZE);
2427
2428 return wxString::Format(_("%.*f TB"), precision, bytesize/TERABYTESIZE);
2429}
2430
2431wxULongLong wxFileName::GetSize() const
2432{
2433 return GetSize(GetFullPath());
2434}
2435
2436wxString wxFileName::GetHumanReadableSize(const wxString &failmsg, int precision) const
2437{
2438 return GetHumanReadableSize(GetSize(), failmsg, precision);
2439}
2440
bd08f2f7 2441#endif // wxUSE_LONGLONG
23b8a262
JS
2442
2443// ----------------------------------------------------------------------------
2444// Mac-specific functions
2445// ----------------------------------------------------------------------------
2446
26eef304 2447#if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
4dfbcdd5 2448
56ce942b
VZ
2449namespace
2450{
2451
0ca4ae93 2452class MacDefaultExtensionRecord
4dfbcdd5 2453{
56ce942b
VZ
2454public:
2455 MacDefaultExtensionRecord()
2456 {
2457 m_type =
2458 m_creator = 0 ;
2459 }
4dfbcdd5 2460
56ce942b 2461 // default copy ctor, assignment operator and dtor are ok
0ca4ae93 2462
56ce942b
VZ
2463 MacDefaultExtensionRecord(const wxString& ext, OSType type, OSType creator)
2464 : m_ext(ext)
2465 {
2466 m_type = type;
2467 m_creator = creator;
2468 }
2469
2470 wxString m_ext;
2471 OSType m_type;
2472 OSType m_creator;
2473};
2474
2475WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord, MacDefaultExtensionArray);
2476
2477bool gMacDefaultExtensionsInited = false;
0ca4ae93 2478
4dfbcdd5 2479#include "wx/arrimpl.cpp"
0ca4ae93 2480
56ce942b 2481WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray);
4dfbcdd5 2482
56ce942b 2483MacDefaultExtensionArray gMacDefaultExtensions;
4dfbcdd5 2484
5974c3cf 2485// load the default extensions
56ce942b 2486const MacDefaultExtensionRecord gDefaults[] =
4dfbcdd5 2487{
56ce942b
VZ
2488 MacDefaultExtensionRecord( "txt", 'TEXT', 'ttxt' ),
2489 MacDefaultExtensionRecord( "tif", 'TIFF', '****' ),
2490 MacDefaultExtensionRecord( "jpg", 'JPEG', '****' ),
2491};
0ea621cc 2492
56ce942b 2493void MacEnsureDefaultExtensionsLoaded()
5974c3cf
SC
2494{
2495 if ( !gMacDefaultExtensionsInited )
4dfbcdd5 2496 {
5974c3cf
SC
2497 // we could load the pc exchange prefs here too
2498 for ( size_t i = 0 ; i < WXSIZEOF( gDefaults ) ; ++i )
2499 {
2500 gMacDefaultExtensions.Add( gDefaults[i] ) ;
2501 }
56ce942b 2502 gMacDefaultExtensionsInited = true;
0ea621cc 2503 }
4dfbcdd5 2504}
a2b77260 2505
56ce942b
VZ
2506} // anonymous namespace
2507
0ea621cc 2508bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator )
4dfbcdd5 2509{
a2b77260
SC
2510 FSRef fsRef ;
2511 FSCatalogInfo catInfo;
2512 FileInfo *finfo ;
4dfbcdd5 2513
a2b77260
SC
2514 if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr )
2515 {
a62848fd
WS
2516 if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr )
2517 {
2518 finfo = (FileInfo*)&catInfo.finderInfo;
2519 finfo->fileType = type ;
2520 finfo->fileCreator = creator ;
2521 FSSetCatalogInfo( &fsRef, kFSCatInfoFinderInfo, &catInfo ) ;
a2b77260 2522 return true ;
a62848fd 2523 }
a2b77260
SC
2524 }
2525 return false ;
4dfbcdd5
SC
2526}
2527
0ea621cc 2528bool wxFileName::MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator )
4dfbcdd5 2529{
a2b77260
SC
2530 FSRef fsRef ;
2531 FSCatalogInfo catInfo;
2532 FileInfo *finfo ;
4dfbcdd5 2533
a2b77260
SC
2534 if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr )
2535 {
a62848fd
WS
2536 if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr )
2537 {
2538 finfo = (FileInfo*)&catInfo.finderInfo;
2539 *type = finfo->fileType ;
2540 *creator = finfo->fileCreator ;
a2b77260 2541 return true ;
a62848fd 2542 }
a2b77260
SC
2543 }
2544 return false ;
4dfbcdd5
SC
2545}
2546
2547bool wxFileName::MacSetDefaultTypeAndCreator()
2548{
2549 wxUint32 type , creator ;
2550 if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type ,
2551 &creator ) )
2552 {
f63fb56d
GD
2553 return MacSetTypeAndCreator( type , creator ) ;
2554 }
2555 return false;
4dfbcdd5
SC
2556}
2557
0ea621cc 2558bool wxFileName::MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator )
4dfbcdd5
SC
2559{
2560 MacEnsureDefaultExtensionsLoaded() ;
2561 wxString extl = ext.Lower() ;
2562 for( int i = gMacDefaultExtensions.Count() - 1 ; i >= 0 ; --i )
2563 {
2564 if ( gMacDefaultExtensions.Item(i).m_ext == extl )
2565 {
2566 *type = gMacDefaultExtensions.Item(i).m_type ;
2567 *creator = gMacDefaultExtensions.Item(i).m_creator ;
2568 return true ;
2569 }
2570 }
2571 return false ;
2572}
2573
2574void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator )
2575{
56ce942b
VZ
2576 MacEnsureDefaultExtensionsLoaded();
2577 MacDefaultExtensionRecord rec(ext.Lower(), type, creator);
2578 gMacDefaultExtensions.Add( rec );
4dfbcdd5 2579}
56ce942b
VZ
2580
2581#endif // defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON