X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b931e275f57c63d1976e370842877ac5d78da2b0..a2d6d210991fb1d29570a46e0f26c7a4b9cf89ff:/src/common/filefn.cpp diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index 5168a4cff5..5c8e11038e 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -19,18 +19,23 @@ // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" -#include "wx/defs.h" #ifdef __BORLANDC__ #pragma hdrstop #endif -#include "wx/utils.h" -#include "wx/intl.h" +#ifndef WX_PRECOMP + #include "wx/intl.h" + #include "wx/log.h" + #include "wx/utils.h" +#endif + #include "wx/file.h" // This does include filefn.h #include "wx/filename.h" #include "wx/dir.h" +#include "wx/tokenzr.h" + // there are just too many of those... #ifdef __VISUALC__ #pragma warning(disable:4706) // assignment within conditional expression @@ -50,8 +55,6 @@ #include "wx/mac/private.h" // includes mac headers #endif -#include "wx/log.h" - #ifdef __WINDOWS__ #include "wx/msw/private.h" #include "wx/msw/mslu.h" @@ -143,23 +146,25 @@ WXDLLEXPORT int wxOpen( const wxChar *pathname, int flags, mode_t mode ) // wxPathList // ---------------------------------------------------------------------------- -// IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList) - -static inline wxChar* MYcopystring(const wxString& s) +void wxPathList::Add(const wxString& path) { - wxChar* copy = new wxChar[s.length() + 1]; - return wxStrcpy(copy, s.c_str()); -} + // add a path separator to force wxFileName to interpret it always as a directory + // (i.e. if we are called with '/home/user' we want to consider it a folder and + // not, as wxFileName would consider, a filename). + wxFileName fn(path + wxFileName::GetPathSeparator()); -static inline wxChar* MYcopystring(const wxChar* s) -{ - wxChar* copy = new wxChar[wxStrlen(s) + 1]; - return wxStrcpy(copy, s); + // add only normalized relative/absolute paths + fn.Normalize(wxPATH_NORM_DOTS|wxPATH_NORM_TILDE|wxPATH_NORM_LONG|wxPATH_NORM_ENV_VARS); + + wxString toadd = fn.GetPath(); + if (Index(toadd) == wxNOT_FOUND) + wxArrayString::Add(toadd); // do not add duplicates } -void wxPathList::Add (const wxString& path) +void wxPathList::Add(const wxArrayString &arr) { - wxStringList::Add (WXSTRINGCAST path); + for (size_t j=0; j < arr.GetCount(); j++) + Add(arr[j]); } // Add paths e.g. from the PATH environment variable @@ -167,43 +172,24 @@ void wxPathList::AddEnvList (const wxString& WXUNUSED_IN_WINCE(envVariable)) { // No environment variables on WinCE #ifndef __WXWINCE__ + + // The space has been removed from the tokenizers, otherwise a + // path such as "C:\Program Files" would be split into 2 paths: + // "C:\Program" and "Files"; this is true for both Windows and Unix. + static const wxChar PATH_TOKS[] = #if defined(__WINDOWS__) || defined(__OS2__) - /* - The space has been removed from the tokenizers, otherwise a - path such as "C:\Program Files" would be split into 2 paths: - "C:\Program" and "Files" - */ -// wxT(" ;"); // Don't separate with colon in DOS (used for drive) wxT(";"); // Don't separate with colon in DOS (used for drive) #else - wxT(" :;"); + wxT(":;"); #endif - wxString val ; - if (wxGetEnv (WXSTRINGCAST envVariable, &val)) + wxString val; + if ( wxGetEnv(envVariable, &val) ) { - wxChar *s = MYcopystring (val); - wxChar *save_ptr, *token = wxStrtok (s, PATH_TOKS, &save_ptr); - - if (token) - { - Add(token); - while (token) - { - if ( (token = wxStrtok ((wxChar *) NULL, PATH_TOKS, &save_ptr)) - != NULL ) - { - Add(token); - } - } - } - - // suppress warning about unused variable save_ptr when wxStrtok() is a - // macro which throws away its third argument - save_ptr = token; - - delete [] s; + // split into an array of string the value of the env var + wxArrayString arr = wxStringTokenize(val, PATH_TOKS); + WX_APPEND_ARRAY(*this, arr); } #endif // !__WXWINCE__ } @@ -216,60 +202,47 @@ void wxPathList::EnsureFileAccessible (const wxString& path) wxString path_only(wxPathOnly(path)); if ( !path_only.empty() ) { - if ( !Member(path_only) ) + if ( Index(path_only) == wxNOT_FOUND ) Add(path_only); } } -bool wxPathList::Member (const wxString& path) +// deprecated ! +bool wxPathList::Member (const wxString& path) const { - for (wxStringList::compatibility_iterator node = GetFirst(); node; node = node->GetNext()) - { - wxString path2( node->GetData() ); - if ( -#if defined(__WINDOWS__) || defined(__OS2__) || defined(__VMS__) || defined(__WXMAC__) - // Case INDEPENDENT - path.CompareTo (path2, wxString::ignoreCase) == 0 -#else - // Case sensitive File System - path.CompareTo (path2) == 0 -#endif - ) - return true; - } - return false; + return Index(path) != wxNOT_FOUND; } -wxString wxPathList::FindValidPath (const wxString& file) +wxString wxPathList::FindValidPath (const wxString& file) const { - wxExpandPath(wxFileFunctionsBuffer, file); + // normalize the given string as it could be a path + a filename + // and not only a filename + wxFileName fn(file); + wxString strend; - wxChar buf[_MAXPATHLEN]; - wxStrcpy(buf, wxFileFunctionsBuffer); + // NB: normalize without making absolute ! + fn.Normalize(wxPATH_NORM_DOTS|wxPATH_NORM_TILDE|wxPATH_NORM_LONG|wxPATH_NORM_ENV_VARS); - wxChar *filename = wxIsAbsolutePath (buf) ? wxFileNameFromPath (buf) : (wxChar *)buf; + wxASSERT_MSG(!fn.IsDir(), wxT("Cannot search for directories; only for files")); + if (fn.IsAbsolute()) + strend = fn.GetFullName(); // search for the file name and ignore the path part + else + strend = fn.GetFullPath(); - for (wxStringList::compatibility_iterator node = GetFirst(); node; node = node->GetNext()) + for (size_t i=0; iGetData()); - wxStrcpy (wxFileFunctionsBuffer, path); - wxChar ch = wxFileFunctionsBuffer[wxStrlen(wxFileFunctionsBuffer)-1]; - if (ch != wxT('\\') && ch != wxT('/')) - wxStrcat (wxFileFunctionsBuffer, wxT("/")); - wxStrcat (wxFileFunctionsBuffer, filename); -#ifdef __WINDOWS__ - wxUnix2DosFilename (wxFileFunctionsBuffer); -#endif - if (wxFileExists (wxFileFunctionsBuffer)) - { - return wxString(wxFileFunctionsBuffer); // Found! - } - } // for() + wxString strstart = Item(i); + if (!strstart.IsEmpty() && strstart.Last() != wxFileName::GetPathSeparator()) + strstart += wxFileName::GetPathSeparator(); + + if (wxFileExists(strstart + strend)) + return strstart + strend; // Found! + } - return wxEmptyString; // Not found + return wxEmptyString; // Not found } -wxString wxPathList::FindAbsoluteValidPath (const wxString& file) +wxString wxPathList::FindAbsoluteValidPath (const wxString& file) const { wxString f = FindValidPath(file); if ( f.empty() || wxIsAbsolutePath(f) ) @@ -286,6 +259,23 @@ wxString wxPathList::FindAbsoluteValidPath (const wxString& file) return buf; } +// ---------------------------------------------------------------------------- +// miscellaneous global functions (TOFIX!) +// ---------------------------------------------------------------------------- + +static inline wxChar* MYcopystring(const wxString& s) +{ + wxChar* copy = new wxChar[s.length() + 1]; + return wxStrcpy(copy, s.c_str()); +} + +static inline wxChar* MYcopystring(const wxChar* s) +{ + wxChar* copy = new wxChar[wxStrlen(s) + 1]; + return wxStrcpy(copy, s); +} + + bool wxFileExists (const wxString& filename) { @@ -298,16 +288,19 @@ wxFileExists (const wxString& filename) return (ret != (DWORD)-1) && !(ret & FILE_ATTRIBUTE_DIRECTORY); #else // !__WIN32__ + #ifndef S_ISREG + #define S_ISREG(mode) ((mode) & S_IFREG) + #endif wxStructStat st; #ifndef wxNEED_WX_UNISTD_H - return (wxStat( filename.fn_str() , &st) == 0 && (st.st_mode & S_IFREG)) + return (wxStat( filename.fn_str() , &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( filename , &st) == 0 && (st.st_mode & S_IFREG); + return wxStat( filename , &st) == 0 && S_ISREG(st.st_mode); #endif #endif // __WIN32__/!__WIN32__ } @@ -877,13 +870,19 @@ wxString wxMacFSRefToPath( const FSRef *fsRef , CFStringRef additionalPathCompon } CFStringRef cfString = CFURLCopyFileSystemPath(fullURLRef, kDefaultPathStyle); CFRelease( fullURLRef ) ; - return wxMacCFStringHolder(cfString).AsString(wxLocale::GetSystemEncoding()); + CFMutableStringRef cfMutableString = CFStringCreateMutableCopy(NULL, 0, cfString); + CFRelease( cfString ); + CFStringNormalize(cfMutableString,kCFStringNormalizationFormC); + return wxMacCFStringHolder(cfMutableString).AsString(); } OSStatus wxMacPathToFSRef( const wxString&path , FSRef *fsRef ) { OSStatus err = noErr ; - CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, wxMacCFStringHolder(path ,wxLocale::GetSystemEncoding() ) , kDefaultPathStyle, false); + CFMutableStringRef cfMutableString = CFStringCreateMutableCopy(NULL, 0, wxMacCFStringHolder(path)); + CFStringNormalize(cfMutableString,kCFStringNormalizationFormD); + CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfMutableString , kDefaultPathStyle, false); + CFRelease( cfMutableString ); if ( NULL != url ) { if ( CFURLGetFSRef(url, fsRef) == false ) @@ -902,9 +901,14 @@ wxString wxMacHFSUniStrToString( ConstHFSUniStr255Param uniname ) CFStringRef cfname = CFStringCreateWithCharacters( kCFAllocatorDefault, uniname->unicode, uniname->length ); - return wxMacCFStringHolder(cfname).AsString() ; + CFMutableStringRef cfMutableString = CFStringCreateMutableCopy(NULL, 0, cfname); + CFRelease( cfname ); + CFStringNormalize(cfMutableString,kCFStringNormalizationFormC); + return wxMacCFStringHolder(cfMutableString).AsString() ; } +#ifndef __LP64__ + wxString wxMacFSSpec2MacFilename( const FSSpec *spec ) { FSRef fsRef ; @@ -922,6 +926,7 @@ void wxMacFilename2FSSpec( const wxString& path , FSSpec *spec ) wxMacPathToFSRef( path , &fsRef ) ; err = FSRefMakeFSSpec( &fsRef , spec ) ; } +#endif #endif // __WXMAC__ @@ -1106,8 +1111,19 @@ wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) } bool -wxRenameFile (const wxString& file1, const wxString& file2) +wxRenameFile(const wxString& file1, const wxString& file2, bool overwrite) { + if ( !overwrite && wxFileExists(file2) ) + { + wxLogSysError + ( + _("Failed to rename the file '%s' to '%s' because the destination file already exists."), + file1.c_str(), file2.c_str() + ); + + return false; + } + #if !defined(__WXWINCE__) && !defined(__WXPALMOS__) // Normal system call if ( wxRename (file1, file2) == 0 ) @@ -1115,7 +1131,7 @@ wxRenameFile (const wxString& file1, const wxString& file2) #endif // Try to copy - if (wxCopyFile(file1, file2)) { + if (wxCopyFile(file1, file2, overwrite)) { wxRemoveFile(file1); return true; } @@ -1264,9 +1280,8 @@ bool wxDirExists(const wxChar *pszPathName) // Get a temporary filename, opening and closing the file. wxChar *wxGetTempFileName(const wxString& prefix, wxChar *buf) { -#if wxUSE_FILE - wxString filename = wxFileName::CreateTempFileName(prefix); - if ( filename.empty() ) + wxString filename; + if ( !wxGetTempFileName(prefix, filename) ) return NULL; if ( buf ) @@ -1275,19 +1290,20 @@ wxChar *wxGetTempFileName(const wxString& prefix, wxChar *buf) buf = MYcopystring(filename); return buf; -#else - wxUnusedVar(prefix); - wxUnusedVar(buf); - // wxFileName::CreateTempFileName needs wxFile class enabled - return NULL; -#endif } bool wxGetTempFileName(const wxString& prefix, wxString& buf) { - buf = wxGetTempFileName(prefix); +#if wxUSE_FILE + buf = wxFileName::CreateTempFileName(prefix); return !buf.empty(); +#else // !wxUSE_FILE + wxUnusedVar(prefix); + wxUnusedVar(buf); + + return false; +#endif // wxUSE_FILE/!wxUSE_FILE } // Get first file name matching given wild card. @@ -1387,7 +1403,7 @@ wxChar *wxDoGetCwd(wxChar *buf, int sz) #ifdef HAVE_WGETCWD #if wxUSE_UNICODE_MSLU - if ( wxGetOsVersion() != wxWIN95 ) + if ( wxGetOsVersion() != wxOS_WINDOWS_9X ) #else char *cbuf = NULL; // never really used because needsANSI will always be false #endif @@ -1621,48 +1637,19 @@ void WXDLLEXPORT wxSplitPath(const wxChar *pszFileName, wxFileName::SplitPath(pszFileName, pstrPath, pstrName, pstrExt); } +#if wxUSE_DATETIME + time_t WXDLLEXPORT wxFileModificationTime(const wxString& filename) { -#if defined(__WXPALMOS__) - return 0; -#elif defined(__WXWINCE__) - FILETIME ftLastWrite; - AutoHANDLE hFile(::CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, - NULL, 0, FILE_ATTRIBUTE_NORMAL, 0)); - - if ( !hFile.IsOk() ) - return 0; + wxDateTime mtime; + if ( !wxFileName(filename).GetTimes(NULL, &mtime, NULL) ) + return (time_t)-1; - if ( !::GetFileTime(hFile, NULL, NULL, &ftLastWrite) ) - return 0; - - // sure we want to translate to local time here? - FILETIME ftLocal; - if ( !::FileTimeToLocalFileTime(&ftLastWrite, &ftLocal) ) - { - wxLogLastError(_T("FileTimeToLocalFileTime")); - } - - // FILETIME is a counted in 100-ns since 1601-01-01, convert it to - // number of seconds since 1970-01-01 - ULARGE_INTEGER uli; - uli.LowPart = ftLocal.dwLowDateTime; - uli.HighPart = ftLocal.dwHighDateTime; - - ULONGLONG ull = uli.QuadPart; - ull /= wxULL(10000000); // number of 100ns intervals in 1s - ull -= wxULL(11644473600); // 1970-01-01 - 1601-01-01 in seconds - - return wx_static_cast(time_t, ull); -#else - wxStructStat buf; - if ( wxStat( filename, &buf) != 0 ) - return 0; - - return buf.st_mtime; -#endif + return mtime.GetTicks(); } +#endif // wxUSE_DATETIME + // Parses the filterStr, returning the number of filters. // Returns 0 if none or if there's a problem. @@ -1775,6 +1762,82 @@ int WXDLLEXPORT wxParseCommonDialogsFilter(const wxString& filterStr, return filters.GetCount(); } +#if defined( __WINDOWS__ ) +bool wxCheckGenericPermission(const wxString &path, DWORD access) +{ + // quoting the MSDN: "To obtain a handle to a directory, call the + // CreateFile function with the FILE_FLAG_BACKUP_SEMANTICS flag" + wxWinVersion ver = wxGetWinVersion(); + bool isdir = wxDirExists(path); + if (isdir && (ver == wxWinVersion_95 || ver == wxWinVersion_98 || ver == wxWinVersion_ME)) + { + // however Win95/98/ME do not support FILE_FLAG_BACKUP_SEMANTICS... + if (access == GENERIC_READ) + { + WIN32_FILE_ATTRIBUTE_DATA data; + if (GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &data) == 0) + return false; // cannot query attributes + return (data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0; + } + + // FIXME: is it true that directories are always writable & executable on Win9X family ? + return true; + } + else + { + HANDLE h = CreateFile(path.c_str(), access, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, + OPEN_EXISTING, isdir ? FILE_FLAG_BACKUP_SEMANTICS : 0, NULL); + if (h != INVALID_HANDLE_VALUE) + CloseHandle(h); + + return h != INVALID_HANDLE_VALUE; + } +} +#endif + +bool wxIsWritable(const wxString &path) +{ +#if defined( __UNIX__ ) + // access() will take in count also symbolic links + return access(wxConvFile.cWX2MB(path), W_OK) == 0; +#elif defined( __WINDOWS__ ) + return wxCheckGenericPermission(path, GENERIC_WRITE); +#else + wxUnusedVar(path); + // TODO + return false; +#endif +} + +bool wxIsReadable(const wxString &path) +{ +#if defined( __UNIX__ ) + // access() will take in count also symbolic links + return access(wxConvFile.cWX2MB(path), R_OK) == 0; +#elif defined( __WINDOWS__ ) + return wxCheckGenericPermission(path, GENERIC_READ); +#else + wxUnusedVar(path); + // TODO + return false; +#endif +} + +bool wxIsExecutable(const wxString &path) +{ +#if defined( __UNIX__ ) + // access() will take in count also symbolic links + return access(wxConvFile.cWX2MB(path), X_OK) == 0; +#elif defined( __WINDOWS__ ) + return wxCheckGenericPermission(path, GENERIC_EXECUTE); +#else + wxUnusedVar(path); + // TODO + return false; +#endif +} + //------------------------------------------------------------------------ // wild character routines