X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a2b772607a87ffe6fe9af8c0b64b7259ef10a4a4..3db5226517a63bcdc983e638f1a312764fe1312f:/src/common/filename.cpp diff --git a/src/common/filename.cpp b/src/common/filename.cpp index 84af36bbf2..d1766a8179 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -60,10 +60,6 @@ // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) -#pragma implementation "filename.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -87,6 +83,9 @@ // For GetShort/LongPathName #ifdef __WIN32__ #include "wx/msw/wrapwin.h" +#if defined(__MINGW32__) +#include "wx/msw/gccpriv.h" +#endif #endif #ifdef __WXWINCE__ @@ -234,8 +233,8 @@ static void ConvertWxToFileTime(FILETIME *ft, const wxDateTime& dt) { SYSTEMTIME st; st.wDay = dt.GetDay(); - st.wMonth = dt.GetMonth() + 1; - st.wYear = dt.GetYear(); + st.wMonth = (WORD)(dt.GetMonth() + 1); + st.wYear = (WORD)dt.GetYear(); st.wHour = dt.GetHour(); st.wMinute = dt.GetMinute(); st.wSecond = dt.GetSecond(); @@ -297,12 +296,14 @@ void wxFileName::Assign( const wxFileName &filepath ) m_name = filepath.GetName(); m_ext = filepath.GetExt(); m_relative = filepath.m_relative; + m_hasExt = filepath.m_hasExt; } void wxFileName::Assign(const wxString& volume, const wxString& path, const wxString& name, const wxString& ext, + bool hasExt, wxPathFormat format ) { SetPath( path, format ); @@ -310,6 +311,8 @@ void wxFileName::Assign(const wxString& volume, m_volume = volume; m_ext = ext; m_name = name; + + m_hasExt = hasExt; } void wxFileName::SetPath( const wxString& pathOrig, wxPathFormat format ) @@ -408,9 +411,10 @@ void wxFileName::Assign(const wxString& fullpath, wxPathFormat format) { wxString volume, path, name, ext; - SplitPath(fullpath, &volume, &path, &name, &ext, format); + bool hasExt; + SplitPath(fullpath, &volume, &path, &name, &ext, &hasExt, format); - Assign(volume, path, name, ext, format); + Assign(volume, path, name, ext, hasExt, format); } void wxFileName::Assign(const wxString& fullpathOrig, @@ -426,15 +430,16 @@ void wxFileName::Assign(const wxString& fullpathOrig, } wxString volume, path, name, ext; + bool hasExt; // do some consistency checks in debug mode: the name should be really just // the filename and the path should be really just a path #ifdef __WXDEBUG__ - wxString pathDummy, nameDummy, extDummy; + wxString volDummy, pathDummy, nameDummy, extDummy; - SplitPath(fullname, &pathDummy, &name, &ext, format); + SplitPath(fullname, &volDummy, &pathDummy, &name, &ext, &hasExt, format); - wxASSERT_MSG( pathDummy.empty(), + wxASSERT_MSG( volDummy.empty() && pathDummy.empty(), _T("the file name shouldn't contain the path") ); SplitPath(fullpath, &volume, &path, &nameDummy, &extDummy, format); @@ -443,16 +448,29 @@ void wxFileName::Assign(const wxString& fullpathOrig, _T("the path shouldn't contain file name nor extension") ); #else // !__WXDEBUG__ - SplitPath(fullname, NULL /* no path */, &name, &ext, format); + SplitPath(fullname, NULL /* no volume */, NULL /* no path */, + &name, &ext, &hasExt, format); SplitPath(fullpath, &volume, &path, NULL, NULL, format); #endif // __WXDEBUG__/!__WXDEBUG__ + Assign(volume, path, name, ext, hasExt, format); +} + +void wxFileName::Assign(const wxString& pathOrig, + const wxString& name, + const wxString& ext, + wxPathFormat format) +{ + wxString volume, + path; + SplitVolume(pathOrig, &volume, &path, format); + Assign(volume, path, name, ext, format); } void wxFileName::AssignDir(const wxString& dir, wxPathFormat format) { - Assign(dir, _T(""), format); + Assign(dir, wxEmptyString, format); } void wxFileName::Clear() @@ -465,6 +483,9 @@ void wxFileName::Clear() // we don't have any absolute path for now m_relative = true; + + // nor any extension + m_hasExt = false; } /* static */ @@ -558,6 +579,8 @@ wxString wxFileName::GetHomeDir() return ::wxGetHomeDir(); } +#if wxUSE_FILE + void wxFileName::AssignTempFileName(const wxString& prefix, wxFile *fileTemp) { wxString tempname = CreateTempFileName(prefix, fileTemp); @@ -587,11 +610,11 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) // FIXME. Create \temp dir? dir = wxT("\\"); } - path = dir + wxT("\\") + prefix; + path = dir + wxT("\\") + name; int i = 1; - while (wxFileExists(path)) + while (FileExists(path)) { - path = dir + wxT("\\") + prefix ; + path = dir + wxT("\\") + name ; path << i; i ++; } @@ -677,7 +700,7 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) else // mkstemp() succeeded { path = wxConvFile.cMB2WX( (const char*) buf ); - + // avoid leaking the fd if ( fileTemp ) { @@ -705,7 +728,7 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) } #else // !HAVE_MKTEMP (includes __DOS__) // generate the unique file name ourselves - #if !defined(__DOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) ) + #if !defined(__DOS__) && !defined(__PALMOS__) && (!defined(__MWERKS__) || defined(__DARWIN__) ) path << (unsigned int)getpid(); #endif @@ -716,7 +739,7 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) { // 3 hex digits is enough for numTries == 1000 < 4096 pathTry = path + wxString::Format(_T("%.03x"), (unsigned int) n); - if ( !wxFile::Exists(pathTry) ) + if ( !FileExists(pathTry) ) { break; } @@ -767,6 +790,8 @@ wxFileName::CreateTempFileName(const wxString& prefix, wxFile *fileTemp) return path; } +#endif // wxUSE_FILE + // ---------------------------------------------------------------------------- // directory operations // ---------------------------------------------------------------------------- @@ -794,12 +819,12 @@ 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 || + if ( i > 0 || #if defined(__WXMAC__) && !defined(__DARWIN__) - // relative pathnames are exactely the other way round under mac... - !filename.IsAbsolute() + // relative pathnames are exactely the other way round under mac... + !filename.IsAbsolute() #else - filename.IsAbsolute() + filename.IsAbsolute() #endif ) currPath += wxFILE_SEP_PATH; @@ -951,7 +976,7 @@ bool wxFileName::Normalize(int flags, m_dirs.Add(dir); } - + #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE if ( (flags & wxPATH_NORM_SHORTCUT) ) { @@ -1021,49 +1046,54 @@ bool wxFileName::GetShortcutTarget(const wxString& shortcutPath, wxString& targe { wxString path, file, ext; wxSplitPath(shortcutPath, & path, & file, & ext); - - HRESULT hres; - IShellLink* psl; - bool success = FALSE; + + HRESULT hres; + IShellLink* psl; + bool success = false; // Assume it's not a shortcut if it doesn't end with lnk - if (ext.Lower() != wxT("lnk")) - return FALSE; - - // create a ShellLink object - hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, - IID_IShellLink, (LPVOID*) &psl); - - if (SUCCEEDED(hres)) - { - IPersistFile* ppf; - hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf); - if (SUCCEEDED(hres)) - { - WCHAR wsz[MAX_PATH]; - - MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, shortcutPath.mb_str(), -1, wsz, - MAX_PATH); - - hres = ppf->Load(wsz, 0); - if (SUCCEEDED(hres)) - { + if (ext.CmpNoCase(wxT("lnk"))!=0) + return false; + + // create a ShellLink object + hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, + IID_IShellLink, (LPVOID*) &psl); + + if (SUCCEEDED(hres)) + { + IPersistFile* ppf; + hres = psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf); + if (SUCCEEDED(hres)) + { + WCHAR wsz[MAX_PATH]; + + MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, shortcutPath.mb_str(), -1, wsz, + MAX_PATH); + + hres = ppf->Load(wsz, 0); + if (SUCCEEDED(hres)) + { wxChar buf[2048]; - psl->GetPath(buf, 2048, NULL, SLGP_UNCPRIORITY); - targetFilename = wxString(buf); + // Wrong prototype in early versions +#if defined(__MINGW32__) && !wxCHECK_W32API_VERSION(2, 2) + psl->GetPath((CHAR*) buf, 2048, NULL, SLGP_UNCPRIORITY); +#else + psl->GetPath(buf, 2048, NULL, SLGP_UNCPRIORITY); +#endif + targetFilename = wxString(buf); success = (shortcutPath != targetFilename); - psl->GetArguments(buf, 2048); + psl->GetArguments(buf, 2048); wxString args(buf); - if (!args.IsEmpty() && arguments) + if (!args.empty() && arguments) { *arguments = args; - } - } - } - } - psl->Release(); - return success; + } + } + } + } + psl->Release(); + return success; } #endif @@ -1269,10 +1299,8 @@ wxString wxFileName::GetPathTerminators(wxPathFormat format) bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format) { // wxString::Find() doesn't work as expected with NUL - it will always find - // it, so it is almost surely a bug if this function is called with NUL arg - wxASSERT_MSG( ch != _T('\0'), _T("shouldn't be called with NUL") ); - - return GetPathSeparators(format).Find(ch) != wxNOT_FOUND; + // it, so test for it separately + return ch != _T('\0') && GetPathSeparators(format).Find(ch) != wxNOT_FOUND; } // ---------------------------------------------------------------------------- @@ -1302,26 +1330,26 @@ bool wxFileName::IsPathSeparator(wxChar ch, wxPathFormat format) return true; } -void wxFileName::AppendDir( const wxString &dir ) +void wxFileName::AppendDir( const wxString& dir ) { if ( IsValidDirComponent(dir) ) m_dirs.Add( dir ); } -void wxFileName::PrependDir( const wxString &dir ) +void wxFileName::PrependDir( const wxString& dir ) { InsertDir(0, dir); } -void wxFileName::InsertDir( int before, const wxString &dir ) +void wxFileName::InsertDir(size_t before, const wxString& dir) { if ( IsValidDirComponent(dir) ) - m_dirs.Insert( dir, before ); + m_dirs.Insert(dir, before); } -void wxFileName::RemoveDir( int pos ) +void wxFileName::RemoveDir(size_t pos) { - m_dirs.RemoveAt( (size_t)pos ); + m_dirs.RemoveAt(pos); } // ---------------------------------------------------------------------------- @@ -1330,13 +1358,14 @@ void wxFileName::RemoveDir( int pos ) void wxFileName::SetFullName(const wxString& fullname) { - SplitPath(fullname, NULL /* no path */, &m_name, &m_ext); + SplitPath(fullname, NULL /* no volume */, NULL /* no path */, + &m_name, &m_ext, &m_hasExt); } wxString wxFileName::GetFullName() const { wxString fullname = m_name; - if ( !m_ext.empty() ) + if ( m_hasExt ) { fullname << wxFILE_SEP_EXT << m_ext; } @@ -1420,7 +1449,7 @@ wxString wxFileName::GetPath( int flags, wxPathFormat format ) const } // convert back from ".." to nothing - if ( m_dirs[i] != wxT("..") ) + if ( !m_dirs[i].IsSameAs(wxT("..")) ) fullpath += m_dirs[i]; break; @@ -1437,7 +1466,7 @@ wxString wxFileName::GetPath( int flags, wxPathFormat format ) const // TODO: What to do with ".." under VMS // convert back from ".." to nothing - if ( m_dirs[i] != wxT("..") ) + if ( !m_dirs[i].IsSameAs(wxT("..")) ) fullpath += m_dirs[i]; break; } @@ -1469,27 +1498,26 @@ wxString wxFileName::GetFullPath( wxPathFormat format ) const // Return the short form of the path (returns identity on non-Windows platforms) wxString wxFileName::GetShortPath() const { -#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) wxString path(GetFullPath()); - wxString pathOut; + +#if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) DWORD sz = ::GetShortPathName(path, NULL, 0); - bool ok = sz != 0; - if ( ok ) + if ( sz != 0 ) { - ok = ::GetShortPathName + wxString pathOut; + if ( ::GetShortPathName ( path, wxStringBuffer(pathOut, sz), sz - ) != 0; + ) != 0 ) + { + return pathOut; + } } - if (ok) - return pathOut; +#endif // Windows return path; -#else - return GetFullPath(); -#endif } // Return the long form of the path (returns identity on non-Windows platforms) @@ -1499,116 +1527,115 @@ wxString wxFileName::GetLongPath() const path = GetFullPath(); #if defined(__WIN32__) && !defined(__WXMICROWIN__) - bool success = false; #if wxUSE_DYNAMIC_LOADER typedef DWORD (WINAPI *GET_LONG_PATH_NAME)(const wxChar *, wxChar *, DWORD); - static bool s_triedToLoad = false; - - if ( !s_triedToLoad ) + // this is MT-safe as in the worst case we're going to resolve the function + // twice -- but as the result is the same in both threads, it's ok + static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL; + if ( !s_pfnGetLongPathName ) { - // suppress the errors about missing GetLongPathName[AW] - wxLogNull noLog; + static bool s_triedToLoad = false; - s_triedToLoad = true; - wxDynamicLibrary dllKernel(_T("kernel32")); - if ( dllKernel.IsLoaded() ) + if ( !s_triedToLoad ) { - // may succeed or fail depending on the Windows version - static GET_LONG_PATH_NAME s_pfnGetLongPathName = NULL; -#ifdef _UNICODE - s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameW")); -#else - s_pfnGetLongPathName = (GET_LONG_PATH_NAME) dllKernel.GetSymbol(_T("GetLongPathNameA")); -#endif + s_triedToLoad = true; - if ( s_pfnGetLongPathName ) - { - DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0); - bool ok = dwSize > 0; + wxDynamicLibrary dllKernel(_T("kernel32")); - if ( ok ) - { - DWORD sz = (*s_pfnGetLongPathName)(path, NULL, 0); - ok = sz != 0; - if ( ok ) - { - ok = (*s_pfnGetLongPathName) - ( - path, - wxStringBuffer(pathOut, sz), - sz - ) != 0; - success = true; - } - } + const wxChar* GetLongPathName = _T("GetLongPathName") +#if wxUSE_UNICODE + _T("W"); +#else // ANSI + _T("A"); +#endif // Unicode/ANSI + + if ( dllKernel.HasSymbol(GetLongPathName) ) + { + s_pfnGetLongPathName = (GET_LONG_PATH_NAME) + dllKernel.GetSymbol(GetLongPathName); } + + // note that kernel32.dll can be unloaded, it stays in memory + // anyhow as all Win32 programs link to it and so it's safe to call + // GetLongPathName() even after unloading it } } - if (success) - return pathOut; + if ( s_pfnGetLongPathName ) + { + DWORD dwSize = (*s_pfnGetLongPathName)(path, NULL, 0); + if ( dwSize > 0 ) + { + if ( (*s_pfnGetLongPathName) + ( + path, + wxStringBuffer(pathOut, dwSize), + dwSize + ) != 0 ) + { + return pathOut; + } + } + } #endif // wxUSE_DYNAMIC_LOADER - if (!success) - { - // The OS didn't support GetLongPathName, or some other error. - // We need to call FindFirstFile on each component in turn. + // The OS didn't support GetLongPathName, or some other error. + // We need to call FindFirstFile on each component in turn. - WIN32_FIND_DATA findFileData; - HANDLE hFind; + WIN32_FIND_DATA findFileData; + HANDLE hFind; - if ( HasVolume() ) - pathOut = GetVolume() + - GetVolumeSeparator(wxPATH_DOS) + - GetPathSeparator(wxPATH_DOS); - else - pathOut = wxEmptyString; + if ( HasVolume() ) + pathOut = GetVolume() + + GetVolumeSeparator(wxPATH_DOS) + + GetPathSeparator(wxPATH_DOS); + else + pathOut = wxEmptyString; - wxArrayString dirs = GetDirs(); - dirs.Add(GetFullName()); + wxArrayString dirs = GetDirs(); + dirs.Add(GetFullName()); - wxString tmpPath; + wxString tmpPath; - size_t count = dirs.GetCount(); - for ( size_t i = 0; i < count; i++ ) - { - // We're using pathOut to collect the long-name path, but using a - // temporary for appending the last path component which may be - // short-name - tmpPath = pathOut + dirs[i]; + size_t count = dirs.GetCount(); + for ( size_t i = 0; i < count; i++ ) + { + // We're using pathOut to collect the long-name path, but using a + // temporary for appending the last path component which may be + // short-name + tmpPath = pathOut + dirs[i]; - if ( tmpPath.empty() ) - continue; + if ( tmpPath.empty() ) + continue; - // 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 - tmpPath += wxFILE_SEP_PATH; - pathOut = tmpPath; - continue; - } + // 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 + tmpPath += wxFILE_SEP_PATH; + pathOut = tmpPath; + continue; + } - hFind = ::FindFirstFile(tmpPath, &findFileData); - if (hFind == INVALID_HANDLE_VALUE) - { - // 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]; + hFind = ::FindFirstFile(tmpPath, &findFileData); + if (hFind == INVALID_HANDLE_VALUE) + { + // 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; - } + return tmpPath; + } - pathOut += findFileData.cFileName; - if ( (i < (count-1)) ) - pathOut += wxFILE_SEP_PATH; + pathOut += findFileData.cFileName; + if ( (i < (count-1)) ) + pathOut += wxFILE_SEP_PATH; - ::FindClose(hFind); - } + ::FindClose(hFind); } #else // !Win32 pathOut = path; @@ -1698,6 +1725,7 @@ void wxFileName::SplitPath(const wxString& fullpathWithVolume, wxString *pstrPath, wxString *pstrName, wxString *pstrExt, + bool *hasExt, wxPathFormat format) { format = GetFormat(format); @@ -1784,18 +1812,25 @@ void wxFileName::SplitPath(const wxString& fullpathWithVolume, *pstrName = fullpath.Mid(nStart, count); } - if ( pstrExt ) + // finally deal with the extension here: we have an added complication that + // extension may be empty (but present) as in "foo." where trailing dot + // indicates the empty extension at the end -- and hence we must remember + // that we have it independently of pstrExt + if ( posLastDot == wxString::npos ) { - if ( posLastDot == wxString::npos ) - { - // no extension - pstrExt->Empty(); - } - else - { - // take everything after the dot + // no extension + if ( pstrExt ) + pstrExt->clear(); + if ( hasExt ) + *hasExt = false; + } + else + { + // take everything after the dot + if ( pstrExt ) *pstrExt = fullpath.Mid(posLastDot + 1); - } + if ( hasExt ) + *hasExt = true; } } @@ -1855,6 +1890,8 @@ bool wxFileName::SetTimes(const wxDateTime *dtAccess, } } #elif defined(__UNIX_LIKE__) || (defined(__DOS__) && defined(__WATCOMC__)) + wxUnusedVar(dtCreate); + if ( !dtAccess && !dtMod ) { // can't modify the creation time anyhow, don't try @@ -1871,6 +1908,9 @@ bool wxFileName::SetTimes(const wxDateTime *dtAccess, return true; } #else // other platform + wxUnusedVar(dtAccess); + wxUnusedVar(dtMod); + wxUnusedVar(dtCreate); #endif // platforms wxLogSysError(_("Failed to modify file times for '%s'"), @@ -1961,6 +2001,9 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, return true; } #else // other platform + wxUnusedVar(dtAccess); + wxUnusedVar(dtMod); + wxUnusedVar(dtCreate); #endif // platforms wxLogSysError(_("Failed to retrieve file times for '%s'"), @@ -1980,7 +2023,7 @@ public : MacDefaultExtensionRecord() { m_ext[0] = 0 ; - m_type = m_creator = NULL ; + m_type = m_creator = 0 ; } MacDefaultExtensionRecord( const MacDefaultExtensionRecord& from ) { @@ -2040,14 +2083,14 @@ bool wxFileName::MacSetTypeAndCreator( wxUint32 type , wxUint32 creator ) if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr ) { - if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) - { - finfo = (FileInfo*)&catInfo.finderInfo; - finfo->fileType = type ; - finfo->fileCreator = creator ; - FSSetCatalogInfo( &fsRef, kFSCatInfoFinderInfo, &catInfo ) ; + if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) + { + finfo = (FileInfo*)&catInfo.finderInfo; + finfo->fileType = type ; + finfo->fileCreator = creator ; + FSSetCatalogInfo( &fsRef, kFSCatInfoFinderInfo, &catInfo ) ; return true ; - } + } } return false ; } @@ -2060,13 +2103,13 @@ bool wxFileName::MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator ) if ( wxMacPathToFSRef( GetFullPath() , &fsRef ) == noErr ) { - if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) - { - finfo = (FileInfo*)&catInfo.finderInfo; - *type = finfo->fileType ; - *creator = finfo->fileCreator ; + if ( FSGetCatalogInfo (&fsRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr ) + { + finfo = (FileInfo*)&catInfo.finderInfo; + *type = finfo->fileType ; + *creator = finfo->fileCreator ; return true ; - } + } } return false ; }