X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2587df2c82960db1b9547b24d1cd895b1b8a5102..dbe0872fc80f7fc9ac37235ab1f8ba8224b0bfa5:/src/common/filefn.cpp diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index b9053ba8ac..cc9f2c89b0 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -96,12 +96,17 @@ #define _MAXPATHLEN 1024 #endif +#ifndef INVALID_FILE_ATTRIBUTES + #define INVALID_FILE_ATTRIBUTES ((DWORD)-1) +#endif + // ---------------------------------------------------------------------------- // private globals // ---------------------------------------------------------------------------- -// MT-FIXME: get rid of this horror and all code using it +#if WXWIN_COMPATIBILITY_2_8 static wxChar wxFileFunctionsBuffer[4*_MAXPATHLEN]; +#endif #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 // @@ -111,13 +116,6 @@ static wxChar wxFileFunctionsBuffer[4*_MAXPATHLEN]; const int wxInvalidOffset = -1; #endif -// ---------------------------------------------------------------------------- -// macros -// ---------------------------------------------------------------------------- - -// translate the filenames before passing them to OS functions -#define OS_FILENAME(s) (s.fn_str()) - // ============================================================================ // implementation // ============================================================================ @@ -133,7 +131,7 @@ const int wxInvalidOffset = -1; // regardless of the mode parameter. This hack works around the problem by // setting the mode with _wchmod. // -int wxCRT_Open(const wchar_t *pathname, int flags, mode_t mode) +int wxCRT_OpenW(const wchar_t *pathname, int flags, mode_t mode) { int moreflags = 0; @@ -289,9 +287,10 @@ wxString wxPathList::FindAbsoluteValidPath (const wxString& file) const } // ---------------------------------------------------------------------------- -// miscellaneous global functions (TOFIX!) +// miscellaneous global functions // ---------------------------------------------------------------------------- +#if WXWIN_COMPATIBILITY_2_8 static inline wxChar* MYcopystring(const wxString& s) { wxChar* copy = new wxChar[s.length() + 1]; @@ -304,6 +303,7 @@ static inline CharType* MYcopystring(const CharType* s) CharType* copy = new CharType[wxStrlen(s) + 1]; return wxStrcpy(copy, s); } +#endif bool @@ -316,7 +316,7 @@ wxFileExists (const wxString& filename) // it can cope with network (UNC) paths unlike them DWORD ret = ::GetFileAttributes(filename.fn_str()); - return (ret != (DWORD)-1) && !(ret & FILE_ATTRIBUTE_DIRECTORY); + return (ret != INVALID_FILE_ATTRIBUTES) && !(ret & FILE_ATTRIBUTE_DIRECTORY); #else // !__WIN32__ #ifndef S_ISREG #define S_ISREG(mode) ((mode) & S_IFREG) @@ -356,6 +356,7 @@ wxIsAbsolutePath (const wxString& filename) return false ; } +#if WXWIN_COMPATIBILITY_2_8 /* * Strip off any extension (dot something) from end of file, * IF one exists. Inserts zero into buffer. @@ -383,16 +384,7 @@ void wxStripExtension(wchar_t *buffer) { wxDoStripExtension(buffer); } void wxStripExtension(wxString& buffer) { - //RN: Be careful about the handling the case where - //buffer.length() == 0 - for(size_t i = buffer.length() - 1; i != wxString::npos; --i) - { - if (buffer.GetChar(i) == wxT('.')) - { - buffer = buffer.Left(i); - break; - } - } + buffer = wxFileName::StripExtension(buffer); } // Destructive removal of /./ and /../ stuff @@ -474,7 +466,7 @@ wxString wxRealPath(const wxString& path) wxChar *wxCopyAbsolutePath(const wxString& filename) { if (filename.empty()) - return (wxChar *) NULL; + return NULL; if (! wxIsAbsolutePath(wxExpandPath(wxFileFunctionsBuffer, filename))) { @@ -707,7 +699,7 @@ wxContractPath (const wxString& filename, static wxChar dest[_MAXPATHLEN]; if (filename.empty()) - return (wxChar *) NULL; + return NULL; wxStrcpy (dest, filename); #ifdef __WXMSW__ @@ -751,6 +743,8 @@ wxContractPath (const wxString& filename, return dest; } +#endif // #if WXWIN_COMPATIBILITY_2_8 + // Return just the filename, not the path (basename) wxChar *wxFileNameFromPath (wxChar *path) { @@ -818,7 +812,7 @@ wxPathOnly (wxChar *path) } #endif } - return (wxChar *) NULL; + return NULL; } // Return just the directory, or NULL if no directory @@ -945,13 +939,16 @@ void wxMacFilename2FSSpec( const wxString& path , FSSpec *spec ) OSStatus err = noErr; FSRef fsRef; wxMacPathToFSRef( path , &fsRef ); - err = FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, spec, NULL); + err = FSGetCatalogInfo(&fsRef, kFSCatInfoNone, NULL, NULL, spec, NULL); verify_noerr( err ); } #endif #endif // __WXMAC__ + +#if WXWIN_COMPATIBILITY_2_8 + template static void wxDoDos2UnixFilename(T *s) { @@ -994,6 +991,8 @@ wxDoUnix2DosFilename(T *WXUNUSED(s) ) void wxUnix2DosFilename(char *s) { wxDoUnix2DosFilename(s); } void wxUnix2DosFilename(wchar_t *s) { wxDoUnix2DosFilename(s); } +#endif // #if WXWIN_COMPATIBILITY_2_8 + // Concatenate two files to form third bool wxConcatFiles (const wxString& file1, const wxString& file2, const wxString& file3) @@ -1173,7 +1172,7 @@ wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) #if !defined(__VISAGECPP__) && !defined(__WXMAC__) || defined(__UNIX__) // no chmod in VA. Should be some permission API for HPFS386 partitions // however - if ( chmod(OS_FILENAME(file2), fbuf.st_mode) != 0 ) + if ( chmod(file2.fn_str(), fbuf.st_mode) != 0 ) { wxLogSysError(_("Impossible to set permissions for the file '%s'"), file2.c_str()); @@ -1238,7 +1237,7 @@ bool wxRemoveFile(const wxString& file) int res = 1; // TODO with VFSFileDelete() #else - int res = unlink(OS_FILENAME(file)); + int res = unlink(file.fn_str()); #endif return res == 0; @@ -1248,14 +1247,16 @@ bool wxMkdir(const wxString& dir, int perm) { #if defined(__WXPALMOS__) return false; -#elif defined(__WXMAC__) && !defined(__UNIX__) - return (mkdir(dir.fn_str() , 0 ) == 0); -#else // !Mac - const wxChar *dirname = dir.c_str(); +#else +#if defined(__WXMAC__) && !defined(__UNIX__) + if ( mkdir(dir.fn_str(), 0) != 0 ) // assume mkdir() has 2 args on non Windows-OS/2 platforms and on Windows too // for the GNU compiler -#if (!(defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__))) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) || defined(__WINE__) || defined(__WXMICROWIN__) +#elif (!(defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__))) || \ + (defined(__GNUWIN32__) && !defined(__MINGW32__)) || \ + defined(__WINE__) || defined(__WXMICROWIN__) + const wxChar *dirname = dir.c_str(); #if defined(MSVCRT) wxUnusedVar(perm); if ( mkdir(wxFNCONV(dirname)) != 0 ) @@ -1264,8 +1265,9 @@ bool wxMkdir(const wxString& dir, int perm) #endif #elif defined(__OS2__) wxUnusedVar(perm); - if (::DosCreateDir((PSZ)dirname, NULL) != 0) // enhance for EAB's?? + if (::DosCreateDir(dir.c_str(), NULL) != 0) // enhance for EAB's?? #elif defined(__DOS__) + const wxChar *dirname = dir.c_str(); #if defined(__WATCOMC__) (void)perm; if ( wxMkDir(wxFNSTRINGCAST wxFNCONV(dirname)) != 0 ) @@ -1276,36 +1278,43 @@ bool wxMkdir(const wxString& dir, int perm) #endif #else // !MSW, !DOS and !OS/2 VAC++ wxUnusedVar(perm); -#ifdef __WXWINCE__ - if ( !CreateDirectory(dirname, NULL) ) -#else + #ifdef __WXWINCE__ + if ( CreateDirectory(dir.fn_str(), NULL) == 0 ) + #else if ( wxMkDir(dir.fn_str()) != 0 ) -#endif + #endif #endif // !MSW/MSW { - wxLogSysError(_("Directory '%s' couldn't be created"), dirname); - + wxLogSysError(_("Directory '%s' couldn't be created"), dir); return false; } return true; -#endif // Mac/!Mac +#endif // PALMOS/!PALMOS } bool wxRmdir(const wxString& dir, int WXUNUSED(flags)) { #if defined(__VMS__) return false; //to be changed since rmdir exists in VMS7.x -#elif defined(__OS2__) - return (::DosDeleteDir(dir.c_str()) == 0); -#elif defined(__WXWINCE__) - return (RemoveDirectory(dir) != 0); #elif defined(__WXPALMOS__) // TODO with VFSFileRename() return false; #else - return (wxRmDir(OS_FILENAME(dir)) == 0); -#endif + #if defined(__OS2__) + if ( ::DosDeleteDir(dir.c_str()) != 0 ) + #elif defined(__WXWINCE__) + if ( RemoveDirectory(dir.fn_str()) == 0 ) + #else + if ( wxRmDir(dir.fn_str()) != 0 ) + #endif + { + wxLogSysError(_("Directory '%s' couldn't be deleted"), dir); + return false; + } + + return true; +#endif // PALMOS/!PALMOS } // does the path exists? (may have or not '/' or '\\' at the end) @@ -1339,7 +1348,7 @@ bool wxDirExists(const wxString& pathName) // stat() can't cope with network paths DWORD ret = ::GetFileAttributes(strPath.fn_str()); - return (ret != (DWORD)-1) && (ret & FILE_ATTRIBUTE_DIRECTORY); + return (ret != INVALID_FILE_ATTRIBUTES) && (ret & FILE_ATTRIBUTE_DIRECTORY); #elif defined(__OS2__) FILESTATUS3 Info = {{0}}; APIRET rc = ::DosQueryPathInfo((PSZ)(WXSTRINGCAST strPath), FIL_STANDARD, @@ -1361,6 +1370,8 @@ bool wxDirExists(const wxString& pathName) #endif // __WIN32__/!__WIN32__ } +#if WXWIN_COMPATIBILITY_2_8 + // Get a temporary filename, opening and closing the file. wxChar *wxGetTempFileName(const wxString& prefix, wxChar *buf) { @@ -1395,6 +1406,8 @@ bool wxGetTempFileName(const wxString& prefix, wxString& buf) #endif // wxUSE_FILE/!wxUSE_FILE } +#endif // #if WXWIN_COMPATIBILITY_2_8 + // Get first file name matching given wild card. static wxDir *gs_dir = NULL; @@ -1402,14 +1415,13 @@ static wxString gs_dirPath; wxString wxFindFirstFile(const wxString& spec, int flags) { - wxSplitPath(spec, &gs_dirPath, NULL, NULL); + wxFileName::SplitPath(spec, &gs_dirPath, NULL, NULL); if ( gs_dirPath.empty() ) gs_dirPath = wxT("."); if ( !wxEndsWithPathSeparator(gs_dirPath ) ) gs_dirPath << wxFILE_SEP_PATH; - if (gs_dir) - delete gs_dir; + delete gs_dir; // can be NULL, this is ok gs_dir = new wxDir(gs_dirPath); if ( !gs_dir->IsOpened() ) @@ -1439,7 +1451,7 @@ wxString wxFindFirstFile(const wxString& spec, int flags) wxString wxFindNextFile() { - wxASSERT_MSG( gs_dir, wxT("You must call wxFindFirstFile before!") ); + wxCHECK_MSG( gs_dir, "", "You must call wxFindFirstFile before!" ); wxString result; gs_dir->GetNext(&result); @@ -1517,7 +1529,7 @@ wxChar *wxDoGetCwd(wxChar *buf, int sz) { sz -= 3; rc = ::DosQueryCurrentDir( 0 // current drive - ,cbuf + 3 + ,(PBYTE)cbuf + 3 ,(PULONG)&sz ); cbuf[0] = char('A' + (ulDriveNum - 1)); @@ -1657,7 +1669,7 @@ wxString wxGetOSDirectory() wxChar buf[256]; GetWindowsDirectory(buf, 256); return wxString(buf); -#elif defined(__WXMAC__) && !defined(__WXOSX_IPHONE__) +#elif defined(__WXMAC__) && wxOSX_USE_CARBON return wxMacFindFolder(kOnSystemDisk, 'macs', false); #else return wxEmptyString; @@ -1702,6 +1714,7 @@ bool wxFindFileInPath(wxString *pStr, const wxString& szPath, const wxString& sz return false; } +#if WXWIN_COMPATIBILITY_2_8 void WXDLLIMPEXP_BASE wxSplitPath(const wxString& fileName, wxString *pstrPath, wxString *pstrName, @@ -1709,6 +1722,7 @@ void WXDLLIMPEXP_BASE wxSplitPath(const wxString& fileName, { wxFileName::SplitPath(fileName, pstrPath, pstrName, pstrExt); } +#endif // #if WXWIN_COMPATIBILITY_2_8 #if wxUSE_DATETIME @@ -1841,12 +1855,20 @@ static bool wxCheckWin32Permission(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", but this // doesn't work under Win9x/ME but then it's not needed there anyhow - bool isdir = wxDirExists(path); - if ( isdir && wxGetOsVersion() == wxOS_WINDOWS_9X ) + const DWORD dwAttr = ::GetFileAttributes(path.fn_str()); + if ( dwAttr == INVALID_FILE_ATTRIBUTES ) + { + // file probably doesn't exist at all + return false; + } + + if ( wxGetOsVersion() == wxOS_WINDOWS_9X ) { // FAT directories always allow all access, even if they have the - // readonly flag set - return true; + // readonly flag set, and FAT files can only be read-only + return (dwAttr & FILE_ATTRIBUTE_DIRECTORY) || + (access != GENERIC_WRITE || + !(dwAttr & FILE_ATTRIBUTE_READONLY)); } HANDLE h = ::CreateFile @@ -1856,7 +1878,9 @@ static bool wxCheckWin32Permission(const wxString& path, DWORD access) FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, - isdir ? FILE_FLAG_BACKUP_SEMANTICS : 0, + dwAttr & FILE_ATTRIBUTE_DIRECTORY + ? FILE_FLAG_BACKUP_SEMANTICS + : 0, NULL ); if ( h != INVALID_HANDLE_VALUE )