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::AssignDir(const wxString
& dir
, wxPathFormat format
)
458 Assign(dir
, _T(""), format
);
461 void wxFileName::Clear()
467 m_ext
= wxEmptyString
;
469 // we don't have any absolute path for now
474 wxFileName
wxFileName::FileName(const wxString
& file
, wxPathFormat format
)
476 return wxFileName(file
, format
);
480 wxFileName
wxFileName::DirName(const wxString
& dir
, wxPathFormat format
)
483 fn
.AssignDir(dir
, format
);
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 bool wxFileName::FileExists() const
493 return wxFileName::FileExists( GetFullPath() );
496 bool wxFileName::FileExists( const wxString
&file
)
498 return ::wxFileExists( file
);
501 bool wxFileName::DirExists() const
503 return wxFileName::DirExists( GetFullPath() );
506 bool wxFileName::DirExists( const wxString
&dir
)
508 return ::wxDirExists( dir
);
511 // ----------------------------------------------------------------------------
512 // CWD and HOME stuff
513 // ----------------------------------------------------------------------------
515 void wxFileName::AssignCwd(const wxString
& volume
)
517 AssignDir(wxFileName::GetCwd(volume
));
521 wxString
wxFileName::GetCwd(const wxString
& volume
)
523 // if we have the volume, we must get the current directory on this drive
524 // and to do this we have to chdir to this volume - at least under Windows,
525 // I don't know how to get the current drive on another volume elsewhere
528 if ( !volume
.empty() )
531 SetCwd(volume
+ GetVolumeSeparator());
534 wxString cwd
= ::wxGetCwd();
536 if ( !volume
.empty() )
544 bool wxFileName::SetCwd()
546 return wxFileName::SetCwd( GetFullPath() );
549 bool wxFileName::SetCwd( const wxString
&cwd
)
551 return ::wxSetWorkingDirectory( cwd
);
554 void wxFileName::AssignHomeDir()
556 AssignDir(wxFileName::GetHomeDir());
559 wxString
wxFileName::GetHomeDir()
561 return ::wxGetHomeDir();
564 void wxFileName::AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
566 wxString tempname
= CreateTempFileName(prefix
, fileTemp
);
567 if ( tempname
.empty() )
569 // error, failed to get temp file name
580 wxFileName::CreateTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
582 wxString path
, dir
, name
;
584 // use the directory specified by the prefix
585 SplitPath(prefix
, &dir
, &name
, NULL
/* extension */);
587 #if defined(__WXWINCE__)
590 // FIXME. Create \temp dir?
593 path
= dir
+ wxT("\\") + prefix
;
595 while (wxFileExists(path
))
597 path
= dir
+ wxT("\\") + prefix
;
602 #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
606 if ( !::GetTempPath(MAX_PATH
, wxStringBuffer(dir
, MAX_PATH
+ 1)) )
608 wxLogLastError(_T("GetTempPath"));
613 // GetTempFileName() fails if we pass it an empty string
617 else // we have a dir to create the file in
619 // ensure we use only the back slashes as GetTempFileName(), unlike all
620 // the other APIs, is picky and doesn't accept the forward ones
621 dir
.Replace(_T("/"), _T("\\"));
624 if ( !::GetTempFileName(dir
, name
, 0, wxStringBuffer(path
, MAX_PATH
+ 1)) )
626 wxLogLastError(_T("GetTempFileName"));
634 #if defined(__WXMAC__) && !defined(__DARWIN__)
635 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
637 dir
= wxGetenv(_T("TMP"));
640 dir
= wxGetenv(_T("TEMP"));
646 #if defined(__DOS__) || defined(__OS2__)
657 if ( !wxEndsWithPathSeparator(dir
) &&
658 (name
.empty() || !wxIsPathSeparator(name
[0u])) )
660 path
+= wxFILE_SEP_PATH
;
665 #if defined(HAVE_MKSTEMP)
666 // scratch space for mkstemp()
667 path
+= _T("XXXXXX");
669 // we need to copy the path to the buffer in which mkstemp() can modify it
670 wxCharBuffer
buf( wxConvFile
.cWX2MB( path
) );
672 // cast is safe because the string length doesn't change
673 int fdTemp
= mkstemp( (char*)(const char*) buf
);
676 // this might be not necessary as mkstemp() on most systems should have
677 // already done it but it doesn't hurt neither...
680 else // mkstemp() succeeded
682 path
= wxConvFile
.cMB2WX( (const char*) buf
);
684 // avoid leaking the fd
687 fileTemp
->Attach(fdTemp
);
694 #else // !HAVE_MKSTEMP
698 path
+= _T("XXXXXX");
700 wxCharBuffer buf
= wxConvFile
.cWX2MB( path
);
701 if ( !mktemp( (const char*) buf
) )
707 path
= wxConvFile
.cMB2WX( (const char*) buf
);
709 #else // !HAVE_MKTEMP (includes __DOS__)
710 // generate the unique file name ourselves
711 #if !defined(__DOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
712 path
<< (unsigned int)getpid();
717 static const size_t numTries
= 1000;
718 for ( size_t n
= 0; n
< numTries
; n
++ )
720 // 3 hex digits is enough for numTries == 1000 < 4096
721 pathTry
= path
+ wxString::Format(_T("%.03x"), (unsigned int) n
);
722 if ( !wxFile::Exists(pathTry
) )
731 #endif // HAVE_MKTEMP/!HAVE_MKTEMP
736 #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
738 #endif // Windows/!Windows
742 wxLogSysError(_("Failed to create a temporary file name"));
744 else if ( fileTemp
&& !fileTemp
->IsOpened() )
746 // open the file - of course, there is a race condition here, this is
747 // why we always prefer using mkstemp()...
749 // NB: GetTempFileName() under Windows creates the file, so using
750 // write_excl there would fail
751 if ( !fileTemp
->Open(path
,
752 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
757 wxS_IRUSR
| wxS_IWUSR
) )
759 // FIXME: If !ok here should we loop and try again with another
760 // file name? That is the standard recourse if open(O_EXCL)
761 // fails, though of course it should be protected against
762 // possible infinite looping too.
764 wxLogError(_("Failed to open temporary file."));
773 // ----------------------------------------------------------------------------
774 // directory operations
775 // ----------------------------------------------------------------------------
777 bool wxFileName::Mkdir( int perm
, int flags
)
779 return wxFileName::Mkdir( GetFullPath(), perm
, flags
);
782 bool wxFileName::Mkdir( const wxString
& dir
, int perm
, int flags
)
784 if ( flags
& wxPATH_MKDIR_FULL
)
786 // split the path in components
788 filename
.AssignDir(dir
);
791 if ( filename
.HasVolume())
793 currPath
<< wxGetVolumeString(filename
.GetVolume(), wxPATH_NATIVE
);
796 wxArrayString dirs
= filename
.GetDirs();
797 size_t count
= dirs
.GetCount();
798 for ( size_t i
= 0; i
< count
; i
++ )
801 #if defined(__WXMAC__) && !defined(__DARWIN__)
802 // relative pathnames are exactely the other way round under mac...
803 !filename
.IsAbsolute()
805 filename
.IsAbsolute()
808 currPath
+= wxFILE_SEP_PATH
;
811 if (!DirExists(currPath
))
813 if (!wxMkdir(currPath
, perm
))
815 // no need to try creating further directories
825 return ::wxMkdir( dir
, perm
);
828 bool wxFileName::Rmdir()
830 return wxFileName::Rmdir( GetFullPath() );
833 bool wxFileName::Rmdir( const wxString
&dir
)
835 return ::wxRmdir( dir
);
838 // ----------------------------------------------------------------------------
839 // path normalization
840 // ----------------------------------------------------------------------------
842 bool wxFileName::Normalize(int flags
,
846 // deal with env vars renaming first as this may seriously change the path
847 if ( flags
& wxPATH_NORM_ENV_VARS
)
849 wxString pathOrig
= GetFullPath(format
);
850 wxString path
= wxExpandEnvVars(pathOrig
);
851 if ( path
!= pathOrig
)
858 // the existing path components
859 wxArrayString dirs
= GetDirs();
861 // the path to prepend in front to make the path absolute
864 format
= GetFormat(format
);
866 // make the path absolute
867 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute(format
) )
871 curDir
.AssignCwd(GetVolume());
875 curDir
.AssignDir(cwd
);
878 // the path may be not absolute because it doesn't have the volume name
879 // but in this case we shouldn't modify the directory components of it
880 // but just set the current volume
881 if ( !HasVolume() && curDir
.HasVolume() )
883 SetVolume(curDir
.GetVolume());
887 // yes, it was the case - we don't need curDir then
893 // handle ~ stuff under Unix only
894 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
896 if ( !dirs
.IsEmpty() )
898 wxString dir
= dirs
[0u];
899 if ( !dir
.empty() && dir
[0u] == _T('~') )
901 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
908 // transform relative path into abs one
911 wxArrayString dirsNew
= curDir
.GetDirs();
912 size_t count
= dirs
.GetCount();
913 for ( size_t n
= 0; n
< count
; n
++ )
915 dirsNew
.Add(dirs
[n
]);
921 // now deal with ".", ".." and the rest
923 size_t count
= dirs
.GetCount();
924 for ( size_t n
= 0; n
< count
; n
++ )
926 wxString dir
= dirs
[n
];
928 if ( flags
& wxPATH_NORM_DOTS
)
930 if ( dir
== wxT(".") )
936 if ( dir
== wxT("..") )
938 if ( m_dirs
.IsEmpty() )
940 wxLogError(_("The path '%s' contains too many \"..\"!"),
941 GetFullPath().c_str());
945 m_dirs
.RemoveAt(m_dirs
.GetCount() - 1);
950 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
958 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
959 if ( (flags
& wxPATH_NORM_SHORTCUT
) )
962 if (GetShortcutTarget(GetFullPath(format
), filename
))
964 // Repeat this since we may now have a new path
965 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
967 filename
.MakeLower();
975 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
977 // VZ: expand env vars here too?
979 m_volume
.MakeLower();
984 // we do have the path now
986 // NB: need to do this before (maybe) calling Assign() below
989 #if defined(__WIN32__)
990 if ( (flags
& wxPATH_NORM_LONG
) && (format
== wxPATH_DOS
) )
992 Assign(GetLongPath());
999 // ----------------------------------------------------------------------------
1000 // get the shortcut target
1001 // ----------------------------------------------------------------------------
1003 // WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions.
1004 // The .lnk file is a plain text file so it should be easy to
1005 // make it work. Hint from Google Groups:
1006 // "If you open up a lnk file, you'll see a
1007 // number, followed by a pound sign (#), followed by more text. The
1008 // number is the number of characters that follows the pound sign. The
1009 // characters after the pound sign are the command line (which _can_
1010 // include arguments) to be executed. Any path (e.g. \windows\program
1011 // files\myapp.exe) that includes spaces needs to be enclosed in
1012 // quotation marks."
1014 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
1015 // The following lines are necessary under WinCE
1016 // #include "wx/msw/private.h"
1017 // #include <ole2.h>
1019 #if defined(__WXWINCE__)
1020 #include <shlguid.h>
1023 bool wxFileName::GetShortcutTarget(const wxString
& shortcutPath
, wxString
& targetFilename
, wxString
* arguments
)
1025 wxString path
, file
, ext
;
1026 wxSplitPath(shortcutPath
, & path
, & file
, & ext
);
1030 bool success
= false;
1032 // Assume it's not a shortcut if it doesn't end with lnk
1033 if (ext
.Lower() != wxT("lnk"))
1036 // create a ShellLink object
1037 hres
= CoCreateInstance(CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
,
1038 IID_IShellLink
, (LPVOID
*) &psl
);
1040 if (SUCCEEDED(hres
))
1043 hres
= psl
->QueryInterface( IID_IPersistFile
, (LPVOID
*) &ppf
);
1044 if (SUCCEEDED(hres
))
1046 WCHAR wsz
[MAX_PATH
];
1048 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, shortcutPath
.mb_str(), -1, wsz
,
1051 hres
= ppf
->Load(wsz
, 0);
1052 if (SUCCEEDED(hres
))
1055 // Wrong prototype in early versions
1056 #if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2)
1057 psl
->GetPath((CHAR
*) buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1059 psl
->GetPath(buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1061 targetFilename
= wxString(buf
);
1062 success
= (shortcutPath
!= targetFilename
);
1064 psl
->GetArguments(buf
, 2048);
1066 if (!args
.IsEmpty() && arguments
)
1079 // ----------------------------------------------------------------------------
1080 // absolute/relative paths
1081 // ----------------------------------------------------------------------------
1083 bool wxFileName::IsAbsolute(wxPathFormat format
) const
1085 // if our path doesn't start with a path separator, it's not an absolute
1090 if ( !GetVolumeSeparator(format
).empty() )
1092 // this format has volumes and an absolute path must have one, it's not
1093 // enough to have the full path to bean absolute file under Windows
1094 if ( GetVolume().empty() )
1101 bool wxFileName::MakeRelativeTo(const wxString
& pathBase
, wxPathFormat format
)
1103 wxFileName fnBase
= wxFileName::DirName(pathBase
, format
);
1105 // get cwd only once - small time saving
1106 wxString cwd
= wxGetCwd();
1107 Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1108 fnBase
.Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1110 bool withCase
= IsCaseSensitive(format
);
1112 // we can't do anything if the files live on different volumes
1113 if ( !GetVolume().IsSameAs(fnBase
.GetVolume(), withCase
) )
1119 // same drive, so we don't need our volume
1122 // remove common directories starting at the top
1123 while ( !m_dirs
.IsEmpty() && !fnBase
.m_dirs
.IsEmpty() &&
1124 m_dirs
[0u].IsSameAs(fnBase
.m_dirs
[0u], withCase
) )
1127 fnBase
.m_dirs
.RemoveAt(0);
1130 // add as many ".." as needed
1131 size_t count
= fnBase
.m_dirs
.GetCount();
1132 for ( size_t i
= 0; i
< count
; i
++ )
1134 m_dirs
.Insert(wxT(".."), 0u);
1137 if ( format
== wxPATH_UNIX
|| format
== wxPATH_DOS
)
1139 // a directory made relative with respect to itself is '.' under Unix
1140 // and DOS, by definition (but we don't have to insert "./" for the
1142 if ( m_dirs
.IsEmpty() && IsDir() )
1144 m_dirs
.Add(_T('.'));
1154 // ----------------------------------------------------------------------------
1155 // filename kind tests
1156 // ----------------------------------------------------------------------------
1158 bool wxFileName::SameAs(const wxFileName
& filepath
, wxPathFormat format
) const
1160 wxFileName fn1
= *this,
1163 // get cwd only once - small time saving
1164 wxString cwd
= wxGetCwd();
1165 fn1
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1166 fn2
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1168 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
1171 // TODO: compare inodes for Unix, this works even when filenames are
1172 // different but files are the same (symlinks) (VZ)
1178 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
1180 // only Unix filenames are truely case-sensitive
1181 return GetFormat(format
) == wxPATH_UNIX
;
1185 wxString
wxFileName::GetForbiddenChars(wxPathFormat format
)
1187 // Inits to forbidden characters that are common to (almost) all platforms.
1188 wxString strForbiddenChars
= wxT("*?");
1190 // If asserts, wxPathFormat has been changed. In case of a new path format
1191 // addition, the following code might have to be updated.
1192 wxCOMPILE_TIME_ASSERT(wxPATH_MAX
== 5, wxPathFormatChanged
);
1193 switch ( GetFormat(format
) )
1196 wxFAIL_MSG( wxT("Unknown path format") );
1197 // !! Fall through !!
1203 // On a Mac even names with * and ? are allowed (Tested with OS
1204 // 9.2.1 and OS X 10.2.5)
1205 strForbiddenChars
= wxEmptyString
;
1209 strForbiddenChars
+= wxT("\\/:\"<>|");
1216 return strForbiddenChars
;
1220 wxString
wxFileName::GetVolumeSeparator(wxPathFormat format
)
1224 if ( (GetFormat(format
) == wxPATH_DOS
) ||
1225 (GetFormat(format
) == wxPATH_VMS
) )
1227 sepVol
= wxFILE_SEP_DSK
;
1235 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
1238 switch ( GetFormat(format
) )
1241 // accept both as native APIs do but put the native one first as
1242 // this is the one we use in GetFullPath()
1243 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
1247 wxFAIL_MSG( _T("Unknown wxPATH_XXX style") );
1251 seps
= wxFILE_SEP_PATH_UNIX
;
1255 seps
= wxFILE_SEP_PATH_MAC
;
1259 seps
= wxFILE_SEP_PATH_VMS
;
1267 wxString
wxFileName::GetPathTerminators(wxPathFormat format
)
1269 format
= GetFormat(format
);
1271 // under VMS the end of the path is ']', not the path separator used to
1272 // separate the components
1273 return format
== wxPATH_VMS
? wxString(_T(']')) : GetPathSeparators(format
);
1277 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
1279 // wxString::Find() doesn't work as expected with NUL - it will always find
1280 // it, so it is almost surely a bug if this function is called with NUL arg
1281 wxASSERT_MSG( ch
!= _T('\0'), _T("shouldn't be called with NUL") );
1283 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
1286 // ----------------------------------------------------------------------------
1287 // path components manipulation
1288 // ----------------------------------------------------------------------------
1290 /* static */ bool wxFileName::IsValidDirComponent(const wxString
& dir
)
1294 wxFAIL_MSG( _T("empty directory passed to wxFileName::InsertDir()") );
1299 const size_t len
= dir
.length();
1300 for ( size_t n
= 0; n
< len
; n
++ )
1302 if ( dir
[n
] == GetVolumeSeparator() || IsPathSeparator(dir
[n
]) )
1304 wxFAIL_MSG( _T("invalid directory component in wxFileName") );
1313 void wxFileName::AppendDir( const wxString
&dir
)
1315 if ( IsValidDirComponent(dir
) )
1319 void wxFileName::PrependDir( const wxString
&dir
)
1324 void wxFileName::InsertDir( int before
, const wxString
&dir
)
1326 if ( IsValidDirComponent(dir
) )
1327 m_dirs
.Insert( dir
, before
);
1330 void wxFileName::RemoveDir( int pos
)
1332 m_dirs
.RemoveAt( (size_t)pos
);
1335 // ----------------------------------------------------------------------------
1337 // ----------------------------------------------------------------------------
1339 void wxFileName::SetFullName(const wxString
& fullname
)
1341 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
1344 wxString
wxFileName::GetFullName() const
1346 wxString fullname
= m_name
;
1347 if ( !m_ext
.empty() )
1349 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
1355 wxString
wxFileName::GetPath( int flags
, wxPathFormat format
) const
1357 format
= GetFormat( format
);
1361 // return the volume with the path as well if requested
1362 if ( flags
& wxPATH_GET_VOLUME
)
1364 fullpath
+= wxGetVolumeString(GetVolume(), format
);
1367 // the leading character
1372 fullpath
+= wxFILE_SEP_PATH_MAC
;
1377 fullpath
+= wxFILE_SEP_PATH_DOS
;
1381 wxFAIL_MSG( wxT("Unknown path format") );
1387 // normally the absolute file names start with a slash
1388 // with one exception: the ones like "~/foo.bar" don't
1390 if ( m_dirs
.IsEmpty() || m_dirs
[0u] != _T('~') )
1392 fullpath
+= wxFILE_SEP_PATH_UNIX
;
1398 // no leading character here but use this place to unset
1399 // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense
1400 // as, if I understand correctly, there should never be a dot
1401 // before the closing bracket
1402 flags
&= ~wxPATH_GET_SEPARATOR
;
1405 if ( m_dirs
.empty() )
1407 // there is nothing more
1411 // then concatenate all the path components using the path separator
1412 if ( format
== wxPATH_VMS
)
1414 fullpath
+= wxT('[');
1417 const size_t dirCount
= m_dirs
.GetCount();
1418 for ( size_t i
= 0; i
< dirCount
; i
++ )
1423 if ( m_dirs
[i
] == wxT(".") )
1425 // skip appending ':', this shouldn't be done in this
1426 // case as "::" is interpreted as ".." under Unix
1430 // convert back from ".." to nothing
1431 if ( m_dirs
[i
] != wxT("..") )
1432 fullpath
+= m_dirs
[i
];
1436 wxFAIL_MSG( wxT("Unexpected path format") );
1437 // still fall through
1441 fullpath
+= m_dirs
[i
];
1445 // TODO: What to do with ".." under VMS
1447 // convert back from ".." to nothing
1448 if ( m_dirs
[i
] != wxT("..") )
1449 fullpath
+= m_dirs
[i
];
1453 if ( (flags
& wxPATH_GET_SEPARATOR
) || (i
!= dirCount
- 1) )
1454 fullpath
+= GetPathSeparator(format
);
1457 if ( format
== wxPATH_VMS
)
1459 fullpath
+= wxT(']');
1465 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
1467 // we already have a function to get the path
1468 wxString fullpath
= GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
,
1471 // now just add the file name and extension to it
1472 fullpath
+= GetFullName();
1477 // Return the short form of the path (returns identity on non-Windows platforms)
1478 wxString
wxFileName::GetShortPath() const
1480 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
1481 wxString
path(GetFullPath());
1483 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
1487 ok
= ::GetShortPathName
1490 wxStringBuffer(pathOut
, sz
),
1499 return GetFullPath();
1503 // Return the long form of the path (returns identity on non-Windows platforms)
1504 wxString
wxFileName::GetLongPath() const
1507 path
= GetFullPath();
1509 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1510 bool success
= false;
1512 #if wxUSE_DYNAMIC_LOADER
1513 typedef DWORD (WINAPI
*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
1515 static bool s_triedToLoad
= false;
1517 if ( !s_triedToLoad
)
1519 // suppress the errors about missing GetLongPathName[AW]
1522 s_triedToLoad
= true;
1523 wxDynamicLibrary
dllKernel(_T("kernel32"));
1524 if ( dllKernel
.IsLoaded() )
1526 // may succeed or fail depending on the Windows version
1527 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
1529 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) dllKernel
.GetSymbol(_T("GetLongPathNameW"));
1531 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) dllKernel
.GetSymbol(_T("GetLongPathNameA"));
1534 if ( s_pfnGetLongPathName
)
1536 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1537 bool ok
= dwSize
> 0;
1541 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1545 ok
= (*s_pfnGetLongPathName
)
1548 wxStringBuffer(pathOut
, sz
),
1560 #endif // wxUSE_DYNAMIC_LOADER
1564 // The OS didn't support GetLongPathName, or some other error.
1565 // We need to call FindFirstFile on each component in turn.
1567 WIN32_FIND_DATA findFileData
;
1571 pathOut
= GetVolume() +
1572 GetVolumeSeparator(wxPATH_DOS
) +
1573 GetPathSeparator(wxPATH_DOS
);
1575 pathOut
= wxEmptyString
;
1577 wxArrayString dirs
= GetDirs();
1578 dirs
.Add(GetFullName());
1582 size_t count
= dirs
.GetCount();
1583 for ( size_t i
= 0; i
< count
; i
++ )
1585 // We're using pathOut to collect the long-name path, but using a
1586 // temporary for appending the last path component which may be
1588 tmpPath
= pathOut
+ dirs
[i
];
1590 if ( tmpPath
.empty() )
1593 // can't see this being necessary? MF
1594 if ( tmpPath
.Last() == GetVolumeSeparator(wxPATH_DOS
) )
1596 // Can't pass a drive and root dir to FindFirstFile,
1597 // so continue to next dir
1598 tmpPath
+= wxFILE_SEP_PATH
;
1603 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
1604 if (hFind
== INVALID_HANDLE_VALUE
)
1606 // Error: most likely reason is that path doesn't exist, so
1607 // append any unprocessed parts and return
1608 for ( i
+= 1; i
< count
; i
++ )
1609 tmpPath
+= wxFILE_SEP_PATH
+ dirs
[i
];
1614 pathOut
+= findFileData
.cFileName
;
1615 if ( (i
< (count
-1)) )
1616 pathOut
+= wxFILE_SEP_PATH
;
1623 #endif // Win32/!Win32
1628 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
1630 if (format
== wxPATH_NATIVE
)
1632 #if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__)
1633 format
= wxPATH_DOS
;
1634 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1635 format
= wxPATH_MAC
;
1636 #elif defined(__VMS)
1637 format
= wxPATH_VMS
;
1639 format
= wxPATH_UNIX
;
1645 // ----------------------------------------------------------------------------
1646 // path splitting function
1647 // ----------------------------------------------------------------------------
1651 wxFileName::SplitVolume(const wxString
& fullpathWithVolume
,
1652 wxString
*pstrVolume
,
1654 wxPathFormat format
)
1656 format
= GetFormat(format
);
1658 wxString fullpath
= fullpathWithVolume
;
1660 // special Windows UNC paths hack: transform \\share\path into share:path
1661 if ( format
== wxPATH_DOS
)
1663 if ( fullpath
.length() >= 4 &&
1664 fullpath
[0u] == wxFILE_SEP_PATH_DOS
&&
1665 fullpath
[1u] == wxFILE_SEP_PATH_DOS
)
1667 fullpath
.erase(0, 2);
1669 size_t posFirstSlash
=
1670 fullpath
.find_first_of(GetPathTerminators(format
));
1671 if ( posFirstSlash
!= wxString::npos
)
1673 fullpath
[posFirstSlash
] = wxFILE_SEP_DSK
;
1675 // UNC paths are always absolute, right? (FIXME)
1676 fullpath
.insert(posFirstSlash
+ 1, 1, wxFILE_SEP_PATH_DOS
);
1681 // We separate the volume here
1682 if ( format
== wxPATH_DOS
|| format
== wxPATH_VMS
)
1684 wxString sepVol
= GetVolumeSeparator(format
);
1686 size_t posFirstColon
= fullpath
.find_first_of(sepVol
);
1687 if ( posFirstColon
!= wxString::npos
)
1691 *pstrVolume
= fullpath
.Left(posFirstColon
);
1694 // remove the volume name and the separator from the full path
1695 fullpath
.erase(0, posFirstColon
+ sepVol
.length());
1700 *pstrPath
= fullpath
;
1704 void wxFileName::SplitPath(const wxString
& fullpathWithVolume
,
1705 wxString
*pstrVolume
,
1709 wxPathFormat format
)
1711 format
= GetFormat(format
);
1714 SplitVolume(fullpathWithVolume
, pstrVolume
, &fullpath
, format
);
1716 // find the positions of the last dot and last path separator in the path
1717 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
1718 size_t posLastSlash
= fullpath
.find_last_of(GetPathTerminators(format
));
1720 // check whether this dot occurs at the very beginning of a path component
1721 if ( (posLastDot
!= wxString::npos
) &&
1723 IsPathSeparator(fullpath
[posLastDot
- 1]) ||
1724 (format
== wxPATH_VMS
&& fullpath
[posLastDot
- 1] == _T(']'))) )
1726 // dot may be (and commonly -- at least under Unix -- is) the first
1727 // character of the filename, don't treat the entire filename as
1728 // extension in this case
1729 posLastDot
= wxString::npos
;
1732 // if we do have a dot and a slash, check that the dot is in the name part
1733 if ( (posLastDot
!= wxString::npos
) &&
1734 (posLastSlash
!= wxString::npos
) &&
1735 (posLastDot
< posLastSlash
) )
1737 // the dot is part of the path, not the start of the extension
1738 posLastDot
= wxString::npos
;
1741 // now fill in the variables provided by user
1744 if ( posLastSlash
== wxString::npos
)
1751 // take everything up to the path separator but take care to make
1752 // the path equal to something like '/', not empty, for the files
1753 // immediately under root directory
1754 size_t len
= posLastSlash
;
1756 // this rule does not apply to mac since we do not start with colons (sep)
1757 // except for relative paths
1758 if ( !len
&& format
!= wxPATH_MAC
)
1761 *pstrPath
= fullpath
.Left(len
);
1763 // special VMS hack: remove the initial bracket
1764 if ( format
== wxPATH_VMS
)
1766 if ( (*pstrPath
)[0u] == _T('[') )
1767 pstrPath
->erase(0, 1);
1774 // take all characters starting from the one after the last slash and
1775 // up to, but excluding, the last dot
1776 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
1778 if ( posLastDot
== wxString::npos
)
1780 // take all until the end
1781 count
= wxString::npos
;
1783 else if ( posLastSlash
== wxString::npos
)
1787 else // have both dot and slash
1789 count
= posLastDot
- posLastSlash
- 1;
1792 *pstrName
= fullpath
.Mid(nStart
, count
);
1797 if ( posLastDot
== wxString::npos
)
1804 // take everything after the dot
1805 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
1811 void wxFileName::SplitPath(const wxString
& fullpath
,
1815 wxPathFormat format
)
1818 SplitPath(fullpath
, &volume
, path
, name
, ext
, format
);
1822 path
->Prepend(wxGetVolumeString(volume
, format
));
1826 // ----------------------------------------------------------------------------
1828 // ----------------------------------------------------------------------------
1832 bool wxFileName::SetTimes(const wxDateTime
*dtAccess
,
1833 const wxDateTime
*dtMod
,
1834 const wxDateTime
*dtCreate
)
1836 #if defined(__WIN32__)
1839 // VZ: please let me know how to do this if you can
1840 wxFAIL_MSG( _T("SetTimes() not implemented for the directories") );
1844 wxFileHandle
fh(GetFullPath(), wxFileHandle::Write
);
1847 FILETIME ftAccess
, ftCreate
, ftWrite
;
1850 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
1852 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
1854 ConvertWxToFileTime(&ftWrite
, *dtMod
);
1856 if ( ::SetFileTime(fh
,
1857 dtCreate
? &ftCreate
: NULL
,
1858 dtAccess
? &ftAccess
: NULL
,
1859 dtMod
? &ftWrite
: NULL
) )
1865 #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
1866 if ( !dtAccess
&& !dtMod
)
1868 // can't modify the creation time anyhow, don't try
1872 // if dtAccess or dtMod is not specified, use the other one (which must be
1873 // non NULL because of the test above) for both times
1875 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
1876 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
1877 if ( utime(GetFullPath().fn_str(), &utm
) == 0 )
1881 #else // other platform
1884 wxLogSysError(_("Failed to modify file times for '%s'"),
1885 GetFullPath().c_str());
1890 bool wxFileName::Touch()
1892 #if defined(__UNIX_LIKE__)
1893 // under Unix touching file is simple: just pass NULL to utime()
1894 if ( utime(GetFullPath().fn_str(), NULL
) == 0 )
1899 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1902 #else // other platform
1903 wxDateTime dtNow
= wxDateTime::Now();
1905 return SetTimes(&dtNow
, &dtNow
, NULL
/* don't change create time */);
1909 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
1911 wxDateTime
*dtCreate
) const
1913 #if defined(__WIN32__)
1914 // we must use different methods for the files and directories under
1915 // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and
1916 // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and
1919 FILETIME ftAccess
, ftCreate
, ftWrite
;
1922 // implemented in msw/dir.cpp
1923 extern bool wxGetDirectoryTimes(const wxString
& dirname
,
1924 FILETIME
*, FILETIME
*, FILETIME
*);
1926 // we should pass the path without the trailing separator to
1927 // wxGetDirectoryTimes()
1928 ok
= wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME
),
1929 &ftAccess
, &ftCreate
, &ftWrite
);
1933 wxFileHandle
fh(GetFullPath(), wxFileHandle::Read
);
1936 ok
= ::GetFileTime(fh
,
1937 dtCreate
? &ftCreate
: NULL
,
1938 dtAccess
? &ftAccess
: NULL
,
1939 dtMod
? &ftWrite
: NULL
) != 0;
1950 ConvertFileTimeToWx(dtCreate
, ftCreate
);
1952 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1954 ConvertFileTimeToWx(dtMod
, ftWrite
);
1958 #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
1960 if ( wxStat( GetFullPath().c_str(), &stBuf
) == 0 )
1963 dtAccess
->Set(stBuf
.st_atime
);
1965 dtMod
->Set(stBuf
.st_mtime
);
1967 dtCreate
->Set(stBuf
.st_ctime
);
1971 #else // other platform
1974 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1975 GetFullPath().c_str());
1980 #endif // wxUSE_DATETIME
1984 const short kMacExtensionMaxLength
= 16 ;
1985 class MacDefaultExtensionRecord
1988 MacDefaultExtensionRecord()
1991 m_type
= m_creator
= NULL
;
1993 MacDefaultExtensionRecord( const MacDefaultExtensionRecord
& from
)
1995 wxStrcpy( m_ext
, from
.m_ext
) ;
1996 m_type
= from
.m_type
;
1997 m_creator
= from
.m_creator
;
1999 MacDefaultExtensionRecord( const wxChar
* extension
, OSType type
, OSType creator
)
2001 wxStrncpy( m_ext
, extension
, kMacExtensionMaxLength
) ;
2002 m_ext
[kMacExtensionMaxLength
] = 0 ;
2004 m_creator
= creator
;
2006 wxChar m_ext
[kMacExtensionMaxLength
] ;
2011 #include "wx/dynarray.h"
2012 WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord
, MacDefaultExtensionArray
) ;
2014 bool gMacDefaultExtensionsInited
= false ;
2016 #include "wx/arrimpl.cpp"
2018 WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray
) ;
2020 MacDefaultExtensionArray gMacDefaultExtensions
;
2022 // load the default extensions
2023 MacDefaultExtensionRecord gDefaults
[] =
2025 MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) ,
2026 MacDefaultExtensionRecord( wxT("tif") , 'TIFF' , '****' ) ,
2027 MacDefaultExtensionRecord( wxT("jpg") , 'JPEG' , '****' ) ,
2030 static void MacEnsureDefaultExtensionsLoaded()
2032 if ( !gMacDefaultExtensionsInited
)
2034 // we could load the pc exchange prefs here too
2035 for ( size_t i
= 0 ; i
< WXSIZEOF( gDefaults
) ; ++i
)
2037 gMacDefaultExtensions
.Add( gDefaults
[i
] ) ;
2039 gMacDefaultExtensionsInited
= true ;
2043 bool wxFileName::MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
)
2046 FSCatalogInfo catInfo
;
2049 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2051 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2053 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2054 finfo
->fileType
= type
;
2055 finfo
->fileCreator
= creator
;
2056 FSSetCatalogInfo( &fsRef
, kFSCatInfoFinderInfo
, &catInfo
) ;
2063 bool wxFileName::MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
)
2066 FSCatalogInfo catInfo
;
2069 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2071 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2073 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2074 *type
= finfo
->fileType
;
2075 *creator
= finfo
->fileCreator
;
2082 bool wxFileName::MacSetDefaultTypeAndCreator()
2084 wxUint32 type
, creator
;
2085 if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type
,
2088 return MacSetTypeAndCreator( type
, creator
) ;
2093 bool wxFileName::MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
)
2095 MacEnsureDefaultExtensionsLoaded() ;
2096 wxString extl
= ext
.Lower() ;
2097 for( int i
= gMacDefaultExtensions
.Count() - 1 ; i
>= 0 ; --i
)
2099 if ( gMacDefaultExtensions
.Item(i
).m_ext
== extl
)
2101 *type
= gMacDefaultExtensions
.Item(i
).m_type
;
2102 *creator
= gMacDefaultExtensions
.Item(i
).m_creator
;
2109 void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
)
2111 MacEnsureDefaultExtensionsLoaded() ;
2112 MacDefaultExtensionRecord rec
;
2114 rec
.m_creator
= creator
;
2115 wxStrncpy( rec
.m_ext
, ext
.Lower().c_str() , kMacExtensionMaxLength
) ;
2116 gMacDefaultExtensions
.Add( rec
) ;