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
;
303 m_hasExt
= filepath
.m_hasExt
;
306 void wxFileName::Assign(const wxString
& volume
,
307 const wxString
& path
,
308 const wxString
& name
,
311 wxPathFormat format
)
313 SetPath( path
, format
);
322 void wxFileName::SetPath( const wxString
& pathOrig
, wxPathFormat format
)
326 if ( pathOrig
.empty() )
334 format
= GetFormat( format
);
336 // 0) deal with possible volume part first
339 SplitVolume(pathOrig
, &volume
, &path
, format
);
340 if ( !volume
.empty() )
347 // 1) Determine if the path is relative or absolute.
348 wxChar leadingChar
= path
[0u];
353 m_relative
= leadingChar
== wxT(':');
355 // We then remove a leading ":". The reason is in our
356 // storage form for relative paths:
357 // ":dir:file.txt" actually means "./dir/file.txt" in
358 // DOS notation and should get stored as
359 // (relative) (dir) (file.txt)
360 // "::dir:file.txt" actually means "../dir/file.txt"
361 // stored as (relative) (..) (dir) (file.txt)
362 // This is important only for the Mac as an empty dir
363 // actually means <UP>, whereas under DOS, double
364 // slashes can be ignored: "\\\\" is the same as "\\".
370 // TODO: what is the relative path format here?
375 wxFAIL_MSG( _T("Unknown path format") );
376 // !! Fall through !!
379 // the paths of the form "~" or "~username" are absolute
380 m_relative
= leadingChar
!= wxT('/') && leadingChar
!= _T('~');
384 m_relative
= !IsPathSeparator(leadingChar
, format
);
389 // 2) Break up the path into its members. If the original path
390 // was just "/" or "\\", m_dirs will be empty. We know from
391 // the m_relative field, if this means "nothing" or "root dir".
393 wxStringTokenizer
tn( path
, GetPathSeparators(format
) );
395 while ( tn
.HasMoreTokens() )
397 wxString token
= tn
.GetNextToken();
399 // Remove empty token under DOS and Unix, interpret them
403 if (format
== wxPATH_MAC
)
404 m_dirs
.Add( wxT("..") );
414 void wxFileName::Assign(const wxString
& fullpath
,
417 wxString volume
, path
, name
, ext
;
419 SplitPath(fullpath
, &volume
, &path
, &name
, &ext
, &hasExt
, format
);
421 Assign(volume
, path
, name
, ext
, hasExt
, format
);
424 void wxFileName::Assign(const wxString
& fullpathOrig
,
425 const wxString
& fullname
,
428 // always recognize fullpath as directory, even if it doesn't end with a
430 wxString fullpath
= fullpathOrig
;
431 if ( !wxEndsWithPathSeparator(fullpath
) )
433 fullpath
+= GetPathSeparator(format
);
436 wxString volume
, path
, name
, ext
;
439 // do some consistency checks in debug mode: the name should be really just
440 // the filename and the path should be really just a path
442 wxString volDummy
, pathDummy
, nameDummy
, extDummy
;
444 SplitPath(fullname
, &volDummy
, &pathDummy
, &name
, &ext
, &hasExt
, format
);
446 wxASSERT_MSG( volDummy
.empty() && pathDummy
.empty(),
447 _T("the file name shouldn't contain the path") );
449 SplitPath(fullpath
, &volume
, &path
, &nameDummy
, &extDummy
, format
);
451 wxASSERT_MSG( nameDummy
.empty() && extDummy
.empty(),
452 _T("the path shouldn't contain file name nor extension") );
454 #else // !__WXDEBUG__
455 SplitPath(fullname
, NULL
/* no path */, &name
, &ext
, &hasExt
, format
);
456 SplitPath(fullpath
, &volume
, &path
, NULL
, NULL
, format
);
457 #endif // __WXDEBUG__/!__WXDEBUG__
459 Assign(volume
, path
, name
, ext
, hasExt
, format
);
462 void wxFileName::Assign(const wxString
& pathOrig
,
463 const wxString
& name
,
469 SplitVolume(pathOrig
, &volume
, &path
, format
);
471 Assign(volume
, path
, name
, ext
, format
);
474 void wxFileName::AssignDir(const wxString
& dir
, wxPathFormat format
)
476 Assign(dir
, wxEmptyString
, format
);
479 void wxFileName::Clear()
485 m_ext
= wxEmptyString
;
487 // we don't have any absolute path for now
495 wxFileName
wxFileName::FileName(const wxString
& file
, wxPathFormat format
)
497 return wxFileName(file
, format
);
501 wxFileName
wxFileName::DirName(const wxString
& dir
, wxPathFormat format
)
504 fn
.AssignDir(dir
, format
);
508 // ----------------------------------------------------------------------------
510 // ----------------------------------------------------------------------------
512 bool wxFileName::FileExists() const
514 return wxFileName::FileExists( GetFullPath() );
517 bool wxFileName::FileExists( const wxString
&file
)
519 return ::wxFileExists( file
);
522 bool wxFileName::DirExists() const
524 return wxFileName::DirExists( GetFullPath() );
527 bool wxFileName::DirExists( const wxString
&dir
)
529 return ::wxDirExists( dir
);
532 // ----------------------------------------------------------------------------
533 // CWD and HOME stuff
534 // ----------------------------------------------------------------------------
536 void wxFileName::AssignCwd(const wxString
& volume
)
538 AssignDir(wxFileName::GetCwd(volume
));
542 wxString
wxFileName::GetCwd(const wxString
& volume
)
544 // if we have the volume, we must get the current directory on this drive
545 // and to do this we have to chdir to this volume - at least under Windows,
546 // I don't know how to get the current drive on another volume elsewhere
549 if ( !volume
.empty() )
552 SetCwd(volume
+ GetVolumeSeparator());
555 wxString cwd
= ::wxGetCwd();
557 if ( !volume
.empty() )
565 bool wxFileName::SetCwd()
567 return wxFileName::SetCwd( GetFullPath() );
570 bool wxFileName::SetCwd( const wxString
&cwd
)
572 return ::wxSetWorkingDirectory( cwd
);
575 void wxFileName::AssignHomeDir()
577 AssignDir(wxFileName::GetHomeDir());
580 wxString
wxFileName::GetHomeDir()
582 return ::wxGetHomeDir();
587 void wxFileName::AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
589 wxString tempname
= CreateTempFileName(prefix
, fileTemp
);
590 if ( tempname
.empty() )
592 // error, failed to get temp file name
603 wxFileName::CreateTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
605 wxString path
, dir
, name
;
607 // use the directory specified by the prefix
608 SplitPath(prefix
, &dir
, &name
, NULL
/* extension */);
610 #if defined(__WXWINCE__)
613 // FIXME. Create \temp dir?
616 path
= dir
+ wxT("\\") + prefix
;
618 while (FileExists(path
))
620 path
= dir
+ wxT("\\") + prefix
;
625 #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
629 if ( !::GetTempPath(MAX_PATH
, wxStringBuffer(dir
, MAX_PATH
+ 1)) )
631 wxLogLastError(_T("GetTempPath"));
636 // GetTempFileName() fails if we pass it an empty string
640 else // we have a dir to create the file in
642 // ensure we use only the back slashes as GetTempFileName(), unlike all
643 // the other APIs, is picky and doesn't accept the forward ones
644 dir
.Replace(_T("/"), _T("\\"));
647 if ( !::GetTempFileName(dir
, name
, 0, wxStringBuffer(path
, MAX_PATH
+ 1)) )
649 wxLogLastError(_T("GetTempFileName"));
657 #if defined(__WXMAC__) && !defined(__DARWIN__)
658 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
660 dir
= wxGetenv(_T("TMP"));
663 dir
= wxGetenv(_T("TEMP"));
669 #if defined(__DOS__) || defined(__OS2__)
680 if ( !wxEndsWithPathSeparator(dir
) &&
681 (name
.empty() || !wxIsPathSeparator(name
[0u])) )
683 path
+= wxFILE_SEP_PATH
;
688 #if defined(HAVE_MKSTEMP)
689 // scratch space for mkstemp()
690 path
+= _T("XXXXXX");
692 // we need to copy the path to the buffer in which mkstemp() can modify it
693 wxCharBuffer
buf( wxConvFile
.cWX2MB( path
) );
695 // cast is safe because the string length doesn't change
696 int fdTemp
= mkstemp( (char*)(const char*) buf
);
699 // this might be not necessary as mkstemp() on most systems should have
700 // already done it but it doesn't hurt neither...
703 else // mkstemp() succeeded
705 path
= wxConvFile
.cMB2WX( (const char*) buf
);
707 // avoid leaking the fd
710 fileTemp
->Attach(fdTemp
);
717 #else // !HAVE_MKSTEMP
721 path
+= _T("XXXXXX");
723 wxCharBuffer buf
= wxConvFile
.cWX2MB( path
);
724 if ( !mktemp( (const char*) buf
) )
730 path
= wxConvFile
.cMB2WX( (const char*) buf
);
732 #else // !HAVE_MKTEMP (includes __DOS__)
733 // generate the unique file name ourselves
734 #if !defined(__DOS__) && !defined(__PALMOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
735 path
<< (unsigned int)getpid();
740 static const size_t numTries
= 1000;
741 for ( size_t n
= 0; n
< numTries
; n
++ )
743 // 3 hex digits is enough for numTries == 1000 < 4096
744 pathTry
= path
+ wxString::Format(_T("%.03x"), (unsigned int) n
);
745 if ( !FileExists(pathTry
) )
754 #endif // HAVE_MKTEMP/!HAVE_MKTEMP
759 #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
761 #endif // Windows/!Windows
765 wxLogSysError(_("Failed to create a temporary file name"));
767 else if ( fileTemp
&& !fileTemp
->IsOpened() )
769 // open the file - of course, there is a race condition here, this is
770 // why we always prefer using mkstemp()...
772 // NB: GetTempFileName() under Windows creates the file, so using
773 // write_excl there would fail
774 if ( !fileTemp
->Open(path
,
775 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
780 wxS_IRUSR
| wxS_IWUSR
) )
782 // FIXME: If !ok here should we loop and try again with another
783 // file name? That is the standard recourse if open(O_EXCL)
784 // fails, though of course it should be protected against
785 // possible infinite looping too.
787 wxLogError(_("Failed to open temporary file."));
798 // ----------------------------------------------------------------------------
799 // directory operations
800 // ----------------------------------------------------------------------------
802 bool wxFileName::Mkdir( int perm
, int flags
)
804 return wxFileName::Mkdir( GetFullPath(), perm
, flags
);
807 bool wxFileName::Mkdir( const wxString
& dir
, int perm
, int flags
)
809 if ( flags
& wxPATH_MKDIR_FULL
)
811 // split the path in components
813 filename
.AssignDir(dir
);
816 if ( filename
.HasVolume())
818 currPath
<< wxGetVolumeString(filename
.GetVolume(), wxPATH_NATIVE
);
821 wxArrayString dirs
= filename
.GetDirs();
822 size_t count
= dirs
.GetCount();
823 for ( size_t i
= 0; i
< count
; i
++ )
826 #if defined(__WXMAC__) && !defined(__DARWIN__)
827 // relative pathnames are exactely the other way round under mac...
828 !filename
.IsAbsolute()
830 filename
.IsAbsolute()
833 currPath
+= wxFILE_SEP_PATH
;
836 if (!DirExists(currPath
))
838 if (!wxMkdir(currPath
, perm
))
840 // no need to try creating further directories
850 return ::wxMkdir( dir
, perm
);
853 bool wxFileName::Rmdir()
855 return wxFileName::Rmdir( GetFullPath() );
858 bool wxFileName::Rmdir( const wxString
&dir
)
860 return ::wxRmdir( dir
);
863 // ----------------------------------------------------------------------------
864 // path normalization
865 // ----------------------------------------------------------------------------
867 bool wxFileName::Normalize(int flags
,
871 // deal with env vars renaming first as this may seriously change the path
872 if ( flags
& wxPATH_NORM_ENV_VARS
)
874 wxString pathOrig
= GetFullPath(format
);
875 wxString path
= wxExpandEnvVars(pathOrig
);
876 if ( path
!= pathOrig
)
883 // the existing path components
884 wxArrayString dirs
= GetDirs();
886 // the path to prepend in front to make the path absolute
889 format
= GetFormat(format
);
891 // make the path absolute
892 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute(format
) )
896 curDir
.AssignCwd(GetVolume());
900 curDir
.AssignDir(cwd
);
903 // the path may be not absolute because it doesn't have the volume name
904 // but in this case we shouldn't modify the directory components of it
905 // but just set the current volume
906 if ( !HasVolume() && curDir
.HasVolume() )
908 SetVolume(curDir
.GetVolume());
912 // yes, it was the case - we don't need curDir then
918 // handle ~ stuff under Unix only
919 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
921 if ( !dirs
.IsEmpty() )
923 wxString dir
= dirs
[0u];
924 if ( !dir
.empty() && dir
[0u] == _T('~') )
926 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
933 // transform relative path into abs one
936 wxArrayString dirsNew
= curDir
.GetDirs();
937 size_t count
= dirs
.GetCount();
938 for ( size_t n
= 0; n
< count
; n
++ )
940 dirsNew
.Add(dirs
[n
]);
946 // now deal with ".", ".." and the rest
948 size_t count
= dirs
.GetCount();
949 for ( size_t n
= 0; n
< count
; n
++ )
951 wxString dir
= dirs
[n
];
953 if ( flags
& wxPATH_NORM_DOTS
)
955 if ( dir
== wxT(".") )
961 if ( dir
== wxT("..") )
963 if ( m_dirs
.IsEmpty() )
965 wxLogError(_("The path '%s' contains too many \"..\"!"),
966 GetFullPath().c_str());
970 m_dirs
.RemoveAt(m_dirs
.GetCount() - 1);
975 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
983 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
984 if ( (flags
& wxPATH_NORM_SHORTCUT
) )
987 if (GetShortcutTarget(GetFullPath(format
), filename
))
989 // Repeat this since we may now have a new path
990 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
992 filename
.MakeLower();
1000 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
1002 // VZ: expand env vars here too?
1004 m_volume
.MakeLower();
1009 // we do have the path now
1011 // NB: need to do this before (maybe) calling Assign() below
1014 #if defined(__WIN32__)
1015 if ( (flags
& wxPATH_NORM_LONG
) && (format
== wxPATH_DOS
) )
1017 Assign(GetLongPath());
1024 // ----------------------------------------------------------------------------
1025 // get the shortcut target
1026 // ----------------------------------------------------------------------------
1028 // WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions.
1029 // The .lnk file is a plain text file so it should be easy to
1030 // make it work. Hint from Google Groups:
1031 // "If you open up a lnk file, you'll see a
1032 // number, followed by a pound sign (#), followed by more text. The
1033 // number is the number of characters that follows the pound sign. The
1034 // characters after the pound sign are the command line (which _can_
1035 // include arguments) to be executed. Any path (e.g. \windows\program
1036 // files\myapp.exe) that includes spaces needs to be enclosed in
1037 // quotation marks."
1039 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
1040 // The following lines are necessary under WinCE
1041 // #include "wx/msw/private.h"
1042 // #include <ole2.h>
1044 #if defined(__WXWINCE__)
1045 #include <shlguid.h>
1048 bool wxFileName::GetShortcutTarget(const wxString
& shortcutPath
, wxString
& targetFilename
, wxString
* arguments
)
1050 wxString path
, file
, ext
;
1051 wxSplitPath(shortcutPath
, & path
, & file
, & ext
);
1055 bool success
= false;
1057 // Assume it's not a shortcut if it doesn't end with lnk
1058 if (ext
.Lower() != wxT("lnk"))
1061 // create a ShellLink object
1062 hres
= CoCreateInstance(CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
,
1063 IID_IShellLink
, (LPVOID
*) &psl
);
1065 if (SUCCEEDED(hres
))
1068 hres
= psl
->QueryInterface( IID_IPersistFile
, (LPVOID
*) &ppf
);
1069 if (SUCCEEDED(hres
))
1071 WCHAR wsz
[MAX_PATH
];
1073 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, shortcutPath
.mb_str(), -1, wsz
,
1076 hres
= ppf
->Load(wsz
, 0);
1077 if (SUCCEEDED(hres
))
1080 // Wrong prototype in early versions
1081 #if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2)
1082 psl
->GetPath((CHAR
*) buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1084 psl
->GetPath(buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1086 targetFilename
= wxString(buf
);
1087 success
= (shortcutPath
!= targetFilename
);
1089 psl
->GetArguments(buf
, 2048);
1091 if (!args
.empty() && arguments
)
1104 // ----------------------------------------------------------------------------
1105 // absolute/relative paths
1106 // ----------------------------------------------------------------------------
1108 bool wxFileName::IsAbsolute(wxPathFormat format
) const
1110 // if our path doesn't start with a path separator, it's not an absolute
1115 if ( !GetVolumeSeparator(format
).empty() )
1117 // this format has volumes and an absolute path must have one, it's not
1118 // enough to have the full path to bean absolute file under Windows
1119 if ( GetVolume().empty() )
1126 bool wxFileName::MakeRelativeTo(const wxString
& pathBase
, wxPathFormat format
)
1128 wxFileName fnBase
= wxFileName::DirName(pathBase
, format
);
1130 // get cwd only once - small time saving
1131 wxString cwd
= wxGetCwd();
1132 Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1133 fnBase
.Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1135 bool withCase
= IsCaseSensitive(format
);
1137 // we can't do anything if the files live on different volumes
1138 if ( !GetVolume().IsSameAs(fnBase
.GetVolume(), withCase
) )
1144 // same drive, so we don't need our volume
1147 // remove common directories starting at the top
1148 while ( !m_dirs
.IsEmpty() && !fnBase
.m_dirs
.IsEmpty() &&
1149 m_dirs
[0u].IsSameAs(fnBase
.m_dirs
[0u], withCase
) )
1152 fnBase
.m_dirs
.RemoveAt(0);
1155 // add as many ".." as needed
1156 size_t count
= fnBase
.m_dirs
.GetCount();
1157 for ( size_t i
= 0; i
< count
; i
++ )
1159 m_dirs
.Insert(wxT(".."), 0u);
1162 if ( format
== wxPATH_UNIX
|| format
== wxPATH_DOS
)
1164 // a directory made relative with respect to itself is '.' under Unix
1165 // and DOS, by definition (but we don't have to insert "./" for the
1167 if ( m_dirs
.IsEmpty() && IsDir() )
1169 m_dirs
.Add(_T('.'));
1179 // ----------------------------------------------------------------------------
1180 // filename kind tests
1181 // ----------------------------------------------------------------------------
1183 bool wxFileName::SameAs(const wxFileName
& filepath
, wxPathFormat format
) const
1185 wxFileName fn1
= *this,
1188 // get cwd only once - small time saving
1189 wxString cwd
= wxGetCwd();
1190 fn1
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1191 fn2
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1193 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
1196 // TODO: compare inodes for Unix, this works even when filenames are
1197 // different but files are the same (symlinks) (VZ)
1203 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
1205 // only Unix filenames are truely case-sensitive
1206 return GetFormat(format
) == wxPATH_UNIX
;
1210 wxString
wxFileName::GetForbiddenChars(wxPathFormat format
)
1212 // Inits to forbidden characters that are common to (almost) all platforms.
1213 wxString strForbiddenChars
= wxT("*?");
1215 // If asserts, wxPathFormat has been changed. In case of a new path format
1216 // addition, the following code might have to be updated.
1217 wxCOMPILE_TIME_ASSERT(wxPATH_MAX
== 5, wxPathFormatChanged
);
1218 switch ( GetFormat(format
) )
1221 wxFAIL_MSG( wxT("Unknown path format") );
1222 // !! Fall through !!
1228 // On a Mac even names with * and ? are allowed (Tested with OS
1229 // 9.2.1 and OS X 10.2.5)
1230 strForbiddenChars
= wxEmptyString
;
1234 strForbiddenChars
+= wxT("\\/:\"<>|");
1241 return strForbiddenChars
;
1245 wxString
wxFileName::GetVolumeSeparator(wxPathFormat format
)
1249 if ( (GetFormat(format
) == wxPATH_DOS
) ||
1250 (GetFormat(format
) == wxPATH_VMS
) )
1252 sepVol
= wxFILE_SEP_DSK
;
1260 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
1263 switch ( GetFormat(format
) )
1266 // accept both as native APIs do but put the native one first as
1267 // this is the one we use in GetFullPath()
1268 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
1272 wxFAIL_MSG( _T("Unknown wxPATH_XXX style") );
1276 seps
= wxFILE_SEP_PATH_UNIX
;
1280 seps
= wxFILE_SEP_PATH_MAC
;
1284 seps
= wxFILE_SEP_PATH_VMS
;
1292 wxString
wxFileName::GetPathTerminators(wxPathFormat format
)
1294 format
= GetFormat(format
);
1296 // under VMS the end of the path is ']', not the path separator used to
1297 // separate the components
1298 return format
== wxPATH_VMS
? wxString(_T(']')) : GetPathSeparators(format
);
1302 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
1304 // wxString::Find() doesn't work as expected with NUL - it will always find
1305 // it, so test for it separately
1306 return ch
!= _T('\0') && GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
1309 // ----------------------------------------------------------------------------
1310 // path components manipulation
1311 // ----------------------------------------------------------------------------
1313 /* static */ bool wxFileName::IsValidDirComponent(const wxString
& dir
)
1317 wxFAIL_MSG( _T("empty directory passed to wxFileName::InsertDir()") );
1322 const size_t len
= dir
.length();
1323 for ( size_t n
= 0; n
< len
; n
++ )
1325 if ( dir
[n
] == GetVolumeSeparator() || IsPathSeparator(dir
[n
]) )
1327 wxFAIL_MSG( _T("invalid directory component in wxFileName") );
1336 void wxFileName::AppendDir( const wxString
& dir
)
1338 if ( IsValidDirComponent(dir
) )
1342 void wxFileName::PrependDir( const wxString
& dir
)
1347 void wxFileName::InsertDir(size_t before
, const wxString
& dir
)
1349 if ( IsValidDirComponent(dir
) )
1350 m_dirs
.Insert(dir
, before
);
1353 void wxFileName::RemoveDir(size_t pos
)
1355 m_dirs
.RemoveAt(pos
);
1358 // ----------------------------------------------------------------------------
1360 // ----------------------------------------------------------------------------
1362 void wxFileName::SetFullName(const wxString
& fullname
)
1364 SplitPath(fullname
, NULL
/* no volume */, NULL
/* no path */,
1365 &m_name
, &m_ext
, &m_hasExt
);
1368 wxString
wxFileName::GetFullName() const
1370 wxString fullname
= m_name
;
1373 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
1379 wxString
wxFileName::GetPath( int flags
, wxPathFormat format
) const
1381 format
= GetFormat( format
);
1385 // return the volume with the path as well if requested
1386 if ( flags
& wxPATH_GET_VOLUME
)
1388 fullpath
+= wxGetVolumeString(GetVolume(), format
);
1391 // the leading character
1396 fullpath
+= wxFILE_SEP_PATH_MAC
;
1401 fullpath
+= wxFILE_SEP_PATH_DOS
;
1405 wxFAIL_MSG( wxT("Unknown path format") );
1411 // normally the absolute file names start with a slash
1412 // with one exception: the ones like "~/foo.bar" don't
1414 if ( m_dirs
.IsEmpty() || m_dirs
[0u] != _T('~') )
1416 fullpath
+= wxFILE_SEP_PATH_UNIX
;
1422 // no leading character here but use this place to unset
1423 // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense
1424 // as, if I understand correctly, there should never be a dot
1425 // before the closing bracket
1426 flags
&= ~wxPATH_GET_SEPARATOR
;
1429 if ( m_dirs
.empty() )
1431 // there is nothing more
1435 // then concatenate all the path components using the path separator
1436 if ( format
== wxPATH_VMS
)
1438 fullpath
+= wxT('[');
1441 const size_t dirCount
= m_dirs
.GetCount();
1442 for ( size_t i
= 0; i
< dirCount
; i
++ )
1447 if ( m_dirs
[i
] == wxT(".") )
1449 // skip appending ':', this shouldn't be done in this
1450 // case as "::" is interpreted as ".." under Unix
1454 // convert back from ".." to nothing
1455 if ( m_dirs
[i
] != wxT("..") )
1456 fullpath
+= m_dirs
[i
];
1460 wxFAIL_MSG( wxT("Unexpected path format") );
1461 // still fall through
1465 fullpath
+= m_dirs
[i
];
1469 // TODO: What to do with ".." under VMS
1471 // convert back from ".." to nothing
1472 if ( m_dirs
[i
] != wxT("..") )
1473 fullpath
+= m_dirs
[i
];
1477 if ( (flags
& wxPATH_GET_SEPARATOR
) || (i
!= dirCount
- 1) )
1478 fullpath
+= GetPathSeparator(format
);
1481 if ( format
== wxPATH_VMS
)
1483 fullpath
+= wxT(']');
1489 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
1491 // we already have a function to get the path
1492 wxString fullpath
= GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
,
1495 // now just add the file name and extension to it
1496 fullpath
+= GetFullName();
1501 // Return the short form of the path (returns identity on non-Windows platforms)
1502 wxString
wxFileName::GetShortPath() const
1504 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
1505 wxString
path(GetFullPath());
1507 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
1511 ok
= ::GetShortPathName
1514 wxStringBuffer(pathOut
, sz
),
1523 return GetFullPath();
1527 // Return the long form of the path (returns identity on non-Windows platforms)
1528 wxString
wxFileName::GetLongPath() const
1531 path
= GetFullPath();
1533 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1534 bool success
= false;
1536 #if wxUSE_DYNAMIC_LOADER
1537 typedef DWORD (WINAPI
*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
1539 static bool s_triedToLoad
= false;
1541 if ( !s_triedToLoad
)
1543 // suppress the errors about missing GetLongPathName[AW]
1546 s_triedToLoad
= true;
1547 wxDynamicLibrary
dllKernel(_T("kernel32"));
1548 if ( dllKernel
.IsLoaded() )
1550 // may succeed or fail depending on the Windows version
1551 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
1553 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) dllKernel
.GetSymbol(_T("GetLongPathNameW"));
1555 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) dllKernel
.GetSymbol(_T("GetLongPathNameA"));
1558 if ( s_pfnGetLongPathName
)
1560 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1561 bool ok
= dwSize
> 0;
1565 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1569 ok
= (*s_pfnGetLongPathName
)
1572 wxStringBuffer(pathOut
, sz
),
1584 #endif // wxUSE_DYNAMIC_LOADER
1588 // The OS didn't support GetLongPathName, or some other error.
1589 // We need to call FindFirstFile on each component in turn.
1591 WIN32_FIND_DATA findFileData
;
1595 pathOut
= GetVolume() +
1596 GetVolumeSeparator(wxPATH_DOS
) +
1597 GetPathSeparator(wxPATH_DOS
);
1599 pathOut
= wxEmptyString
;
1601 wxArrayString dirs
= GetDirs();
1602 dirs
.Add(GetFullName());
1606 size_t count
= dirs
.GetCount();
1607 for ( size_t i
= 0; i
< count
; i
++ )
1609 // We're using pathOut to collect the long-name path, but using a
1610 // temporary for appending the last path component which may be
1612 tmpPath
= pathOut
+ dirs
[i
];
1614 if ( tmpPath
.empty() )
1617 // can't see this being necessary? MF
1618 if ( tmpPath
.Last() == GetVolumeSeparator(wxPATH_DOS
) )
1620 // Can't pass a drive and root dir to FindFirstFile,
1621 // so continue to next dir
1622 tmpPath
+= wxFILE_SEP_PATH
;
1627 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
1628 if (hFind
== INVALID_HANDLE_VALUE
)
1630 // Error: most likely reason is that path doesn't exist, so
1631 // append any unprocessed parts and return
1632 for ( i
+= 1; i
< count
; i
++ )
1633 tmpPath
+= wxFILE_SEP_PATH
+ dirs
[i
];
1638 pathOut
+= findFileData
.cFileName
;
1639 if ( (i
< (count
-1)) )
1640 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 if ( !dtAccess
&& !dtMod
)
1900 // can't modify the creation time anyhow, don't try
1904 // if dtAccess or dtMod is not specified, use the other one (which must be
1905 // non NULL because of the test above) for both times
1907 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
1908 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
1909 if ( utime(GetFullPath().fn_str(), &utm
) == 0 )
1913 #else // other platform
1916 wxLogSysError(_("Failed to modify file times for '%s'"),
1917 GetFullPath().c_str());
1922 bool wxFileName::Touch()
1924 #if defined(__UNIX_LIKE__)
1925 // under Unix touching file is simple: just pass NULL to utime()
1926 if ( utime(GetFullPath().fn_str(), NULL
) == 0 )
1931 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1934 #else // other platform
1935 wxDateTime dtNow
= wxDateTime::Now();
1937 return SetTimes(&dtNow
, &dtNow
, NULL
/* don't change create time */);
1941 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
1943 wxDateTime
*dtCreate
) const
1945 #if defined(__WIN32__)
1946 // we must use different methods for the files and directories under
1947 // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and
1948 // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and
1951 FILETIME ftAccess
, ftCreate
, ftWrite
;
1954 // implemented in msw/dir.cpp
1955 extern bool wxGetDirectoryTimes(const wxString
& dirname
,
1956 FILETIME
*, FILETIME
*, FILETIME
*);
1958 // we should pass the path without the trailing separator to
1959 // wxGetDirectoryTimes()
1960 ok
= wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME
),
1961 &ftAccess
, &ftCreate
, &ftWrite
);
1965 wxFileHandle
fh(GetFullPath(), wxFileHandle::Read
);
1968 ok
= ::GetFileTime(fh
,
1969 dtCreate
? &ftCreate
: NULL
,
1970 dtAccess
? &ftAccess
: NULL
,
1971 dtMod
? &ftWrite
: NULL
) != 0;
1982 ConvertFileTimeToWx(dtCreate
, ftCreate
);
1984 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1986 ConvertFileTimeToWx(dtMod
, ftWrite
);
1990 #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
1992 if ( wxStat( GetFullPath().c_str(), &stBuf
) == 0 )
1995 dtAccess
->Set(stBuf
.st_atime
);
1997 dtMod
->Set(stBuf
.st_mtime
);
1999 dtCreate
->Set(stBuf
.st_ctime
);
2003 #else // other platform
2006 wxLogSysError(_("Failed to retrieve file times for '%s'"),
2007 GetFullPath().c_str());
2012 #endif // wxUSE_DATETIME
2016 const short kMacExtensionMaxLength
= 16 ;
2017 class MacDefaultExtensionRecord
2020 MacDefaultExtensionRecord()
2023 m_type
= m_creator
= 0 ;
2025 MacDefaultExtensionRecord( const MacDefaultExtensionRecord
& from
)
2027 wxStrcpy( m_ext
, from
.m_ext
) ;
2028 m_type
= from
.m_type
;
2029 m_creator
= from
.m_creator
;
2031 MacDefaultExtensionRecord( const wxChar
* extension
, OSType type
, OSType creator
)
2033 wxStrncpy( m_ext
, extension
, kMacExtensionMaxLength
) ;
2034 m_ext
[kMacExtensionMaxLength
] = 0 ;
2036 m_creator
= creator
;
2038 wxChar m_ext
[kMacExtensionMaxLength
] ;
2043 #include "wx/dynarray.h"
2044 WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord
, MacDefaultExtensionArray
) ;
2046 bool gMacDefaultExtensionsInited
= false ;
2048 #include "wx/arrimpl.cpp"
2050 WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray
) ;
2052 MacDefaultExtensionArray gMacDefaultExtensions
;
2054 // load the default extensions
2055 MacDefaultExtensionRecord gDefaults
[] =
2057 MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) ,
2058 MacDefaultExtensionRecord( wxT("tif") , 'TIFF' , '****' ) ,
2059 MacDefaultExtensionRecord( wxT("jpg") , 'JPEG' , '****' ) ,
2062 static void MacEnsureDefaultExtensionsLoaded()
2064 if ( !gMacDefaultExtensionsInited
)
2066 // we could load the pc exchange prefs here too
2067 for ( size_t i
= 0 ; i
< WXSIZEOF( gDefaults
) ; ++i
)
2069 gMacDefaultExtensions
.Add( gDefaults
[i
] ) ;
2071 gMacDefaultExtensionsInited
= true ;
2075 bool wxFileName::MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
)
2078 FSCatalogInfo catInfo
;
2081 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2083 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2085 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2086 finfo
->fileType
= type
;
2087 finfo
->fileCreator
= creator
;
2088 FSSetCatalogInfo( &fsRef
, kFSCatInfoFinderInfo
, &catInfo
) ;
2095 bool wxFileName::MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
)
2098 FSCatalogInfo catInfo
;
2101 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2103 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2105 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2106 *type
= finfo
->fileType
;
2107 *creator
= finfo
->fileCreator
;
2114 bool wxFileName::MacSetDefaultTypeAndCreator()
2116 wxUint32 type
, creator
;
2117 if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type
,
2120 return MacSetTypeAndCreator( type
, creator
) ;
2125 bool wxFileName::MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
)
2127 MacEnsureDefaultExtensionsLoaded() ;
2128 wxString extl
= ext
.Lower() ;
2129 for( int i
= gMacDefaultExtensions
.Count() - 1 ; i
>= 0 ; --i
)
2131 if ( gMacDefaultExtensions
.Item(i
).m_ext
== extl
)
2133 *type
= gMacDefaultExtensions
.Item(i
).m_type
;
2134 *creator
= gMacDefaultExtensions
.Item(i
).m_creator
;
2141 void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
)
2143 MacEnsureDefaultExtensionsLoaded() ;
2144 MacDefaultExtensionRecord rec
;
2146 rec
.m_creator
= creator
;
2147 wxStrncpy( rec
.m_ext
, ext
.Lower().c_str() , kMacExtensionMaxLength
) ;
2148 gMacDefaultExtensions
.Add( rec
) ;