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 license
10 /////////////////////////////////////////////////////////////////////////////
13 Here are brief descriptions of the filename formats supported by this class:
15 wxPATH_UNIX: standard Unix format, absolute file names have the form
16 /dir1/dir2/.../dirN/filename, "." and ".." stand for the
17 current and parent directory respectively, "~" is parsed as the
18 user HOME and "~username" as the HOME of that user
20 wxPATH_DOS: DOS/Windows format, absolute file names have the form
21 drive:\dir1\dir2\...\dirN\filename.ext where drive is a single
22 letter. "." and ".." as for Unix but no "~".
24 There are also UNC names of the form \\share\fullpath
26 wxPATH_MAC: Mac OS 8/9 format, absolute file names have the form
27 volume:dir1:...:dirN:filename
28 and the relative file names are either
29 :dir1:...:dirN:filename
32 (although :filename works as well).
34 wxPATH_VMS: VMS native format, absolute file names have the form
35 <device>:[dir1.dir2.dir3]file.txt
37 <device>:[000000.dir1.dir2.dir3]file.txt
39 the <device> is the physical device (i.e. disk). 000000 is the
40 root directory on the device which can be omitted.
42 Note that VMS uses different separators unlike Unix:
43 : always after the device. If the path does not contain : than
44 the default (the device of the current directory) is assumed.
45 [ start of directory specyfication
46 . separator between directory and subdirectory
47 ] between directory and file
50 // ============================================================================
52 // ============================================================================
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
59 #pragma implementation "filename.h"
62 // For compilers that support precompilation, includes "wx.h".
63 #include "wx/wxprec.h"
74 #include "wx/filename.h"
75 #include "wx/tokenzr.h"
76 #include "wx/config.h" // for wxExpandEnvVars
79 #if wxUSE_DYNLIB_CLASS
80 #include "wx/dynlib.h"
83 // For GetShort/LongPathName
87 #include "wx/msw/winundef.h"
90 // utime() is POSIX so should normally be available on all Unices
92 #include <sys/types.h>
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 // small helper class which opens and closes the file - we use it just to get
109 // a file handle for the given file name to pass it to some Win32 API function
110 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
115 wxFileHandle(const wxString
& filename
)
117 m_hFile
= ::CreateFile
120 GENERIC_READ
, // access mask
122 NULL
, // no secutity attr
123 OPEN_EXISTING
, // creation disposition
125 NULL
// no template file
128 if ( m_hFile
== INVALID_HANDLE_VALUE
)
130 wxLogSysError(_("Failed to open '%s' for reading"),
137 if ( m_hFile
!= INVALID_HANDLE_VALUE
)
139 if ( !::CloseHandle(m_hFile
) )
141 wxLogSysError(_("Failed to close file handle"));
146 // return TRUE only if the file could be opened successfully
147 bool IsOk() const { return m_hFile
!= INVALID_HANDLE_VALUE
; }
150 operator HANDLE() const { return m_hFile
; }
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
164 // convert between wxDateTime and FILETIME which is a 64-bit value representing
165 // the number of 100-nanosecond intervals since January 1, 1601.
167 // the number of milliseconds between the Unix Epoch (January 1, 1970) and the
168 // FILETIME reference point (January 1, 1601)
169 static const wxLongLong FILETIME_EPOCH_OFFSET
= wxLongLong(0xa97, 0x30b66800);
171 static void ConvertFileTimeToWx(wxDateTime
*dt
, const FILETIME
&ft
)
173 wxLongLong
ll(ft
.dwHighDateTime
, ft
.dwLowDateTime
);
175 // convert 100ns to ms
178 // move it to our Epoch
179 ll
-= FILETIME_EPOCH_OFFSET
;
181 *dt
= wxDateTime(ll
);
184 static void ConvertWxToFileTime(FILETIME
*ft
, const wxDateTime
& dt
)
186 // do the reverse of ConvertFileTimeToWx()
187 wxLongLong ll
= dt
.GetValue();
189 ll
+= FILETIME_EPOCH_OFFSET
;
191 ft
->dwHighDateTime
= ll
.GetHi();
192 ft
->dwLowDateTime
= ll
.GetLo();
197 // ============================================================================
199 // ============================================================================
201 // ----------------------------------------------------------------------------
202 // wxFileName construction
203 // ----------------------------------------------------------------------------
205 void wxFileName::Assign( const wxFileName
&filepath
)
207 m_volume
= filepath
.GetVolume();
208 m_dirs
= filepath
.GetDirs();
209 m_name
= filepath
.GetName();
210 m_ext
= filepath
.GetExt();
213 void wxFileName::Assign(const wxString
& volume
,
214 const wxString
& path
,
215 const wxString
& name
,
217 wxPathFormat format
)
219 wxStringTokenizer
tn(path
, GetPathSeparators(format
));
222 while ( tn
.HasMoreTokens() )
224 wxString token
= tn
.GetNextToken();
226 // if the path starts with a slash, we do need the first empty dir
227 // entry to be able to tell later that it was an absolute path, but
228 // otherwise ignore the double slashes
229 if ( m_dirs
.IsEmpty() || !token
.IsEmpty() )
238 void wxFileName::Assign(const wxString
& fullpath
,
241 wxString volume
, path
, name
, ext
;
242 SplitPath(fullpath
, &volume
, &path
, &name
, &ext
, format
);
244 Assign(volume
, path
, name
, ext
, format
);
247 void wxFileName::Assign(const wxString
& path
,
248 const wxString
& fullname
,
252 SplitPath(fullname
, NULL
/* no path */, &name
, &ext
, format
);
254 Assign(path
, name
, ext
, format
);
257 void wxFileName::Clear()
263 m_ext
= wxEmptyString
;
267 wxFileName
wxFileName::FileName(const wxString
& file
)
269 return wxFileName(file
);
273 wxFileName
wxFileName::DirName(const wxString
& dir
)
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 bool wxFileName::FileExists()
286 return wxFileName::FileExists( GetFullPath() );
289 bool wxFileName::FileExists( const wxString
&file
)
291 return ::wxFileExists( file
);
294 bool wxFileName::DirExists()
296 return wxFileName::DirExists( GetFullPath() );
299 bool wxFileName::DirExists( const wxString
&dir
)
301 return ::wxDirExists( dir
);
304 // ----------------------------------------------------------------------------
305 // CWD and HOME stuff
306 // ----------------------------------------------------------------------------
308 void wxFileName::AssignCwd()
310 AssignDir(wxFileName::GetCwd());
314 wxString
wxFileName::GetCwd()
319 bool wxFileName::SetCwd()
321 return wxFileName::SetCwd( GetFullPath() );
324 bool wxFileName::SetCwd( const wxString
&cwd
)
326 return ::wxSetWorkingDirectory( cwd
);
329 void wxFileName::AssignHomeDir()
331 AssignDir(wxFileName::GetHomeDir());
334 wxString
wxFileName::GetHomeDir()
336 return ::wxGetHomeDir();
339 void wxFileName::AssignTempFileName( const wxString
&prefix
)
342 if ( wxGetTempFileName(prefix
, fullname
) )
352 // ----------------------------------------------------------------------------
353 // directory operations
354 // ----------------------------------------------------------------------------
356 bool wxFileName::Mkdir( int perm
, bool full
)
358 return wxFileName::Mkdir( GetFullPath(), perm
, full
);
361 bool wxFileName::Mkdir( const wxString
&dir
, int perm
, bool full
)
365 wxFileName
filename(dir
);
366 wxArrayString dirs
= filename
.GetDirs();
367 dirs
.Add(filename
.GetName());
369 size_t count
= dirs
.GetCount();
373 for ( i
= 0; i
< count
; i
++ )
377 if (currPath
.Last() == wxT(':'))
379 // Can't create a root directory so continue to next dir
380 currPath
+= wxFILE_SEP_PATH
;
384 if (!DirExists(currPath
))
385 if (!wxMkdir(currPath
, perm
))
388 if ( (i
< (count
-1)) )
389 currPath
+= wxFILE_SEP_PATH
;
392 return (noErrors
== 0);
396 return ::wxMkdir( dir
, perm
);
399 bool wxFileName::Rmdir()
401 return wxFileName::Rmdir( GetFullPath() );
404 bool wxFileName::Rmdir( const wxString
&dir
)
406 return ::wxRmdir( dir
);
409 // ----------------------------------------------------------------------------
410 // path normalization
411 // ----------------------------------------------------------------------------
413 bool wxFileName::Normalize(wxPathNormalize flags
,
417 // the existing path components
418 wxArrayString dirs
= GetDirs();
420 // the path to prepend in front to make the path absolute
423 format
= GetFormat(format
);
425 // make the path absolute
426 if ( (flags
& wxPATH_NORM_ABSOLUTE
) && !IsAbsolute() )
431 curDir
.AssignDir(cwd
);
434 // handle ~ stuff under Unix only
435 if ( (format
== wxPATH_UNIX
) && (flags
& wxPATH_NORM_TILDE
) )
437 if ( !dirs
.IsEmpty() )
439 wxString dir
= dirs
[0u];
440 if ( !dir
.empty() && dir
[0u] == _T('~') )
442 curDir
.AssignDir(wxGetUserHome(dir
.c_str() + 1));
451 wxArrayString dirsNew
= curDir
.GetDirs();
452 size_t count
= dirs
.GetCount();
453 for ( size_t n
= 0; n
< count
; n
++ )
455 dirsNew
.Add(dirs
[n
]);
461 // now deal with ".", ".." and the rest
463 size_t count
= dirs
.GetCount();
464 for ( size_t n
= 0; n
< count
; n
++ )
466 wxString dir
= dirs
[n
];
468 if ( flags
&& wxPATH_NORM_DOTS
)
470 if ( dir
== wxT(".") )
476 if ( dir
== wxT("..") )
478 if ( m_dirs
.IsEmpty() )
480 wxLogError(_("The path '%s' contains too many \"..\"!"),
481 GetFullPath().c_str());
485 m_dirs
.Remove(m_dirs
.GetCount() - 1);
490 if ( flags
& wxPATH_NORM_ENV_VARS
)
492 dir
= wxExpandEnvVars(dir
);
495 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
503 if ( (flags
& wxPATH_NORM_CASE
) && !IsCaseSensitive(format
) )
505 // VZ: expand env vars here too?
511 #if defined(__WXMSW__) && defined(__WIN32__)
512 if (flags
& wxPATH_NORM_LONG
)
514 Assign(GetLongPath());
521 // ----------------------------------------------------------------------------
522 // filename kind tests
523 // ----------------------------------------------------------------------------
525 bool wxFileName::SameAs( const wxFileName
&filepath
, wxPathFormat format
)
527 wxFileName fn1
= *this,
530 // get cwd only once - small time saving
531 wxString cwd
= wxGetCwd();
532 fn1
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
533 fn2
.Normalize(wxPATH_NORM_ALL
, cwd
, format
);
535 if ( fn1
.GetFullPath() == fn2
.GetFullPath() )
538 // TODO: compare inodes for Unix, this works even when filenames are
539 // different but files are the same (symlinks) (VZ)
545 bool wxFileName::IsCaseSensitive( wxPathFormat format
)
547 // only Unix filenames are truely case-sensitive
548 return GetFormat(format
) == wxPATH_UNIX
;
551 bool wxFileName::IsAbsolute( wxPathFormat format
)
553 // if we have no path, we can't be an abs filename
554 if ( m_dirs
.IsEmpty() )
559 switch ( GetFormat(format
) )
564 // must have the drive
565 return !m_volume
.empty();
568 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
572 const wxString
& str
= m_dirs
[0u];
575 // the path started with '/', it's an absolute one
579 // the path is absolute if it starts with a path separator or
580 // with "~" or "~user"
583 return IsPathSeparator(ch
, format
) || ch
== _T('~');
588 wxString
wxFileName::GetVolumeSeparator(wxPathFormat format
)
592 if ( GetFormat(format
) != wxPATH_UNIX
)
594 // so far it is the same for all systems which have it
595 sepVol
= wxFILE_SEP_DSK
;
597 //else: leave empty, no volume separators under Unix
603 wxString
wxFileName::GetPathSeparators(wxPathFormat format
)
606 switch ( GetFormat(format
) )
609 // accept both as native APIs do but put the native one first as
610 // this is the one we use in GetFullPath()
611 seps
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_UNIX
;
615 wxFAIL_MSG( _T("unknown wxPATH_XXX style") );
619 seps
= wxFILE_SEP_PATH_UNIX
;
623 seps
= wxFILE_SEP_PATH_MAC
;
627 seps
= wxFILE_SEP_PATH_VMS
;
635 bool wxFileName::IsPathSeparator(wxChar ch
, wxPathFormat format
)
637 // wxString::Find() doesn't work as expected with NUL - it will always find
638 // it, so it is almost surely a bug if this function is called with NUL arg
639 wxASSERT_MSG( ch
!= _T('\0'), _T("shouldn't be called with NUL") );
641 return GetPathSeparators(format
).Find(ch
) != wxNOT_FOUND
;
644 bool wxFileName::IsWild( wxPathFormat format
)
646 // FIXME: this is probably false for Mac and this is surely wrong for most
647 // of Unix shells (think about "[...]")
649 return m_name
.find_first_of(_T("*?")) != wxString::npos
;
652 // ----------------------------------------------------------------------------
653 // path components manipulation
654 // ----------------------------------------------------------------------------
656 void wxFileName::AppendDir( const wxString
&dir
)
661 void wxFileName::PrependDir( const wxString
&dir
)
663 m_dirs
.Insert( dir
, 0 );
666 void wxFileName::InsertDir( int before
, const wxString
&dir
)
668 m_dirs
.Insert( dir
, before
);
671 void wxFileName::RemoveDir( int pos
)
673 m_dirs
.Remove( (size_t)pos
);
676 // ----------------------------------------------------------------------------
678 // ----------------------------------------------------------------------------
680 void wxFileName::SetFullName(const wxString
& fullname
)
682 SplitPath(fullname
, NULL
/* no path */, &m_name
, &m_ext
);
685 wxString
wxFileName::GetFullName() const
687 wxString fullname
= m_name
;
688 if ( !m_ext
.empty() )
690 fullname
<< wxFILE_SEP_EXT
<< m_ext
;
696 wxString
wxFileName::GetPath( bool add_separator
, wxPathFormat format
) const
698 format
= GetFormat( format
);
701 size_t count
= m_dirs
.GetCount();
702 for ( size_t i
= 0; i
< count
; i
++ )
705 if ( add_separator
|| (i
< count
) )
706 ret
+= wxFILE_SEP_PATH
;
712 wxString
wxFileName::GetFullPath( wxPathFormat format
) const
714 format
= GetFormat(format
);
718 // first put the volume
719 if ( !m_volume
.empty() )
721 // special Windows UNC paths hack, part 2: undo what we did in
722 // SplitPath() and make an UNC path if we have a drive which is not a
723 // single letter (hopefully the network shares can't be one letter only
724 // although I didn't find any authoritative docs on this)
725 if ( format
== wxPATH_DOS
&& m_volume
.length() > 1 )
727 fullpath
<< wxFILE_SEP_PATH_DOS
<< wxFILE_SEP_PATH_DOS
<< m_volume
;
731 fullpath
<< m_volume
<< GetVolumeSeparator(format
);
735 // then concatenate all the path components using the path separator
736 size_t dirCount
= m_dirs
.GetCount();
739 // under Mac, we must have a path separator in the beginning of the
740 // relative path - otherwise it would be parsed as an absolute one
741 if ( format
== wxPATH_MAC
&& m_volume
.empty() && !m_dirs
[0].empty() )
743 fullpath
+= wxFILE_SEP_PATH_MAC
;
746 wxChar chPathSep
= GetPathSeparators(format
)[0u];
747 if ( format
== wxPATH_VMS
)
752 for ( size_t i
= 0; i
< dirCount
; i
++ )
754 // under VMS, we shouldn't have a leading dot
755 if ( i
&& (format
!= wxPATH_VMS
|| !m_dirs
[i
- 1].empty()) )
756 fullpath
+= chPathSep
;
758 fullpath
+= m_dirs
[i
];
761 if ( format
== wxPATH_VMS
)
767 // separate the file name from the last directory, notice that we
768 // intentionally do it even if the name and extension are empty as
769 // this allows us to distinguish the directories from the file
770 // names (the directories have the trailing slash)
771 fullpath
+= chPathSep
;
775 // finally add the file name and extension
776 fullpath
+= GetFullName();
781 // Return the short form of the path (returns identity on non-Windows platforms)
782 wxString
wxFileName::GetShortPath() const
784 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
785 wxString
path(GetFullPath());
787 DWORD sz
= ::GetShortPathName(path
, NULL
, 0);
791 ok
= ::GetShortPathName
794 pathOut
.GetWriteBuf(sz
),
797 pathOut
.UngetWriteBuf();
804 return GetFullPath();
808 // Return the long form of the path (returns identity on non-Windows platforms)
809 wxString
wxFileName::GetLongPath() const
811 #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__)
812 wxString
path(GetFullPath());
814 bool success
= FALSE
;
816 // VZ: this code was disabled, why?
817 #if 0 // wxUSE_DYNLIB_CLASS
818 typedef DWORD (*GET_LONG_PATH_NAME
)(const wxChar
*, wxChar
*, DWORD
);
820 static bool s_triedToLoad
= FALSE
;
822 if ( !s_triedToLoad
)
824 s_triedToLoad
= TRUE
;
825 wxDllType dllKernel
= wxDllLoader::LoadLibrary(_T("kernel32"));
828 // may succeed or fail depending on the Windows version
829 static GET_LONG_PATH_NAME s_pfnGetLongPathName
= NULL
;
831 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameW"));
833 s_pfnGetLongPathName
= (GET_LONG_PATH_NAME
) wxDllLoader::GetSymbol(dllKernel
, _T("GetLongPathNameA"));
836 wxDllLoader::UnloadLibrary(dllKernel
);
838 if ( s_pfnGetLongPathName
)
840 DWORD dwSize
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
841 bool ok
= dwSize
> 0;
845 DWORD sz
= (*s_pfnGetLongPathName
)(path
, NULL
, 0);
849 ok
= (*s_pfnGetLongPathName
)
852 pathOut
.GetWriteBuf(sz
),
855 pathOut
.UngetWriteBuf();
865 #endif // wxUSE_DYNLIB_CLASS
869 // The OS didn't support GetLongPathName, or some other error.
870 // We need to call FindFirstFile on each component in turn.
872 WIN32_FIND_DATA findFileData
;
874 pathOut
= wxEmptyString
;
876 wxArrayString dirs
= GetDirs();
877 dirs
.Add(GetFullName());
879 size_t count
= dirs
.GetCount();
883 for ( i
= 0; i
< count
; i
++ )
885 // We're using pathOut to collect the long-name path,
886 // but using a temporary for appending the last path component which may be short-name
887 tmpPath
= pathOut
+ dirs
[i
];
889 if (tmpPath
.Last() == wxT(':'))
891 // Can't pass a drive and root dir to FindFirstFile,
892 // so continue to next dir
893 tmpPath
+= wxFILE_SEP_PATH
;
898 hFind
= ::FindFirstFile(tmpPath
, &findFileData
);
899 if (hFind
== INVALID_HANDLE_VALUE
)
901 // Error: return immediately with the original path
906 pathOut
+= findFileData
.cFileName
;
907 if ( (i
< (count
-1)) )
908 pathOut
+= wxFILE_SEP_PATH
;
917 return GetFullPath();
921 wxPathFormat
wxFileName::GetFormat( wxPathFormat format
)
923 if (format
== wxPATH_NATIVE
)
925 #if defined(__WXMSW__) || defined(__WXPM__)
927 #elif defined(__WXMAC__) && !defined(__DARWIN__)
932 format
= wxPATH_UNIX
;
938 // ----------------------------------------------------------------------------
939 // path splitting function
940 // ----------------------------------------------------------------------------
942 void wxFileName::SplitPath(const wxString
& fullpathWithVolume
,
943 wxString
*pstrVolume
,
949 format
= GetFormat(format
);
951 wxString fullpath
= fullpathWithVolume
;
953 // under VMS the end of the path is ']', not the path separator used to
954 // separate the components
955 wxString sepPath
= format
== wxPATH_VMS
? _T(']')
956 : GetPathSeparators(format
);
958 // special Windows UNC paths hack: transform \\share\path into share:path
959 if ( format
== wxPATH_DOS
)
961 if ( fullpath
.length() >= 4 &&
962 fullpath
[0u] == wxFILE_SEP_PATH_DOS
&&
963 fullpath
[1u] == wxFILE_SEP_PATH_DOS
)
965 fullpath
.erase(0, 2);
967 size_t posFirstSlash
= fullpath
.find_first_of(sepPath
);
968 if ( posFirstSlash
!= wxString::npos
)
970 fullpath
[posFirstSlash
] = wxFILE_SEP_DSK
;
972 // UNC paths are always absolute, right? (FIXME)
973 fullpath
.insert(posFirstSlash
+ 1, wxFILE_SEP_PATH_DOS
);
978 // do we have the volume name in the beginning?
979 wxString sepVol
= GetVolumeSeparator(format
);
980 if ( !sepVol
.empty() )
982 size_t posFirstColon
= fullpath
.find_first_of(sepVol
);
983 if ( posFirstColon
!= wxString::npos
)
987 *pstrVolume
= fullpath
.Left(posFirstColon
);
990 // remove the volume name and the separator from the full path
991 fullpath
.erase(0, posFirstColon
+ sepVol
.length());
995 // find the positions of the last dot and last path separator in the path
996 size_t posLastDot
= fullpath
.find_last_of(wxFILE_SEP_EXT
);
997 size_t posLastSlash
= fullpath
.find_last_of(sepPath
);
999 if ( (posLastDot
!= wxString::npos
) &&
1000 ((format
== wxPATH_UNIX
) || (format
== wxPATH_VMS
)) )
1002 if ( (posLastDot
== 0) ||
1003 (fullpath
[posLastDot
- 1] == sepPath
[0u] ) )
1005 // under Unix and VMS, dot may be (and commonly is) the first
1006 // character of the filename, don't treat the entire filename as
1007 // extension in this case
1008 posLastDot
= wxString::npos
;
1012 // if we do have a dot and a slash, check that the dot is in the name part
1013 if ( (posLastDot
!= wxString::npos
) &&
1014 (posLastSlash
!= wxString::npos
) &&
1015 (posLastDot
< posLastSlash
) )
1017 // the dot is part of the path, not the start of the extension
1018 posLastDot
= wxString::npos
;
1021 // now fill in the variables provided by user
1024 if ( posLastSlash
== wxString::npos
)
1031 // take everything up to the path separator but take care to make
1032 // tha path equal to something like '/', not empty, for the files
1033 // immediately under root directory
1034 size_t len
= posLastSlash
;
1038 *pstrPath
= fullpath
.Left(len
);
1040 // special VMS hack: remove the initial bracket
1041 if ( format
== wxPATH_VMS
)
1043 if ( (*pstrPath
)[0u] == _T('[') )
1044 pstrPath
->erase(0, 1);
1051 // take all characters starting from the one after the last slash and
1052 // up to, but excluding, the last dot
1053 size_t nStart
= posLastSlash
== wxString::npos
? 0 : posLastSlash
+ 1;
1055 if ( posLastDot
== wxString::npos
)
1057 // take all until the end
1058 count
= wxString::npos
;
1060 else if ( posLastSlash
== wxString::npos
)
1064 else // have both dot and slash
1066 count
= posLastDot
- posLastSlash
- 1;
1069 *pstrName
= fullpath
.Mid(nStart
, count
);
1074 if ( posLastDot
== wxString::npos
)
1081 // take everything after the dot
1082 *pstrExt
= fullpath
.Mid(posLastDot
+ 1);
1087 // ----------------------------------------------------------------------------
1089 // ----------------------------------------------------------------------------
1091 bool wxFileName::SetTimes(const wxDateTime
*dtCreate
,
1092 const wxDateTime
*dtAccess
,
1093 const wxDateTime
*dtMod
)
1095 #if defined(__UNIX_LIKE__)
1096 if ( !dtAccess
&& !dtMod
)
1098 // can't modify the creation time anyhow, don't try
1102 // if dtAccess or dtMod is not specified, use the other one (which must be
1103 // non NULL because of the test above) for both times
1105 utm
.actime
= dtAccess
? dtAccess
->GetTicks() : dtMod
->GetTicks();
1106 utm
.modtime
= dtMod
? dtMod
->GetTicks() : dtAccess
->GetTicks();
1107 if ( utime(GetFullPath(), &utm
) == 0 )
1111 #elif defined(__WIN32__)
1112 wxFileHandle
fh(GetFullPath());
1115 FILETIME ftAccess
, ftCreate
, ftWrite
;
1118 ConvertWxToFileTime(&ftCreate
, *dtCreate
);
1120 ConvertWxToFileTime(&ftAccess
, *dtAccess
);
1122 ConvertWxToFileTime(&ftWrite
, *dtMod
);
1124 if ( ::SetFileTime(fh
,
1125 dtCreate
? &ftCreate
: NULL
,
1126 dtAccess
? &ftAccess
: NULL
,
1127 dtMod
? &ftWrite
: NULL
) )
1132 #else // other platform
1135 wxLogSysError(_("Failed to modify file times for '%s'"),
1136 GetFullPath().c_str());
1141 bool wxFileName::Touch()
1143 #if defined(__UNIX_LIKE__)
1144 // under Unix touching file is simple: just pass NULL to utime()
1145 if ( utime(GetFullPath(), NULL
) == 0 )
1150 wxLogSysError(_("Failed to touch the file '%s'"), GetFullPath().c_str());
1153 #else // other platform
1154 wxDateTime dtNow
= wxDateTime::Now();
1156 return SetTimes(NULL
/* don't change create time */, &dtNow
, &dtNow
);
1160 bool wxFileName::GetTimes(wxDateTime
*dtAccess
,
1162 wxDateTime
*dtChange
) const
1164 #if defined(__UNIX_LIKE__)
1166 if ( wxStat(GetFullPath(), &stBuf
) == 0 )
1169 dtAccess
->Set(stBuf
.st_atime
);
1171 dtMod
->Set(stBuf
.st_mtime
);
1173 dtChange
->Set(stBuf
.st_ctime
);
1177 #elif defined(__WXMAC__)
1179 if ( wxStat(GetFullPath(), &stBuf
) == 0 )
1182 dtAccess
->Set(stBuf
.st_atime
);
1184 dtMod
->Set(stBuf
.st_mtime
);
1186 dtChange
->Set(stBuf
.st_ctime
);
1190 #elif defined(__WIN32__)
1191 wxFileHandle
fh(GetFullPath());
1194 FILETIME ftAccess
, ftCreate
, ftWrite
;
1196 if ( ::GetFileTime(fh
,
1197 dtMod
? &ftCreate
: NULL
,
1198 dtAccess
? &ftAccess
: NULL
,
1199 dtChange
? &ftWrite
: NULL
) )
1202 ConvertFileTimeToWx(dtMod
, ftCreate
);
1204 ConvertFileTimeToWx(dtAccess
, ftAccess
);
1206 ConvertFileTimeToWx(dtChange
, ftWrite
);
1211 #else // other platform
1214 wxLogSysError(_("Failed to retrieve file times for '%s'"),
1215 GetFullPath().c_str());