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 // For compilers that support precompilation, includes "wx.h".
64 #include "wx/wxprec.h"
71 #include "wx/dynarray.h"
78 #include "wx/filename.h"
79 #include "wx/tokenzr.h"
80 #include "wx/config.h" // for wxExpandEnvVars
82 #include "wx/dynlib.h"
84 // For GetShort/LongPathName
86 #include "wx/msw/wrapwin.h"
87 #if defined(__MINGW32__)
88 #include "wx/msw/gccpriv.h"
93 #include "wx/msw/private.h"
96 #if defined(__WXMAC__)
97 #include "wx/mac/private.h" // includes mac headers
100 // utime() is POSIX so should normally be available on all Unices
102 #include <sys/types.h>
104 #include <sys/stat.h>
114 #include <sys/types.h>
116 #include <sys/stat.h>
127 #include <sys/utime.h>
128 #include <sys/stat.h>
139 #define MAX_PATH _MAX_PATH
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 // small helper class which opens and closes the file - we use it just to get
147 // a file handle for the given file name to pass it to some Win32 API function
148 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
159 wxFileHandle(const wxString
& filename
, OpenMode mode
)
161 m_hFile
= ::CreateFile
164 mode
== Read
? GENERIC_READ
// access mask
166 FILE_SHARE_READ
| // sharing mode
167 FILE_SHARE_WRITE
, // (allow everything)
168 NULL
, // no secutity attr
169 OPEN_EXISTING
, // creation disposition
171 NULL
// no template file
174 if ( m_hFile
== INVALID_HANDLE_VALUE
)
176 wxLogSysError(_("Failed to open '%s' for %s"),
178 mode
== Read
? _("reading") : _("writing"));
184 if ( m_hFile
!= INVALID_HANDLE_VALUE
)
186 if ( !::CloseHandle(m_hFile
) )
188 wxLogSysError(_("Failed to close file handle"));
193 // return true only if the file could be opened successfully
194 bool IsOk() const { return m_hFile
!= INVALID_HANDLE_VALUE
; }
197 operator HANDLE() const { return m_hFile
; }
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
211 // convert between wxDateTime and FILETIME which is a 64-bit value representing
212 // the number of 100-nanosecond intervals since January 1, 1601.
214 static void ConvertFileTimeToWx(wxDateTime
*dt
, const FILETIME
&ft
)
216 FILETIME ftcopy
= ft
;
218 if ( !::FileTimeToLocalFileTime(&ftcopy
, &ftLocal
) )
220 wxLogLastError(_T("FileTimeToLocalFileTime"));
224 if ( !::FileTimeToSystemTime(&ftLocal
, &st
) )
226 wxLogLastError(_T("FileTimeToSystemTime"));
229 dt
->Set(st
.wDay
, wxDateTime::Month(st
.wMonth
- 1), st
.wYear
,
230 st
.wHour
, st
.wMinute
, st
.wSecond
, st
.wMilliseconds
);
233 static void ConvertWxToFileTime(FILETIME
*ft
, const wxDateTime
& dt
)
236 st
.wDay
= dt
.GetDay();
237 st
.wMonth
= (WORD
)(dt
.GetMonth() + 1);
238 st
.wYear
= (WORD
)dt
.GetYear();
239 st
.wHour
= dt
.GetHour();
240 st
.wMinute
= dt
.GetMinute();
241 st
.wSecond
= dt
.GetSecond();
242 st
.wMilliseconds
= dt
.GetMillisecond();
245 if ( !::SystemTimeToFileTime(&st
, &ftLocal
) )
247 wxLogLastError(_T("SystemTimeToFileTime"));
250 if ( !::LocalFileTimeToFileTime(&ftLocal
, ft
) )
252 wxLogLastError(_T("LocalFileTimeToFileTime"));
256 #endif // wxUSE_DATETIME && __WIN32__
258 // return a string with the volume par
259 static wxString
wxGetVolumeString(const wxString
& volume
, wxPathFormat format
)
263 if ( !volume
.empty() )
265 format
= wxFileName::GetFormat(format
);
267 // Special Windows UNC paths hack, part 2: undo what we did in
268 // SplitPath() and make an UNC path if we have a drive which is not a
269 // single letter (hopefully the network shares can't be one letter only
270 // although I didn't find any authoritative docs on this)
271 if ( format
== wxPATH_DOS
&& volume
.length() > 1 )
273 path
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_DOS
<< volume
;
275 else if ( format
== wxPATH_DOS
|| format
== wxPATH_VMS
)
277 path
<< volume
<< wxFileName::GetVolumeSeparator(format
);
285 // ============================================================================
287 // ============================================================================
289 // ----------------------------------------------------------------------------
290 // wxFileName construction
291 // ----------------------------------------------------------------------------
293 void wxFileName::Assign( const wxFileName
&filepath
)
295 m_volume
= filepath
.GetVolume();
296 m_dirs
= filepath
.GetDirs();
297 m_name
= filepath
.GetName();
298 m_ext
= filepath
.GetExt();
299 m_relative
= filepath
.m_relative
;
300 m_hasExt
= filepath
.m_hasExt
;
303 void wxFileName::Assign(const wxString
& volume
,
304 const wxString
& path
,
305 const wxString
& name
,
308 wxPathFormat format
)
310 SetPath( path
, format
);
319 void wxFileName::SetPath( const wxString
& pathOrig
, wxPathFormat format
)
323 if ( pathOrig
.empty() )
331 format
= GetFormat( format
);
333 // 0) deal with possible volume part first
336 SplitVolume(pathOrig
, &volume
, &path
, format
);
337 if ( !volume
.empty() )
344 // 1) Determine if the path is relative or absolute.
345 wxChar leadingChar
= path
[0u];
350 m_relative
= leadingChar
== wxT(':');
352 // We then remove a leading ":". The reason is in our
353 // storage form for relative paths:
354 // ":dir:file.txt" actually means "./dir/file.txt" in
355 // DOS notation and should get stored as
356 // (relative) (dir) (file.txt)
357 // "::dir:file.txt" actually means "../dir/file.txt"
358 // stored as (relative) (..) (dir) (file.txt)
359 // This is important only for the Mac as an empty dir
360 // actually means <UP>, whereas under DOS, double
361 // slashes can be ignored: "\\\\" is the same as "\\".
367 // TODO: what is the relative path format here?
372 wxFAIL_MSG( _T("Unknown path format") );
373 // !! Fall through !!
376 // the paths of the form "~" or "~username" are absolute
377 m_relative
= leadingChar
!= wxT('/') && leadingChar
!= _T('~');
381 m_relative
= !IsPathSeparator(leadingChar
, format
);
386 // 2) Break up the path into its members. If the original path
387 // was just "/" or "\\", m_dirs will be empty. We know from
388 // the m_relative field, if this means "nothing" or "root dir".
390 wxStringTokenizer
tn( path
, GetPathSeparators(format
) );
392 while ( tn
.HasMoreTokens() )
394 wxString token
= tn
.GetNextToken();
396 // Remove empty token under DOS and Unix, interpret them
400 if (format
== wxPATH_MAC
)
401 m_dirs
.Add( wxT("..") );
411 void wxFileName::Assign(const wxString
& fullpath
,
414 wxString volume
, path
, name
, ext
;
416 SplitPath(fullpath
, &volume
, &path
, &name
, &ext
, &hasExt
, format
);
418 Assign(volume
, path
, name
, ext
, hasExt
, format
);
421 void wxFileName::Assign(const wxString
& fullpathOrig
,
422 const wxString
& fullname
,
425 // always recognize fullpath as directory, even if it doesn't end with a
427 wxString fullpath
= fullpathOrig
;
428 if ( !wxEndsWithPathSeparator(fullpath
) )
430 fullpath
+= GetPathSeparator(format
);
433 wxString volume
, path
, name
, ext
;
436 // do some consistency checks in debug mode: the name should be really just
437 // the filename and the path should be really just a path
439 wxString volDummy
, pathDummy
, nameDummy
, extDummy
;
441 SplitPath(fullname
, &volDummy
, &pathDummy
, &name
, &ext
, &hasExt
, format
);
443 wxASSERT_MSG( volDummy
.empty() && pathDummy
.empty(),
444 _T("the file name shouldn't contain the path") );
446 SplitPath(fullpath
, &volume
, &path
, &nameDummy
, &extDummy
, format
);
448 wxASSERT_MSG( nameDummy
.empty() && extDummy
.empty(),
449 _T("the path shouldn't contain file name nor extension") );
451 #else // !__WXDEBUG__
452 SplitPath(fullname
, NULL
/* no volume */, NULL
/* no path */,
453 &name
, &ext
, &hasExt
, format
);
454 SplitPath(fullpath
, &volume
, &path
, NULL
, NULL
, format
);
455 #endif // __WXDEBUG__/!__WXDEBUG__
457 Assign(volume
, path
, name
, ext
, hasExt
, format
);
460 void wxFileName::Assign(const wxString
& pathOrig
,
461 const wxString
& name
,
467 SplitVolume(pathOrig
, &volume
, &path
, format
);
469 Assign(volume
, path
, name
, ext
, format
);
472 void wxFileName::AssignDir(const wxString
& dir
, wxPathFormat format
)
474 Assign(dir
, wxEmptyString
, format
);
477 void wxFileName::Clear()
483 m_ext
= wxEmptyString
;
485 // we don't have any absolute path for now
493 wxFileName
wxFileName::FileName(const wxString
& file
, wxPathFormat format
)
495 return wxFileName(file
, format
);
499 wxFileName
wxFileName::DirName(const wxString
& dir
, wxPathFormat format
)
502 fn
.AssignDir(dir
, format
);
506 // ----------------------------------------------------------------------------
508 // ----------------------------------------------------------------------------
510 bool wxFileName::FileExists() const
512 return wxFileName::FileExists( GetFullPath() );
515 bool wxFileName::FileExists( const wxString
&file
)
517 return ::wxFileExists( file
);
520 bool wxFileName::DirExists() const
522 return wxFileName::DirExists( GetFullPath() );
525 bool wxFileName::DirExists( const wxString
&dir
)
527 return ::wxDirExists( dir
);
530 // ----------------------------------------------------------------------------
531 // CWD and HOME stuff
532 // ----------------------------------------------------------------------------
534 void wxFileName::AssignCwd(const wxString
& volume
)
536 AssignDir(wxFileName::GetCwd(volume
));
540 wxString
wxFileName::GetCwd(const wxString
& volume
)
542 // if we have the volume, we must get the current directory on this drive
543 // and to do this we have to chdir to this volume - at least under Windows,
544 // I don't know how to get the current drive on another volume elsewhere
547 if ( !volume
.empty() )
550 SetCwd(volume
+ GetVolumeSeparator());
553 wxString cwd
= ::wxGetCwd();
555 if ( !volume
.empty() )
563 bool wxFileName::SetCwd()
565 return wxFileName::SetCwd( GetFullPath() );
568 bool wxFileName::SetCwd( const wxString
&cwd
)
570 return ::wxSetWorkingDirectory( cwd
);
573 void wxFileName::AssignHomeDir()
575 AssignDir(wxFileName::GetHomeDir());
578 wxString
wxFileName::GetHomeDir()
580 return ::wxGetHomeDir();
585 void wxFileName::AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
587 wxString tempname
= CreateTempFileName(prefix
, fileTemp
);
588 if ( tempname
.empty() )
590 // error, failed to get temp file name
601 wxFileName::CreateTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
603 wxString path
, dir
, name
;
605 // use the directory specified by the prefix
606 SplitPath(prefix
, &dir
, &name
, NULL
/* extension */);
610 dir
= wxGetenv(_T("TMPDIR"));
613 dir
= wxGetenv(_T("TMP"));
616 dir
= wxGetenv(_T("TEMP"));
621 #if defined(__WXWINCE__)
624 // FIXME. Create \temp dir?
625 if (DirExists(wxT("\\temp")))
628 path
= dir
+ wxT("\\") + name
;
630 while (FileExists(path
))
632 path
= dir
+ wxT("\\") + name
;
637 #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
641 if ( !::GetTempPath(MAX_PATH
, wxStringBuffer(dir
, MAX_PATH
+ 1)) )
643 wxLogLastError(_T("GetTempPath"));
648 // GetTempFileName() fails if we pass it an empty string
652 else // we have a dir to create the file in
654 // ensure we use only the back slashes as GetTempFileName(), unlike all
655 // the other APIs, is picky and doesn't accept the forward ones
656 dir
.Replace(_T("/"), _T("\\"));
659 if ( !::GetTempFileName(dir
, name
, 0, wxStringBuffer(path
, MAX_PATH
+ 1)) )
661 wxLogLastError(_T("GetTempFileName"));
670 #if defined(__DOS__) || defined(__OS2__)
672 #elif defined(__WXMAC__)
673 dir
= wxMacFindFolder(short(kOnSystemDisk
), kTemporaryFolderType
, kCreateFolder
);
681 if ( !wxEndsWithPathSeparator(dir
) &&
682 (name
.empty() || !wxIsPathSeparator(name
[0u])) )
684 path
+= wxFILE_SEP_PATH
;
689 #if defined(HAVE_MKSTEMP)
690 // scratch space for mkstemp()
691 path
+= _T("XXXXXX");
693 // we need to copy the path to the buffer in which mkstemp() can modify it
694 wxCharBuffer
buf( wxConvFile
.cWX2MB( path
) );
696 // cast is safe because the string length doesn't change
697 int fdTemp
= mkstemp( (char*)(const char*) buf
);
700 // this might be not necessary as mkstemp() on most systems should have
701 // already done it but it doesn't hurt neither...
704 else // mkstemp() succeeded
706 path
= wxConvFile
.cMB2WX( (const char*) buf
);
708 // avoid leaking the fd
711 fileTemp
->Attach(fdTemp
);
718 #else // !HAVE_MKSTEMP
722 path
+= _T("XXXXXX");
724 wxCharBuffer buf
= wxConvFile
.cWX2MB( path
);
725 if ( !mktemp( (const char*) buf
) )
731 path
= wxConvFile
.cMB2WX( (const char*) buf
);
733 #else // !HAVE_MKTEMP (includes __DOS__)
734 // generate the unique file name ourselves
735 #if !defined(__DOS__) && !defined(__PALMOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
736 path
<< (unsigned int)getpid();
741 static const size_t numTries
= 1000;
742 for ( size_t n
= 0; n
< numTries
; n
++ )
744 // 3 hex digits is enough for numTries == 1000 < 4096
745 pathTry
= path
+ wxString::Format(_T("%.03x"), (unsigned int) n
);
746 if ( !FileExists(pathTry
) )
755 #endif // HAVE_MKTEMP/!HAVE_MKTEMP
757 #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
759 #endif // Windows/!Windows
763 wxLogSysError(_("Failed to create a temporary file name"));
765 else if ( fileTemp
&& !fileTemp
->IsOpened() )
767 // open the file - of course, there is a race condition here, this is
768 // why we always prefer using mkstemp()...
770 // NB: GetTempFileName() under Windows creates the file, so using
771 // write_excl there would fail
772 if ( !fileTemp
->Open(path
,
773 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
778 wxS_IRUSR
| wxS_IWUSR
) )
780 // FIXME: If !ok here should we loop and try again with another
781 // file name? That is the standard recourse if open(O_EXCL)
782 // fails, though of course it should be protected against
783 // possible infinite looping too.
785 wxLogError(_("Failed to open temporary file."));
796 // ----------------------------------------------------------------------------
797 // directory operations
798 // ----------------------------------------------------------------------------
800 bool wxFileName::Mkdir( int perm
, int flags
)
802 return wxFileName::Mkdir( GetFullPath(), perm
, flags
);
805 bool wxFileName::Mkdir( const wxString
& dir
, int perm
, int flags
)
807 if ( flags
& wxPATH_MKDIR_FULL
)
809 // split the path in components
811 filename
.AssignDir(dir
);
814 if ( filename
.HasVolume())
816 currPath
<< wxGetVolumeString(filename
.GetVolume(), wxPATH_NATIVE
);
819 wxArrayString dirs
= filename
.GetDirs();
820 size_t count
= dirs
.GetCount();
821 for ( size_t i
= 0; i
< count
; i
++ )
824 #if defined(__WXMAC__) && !defined(__DARWIN__)
825 // relative pathnames are exactely the other way round under mac...
826 !filename
.IsAbsolute()
828 filename
.IsAbsolute()
831 currPath
+= wxFILE_SEP_PATH
;
834 if (!DirExists(currPath
))
836 if (!wxMkdir(currPath
, perm
))
838 // no need to try creating further directories
848 return ::wxMkdir( dir
, perm
);
851 bool wxFileName::Rmdir()
853 return wxFileName::Rmdir( GetFullPath() );
856 bool wxFileName::Rmdir( const wxString
&dir
)
858 return ::wxRmdir( dir
);
861 // ----------------------------------------------------------------------------
862 // path normalization
863 // ----------------------------------------------------------------------------
865 bool wxFileName::Normalize(int flags
,
869 // deal with env vars renaming first as this may seriously change the path
870 if ( flags
& wxPATH_NORM_ENV_VARS
)
872 wxString pathOrig
= GetFullPath(format
);
873 wxString path
= wxExpandEnvVars(pathOrig
);
874 if ( path
!= pathOrig
)
881 // the existing path components
882 wxArrayString dirs
= GetDirs();
884 // the path to prepend in front to make the path absolute
887 format
= GetFormat(format
);
889 // make the path absolute
890 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute(format
) )
894 curDir
.AssignCwd(GetVolume());
898 curDir
.AssignDir(cwd
);
901 // the path may be not absolute because it doesn't have the volume name
902 // but in this case we shouldn't modify the directory components of it
903 // but just set the current volume
904 if ( !HasVolume() && curDir
.HasVolume() )
906 SetVolume(curDir
.GetVolume());
910 // yes, it was the case - we don't need curDir then
916 // handle ~ stuff under Unix only
917 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
919 if ( !dirs
.IsEmpty() )
921 wxString dir
= dirs
[0u];
922 if ( !dir
.empty() && dir
[0u] == _T('~') )
924 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
931 // transform relative path into abs one
934 wxArrayString dirsNew
= curDir
.GetDirs();
935 size_t count
= dirs
.GetCount();
936 for ( size_t n
= 0; n
< count
; n
++ )
938 dirsNew
.Add(dirs
[n
]);
944 // now deal with ".", ".." and the rest
946 size_t count
= dirs
.GetCount();
947 for ( size_t n
= 0; n
< count
; n
++ )
949 wxString dir
= dirs
[n
];
951 if ( flags
& wxPATH_NORM_DOTS
)
953 if ( dir
== wxT(".") )
959 if ( dir
== wxT("..") )
961 if ( m_dirs
.IsEmpty() )
963 wxLogError(_("The path '%s' contains too many \"..\"!"),
964 GetFullPath().c_str());
968 m_dirs
.RemoveAt(m_dirs
.GetCount() - 1);
973 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
981 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
982 if ( (flags
& wxPATH_NORM_SHORTCUT
) )
985 if (GetShortcutTarget(GetFullPath(format
), filename
))
987 // Repeat this since we may now have a new path
988 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
990 filename
.MakeLower();
998 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
1000 // VZ: expand env vars here too?
1002 m_volume
.MakeLower();
1007 // we do have the path now
1009 // NB: need to do this before (maybe) calling Assign() below
1012 #if defined(__WIN32__)
1013 if ( (flags
& wxPATH_NORM_LONG
) && (format
== wxPATH_DOS
) )
1015 Assign(GetLongPath());
1022 // ----------------------------------------------------------------------------
1023 // get the shortcut target
1024 // ----------------------------------------------------------------------------
1026 // WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions.
1027 // The .lnk file is a plain text file so it should be easy to
1028 // make it work. Hint from Google Groups:
1029 // "If you open up a lnk file, you'll see a
1030 // number, followed by a pound sign (#), followed by more text. The
1031 // number is the number of characters that follows the pound sign. The
1032 // characters after the pound sign are the command line (which _can_
1033 // include arguments) to be executed. Any path (e.g. \windows\program
1034 // files\myapp.exe) that includes spaces needs to be enclosed in
1035 // quotation marks."
1037 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
1038 // The following lines are necessary under WinCE
1039 // #include "wx/msw/private.h"
1040 // #include <ole2.h>
1042 #if defined(__WXWINCE__)
1043 #include <shlguid.h>
1046 bool wxFileName::GetShortcutTarget(const wxString
& shortcutPath
, wxString
& targetFilename
, wxString
* arguments
)
1048 wxString path
, file
, ext
;
1049 wxSplitPath(shortcutPath
, & path
, & file
, & ext
);
1053 bool success
= false;
1055 // Assume it's not a shortcut if it doesn't end with lnk
1056 if (ext
.CmpNoCase(wxT("lnk"))!=0)
1059 // create a ShellLink object
1060 hres
= CoCreateInstance(CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
,
1061 IID_IShellLink
, (LPVOID
*) &psl
);
1063 if (SUCCEEDED(hres
))
1066 hres
= psl
->QueryInterface( IID_IPersistFile
, (LPVOID
*) &ppf
);
1067 if (SUCCEEDED(hres
))
1069 WCHAR wsz
[MAX_PATH
];
1071 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, shortcutPath
.mb_str(), -1, wsz
,
1074 hres
= ppf
->Load(wsz
, 0);
1075 if (SUCCEEDED(hres
))
1078 // Wrong prototype in early versions
1079 #if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2)
1080 psl
->GetPath((CHAR
*) buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1082 psl
->GetPath(buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1084 targetFilename
= wxString(buf
);
1085 success
= (shortcutPath
!= targetFilename
);
1087 psl
->GetArguments(buf
, 2048);
1089 if (!args
.empty() && arguments
)
1102 // ----------------------------------------------------------------------------
1103 // absolute/relative paths
1104 // ----------------------------------------------------------------------------
1106 bool wxFileName::IsAbsolute(wxPathFormat format
) const
1108 // if our path doesn't start with a path separator, it's not an absolute
1113 if ( !GetVolumeSeparator(format
).empty() )
1115 // this format has volumes and an absolute path must have one, it's not
1116 // enough to have the full path to bean absolute file under Windows
1117 if ( GetVolume().empty() )
1124 bool wxFileName::MakeRelativeTo(const wxString
& pathBase
, wxPathFormat format
)
1126 wxFileName fnBase
= wxFileName::DirName(pathBase
, format
);
1128 // get cwd only once - small time saving
1129 wxString cwd
= wxGetCwd();
1130 Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1131 fnBase
.Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1133 bool withCase
= IsCaseSensitive(format
);
1135 // we can't do anything if the files live on different volumes
1136 if ( !GetVolume().IsSameAs(fnBase
.GetVolume(), withCase
) )
1142 // same drive, so we don't need our volume
1145 // remove common directories starting at the top
1146 while ( !m_dirs
.IsEmpty() && !fnBase
.m_dirs
.IsEmpty() &&
1147 m_dirs
[0u].IsSameAs(fnBase
.m_dirs
[0u], withCase
) )
1150 fnBase
.m_dirs
.RemoveAt(0);
1153 // add as many ".." as needed
1154 size_t count
= fnBase
.m_dirs
.GetCount();
1155 for ( size_t i
= 0; i
< count
; i
++ )
1157 m_dirs
.Insert(wxT(".."), 0u);
1160 if ( format
== wxPATH_UNIX
|| format
== wxPATH_DOS
)
1162 // a directory made relative with respect to itself is '.' under Unix
1163 // and DOS, by definition (but we don't have to insert "./" for the
1165 if ( m_dirs
.IsEmpty() && IsDir() )
1167 m_dirs
.Add(_T('.'));
1177 // ----------------------------------------------------------------------------
1178 // filename kind tests
1179 // ----------------------------------------------------------------------------
1181 bool wxFileName::SameAs(const wxFileName
& filepath
, wxPathFormat format
) const
1183 wxFileName fn1
= *this,
1186 // get cwd only once - small time saving
1187 wxString cwd
= wxGetCwd();
1188 fn1
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1189 fn2
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1191 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
1194 // TODO: compare inodes for Unix, this works even when filenames are
1195 // different but files are the same (symlinks) (VZ)
1201 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
1203 // only Unix filenames are truely case-sensitive
1204 return GetFormat(format
) == wxPATH_UNIX
;
1208 wxString
wxFileName::GetForbiddenChars(wxPathFormat format
)
1210 // Inits to forbidden characters that are common to (almost) all platforms.
1211 wxString strForbiddenChars
= wxT("*?");
1213 // If asserts, wxPathFormat has been changed. In case of a new path format
1214 // addition, the following code might have to be updated.
1215 wxCOMPILE_TIME_ASSERT(wxPATH_MAX
== 5, wxPathFormatChanged
);
1216 switch ( GetFormat(format
) )
1219 wxFAIL_MSG( wxT("Unknown path format") );
1220 // !! Fall through !!
1226 // On a Mac even names with * and ? are allowed (Tested with OS
1227 // 9.2.1 and OS X 10.2.5)
1228 strForbiddenChars
= wxEmptyString
;
1232 strForbiddenChars
+= wxT("\\/:\"<>|");
1239 return strForbiddenChars
;
1243 wxString
wxFileName::GetVolumeSeparator(wxPathFormat
WXUNUSED_IN_WINCE(format
))
1246 return wxEmptyString
;
1250 if ( (GetFormat(format
) == wxPATH_DOS
) ||
1251 (GetFormat(format
) == wxPATH_VMS
) )
1253 sepVol
= wxFILE_SEP_DSK
;
1262 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
1265 switch ( GetFormat(format
) )
1268 // accept both as native APIs do but put the native one first as
1269 // this is the one we use in GetFullPath()
1270 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
1274 wxFAIL_MSG( _T("Unknown wxPATH_XXX style") );
1278 seps
= wxFILE_SEP_PATH_UNIX
;
1282 seps
= wxFILE_SEP_PATH_MAC
;
1286 seps
= wxFILE_SEP_PATH_VMS
;
1294 wxString
wxFileName::GetPathTerminators(wxPathFormat format
)
1296 format
= GetFormat(format
);
1298 // under VMS the end of the path is ']', not the path separator used to
1299 // separate the components
1300 return format
== wxPATH_VMS
? wxString(_T(']')) : GetPathSeparators(format
);
1304 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
1306 // wxString::Find() doesn't work as expected with NUL - it will always find
1307 // it, so test for it separately
1308 return ch
!= _T('\0') && GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
1311 // ----------------------------------------------------------------------------
1312 // path components manipulation
1313 // ----------------------------------------------------------------------------
1315 /* static */ bool wxFileName::IsValidDirComponent(const wxString
& dir
)
1319 wxFAIL_MSG( _T("empty directory passed to wxFileName::InsertDir()") );
1324 const size_t len
= dir
.length();
1325 for ( size_t n
= 0; n
< len
; n
++ )
1327 if ( dir
[n
] == GetVolumeSeparator() || IsPathSeparator(dir
[n
]) )
1329 wxFAIL_MSG( _T("invalid directory component in wxFileName") );
1338 void wxFileName::AppendDir( const wxString
& dir
)
1340 if ( IsValidDirComponent(dir
) )
1344 void wxFileName::PrependDir( const wxString
& dir
)
1349 void wxFileName::InsertDir(size_t before
, const wxString
& dir
)
1351 if ( IsValidDirComponent(dir
) )
1352 m_dirs
.Insert(dir
, before
);
1355 void wxFileName::RemoveDir(size_t pos
)
1357 m_dirs
.RemoveAt(pos
);
1360 // ----------------------------------------------------------------------------
1362 // ----------------------------------------------------------------------------
1364 void wxFileName::SetFullName(const wxString
& fullname
)
1366 SplitPath(fullname
, NULL
/* no volume */, NULL
/* no path */,
1367 &m_name
, &m_ext
, &m_hasExt
);
1370 wxString
wxFileName::GetFullName() const
1372 wxString fullname
= m_name
;
1375 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
1381 wxString
wxFileName::GetPath( int flags
, wxPathFormat format
) const
1383 format
= GetFormat( format
);
1387 // return the volume with the path as well if requested
1388 if ( flags
& wxPATH_GET_VOLUME
)
1390 fullpath
+= wxGetVolumeString(GetVolume(), format
);
1393 // the leading character
1398 fullpath
+= wxFILE_SEP_PATH_MAC
;
1403 fullpath
+= wxFILE_SEP_PATH_DOS
;
1407 wxFAIL_MSG( wxT("Unknown path format") );
1413 // normally the absolute file names start with a slash
1414 // with one exception: the ones like "~/foo.bar" don't
1416 if ( m_dirs
.IsEmpty() || m_dirs
[0u] != _T('~') )
1418 fullpath
+= wxFILE_SEP_PATH_UNIX
;
1424 // no leading character here but use this place to unset
1425 // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense
1426 // as, if I understand correctly, there should never be a dot
1427 // before the closing bracket
1428 flags
&= ~wxPATH_GET_SEPARATOR
;
1431 if ( m_dirs
.empty() )
1433 // there is nothing more
1437 // then concatenate all the path components using the path separator
1438 if ( format
== wxPATH_VMS
)
1440 fullpath
+= wxT('[');
1443 const size_t dirCount
= m_dirs
.GetCount();
1444 for ( size_t i
= 0; i
< dirCount
; i
++ )
1449 if ( m_dirs
[i
] == wxT(".") )
1451 // skip appending ':', this shouldn't be done in this
1452 // case as "::" is interpreted as ".." under Unix
1456 // convert back from ".." to nothing
1457 if ( !m_dirs
[i
].IsSameAs(wxT("..")) )
1458 fullpath
+= m_dirs
[i
];
1462 wxFAIL_MSG( wxT("Unexpected path format") );
1463 // still fall through
1467 fullpath
+= m_dirs
[i
];
1471 // TODO: What to do with ".." under VMS
1473 // convert back from ".." to nothing
1474 if ( !m_dirs
[i
].IsSameAs(wxT("..")) )
1475 fullpath
+= m_dirs
[i
];
1479 if ( (flags
& wxPATH_GET_SEPARATOR
) || (i
!= dirCount
- 1) )
1480 fullpath
+= GetPathSeparator(format
);
1483 if ( format
== wxPATH_VMS
)
1485 fullpath
+= wxT(']');
1491 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
1493 // we already have a function to get the path
1494 wxString fullpath
= GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
,
1497 // now just add the file name and extension to it
1498 fullpath
+= GetFullName();
1503 // Return the short form of the path (returns identity on non-Windows platforms)
1504 wxString
wxFileName::GetShortPath() const
1506 wxString
path(GetFullPath());
1508 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
1509 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
1513 if ( ::GetShortPathName
1516 wxStringBuffer(pathOut
, sz
),
1528 // Return the long form of the path (returns identity on non-Windows platforms)
1529 wxString
wxFileName::GetLongPath() const
1532 path
= GetFullPath();
1534 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1536 #if wxUSE_DYNAMIC_LOADER
1537 typedef DWORD (WINAPI
*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
1539 // this is MT-safe as in the worst case we're going to resolve the function
1540 // twice -- but as the result is the same in both threads, it's ok
1541 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
1542 if ( !s_pfnGetLongPathName
)
1544 static bool s_triedToLoad
= false;
1546 if ( !s_triedToLoad
)
1548 s_triedToLoad
= true;
1550 wxDynamicLibrary
dllKernel(_T("kernel32"));
1552 const wxChar
* GetLongPathName
= _T("GetLongPathName")
1557 #endif // Unicode/ANSI
1559 if ( dllKernel
.HasSymbol(GetLongPathName
) )
1561 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
)
1562 dllKernel
.GetSymbol(GetLongPathName
);
1565 // note that kernel32.dll can be unloaded, it stays in memory
1566 // anyhow as all Win32 programs link to it and so it's safe to call
1567 // GetLongPathName() even after unloading it
1571 if ( s_pfnGetLongPathName
)
1573 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1576 if ( (*s_pfnGetLongPathName
)
1579 wxStringBuffer(pathOut
, dwSize
),
1587 #endif // wxUSE_DYNAMIC_LOADER
1589 // The OS didn't support GetLongPathName, or some other error.
1590 // We need to call FindFirstFile on each component in turn.
1592 WIN32_FIND_DATA findFileData
;
1596 pathOut
= GetVolume() +
1597 GetVolumeSeparator(wxPATH_DOS
) +
1598 GetPathSeparator(wxPATH_DOS
);
1600 pathOut
= wxEmptyString
;
1602 wxArrayString dirs
= GetDirs();
1603 dirs
.Add(GetFullName());
1607 size_t count
= dirs
.GetCount();
1608 for ( size_t i
= 0; i
< count
; i
++ )
1610 // We're using pathOut to collect the long-name path, but using a
1611 // temporary for appending the last path component which may be
1613 tmpPath
= pathOut
+ dirs
[i
];
1615 if ( tmpPath
.empty() )
1618 // can't see this being necessary? MF
1619 if ( tmpPath
.Last() == GetVolumeSeparator(wxPATH_DOS
) )
1621 // Can't pass a drive and root dir to FindFirstFile,
1622 // so continue to next dir
1623 tmpPath
+= wxFILE_SEP_PATH
;
1628 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
1629 if (hFind
== INVALID_HANDLE_VALUE
)
1631 // Error: most likely reason is that path doesn't exist, so
1632 // append any unprocessed parts and return
1633 for ( i
+= 1; i
< count
; i
++ )
1634 tmpPath
+= wxFILE_SEP_PATH
+ dirs
[i
];
1639 pathOut
+= findFileData
.cFileName
;
1640 if ( (i
< (count
-1)) )
1641 pathOut
+= wxFILE_SEP_PATH
;
1647 #endif // Win32/!Win32
1652 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
1654 if (format
== wxPATH_NATIVE
)
1656 #if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__)
1657 format
= wxPATH_DOS
;
1658 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1659 format
= wxPATH_MAC
;
1660 #elif defined(__VMS)
1661 format
= wxPATH_VMS
;
1663 format
= wxPATH_UNIX
;
1669 // ----------------------------------------------------------------------------
1670 // path splitting function
1671 // ----------------------------------------------------------------------------
1675 wxFileName::SplitVolume(const wxString
& fullpathWithVolume
,
1676 wxString
*pstrVolume
,
1678 wxPathFormat format
)
1680 format
= GetFormat(format
);
1682 wxString fullpath
= fullpathWithVolume
;
1684 // special Windows UNC paths hack: transform \\share\path into share:path
1685 if ( format
== wxPATH_DOS
)
1687 if ( fullpath
.length() >= 4 &&
1688 fullpath
[0u] == wxFILE_SEP_PATH_DOS
&&
1689 fullpath
[1u] == wxFILE_SEP_PATH_DOS
)
1691 fullpath
.erase(0, 2);
1693 size_t posFirstSlash
=
1694 fullpath
.find_first_of(GetPathTerminators(format
));
1695 if ( posFirstSlash
!= wxString::npos
)
1697 fullpath
[posFirstSlash
] = wxFILE_SEP_DSK
;
1699 // UNC paths are always absolute, right? (FIXME)
1700 fullpath
.insert(posFirstSlash
+ 1, 1, wxFILE_SEP_PATH_DOS
);
1705 // We separate the volume here
1706 if ( format
== wxPATH_DOS
|| format
== wxPATH_VMS
)
1708 wxString sepVol
= GetVolumeSeparator(format
);
1710 size_t posFirstColon
= fullpath
.find_first_of(sepVol
);
1711 if ( posFirstColon
!= wxString::npos
)
1715 *pstrVolume
= fullpath
.Left(posFirstColon
);
1718 // remove the volume name and the separator from the full path
1719 fullpath
.erase(0, posFirstColon
+ sepVol
.length());
1724 *pstrPath
= fullpath
;
1728 void wxFileName::SplitPath(const wxString
& fullpathWithVolume
,
1729 wxString
*pstrVolume
,
1734 wxPathFormat format
)
1736 format
= GetFormat(format
);
1739 SplitVolume(fullpathWithVolume
, pstrVolume
, &fullpath
, format
);
1741 // find the positions of the last dot and last path separator in the path
1742 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
1743 size_t posLastSlash
= fullpath
.find_last_of(GetPathTerminators(format
));
1745 // check whether this dot occurs at the very beginning of a path component
1746 if ( (posLastDot
!= wxString::npos
) &&
1748 IsPathSeparator(fullpath
[posLastDot
- 1]) ||
1749 (format
== wxPATH_VMS
&& fullpath
[posLastDot
- 1] == _T(']'))) )
1751 // dot may be (and commonly -- at least under Unix -- is) the first
1752 // character of the filename, don't treat the entire filename as
1753 // extension in this case
1754 posLastDot
= wxString::npos
;
1757 // if we do have a dot and a slash, check that the dot is in the name part
1758 if ( (posLastDot
!= wxString::npos
) &&
1759 (posLastSlash
!= wxString::npos
) &&
1760 (posLastDot
< posLastSlash
) )
1762 // the dot is part of the path, not the start of the extension
1763 posLastDot
= wxString::npos
;
1766 // now fill in the variables provided by user
1769 if ( posLastSlash
== wxString::npos
)
1776 // take everything up to the path separator but take care to make
1777 // the path equal to something like '/', not empty, for the files
1778 // immediately under root directory
1779 size_t len
= posLastSlash
;
1781 // this rule does not apply to mac since we do not start with colons (sep)
1782 // except for relative paths
1783 if ( !len
&& format
!= wxPATH_MAC
)
1786 *pstrPath
= fullpath
.Left(len
);
1788 // special VMS hack: remove the initial bracket
1789 if ( format
== wxPATH_VMS
)
1791 if ( (*pstrPath
)[0u] == _T('[') )
1792 pstrPath
->erase(0, 1);
1799 // take all characters starting from the one after the last slash and
1800 // up to, but excluding, the last dot
1801 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
1803 if ( posLastDot
== wxString::npos
)
1805 // take all until the end
1806 count
= wxString::npos
;
1808 else if ( posLastSlash
== wxString::npos
)
1812 else // have both dot and slash
1814 count
= posLastDot
- posLastSlash
- 1;
1817 *pstrName
= fullpath
.Mid(nStart
, count
);
1820 // finally deal with the extension here: we have an added complication that
1821 // extension may be empty (but present) as in "foo." where trailing dot
1822 // indicates the empty extension at the end -- and hence we must remember
1823 // that we have it independently of pstrExt
1824 if ( posLastDot
== wxString::npos
)
1834 // take everything after the dot
1836 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
1843 void wxFileName::SplitPath(const wxString
& fullpath
,
1847 wxPathFormat format
)
1850 SplitPath(fullpath
, &volume
, path
, name
, ext
, format
);
1854 path
->Prepend(wxGetVolumeString(volume
, format
));
1858 // ----------------------------------------------------------------------------
1860 // ----------------------------------------------------------------------------
1864 bool wxFileName::SetTimes(const wxDateTime
*dtAccess
,
1865 const wxDateTime
*dtMod
,
1866 const wxDateTime
*dtCreate
)
1868 #if defined(__WIN32__)
1871 // VZ: please let me know how to do this if you can
1872 wxFAIL_MSG( _T("SetTimes() not implemented for the directories") );
1876 wxFileHandle
fh(GetFullPath(), wxFileHandle::Write
);
1879 FILETIME ftAccess
, ftCreate
, ftWrite
;
1882 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
1884 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
1886 ConvertWxToFileTime(&ftWrite
, *dtMod
);
1888 if ( ::SetFileTime(fh
,
1889 dtCreate
? &ftCreate
: NULL
,
1890 dtAccess
? &ftAccess
: NULL
,
1891 dtMod
? &ftWrite
: NULL
) )
1897 #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
1898 wxUnusedVar(dtCreate
);
1900 if ( !dtAccess
&& !dtMod
)
1902 // can't modify the creation time anyhow, don't try
1906 // if dtAccess or dtMod is not specified, use the other one (which must be
1907 // non NULL because of the test above) for both times
1909 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
1910 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
1911 if ( utime(GetFullPath().fn_str(), &utm
) == 0 )
1915 #else // other platform
1916 wxUnusedVar(dtAccess
);
1918 wxUnusedVar(dtCreate
);
1921 wxLogSysError(_("Failed to modify file times for '%s'"),
1922 GetFullPath().c_str());
1927 bool wxFileName::Touch()
1929 #if defined(__UNIX_LIKE__)
1930 // under Unix touching file is simple: just pass NULL to utime()
1931 if ( utime(GetFullPath().fn_str(), NULL
) == 0 )
1936 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1939 #else // other platform
1940 wxDateTime dtNow
= wxDateTime::Now();
1942 return SetTimes(&dtNow
, &dtNow
, NULL
/* don't change create time */);
1946 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
1948 wxDateTime
*dtCreate
) const
1950 #if defined(__WIN32__)
1951 // we must use different methods for the files and directories under
1952 // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and
1953 // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and
1956 FILETIME ftAccess
, ftCreate
, ftWrite
;
1959 // implemented in msw/dir.cpp
1960 extern bool wxGetDirectoryTimes(const wxString
& dirname
,
1961 FILETIME
*, FILETIME
*, FILETIME
*);
1963 // we should pass the path without the trailing separator to
1964 // wxGetDirectoryTimes()
1965 ok
= wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME
),
1966 &ftAccess
, &ftCreate
, &ftWrite
);
1970 wxFileHandle
fh(GetFullPath(), wxFileHandle::Read
);
1973 ok
= ::GetFileTime(fh
,
1974 dtCreate
? &ftCreate
: NULL
,
1975 dtAccess
? &ftAccess
: NULL
,
1976 dtMod
? &ftWrite
: NULL
) != 0;
1987 ConvertFileTimeToWx(dtCreate
, ftCreate
);
1989 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1991 ConvertFileTimeToWx(dtMod
, ftWrite
);
1995 #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__))
1997 if ( wxStat( GetFullPath().c_str(), &stBuf
) == 0 )
2000 dtAccess
->Set(stBuf
.st_atime
);
2002 dtMod
->Set(stBuf
.st_mtime
);
2004 dtCreate
->Set(stBuf
.st_ctime
);
2008 #else // other platform
2009 wxUnusedVar(dtAccess
);
2011 wxUnusedVar(dtCreate
);
2014 wxLogSysError(_("Failed to retrieve file times for '%s'"),
2015 GetFullPath().c_str());
2020 #endif // wxUSE_DATETIME
2024 const short kMacExtensionMaxLength
= 16 ;
2025 class MacDefaultExtensionRecord
2028 MacDefaultExtensionRecord()
2031 m_type
= m_creator
= 0 ;
2033 MacDefaultExtensionRecord( const MacDefaultExtensionRecord
& from
)
2035 wxStrcpy( m_ext
, from
.m_ext
) ;
2036 m_type
= from
.m_type
;
2037 m_creator
= from
.m_creator
;
2039 MacDefaultExtensionRecord( const wxChar
* extension
, OSType type
, OSType creator
)
2041 wxStrncpy( m_ext
, extension
, kMacExtensionMaxLength
) ;
2042 m_ext
[kMacExtensionMaxLength
] = 0 ;
2044 m_creator
= creator
;
2046 wxChar m_ext
[kMacExtensionMaxLength
] ;
2051 WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord
, MacDefaultExtensionArray
) ;
2053 bool gMacDefaultExtensionsInited
= false ;
2055 #include "wx/arrimpl.cpp"
2057 WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray
) ;
2059 MacDefaultExtensionArray gMacDefaultExtensions
;
2061 // load the default extensions
2062 MacDefaultExtensionRecord gDefaults
[] =
2064 MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) ,
2065 MacDefaultExtensionRecord( wxT("tif") , 'TIFF' , '****' ) ,
2066 MacDefaultExtensionRecord( wxT("jpg") , 'JPEG' , '****' ) ,
2069 static void MacEnsureDefaultExtensionsLoaded()
2071 if ( !gMacDefaultExtensionsInited
)
2073 // we could load the pc exchange prefs here too
2074 for ( size_t i
= 0 ; i
< WXSIZEOF( gDefaults
) ; ++i
)
2076 gMacDefaultExtensions
.Add( gDefaults
[i
] ) ;
2078 gMacDefaultExtensionsInited
= true ;
2082 bool wxFileName::MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
)
2085 FSCatalogInfo catInfo
;
2088 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2090 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2092 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2093 finfo
->fileType
= type
;
2094 finfo
->fileCreator
= creator
;
2095 FSSetCatalogInfo( &fsRef
, kFSCatInfoFinderInfo
, &catInfo
) ;
2102 bool wxFileName::MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
)
2105 FSCatalogInfo catInfo
;
2108 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2110 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2112 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2113 *type
= finfo
->fileType
;
2114 *creator
= finfo
->fileCreator
;
2121 bool wxFileName::MacSetDefaultTypeAndCreator()
2123 wxUint32 type
, creator
;
2124 if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type
,
2127 return MacSetTypeAndCreator( type
, creator
) ;
2132 bool wxFileName::MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
)
2134 MacEnsureDefaultExtensionsLoaded() ;
2135 wxString extl
= ext
.Lower() ;
2136 for( int i
= gMacDefaultExtensions
.Count() - 1 ; i
>= 0 ; --i
)
2138 if ( gMacDefaultExtensions
.Item(i
).m_ext
== extl
)
2140 *type
= gMacDefaultExtensions
.Item(i
).m_type
;
2141 *creator
= gMacDefaultExtensions
.Item(i
).m_creator
;
2148 void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
)
2150 MacEnsureDefaultExtensionsLoaded() ;
2151 MacDefaultExtensionRecord rec
;
2153 rec
.m_creator
= creator
;
2154 wxStrncpy( rec
.m_ext
, ext
.Lower().c_str() , kMacExtensionMaxLength
) ;
2155 gMacDefaultExtensions
.Add( rec
) ;