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"
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
& pathOrig
, wxPathFormat format
)
319 if ( pathOrig
.empty() )
327 format
= GetFormat( format
);
329 // 0) deal with possible volume part first
332 SplitVolume(pathOrig
, &volume
, &path
, format
);
333 if ( !volume
.empty() )
340 // 1) Determine if the path is relative or absolute.
341 wxChar leadingChar
= path
[0u];
346 m_relative
= leadingChar
== wxT(':');
348 // We then remove a leading ":". The reason is in our
349 // storage form for relative paths:
350 // ":dir:file.txt" actually means "./dir/file.txt" in
351 // DOS notation and should get stored as
352 // (relative) (dir) (file.txt)
353 // "::dir:file.txt" actually means "../dir/file.txt"
354 // stored as (relative) (..) (dir) (file.txt)
355 // This is important only for the Mac as an empty dir
356 // actually means <UP>, whereas under DOS, double
357 // slashes can be ignored: "\\\\" is the same as "\\".
363 // TODO: what is the relative path format here?
368 wxFAIL_MSG( _T("Unknown path format") );
369 // !! Fall through !!
372 // the paths of the form "~" or "~username" are absolute
373 m_relative
= leadingChar
!= wxT('/') && leadingChar
!= _T('~');
377 m_relative
= !IsPathSeparator(leadingChar
, format
);
382 // 2) Break up the path into its members. If the original path
383 // was just "/" or "\\", m_dirs will be empty. We know from
384 // the m_relative field, if this means "nothing" or "root dir".
386 wxStringTokenizer
tn( path
, GetPathSeparators(format
) );
388 while ( tn
.HasMoreTokens() )
390 wxString token
= tn
.GetNextToken();
392 // Remove empty token under DOS and Unix, interpret them
396 if (format
== wxPATH_MAC
)
397 m_dirs
.Add( wxT("..") );
407 void wxFileName::Assign(const wxString
& fullpath
,
410 wxString volume
, path
, name
, ext
;
411 SplitPath(fullpath
, &volume
, &path
, &name
, &ext
, format
);
413 Assign(volume
, path
, name
, ext
, format
);
416 void wxFileName::Assign(const wxString
& fullpathOrig
,
417 const wxString
& fullname
,
420 // always recognize fullpath as directory, even if it doesn't end with a
422 wxString fullpath
= fullpathOrig
;
423 if ( !wxEndsWithPathSeparator(fullpath
) )
425 fullpath
+= GetPathSeparator(format
);
428 wxString volume
, path
, name
, ext
;
430 // do some consistency checks in debug mode: the name should be really just
431 // the filename and the path should be really just a path
433 wxString pathDummy
, nameDummy
, extDummy
;
435 SplitPath(fullname
, &pathDummy
, &name
, &ext
, format
);
437 wxASSERT_MSG( pathDummy
.empty(),
438 _T("the file name shouldn't contain the path") );
440 SplitPath(fullpath
, &volume
, &path
, &nameDummy
, &extDummy
, format
);
442 wxASSERT_MSG( nameDummy
.empty() && extDummy
.empty(),
443 _T("the path shouldn't contain file name nor extension") );
445 #else // !__WXDEBUG__
446 SplitPath(fullname
, NULL
/* no path */, &name
, &ext
, format
);
447 SplitPath(fullpath
, &volume
, &path
, NULL
, NULL
, format
);
448 #endif // __WXDEBUG__/!__WXDEBUG__
450 Assign(volume
, path
, name
, ext
, format
);
453 void wxFileName::AssignDir(const wxString
& dir
, wxPathFormat format
)
455 Assign(dir
, _T(""), format
);
458 void wxFileName::Clear()
464 m_ext
= wxEmptyString
;
466 // we don't have any absolute path for now
471 wxFileName
wxFileName::FileName(const wxString
& file
, wxPathFormat format
)
473 return wxFileName(file
, format
);
477 wxFileName
wxFileName::DirName(const wxString
& dir
, wxPathFormat format
)
480 fn
.AssignDir(dir
, format
);
484 // ----------------------------------------------------------------------------
486 // ----------------------------------------------------------------------------
488 bool wxFileName::FileExists() const
490 return wxFileName::FileExists( GetFullPath() );
493 bool wxFileName::FileExists( const wxString
&file
)
495 return ::wxFileExists( file
);
498 bool wxFileName::DirExists() const
500 return wxFileName::DirExists( GetFullPath() );
503 bool wxFileName::DirExists( const wxString
&dir
)
505 return ::wxDirExists( dir
);
508 // ----------------------------------------------------------------------------
509 // CWD and HOME stuff
510 // ----------------------------------------------------------------------------
512 void wxFileName::AssignCwd(const wxString
& volume
)
514 AssignDir(wxFileName::GetCwd(volume
));
518 wxString
wxFileName::GetCwd(const wxString
& volume
)
520 // if we have the volume, we must get the current directory on this drive
521 // and to do this we have to chdir to this volume - at least under Windows,
522 // I don't know how to get the current drive on another volume elsewhere
525 if ( !volume
.empty() )
528 SetCwd(volume
+ GetVolumeSeparator());
531 wxString cwd
= ::wxGetCwd();
533 if ( !volume
.empty() )
541 bool wxFileName::SetCwd()
543 return wxFileName::SetCwd( GetFullPath() );
546 bool wxFileName::SetCwd( const wxString
&cwd
)
548 return ::wxSetWorkingDirectory( cwd
);
551 void wxFileName::AssignHomeDir()
553 AssignDir(wxFileName::GetHomeDir());
556 wxString
wxFileName::GetHomeDir()
558 return ::wxGetHomeDir();
561 void wxFileName::AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
563 wxString tempname
= CreateTempFileName(prefix
, fileTemp
);
564 if ( tempname
.empty() )
566 // error, failed to get temp file name
577 wxFileName::CreateTempFileName(const wxString
& prefix
, wxFile
*fileTemp
)
579 wxString path
, dir
, name
;
581 // use the directory specified by the prefix
582 SplitPath(prefix
, &dir
, &name
, NULL
/* extension */);
584 #if defined(__WXWINCE__)
587 // FIXME. Create \temp dir?
590 path
= dir
+ wxT("\\") + prefix
;
592 while (wxFileExists(path
))
594 path
= dir
+ wxT("\\") + prefix
;
599 #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
603 if ( !::GetTempPath(MAX_PATH
, wxStringBuffer(dir
, MAX_PATH
+ 1)) )
605 wxLogLastError(_T("GetTempPath"));
610 // GetTempFileName() fails if we pass it an empty string
614 else // we have a dir to create the file in
616 // ensure we use only the back slashes as GetTempFileName(), unlike all
617 // the other APIs, is picky and doesn't accept the forward ones
618 dir
.Replace(_T("/"), _T("\\"));
621 if ( !::GetTempFileName(dir
, name
, 0, wxStringBuffer(path
, MAX_PATH
+ 1)) )
623 wxLogLastError(_T("GetTempFileName"));
631 #if defined(__WXMAC__) && !defined(__DARWIN__)
632 dir
= wxMacFindFolder( (short) kOnSystemDisk
, kTemporaryFolderType
, kCreateFolder
) ;
634 dir
= wxGetenv(_T("TMP"));
637 dir
= wxGetenv(_T("TEMP"));
643 #if defined(__DOS__) || defined(__OS2__)
654 if ( !wxEndsWithPathSeparator(dir
) &&
655 (name
.empty() || !wxIsPathSeparator(name
[0u])) )
657 path
+= wxFILE_SEP_PATH
;
662 #if defined(HAVE_MKSTEMP)
663 // scratch space for mkstemp()
664 path
+= _T("XXXXXX");
666 // we need to copy the path to the buffer in which mkstemp() can modify it
667 wxCharBuffer
buf( wxConvFile
.cWX2MB( path
) );
669 // cast is safe because the string length doesn't change
670 int fdTemp
= mkstemp( (char*)(const char*) buf
);
673 // this might be not necessary as mkstemp() on most systems should have
674 // already done it but it doesn't hurt neither...
677 else // mkstemp() succeeded
679 path
= wxConvFile
.cMB2WX( (const char*) buf
);
681 // avoid leaking the fd
684 fileTemp
->Attach(fdTemp
);
691 #else // !HAVE_MKSTEMP
695 path
+= _T("XXXXXX");
697 wxCharBuffer buf
= wxConvFile
.cWX2MB( path
);
698 if ( !mktemp( (const char*) buf
) )
704 path
= wxConvFile
.cMB2WX( (const char*) buf
);
706 #else // !HAVE_MKTEMP (includes __DOS__)
707 // generate the unique file name ourselves
708 #if !defined(__DOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) )
709 path
<< (unsigned int)getpid();
714 static const size_t numTries
= 1000;
715 for ( size_t n
= 0; n
< numTries
; n
++ )
717 // 3 hex digits is enough for numTries == 1000 < 4096
718 pathTry
= path
+ wxString::Format(_T("%.03x"), (unsigned int) n
);
719 if ( !wxFile::Exists(pathTry
) )
728 #endif // HAVE_MKTEMP/!HAVE_MKTEMP
733 #endif // HAVE_MKSTEMP/!HAVE_MKSTEMP
735 #endif // Windows/!Windows
739 wxLogSysError(_("Failed to create a temporary file name"));
741 else if ( fileTemp
&& !fileTemp
->IsOpened() )
743 // open the file - of course, there is a race condition here, this is
744 // why we always prefer using mkstemp()...
746 // NB: GetTempFileName() under Windows creates the file, so using
747 // write_excl there would fail
748 if ( !fileTemp
->Open(path
,
749 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
754 wxS_IRUSR
| wxS_IWUSR
) )
756 // FIXME: If !ok here should we loop and try again with another
757 // file name? That is the standard recourse if open(O_EXCL)
758 // fails, though of course it should be protected against
759 // possible infinite looping too.
761 wxLogError(_("Failed to open temporary file."));
770 // ----------------------------------------------------------------------------
771 // directory operations
772 // ----------------------------------------------------------------------------
774 bool wxFileName::Mkdir( int perm
, int flags
)
776 return wxFileName::Mkdir( GetFullPath(), perm
, flags
);
779 bool wxFileName::Mkdir( const wxString
& dir
, int perm
, int flags
)
781 if ( flags
& wxPATH_MKDIR_FULL
)
783 // split the path in components
785 filename
.AssignDir(dir
);
788 if ( filename
.HasVolume())
790 currPath
<< wxGetVolumeString(filename
.GetVolume(), wxPATH_NATIVE
);
793 wxArrayString dirs
= filename
.GetDirs();
794 size_t count
= dirs
.GetCount();
795 for ( size_t i
= 0; i
< count
; i
++ )
798 #if defined(__WXMAC__) && !defined(__DARWIN__)
799 // relative pathnames are exactely the other way round under mac...
800 !filename
.IsAbsolute()
802 filename
.IsAbsolute()
805 currPath
+= wxFILE_SEP_PATH
;
808 if (!DirExists(currPath
))
810 if (!wxMkdir(currPath
, perm
))
812 // no need to try creating further directories
822 return ::wxMkdir( dir
, perm
);
825 bool wxFileName::Rmdir()
827 return wxFileName::Rmdir( GetFullPath() );
830 bool wxFileName::Rmdir( const wxString
&dir
)
832 return ::wxRmdir( dir
);
835 // ----------------------------------------------------------------------------
836 // path normalization
837 // ----------------------------------------------------------------------------
839 bool wxFileName::Normalize(int flags
,
843 // deal with env vars renaming first as this may seriously change the path
844 if ( flags
& wxPATH_NORM_ENV_VARS
)
846 wxString pathOrig
= GetFullPath(format
);
847 wxString path
= wxExpandEnvVars(pathOrig
);
848 if ( path
!= pathOrig
)
855 // the existing path components
856 wxArrayString dirs
= GetDirs();
858 // the path to prepend in front to make the path absolute
861 format
= GetFormat(format
);
863 // make the path absolute
864 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute(format
) )
868 curDir
.AssignCwd(GetVolume());
872 curDir
.AssignDir(cwd
);
875 // the path may be not absolute because it doesn't have the volume name
876 // but in this case we shouldn't modify the directory components of it
877 // but just set the current volume
878 if ( !HasVolume() && curDir
.HasVolume() )
880 SetVolume(curDir
.GetVolume());
884 // yes, it was the case - we don't need curDir then
890 // handle ~ stuff under Unix only
891 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
893 if ( !dirs
.IsEmpty() )
895 wxString dir
= dirs
[0u];
896 if ( !dir
.empty() && dir
[0u] == _T('~') )
898 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
905 // transform relative path into abs one
908 wxArrayString dirsNew
= curDir
.GetDirs();
909 size_t count
= dirs
.GetCount();
910 for ( size_t n
= 0; n
< count
; n
++ )
912 dirsNew
.Add(dirs
[n
]);
918 // now deal with ".", ".." and the rest
920 size_t count
= dirs
.GetCount();
921 for ( size_t n
= 0; n
< count
; n
++ )
923 wxString dir
= dirs
[n
];
925 if ( flags
& wxPATH_NORM_DOTS
)
927 if ( dir
== wxT(".") )
933 if ( dir
== wxT("..") )
935 if ( m_dirs
.IsEmpty() )
937 wxLogError(_("The path '%s' contains too many \"..\"!"),
938 GetFullPath().c_str());
942 m_dirs
.RemoveAt(m_dirs
.GetCount() - 1);
947 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
955 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
956 if ( (flags
& wxPATH_NORM_SHORTCUT
) )
959 if (GetShortcutTarget(GetFullPath(format
), filename
))
961 // Repeat this since we may now have a new path
962 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
964 filename
.MakeLower();
972 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
974 // VZ: expand env vars here too?
976 m_volume
.MakeLower();
981 // we do have the path now
983 // NB: need to do this before (maybe) calling Assign() below
986 #if defined(__WIN32__)
987 if ( (flags
& wxPATH_NORM_LONG
) && (format
== wxPATH_DOS
) )
989 Assign(GetLongPath());
996 // ----------------------------------------------------------------------------
997 // get the shortcut target
998 // ----------------------------------------------------------------------------
1000 // WinCE (3) doesn't have CLSID_ShellLink, IID_IShellLink definitions.
1001 // The .lnk file is a plain text file so it should be easy to
1002 // make it work. Hint from Google Groups:
1003 // "If you open up a lnk file, you'll see a
1004 // number, followed by a pound sign (#), followed by more text. The
1005 // number is the number of characters that follows the pound sign. The
1006 // characters after the pound sign are the command line (which _can_
1007 // include arguments) to be executed. Any path (e.g. \windows\program
1008 // files\myapp.exe) that includes spaces needs to be enclosed in
1009 // quotation marks."
1011 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
1012 // The following lines are necessary under WinCE
1013 // #include "wx/msw/private.h"
1014 // #include <ole2.h>
1016 #if defined(__WXWINCE__)
1017 #include <shlguid.h>
1020 bool wxFileName::GetShortcutTarget(const wxString
& shortcutPath
, wxString
& targetFilename
, wxString
* arguments
)
1022 wxString path
, file
, ext
;
1023 wxSplitPath(shortcutPath
, & path
, & file
, & ext
);
1027 bool success
= false;
1029 // Assume it's not a shortcut if it doesn't end with lnk
1030 if (ext
.Lower() != wxT("lnk"))
1033 // create a ShellLink object
1034 hres
= CoCreateInstance(CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
,
1035 IID_IShellLink
, (LPVOID
*) &psl
);
1037 if (SUCCEEDED(hres
))
1040 hres
= psl
->QueryInterface( IID_IPersistFile
, (LPVOID
*) &ppf
);
1041 if (SUCCEEDED(hres
))
1043 WCHAR wsz
[MAX_PATH
];
1045 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, shortcutPath
.mb_str(), -1, wsz
,
1048 hres
= ppf
->Load(wsz
, 0);
1049 if (SUCCEEDED(hres
))
1052 psl
->GetPath(buf
, 2048, NULL
, SLGP_UNCPRIORITY
);
1053 targetFilename
= wxString(buf
);
1054 success
= (shortcutPath
!= targetFilename
);
1056 psl
->GetArguments(buf
, 2048);
1058 if (!args
.IsEmpty() && arguments
)
1071 // ----------------------------------------------------------------------------
1072 // absolute/relative paths
1073 // ----------------------------------------------------------------------------
1075 bool wxFileName::IsAbsolute(wxPathFormat format
) const
1077 // if our path doesn't start with a path separator, it's not an absolute
1082 if ( !GetVolumeSeparator(format
).empty() )
1084 // this format has volumes and an absolute path must have one, it's not
1085 // enough to have the full path to bean absolute file under Windows
1086 if ( GetVolume().empty() )
1093 bool wxFileName::MakeRelativeTo(const wxString
& pathBase
, wxPathFormat format
)
1095 wxFileName fnBase
= wxFileName::DirName(pathBase
, format
);
1097 // get cwd only once - small time saving
1098 wxString cwd
= wxGetCwd();
1099 Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1100 fnBase
.Normalize(wxPATH_NORM_ALL
& ~wxPATH_NORM_CASE
, cwd
, format
);
1102 bool withCase
= IsCaseSensitive(format
);
1104 // we can't do anything if the files live on different volumes
1105 if ( !GetVolume().IsSameAs(fnBase
.GetVolume(), withCase
) )
1111 // same drive, so we don't need our volume
1114 // remove common directories starting at the top
1115 while ( !m_dirs
.IsEmpty() && !fnBase
.m_dirs
.IsEmpty() &&
1116 m_dirs
[0u].IsSameAs(fnBase
.m_dirs
[0u], withCase
) )
1119 fnBase
.m_dirs
.RemoveAt(0);
1122 // add as many ".." as needed
1123 size_t count
= fnBase
.m_dirs
.GetCount();
1124 for ( size_t i
= 0; i
< count
; i
++ )
1126 m_dirs
.Insert(wxT(".."), 0u);
1129 if ( format
== wxPATH_UNIX
|| format
== wxPATH_DOS
)
1131 // a directory made relative with respect to itself is '.' under Unix
1132 // and DOS, by definition (but we don't have to insert "./" for the
1134 if ( m_dirs
.IsEmpty() && IsDir() )
1136 m_dirs
.Add(_T('.'));
1146 // ----------------------------------------------------------------------------
1147 // filename kind tests
1148 // ----------------------------------------------------------------------------
1150 bool wxFileName::SameAs(const wxFileName
& filepath
, wxPathFormat format
) const
1152 wxFileName fn1
= *this,
1155 // get cwd only once - small time saving
1156 wxString cwd
= wxGetCwd();
1157 fn1
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1158 fn2
.Normalize(wxPATH_NORM_ALL
| wxPATH_NORM_CASE
, cwd
, format
);
1160 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
1163 // TODO: compare inodes for Unix, this works even when filenames are
1164 // different but files are the same (symlinks) (VZ)
1170 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
1172 // only Unix filenames are truely case-sensitive
1173 return GetFormat(format
) == wxPATH_UNIX
;
1177 wxString
wxFileName::GetForbiddenChars(wxPathFormat format
)
1179 // Inits to forbidden characters that are common to (almost) all platforms.
1180 wxString strForbiddenChars
= wxT("*?");
1182 // If asserts, wxPathFormat has been changed. In case of a new path format
1183 // addition, the following code might have to be updated.
1184 wxCOMPILE_TIME_ASSERT(wxPATH_MAX
== 5, wxPathFormatChanged
);
1185 switch ( GetFormat(format
) )
1188 wxFAIL_MSG( wxT("Unknown path format") );
1189 // !! Fall through !!
1195 // On a Mac even names with * and ? are allowed (Tested with OS
1196 // 9.2.1 and OS X 10.2.5)
1197 strForbiddenChars
= wxEmptyString
;
1201 strForbiddenChars
+= wxT("\\/:\"<>|");
1208 return strForbiddenChars
;
1212 wxString
wxFileName::GetVolumeSeparator(wxPathFormat format
)
1216 if ( (GetFormat(format
) == wxPATH_DOS
) ||
1217 (GetFormat(format
) == wxPATH_VMS
) )
1219 sepVol
= wxFILE_SEP_DSK
;
1227 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
1230 switch ( GetFormat(format
) )
1233 // accept both as native APIs do but put the native one first as
1234 // this is the one we use in GetFullPath()
1235 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
1239 wxFAIL_MSG( _T("Unknown wxPATH_XXX style") );
1243 seps
= wxFILE_SEP_PATH_UNIX
;
1247 seps
= wxFILE_SEP_PATH_MAC
;
1251 seps
= wxFILE_SEP_PATH_VMS
;
1259 wxString
wxFileName::GetPathTerminators(wxPathFormat format
)
1261 format
= GetFormat(format
);
1263 // under VMS the end of the path is ']', not the path separator used to
1264 // separate the components
1265 return format
== wxPATH_VMS
? wxString(_T(']')) : GetPathSeparators(format
);
1269 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
1271 // wxString::Find() doesn't work as expected with NUL - it will always find
1272 // it, so it is almost surely a bug if this function is called with NUL arg
1273 wxASSERT_MSG( ch
!= _T('\0'), _T("shouldn't be called with NUL") );
1275 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
1278 // ----------------------------------------------------------------------------
1279 // path components manipulation
1280 // ----------------------------------------------------------------------------
1282 /* static */ bool wxFileName::IsValidDirComponent(const wxString
& dir
)
1286 wxFAIL_MSG( _T("empty directory passed to wxFileName::InsertDir()") );
1291 const size_t len
= dir
.length();
1292 for ( size_t n
= 0; n
< len
; n
++ )
1294 if ( dir
[n
] == GetVolumeSeparator() || IsPathSeparator(dir
[n
]) )
1296 wxFAIL_MSG( _T("invalid directory component in wxFileName") );
1305 void wxFileName::AppendDir( const wxString
&dir
)
1307 if ( IsValidDirComponent(dir
) )
1311 void wxFileName::PrependDir( const wxString
&dir
)
1316 void wxFileName::InsertDir( int before
, const wxString
&dir
)
1318 if ( IsValidDirComponent(dir
) )
1319 m_dirs
.Insert( dir
, before
);
1322 void wxFileName::RemoveDir( int pos
)
1324 m_dirs
.RemoveAt( (size_t)pos
);
1327 // ----------------------------------------------------------------------------
1329 // ----------------------------------------------------------------------------
1331 void wxFileName::SetFullName(const wxString
& fullname
)
1333 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
1336 wxString
wxFileName::GetFullName() const
1338 wxString fullname
= m_name
;
1339 if ( !m_ext
.empty() )
1341 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
1347 wxString
wxFileName::GetPath( int flags
, wxPathFormat format
) const
1349 format
= GetFormat( format
);
1353 // return the volume with the path as well if requested
1354 if ( flags
& wxPATH_GET_VOLUME
)
1356 fullpath
+= wxGetVolumeString(GetVolume(), format
);
1359 // the leading character
1364 fullpath
+= wxFILE_SEP_PATH_MAC
;
1369 fullpath
+= wxFILE_SEP_PATH_DOS
;
1373 wxFAIL_MSG( wxT("Unknown path format") );
1379 // normally the absolute file names start with a slash
1380 // with one exception: the ones like "~/foo.bar" don't
1382 if ( m_dirs
.IsEmpty() || m_dirs
[0u] != _T('~') )
1384 fullpath
+= wxFILE_SEP_PATH_UNIX
;
1390 // no leading character here but use this place to unset
1391 // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense
1392 // as, if I understand correctly, there should never be a dot
1393 // before the closing bracket
1394 flags
&= ~wxPATH_GET_SEPARATOR
;
1397 if ( m_dirs
.empty() )
1399 // there is nothing more
1403 // then concatenate all the path components using the path separator
1404 if ( format
== wxPATH_VMS
)
1406 fullpath
+= wxT('[');
1409 const size_t dirCount
= m_dirs
.GetCount();
1410 for ( size_t i
= 0; i
< dirCount
; i
++ )
1415 if ( m_dirs
[i
] == wxT(".") )
1417 // skip appending ':', this shouldn't be done in this
1418 // case as "::" is interpreted as ".." under Unix
1422 // convert back from ".." to nothing
1423 if ( m_dirs
[i
] != wxT("..") )
1424 fullpath
+= m_dirs
[i
];
1428 wxFAIL_MSG( wxT("Unexpected path format") );
1429 // still fall through
1433 fullpath
+= m_dirs
[i
];
1437 // TODO: What to do with ".." under VMS
1439 // convert back from ".." to nothing
1440 if ( m_dirs
[i
] != wxT("..") )
1441 fullpath
+= m_dirs
[i
];
1445 if ( (flags
& wxPATH_GET_SEPARATOR
) || (i
!= dirCount
- 1) )
1446 fullpath
+= GetPathSeparator(format
);
1449 if ( format
== wxPATH_VMS
)
1451 fullpath
+= wxT(']');
1457 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
1459 // we already have a function to get the path
1460 wxString fullpath
= GetPath(wxPATH_GET_VOLUME
| wxPATH_GET_SEPARATOR
,
1463 // now just add the file name and extension to it
1464 fullpath
+= GetFullName();
1469 // Return the short form of the path (returns identity on non-Windows platforms)
1470 wxString
wxFileName::GetShortPath() const
1472 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
1473 wxString
path(GetFullPath());
1475 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
1479 ok
= ::GetShortPathName
1482 wxStringBuffer(pathOut
, sz
),
1491 return GetFullPath();
1495 // Return the long form of the path (returns identity on non-Windows platforms)
1496 wxString
wxFileName::GetLongPath() const
1499 path
= GetFullPath();
1501 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1502 bool success
= false;
1504 #if wxUSE_DYNAMIC_LOADER
1505 typedef DWORD (WINAPI
*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
1507 static bool s_triedToLoad
= false;
1509 if ( !s_triedToLoad
)
1511 // suppress the errors about missing GetLongPathName[AW]
1514 s_triedToLoad
= true;
1515 wxDynamicLibrary
dllKernel(_T("kernel32"));
1516 if ( dllKernel
.IsLoaded() )
1518 // may succeed or fail depending on the Windows version
1519 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
1521 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) dllKernel
.GetSymbol(_T("GetLongPathNameW"));
1523 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) dllKernel
.GetSymbol(_T("GetLongPathNameA"));
1526 if ( s_pfnGetLongPathName
)
1528 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1529 bool ok
= dwSize
> 0;
1533 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
1537 ok
= (*s_pfnGetLongPathName
)
1540 wxStringBuffer(pathOut
, sz
),
1552 #endif // wxUSE_DYNAMIC_LOADER
1556 // The OS didn't support GetLongPathName, or some other error.
1557 // We need to call FindFirstFile on each component in turn.
1559 WIN32_FIND_DATA findFileData
;
1563 pathOut
= GetVolume() +
1564 GetVolumeSeparator(wxPATH_DOS
) +
1565 GetPathSeparator(wxPATH_DOS
);
1567 pathOut
= wxEmptyString
;
1569 wxArrayString dirs
= GetDirs();
1570 dirs
.Add(GetFullName());
1574 size_t count
= dirs
.GetCount();
1575 for ( size_t i
= 0; i
< count
; i
++ )
1577 // We're using pathOut to collect the long-name path, but using a
1578 // temporary for appending the last path component which may be
1580 tmpPath
= pathOut
+ dirs
[i
];
1582 if ( tmpPath
.empty() )
1585 // can't see this being necessary? MF
1586 if ( tmpPath
.Last() == GetVolumeSeparator(wxPATH_DOS
) )
1588 // Can't pass a drive and root dir to FindFirstFile,
1589 // so continue to next dir
1590 tmpPath
+= wxFILE_SEP_PATH
;
1595 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
1596 if (hFind
== INVALID_HANDLE_VALUE
)
1598 // Error: most likely reason is that path doesn't exist, so
1599 // append any unprocessed parts and return
1600 for ( i
+= 1; i
< count
; i
++ )
1601 tmpPath
+= wxFILE_SEP_PATH
+ dirs
[i
];
1606 pathOut
+= findFileData
.cFileName
;
1607 if ( (i
< (count
-1)) )
1608 pathOut
+= wxFILE_SEP_PATH
;
1615 #endif // Win32/!Win32
1620 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
1622 if (format
== wxPATH_NATIVE
)
1624 #if defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__)
1625 format
= wxPATH_DOS
;
1626 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1627 format
= wxPATH_MAC
;
1628 #elif defined(__VMS)
1629 format
= wxPATH_VMS
;
1631 format
= wxPATH_UNIX
;
1637 // ----------------------------------------------------------------------------
1638 // path splitting function
1639 // ----------------------------------------------------------------------------
1643 wxFileName::SplitVolume(const wxString
& fullpathWithVolume
,
1644 wxString
*pstrVolume
,
1646 wxPathFormat format
)
1648 format
= GetFormat(format
);
1650 wxString fullpath
= fullpathWithVolume
;
1652 // special Windows UNC paths hack: transform \\share\path into share:path
1653 if ( format
== wxPATH_DOS
)
1655 if ( fullpath
.length() >= 4 &&
1656 fullpath
[0u] == wxFILE_SEP_PATH_DOS
&&
1657 fullpath
[1u] == wxFILE_SEP_PATH_DOS
)
1659 fullpath
.erase(0, 2);
1661 size_t posFirstSlash
=
1662 fullpath
.find_first_of(GetPathTerminators(format
));
1663 if ( posFirstSlash
!= wxString::npos
)
1665 fullpath
[posFirstSlash
] = wxFILE_SEP_DSK
;
1667 // UNC paths are always absolute, right? (FIXME)
1668 fullpath
.insert(posFirstSlash
+ 1, 1, wxFILE_SEP_PATH_DOS
);
1673 // We separate the volume here
1674 if ( format
== wxPATH_DOS
|| format
== wxPATH_VMS
)
1676 wxString sepVol
= GetVolumeSeparator(format
);
1678 size_t posFirstColon
= fullpath
.find_first_of(sepVol
);
1679 if ( posFirstColon
!= wxString::npos
)
1683 *pstrVolume
= fullpath
.Left(posFirstColon
);
1686 // remove the volume name and the separator from the full path
1687 fullpath
.erase(0, posFirstColon
+ sepVol
.length());
1692 *pstrPath
= fullpath
;
1696 void wxFileName::SplitPath(const wxString
& fullpathWithVolume
,
1697 wxString
*pstrVolume
,
1701 wxPathFormat format
)
1703 format
= GetFormat(format
);
1706 SplitVolume(fullpathWithVolume
, pstrVolume
, &fullpath
, format
);
1708 // find the positions of the last dot and last path separator in the path
1709 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
1710 size_t posLastSlash
= fullpath
.find_last_of(GetPathTerminators(format
));
1712 // check whether this dot occurs at the very beginning of a path component
1713 if ( (posLastDot
!= wxString::npos
) &&
1715 IsPathSeparator(fullpath
[posLastDot
- 1]) ||
1716 (format
== wxPATH_VMS
&& fullpath
[posLastDot
- 1] == _T(']'))) )
1718 // dot may be (and commonly -- at least under Unix -- is) the first
1719 // character of the filename, don't treat the entire filename as
1720 // extension in this case
1721 posLastDot
= wxString::npos
;
1724 // if we do have a dot and a slash, check that the dot is in the name part
1725 if ( (posLastDot
!= wxString::npos
) &&
1726 (posLastSlash
!= wxString::npos
) &&
1727 (posLastDot
< posLastSlash
) )
1729 // the dot is part of the path, not the start of the extension
1730 posLastDot
= wxString::npos
;
1733 // now fill in the variables provided by user
1736 if ( posLastSlash
== wxString::npos
)
1743 // take everything up to the path separator but take care to make
1744 // the path equal to something like '/', not empty, for the files
1745 // immediately under root directory
1746 size_t len
= posLastSlash
;
1748 // this rule does not apply to mac since we do not start with colons (sep)
1749 // except for relative paths
1750 if ( !len
&& format
!= wxPATH_MAC
)
1753 *pstrPath
= fullpath
.Left(len
);
1755 // special VMS hack: remove the initial bracket
1756 if ( format
== wxPATH_VMS
)
1758 if ( (*pstrPath
)[0u] == _T('[') )
1759 pstrPath
->erase(0, 1);
1766 // take all characters starting from the one after the last slash and
1767 // up to, but excluding, the last dot
1768 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
1770 if ( posLastDot
== wxString::npos
)
1772 // take all until the end
1773 count
= wxString::npos
;
1775 else if ( posLastSlash
== wxString::npos
)
1779 else // have both dot and slash
1781 count
= posLastDot
- posLastSlash
- 1;
1784 *pstrName
= fullpath
.Mid(nStart
, count
);
1789 if ( posLastDot
== wxString::npos
)
1796 // take everything after the dot
1797 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
1803 void wxFileName::SplitPath(const wxString
& fullpath
,
1807 wxPathFormat format
)
1810 SplitPath(fullpath
, &volume
, path
, name
, ext
, format
);
1814 path
->Prepend(wxGetVolumeString(volume
, format
));
1818 // ----------------------------------------------------------------------------
1820 // ----------------------------------------------------------------------------
1824 bool wxFileName::SetTimes(const wxDateTime
*dtAccess
,
1825 const wxDateTime
*dtMod
,
1826 const wxDateTime
*dtCreate
)
1828 #if defined(__WIN32__)
1831 // VZ: please let me know how to do this if you can
1832 wxFAIL_MSG( _T("SetTimes() not implemented for the directories") );
1836 wxFileHandle
fh(GetFullPath(), wxFileHandle::Write
);
1839 FILETIME ftAccess
, ftCreate
, ftWrite
;
1842 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
1844 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
1846 ConvertWxToFileTime(&ftWrite
, *dtMod
);
1848 if ( ::SetFileTime(fh
,
1849 dtCreate
? &ftCreate
: NULL
,
1850 dtAccess
? &ftAccess
: NULL
,
1851 dtMod
? &ftWrite
: NULL
) )
1857 #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__))
1858 if ( !dtAccess
&& !dtMod
)
1860 // can't modify the creation time anyhow, don't try
1864 // if dtAccess or dtMod is not specified, use the other one (which must be
1865 // non NULL because of the test above) for both times
1867 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
1868 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
1869 if ( utime(GetFullPath().fn_str(), &utm
) == 0 )
1873 #else // other platform
1876 wxLogSysError(_("Failed to modify file times for '%s'"),
1877 GetFullPath().c_str());
1882 bool wxFileName::Touch()
1884 #if defined(__UNIX_LIKE__)
1885 // under Unix touching file is simple: just pass NULL to utime()
1886 if ( utime(GetFullPath().fn_str(), NULL
) == 0 )
1891 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1894 #else // other platform
1895 wxDateTime dtNow
= wxDateTime::Now();
1897 return SetTimes(&dtNow
, &dtNow
, NULL
/* don't change create time */);
1901 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
1903 wxDateTime
*dtCreate
) const
1905 #if defined(__WIN32__)
1906 // we must use different methods for the files and directories under
1907 // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and
1908 // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and
1911 FILETIME ftAccess
, ftCreate
, ftWrite
;
1914 // implemented in msw/dir.cpp
1915 extern bool wxGetDirectoryTimes(const wxString
& dirname
,
1916 FILETIME
*, FILETIME
*, FILETIME
*);
1918 // we should pass the path without the trailing separator to
1919 // wxGetDirectoryTimes()
1920 ok
= wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME
),
1921 &ftAccess
, &ftCreate
, &ftWrite
);
1925 wxFileHandle
fh(GetFullPath(), wxFileHandle::Read
);
1928 ok
= ::GetFileTime(fh
,
1929 dtCreate
? &ftCreate
: NULL
,
1930 dtAccess
? &ftAccess
: NULL
,
1931 dtMod
? &ftWrite
: NULL
) != 0;
1942 ConvertFileTimeToWx(dtCreate
, ftCreate
);
1944 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1946 ConvertFileTimeToWx(dtMod
, ftWrite
);
1950 #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__))
1952 if ( wxStat( GetFullPath().c_str(), &stBuf
) == 0 )
1955 dtAccess
->Set(stBuf
.st_atime
);
1957 dtMod
->Set(stBuf
.st_mtime
);
1959 dtCreate
->Set(stBuf
.st_ctime
);
1963 #else // other platform
1966 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1967 GetFullPath().c_str());
1972 #endif // wxUSE_DATETIME
1976 const short kMacExtensionMaxLength
= 16 ;
1977 class MacDefaultExtensionRecord
1980 MacDefaultExtensionRecord()
1983 m_type
= m_creator
= NULL
;
1985 MacDefaultExtensionRecord( const MacDefaultExtensionRecord
& from
)
1987 wxStrcpy( m_ext
, from
.m_ext
) ;
1988 m_type
= from
.m_type
;
1989 m_creator
= from
.m_creator
;
1991 MacDefaultExtensionRecord( const wxChar
* extension
, OSType type
, OSType creator
)
1993 wxStrncpy( m_ext
, extension
, kMacExtensionMaxLength
) ;
1994 m_ext
[kMacExtensionMaxLength
] = 0 ;
1996 m_creator
= creator
;
1998 wxChar m_ext
[kMacExtensionMaxLength
] ;
2003 #include "wx/dynarray.h"
2004 WX_DECLARE_OBJARRAY(MacDefaultExtensionRecord
, MacDefaultExtensionArray
) ;
2006 bool gMacDefaultExtensionsInited
= false ;
2008 #include "wx/arrimpl.cpp"
2010 WX_DEFINE_EXPORTED_OBJARRAY(MacDefaultExtensionArray
) ;
2012 MacDefaultExtensionArray gMacDefaultExtensions
;
2014 // load the default extensions
2015 MacDefaultExtensionRecord gDefaults
[] =
2017 MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) ,
2018 MacDefaultExtensionRecord( wxT("tif") , 'TIFF' , '****' ) ,
2019 MacDefaultExtensionRecord( wxT("jpg") , 'JPEG' , '****' ) ,
2022 static void MacEnsureDefaultExtensionsLoaded()
2024 if ( !gMacDefaultExtensionsInited
)
2026 // we could load the pc exchange prefs here too
2027 for ( size_t i
= 0 ; i
< WXSIZEOF( gDefaults
) ; ++i
)
2029 gMacDefaultExtensions
.Add( gDefaults
[i
] ) ;
2031 gMacDefaultExtensionsInited
= true ;
2035 bool wxFileName::MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
)
2038 FSCatalogInfo catInfo
;
2041 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2043 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2045 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2046 finfo
->fileType
= type
;
2047 finfo
->fileCreator
= creator
;
2048 FSSetCatalogInfo( &fsRef
, kFSCatInfoFinderInfo
, &catInfo
) ;
2055 bool wxFileName::MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
)
2058 FSCatalogInfo catInfo
;
2061 if ( wxMacPathToFSRef( GetFullPath() , &fsRef
) == noErr
)
2063 if ( FSGetCatalogInfo (&fsRef
, kFSCatInfoFinderInfo
, &catInfo
, NULL
, NULL
, NULL
) == noErr
)
2065 finfo
= (FileInfo
*)&catInfo
.finderInfo
;
2066 *type
= finfo
->fileType
;
2067 *creator
= finfo
->fileCreator
;
2074 bool wxFileName::MacSetDefaultTypeAndCreator()
2076 wxUint32 type
, creator
;
2077 if ( wxFileName::MacFindDefaultTypeAndCreator(GetExt() , &type
,
2080 return MacSetTypeAndCreator( type
, creator
) ;
2085 bool wxFileName::MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
)
2087 MacEnsureDefaultExtensionsLoaded() ;
2088 wxString extl
= ext
.Lower() ;
2089 for( int i
= gMacDefaultExtensions
.Count() - 1 ; i
>= 0 ; --i
)
2091 if ( gMacDefaultExtensions
.Item(i
).m_ext
== extl
)
2093 *type
= gMacDefaultExtensions
.Item(i
).m_type
;
2094 *creator
= gMacDefaultExtensions
.Item(i
).m_creator
;
2101 void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
)
2103 MacEnsureDefaultExtensionsLoaded() ;
2104 MacDefaultExtensionRecord rec
;
2106 rec
.m_creator
= creator
;
2107 wxStrncpy( rec
.m_ext
, ext
.Lower().c_str() , kMacExtensionMaxLength
) ;
2108 gMacDefaultExtensions
.Add( rec
) ;