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 specyfication
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"
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
= dt
.GetMonth() + 1;
238 st
.wYear
= 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
;
302 void wxFileName::Assign(const wxString
& volume
,
303 const wxString
& path
,
304 const wxString
& name
,
306 wxPathFormat format
)
308 SetPath( path
, format
);
315 void wxFileName::SetPath( const wxString
&path
, wxPathFormat format
)
321 wxPathFormat my_format
= GetFormat( format
);
322 wxString my_path
= path
;
324 // 1) Determine if the path is relative or absolute.
325 wxChar leadingChar
= my_path
[0u];
330 m_relative
= leadingChar
== wxT(':');
332 // We then remove a leading ":". The reason is in our
333 // storage form for relative paths:
334 // ":dir:file.txt" actually means "./dir/file.txt" in
335 // DOS notation and should get stored as
336 // (relative) (dir) (file.txt)
337 // "::dir:file.txt" actually means "../dir/file.txt"
338 // stored as (relative) (..) (dir) (file.txt)
339 // This is important only for the Mac as an empty dir
340 // actually means <UP>, whereas under DOS, double
341 // slashes can be ignored: "\\\\" is the same as "\\".
343 my_path
.erase( 0, 1 );
347 // TODO: what is the relative path format here?
352 wxFAIL_MSG( _T("Unknown path format") );
353 // !! Fall through !!
356 // the paths of the form "~" or "~username" are absolute
357 m_relative
= leadingChar
!= wxT('/') && leadingChar
!= _T('~');
361 m_relative
= !IsPathSeparator(leadingChar
, my_format
);
366 // 2) Break up the path into its members. If the original path
367 // was just "/" or "\\", m_dirs will be empty. We know from
368 // the m_relative field, if this means "nothing" or "root dir".
370 wxStringTokenizer
tn( my_path
, GetPathSeparators(my_format
) );
372 while ( tn
.HasMoreTokens() )
374 wxString token
= tn
.GetNextToken();
376 // Remove empty token under DOS and Unix, interpret them
380 if (my_format
== wxPATH_MAC
)
381 m_dirs
.Add( wxT("..") );
390 else // no path at all
396 void wxFileName::Assign(const wxString
& fullpath
,
399 wxString volume
, path
, name
, ext
;
400 SplitPath(fullpath
, &volume
, &path
, &name
, &ext
, format
);
402 Assign(volume
, path
, name
, ext
, format
);
405 void wxFileName::Assign(const wxString
& fullpathOrig
,
406 const wxString
& fullname
,
409 // always recognize fullpath as directory, even if it doesn't end with a
411 wxString fullpath
= fullpathOrig
;
412 if ( !wxEndsWithPathSeparator(fullpath
) )
414 fullpath
+= GetPathSeparator(format
);
417 wxString volume
, path
, name
, ext
;
419 // do some consistency checks in debug mode: the name should be really just
420 // the filename and the path should be really just a path
422 wxString pathDummy
, nameDummy
, extDummy
;
424 SplitPath(fullname
, &pathDummy
, &name
, &ext
, format
);
426 wxASSERT_MSG( pathDummy
.empty(),
427 _T("the file name shouldn't contain the path") );
429 SplitPath(fullpath
, &volume
, &path
, &nameDummy
, &extDummy
, format
);
431 wxASSERT_MSG( nameDummy
.empty() && extDummy
.empty(),
432 _T("the path shouldn't contain file name nor extension") );
434 #else // !__WXDEBUG__
435 SplitPath(fullname
, NULL
/* no path */, &name
, &ext
, format
);
436 SplitPath(fullpath
, &volume
, &path
, NULL
, NULL
, format
);
437 #endif // __WXDEBUG__/!__WXDEBUG__
439 Assign(volume
, path
, name
, ext
, format
);
442 void wxFileName::AssignDir(const wxString
& dir
, wxPathFormat format
)
444 Assign(dir
, _T(""), format
);
447 void wxFileName::Clear()
453 m_ext
= wxEmptyString
;
455 // we don't have any absolute path for now
460 wxFileName
wxFileName::FileName(const wxString
& file
, wxPathFormat format
)
462 return wxFileName(file
, format
);
466 wxFileName
wxFileName::DirName(const wxString
& dir
, wxPathFormat format
)
469 fn
.AssignDir(dir
, format
);
473 // ----------------------------------------------------------------------------
475 // ----------------------------------------------------------------------------
477 bool wxFileName::FileExists() const
479 return wxFileName::FileExists( GetFullPath() );
482 bool wxFileName::FileExists( const wxString
&file
)
484 return ::wxFileExists( file
);
487 bool wxFileName::DirExists() const
489 return wxFileName::DirExists( GetFullPath() );
492 bool wxFileName::DirExists( const wxString
&dir
)
494 return ::wxDirExists( dir
);
497 // ----------------------------------------------------------------------------
498 // CWD and HOME stuff
499 // ----------------------------------------------------------------------------
501 void wxFileName::AssignCwd(const wxString
& volume
)
503 AssignDir(wxFileName::GetCwd(volume
));
507 wxString
wxFileName::GetCwd(const wxString
& volume
)
509 // if we have the volume, we must get the current directory on this drive
510 // and to do this we have to chdir to this volume - at least under Windows,
511 // I don't know how to get the current drive on another volume elsewhere
514 if ( !volume
.empty() )
517 SetCwd(volume
+ GetVolumeSeparator());
520 wxString cwd
= ::wxGetCwd();
522 if ( !volume
.empty() )
530 bool wxFileName::SetCwd()
532 return wxFileName::SetCwd( GetFullPath() );
535 bool wxFileName::SetCwd( const wxString
&cwd
)
537 return ::wxSetWorkingDirectory( cwd
);
540 void wxFileName::AssignHomeDir()
542 AssignDir(wxFileName::GetHomeDir());
545 wxString
wxFileName::GetHomeDir()
547 return ::wxGetHomeDir();
550 void wxFileName::AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
552 wxString tempname
= CreateTempFileName(prefix
, fileTemp
);
553 if ( tempname
.empty() )
555 // error, failed to get temp file name
566 wxFileName::CreateTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
568 wxString path
, dir
, name
;
570 // use the directory specified by the prefix
571 SplitPath(prefix
, &dir
, &name
, NULL
/* extension */);
573 #if defined(__WXWINCE__)
576 // FIXME. Create \temp dir?
579 path
= dir
+ wxT("\\") + prefix
;
581 while (wxFileExists(path
))
583 path
= dir
+ wxT("\\") + prefix
;
588 #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
592 if ( !::GetTempPath(MAX_PATH
, wxStringBuffer(dir
, MAX_PATH
+ 1)) )
594 wxLogLastError(_T("GetTempPath"));
599 // GetTempFileName() fails if we pass it an empty string
603 else // we have a dir to create the file in
605 // ensure we use only the back slashes as GetTempFileName(), unlike all
606 // the other APIs, is picky and doesn't accept the forward ones
607 dir
.Replace(_T("/"), _T("\\"));
610 if ( !::GetTempFileName(dir
, name
, 0, wxStringBuffer(path
, MAX_PATH
+ 1)) )
612 wxLogLastError(_T("GetTempFileName"));
620 #if defined(__WXMAC__) && !defined(__DARWIN__)
621 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
623 dir
= wxGetenv(_T("TMP"));
626 dir
= wxGetenv(_T("TEMP"));
632 #if defined(__DOS__) || defined(__OS2__)
643 if ( !wxEndsWithPathSeparator(dir
) &&
644 (name
.empty() || !wxIsPathSeparator(name
[0u])) )
646 path
+= wxFILE_SEP_PATH
;
651 #if defined(HAVE_MKSTEMP)
652 // scratch space for mkstemp()
653 path
+= _T("XXXXXX");
655 // we need to copy the path to the buffer in which mkstemp() can modify it
656 wxCharBuffer
buf( wxConvFile
.cWX2MB( path
) );
658 // cast is safe because the string length doesn't change
659 int fdTemp
= mkstemp( (char*)(const char*) buf
);
662 // this might be not necessary as mkstemp() on most systems should have
663 // already done it but it doesn't hurt neither...
666 else // mkstemp() succeeded
668 path
= wxConvFile
.cMB2WX( (const char*) buf
);
670 // avoid leaking the fd
673 fileTemp
->Attach(fdTemp
);
680 #else // !HAVE_MKSTEMP
684 path
+= _T("XXXXXX");
686 wxCharBuffer buf
= wxConvFile
.cWX2MB( path
);
687 if ( !mktemp( (const char*) buf
) )
693 path
= wxConvFile
.cMB2WX( (const char*) buf
);
695 #else // !HAVE_MKTEMP (includes __DOS__)
696 // generate the unique file name ourselves
697 #if !defined(__DOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
698 path
<< (unsigned int)getpid();
703 static const size_t numTries
= 1000;
704 for ( size_t n
= 0; n
< numTries
; n
++ )
706 // 3 hex digits is enough for numTries == 1000 < 4096
707 pathTry
= path
+ wxString::Format(_T("%.03x"), (unsigned int) n
);
708 if ( !wxFile::Exists(pathTry
) )
717 #endif // HAVE_MKTEMP/!HAVE_MKTEMP
722 #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
724 #endif // Windows/!Windows
728 wxLogSysError(_("Failed to create a temporary file name"));
730 else if ( fileTemp
&& !fileTemp
->IsOpened() )
732 // open the file - of course, there is a race condition here, this is
733 // why we always prefer using mkstemp()...
735 // NB: GetTempFileName() under Windows creates the file, so using
736 // write_excl there would fail
737 if ( !fileTemp
->Open(path
,
738 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
743 wxS_IRUSR
| wxS_IWUSR
) )
745 // FIXME: If !ok here should we loop and try again with another
746 // file name? That is the standard recourse if open(O_EXCL)
747 // fails, though of course it should be protected against
748 // possible infinite looping too.
750 wxLogError(_("Failed to open temporary file."));
759 // ----------------------------------------------------------------------------
760 // directory operations
761 // ----------------------------------------------------------------------------
763 bool wxFileName::Mkdir( int perm
, int flags
)
765 return wxFileName::Mkdir( GetFullPath(), perm
, flags
);
768 bool wxFileName::Mkdir( const wxString
& dir
, int perm
, int flags
)
770 if ( flags
& wxPATH_MKDIR_FULL
)
772 // split the path in components
774 filename
.AssignDir(dir
);
777 if ( filename
.HasVolume())
779 currPath
<< wxGetVolumeString(filename
.GetVolume(), wxPATH_NATIVE
);
782 wxArrayString dirs
= filename
.GetDirs();
783 size_t count
= dirs
.GetCount();
784 for ( size_t i
= 0; i
< count
; i
++ )
787 #if defined(__WXMAC__) && !defined(__DARWIN__)
788 // relative pathnames are exactely the other way round under mac...
789 !filename
.IsAbsolute()
791 filename
.IsAbsolute()
794 currPath
+= wxFILE_SEP_PATH
;
797 if (!DirExists(currPath
))
799 if (!wxMkdir(currPath
, perm
))
801 // no need to try creating further directories
811 return ::wxMkdir( dir
, perm
);
814 bool wxFileName::Rmdir()
816 return wxFileName::Rmdir( GetFullPath() );
819 bool wxFileName::Rmdir( const wxString
&dir
)
821 return ::wxRmdir( dir
);
824 // ----------------------------------------------------------------------------
825 // path normalization
826 // ----------------------------------------------------------------------------
828 bool wxFileName::Normalize(int flags
,
832 // deal with env vars renaming first as this may seriously change the path
833 if ( flags
& wxPATH_NORM_ENV_VARS
)
835 wxString pathOrig
= GetFullPath(format
);
836 wxString path
= wxExpandEnvVars(pathOrig
);
837 if ( path
!= pathOrig
)
844 // the existing path components
845 wxArrayString dirs
= GetDirs();
847 // the path to prepend in front to make the path absolute
850 format
= GetFormat(format
);
852 // make the path absolute
853 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute(format
) )
857 curDir
.AssignCwd(GetVolume());
861 curDir
.AssignDir(cwd
);
864 // the path may be not absolute because it doesn't have the volume name
865 // but in this case we shouldn't modify the directory components of it
866 // but just set the current volume
867 if ( !HasVolume() && curDir
.HasVolume() )
869 SetVolume(curDir
.GetVolume());
873 // yes, it was the case - we don't need curDir then
879 // handle ~ stuff under Unix only
880 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
882 if ( !dirs
.IsEmpty() )
884 wxString dir
= dirs
[0u];
885 if ( !dir
.empty() && dir
[0u] == _T('~') )
887 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
894 // transform relative path into abs one
897 wxArrayString dirsNew
= curDir
.GetDirs();
898 size_t count
= dirs
.GetCount();
899 for ( size_t n
= 0; n
< count
; n
++ )
901 dirsNew
.Add(dirs
[n
]);
907 // now deal with ".", ".." and the rest
909 size_t count
= dirs
.GetCount();
910 for ( size_t n
= 0; n
< count
; n
++ )
912 wxString dir
= dirs
[n
];
914 if ( flags
& wxPATH_NORM_DOTS
)
916 if ( dir
== wxT(".") )
922 if ( dir
== wxT("..") )
924 if ( m_dirs
.IsEmpty() )
926 wxLogError(_("The path '%s' contains too many \"..\"!"),
927 GetFullPath().c_str());
931 m_dirs
.RemoveAt(m_dirs
.GetCount() - 1);
936 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
944 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
945 if ( (flags
& wxPATH_NORM_SHORTCUT
) )
948 if (GetShortcutTarget(GetFullPath(format
), filename
))
950 // Repeat this since we may now have a new path
951 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
953 filename
.MakeLower();
961 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
963 // VZ: expand env vars here too?
965 m_volume
.MakeLower();
970 // we do have the path now
972 // NB: need to do this before (maybe) calling Assign() below
975 #if defined(__WIN32__)
976 if ( (flags
& wxPATH_NORM_LONG
) && (format
== wxPATH_DOS
) )
978 Assign(GetLongPath());
985 // ----------------------------------------------------------------------------
986 // get the shortcut target
987 // ----------------------------------------------------------------------------
989 // WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions.
990 // The .lnk file is a plain text file so it should be easy to
991 // make it work. Hint from Google Groups:
992 // "If you open up a lnk file, you'll see a
993 // number, followed by a pound sign (#), followed by more text. The
994 // number is the number of characters that follows the pound sign. The
995 // characters after the pound sign are the command line (which _can_
996 // include arguments) to be executed. Any path (e.g. \windows\program
997 // files\myapp.exe) that includes spaces needs to be enclosed in
1000 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
1001 // The following lines are necessary under WinCE
1002 // #include "wx/msw/private.h"
1003 // #include <ole2.h>
1005 #if defined(__WXWINCE__)
1006 #include <shlguid.h>
1009 bool wxFileName::GetShortcutTarget(const wxString
& shortcutPath
, wxString
& targetFilename
, wxString
* arguments
)
1011 wxString path
, file
, ext
;
1012 wxSplitPath(shortcutPath
, & path
, & file
, & ext
);
1016 bool success
= FALSE
;
1018 // Assume it's not a shortcut if it doesn't end with lnk
1019 if (ext
.Lower() != wxT("lnk"))
1022 // create a ShellLink object
1023 hres
= CoCreateInstance(CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
,
1024 IID_IShellLink
, (LPVOID
*) &psl
);
1026 if (SUCCEEDED(hres
))
1029 hres
= psl
->QueryInterface( IID_IPersistFile
, (LPVOID
*) &ppf
);
1030 if (SUCCEEDED(hres
))
1032 WCHAR wsz
[MAX_PATH
];
1034 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, shortcutPath
.mb_str(), -1, wsz
,
1037 hres
= ppf
->Load(wsz
, 0);
1038 if (SUCCEEDED(hres
))
1041 psl
->GetPath(buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1042 targetFilename
= wxString(buf
);
1043 success
= (shortcutPath
!= targetFilename
);
1045 psl
->GetArguments(buf
, 2048);
1047 if (!args
.IsEmpty() && arguments
)
1060 // ----------------------------------------------------------------------------
1061 // absolute/relative paths
1062 // ----------------------------------------------------------------------------
1064 bool wxFileName::IsAbsolute(wxPathFormat format
) const
1066 // if our path doesn't start with a path separator, it's not an absolute
1071 if ( !GetVolumeSeparator(format
).empty() )
1073 // this format has volumes and an absolute path must have one, it's not
1074 // enough to have the full path to bean absolute file under Windows
1075 if ( GetVolume().empty() )
1082 bool wxFileName::MakeRelativeTo(const wxString
& pathBase
, wxPathFormat format
)
1084 wxFileName fnBase
= wxFileName::DirName(pathBase
, format
);
1086 // get cwd only once - small time saving
1087 wxString cwd
= wxGetCwd();
1088 Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1089 fnBase
.Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1091 bool withCase
= IsCaseSensitive(format
);
1093 // we can't do anything if the files live on different volumes
1094 if ( !GetVolume().IsSameAs(fnBase
.GetVolume(), withCase
) )
1100 // same drive, so we don't need our volume
1103 // remove common directories starting at the top
1104 while ( !m_dirs
.IsEmpty() && !fnBase
.m_dirs
.IsEmpty() &&
1105 m_dirs
[0u].IsSameAs(fnBase
.m_dirs
[0u], withCase
) )
1108 fnBase
.m_dirs
.RemoveAt(0);
1111 // add as many ".." as needed
1112 size_t count
= fnBase
.m_dirs
.GetCount();
1113 for ( size_t i
= 0; i
< count
; i
++ )
1115 m_dirs
.Insert(wxT(".."), 0u);
1118 if ( format
== wxPATH_UNIX
|| format
== wxPATH_DOS
)
1120 // a directory made relative with respect to itself is '.' under Unix
1121 // and DOS, by definition (but we don't have to insert "./" for the
1123 if ( m_dirs
.IsEmpty() && IsDir() )
1125 m_dirs
.Add(_T('.'));
1135 // ----------------------------------------------------------------------------
1136 // filename kind tests
1137 // ----------------------------------------------------------------------------
1139 bool wxFileName::SameAs(const wxFileName
& filepath
, wxPathFormat format
) const
1141 wxFileName fn1
= *this,
1144 // get cwd only once - small time saving
1145 wxString cwd
= wxGetCwd();
1146 fn1
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1147 fn2
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1149 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
1152 // TODO: compare inodes for Unix, this works even when filenames are
1153 // different but files are the same (symlinks) (VZ)
1159 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
1161 // only Unix filenames are truely case-sensitive
1162 return GetFormat(format
) == wxPATH_UNIX
;
1166 wxString
wxFileName::GetForbiddenChars(wxPathFormat format
)
1168 // Inits to forbidden characters that are common to (almost) all platforms.
1169 wxString strForbiddenChars
= wxT("*?");
1171 // If asserts, wxPathFormat has been changed. In case of a new path format
1172 // addition, the following code might have to be updated.
1173 wxCOMPILE_TIME_ASSERT(wxPATH_MAX
== 5, wxPathFormatChanged
);
1174 switch ( GetFormat(format
) )
1177 wxFAIL_MSG( wxT("Unknown path format") );
1178 // !! Fall through !!
1184 // On a Mac even names with * and ? are allowed (Tested with OS
1185 // 9.2.1 and OS X 10.2.5)
1186 strForbiddenChars
= wxEmptyString
;
1190 strForbiddenChars
+= wxT("\\/:\"<>|");
1197 return strForbiddenChars
;
1201 wxString
wxFileName::GetVolumeSeparator(wxPathFormat format
)
1205 if ( (GetFormat(format
) == wxPATH_DOS
) ||
1206 (GetFormat(format
) == wxPATH_VMS
) )
1208 sepVol
= wxFILE_SEP_DSK
;
1216 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
1219 switch ( GetFormat(format
) )
1222 // accept both as native APIs do but put the native one first as
1223 // this is the one we use in GetFullPath()
1224 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
1228 wxFAIL_MSG( _T("Unknown wxPATH_XXX style") );
1232 seps
= wxFILE_SEP_PATH_UNIX
;
1236 seps
= wxFILE_SEP_PATH_MAC
;
1240 seps
= wxFILE_SEP_PATH_VMS
;
1248 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
1250 // wxString::Find() doesn't work as expected with NUL - it will always find
1251 // it, so it is almost surely a bug if this function is called with NUL arg
1252 wxASSERT_MSG( ch
!= _T('\0'), _T("shouldn't be called with NUL") );
1254 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
1257 // ----------------------------------------------------------------------------
1258 // path components manipulation
1259 // ----------------------------------------------------------------------------
1261 /* static */ bool wxFileName::IsValidDirComponent(const wxString
& dir
)
1265 wxFAIL_MSG( _T("empty directory passed to wxFileName::InsertDir()") );
1270 const size_t len
= dir
.length();
1271 for ( size_t n
= 0; n
< len
; n
++ )
1273 if ( dir
[n
] == GetVolumeSeparator() || IsPathSeparator(dir
[n
]) )
1275 wxFAIL_MSG( _T("invalid directory component in wxFileName") );
1284 void wxFileName::AppendDir( const wxString
&dir
)
1286 if ( IsValidDirComponent(dir
) )
1290 void wxFileName::PrependDir( const wxString
&dir
)
1295 void wxFileName::InsertDir( int before
, const wxString
&dir
)
1297 if ( IsValidDirComponent(dir
) )
1298 m_dirs
.Insert( dir
, before
);
1301 void wxFileName::RemoveDir( int pos
)
1303 m_dirs
.RemoveAt( (size_t)pos
);
1306 // ----------------------------------------------------------------------------
1308 // ----------------------------------------------------------------------------
1310 void wxFileName::SetFullName(const wxString
& fullname
)
1312 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
1315 wxString
wxFileName::GetFullName() const
1317 wxString fullname
= m_name
;
1318 if ( !m_ext
.empty() )
1320 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
1326 wxString
wxFileName::GetPath( int flags
, wxPathFormat format
) const
1328 format
= GetFormat( format
);
1332 // return the volume with the path as well if requested
1333 if ( flags
& wxPATH_GET_VOLUME
)
1335 fullpath
+= wxGetVolumeString(GetVolume(), format
);
1338 // the leading character
1343 fullpath
+= wxFILE_SEP_PATH_MAC
;
1348 fullpath
+= wxFILE_SEP_PATH_DOS
;
1352 wxFAIL_MSG( wxT("Unknown path format") );
1358 // normally the absolute file names start with a slash
1359 // with one exception: the ones like "~/foo.bar" don't
1361 if ( m_dirs
.IsEmpty() || m_dirs
[0u] != _T('~') )
1363 fullpath
+= wxFILE_SEP_PATH_UNIX
;
1369 // no leading character here but use this place to unset
1370 // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense
1371 // as, if I understand correctly, there should never be a dot
1372 // before the closing bracket
1373 flags
&= ~wxPATH_GET_SEPARATOR
;
1376 if ( m_dirs
.empty() )
1378 // there is nothing more
1382 // then concatenate all the path components using the path separator
1383 if ( format
== wxPATH_VMS
)
1385 fullpath
+= wxT('[');
1388 const size_t dirCount
= m_dirs
.GetCount();
1389 for ( size_t i
= 0; i
< dirCount
; i
++ )
1394 if ( m_dirs
[i
] == wxT(".") )
1396 // skip appending ':', this shouldn't be done in this
1397 // case as "::" is interpreted as ".." under Unix
1401 // convert back from ".." to nothing
1402 if ( m_dirs
[i
] != wxT("..") )
1403 fullpath
+= m_dirs
[i
];
1407 wxFAIL_MSG( wxT("Unexpected path format") );
1408 // still fall through
1412 fullpath
+= m_dirs
[i
];
1416 // TODO: What to do with ".." under VMS
1418 // convert back from ".." to nothing
1419 if ( m_dirs
[i
] != wxT("..") )
1420 fullpath
+= m_dirs
[i
];
1424 if ( (flags
& wxPATH_GET_SEPARATOR
) || (i
!= dirCount
- 1) )
1425 fullpath
+= GetPathSeparator(format
);
1428 if ( format
== wxPATH_VMS
)
1430 fullpath
+= wxT(']');
1436 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
1438 // we already have a function to get the path
1439 wxString fullpath
= GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
,
1442 // now just add the file name and extension to it
1443 fullpath
+= GetFullName();
1448 // Return the short form of the path (returns identity on non-Windows platforms)
1449 wxString
wxFileName::GetShortPath() const
1451 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
1452 wxString
path(GetFullPath());
1454 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
1458 ok
= ::GetShortPathName
1461 wxStringBuffer(pathOut
, sz
),
1470 return GetFullPath();
1474 // Return the long form of the path (returns identity on non-Windows platforms)
1475 wxString
wxFileName::GetLongPath() const
1478 path
= GetFullPath();
1480 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1481 bool success
= false;
1483 #if wxUSE_DYNAMIC_LOADER
1484 typedef DWORD (WINAPI
*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
1486 static bool s_triedToLoad
= false;
1488 if ( !s_triedToLoad
)
1490 // suppress the errors about missing GetLongPathName[AW]
1493 s_triedToLoad
= true;
1494 wxDynamicLibrary
dllKernel(_T("kernel32"));
1495 if ( dllKernel
.IsLoaded() )
1497 // may succeed or fail depending on the Windows version
1498 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
1500 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) dllKernel
.GetSymbol(_T("GetLongPathNameW"));
1502 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) dllKernel
.GetSymbol(_T("GetLongPathNameA"));
1505 if ( s_pfnGetLongPathName
)
1507 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1508 bool ok
= dwSize
> 0;
1512 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1516 ok
= (*s_pfnGetLongPathName
)
1519 wxStringBuffer(pathOut
, sz
),
1531 #endif // wxUSE_DYNAMIC_LOADER
1535 // The OS didn't support GetLongPathName, or some other error.
1536 // We need to call FindFirstFile on each component in turn.
1538 WIN32_FIND_DATA findFileData
;
1542 pathOut
= GetVolume() +
1543 GetVolumeSeparator(wxPATH_DOS
) +
1544 GetPathSeparator(wxPATH_DOS
);
1546 pathOut
= wxEmptyString
;
1548 wxArrayString dirs
= GetDirs();
1549 dirs
.Add(GetFullName());
1553 size_t count
= dirs
.GetCount();
1554 for ( size_t i
= 0; i
< count
; i
++ )
1556 // We're using pathOut to collect the long-name path, but using a
1557 // temporary for appending the last path component which may be
1559 tmpPath
= pathOut
+ dirs
[i
];
1561 if ( tmpPath
.empty() )
1564 // can't see this being necessary? MF
1565 if ( tmpPath
.Last() == GetVolumeSeparator(wxPATH_DOS
) )
1567 // Can't pass a drive and root dir to FindFirstFile,
1568 // so continue to next dir
1569 tmpPath
+= wxFILE_SEP_PATH
;
1574 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
1575 if (hFind
== INVALID_HANDLE_VALUE
)
1577 // Error: most likely reason is that path doesn't exist, so
1578 // append any unprocessed parts and return
1579 for ( i
+= 1; i
< count
; i
++ )
1580 tmpPath
+= wxFILE_SEP_PATH
+ dirs
[i
];
1585 pathOut
+= findFileData
.cFileName
;
1586 if ( (i
< (count
-1)) )
1587 pathOut
+= wxFILE_SEP_PATH
;
1594 #endif // Win32/!Win32
1599 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
1601 if (format
== wxPATH_NATIVE
)
1603 #if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__)
1604 format
= wxPATH_DOS
;
1605 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1606 format
= wxPATH_MAC
;
1607 #elif defined(__VMS)
1608 format
= wxPATH_VMS
;
1610 format
= wxPATH_UNIX
;
1616 // ----------------------------------------------------------------------------
1617 // path splitting function
1618 // ----------------------------------------------------------------------------
1621 void wxFileName::SplitPath(const wxString
& fullpathWithVolume
,
1622 wxString
*pstrVolume
,
1626 wxPathFormat format
)
1628 format
= GetFormat(format
);
1630 wxString fullpath
= fullpathWithVolume
;
1632 // under VMS the end of the path is ']', not the path separator used to
1633 // separate the components
1634 wxString sepPath
= format
== wxPATH_VMS
? wxString(_T(']'))
1635 : GetPathSeparators(format
);
1637 // special Windows UNC paths hack: transform \\share\path into share:path
1638 if ( format
== wxPATH_DOS
)
1640 if ( fullpath
.length() >= 4 &&
1641 fullpath
[0u] == wxFILE_SEP_PATH_DOS
&&
1642 fullpath
[1u] == wxFILE_SEP_PATH_DOS
)
1644 fullpath
.erase(0, 2);
1646 size_t posFirstSlash
= fullpath
.find_first_of(sepPath
);
1647 if ( posFirstSlash
!= wxString::npos
)
1649 fullpath
[posFirstSlash
] = wxFILE_SEP_DSK
;
1651 // UNC paths are always absolute, right? (FIXME)
1652 fullpath
.insert(posFirstSlash
+ 1, 1, wxFILE_SEP_PATH_DOS
);
1657 // We separate the volume here
1658 if ( format
== wxPATH_DOS
|| format
== wxPATH_VMS
)
1660 wxString sepVol
= GetVolumeSeparator(format
);
1662 size_t posFirstColon
= fullpath
.find_first_of(sepVol
);
1663 if ( posFirstColon
!= wxString::npos
)
1667 *pstrVolume
= fullpath
.Left(posFirstColon
);
1670 // remove the volume name and the separator from the full path
1671 fullpath
.erase(0, posFirstColon
+ sepVol
.length());
1675 // find the positions of the last dot and last path separator in the path
1676 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
1677 size_t posLastSlash
= fullpath
.find_last_of(sepPath
);
1679 if ( (posLastDot
!= wxString::npos
) &&
1680 ((format
== wxPATH_UNIX
) || (format
== wxPATH_VMS
)) )
1682 if ( (posLastDot
== 0) ||
1683 (fullpath
[posLastDot
- 1] == sepPath
[0u] ) )
1685 // under Unix and VMS, dot may be (and commonly is) the first
1686 // character of the filename, don't treat the entire filename as
1687 // extension in this case
1688 posLastDot
= wxString::npos
;
1692 // if we do have a dot and a slash, check that the dot is in the name part
1693 if ( (posLastDot
!= wxString::npos
) &&
1694 (posLastSlash
!= wxString::npos
) &&
1695 (posLastDot
< posLastSlash
) )
1697 // the dot is part of the path, not the start of the extension
1698 posLastDot
= wxString::npos
;
1701 // now fill in the variables provided by user
1704 if ( posLastSlash
== wxString::npos
)
1711 // take everything up to the path separator but take care to make
1712 // the path equal to something like '/', not empty, for the files
1713 // immediately under root directory
1714 size_t len
= posLastSlash
;
1716 // this rule does not apply to mac since we do not start with colons (sep)
1717 // except for relative paths
1718 if ( !len
&& format
!= wxPATH_MAC
)
1721 *pstrPath
= fullpath
.Left(len
);
1723 // special VMS hack: remove the initial bracket
1724 if ( format
== wxPATH_VMS
)
1726 if ( (*pstrPath
)[0u] == _T('[') )
1727 pstrPath
->erase(0, 1);
1734 // take all characters starting from the one after the last slash and
1735 // up to, but excluding, the last dot
1736 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
1738 if ( posLastDot
== wxString::npos
)
1740 // take all until the end
1741 count
= wxString::npos
;
1743 else if ( posLastSlash
== wxString::npos
)
1747 else // have both dot and slash
1749 count
= posLastDot
- posLastSlash
- 1;
1752 *pstrName
= fullpath
.Mid(nStart
, count
);
1757 if ( posLastDot
== wxString::npos
)
1764 // take everything after the dot
1765 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
1771 void wxFileName::SplitPath(const wxString
& fullpath
,
1775 wxPathFormat format
)
1778 SplitPath(fullpath
, &volume
, path
, name
, ext
, format
);
1782 path
->Prepend(wxGetVolumeString(volume
, format
));
1786 // ----------------------------------------------------------------------------
1788 // ----------------------------------------------------------------------------
1792 bool wxFileName::SetTimes(const wxDateTime
*dtAccess
,
1793 const wxDateTime
*dtMod
,
1794 const wxDateTime
*dtCreate
)
1796 #if defined(__WIN32__)
1799 // VZ: please let me know how to do this if you can
1800 wxFAIL_MSG( _T("SetTimes() not implemented for the directories") );
1804 wxFileHandle
fh(GetFullPath(), wxFileHandle::Write
);
1807 FILETIME ftAccess
, ftCreate
, ftWrite
;
1810 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
1812 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
1814 ConvertWxToFileTime(&ftWrite
, *dtMod
);
1816 if ( ::SetFileTime(fh
,
1817 dtCreate
? &ftCreate
: NULL
,
1818 dtAccess
? &ftAccess
: NULL
,
1819 dtMod
? &ftWrite
: NULL
) )
1825 #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
1826 if ( !dtAccess
&& !dtMod
)
1828 // can't modify the creation time anyhow, don't try
1832 // if dtAccess or dtMod is not specified, use the other one (which must be
1833 // non NULL because of the test above) for both times
1835 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
1836 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
1837 if ( utime(GetFullPath().fn_str(), &utm
) == 0 )
1841 #else // other platform
1844 wxLogSysError(_("Failed to modify file times for '%s'"),
1845 GetFullPath().c_str());
1850 bool wxFileName::Touch()
1852 #if defined(__UNIX_LIKE__)
1853 // under Unix touching file is simple: just pass NULL to utime()
1854 if ( utime(GetFullPath().fn_str(), NULL
) == 0 )
1859 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1862 #else // other platform
1863 wxDateTime dtNow
= wxDateTime::Now();
1865 return SetTimes(&dtNow
, &dtNow
, NULL
/* don't change create time */);
1869 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
1871 wxDateTime
*dtCreate
) const
1873 #if defined(__WIN32__)
1874 // we must use different methods for the files and directories under
1875 // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and
1876 // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and
1879 FILETIME ftAccess
, ftCreate
, ftWrite
;
1882 // implemented in msw/dir.cpp
1883 extern bool wxGetDirectoryTimes(const wxString
& dirname
,
1884 FILETIME
*, FILETIME
*, FILETIME
*);
1886 // we should pass the path without the trailing separator to
1887 // wxGetDirectoryTimes()
1888 ok
= wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME
),
1889 &ftAccess
, &ftCreate
, &ftWrite
);
1893 wxFileHandle
fh(GetFullPath(), wxFileHandle::Read
);
1896 ok
= ::GetFileTime(fh
,
1897 dtCreate
? &ftCreate
: NULL
,
1898 dtAccess
? &ftAccess
: NULL
,
1899 dtMod
? &ftWrite
: NULL
) != 0;
1910 ConvertFileTimeToWx(dtCreate
, ftCreate
);
1912 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1914 ConvertFileTimeToWx(dtMod
, ftWrite
);
1918 #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
1920 if ( wxStat( GetFullPath().c_str(), &stBuf
) == 0 )
1923 dtAccess
->Set(stBuf
.st_atime
);
1925 dtMod
->Set(stBuf
.st_mtime
);
1927 dtCreate
->Set(stBuf
.st_ctime
);
1931 #else // other platform
1934 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1935 GetFullPath().c_str());
1940 #endif // wxUSE_DATETIME
1944 const short kMacExtensionMaxLength
= 16 ;
1945 class MacDefaultExtensionRecord
1948 MacDefaultExtensionRecord()
1951 m_type
= m_creator
= NULL
;
1953 MacDefaultExtensionRecord( const MacDefaultExtensionRecord
& from
)
1955 wxStrcpy( m_ext
, from
.m_ext
) ;
1956 m_type
= from
.m_type
;
1957 m_creator
= from
.m_creator
;
1959 MacDefaultExtensionRecord( const wxChar
* extension
, OSType type
, OSType creator
)
1961 wxStrncpy( m_ext
, extension
, kMacExtensionMaxLength
) ;
1962 m_ext
[kMacExtensionMaxLength
] = 0 ;
1964 m_creator
= creator
;
1966 wxChar m_ext
[kMacExtensionMaxLength
] ;
1971 #include "wx/dynarray.h"
1972 WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord
, MacDefaultExtensionArray
) ;
1974 bool gMacDefaultExtensionsInited
= false ;
1976 #include "wx/arrimpl.cpp"
1978 WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray
) ;
1980 MacDefaultExtensionArray gMacDefaultExtensions
;
1982 // load the default extensions
1983 MacDefaultExtensionRecord gDefaults
[] =
1985 MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) ,
1986 MacDefaultExtensionRecord( wxT("tif") , 'TIFF' , '****' ) ,
1987 MacDefaultExtensionRecord( wxT("jpg") , 'JPEG' , '****' ) ,
1990 static void MacEnsureDefaultExtensionsLoaded()
1992 if ( !gMacDefaultExtensionsInited
)
1994 // we could load the pc exchange prefs here too
1995 for ( size_t i
= 0 ; i
< WXSIZEOF( gDefaults
) ; ++i
)
1997 gMacDefaultExtensions
.Add( gDefaults
[i
] ) ;
1999 gMacDefaultExtensionsInited
= true ;
2002 bool wxFileName::MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
)
2006 wxMacFilename2FSSpec(GetFullPath(),&spec
) ;
2007 OSErr err
= FSpGetFInfo( &spec
, &fndrInfo
) ;
2008 wxCHECK( err
== noErr
, false ) ;
2010 fndrInfo
.fdType
= type
;
2011 fndrInfo
.fdCreator
= creator
;
2012 FSpSetFInfo( &spec
, &fndrInfo
) ;
2016 bool wxFileName::MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
)
2020 wxMacFilename2FSSpec(GetFullPath(),&spec
) ;
2021 OSErr err
= FSpGetFInfo( &spec
, &fndrInfo
) ;
2022 wxCHECK( err
== noErr
, false ) ;
2024 *type
= fndrInfo
.fdType
;
2025 *creator
= fndrInfo
.fdCreator
;
2029 bool wxFileName::MacSetDefaultTypeAndCreator()
2031 wxUint32 type
, creator
;
2032 if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type
,
2035 return MacSetTypeAndCreator( type
, creator
) ;
2040 bool wxFileName::MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
)
2042 MacEnsureDefaultExtensionsLoaded() ;
2043 wxString extl
= ext
.Lower() ;
2044 for( int i
= gMacDefaultExtensions
.Count() - 1 ; i
>= 0 ; --i
)
2046 if ( gMacDefaultExtensions
.Item(i
).m_ext
== extl
)
2048 *type
= gMacDefaultExtensions
.Item(i
).m_type
;
2049 *creator
= gMacDefaultExtensions
.Item(i
).m_creator
;
2056 void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
)
2058 MacEnsureDefaultExtensionsLoaded() ;
2059 MacDefaultExtensionRecord rec
;
2061 rec
.m_creator
= creator
;
2062 wxStrncpy( rec
.m_ext
, ext
.Lower().c_str() , kMacExtensionMaxLength
) ;
2063 gMacDefaultExtensions
.Add( rec
) ;