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"
76 #include "wx/filename.h"
77 #include "wx/tokenzr.h"
78 #include "wx/config.h" // for wxExpandEnvVars
81 #include "wx/dynlib.h"
83 // For GetShort/LongPathName
85 #include "wx/msw/wrapwin.h"
86 #if defined(__MINGW32__)
87 #include "wx/msw/gccpriv.h"
92 #include "wx/msw/private.h"
95 #if defined(__WXMAC__)
96 #include "wx/mac/private.h" // includes mac headers
99 // utime() is POSIX so should normally be available on all Unices
101 #include <sys/types.h>
103 #include <sys/stat.h>
113 #include <sys/types.h>
115 #include <sys/stat.h>
126 #include <sys/utime.h>
127 #include <sys/stat.h>
138 #define MAX_PATH _MAX_PATH
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 // small helper class which opens and closes the file - we use it just to get
146 // a file handle for the given file name to pass it to some Win32 API function
147 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
158 wxFileHandle(const wxString
& filename
, OpenMode mode
)
160 m_hFile
= ::CreateFile
163 mode
== Read ? GENERIC_READ
// access mask
165 FILE_SHARE_READ
| // sharing mode
166 FILE_SHARE_WRITE
, // (allow everything)
167 NULL
, // no secutity attr
168 OPEN_EXISTING
, // creation disposition
170 NULL
// no template file
173 if ( m_hFile
== INVALID_HANDLE_VALUE
)
175 wxLogSysError(_("Failed to open '%s' for %s"),
177 mode
== Read ?
_("reading") : _("writing"));
183 if ( m_hFile
!= INVALID_HANDLE_VALUE
)
185 if ( !::CloseHandle(m_hFile
) )
187 wxLogSysError(_("Failed to close file handle"));
192 // return true only if the file could be opened successfully
193 bool IsOk() const { return m_hFile
!= INVALID_HANDLE_VALUE
; }
196 operator HANDLE() const { return m_hFile
; }
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 #if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__)
210 // convert between wxDateTime and FILETIME which is a 64-bit value representing
211 // the number of 100-nanosecond intervals since January 1, 1601.
213 static void ConvertFileTimeToWx(wxDateTime
*dt
, const FILETIME
&ft
)
215 FILETIME ftcopy
= ft
;
217 if ( !::FileTimeToLocalFileTime(&ftcopy
, &ftLocal
) )
219 wxLogLastError(_T("FileTimeToLocalFileTime"));
223 if ( !::FileTimeToSystemTime(&ftLocal
, &st
) )
225 wxLogLastError(_T("FileTimeToSystemTime"));
228 dt
->Set(st
.wDay
, wxDateTime
::Month(st
.wMonth
- 1), st
.wYear
,
229 st
.wHour
, st
.wMinute
, st
.wSecond
, st
.wMilliseconds
);
232 static void ConvertWxToFileTime(FILETIME
*ft
, const wxDateTime
& dt
)
235 st
.wDay
= dt
.GetDay();
236 st
.wMonth
= (WORD
)(dt
.GetMonth() + 1);
237 st
.wYear
= (WORD
)dt
.GetYear();
238 st
.wHour
= dt
.GetHour();
239 st
.wMinute
= dt
.GetMinute();
240 st
.wSecond
= dt
.GetSecond();
241 st
.wMilliseconds
= dt
.GetMillisecond();
244 if ( !::SystemTimeToFileTime(&st
, &ftLocal
) )
246 wxLogLastError(_T("SystemTimeToFileTime"));
249 if ( !::LocalFileTimeToFileTime(&ftLocal
, ft
) )
251 wxLogLastError(_T("LocalFileTimeToFileTime"));
255 #endif // wxUSE_DATETIME && __WIN32__
257 // return a string with the volume par
258 static wxString
wxGetVolumeString(const wxString
& volume
, wxPathFormat format
)
262 if ( !volume
.empty() )
264 format
= wxFileName
::GetFormat(format
);
266 // Special Windows UNC paths hack, part 2: undo what we did in
267 // SplitPath() and make an UNC path if we have a drive which is not a
268 // single letter (hopefully the network shares can't be one letter only
269 // although I didn't find any authoritative docs on this)
270 if ( format
== wxPATH_DOS
&& volume
.length() > 1 )
272 path
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_DOS
<< volume
;
274 else if ( format
== wxPATH_DOS
|| format
== wxPATH_VMS
)
276 path
<< volume
<< wxFileName
::GetVolumeSeparator(format
);
284 // ============================================================================
286 // ============================================================================
288 // ----------------------------------------------------------------------------
289 // wxFileName construction
290 // ----------------------------------------------------------------------------
292 void wxFileName
::Assign( const wxFileName
&filepath
)
294 m_volume
= filepath
.GetVolume();
295 m_dirs
= filepath
.GetDirs();
296 m_name
= filepath
.GetName();
297 m_ext
= filepath
.GetExt();
298 m_relative
= filepath
.m_relative
;
299 m_hasExt
= filepath
.m_hasExt
;
302 void wxFileName
::Assign(const wxString
& volume
,
303 const wxString
& path
,
304 const wxString
& name
,
307 wxPathFormat format
)
309 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
;
415 SplitPath(fullpath
, &volume
, &path
, &name
, &ext
, &hasExt
, format
);
417 Assign(volume
, path
, name
, ext
, hasExt
, format
);
420 void wxFileName
::Assign(const wxString
& fullpathOrig
,
421 const wxString
& fullname
,
424 // always recognize fullpath as directory, even if it doesn't end with a
426 wxString fullpath
= fullpathOrig
;
427 if ( !wxEndsWithPathSeparator(fullpath
) )
429 fullpath
+= GetPathSeparator(format
);
432 wxString volume
, path
, name
, ext
;
435 // do some consistency checks in debug mode: the name should be really just
436 // the filename and the path should be really just a path
438 wxString volDummy
, pathDummy
, nameDummy
, extDummy
;
440 SplitPath(fullname
, &volDummy
, &pathDummy
, &name
, &ext
, &hasExt
, format
);
442 wxASSERT_MSG( volDummy
.empty() && pathDummy
.empty(),
443 _T("the file name shouldn't contain the path") );
445 SplitPath(fullpath
, &volume
, &path
, &nameDummy
, &extDummy
, format
);
447 wxASSERT_MSG( nameDummy
.empty() && extDummy
.empty(),
448 _T("the path shouldn't contain file name nor extension") );
450 #else // !__WXDEBUG__
451 SplitPath(fullname
, NULL
/* no volume */, NULL
/* no path */,
452 &name
, &ext
, &hasExt
, format
);
453 SplitPath(fullpath
, &volume
, &path
, NULL
, NULL
, format
);
454 #endif // __WXDEBUG__/!__WXDEBUG__
456 Assign(volume
, path
, name
, ext
, hasExt
, format
);
459 void wxFileName
::Assign(const wxString
& pathOrig
,
460 const wxString
& name
,
466 SplitVolume(pathOrig
, &volume
, &path
, format
);
468 Assign(volume
, path
, name
, ext
, format
);
471 void wxFileName
::AssignDir(const wxString
& dir
, wxPathFormat format
)
473 Assign(dir
, wxEmptyString
, format
);
476 void wxFileName
::Clear()
482 m_ext
= wxEmptyString
;
484 // we don't have any absolute path for now
492 wxFileName wxFileName
::FileName(const wxString
& file
, wxPathFormat format
)
494 return wxFileName(file
, format
);
498 wxFileName wxFileName
::DirName(const wxString
& dir
, wxPathFormat format
)
501 fn
.AssignDir(dir
, format
);
505 // ----------------------------------------------------------------------------
507 // ----------------------------------------------------------------------------
509 bool wxFileName
::FileExists() const
511 return wxFileName
::FileExists( GetFullPath() );
514 bool wxFileName
::FileExists( const wxString
&file
)
516 return ::wxFileExists( file
);
519 bool wxFileName
::DirExists() const
521 return wxFileName
::DirExists( GetFullPath() );
524 bool wxFileName
::DirExists( const wxString
&dir
)
526 return ::wxDirExists( dir
);
529 // ----------------------------------------------------------------------------
530 // CWD and HOME stuff
531 // ----------------------------------------------------------------------------
533 void wxFileName
::AssignCwd(const wxString
& volume
)
535 AssignDir(wxFileName
::GetCwd(volume
));
539 wxString wxFileName
::GetCwd(const wxString
& volume
)
541 // if we have the volume, we must get the current directory on this drive
542 // and to do this we have to chdir to this volume - at least under Windows,
543 // I don't know how to get the current drive on another volume elsewhere
546 if ( !volume
.empty() )
549 SetCwd(volume
+ GetVolumeSeparator());
552 wxString cwd
= ::wxGetCwd();
554 if ( !volume
.empty() )
562 bool wxFileName
::SetCwd()
564 return wxFileName
::SetCwd( GetFullPath() );
567 bool wxFileName
::SetCwd( const wxString
&cwd
)
569 return ::wxSetWorkingDirectory( cwd
);
572 void wxFileName
::AssignHomeDir()
574 AssignDir(wxFileName
::GetHomeDir());
577 wxString wxFileName
::GetHomeDir()
579 return ::wxGetHomeDir();
584 void wxFileName
::AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
586 wxString tempname
= CreateTempFileName(prefix
, fileTemp
);
587 if ( tempname
.empty() )
589 // error, failed to get temp file name
600 wxFileName
::CreateTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
602 wxString path
, dir
, name
;
604 // use the directory specified by the prefix
605 SplitPath(prefix
, &dir
, &name
, NULL
/* extension */);
607 #if defined(__WXWINCE__)
610 // FIXME. Create \temp dir?
613 path
= dir
+ wxT("\\") + name
;
615 while (FileExists(path
))
617 path
= dir
+ wxT("\\") + name
;
622 #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
626 if ( !::GetTempPath(MAX_PATH
, wxStringBuffer(dir
, MAX_PATH
+ 1)) )
628 wxLogLastError(_T("GetTempPath"));
633 // GetTempFileName() fails if we pass it an empty string
637 else // we have a dir to create the file in
639 // ensure we use only the back slashes as GetTempFileName(), unlike all
640 // the other APIs, is picky and doesn't accept the forward ones
641 dir
.Replace(_T("/"), _T("\\"));
644 if ( !::GetTempFileName(dir
, name
, 0, wxStringBuffer(path
, MAX_PATH
+ 1)) )
646 wxLogLastError(_T("GetTempFileName"));
654 #if defined(__WXMAC__) && !defined(__DARWIN__)
655 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
657 dir
= wxGetenv(_T("TMP"));
660 dir
= wxGetenv(_T("TEMP"));
666 #if defined(__DOS__) || defined(__OS2__)
668 #elif defined(__WXMAC__)
669 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
679 if ( !wxEndsWithPathSeparator(dir
) &&
680 (name
.empty() || !wxIsPathSeparator(name
[0u])) )
682 path
+= wxFILE_SEP_PATH
;
687 #if defined(HAVE_MKSTEMP)
688 // scratch space for mkstemp()
689 path
+= _T("XXXXXX");
691 // we need to copy the path to the buffer in which mkstemp() can modify it
692 wxCharBuffer
buf( wxConvFile
.cWX2MB( path
) );
694 // cast is safe because the string length doesn't change
695 int fdTemp
= mkstemp( (char*)(const char*) buf
);
698 // this might be not necessary as mkstemp() on most systems should have
699 // already done it but it doesn't hurt neither...
702 else // mkstemp() succeeded
704 path
= wxConvFile
.cMB2WX( (const char*) buf
);
706 // avoid leaking the fd
709 fileTemp
->Attach(fdTemp
);
716 #else // !HAVE_MKSTEMP
720 path
+= _T("XXXXXX");
722 wxCharBuffer buf
= wxConvFile
.cWX2MB( path
);
723 if ( !mktemp( (const char*) buf
) )
729 path
= wxConvFile
.cMB2WX( (const char*) buf
);
731 #else // !HAVE_MKTEMP (includes __DOS__)
732 // generate the unique file name ourselves
733 #if !defined(__DOS__) && !defined(__PALMOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
734 path
<< (unsigned int)getpid();
739 static const size_t numTries
= 1000;
740 for ( size_t n
= 0; n
< numTries
; n
++ )
742 // 3 hex digits is enough for numTries == 1000 < 4096
743 pathTry
= path
+ wxString
::Format(_T("%.03x"), (unsigned int) n
);
744 if ( !FileExists(pathTry
) )
753 #endif // HAVE_MKTEMP/!HAVE_MKTEMP
758 #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
760 #endif // Windows/!Windows
764 wxLogSysError(_("Failed to create a temporary file name"));
766 else if ( fileTemp
&& !fileTemp
->IsOpened() )
768 // open the file - of course, there is a race condition here, this is
769 // why we always prefer using mkstemp()...
771 // NB: GetTempFileName() under Windows creates the file, so using
772 // write_excl there would fail
773 if ( !fileTemp
->Open(path
,
774 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
779 wxS_IRUSR
| wxS_IWUSR
) )
781 // FIXME: If !ok here should we loop and try again with another
782 // file name? That is the standard recourse if open(O_EXCL)
783 // fails, though of course it should be protected against
784 // possible infinite looping too.
786 wxLogError(_("Failed to open temporary file."));
797 // ----------------------------------------------------------------------------
798 // directory operations
799 // ----------------------------------------------------------------------------
801 bool wxFileName
::Mkdir( int perm
, int flags
)
803 return wxFileName
::Mkdir( GetFullPath(), perm
, flags
);
806 bool wxFileName
::Mkdir( const wxString
& dir
, int perm
, int flags
)
808 if ( flags
& wxPATH_MKDIR_FULL
)
810 // split the path in components
812 filename
.AssignDir(dir
);
815 if ( filename
.HasVolume())
817 currPath
<< wxGetVolumeString(filename
.GetVolume(), wxPATH_NATIVE
);
820 wxArrayString dirs
= filename
.GetDirs();
821 size_t count
= dirs
.GetCount();
822 for ( size_t i
= 0; i
< count
; i
++ )
825 #if defined(__WXMAC__) && !defined(__DARWIN__)
826 // relative pathnames are exactely the other way round under mac...
827 !filename
.IsAbsolute()
829 filename
.IsAbsolute()
832 currPath
+= wxFILE_SEP_PATH
;
835 if (!DirExists(currPath
))
837 if (!wxMkdir(currPath
, perm
))
839 // no need to try creating further directories
849 return ::wxMkdir( dir
, perm
);
852 bool wxFileName
::Rmdir()
854 return wxFileName
::Rmdir( GetFullPath() );
857 bool wxFileName
::Rmdir( const wxString
&dir
)
859 return ::wxRmdir( dir
);
862 // ----------------------------------------------------------------------------
863 // path normalization
864 // ----------------------------------------------------------------------------
866 bool wxFileName
::Normalize(int flags
,
870 // deal with env vars renaming first as this may seriously change the path
871 if ( flags
& wxPATH_NORM_ENV_VARS
)
873 wxString pathOrig
= GetFullPath(format
);
874 wxString path
= wxExpandEnvVars(pathOrig
);
875 if ( path
!= pathOrig
)
882 // the existing path components
883 wxArrayString dirs
= GetDirs();
885 // the path to prepend in front to make the path absolute
888 format
= GetFormat(format
);
890 // make the path absolute
891 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute(format
) )
895 curDir
.AssignCwd(GetVolume());
899 curDir
.AssignDir(cwd
);
902 // the path may be not absolute because it doesn't have the volume name
903 // but in this case we shouldn't modify the directory components of it
904 // but just set the current volume
905 if ( !HasVolume() && curDir
.HasVolume() )
907 SetVolume(curDir
.GetVolume());
911 // yes, it was the case - we don't need curDir then
917 // handle ~ stuff under Unix only
918 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
920 if ( !dirs
.IsEmpty() )
922 wxString dir
= dirs
[0u];
923 if ( !dir
.empty() && dir
[0u] == _T('~') )
925 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
932 // transform relative path into abs one
935 wxArrayString dirsNew
= curDir
.GetDirs();
936 size_t count
= dirs
.GetCount();
937 for ( size_t n
= 0; n
< count
; n
++ )
939 dirsNew
.Add(dirs
[n
]);
945 // now deal with ".", ".." and the rest
947 size_t count
= dirs
.GetCount();
948 for ( size_t n
= 0; n
< count
; n
++ )
950 wxString dir
= dirs
[n
];
952 if ( flags
& wxPATH_NORM_DOTS
)
954 if ( dir
== wxT(".") )
960 if ( dir
== wxT("..") )
962 if ( m_dirs
.IsEmpty() )
964 wxLogError(_("The path '%s' contains too many \"..\"!"),
965 GetFullPath().c_str());
969 m_dirs
.RemoveAt(m_dirs
.GetCount() - 1);
974 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
982 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
983 if ( (flags
& wxPATH_NORM_SHORTCUT
) )
986 if (GetShortcutTarget(GetFullPath(format
), filename
))
988 // Repeat this since we may now have a new path
989 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
991 filename
.MakeLower();
999 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
1001 // VZ: expand env vars here too?
1003 m_volume
.MakeLower();
1008 // we do have the path now
1010 // NB: need to do this before (maybe) calling Assign() below
1013 #if defined(__WIN32__)
1014 if ( (flags
& wxPATH_NORM_LONG
) && (format
== wxPATH_DOS
) )
1016 Assign(GetLongPath());
1023 // ----------------------------------------------------------------------------
1024 // get the shortcut target
1025 // ----------------------------------------------------------------------------
1027 // WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions.
1028 // The .lnk file is a plain text file so it should be easy to
1029 // make it work. Hint from Google Groups:
1030 // "If you open up a lnk file, you'll see a
1031 // number, followed by a pound sign (#), followed by more text. The
1032 // number is the number of characters that follows the pound sign. The
1033 // characters after the pound sign are the command line (which _can_
1034 // include arguments) to be executed. Any path (e.g. \windows\program
1035 // files\myapp.exe) that includes spaces needs to be enclosed in
1036 // quotation marks."
1038 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
1039 // The following lines are necessary under WinCE
1040 // #include "wx/msw/private.h"
1041 // #include <ole2.h>
1043 #if defined(__WXWINCE__)
1044 #include <shlguid.h>
1047 bool wxFileName
::GetShortcutTarget(const wxString
& shortcutPath
, wxString
& targetFilename
, wxString
* arguments
)
1049 wxString path
, file
, ext
;
1050 wxSplitPath(shortcutPath
, & path
, & file
, & ext
);
1054 bool success
= false;
1056 // Assume it's not a shortcut if it doesn't end with lnk
1057 if (ext
.CmpNoCase(wxT("lnk"))!=0)
1060 // create a ShellLink object
1061 hres
= CoCreateInstance(CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
,
1062 IID_IShellLink
, (LPVOID
*) &psl
);
1064 if (SUCCEEDED(hres
))
1067 hres
= psl
->QueryInterface( IID_IPersistFile
, (LPVOID
*) &ppf
);
1068 if (SUCCEEDED(hres
))
1070 WCHAR wsz
[MAX_PATH
];
1072 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, shortcutPath
.mb_str(), -1, wsz
,
1075 hres
= ppf
->Load(wsz
, 0);
1076 if (SUCCEEDED(hres
))
1079 // Wrong prototype in early versions
1080 #if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2)
1081 psl
->GetPath((CHAR
*) buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1083 psl
->GetPath(buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1085 targetFilename
= wxString(buf
);
1086 success
= (shortcutPath
!= targetFilename
);
1088 psl
->GetArguments(buf
, 2048);
1090 if (!args
.empty() && arguments
)
1103 // ----------------------------------------------------------------------------
1104 // absolute/relative paths
1105 // ----------------------------------------------------------------------------
1107 bool wxFileName
::IsAbsolute(wxPathFormat format
) const
1109 // if our path doesn't start with a path separator, it's not an absolute
1114 if ( !GetVolumeSeparator(format
).empty() )
1116 // this format has volumes and an absolute path must have one, it's not
1117 // enough to have the full path to bean absolute file under Windows
1118 if ( GetVolume().empty() )
1125 bool wxFileName
::MakeRelativeTo(const wxString
& pathBase
, wxPathFormat format
)
1127 wxFileName fnBase
= wxFileName
::DirName(pathBase
, format
);
1129 // get cwd only once - small time saving
1130 wxString cwd
= wxGetCwd();
1131 Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1132 fnBase
.Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1134 bool withCase
= IsCaseSensitive(format
);
1136 // we can't do anything if the files live on different volumes
1137 if ( !GetVolume().IsSameAs(fnBase
.GetVolume(), withCase
) )
1143 // same drive, so we don't need our volume
1146 // remove common directories starting at the top
1147 while ( !m_dirs
.IsEmpty() && !fnBase
.m_dirs
.IsEmpty() &&
1148 m_dirs
[0u].IsSameAs(fnBase
.m_dirs
[0u], withCase
) )
1151 fnBase
.m_dirs
.RemoveAt(0);
1154 // add as many ".." as needed
1155 size_t count
= fnBase
.m_dirs
.GetCount();
1156 for ( size_t i
= 0; i
< count
; i
++ )
1158 m_dirs
.Insert(wxT(".."), 0u);
1161 if ( format
== wxPATH_UNIX
|| format
== wxPATH_DOS
)
1163 // a directory made relative with respect to itself is '.' under Unix
1164 // and DOS, by definition (but we don't have to insert "./" for the
1166 if ( m_dirs
.IsEmpty() && IsDir() )
1168 m_dirs
.Add(_T('.'));
1178 // ----------------------------------------------------------------------------
1179 // filename kind tests
1180 // ----------------------------------------------------------------------------
1182 bool wxFileName
::SameAs(const wxFileName
& filepath
, wxPathFormat format
) const
1184 wxFileName fn1
= *this,
1187 // get cwd only once - small time saving
1188 wxString cwd
= wxGetCwd();
1189 fn1
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1190 fn2
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1192 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
1195 // TODO: compare inodes for Unix, this works even when filenames are
1196 // different but files are the same (symlinks) (VZ)
1202 bool wxFileName
::IsCaseSensitive( wxPathFormat format
)
1204 // only Unix filenames are truely case-sensitive
1205 return GetFormat(format
) == wxPATH_UNIX
;
1209 wxString wxFileName
::GetForbiddenChars(wxPathFormat format
)
1211 // Inits to forbidden characters that are common to (almost) all platforms.
1212 wxString strForbiddenChars
= wxT("*?");
1214 // If asserts, wxPathFormat has been changed. In case of a new path format
1215 // addition, the following code might have to be updated.
1216 wxCOMPILE_TIME_ASSERT(wxPATH_MAX
== 5, wxPathFormatChanged
);
1217 switch ( GetFormat(format
) )
1220 wxFAIL_MSG( wxT("Unknown path format") );
1221 // !! Fall through !!
1227 // On a Mac even names with * and ? are allowed (Tested with OS
1228 // 9.2.1 and OS X 10.2.5)
1229 strForbiddenChars
= wxEmptyString
;
1233 strForbiddenChars
+= wxT("\\/:\"<>|");
1240 return strForbiddenChars
;
1244 wxString wxFileName
::GetVolumeSeparator(wxPathFormat
WXUNUSED_IN_WINCE(format
))
1247 return wxEmptyString
;
1251 if ( (GetFormat(format
) == wxPATH_DOS
) ||
1252 (GetFormat(format
) == wxPATH_VMS
) )
1254 sepVol
= wxFILE_SEP_DSK
;
1263 wxString wxFileName
::GetPathSeparators(wxPathFormat format
)
1266 switch ( GetFormat(format
) )
1269 // accept both as native APIs do but put the native one first as
1270 // this is the one we use in GetFullPath()
1271 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
1275 wxFAIL_MSG( _T("Unknown wxPATH_XXX style") );
1279 seps
= wxFILE_SEP_PATH_UNIX
;
1283 seps
= wxFILE_SEP_PATH_MAC
;
1287 seps
= wxFILE_SEP_PATH_VMS
;
1295 wxString wxFileName
::GetPathTerminators(wxPathFormat format
)
1297 format
= GetFormat(format
);
1299 // under VMS the end of the path is ']', not the path separator used to
1300 // separate the components
1301 return format
== wxPATH_VMS ?
wxString(_T(']')) : GetPathSeparators(format
);
1305 bool wxFileName
::IsPathSeparator(wxChar ch
, wxPathFormat format
)
1307 // wxString::Find() doesn't work as expected with NUL - it will always find
1308 // it, so test for it separately
1309 return ch
!= _T('\0') && GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
1312 // ----------------------------------------------------------------------------
1313 // path components manipulation
1314 // ----------------------------------------------------------------------------
1316 /* static */ bool wxFileName
::IsValidDirComponent(const wxString
& dir
)
1320 wxFAIL_MSG( _T("empty directory passed to wxFileName::InsertDir()") );
1325 const size_t len
= dir
.length();
1326 for ( size_t n
= 0; n
< len
; n
++ )
1328 if ( dir
[n
] == GetVolumeSeparator() || IsPathSeparator(dir
[n
]) )
1330 wxFAIL_MSG( _T("invalid directory component in wxFileName") );
1339 void wxFileName
::AppendDir( const wxString
& dir
)
1341 if ( IsValidDirComponent(dir
) )
1345 void wxFileName
::PrependDir( const wxString
& dir
)
1350 void wxFileName
::InsertDir(size_t before
, const wxString
& dir
)
1352 if ( IsValidDirComponent(dir
) )
1353 m_dirs
.Insert(dir
, before
);
1356 void wxFileName
::RemoveDir(size_t pos
)
1358 m_dirs
.RemoveAt(pos
);
1361 // ----------------------------------------------------------------------------
1363 // ----------------------------------------------------------------------------
1365 void wxFileName
::SetFullName(const wxString
& fullname
)
1367 SplitPath(fullname
, NULL
/* no volume */, NULL
/* no path */,
1368 &m_name
, &m_ext
, &m_hasExt
);
1371 wxString wxFileName
::GetFullName() const
1373 wxString fullname
= m_name
;
1376 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
1382 wxString wxFileName
::GetPath( int flags
, wxPathFormat format
) const
1384 format
= GetFormat( format
);
1388 // return the volume with the path as well if requested
1389 if ( flags
& wxPATH_GET_VOLUME
)
1391 fullpath
+= wxGetVolumeString(GetVolume(), format
);
1394 // the leading character
1399 fullpath
+= wxFILE_SEP_PATH_MAC
;
1404 fullpath
+= wxFILE_SEP_PATH_DOS
;
1408 wxFAIL_MSG( wxT("Unknown path format") );
1414 // normally the absolute file names start with a slash
1415 // with one exception: the ones like "~/foo.bar" don't
1417 if ( m_dirs
.IsEmpty() || m_dirs
[0u] != _T('~') )
1419 fullpath
+= wxFILE_SEP_PATH_UNIX
;
1425 // no leading character here but use this place to unset
1426 // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense
1427 // as, if I understand correctly, there should never be a dot
1428 // before the closing bracket
1429 flags
&= ~wxPATH_GET_SEPARATOR
;
1432 if ( m_dirs
.empty() )
1434 // there is nothing more
1438 // then concatenate all the path components using the path separator
1439 if ( format
== wxPATH_VMS
)
1441 fullpath
+= wxT('[');
1444 const size_t dirCount
= m_dirs
.GetCount();
1445 for ( size_t i
= 0; i
< dirCount
; i
++ )
1450 if ( m_dirs
[i
] == wxT(".") )
1452 // skip appending ':', this shouldn't be done in this
1453 // case as "::" is interpreted as ".." under Unix
1457 // convert back from ".." to nothing
1458 if ( !m_dirs
[i
].IsSameAs(wxT("..")) )
1459 fullpath
+= m_dirs
[i
];
1463 wxFAIL_MSG( wxT("Unexpected path format") );
1464 // still fall through
1468 fullpath
+= m_dirs
[i
];
1472 // TODO: What to do with ".." under VMS
1474 // convert back from ".." to nothing
1475 if ( !m_dirs
[i
].IsSameAs(wxT("..")) )
1476 fullpath
+= m_dirs
[i
];
1480 if ( (flags
& wxPATH_GET_SEPARATOR
) || (i
!= dirCount
- 1) )
1481 fullpath
+= GetPathSeparator(format
);
1484 if ( format
== wxPATH_VMS
)
1486 fullpath
+= wxT(']');
1492 wxString wxFileName
::GetFullPath( wxPathFormat format
) const
1494 // we already have a function to get the path
1495 wxString fullpath
= GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
,
1498 // now just add the file name and extension to it
1499 fullpath
+= GetFullName();
1504 // Return the short form of the path (returns identity on non-Windows platforms)
1505 wxString wxFileName
::GetShortPath() const
1507 wxString
path(GetFullPath());
1509 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
1510 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
1514 if ( ::GetShortPathName
1517 wxStringBuffer(pathOut
, sz
),
1529 // Return the long form of the path (returns identity on non-Windows platforms)
1530 wxString wxFileName
::GetLongPath() const
1533 path
= GetFullPath();
1535 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1537 #if wxUSE_DYNAMIC_LOADER
1538 typedef DWORD (WINAPI
*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
1540 // this is MT-safe as in the worst case we're going to resolve the function
1541 // twice -- but as the result is the same in both threads, it's ok
1542 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
1543 if ( !s_pfnGetLongPathName
)
1545 static bool s_triedToLoad
= false;
1547 if ( !s_triedToLoad
)
1549 s_triedToLoad
= true;
1551 wxDynamicLibrary
dllKernel(_T("kernel32"));
1553 const wxChar
* GetLongPathName
= _T("GetLongPathName")
1558 #endif // Unicode/ANSI
1560 if ( dllKernel
.HasSymbol(GetLongPathName
) )
1562 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
)
1563 dllKernel
.GetSymbol(GetLongPathName
);
1566 // note that kernel32.dll can be unloaded, it stays in memory
1567 // anyhow as all Win32 programs link to it and so it's safe to call
1568 // GetLongPathName() even after unloading it
1572 if ( s_pfnGetLongPathName
)
1574 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1577 if ( (*s_pfnGetLongPathName
)
1580 wxStringBuffer(pathOut
, dwSize
),
1588 #endif // wxUSE_DYNAMIC_LOADER
1590 // The OS didn't support GetLongPathName, or some other error.
1591 // We need to call FindFirstFile on each component in turn.
1593 WIN32_FIND_DATA findFileData
;
1597 pathOut
= GetVolume() +
1598 GetVolumeSeparator(wxPATH_DOS
) +
1599 GetPathSeparator(wxPATH_DOS
);
1601 pathOut
= wxEmptyString
;
1603 wxArrayString dirs
= GetDirs();
1604 dirs
.Add(GetFullName());
1608 size_t count
= dirs
.GetCount();
1609 for ( size_t i
= 0; i
< count
; i
++ )
1611 // We're using pathOut to collect the long-name path, but using a
1612 // temporary for appending the last path component which may be
1614 tmpPath
= pathOut
+ dirs
[i
];
1616 if ( tmpPath
.empty() )
1619 // can't see this being necessary? MF
1620 if ( tmpPath
.Last() == GetVolumeSeparator(wxPATH_DOS
) )
1622 // Can't pass a drive and root dir to FindFirstFile,
1623 // so continue to next dir
1624 tmpPath
+= wxFILE_SEP_PATH
;
1629 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
1630 if (hFind
== INVALID_HANDLE_VALUE
)
1632 // Error: most likely reason is that path doesn't exist, so
1633 // append any unprocessed parts and return
1634 for ( i
+= 1; i
< count
; i
++ )
1635 tmpPath
+= wxFILE_SEP_PATH
+ dirs
[i
];
1640 pathOut
+= findFileData
.cFileName
;
1641 if ( (i
< (count
-1)) )
1642 pathOut
+= wxFILE_SEP_PATH
;
1648 #endif // Win32/!Win32
1653 wxPathFormat wxFileName
::GetFormat( wxPathFormat format
)
1655 if (format
== wxPATH_NATIVE
)
1657 #if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__)
1658 format
= wxPATH_DOS
;
1659 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1660 format
= wxPATH_MAC
;
1661 #elif defined(__VMS)
1662 format
= wxPATH_VMS
;
1664 format
= wxPATH_UNIX
;
1670 // ----------------------------------------------------------------------------
1671 // path splitting function
1672 // ----------------------------------------------------------------------------
1676 wxFileName
::SplitVolume(const wxString
& fullpathWithVolume
,
1677 wxString
*pstrVolume
,
1679 wxPathFormat format
)
1681 format
= GetFormat(format
);
1683 wxString fullpath
= fullpathWithVolume
;
1685 // special Windows UNC paths hack: transform \\share\path into share:path
1686 if ( format
== wxPATH_DOS
)
1688 if ( fullpath
.length() >= 4 &&
1689 fullpath
[0u] == wxFILE_SEP_PATH_DOS
&&
1690 fullpath
[1u] == wxFILE_SEP_PATH_DOS
)
1692 fullpath
.erase(0, 2);
1694 size_t posFirstSlash
=
1695 fullpath
.find_first_of(GetPathTerminators(format
));
1696 if ( posFirstSlash
!= wxString
::npos
)
1698 fullpath
[posFirstSlash
] = wxFILE_SEP_DSK
;
1700 // UNC paths are always absolute, right? (FIXME)
1701 fullpath
.insert(posFirstSlash
+ 1, 1, wxFILE_SEP_PATH_DOS
);
1706 // We separate the volume here
1707 if ( format
== wxPATH_DOS
|| format
== wxPATH_VMS
)
1709 wxString sepVol
= GetVolumeSeparator(format
);
1711 size_t posFirstColon
= fullpath
.find_first_of(sepVol
);
1712 if ( posFirstColon
!= wxString
::npos
)
1716 *pstrVolume
= fullpath
.Left(posFirstColon
);
1719 // remove the volume name and the separator from the full path
1720 fullpath
.erase(0, posFirstColon
+ sepVol
.length());
1725 *pstrPath
= fullpath
;
1729 void wxFileName
::SplitPath(const wxString
& fullpathWithVolume
,
1730 wxString
*pstrVolume
,
1735 wxPathFormat format
)
1737 format
= GetFormat(format
);
1740 SplitVolume(fullpathWithVolume
, pstrVolume
, &fullpath
, format
);
1742 // find the positions of the last dot and last path separator in the path
1743 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
1744 size_t posLastSlash
= fullpath
.find_last_of(GetPathTerminators(format
));
1746 // check whether this dot occurs at the very beginning of a path component
1747 if ( (posLastDot
!= wxString
::npos
) &&
1749 IsPathSeparator(fullpath
[posLastDot
- 1]) ||
1750 (format
== wxPATH_VMS
&& fullpath
[posLastDot
- 1] == _T(']'))) )
1752 // dot may be (and commonly -- at least under Unix -- is) the first
1753 // character of the filename, don't treat the entire filename as
1754 // extension in this case
1755 posLastDot
= wxString
::npos
;
1758 // if we do have a dot and a slash, check that the dot is in the name part
1759 if ( (posLastDot
!= wxString
::npos
) &&
1760 (posLastSlash
!= wxString
::npos
) &&
1761 (posLastDot
< posLastSlash
) )
1763 // the dot is part of the path, not the start of the extension
1764 posLastDot
= wxString
::npos
;
1767 // now fill in the variables provided by user
1770 if ( posLastSlash
== wxString
::npos
)
1777 // take everything up to the path separator but take care to make
1778 // the path equal to something like '/', not empty, for the files
1779 // immediately under root directory
1780 size_t len
= posLastSlash
;
1782 // this rule does not apply to mac since we do not start with colons (sep)
1783 // except for relative paths
1784 if ( !len
&& format
!= wxPATH_MAC
)
1787 *pstrPath
= fullpath
.Left(len
);
1789 // special VMS hack: remove the initial bracket
1790 if ( format
== wxPATH_VMS
)
1792 if ( (*pstrPath
)[0u] == _T('[') )
1793 pstrPath
->erase(0, 1);
1800 // take all characters starting from the one after the last slash and
1801 // up to, but excluding, the last dot
1802 size_t nStart
= posLastSlash
== wxString
::npos ?
0 : posLastSlash
+ 1;
1804 if ( posLastDot
== wxString
::npos
)
1806 // take all until the end
1807 count
= wxString
::npos
;
1809 else if ( posLastSlash
== wxString
::npos
)
1813 else // have both dot and slash
1815 count
= posLastDot
- posLastSlash
- 1;
1818 *pstrName
= fullpath
.Mid(nStart
, count
);
1821 // finally deal with the extension here: we have an added complication that
1822 // extension may be empty (but present) as in "foo." where trailing dot
1823 // indicates the empty extension at the end -- and hence we must remember
1824 // that we have it independently of pstrExt
1825 if ( posLastDot
== wxString
::npos
)
1835 // take everything after the dot
1837 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
1844 void wxFileName
::SplitPath(const wxString
& fullpath
,
1848 wxPathFormat format
)
1851 SplitPath(fullpath
, &volume
, path
, name
, ext
, format
);
1855 path
->Prepend(wxGetVolumeString(volume
, format
));
1859 // ----------------------------------------------------------------------------
1861 // ----------------------------------------------------------------------------
1865 bool wxFileName
::SetTimes(const wxDateTime
*dtAccess
,
1866 const wxDateTime
*dtMod
,
1867 const wxDateTime
*dtCreate
)
1869 #if defined(__WIN32__)
1872 // VZ: please let me know how to do this if you can
1873 wxFAIL_MSG( _T("SetTimes() not implemented for the directories") );
1877 wxFileHandle
fh(GetFullPath(), wxFileHandle
::Write
);
1880 FILETIME ftAccess
, ftCreate
, ftWrite
;
1883 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
1885 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
1887 ConvertWxToFileTime(&ftWrite
, *dtMod
);
1889 if ( ::SetFileTime(fh
,
1890 dtCreate ?
&ftCreate
: NULL
,
1891 dtAccess ?
&ftAccess
: NULL
,
1892 dtMod ?
&ftWrite
: NULL
) )
1898 #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
1899 wxUnusedVar(dtCreate
);
1901 if ( !dtAccess
&& !dtMod
)
1903 // can't modify the creation time anyhow, don't try
1907 // if dtAccess or dtMod is not specified, use the other one (which must be
1908 // non NULL because of the test above) for both times
1910 utm
.actime
= dtAccess ? dtAccess
->GetTicks() : dtMod
->GetTicks();
1911 utm
.modtime
= dtMod ? dtMod
->GetTicks() : dtAccess
->GetTicks();
1912 if ( utime(GetFullPath().fn_str(), &utm
) == 0 )
1916 #else // other platform
1917 wxUnusedVar(dtAccess
);
1919 wxUnusedVar(dtCreate
);
1922 wxLogSysError(_("Failed to modify file times for '%s'"),
1923 GetFullPath().c_str());
1928 bool wxFileName
::Touch()
1930 #if defined(__UNIX_LIKE__)
1931 // under Unix touching file is simple: just pass NULL to utime()
1932 if ( utime(GetFullPath().fn_str(), NULL
) == 0 )
1937 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1940 #else // other platform
1941 wxDateTime dtNow
= wxDateTime
::Now();
1943 return SetTimes(&dtNow
, &dtNow
, NULL
/* don't change create time */);
1947 bool wxFileName
::GetTimes(wxDateTime
*dtAccess
,
1949 wxDateTime
*dtCreate
) const
1951 #if defined(__WIN32__)
1952 // we must use different methods for the files and directories under
1953 // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and
1954 // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and
1957 FILETIME ftAccess
, ftCreate
, ftWrite
;
1960 // implemented in msw/dir.cpp
1961 extern bool wxGetDirectoryTimes(const wxString
& dirname
,
1962 FILETIME
*, FILETIME
*, FILETIME
*);
1964 // we should pass the path without the trailing separator to
1965 // wxGetDirectoryTimes()
1966 ok
= wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME
),
1967 &ftAccess
, &ftCreate
, &ftWrite
);
1971 wxFileHandle
fh(GetFullPath(), wxFileHandle
::Read
);
1974 ok
= ::GetFileTime(fh
,
1975 dtCreate ?
&ftCreate
: NULL
,
1976 dtAccess ?
&ftAccess
: NULL
,
1977 dtMod ?
&ftWrite
: NULL
) != 0;
1988 ConvertFileTimeToWx(dtCreate
, ftCreate
);
1990 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1992 ConvertFileTimeToWx(dtMod
, ftWrite
);
1996 #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__))
1998 if ( wxStat( GetFullPath().c_str(), &stBuf
) == 0 )
2001 dtAccess
->Set(stBuf
.st_atime
);
2003 dtMod
->Set(stBuf
.st_mtime
);
2005 dtCreate
->Set(stBuf
.st_ctime
);
2009 #else // other platform
2010 wxUnusedVar(dtAccess
);
2012 wxUnusedVar(dtCreate
);
2015 wxLogSysError(_("Failed to retrieve file times for '%s'"),
2016 GetFullPath().c_str());
2021 #endif // wxUSE_DATETIME
2025 const short kMacExtensionMaxLength
= 16 ;
2026 class MacDefaultExtensionRecord
2029 MacDefaultExtensionRecord()
2032 m_type
= m_creator
= 0 ;
2034 MacDefaultExtensionRecord( const MacDefaultExtensionRecord
& from
)
2036 wxStrcpy( m_ext
, from
.m_ext
) ;
2037 m_type
= from
.m_type
;
2038 m_creator
= from
.m_creator
;
2040 MacDefaultExtensionRecord( const wxChar
* extension
, OSType type
, OSType creator
)
2042 wxStrncpy( m_ext
, extension
, kMacExtensionMaxLength
) ;
2043 m_ext
[kMacExtensionMaxLength
] = 0 ;
2045 m_creator
= creator
;
2047 wxChar m_ext
[kMacExtensionMaxLength
] ;
2052 #include "wx/dynarray.h"
2053 WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord
, MacDefaultExtensionArray
) ;
2055 bool gMacDefaultExtensionsInited
= false ;
2057 #include "wx/arrimpl.cpp"
2059 WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray
) ;
2061 MacDefaultExtensionArray gMacDefaultExtensions
;
2063 // load the default extensions
2064 MacDefaultExtensionRecord gDefaults
[] =
2066 MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) ,
2067 MacDefaultExtensionRecord( wxT("tif") , 'TIFF' , '****' ) ,
2068 MacDefaultExtensionRecord( wxT("jpg") , 'JPEG' , '****' ) ,
2071 static void MacEnsureDefaultExtensionsLoaded()
2073 if ( !gMacDefaultExtensionsInited
)
2075 // we could load the pc exchange prefs here too
2076 for ( size_t i
= 0 ; i
< WXSIZEOF( gDefaults
) ; ++i
)
2078 gMacDefaultExtensions
.Add( gDefaults
[i
] ) ;
2080 gMacDefaultExtensionsInited
= true ;
2084 bool wxFileName
::MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
)
2087 FSCatalogInfo catInfo
;
2090 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2092 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2094 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2095 finfo
->fileType
= type
;
2096 finfo
->fileCreator
= creator
;
2097 FSSetCatalogInfo( &fsRef
, kFSCatInfoFinderInfo
, &catInfo
) ;
2104 bool wxFileName
::MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
)
2107 FSCatalogInfo catInfo
;
2110 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2112 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2114 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2115 *type
= finfo
->fileType
;
2116 *creator
= finfo
->fileCreator
;
2123 bool wxFileName
::MacSetDefaultTypeAndCreator()
2125 wxUint32 type
, creator
;
2126 if ( wxFileName
::MacFindDefaultTypeAndCreator(GetExt() , &type
,
2129 return MacSetTypeAndCreator( type
, creator
) ;
2134 bool wxFileName
::MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
)
2136 MacEnsureDefaultExtensionsLoaded() ;
2137 wxString extl
= ext
.Lower() ;
2138 for( int i
= gMacDefaultExtensions
.Count() - 1 ; i
>= 0 ; --i
)
2140 if ( gMacDefaultExtensions
.Item(i
).m_ext
== extl
)
2142 *type
= gMacDefaultExtensions
.Item(i
).m_type
;
2143 *creator
= gMacDefaultExtensions
.Item(i
).m_creator
;
2150 void wxFileName
::MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
)
2152 MacEnsureDefaultExtensionsLoaded() ;
2153 MacDefaultExtensionRecord rec
;
2155 rec
.m_creator
= creator
;
2156 wxStrncpy( rec
.m_ext
, ext
.Lower().c_str() , kMacExtensionMaxLength
) ;
2157 gMacDefaultExtensions
.Add( rec
) ;