1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/filename.cpp
3 // Purpose: wxFileName - encapsulates a file path
4 // Author: Robert Roebling, Vadim Zeitlin
8 // Copyright: (c) 2000 Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 Here are brief descriptions of the filename formats supported by this class:
15 wxPATH_UNIX: standard Unix format, used under Darwin as well, absolute file
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
21 wxPATH_DOS: DOS/Windows format, absolute file names have the form:
22 drive:\dir1\dir2\...\dirN\filename.ext where drive is a single
23 letter. "." and ".." as for Unix but no "~".
25 There are also UNC names of the form \\share\fullpath
27 wxPATH_MAC: Mac OS 8/9 and Mac OS X under CodeWarrior 7 format, absolute file
29 volume:dir1:...:dirN:filename
30 and the relative file names are either
31 :dir1:...:dirN:filename
34 (although :filename works as well).
35 Since the volume is just part of the file path, it is not
36 treated like a separate entity as it is done under DOS and
37 VMS, it is just treated as another dir.
39 wxPATH_VMS: VMS native format, absolute file names have the form
40 <device>:[dir1.dir2.dir3]file.txt
42 <device>:[000000.dir1.dir2.dir3]file.txt
44 the <device> is the physical device (i.e. disk). 000000 is the
45 root directory on the device which can be omitted.
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 specification
51 . separator between directory and subdirectory
52 ] between directory and file
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
64 #pragma implementation "filename.h"
67 // For compilers that support precompilation, includes "wx.h".
68 #include "wx/wxprec.h"
80 #include "wx/filename.h"
81 #include "wx/tokenzr.h"
82 #include "wx/config.h" // for wxExpandEnvVars
85 #include "wx/dynlib.h"
87 // For GetShort/LongPathName
89 #include "wx/msw/wrapwin.h"
90 #if defined(__MINGW32__)
91 #include "wx/msw/gccpriv.h"
96 #include "wx/msw/private.h"
99 #if defined(__WXMAC__)
100 #include "wx/mac/private.h" // includes mac headers
103 // utime() is POSIX so should normally be available on all Unices
105 #include <sys/types.h>
107 #include <sys/stat.h>
117 #include <sys/types.h>
119 #include <sys/stat.h>
130 #include <sys/utime.h>
131 #include <sys/stat.h>
142 #define MAX_PATH _MAX_PATH
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 // small helper class which opens and closes the file - we use it just to get
150 // a file handle for the given file name to pass it to some Win32 API function
151 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
162 wxFileHandle(const wxString
& filename
, OpenMode mode
)
164 m_hFile
= ::CreateFile
167 mode
== Read
? GENERIC_READ
// access mask
169 FILE_SHARE_READ
| // sharing mode
170 FILE_SHARE_WRITE
, // (allow everything)
171 NULL
, // no secutity attr
172 OPEN_EXISTING
, // creation disposition
174 NULL
// no template file
177 if ( m_hFile
== INVALID_HANDLE_VALUE
)
179 wxLogSysError(_("Failed to open '%s' for %s"),
181 mode
== Read
? _("reading") : _("writing"));
187 if ( m_hFile
!= INVALID_HANDLE_VALUE
)
189 if ( !::CloseHandle(m_hFile
) )
191 wxLogSysError(_("Failed to close file handle"));
196 // return true only if the file could be opened successfully
197 bool IsOk() const { return m_hFile
!= INVALID_HANDLE_VALUE
; }
200 operator HANDLE() const { return m_hFile
; }
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
212 #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
214 // convert between wxDateTime and FILETIME which is a 64-bit value representing
215 // the number of 100-nanosecond intervals since January 1, 1601.
217 static void ConvertFileTimeToWx(wxDateTime
*dt
, const FILETIME
&ft
)
219 FILETIME ftcopy
= ft
;
221 if ( !::FileTimeToLocalFileTime(&ftcopy
, &ftLocal
) )
223 wxLogLastError(_T("FileTimeToLocalFileTime"));
227 if ( !::FileTimeToSystemTime(&ftLocal
, &st
) )
229 wxLogLastError(_T("FileTimeToSystemTime"));
232 dt
->Set(st
.wDay
, wxDateTime::Month(st
.wMonth
- 1), st
.wYear
,
233 st
.wHour
, st
.wMinute
, st
.wSecond
, st
.wMilliseconds
);
236 static void ConvertWxToFileTime(FILETIME
*ft
, const wxDateTime
& dt
)
239 st
.wDay
= dt
.GetDay();
240 st
.wMonth
= (WORD
)(dt
.GetMonth() + 1);
241 st
.wYear
= (WORD
)dt
.GetYear();
242 st
.wHour
= dt
.GetHour();
243 st
.wMinute
= dt
.GetMinute();
244 st
.wSecond
= dt
.GetSecond();
245 st
.wMilliseconds
= dt
.GetMillisecond();
248 if ( !::SystemTimeToFileTime(&st
, &ftLocal
) )
250 wxLogLastError(_T("SystemTimeToFileTime"));
253 if ( !::LocalFileTimeToFileTime(&ftLocal
, ft
) )
255 wxLogLastError(_T("LocalFileTimeToFileTime"));
259 #endif // wxUSE_DATETIME && __WIN32__
261 // return a string with the volume par
262 static wxString
wxGetVolumeString(const wxString
& volume
, wxPathFormat format
)
266 if ( !volume
.empty() )
268 format
= wxFileName::GetFormat(format
);
270 // Special Windows UNC paths hack, part 2: undo what we did in
271 // SplitPath() and make an UNC path if we have a drive which is not a
272 // single letter (hopefully the network shares can't be one letter only
273 // although I didn't find any authoritative docs on this)
274 if ( format
== wxPATH_DOS
&& volume
.length() > 1 )
276 path
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_DOS
<< volume
;
278 else if ( format
== wxPATH_DOS
|| format
== wxPATH_VMS
)
280 path
<< volume
<< wxFileName::GetVolumeSeparator(format
);
288 // ============================================================================
290 // ============================================================================
292 // ----------------------------------------------------------------------------
293 // wxFileName construction
294 // ----------------------------------------------------------------------------
296 void wxFileName::Assign( const wxFileName
&filepath
)
298 m_volume
= filepath
.GetVolume();
299 m_dirs
= filepath
.GetDirs();
300 m_name
= filepath
.GetName();
301 m_ext
= filepath
.GetExt();
302 m_relative
= filepath
.m_relative
;
305 void wxFileName::Assign(const wxString
& volume
,
306 const wxString
& path
,
307 const wxString
& name
,
309 wxPathFormat format
)
311 SetPath( path
, format
);
318 void wxFileName::SetPath( const wxString
& pathOrig
, wxPathFormat format
)
322 if ( pathOrig
.empty() )
330 format
= GetFormat( format
);
332 // 0) deal with possible volume part first
335 SplitVolume(pathOrig
, &volume
, &path
, format
);
336 if ( !volume
.empty() )
343 // 1) Determine if the path is relative or absolute.
344 wxChar leadingChar
= path
[0u];
349 m_relative
= leadingChar
== wxT(':');
351 // We then remove a leading ":". The reason is in our
352 // storage form for relative paths:
353 // ":dir:file.txt" actually means "./dir/file.txt" in
354 // DOS notation and should get stored as
355 // (relative) (dir) (file.txt)
356 // "::dir:file.txt" actually means "../dir/file.txt"
357 // stored as (relative) (..) (dir) (file.txt)
358 // This is important only for the Mac as an empty dir
359 // actually means <UP>, whereas under DOS, double
360 // slashes can be ignored: "\\\\" is the same as "\\".
366 // TODO: what is the relative path format here?
371 wxFAIL_MSG( _T("Unknown path format") );
372 // !! Fall through !!
375 // the paths of the form "~" or "~username" are absolute
376 m_relative
= leadingChar
!= wxT('/') && leadingChar
!= _T('~');
380 m_relative
= !IsPathSeparator(leadingChar
, format
);
385 // 2) Break up the path into its members. If the original path
386 // was just "/" or "\\", m_dirs will be empty. We know from
387 // the m_relative field, if this means "nothing" or "root dir".
389 wxStringTokenizer
tn( path
, GetPathSeparators(format
) );
391 while ( tn
.HasMoreTokens() )
393 wxString token
= tn
.GetNextToken();
395 // Remove empty token under DOS and Unix, interpret them
399 if (format
== wxPATH_MAC
)
400 m_dirs
.Add( wxT("..") );
410 void wxFileName::Assign(const wxString
& fullpath
,
413 wxString volume
, path
, name
, ext
;
414 SplitPath(fullpath
, &volume
, &path
, &name
, &ext
, format
);
416 Assign(volume
, path
, name
, ext
, format
);
419 void wxFileName::Assign(const wxString
& fullpathOrig
,
420 const wxString
& fullname
,
423 // always recognize fullpath as directory, even if it doesn't end with a
425 wxString fullpath
= fullpathOrig
;
426 if ( !wxEndsWithPathSeparator(fullpath
) )
428 fullpath
+= GetPathSeparator(format
);
431 wxString volume
, path
, name
, ext
;
433 // do some consistency checks in debug mode: the name should be really just
434 // the filename and the path should be really just a path
436 wxString pathDummy
, nameDummy
, extDummy
;
438 SplitPath(fullname
, &pathDummy
, &name
, &ext
, format
);
440 wxASSERT_MSG( pathDummy
.empty(),
441 _T("the file name shouldn't contain the path") );
443 SplitPath(fullpath
, &volume
, &path
, &nameDummy
, &extDummy
, format
);
445 wxASSERT_MSG( nameDummy
.empty() && extDummy
.empty(),
446 _T("the path shouldn't contain file name nor extension") );
448 #else // !__WXDEBUG__
449 SplitPath(fullname
, NULL
/* no path */, &name
, &ext
, format
);
450 SplitPath(fullpath
, &volume
, &path
, NULL
, NULL
, format
);
451 #endif // __WXDEBUG__/!__WXDEBUG__
453 Assign(volume
, path
, name
, ext
, format
);
456 void wxFileName::Assign(const wxString
& pathOrig
,
457 const wxString
& name
,
463 SplitVolume(pathOrig
, &volume
, &path
, format
);
465 Assign(volume
, path
, name
, ext
, format
);
468 void wxFileName::AssignDir(const wxString
& dir
, wxPathFormat format
)
470 Assign(dir
, wxEmptyString
, format
);
473 void wxFileName::Clear()
479 m_ext
= wxEmptyString
;
481 // we don't have any absolute path for now
486 wxFileName
wxFileName::FileName(const wxString
& file
, wxPathFormat format
)
488 return wxFileName(file
, format
);
492 wxFileName
wxFileName::DirName(const wxString
& dir
, wxPathFormat format
)
495 fn
.AssignDir(dir
, format
);
499 // ----------------------------------------------------------------------------
501 // ----------------------------------------------------------------------------
503 bool wxFileName::FileExists() const
505 return wxFileName::FileExists( GetFullPath() );
508 bool wxFileName::FileExists( const wxString
&file
)
510 return ::wxFileExists( file
);
513 bool wxFileName::DirExists() const
515 return wxFileName::DirExists( GetFullPath() );
518 bool wxFileName::DirExists( const wxString
&dir
)
520 return ::wxDirExists( dir
);
523 // ----------------------------------------------------------------------------
524 // CWD and HOME stuff
525 // ----------------------------------------------------------------------------
527 void wxFileName::AssignCwd(const wxString
& volume
)
529 AssignDir(wxFileName::GetCwd(volume
));
533 wxString
wxFileName::GetCwd(const wxString
& volume
)
535 // if we have the volume, we must get the current directory on this drive
536 // and to do this we have to chdir to this volume - at least under Windows,
537 // I don't know how to get the current drive on another volume elsewhere
540 if ( !volume
.empty() )
543 SetCwd(volume
+ GetVolumeSeparator());
546 wxString cwd
= ::wxGetCwd();
548 if ( !volume
.empty() )
556 bool wxFileName::SetCwd()
558 return wxFileName::SetCwd( GetFullPath() );
561 bool wxFileName::SetCwd( const wxString
&cwd
)
563 return ::wxSetWorkingDirectory( cwd
);
566 void wxFileName::AssignHomeDir()
568 AssignDir(wxFileName::GetHomeDir());
571 wxString
wxFileName::GetHomeDir()
573 return ::wxGetHomeDir();
578 void wxFileName::AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
580 wxString tempname
= CreateTempFileName(prefix
, fileTemp
);
581 if ( tempname
.empty() )
583 // error, failed to get temp file name
594 wxFileName::CreateTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
596 wxString path
, dir
, name
;
598 // use the directory specified by the prefix
599 SplitPath(prefix
, &dir
, &name
, NULL
/* extension */);
601 #if defined(__WXWINCE__)
604 // FIXME. Create \temp dir?
607 path
= dir
+ wxT("\\") + prefix
;
609 while (FileExists(path
))
611 path
= dir
+ wxT("\\") + prefix
;
616 #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
620 if ( !::GetTempPath(MAX_PATH
, wxStringBuffer(dir
, MAX_PATH
+ 1)) )
622 wxLogLastError(_T("GetTempPath"));
627 // GetTempFileName() fails if we pass it an empty string
631 else // we have a dir to create the file in
633 // ensure we use only the back slashes as GetTempFileName(), unlike all
634 // the other APIs, is picky and doesn't accept the forward ones
635 dir
.Replace(_T("/"), _T("\\"));
638 if ( !::GetTempFileName(dir
, name
, 0, wxStringBuffer(path
, MAX_PATH
+ 1)) )
640 wxLogLastError(_T("GetTempFileName"));
648 #if defined(__WXMAC__) && !defined(__DARWIN__)
649 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
651 dir
= wxGetenv(_T("TMP"));
654 dir
= wxGetenv(_T("TEMP"));
660 #if defined(__DOS__) || defined(__OS2__)
671 if ( !wxEndsWithPathSeparator(dir
) &&
672 (name
.empty() || !wxIsPathSeparator(name
[0u])) )
674 path
+= wxFILE_SEP_PATH
;
679 #if defined(HAVE_MKSTEMP)
680 // scratch space for mkstemp()
681 path
+= _T("XXXXXX");
683 // we need to copy the path to the buffer in which mkstemp() can modify it
684 wxCharBuffer
buf( wxConvFile
.cWX2MB( path
) );
686 // cast is safe because the string length doesn't change
687 int fdTemp
= mkstemp( (char*)(const char*) buf
);
690 // this might be not necessary as mkstemp() on most systems should have
691 // already done it but it doesn't hurt neither...
694 else // mkstemp() succeeded
696 path
= wxConvFile
.cMB2WX( (const char*) buf
);
698 // avoid leaking the fd
701 fileTemp
->Attach(fdTemp
);
708 #else // !HAVE_MKSTEMP
712 path
+= _T("XXXXXX");
714 wxCharBuffer buf
= wxConvFile
.cWX2MB( path
);
715 if ( !mktemp( (const char*) buf
) )
721 path
= wxConvFile
.cMB2WX( (const char*) buf
);
723 #else // !HAVE_MKTEMP (includes __DOS__)
724 // generate the unique file name ourselves
725 #if !defined(__DOS__) && !defined(__PALMOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
726 path
<< (unsigned int)getpid();
731 static const size_t numTries
= 1000;
732 for ( size_t n
= 0; n
< numTries
; n
++ )
734 // 3 hex digits is enough for numTries == 1000 < 4096
735 pathTry
= path
+ wxString::Format(_T("%.03x"), (unsigned int) n
);
736 if ( !FileExists(pathTry
) )
745 #endif // HAVE_MKTEMP/!HAVE_MKTEMP
750 #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
752 #endif // Windows/!Windows
756 wxLogSysError(_("Failed to create a temporary file name"));
758 else if ( fileTemp
&& !fileTemp
->IsOpened() )
760 // open the file - of course, there is a race condition here, this is
761 // why we always prefer using mkstemp()...
763 // NB: GetTempFileName() under Windows creates the file, so using
764 // write_excl there would fail
765 if ( !fileTemp
->Open(path
,
766 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
771 wxS_IRUSR
| wxS_IWUSR
) )
773 // FIXME: If !ok here should we loop and try again with another
774 // file name? That is the standard recourse if open(O_EXCL)
775 // fails, though of course it should be protected against
776 // possible infinite looping too.
778 wxLogError(_("Failed to open temporary file."));
789 // ----------------------------------------------------------------------------
790 // directory operations
791 // ----------------------------------------------------------------------------
793 bool wxFileName::Mkdir( int perm
, int flags
)
795 return wxFileName::Mkdir( GetFullPath(), perm
, flags
);
798 bool wxFileName::Mkdir( const wxString
& dir
, int perm
, int flags
)
800 if ( flags
& wxPATH_MKDIR_FULL
)
802 // split the path in components
804 filename
.AssignDir(dir
);
807 if ( filename
.HasVolume())
809 currPath
<< wxGetVolumeString(filename
.GetVolume(), wxPATH_NATIVE
);
812 wxArrayString dirs
= filename
.GetDirs();
813 size_t count
= dirs
.GetCount();
814 for ( size_t i
= 0; i
< count
; i
++ )
817 #if defined(__WXMAC__) && !defined(__DARWIN__)
818 // relative pathnames are exactely the other way round under mac...
819 !filename
.IsAbsolute()
821 filename
.IsAbsolute()
824 currPath
+= wxFILE_SEP_PATH
;
827 if (!DirExists(currPath
))
829 if (!wxMkdir(currPath
, perm
))
831 // no need to try creating further directories
841 return ::wxMkdir( dir
, perm
);
844 bool wxFileName::Rmdir()
846 return wxFileName::Rmdir( GetFullPath() );
849 bool wxFileName::Rmdir( const wxString
&dir
)
851 return ::wxRmdir( dir
);
854 // ----------------------------------------------------------------------------
855 // path normalization
856 // ----------------------------------------------------------------------------
858 bool wxFileName::Normalize(int flags
,
862 // deal with env vars renaming first as this may seriously change the path
863 if ( flags
& wxPATH_NORM_ENV_VARS
)
865 wxString pathOrig
= GetFullPath(format
);
866 wxString path
= wxExpandEnvVars(pathOrig
);
867 if ( path
!= pathOrig
)
874 // the existing path components
875 wxArrayString dirs
= GetDirs();
877 // the path to prepend in front to make the path absolute
880 format
= GetFormat(format
);
882 // make the path absolute
883 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute(format
) )
887 curDir
.AssignCwd(GetVolume());
891 curDir
.AssignDir(cwd
);
894 // the path may be not absolute because it doesn't have the volume name
895 // but in this case we shouldn't modify the directory components of it
896 // but just set the current volume
897 if ( !HasVolume() && curDir
.HasVolume() )
899 SetVolume(curDir
.GetVolume());
903 // yes, it was the case - we don't need curDir then
909 // handle ~ stuff under Unix only
910 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
912 if ( !dirs
.IsEmpty() )
914 wxString dir
= dirs
[0u];
915 if ( !dir
.empty() && dir
[0u] == _T('~') )
917 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
924 // transform relative path into abs one
927 wxArrayString dirsNew
= curDir
.GetDirs();
928 size_t count
= dirs
.GetCount();
929 for ( size_t n
= 0; n
< count
; n
++ )
931 dirsNew
.Add(dirs
[n
]);
937 // now deal with ".", ".." and the rest
939 size_t count
= dirs
.GetCount();
940 for ( size_t n
= 0; n
< count
; n
++ )
942 wxString dir
= dirs
[n
];
944 if ( flags
& wxPATH_NORM_DOTS
)
946 if ( dir
== wxT(".") )
952 if ( dir
== wxT("..") )
954 if ( m_dirs
.IsEmpty() )
956 wxLogError(_("The path '%s' contains too many \"..\"!"),
957 GetFullPath().c_str());
961 m_dirs
.RemoveAt(m_dirs
.GetCount() - 1);
966 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
974 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
975 if ( (flags
& wxPATH_NORM_SHORTCUT
) )
978 if (GetShortcutTarget(GetFullPath(format
), filename
))
980 // Repeat this since we may now have a new path
981 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
983 filename
.MakeLower();
991 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
993 // VZ: expand env vars here too?
995 m_volume
.MakeLower();
1000 // we do have the path now
1002 // NB: need to do this before (maybe) calling Assign() below
1005 #if defined(__WIN32__)
1006 if ( (flags
& wxPATH_NORM_LONG
) && (format
== wxPATH_DOS
) )
1008 Assign(GetLongPath());
1015 // ----------------------------------------------------------------------------
1016 // get the shortcut target
1017 // ----------------------------------------------------------------------------
1019 // WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions.
1020 // The .lnk file is a plain text file so it should be easy to
1021 // make it work. Hint from Google Groups:
1022 // "If you open up a lnk file, you'll see a
1023 // number, followed by a pound sign (#), followed by more text. The
1024 // number is the number of characters that follows the pound sign. The
1025 // characters after the pound sign are the command line (which _can_
1026 // include arguments) to be executed. Any path (e.g. \windows\program
1027 // files\myapp.exe) that includes spaces needs to be enclosed in
1028 // quotation marks."
1030 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
1031 // The following lines are necessary under WinCE
1032 // #include "wx/msw/private.h"
1033 // #include <ole2.h>
1035 #if defined(__WXWINCE__)
1036 #include <shlguid.h>
1039 bool wxFileName::GetShortcutTarget(const wxString
& shortcutPath
, wxString
& targetFilename
, wxString
* arguments
)
1041 wxString path
, file
, ext
;
1042 wxSplitPath(shortcutPath
, & path
, & file
, & ext
);
1046 bool success
= false;
1048 // Assume it's not a shortcut if it doesn't end with lnk
1049 if (ext
.Lower() != wxT("lnk"))
1052 // create a ShellLink object
1053 hres
= CoCreateInstance(CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
,
1054 IID_IShellLink
, (LPVOID
*) &psl
);
1056 if (SUCCEEDED(hres
))
1059 hres
= psl
->QueryInterface( IID_IPersistFile
, (LPVOID
*) &ppf
);
1060 if (SUCCEEDED(hres
))
1062 WCHAR wsz
[MAX_PATH
];
1064 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, shortcutPath
.mb_str(), -1, wsz
,
1067 hres
= ppf
->Load(wsz
, 0);
1068 if (SUCCEEDED(hres
))
1071 // Wrong prototype in early versions
1072 #if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2)
1073 psl
->GetPath((CHAR
*) buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1075 psl
->GetPath(buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1077 targetFilename
= wxString(buf
);
1078 success
= (shortcutPath
!= targetFilename
);
1080 psl
->GetArguments(buf
, 2048);
1082 if (!args
.empty() && arguments
)
1095 // ----------------------------------------------------------------------------
1096 // absolute/relative paths
1097 // ----------------------------------------------------------------------------
1099 bool wxFileName::IsAbsolute(wxPathFormat format
) const
1101 // if our path doesn't start with a path separator, it's not an absolute
1106 if ( !GetVolumeSeparator(format
).empty() )
1108 // this format has volumes and an absolute path must have one, it's not
1109 // enough to have the full path to bean absolute file under Windows
1110 if ( GetVolume().empty() )
1117 bool wxFileName::MakeRelativeTo(const wxString
& pathBase
, wxPathFormat format
)
1119 wxFileName fnBase
= wxFileName::DirName(pathBase
, format
);
1121 // get cwd only once - small time saving
1122 wxString cwd
= wxGetCwd();
1123 Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1124 fnBase
.Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1126 bool withCase
= IsCaseSensitive(format
);
1128 // we can't do anything if the files live on different volumes
1129 if ( !GetVolume().IsSameAs(fnBase
.GetVolume(), withCase
) )
1135 // same drive, so we don't need our volume
1138 // remove common directories starting at the top
1139 while ( !m_dirs
.IsEmpty() && !fnBase
.m_dirs
.IsEmpty() &&
1140 m_dirs
[0u].IsSameAs(fnBase
.m_dirs
[0u], withCase
) )
1143 fnBase
.m_dirs
.RemoveAt(0);
1146 // add as many ".." as needed
1147 size_t count
= fnBase
.m_dirs
.GetCount();
1148 for ( size_t i
= 0; i
< count
; i
++ )
1150 m_dirs
.Insert(wxT(".."), 0u);
1153 if ( format
== wxPATH_UNIX
|| format
== wxPATH_DOS
)
1155 // a directory made relative with respect to itself is '.' under Unix
1156 // and DOS, by definition (but we don't have to insert "./" for the
1158 if ( m_dirs
.IsEmpty() && IsDir() )
1160 m_dirs
.Add(_T('.'));
1170 // ----------------------------------------------------------------------------
1171 // filename kind tests
1172 // ----------------------------------------------------------------------------
1174 bool wxFileName::SameAs(const wxFileName
& filepath
, wxPathFormat format
) const
1176 wxFileName fn1
= *this,
1179 // get cwd only once - small time saving
1180 wxString cwd
= wxGetCwd();
1181 fn1
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1182 fn2
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1184 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
1187 // TODO: compare inodes for Unix, this works even when filenames are
1188 // different but files are the same (symlinks) (VZ)
1194 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
1196 // only Unix filenames are truely case-sensitive
1197 return GetFormat(format
) == wxPATH_UNIX
;
1201 wxString
wxFileName::GetForbiddenChars(wxPathFormat format
)
1203 // Inits to forbidden characters that are common to (almost) all platforms.
1204 wxString strForbiddenChars
= wxT("*?");
1206 // If asserts, wxPathFormat has been changed. In case of a new path format
1207 // addition, the following code might have to be updated.
1208 wxCOMPILE_TIME_ASSERT(wxPATH_MAX
== 5, wxPathFormatChanged
);
1209 switch ( GetFormat(format
) )
1212 wxFAIL_MSG( wxT("Unknown path format") );
1213 // !! Fall through !!
1219 // On a Mac even names with * and ? are allowed (Tested with OS
1220 // 9.2.1 and OS X 10.2.5)
1221 strForbiddenChars
= wxEmptyString
;
1225 strForbiddenChars
+= wxT("\\/:\"<>|");
1232 return strForbiddenChars
;
1236 wxString
wxFileName::GetVolumeSeparator(wxPathFormat format
)
1240 if ( (GetFormat(format
) == wxPATH_DOS
) ||
1241 (GetFormat(format
) == wxPATH_VMS
) )
1243 sepVol
= wxFILE_SEP_DSK
;
1251 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
1254 switch ( GetFormat(format
) )
1257 // accept both as native APIs do but put the native one first as
1258 // this is the one we use in GetFullPath()
1259 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
1263 wxFAIL_MSG( _T("Unknown wxPATH_XXX style") );
1267 seps
= wxFILE_SEP_PATH_UNIX
;
1271 seps
= wxFILE_SEP_PATH_MAC
;
1275 seps
= wxFILE_SEP_PATH_VMS
;
1283 wxString
wxFileName::GetPathTerminators(wxPathFormat format
)
1285 format
= GetFormat(format
);
1287 // under VMS the end of the path is ']', not the path separator used to
1288 // separate the components
1289 return format
== wxPATH_VMS
? wxString(_T(']')) : GetPathSeparators(format
);
1293 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
1295 // wxString::Find() doesn't work as expected with NUL - it will always find
1296 // it, so test for it separately
1297 return ch
!= _T('\0') && GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
1300 // ----------------------------------------------------------------------------
1301 // path components manipulation
1302 // ----------------------------------------------------------------------------
1304 /* static */ bool wxFileName::IsValidDirComponent(const wxString
& dir
)
1308 wxFAIL_MSG( _T("empty directory passed to wxFileName::InsertDir()") );
1313 const size_t len
= dir
.length();
1314 for ( size_t n
= 0; n
< len
; n
++ )
1316 if ( dir
[n
] == GetVolumeSeparator() || IsPathSeparator(dir
[n
]) )
1318 wxFAIL_MSG( _T("invalid directory component in wxFileName") );
1327 void wxFileName::AppendDir( const wxString
& dir
)
1329 if ( IsValidDirComponent(dir
) )
1333 void wxFileName::PrependDir( const wxString
& dir
)
1338 void wxFileName::InsertDir(size_t before
, const wxString
& dir
)
1340 if ( IsValidDirComponent(dir
) )
1341 m_dirs
.Insert(dir
, before
);
1344 void wxFileName::RemoveDir(size_t pos
)
1346 m_dirs
.RemoveAt(pos
);
1349 // ----------------------------------------------------------------------------
1351 // ----------------------------------------------------------------------------
1353 void wxFileName::SetFullName(const wxString
& fullname
)
1355 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
1358 wxString
wxFileName::GetFullName() const
1360 wxString fullname
= m_name
;
1361 if ( !m_ext
.empty() )
1363 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
1369 wxString
wxFileName::GetPath( int flags
, wxPathFormat format
) const
1371 format
= GetFormat( format
);
1375 // return the volume with the path as well if requested
1376 if ( flags
& wxPATH_GET_VOLUME
)
1378 fullpath
+= wxGetVolumeString(GetVolume(), format
);
1381 // the leading character
1386 fullpath
+= wxFILE_SEP_PATH_MAC
;
1391 fullpath
+= wxFILE_SEP_PATH_DOS
;
1395 wxFAIL_MSG( wxT("Unknown path format") );
1401 // normally the absolute file names start with a slash
1402 // with one exception: the ones like "~/foo.bar" don't
1404 if ( m_dirs
.IsEmpty() || m_dirs
[0u] != _T('~') )
1406 fullpath
+= wxFILE_SEP_PATH_UNIX
;
1412 // no leading character here but use this place to unset
1413 // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense
1414 // as, if I understand correctly, there should never be a dot
1415 // before the closing bracket
1416 flags
&= ~wxPATH_GET_SEPARATOR
;
1419 if ( m_dirs
.empty() )
1421 // there is nothing more
1425 // then concatenate all the path components using the path separator
1426 if ( format
== wxPATH_VMS
)
1428 fullpath
+= wxT('[');
1431 const size_t dirCount
= m_dirs
.GetCount();
1432 for ( size_t i
= 0; i
< dirCount
; i
++ )
1437 if ( m_dirs
[i
] == wxT(".") )
1439 // skip appending ':', this shouldn't be done in this
1440 // case as "::" is interpreted as ".." under Unix
1444 // convert back from ".." to nothing
1445 if ( m_dirs
[i
] != wxT("..") )
1446 fullpath
+= m_dirs
[i
];
1450 wxFAIL_MSG( wxT("Unexpected path format") );
1451 // still fall through
1455 fullpath
+= m_dirs
[i
];
1459 // TODO: What to do with ".." under VMS
1461 // convert back from ".." to nothing
1462 if ( m_dirs
[i
] != wxT("..") )
1463 fullpath
+= m_dirs
[i
];
1467 if ( (flags
& wxPATH_GET_SEPARATOR
) || (i
!= dirCount
- 1) )
1468 fullpath
+= GetPathSeparator(format
);
1471 if ( format
== wxPATH_VMS
)
1473 fullpath
+= wxT(']');
1479 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
1481 // we already have a function to get the path
1482 wxString fullpath
= GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
,
1485 // now just add the file name and extension to it
1486 fullpath
+= GetFullName();
1491 // Return the short form of the path (returns identity on non-Windows platforms)
1492 wxString
wxFileName::GetShortPath() const
1494 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
1495 wxString
path(GetFullPath());
1497 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
1501 ok
= ::GetShortPathName
1504 wxStringBuffer(pathOut
, sz
),
1513 return GetFullPath();
1517 // Return the long form of the path (returns identity on non-Windows platforms)
1518 wxString
wxFileName::GetLongPath() const
1521 path
= GetFullPath();
1523 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1524 bool success
= false;
1526 #if wxUSE_DYNAMIC_LOADER
1527 typedef DWORD (WINAPI
*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
1529 static bool s_triedToLoad
= false;
1531 if ( !s_triedToLoad
)
1533 // suppress the errors about missing GetLongPathName[AW]
1536 s_triedToLoad
= true;
1537 wxDynamicLibrary
dllKernel(_T("kernel32"));
1538 if ( dllKernel
.IsLoaded() )
1540 // may succeed or fail depending on the Windows version
1541 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
1543 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) dllKernel
.GetSymbol(_T("GetLongPathNameW"));
1545 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) dllKernel
.GetSymbol(_T("GetLongPathNameA"));
1548 if ( s_pfnGetLongPathName
)
1550 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1551 bool ok
= dwSize
> 0;
1555 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1559 ok
= (*s_pfnGetLongPathName
)
1562 wxStringBuffer(pathOut
, sz
),
1574 #endif // wxUSE_DYNAMIC_LOADER
1578 // The OS didn't support GetLongPathName, or some other error.
1579 // We need to call FindFirstFile on each component in turn.
1581 WIN32_FIND_DATA findFileData
;
1585 pathOut
= GetVolume() +
1586 GetVolumeSeparator(wxPATH_DOS
) +
1587 GetPathSeparator(wxPATH_DOS
);
1589 pathOut
= wxEmptyString
;
1591 wxArrayString dirs
= GetDirs();
1592 dirs
.Add(GetFullName());
1596 size_t count
= dirs
.GetCount();
1597 for ( size_t i
= 0; i
< count
; i
++ )
1599 // We're using pathOut to collect the long-name path, but using a
1600 // temporary for appending the last path component which may be
1602 tmpPath
= pathOut
+ dirs
[i
];
1604 if ( tmpPath
.empty() )
1607 // can't see this being necessary? MF
1608 if ( tmpPath
.Last() == GetVolumeSeparator(wxPATH_DOS
) )
1610 // Can't pass a drive and root dir to FindFirstFile,
1611 // so continue to next dir
1612 tmpPath
+= wxFILE_SEP_PATH
;
1617 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
1618 if (hFind
== INVALID_HANDLE_VALUE
)
1620 // Error: most likely reason is that path doesn't exist, so
1621 // append any unprocessed parts and return
1622 for ( i
+= 1; i
< count
; i
++ )
1623 tmpPath
+= wxFILE_SEP_PATH
+ dirs
[i
];
1628 pathOut
+= findFileData
.cFileName
;
1629 if ( (i
< (count
-1)) )
1630 pathOut
+= wxFILE_SEP_PATH
;
1637 #endif // Win32/!Win32
1642 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
1644 if (format
== wxPATH_NATIVE
)
1646 #if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__)
1647 format
= wxPATH_DOS
;
1648 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1649 format
= wxPATH_MAC
;
1650 #elif defined(__VMS)
1651 format
= wxPATH_VMS
;
1653 format
= wxPATH_UNIX
;
1659 // ----------------------------------------------------------------------------
1660 // path splitting function
1661 // ----------------------------------------------------------------------------
1665 wxFileName::SplitVolume(const wxString
& fullpathWithVolume
,
1666 wxString
*pstrVolume
,
1668 wxPathFormat format
)
1670 format
= GetFormat(format
);
1672 wxString fullpath
= fullpathWithVolume
;
1674 // special Windows UNC paths hack: transform \\share\path into share:path
1675 if ( format
== wxPATH_DOS
)
1677 if ( fullpath
.length() >= 4 &&
1678 fullpath
[0u] == wxFILE_SEP_PATH_DOS
&&
1679 fullpath
[1u] == wxFILE_SEP_PATH_DOS
)
1681 fullpath
.erase(0, 2);
1683 size_t posFirstSlash
=
1684 fullpath
.find_first_of(GetPathTerminators(format
));
1685 if ( posFirstSlash
!= wxString::npos
)
1687 fullpath
[posFirstSlash
] = wxFILE_SEP_DSK
;
1689 // UNC paths are always absolute, right? (FIXME)
1690 fullpath
.insert(posFirstSlash
+ 1, 1, wxFILE_SEP_PATH_DOS
);
1695 // We separate the volume here
1696 if ( format
== wxPATH_DOS
|| format
== wxPATH_VMS
)
1698 wxString sepVol
= GetVolumeSeparator(format
);
1700 size_t posFirstColon
= fullpath
.find_first_of(sepVol
);
1701 if ( posFirstColon
!= wxString::npos
)
1705 *pstrVolume
= fullpath
.Left(posFirstColon
);
1708 // remove the volume name and the separator from the full path
1709 fullpath
.erase(0, posFirstColon
+ sepVol
.length());
1714 *pstrPath
= fullpath
;
1718 void wxFileName::SplitPath(const wxString
& fullpathWithVolume
,
1719 wxString
*pstrVolume
,
1723 wxPathFormat format
)
1725 format
= GetFormat(format
);
1728 SplitVolume(fullpathWithVolume
, pstrVolume
, &fullpath
, format
);
1730 // find the positions of the last dot and last path separator in the path
1731 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
1732 size_t posLastSlash
= fullpath
.find_last_of(GetPathTerminators(format
));
1734 // check whether this dot occurs at the very beginning of a path component
1735 if ( (posLastDot
!= wxString::npos
) &&
1737 IsPathSeparator(fullpath
[posLastDot
- 1]) ||
1738 (format
== wxPATH_VMS
&& fullpath
[posLastDot
- 1] == _T(']'))) )
1740 // dot may be (and commonly -- at least under Unix -- is) the first
1741 // character of the filename, don't treat the entire filename as
1742 // extension in this case
1743 posLastDot
= wxString::npos
;
1746 // if we do have a dot and a slash, check that the dot is in the name part
1747 if ( (posLastDot
!= wxString::npos
) &&
1748 (posLastSlash
!= wxString::npos
) &&
1749 (posLastDot
< posLastSlash
) )
1751 // the dot is part of the path, not the start of the extension
1752 posLastDot
= wxString::npos
;
1755 // now fill in the variables provided by user
1758 if ( posLastSlash
== wxString::npos
)
1765 // take everything up to the path separator but take care to make
1766 // the path equal to something like '/', not empty, for the files
1767 // immediately under root directory
1768 size_t len
= posLastSlash
;
1770 // this rule does not apply to mac since we do not start with colons (sep)
1771 // except for relative paths
1772 if ( !len
&& format
!= wxPATH_MAC
)
1775 *pstrPath
= fullpath
.Left(len
);
1777 // special VMS hack: remove the initial bracket
1778 if ( format
== wxPATH_VMS
)
1780 if ( (*pstrPath
)[0u] == _T('[') )
1781 pstrPath
->erase(0, 1);
1788 // take all characters starting from the one after the last slash and
1789 // up to, but excluding, the last dot
1790 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
1792 if ( posLastDot
== wxString::npos
)
1794 // take all until the end
1795 count
= wxString::npos
;
1797 else if ( posLastSlash
== wxString::npos
)
1801 else // have both dot and slash
1803 count
= posLastDot
- posLastSlash
- 1;
1806 *pstrName
= fullpath
.Mid(nStart
, count
);
1811 if ( posLastDot
== wxString::npos
)
1818 // take everything after the dot
1819 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
1825 void wxFileName::SplitPath(const wxString
& fullpath
,
1829 wxPathFormat format
)
1832 SplitPath(fullpath
, &volume
, path
, name
, ext
, format
);
1836 path
->Prepend(wxGetVolumeString(volume
, format
));
1840 // ----------------------------------------------------------------------------
1842 // ----------------------------------------------------------------------------
1846 bool wxFileName::SetTimes(const wxDateTime
*dtAccess
,
1847 const wxDateTime
*dtMod
,
1848 const wxDateTime
*dtCreate
)
1850 #if defined(__WIN32__)
1853 // VZ: please let me know how to do this if you can
1854 wxFAIL_MSG( _T("SetTimes() not implemented for the directories") );
1858 wxFileHandle
fh(GetFullPath(), wxFileHandle::Write
);
1861 FILETIME ftAccess
, ftCreate
, ftWrite
;
1864 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
1866 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
1868 ConvertWxToFileTime(&ftWrite
, *dtMod
);
1870 if ( ::SetFileTime(fh
,
1871 dtCreate
? &ftCreate
: NULL
,
1872 dtAccess
? &ftAccess
: NULL
,
1873 dtMod
? &ftWrite
: NULL
) )
1879 #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
1880 if ( !dtAccess
&& !dtMod
)
1882 // can't modify the creation time anyhow, don't try
1886 // if dtAccess or dtMod is not specified, use the other one (which must be
1887 // non NULL because of the test above) for both times
1889 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
1890 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
1891 if ( utime(GetFullPath().fn_str(), &utm
) == 0 )
1895 #else // other platform
1898 wxLogSysError(_("Failed to modify file times for '%s'"),
1899 GetFullPath().c_str());
1904 bool wxFileName::Touch()
1906 #if defined(__UNIX_LIKE__)
1907 // under Unix touching file is simple: just pass NULL to utime()
1908 if ( utime(GetFullPath().fn_str(), NULL
) == 0 )
1913 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1916 #else // other platform
1917 wxDateTime dtNow
= wxDateTime::Now();
1919 return SetTimes(&dtNow
, &dtNow
, NULL
/* don't change create time */);
1923 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
1925 wxDateTime
*dtCreate
) const
1927 #if defined(__WIN32__)
1928 // we must use different methods for the files and directories under
1929 // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and
1930 // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and
1933 FILETIME ftAccess
, ftCreate
, ftWrite
;
1936 // implemented in msw/dir.cpp
1937 extern bool wxGetDirectoryTimes(const wxString
& dirname
,
1938 FILETIME
*, FILETIME
*, FILETIME
*);
1940 // we should pass the path without the trailing separator to
1941 // wxGetDirectoryTimes()
1942 ok
= wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME
),
1943 &ftAccess
, &ftCreate
, &ftWrite
);
1947 wxFileHandle
fh(GetFullPath(), wxFileHandle::Read
);
1950 ok
= ::GetFileTime(fh
,
1951 dtCreate
? &ftCreate
: NULL
,
1952 dtAccess
? &ftAccess
: NULL
,
1953 dtMod
? &ftWrite
: NULL
) != 0;
1964 ConvertFileTimeToWx(dtCreate
, ftCreate
);
1966 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1968 ConvertFileTimeToWx(dtMod
, ftWrite
);
1972 #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
1974 if ( wxStat( GetFullPath().c_str(), &stBuf
) == 0 )
1977 dtAccess
->Set(stBuf
.st_atime
);
1979 dtMod
->Set(stBuf
.st_mtime
);
1981 dtCreate
->Set(stBuf
.st_ctime
);
1985 #else // other platform
1988 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1989 GetFullPath().c_str());
1994 #endif // wxUSE_DATETIME
1998 const short kMacExtensionMaxLength
= 16 ;
1999 class MacDefaultExtensionRecord
2002 MacDefaultExtensionRecord()
2005 m_type
= m_creator
= 0 ;
2007 MacDefaultExtensionRecord( const MacDefaultExtensionRecord
& from
)
2009 wxStrcpy( m_ext
, from
.m_ext
) ;
2010 m_type
= from
.m_type
;
2011 m_creator
= from
.m_creator
;
2013 MacDefaultExtensionRecord( const wxChar
* extension
, OSType type
, OSType creator
)
2015 wxStrncpy( m_ext
, extension
, kMacExtensionMaxLength
) ;
2016 m_ext
[kMacExtensionMaxLength
] = 0 ;
2018 m_creator
= creator
;
2020 wxChar m_ext
[kMacExtensionMaxLength
] ;
2025 #include "wx/dynarray.h"
2026 WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord
, MacDefaultExtensionArray
) ;
2028 bool gMacDefaultExtensionsInited
= false ;
2030 #include "wx/arrimpl.cpp"
2032 WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray
) ;
2034 MacDefaultExtensionArray gMacDefaultExtensions
;
2036 // load the default extensions
2037 MacDefaultExtensionRecord gDefaults
[] =
2039 MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) ,
2040 MacDefaultExtensionRecord( wxT("tif") , 'TIFF' , '****' ) ,
2041 MacDefaultExtensionRecord( wxT("jpg") , 'JPEG' , '****' ) ,
2044 static void MacEnsureDefaultExtensionsLoaded()
2046 if ( !gMacDefaultExtensionsInited
)
2048 // we could load the pc exchange prefs here too
2049 for ( size_t i
= 0 ; i
< WXSIZEOF( gDefaults
) ; ++i
)
2051 gMacDefaultExtensions
.Add( gDefaults
[i
] ) ;
2053 gMacDefaultExtensionsInited
= true ;
2057 bool wxFileName::MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
)
2060 FSCatalogInfo catInfo
;
2063 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2065 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2067 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2068 finfo
->fileType
= type
;
2069 finfo
->fileCreator
= creator
;
2070 FSSetCatalogInfo( &fsRef
, kFSCatInfoFinderInfo
, &catInfo
) ;
2077 bool wxFileName::MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
)
2080 FSCatalogInfo catInfo
;
2083 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2085 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2087 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2088 *type
= finfo
->fileType
;
2089 *creator
= finfo
->fileCreator
;
2096 bool wxFileName::MacSetDefaultTypeAndCreator()
2098 wxUint32 type
, creator
;
2099 if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type
,
2102 return MacSetTypeAndCreator( type
, creator
) ;
2107 bool wxFileName::MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
)
2109 MacEnsureDefaultExtensionsLoaded() ;
2110 wxString extl
= ext
.Lower() ;
2111 for( int i
= gMacDefaultExtensions
.Count() - 1 ; i
>= 0 ; --i
)
2113 if ( gMacDefaultExtensions
.Item(i
).m_ext
== extl
)
2115 *type
= gMacDefaultExtensions
.Item(i
).m_type
;
2116 *creator
= gMacDefaultExtensions
.Item(i
).m_creator
;
2123 void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
)
2125 MacEnsureDefaultExtensionsLoaded() ;
2126 MacDefaultExtensionRecord rec
;
2128 rec
.m_creator
= creator
;
2129 wxStrncpy( rec
.m_ext
, ext
.Lower().c_str() , kMacExtensionMaxLength
) ;
2130 gMacDefaultExtensions
.Add( rec
) ;