X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0ca4ae936b3c1dbc276947001e8b660c6d29f477..878711c01c1b9ad5b97d35f379a048b8ce1bfb49:/src/common/filename.cpp diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 06dca4aa49..82efd1a6ae 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -6,7 +6,7 @@ // Created: 28.12.2000 // RCS-ID: $Id$ // Copyright: (c) 2000 Robert Roebling -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// /* @@ -107,10 +107,17 @@ #endif #ifdef __MWERKS__ +#ifdef __MACH__ +#include +#include +#include +#include +#else #include #include #include #endif +#endif #ifdef __WATCOMC__ #include @@ -124,6 +131,10 @@ #endif #endif +#ifdef __EMX__ +#define MAX_PATH _MAX_PATH +#endif + // ---------------------------------------------------------------------------- // private classes // ---------------------------------------------------------------------------- @@ -190,7 +201,7 @@ private: // private functions // ---------------------------------------------------------------------------- -#if defined(__WIN32__) && !defined(__WXMICROWIN__) +#if wxUSE_DATETIME && defined(__WIN32__) && !defined(__WXMICROWIN__) // convert between wxDateTime and FILETIME which is a 64-bit value representing // the number of 100-nanosecond intervals since January 1, 1601. @@ -237,7 +248,7 @@ static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt) } } -#endif // __WIN32__ +#endif // wxUSE_DATETIME && __WIN32__ // return a string with the volume par static wxString wxGetVolumeString(const wxString& volume, wxPathFormat format) @@ -434,6 +445,9 @@ void wxFileName::Clear() m_volume = m_name = m_ext = wxEmptyString; + + // we don't have any absolute path for now + m_relative = TRUE; } /* static */ @@ -454,7 +468,7 @@ wxFileName wxFileName::DirName(const wxString& dir) // existence tests // ---------------------------------------------------------------------------- -bool wxFileName::FileExists() +bool wxFileName::FileExists() const { return wxFileName::FileExists( GetFullPath() ); } @@ -464,7 +478,7 @@ bool wxFileName::FileExists( const wxString &file ) return ::wxFileExists( file ); } -bool wxFileName::DirExists() +bool wxFileName::DirExists() const { return wxFileName::DirExists( GetFullPath() ); } @@ -638,9 +652,11 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) // scratch space for mkstemp() path += _T("XXXXXX"); - // can use the cast here because the length doesn't change and the string - // is not shared - int fdTemp = mkstemp((char *)path.mb_str()); + // we need to copy the path to the buffer in which mkstemp() can modify it + wxCharBuffer buf( wxConvFile.cWX2MB( path ) ); + + // cast is safe because the string length doesn't change + int fdTemp = mkstemp( (char*)(const char*) buf ); if ( fdTemp == -1 ) { // this might be not necessary as mkstemp() on most systems should have @@ -649,6 +665,8 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) } else // mkstemp() succeeded { + path = wxConvFile.cMB2WX( (const char*) buf ); + // avoid leaking the fd if ( fileTemp ) { @@ -665,10 +683,15 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) // same as above path += _T("XXXXXX"); - if ( !mktemp((char *)path.mb_str()) ) + wxCharBuffer buf = wxConvFile.cWX2MB( path ); + if ( !mktemp( (const char*) buf ) ) { path.clear(); } + else + { + path = wxConvFile.cMB2WX( (const char*) buf ); + } #else // !HAVE_MKTEMP (includes __DOS__) // generate the unique file name ourselves #ifndef __DOS__ @@ -760,7 +783,14 @@ bool wxFileName::Mkdir( const wxString& dir, int perm, int flags ) size_t count = dirs.GetCount(); for ( size_t i = 0; i < count; i++ ) { - if ( i > 0 || filename.IsAbsolute() ) + if ( i > 0 || +#if defined(__WXMAC__) && !defined(__DARWIN__) + // relative pathnames are exactely the other way round under mac... + !filename.IsAbsolute() +#else + filename.IsAbsolute() +#endif + ) currPath += wxFILE_SEP_PATH; currPath += dirs[i]; @@ -912,6 +942,11 @@ bool wxFileName::Normalize(int flags, m_ext.MakeLower(); } + // we do have the path now + // + // NB: need to do this before (maybe) calling Assign() below + m_relative = FALSE; + #if defined(__WIN32__) if ( (flags & wxPATH_NORM_LONG) && (format == wxPATH_DOS) ) { @@ -919,9 +954,6 @@ bool wxFileName::Normalize(int flags, } #endif // Win32 - // we do have the path now - m_relative = FALSE; - return TRUE; } @@ -953,8 +985,8 @@ bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) // get cwd only once - small time saving wxString cwd = wxGetCwd(); - Normalize(wxPATH_NORM_ALL, cwd, format); - fnBase.Normalize(wxPATH_NORM_ALL, cwd, format); + Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); + fnBase.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); bool withCase = IsCaseSensitive(format); @@ -1004,15 +1036,15 @@ bool wxFileName::MakeRelativeTo(const wxString& pathBase, wxPathFormat format) // filename kind tests // ---------------------------------------------------------------------------- -bool wxFileName::SameAs(const wxFileName &filepath, wxPathFormat format) +bool wxFileName::SameAs(const wxFileName& filepath, wxPathFormat format) const { wxFileName fn1 = *this, fn2 = filepath; // get cwd only once - small time saving wxString cwd = wxGetCwd(); - fn1.Normalize(wxPATH_NORM_ALL, cwd, format); - fn2.Normalize(wxPATH_NORM_ALL, cwd, format); + fn1.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); + fn2.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, cwd, format); if ( fn1.GetFullPath() == fn2.GetFullPath() ) return TRUE; @@ -1144,27 +1176,40 @@ wxString wxFileName::GetPath( int flags, wxPathFormat format ) const } // the leading character - if ( format == wxPATH_MAC ) - { - if ( m_relative ) - fullpath += wxFILE_SEP_PATH_MAC; - } - else if ( format == wxPATH_DOS ) + switch ( format ) { - if (!m_relative) - fullpath += wxFILE_SEP_PATH_DOS; - } - else if ( format == wxPATH_UNIX ) - { - if ( !m_relative ) - { - // normally the absolute file names starts with a slash with one - // exception: file names like "~/foo.bar" don't have it - if ( m_dirs.IsEmpty() || m_dirs[0u] != _T('~') ) + case wxPATH_MAC: + if ( m_relative ) + fullpath += wxFILE_SEP_PATH_MAC; + break; + + case wxPATH_DOS: + if (!m_relative) + fullpath += wxFILE_SEP_PATH_DOS; + break; + + default: + wxFAIL_MSG( _T("unknown path format") ); + // fall through + + case wxPATH_UNIX: + if ( !m_relative ) { - fullpath += wxFILE_SEP_PATH_UNIX; + // normally the absolute file names starts with a slash with + // one exception: file names like "~/foo.bar" don't have it + if ( m_dirs.IsEmpty() || m_dirs[0u] != _T('~') ) + { + fullpath += wxFILE_SEP_PATH_UNIX; + } } - } + break; + + case wxPATH_VMS: + // no leading character here but use this place to unset + // wxPATH_GET_SEPARATOR flag: under VMS it doesn't make sense as, + // if I understand correctly, there should never be a dot before + // the closing bracket + flags &= ~wxPATH_GET_SEPARATOR; } // then concatenate all the path components using the path separator @@ -1204,13 +1249,14 @@ wxString wxFileName::GetPath( int flags, wxPathFormat format ) const case wxPATH_VMS: // TODO: What to do with ".." under VMS + // convert back from ".." to nothing if ( m_dirs[i] != wxT("..") ) fullpath += m_dirs[i]; break; } - if ( i != dirCount - 1 ) + if ( (flags & wxPATH_GET_SEPARATOR) || (i != dirCount - 1) ) fullpath += GetPathSeparator(format); } @@ -1220,11 +1266,6 @@ wxString wxFileName::GetPath( int flags, wxPathFormat format ) const } } - if ( (flags & wxPATH_GET_SEPARATOR) && !fullpath.empty() && fullpath.Last() != GetPathSeparator(format)) - { - fullpath += GetPathSeparator(format); - } - return fullpath; } @@ -1283,6 +1324,9 @@ wxString wxFileName::GetLongPath() const if ( !s_triedToLoad ) { + // suppress the errors about missing GetLongPathName[AW] + wxLogNull noLog; + s_triedToLoad = TRUE; wxDynamicLibrary dllKernel(_T("kernel32")); if ( dllKernel.IsLoaded() ) @@ -1320,6 +1364,7 @@ wxString wxFileName::GetLongPath() const } } } + if (success) return pathOut; #endif // wxUSE_DYNAMIC_LOADER @@ -1331,7 +1376,13 @@ wxString wxFileName::GetLongPath() const WIN32_FIND_DATA findFileData; HANDLE hFind; - pathOut = wxEmptyString; + + if ( HasVolume() ) + pathOut = GetVolume() + + GetVolumeSeparator(wxPATH_DOS) + + GetPathSeparator(wxPATH_DOS); + else + pathOut = wxEmptyString; wxArrayString dirs = GetDirs(); dirs.Add(GetFullName()); @@ -1349,7 +1400,8 @@ wxString wxFileName::GetLongPath() const if ( tmpPath.empty() ) continue; - if ( tmpPath.Last() == wxT(':') ) + // can't see this being necessary? MF + if ( tmpPath.Last() == GetVolumeSeparator(wxPATH_DOS) ) { // Can't pass a drive and root dir to FindFirstFile, // so continue to next dir @@ -1361,8 +1413,12 @@ wxString wxFileName::GetLongPath() const hFind = ::FindFirstFile(tmpPath, &findFileData); if (hFind == INVALID_HANDLE_VALUE) { - // Error: return immediately with the original path - return path; + // Error: most likely reason is that path doesn't exist, so + // append any unprocessed parts and return + for ( i += 1; i < count; i++ ) + tmpPath += wxFILE_SEP_PATH + dirs[i]; + + return tmpPath; } pathOut += findFileData.cFileName; @@ -1570,11 +1626,42 @@ void wxFileName::SplitPath(const wxString& fullpath, // time functions // ---------------------------------------------------------------------------- +#if wxUSE_DATETIME + bool wxFileName::SetTimes(const wxDateTime *dtAccess, const wxDateTime *dtMod, const wxDateTime *dtCreate) { -#if defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) +#if defined(__WIN32__) + if ( IsDir() ) + { + // VZ: please let me know how to do this if you can + wxFAIL_MSG( _T("SetTimes() not implemented for the directories") ); + } + else // file + { + wxFileHandle fh(GetFullPath(), wxFileHandle::Write); + if ( fh.IsOk() ) + { + FILETIME ftAccess, ftCreate, ftWrite; + + if ( dtCreate ) + ConvertWxToFileTime(&ftCreate, *dtCreate); + if ( dtAccess ) + ConvertWxToFileTime(&ftAccess, *dtAccess); + if ( dtMod ) + ConvertWxToFileTime(&ftWrite, *dtMod); + + if ( ::SetFileTime(fh, + dtCreate ? &ftCreate : NULL, + dtAccess ? &ftAccess : NULL, + dtMod ? &ftWrite : NULL) ) + { + return TRUE; + } + } + } +#elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) if ( !dtAccess && !dtMod ) { // can't modify the creation time anyhow, don't try @@ -1586,31 +1673,10 @@ bool wxFileName::SetTimes(const wxDateTime *dtAccess, utimbuf utm; utm.actime = dtAccess ? dtAccess->GetTicks() : dtMod->GetTicks(); utm.modtime = dtMod ? dtMod->GetTicks() : dtAccess->GetTicks(); - if ( utime(GetFullPath(), &utm) == 0 ) + if ( utime(GetFullPath().fn_str(), &utm) == 0 ) { return TRUE; } -#elif defined(__WIN32__) - wxFileHandle fh(GetFullPath(), wxFileHandle::Write); - if ( fh.IsOk() ) - { - FILETIME ftAccess, ftCreate, ftWrite; - - if ( dtCreate ) - ConvertWxToFileTime(&ftCreate, *dtCreate); - if ( dtAccess ) - ConvertWxToFileTime(&ftAccess, *dtAccess); - if ( dtMod ) - ConvertWxToFileTime(&ftWrite, *dtMod); - - if ( ::SetFileTime(fh, - dtCreate ? &ftCreate : NULL, - dtAccess ? &ftAccess : NULL, - dtMod ? &ftWrite : NULL) ) - { - return TRUE; - } - } #else // other platform #endif // platforms @@ -1624,7 +1690,7 @@ bool wxFileName::Touch() { #if defined(__UNIX_LIKE__) // under Unix touching file is simple: just pass NULL to utime() - if ( utime(GetFullPath(), NULL) == 0 ) + if ( utime(GetFullPath().fn_str(), NULL) == 0 ) { return TRUE; } @@ -1643,9 +1709,54 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, wxDateTime *dtMod, wxDateTime *dtCreate) const { -#if defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__)) +#if defined(__WIN32__) + // we must use different methods for the files and directories under + // Windows as CreateFile(GENERIC_READ) doesn't work for the directories and + // CreateFile(FILE_FLAG_BACKUP_SEMANTICS) works -- but only under NT and + // not 9x + bool ok; + FILETIME ftAccess, ftCreate, ftWrite; + if ( IsDir() ) + { + // implemented in msw/dir.cpp + extern bool wxGetDirectoryTimes(const wxString& dirname, + FILETIME *, FILETIME *, FILETIME *); + + // we should pass the path without the trailing separator to + // wxGetDirectoryTimes() + ok = wxGetDirectoryTimes(GetPath(wxPATH_GET_VOLUME), + &ftAccess, &ftCreate, &ftWrite); + } + else // file + { + wxFileHandle fh(GetFullPath(), wxFileHandle::Read); + if ( fh.IsOk() ) + { + ok = ::GetFileTime(fh, + dtCreate ? &ftCreate : NULL, + dtAccess ? &ftAccess : NULL, + dtMod ? &ftWrite : NULL) != 0; + } + else + { + ok = FALSE; + } + } + + if ( ok ) + { + if ( dtCreate ) + ConvertFileTimeToWx(dtCreate, ftCreate); + if ( dtAccess ) + ConvertFileTimeToWx(dtAccess, ftAccess); + if ( dtMod ) + ConvertFileTimeToWx(dtMod, ftWrite); + + return TRUE; + } +#elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || (defined(__DOS__) && defined(__WATCOMC__)) wxStructStat stBuf; - if ( wxStat(GetFullPath(), &stBuf) == 0 ) + if ( wxStat( GetFullPath().c_str(), &stBuf) == 0 ) { if ( dtAccess ) dtAccess->Set(stBuf.st_atime); @@ -1656,27 +1767,6 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, return TRUE; } -#elif defined(__WIN32__) - wxFileHandle fh(GetFullPath(), wxFileHandle::Read); - if ( fh.IsOk() ) - { - FILETIME ftAccess, ftCreate, ftWrite; - - if ( ::GetFileTime(fh, - dtCreate ? &ftCreate : NULL, - dtAccess ? &ftAccess : NULL, - dtMod ? &ftWrite : NULL) ) - { - if ( dtCreate ) - ConvertFileTimeToWx(dtCreate, ftCreate); - if ( dtAccess ) - ConvertFileTimeToWx(dtAccess, ftAccess); - if ( dtMod ) - ConvertFileTimeToWx(dtMod, ftWrite); - - return TRUE; - } - } #else // other platform #endif // platforms @@ -1686,6 +1776,8 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, return FALSE; } +#endif // wxUSE_DATETIME + #ifdef __WXMAC__ const short kMacExtensionMaxLength = 16 ; @@ -1699,18 +1791,18 @@ public : } MacDefaultExtensionRecord( const MacDefaultExtensionRecord& from ) { - strcpy( m_ext , from.m_ext ) ; + wxStrcpy( m_ext , from.m_ext ) ; m_type = from.m_type ; m_creator = from.m_creator ; } - MacDefaultExtensionRecord( char * extension , OSType type , OSType creator ) + MacDefaultExtensionRecord( const wxChar * extension , OSType type , OSType creator ) { - strncpy( m_ext , extension , kMacExtensionMaxLength ) ; + wxStrncpy( m_ext , extension , kMacExtensionMaxLength ) ; m_ext[kMacExtensionMaxLength] = 0 ; m_type = type ; m_creator = creator ; } - char m_ext[kMacExtensionMaxLength] ; + wxChar m_ext[kMacExtensionMaxLength] ; OSType m_type ; OSType m_creator ; } ; @@ -1730,16 +1822,16 @@ static void MacEnsureDefaultExtensionsLoaded() { if ( !gMacDefaultExtensionsInited ) { - + // load the default extensions MacDefaultExtensionRecord defaults[1] = { - MacDefaultExtensionRecord( "txt" , 'TEXT' , 'ttxt' ) , + MacDefaultExtensionRecord( wxT("txt") , 'TEXT' , 'ttxt' ) , } ; // we could load the pc exchange prefs here too - for ( int i = 0 ; i < WXSIZEOF( defaults ) ; ++i ) + for ( size_t i = 0 ; i < WXSIZEOF( defaults ) ; ++i ) { gMacDefaultExtensions.Add( defaults[i] ) ; } @@ -1806,7 +1898,7 @@ void wxFileName::MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint3 MacDefaultExtensionRecord rec ; rec.m_type = type ; rec.m_creator = creator ; - strncpy( rec.m_ext , ext.Lower().c_str() , kMacExtensionMaxLength ) ; + wxStrncpy( rec.m_ext , ext.Lower().c_str() , kMacExtensionMaxLength ) ; gMacDefaultExtensions.Add( rec ) ; } #endif