X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e01a788ee05125b4fefa01a5b11f4088915cb54e..abb8764e32f03040cfa03353f2058a6b98a6e612:/src/common/filename.cpp diff --git a/src/common/filename.cpp b/src/common/filename.cpp index ce4e9f2d31..b70eb4fd8d 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -98,7 +98,7 @@ #include "wx/msw/gccpriv.h" #endif -#ifdef __WXWINCE__ +#ifdef __WXMSW__ #include "wx/msw/private.h" #endif @@ -153,13 +153,6 @@ extern const wxULongLong wxInvalidSize = (unsigned)-1; #endif // wxUSE_LONGLONG -#ifdef __WIN32__ - // this define is missing from VC6 headers - #ifndef INVALID_FILE_ATTRIBUTES - #define INVALID_FILE_ATTRIBUTES ((DWORD)-1) - #endif -#endif // __WIN32__ - namespace { @@ -188,7 +181,7 @@ public: // access time (see #10567) m_hFile = ::CreateFile ( - filename.fn_str(), // name + filename.t_str(), // name mode == ReadAttr ? FILE_READ_ATTRIBUTES // access mask : FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | // sharing mode @@ -613,7 +606,7 @@ bool wxFileName::FileExists( const wxString &filePath ) #elif defined(__WIN32__) && !defined(__WXMICROWIN__) // we must use GetFileAttributes() instead of the ANSI C functions because // it can cope with network (UNC) paths unlike them - DWORD ret = ::GetFileAttributes(filePath.fn_str()); + DWORD ret = ::GetFileAttributes(filePath.t_str()); return (ret != INVALID_FILE_ATTRIBUTES) && !(ret & FILE_ATTRIBUTE_DIRECTORY); #else // !__WIN32__ @@ -621,16 +614,13 @@ bool wxFileName::FileExists( const wxString &filePath ) #define S_ISREG(mode) ((mode) & S_IFREG) #endif wxStructStat st; -#ifndef wxNEED_WX_UNISTD_H - return (wxStat( filePath.fn_str() , &st) == 0 && S_ISREG(st.st_mode)) + + return (wxStat( filePath, &st) == 0 && S_ISREG(st.st_mode)) #ifdef __OS2__ || (errno == EACCES) // if access is denied something with that name // exists and is opened in exclusive mode. #endif ; -#else - return wxStat( filePath , &st) == 0 && S_ISREG(st.st_mode); -#endif #endif // __WIN32__/!__WIN32__ } @@ -673,7 +663,7 @@ bool wxFileName::DirExists( const wxString &dirPath ) return false; #elif defined(__WIN32__) && !defined(__WXMICROWIN__) // stat() can't cope with network paths - DWORD ret = ::GetFileAttributes(strPath.fn_str()); + DWORD ret = ::GetFileAttributes(strPath.t_str()); return (ret != INVALID_FILE_ATTRIBUTES) && (ret & FILE_ATTRIBUTE_DIRECTORY); #elif defined(__OS2__) @@ -688,10 +678,10 @@ bool wxFileName::DirExists( const wxString &dirPath ) wxStructStat st; #ifndef __VISAGECPP__ - return wxStat(strPath.c_str(), &st) == 0 && ((st.st_mode & S_IFMT) == S_IFDIR); + return wxStat(strPath, &st) == 0 && ((st.st_mode & S_IFMT) == S_IFDIR); #else // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only - return wxStat(strPath.c_str(), &st) == 0 && (st.st_mode == S_IFDIR); + return wxStat(strPath, &st) == 0 && (st.st_mode == S_IFDIR); #endif #endif // __WIN32__/!__WIN32__ @@ -879,8 +869,8 @@ static wxString wxCreateTempImpl( } #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__) - if ( !::GetTempFileName(dir.fn_str(), name.fn_str(), 0, - wxStringBuffer(path, MAX_PATH + 1)) ) + if (!::GetTempFileName(dir.t_str(), name.t_str(), 0, + wxStringBuffer(path, MAX_PATH + 1))) { wxLogLastError(wxT("GetTempFileName")); @@ -1194,9 +1184,24 @@ wxString wxFileName::GetTempDir() wxLogLastError(wxT("GetTempPath")); } #elif defined(__WXMAC__) && wxOSX_USE_CARBON - dir = wxMacFindFolder(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder); + dir = wxMacFindFolderNoSeparator(short(kOnSystemDisk), kTemporaryFolderType, kCreateFolder); #endif // systems with native way } + else // we got directory from an environment variable + { + // remove any trailing path separators, we don't want to ever return + // them from this function for consistency + const size_t lastNonSep = dir.find_last_not_of(GetPathSeparators()); + if ( lastNonSep == wxString::npos ) + { + // the string consists entirely of separators, leave only one + dir = GetPathSeparator(); + } + else + { + dir.erase(lastNonSep + 1); + } + } // fall back to hard coded value if ( dir.empty() ) @@ -1275,7 +1280,11 @@ bool wxFileName::Rmdir(const wxString& dir, int flags) SHFILEOPSTRUCT fileop; wxZeroMemory(fileop); fileop.wFunc = FO_DELETE; + #if defined(__CYGWIN__) && defined(wxUSE_UNICODE) + fileop.pFrom = path.wc_str(); + #else fileop.pFrom = path.fn_str(); + #endif fileop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION; #ifndef __WXWINCE__ // FOF_NOERRORUI is not defined in WinCE @@ -1439,15 +1448,24 @@ bool wxFileName::Normalize(int flags, if ( dir == wxT("..") ) { - if ( m_dirs.IsEmpty() ) + if ( m_dirs.empty() ) { - wxLogError(_("The path '%s' contains too many \"..\"!"), - GetFullPath().c_str()); - return false; - } + // We have more ".." than directory components so far. + // Don't treat this as an error as the path could have been + // entered by user so try to handle it reasonably: if the + // path is absolute, just ignore the extra ".." because + // "/.." is the same as "/". Otherwise, i.e. for relative + // paths, keep ".." unchanged because removing it would + // modify the file a relative path refers to. + if ( !m_relative ) + continue; - m_dirs.RemoveAt(m_dirs.GetCount() - 1); - continue; + } + else // Normal case, go one step up. + { + m_dirs.pop_back(); + continue; + } } } @@ -2046,13 +2064,13 @@ wxString wxFileName::GetShortPath() const wxString path(GetFullPath()); #if defined(__WXMSW__) && defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__) - DWORD sz = ::GetShortPathName(path.fn_str(), NULL, 0); + DWORD sz = ::GetShortPathName(path.t_str(), NULL, 0); if ( sz != 0 ) { wxString pathOut; if ( ::GetShortPathName ( - path.fn_str(), + path.t_str(), wxStringBuffer(pathOut, sz), sz ) != 0 ) @@ -2110,12 +2128,12 @@ wxString wxFileName::GetLongPath() const if ( s_pfnGetLongPathName ) { - DWORD dwSize = (*s_pfnGetLongPathName)(path.fn_str(), NULL, 0); + DWORD dwSize = (*s_pfnGetLongPathName)(path.t_str(), NULL, 0); if ( dwSize > 0 ) { if ( (*s_pfnGetLongPathName) ( - path.fn_str(), + path.t_str(), wxStringBuffer(pathOut, dwSize), dwSize ) != 0 ) @@ -2167,7 +2185,7 @@ wxString wxFileName::GetLongPath() const continue; } - hFind = ::FindFirstFile(tmpPath.fn_str(), &findFileData); + hFind = ::FindFirstFile(tmpPath.t_str(), &findFileData); if (hFind == INVALID_HANDLE_VALUE) { // Error: most likely reason is that path doesn't exist, so @@ -2587,7 +2605,7 @@ bool wxFileName::GetTimes(wxDateTime *dtAccess, #elif defined(__UNIX_LIKE__) || defined(__WXMAC__) || defined(__OS2__) || (defined(__DOS__) && defined(__WATCOMC__)) // no need to test for IsDir() here wxStructStat stBuf; - if ( wxStat( GetFullPath().c_str(), &stBuf) == 0 ) + if ( wxStat( GetFullPath(), &stBuf) == 0 ) { if ( dtAccess ) dtAccess->Set(stBuf.st_atime); @@ -2641,11 +2659,7 @@ wxULongLong wxFileName::GetSize(const wxString &filename) return wxULongLong(lpFileSizeHigh, ret); #else // ! __WIN32__ wxStructStat st; -#ifndef wxNEED_WX_UNISTD_H - if (wxStat( filename.fn_str() , &st) != 0) -#else if (wxStat( filename, &st) != 0) -#endif return wxInvalidSize; return wxULongLong(st.st_size); #endif @@ -2654,27 +2668,54 @@ wxULongLong wxFileName::GetSize(const wxString &filename) /* static */ wxString wxFileName::GetHumanReadableSize(const wxULongLong &bs, const wxString &nullsize, - int precision) + int precision, + wxSizeConvention conv) { - static const double KILOBYTESIZE = 1024.0; - static const double MEGABYTESIZE = 1024.0*KILOBYTESIZE; - static const double GIGABYTESIZE = 1024.0*MEGABYTESIZE; - static const double TERABYTESIZE = 1024.0*GIGABYTESIZE; - - if (bs == 0 || bs == wxInvalidSize) + // deal with trivial case first + if ( bs == 0 || bs == wxInvalidSize ) return nullsize; - double bytesize = bs.ToDouble(); - if (bytesize < KILOBYTESIZE) - return wxString::Format(_("%s B"), bs.ToString().c_str()); - if (bytesize < MEGABYTESIZE) - return wxString::Format(_("%.*f kB"), precision, bytesize/KILOBYTESIZE); - if (bytesize < GIGABYTESIZE) - return wxString::Format(_("%.*f MB"), precision, bytesize/MEGABYTESIZE); - if (bytesize < TERABYTESIZE) - return wxString::Format(_("%.*f GB"), precision, bytesize/GIGABYTESIZE); + // depending on the convention used the multiplier may be either 1000 or + // 1024 and the binary infix may be empty (for "KB") or "i" (for "KiB") + double multiplier = 1024.; + wxString biInfix; + + switch ( conv ) + { + case wxSIZE_CONV_TRADITIONAL: + // nothing to do, this corresponds to the default values of both + // the multiplier and infix string + break; + + case wxSIZE_CONV_IEC: + biInfix = "i"; + break; + + case wxSIZE_CONV_SI: + multiplier = 1000; + break; + } + + const double kiloByteSize = multiplier; + const double megaByteSize = multiplier * kiloByteSize; + const double gigaByteSize = multiplier * megaByteSize; + const double teraByteSize = multiplier * gigaByteSize; + + const double bytesize = bs.ToDouble(); + + wxString result; + if ( bytesize < kiloByteSize ) + result.Printf("%s B", bs.ToString()); + else if ( bytesize < megaByteSize ) + result.Printf("%.*f K%sB", precision, bytesize/kiloByteSize, biInfix); + else if (bytesize < gigaByteSize) + result.Printf("%.*f M%sB", precision, bytesize/megaByteSize, biInfix); + else if (bytesize < teraByteSize) + result.Printf("%.*f G%sB", precision, bytesize/gigaByteSize, biInfix); + else + result.Printf("%.*f T%sB", precision, bytesize/teraByteSize, biInfix); - return wxString::Format(_("%.*f TB"), precision, bytesize/TERABYTESIZE); + return result; } wxULongLong wxFileName::GetSize() const @@ -2682,9 +2723,11 @@ wxULongLong wxFileName::GetSize() const return GetSize(GetFullPath()); } -wxString wxFileName::GetHumanReadableSize(const wxString &failmsg, int precision) const +wxString wxFileName::GetHumanReadableSize(const wxString& failmsg, + int precision, + wxSizeConvention conv) const { - return GetHumanReadableSize(GetSize(), failmsg, precision); + return GetHumanReadableSize(GetSize(), failmsg, precision, conv); } #endif // wxUSE_LONGLONG