X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a28ae4096bb21302ce2ad59ca846ce590f8488bf..9ac71bef070f553f4ecef2c549dba33b10422f06:/src/common/filefn.cpp diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index cfc915390c..7a30b5eb6f 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -68,6 +68,7 @@ #ifdef __UNIX__ #include #include + #include #endif #ifdef __WXPM__ @@ -114,12 +115,15 @@ #ifdef __WINDOWS__ #include + #include "wx/msw/mslu.h" // sys/cygwin.h is needed for cygwin_conv_to_full_win32_path() // // note that it must be included after #ifdef __GNUWIN32__ - #include + #ifdef __CYGWIN__ + #include + #endif #include #ifndef __TWIN32__ #include @@ -140,8 +144,6 @@ #define _MAXPATHLEN 1024 #endif -extern wxChar *wxBuffer; - #ifdef __WXMAC__ # include "MoreFiles.h" # include "MoreFilesExtras.h" @@ -149,12 +151,11 @@ extern wxChar *wxBuffer; # include "FSpCompat.h" #endif -IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList) - // ---------------------------------------------------------------------------- // private globals // ---------------------------------------------------------------------------- +// MT-FIXME: get rid of this horror and all code using it static wxChar wxFileFunctionsBuffer[4*_MAXPATHLEN]; #if defined(__VISAGECPP__) && __IBMCPP__ >= 400 @@ -176,6 +177,32 @@ const off_t wxInvalidOffset = (off_t)-1; // implementation // ============================================================================ +#ifdef wxNEED_WX_UNISTD_H + +WXDLLEXPORT int wxStat( const wxChar *file_name, wxStructStat *buf ) +{ + return stat( wxConvFile.cWX2MB( file_name ), buf ); +} + +WXDLLEXPORT int wxAccess( const wxChar *pathname, int mode ) +{ + return access( wxConvFile.cWX2MB( pathname ), mode ); +} + +WXDLLEXPORT int wxOpen( const wxChar *pathname, int flags, mode_t mode ) +{ + return open( wxConvFile.cWX2MB( pathname ), flags, mode ); +} + +#endif + // wxNEED_WX_UNISTD_H + +// ---------------------------------------------------------------------------- +// wxPathList +// ---------------------------------------------------------------------------- + +IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList) + void wxPathList::Add (const wxString& path) { wxStringList::Add (WXSTRINGCAST path); @@ -306,7 +333,7 @@ wxFileExists (const wxString& filename) return (ret != (DWORD)-1) && !(ret & FILE_ATTRIBUTE_DIRECTORY); #else wxStructStat stbuf; - if ( !filename.empty() && wxStat (OS_FILENAME(filename), &stbuf) == 0 ) + if ( !filename.empty() && wxStat( filename, &stbuf) == 0 ) return TRUE; return FALSE; @@ -668,11 +695,6 @@ wxContractPath (const wxString& filename, const wxString& envname, const wxStrin wxStrcpy(wxFileFunctionsBuffer, wxT("~")); if (user != wxT("")) wxStrcat(wxFileFunctionsBuffer, (const wxChar*) user); -#ifdef __WXMSW__ -// strcat(wxFileFunctionsBuffer, "\\"); -#else -// strcat(wxFileFunctionsBuffer, "/"); -#endif wxStrcat(wxFileFunctionsBuffer, dest + len); wxStrcpy (dest, wxFileFunctionsBuffer); } @@ -680,75 +702,27 @@ wxContractPath (const wxString& filename, const wxString& envname, const wxStrin return dest; } -// Return just the filename, not the path -// (basename) +// Return just the filename, not the path (basename) wxChar *wxFileNameFromPath (wxChar *path) { - if (path) - { - register wxChar *tcp; + wxString p = path; + wxString n = wxFileNameFromPath(p); - tcp = path + wxStrlen (path); - while (--tcp >= path) - { -#if defined(__WXMAC__) && !defined(__DARWIN__) - // Classic or Carbon CodeWarrior like - // Carbon with Apple DevTools is Unix like - if (*tcp == wxT(':')) - return tcp + 1; -#else - // Unix like or Windows - if (*tcp == wxT('/') || *tcp == wxT('\\')) - return tcp + 1; -#endif -#ifdef __VMS__ - if (*tcp == wxT(':') || *tcp == wxT(']')) - return tcp + 1; -#endif - } /* while */ -#if defined(__WXMSW__) || defined(__WXPM__) - // MSDOS like - if (wxIsalpha (*path) && *(path + 1) == wxT(':')) - return path + 2; -#endif - } - return path; + return path + p.length() - n.length(); } -wxString wxFileNameFromPath (const wxString& path1) +wxString wxFileNameFromPath (const wxString& path) { - if (path1 != wxT("")) - { - wxChar *path = WXSTRINGCAST path1 ; - register wxChar *tcp; + wxString name, ext; + wxFileName::SplitPath(path, NULL, &name, &ext); - tcp = path + wxStrlen (path); - while (--tcp >= path) - { -#if defined(__WXMAC__) && !defined(__DARWIN__) - // Classic or Carbon CodeWarrior like - // Carbon with Apple DevTools is Unix like - if (*tcp == wxT(':') ) - return wxString(tcp + 1); -#else - // Unix like or Windows - if (*tcp == wxT('/') || *tcp == wxT('\\')) - return wxString(tcp + 1); -#endif -#ifdef __VMS__ - if (*tcp == wxT(':') || *tcp == wxT(']')) - return wxString(tcp + 1); -#endif - } /* while */ -#if defined(__WXMSW__) || defined(__WXPM__) - // MSDOS like - if (wxIsalpha (*path) && *(path + 1) == wxT(':')) - return wxString(path + 2); -#endif + wxString fullname = name; + if ( !ext.empty() ) + { + fullname << wxFILE_SEP_EXT << ext; } - // Yes, this should return the path, not an empty string, otherwise - // we get "thing.txt" -> "". - return path1; + + return fullname; } // Return just the directory, or NULL if no directory @@ -872,13 +846,55 @@ wxString wxPathOnly (const wxString& path) wxString wxMacFSSpec2MacFilename( const FSSpec *spec ) { #ifdef __DARWIN__ - FSRef theRef; - char thePath[FILENAME_MAX]; - - // convert the FSSpec to an FSRef - (void) FSpMakeFSRef( spec, &theRef ); - // get the POSIX path associated with the FSRef - (void) FSRefMakePath( &theRef, (UInt8 *)thePath, sizeof(thePath) ); + int i; + int j; + OSErr theErr; + OSStatus theStatus; + Boolean isDirectory = false; + Str255 theParentPath = "\p"; + FSSpec theParentSpec; + FSRef theParentRef; + char theFileName[FILENAME_MAX]; + char thePath[FILENAME_MAX]; + + strcpy(thePath, ""); + + // GD: Separate file name from path and make a FSRef to the parent + // directory. This is necessary since FSRefs cannot reference files + // that have not yet been created. + // Based on example code from Apple Technical Note TN2022 + // http://developer.apple.com/technotes/tn/tn2022.html + + // check whether we are converting a directory + isDirectory = ((spec->name)[spec->name[0]] == ':'); + // count length of file name + for (i = spec->name[0] - (isDirectory ? 1 : 0); ((spec->name[i] != ':') && (i > 0)); i--); + // copy file name + // prepend path separator since it will later be appended to the path + theFileName[0] = wxFILE_SEP_PATH; + for (j = i + 1; j <= spec->name[0] - (isDirectory ? 1 : 0); j++) { + theFileName[j - i] = spec->name[j]; + } + theFileName[j - i] = '\0'; + // copy path if any + for (j = 1; j <= i; j++) { + theParentPath[++theParentPath[0]] = spec->name[j]; + } + theErr = FSMakeFSSpec(spec->vRefNum, spec->parID, theParentPath, &theParentSpec); + if (theErr == noErr) { + // convert the FSSpec to an FSRef + theErr = FSpMakeFSRef(&theParentSpec, &theParentRef); + } + if (theErr == noErr) { + // get the POSIX path associated with the FSRef + theStatus = FSRefMakePath(&theParentRef, + (UInt8 *)thePath, sizeof(thePath)); + } + if (theStatus == noErr) { + // append file name to path + // includes previously prepended path separator + strcat(thePath, theFileName); + } // create path string for return value wxString result( thePath ) ; @@ -904,26 +920,40 @@ wxString wxMacFSSpec2MacFilename( const FSSpec *spec ) return result ; } +#ifndef __DARWIN__ +// Mac file names are POSIX (Unix style) under Darwin +// therefore the conversion functions below are not needed + +static char sMacFileNameConversion[ 1000 ] ; +#endif void wxMacFilename2FSSpec( const char *path , FSSpec *spec ) { + OSStatus err = noErr ; #ifdef __DARWIN__ FSRef theRef; // get the FSRef associated with the POSIX path - (void) FSPathMakeRef((const UInt8 *) path, &theRef, NULL); + err = FSPathMakeRef((const UInt8 *) path, &theRef, NULL); // convert the FSRef to an FSSpec - (void) FSGetCatalogInfo(&theRef, kFSCatInfoNone, NULL, NULL, spec, NULL); + err = FSGetCatalogInfo(&theRef, kFSCatInfoNone, NULL, NULL, spec, NULL); #else - FSpLocationFromFullPath( strlen(path) , path , spec ) ; + if ( strchr( path , ':' ) == NULL ) + { + // try whether it is a volume / or a mounted volume + strncpy( sMacFileNameConversion , path , 1000 ) ; + sMacFileNameConversion[998] = 0 ; + strcat( sMacFileNameConversion , ":" ) ; + err = FSpLocationFromFullPath( strlen(sMacFileNameConversion) , sMacFileNameConversion , spec ) ; + } + else + { + err = FSpLocationFromFullPath( strlen(path) , path , spec ) ; + } #endif } #ifndef __DARWIN__ -// Mac file names are POSIX (Unix style) under Darwin -// therefore the conversion functions below are not needed - -static char sMacFileNameConversion[ 1000 ] ; wxString wxMac2UnixFilename (const char *str) { @@ -1049,9 +1079,9 @@ wxConcatFiles (const wxString& file1, const wxString& file2, const wxString& fil FILE *fp2 = (FILE *) NULL; FILE *fp3 = (FILE *) NULL; // Open the inputs and outputs - if ((fp1 = wxFopen (OS_FILENAME( file1 ), wxT("rb"))) == NULL || - (fp2 = wxFopen (OS_FILENAME( file2 ), wxT("rb"))) == NULL || - (fp3 = wxFopen (OS_FILENAME( outfile ), wxT("wb"))) == NULL) + if ((fp1 = wxFopen ( file1, wxT("rb"))) == NULL || + (fp2 = wxFopen ( file2, wxT("rb"))) == NULL || + (fp3 = wxFopen ( outfile, wxT("wb"))) == NULL) { if (fp1) fclose (fp1); @@ -1085,17 +1115,21 @@ wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) // instead of our code if available // // NB: 3rd parameter is bFailIfExists i.e. the inverse of overwrite - return ::CopyFile(file1, file2, !overwrite) != 0; + if ( !::CopyFile(file1, file2, !overwrite) ) + { + wxLogSysError(_("Failed to copy the file '%s' to '%s'"), + file1.c_str(), file2.c_str()); + + return FALSE; + } #elif defined(__WXPM__) - if (::DosCopy(file2, file2, overwrite ? DCPY_EXISTING : 0) == 0) - return TRUE; - else + if ( ::DosCopy(file2, file2, overwrite ? DCPY_EXISTING : 0) != 0 ) return FALSE; #else // !Win32 - wxStructStat fbuf; + wxStructStat fbuf; // get permissions of file1 - if ( wxStat(OS_FILENAME(file1), &fbuf) != 0 ) + if ( wxStat( file1.c_str(), &fbuf) != 0 ) { // the file probably doesn't exist or we haven't the rights to read // from it anyhow @@ -1126,6 +1160,7 @@ wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) // create file2 with the same permissions than file1 and open it for // writing + wxFile fileOut; if ( !fileOut.Create(file2, overwrite, fbuf.st_mode & 0777) ) return FALSE; @@ -1168,9 +1203,9 @@ wxCopyFile (const wxString& file1, const wxString& file2, bool overwrite) return FALSE; } #endif // OS/2 || Mac +#endif // __WXMSW__ && __WIN32__ return TRUE; -#endif // __WXMSW__ && __WIN32__ } bool @@ -1284,12 +1319,10 @@ bool wxPathExists(const wxChar *pszPathName) wxStructStat st; #ifndef __VISAGECPP__ - return wxStat(wxFNSTRINGCAST strPath.fn_str(), &st) == 0 && - ((st.st_mode & S_IFMT) == S_IFDIR); + return wxStat(pszPathName, &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(wxFNSTRINGCAST strPath.fn_str(), &st) == 0 && - (st.st_mode == S_IFDIR); + return wxStat(pszPathName, &st) == 0 && (st.st_mode == S_IFDIR); #endif #endif // __WIN32__/!__WIN32__ @@ -1324,7 +1357,7 @@ static wxString gs_dirPath; wxString wxFindFirstFile(const wxChar *spec, int flags) { - gs_dirPath = wxPathOnly(spec); + wxSplitPath(spec, &gs_dirPath, NULL, NULL); if ( gs_dirPath.IsEmpty() ) gs_dirPath = wxT("."); if ( gs_dirPath.Last() != wxFILE_SEP_PATH ) @@ -1349,7 +1382,7 @@ wxString wxFindFirstFile(const wxChar *spec, int flags) } wxString result; - gs_dir->GetFirst(&result, wxFileNameFromPath(spec), dirFlags); + gs_dir->GetFirst(&result, wxFileNameFromPath(wxString(spec)), dirFlags); if ( result.IsEmpty() ) { wxDELETE(gs_dir); @@ -1386,21 +1419,40 @@ wxChar *wxGetWorkingDirectory(wxChar *buf, int sz) buf = new wxChar[sz + 1]; } - bool ok; + bool ok = FALSE; // for the compilers which have Unicode version of _getcwd(), call it // directly, for the others call the ANSI version and do the translation -#if wxUSE_UNICODE +#if !wxUSE_UNICODE + #define cbuf buf +#else // wxUSE_UNICODE + bool needsANSI = TRUE; + + #if !defined(HAVE_WGETCWD) || wxUSE_UNICODE_MSLU + // This is not legal code as the compiler + // is allowed destroy the wxCharBuffer. + // wxCharBuffer c_buffer(sz); + // char *cbuf = (char*)(const char*)c_buffer; + char cbuf[_MAXPATHLEN]; + #endif + #ifdef HAVE_WGETCWD - ok = _wgetcwd(buf, sz) != NULL; - #else // !HAVE_WGETCWD - wxCharBuffer cbuf(sz); + #if wxUSE_UNICODE_MSLU + if ( wxGetOsVersion() != wxWIN95 ) + #else + char *cbuf = NULL; // never really used because needsANSI will always be FALSE + #endif + { + ok = _wgetcwd(buf, sz) != NULL; + needsANSI = FALSE; + } #endif -#endif // -#if !wxUSE_UNICODE || !defined(HAVE_WGETCWD) + if ( needsANSI ) +#endif // wxUSE_UNICODE + { #ifdef _MSC_VER - ok = _getcwd(buf, sz) != NULL; + ok = _getcwd(cbuf, sz) != NULL; #elif defined(__WXMAC__) && !defined(__DARWIN__) FSSpec cwdSpec ; FCBPBRec pb; @@ -1418,8 +1470,8 @@ wxChar *wxGetWorkingDirectory(wxChar *buf, int sz) cwdSpec.name[0] = 0 ; wxString res = wxMacFSSpec2MacFilename( &cwdSpec ) ; - strcpy( buf , res ) ; - buf[res.length()]=0 ; + strcpy( cbuf , res ) ; + cbuf[res.length()]=0 ; ok = TRUE; } @@ -1430,14 +1482,19 @@ wxChar *wxGetWorkingDirectory(wxChar *buf, int sz) #elif defined(__VISAGECPP__) || (defined (__OS2__) && defined (__WATCOMC__)) APIRET rc; rc = ::DosQueryCurrentDir( 0 // current drive - ,buf + ,cbuf ,(PULONG)&sz ); ok = rc != 0; #else // !Win32/VC++ !Mac !OS2 - ok = getcwd(buf, sz) != NULL; + ok = getcwd(cbuf, sz) != NULL; #endif // platform -#endif // !wxUSE_UNICODE || !HAVE_WGETCWD + + #if wxUSE_UNICODE + // finally convert the result to Unicode if needed + wxConvFile.MB2WC(buf, cbuf, sz); + #endif // wxUSE_UNICODE + } if ( !ok ) { @@ -1461,32 +1518,29 @@ wxChar *wxGetWorkingDirectory(wxChar *buf, int sz) } #endif // __DJGPP__ -#ifdef __GNUWIN32__ +// MBN: we hope that in the case the user is compiling a GTK+/Motif app, +// he needs Unix as opposed to Win32 pathnames +#if defined( __CYGWIN__ ) && defined( __WINDOWS__ ) // another example of DOS/Unix mix (Cygwin) wxString pathUnix = buf; cygwin_conv_to_full_win32_path(pathUnix, buf); -#endif // __GNUWIN32__ - - // finally convert the result to Unicode if needed -#if wxUSE_UNICODE && !defined(HAVE_WGETCWD) - wxConvFile.MB2WC(buf, cbuf, sz); -#endif // wxUSE_UNICODE +#endif // __CYGWIN__ } return buf; + +#if !wxUSE_UNICODE + #undef cbuf +#endif } wxString wxGetCwd() { - wxString str; - - // we can't create wxStringBuffer object inline: Sun CC generates buggy - // code in this case! - { - wxStringBuffer buf(str, _MAXPATHLEN); - wxGetWorkingDirectory(buf, _MAXPATHLEN); - } - + wxChar *buffer = new wxChar[_MAXPATHLEN]; + wxGetWorkingDirectory(buffer, _MAXPATHLEN); + wxString str( buffer ); + delete [] buffer; + return str; } @@ -1603,8 +1657,8 @@ void WXDLLEXPORT wxSplitPath(const wxChar *pszFileName, time_t WXDLLEXPORT wxFileModificationTime(const wxString& filename) { wxStructStat buf; - - wxStat(filename.fn_str(), &buf); + wxStat( filename, &buf); + return buf.st_mtime; } @@ -1631,7 +1685,7 @@ bool wxIsWild( const wxString& pattern ) bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) -#if defined(HAVE_FNMATCH_H) +#ifdef HAVE_FNMATCH { // this probably won't work well for multibyte chars in Unicode mode? if(dot_special) @@ -1639,7 +1693,7 @@ bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) else return fnmatch(pat.fn_str(), text.fn_str(), 0) == 0; } -#else +#else // !HAVE_FNMATCH // #pragma error Broken implementation of wxMatchWild() -- needs fixing! @@ -1780,33 +1834,8 @@ bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) return ((*str == wxT('\0')) && (*pattern == wxT('\0'))); }; -#endif +#endif // HAVE_FNMATCH/!HAVE_FNMATCH #ifdef __VISUALC__ #pragma warning(default:4706) // assignment within conditional expression #endif // VC++ - -//------------------------------------------------------------------------ -// Missing functions in Unicode for Win9x -//------------------------------------------------------------------------ - -// NB: MSLU only covers Win32 API, it doesn't provide Unicode implementation of -// libc functions. Unfortunately, some of MSVCRT wchar_t functions -// (e.g. _wopen) don't work on Windows 9x, so we have to workaround it -// by calling the char version. We still want to use wchar_t version on -// NT/2000/XP, though, because they allow for Unicode file names. -#if wxUSE_UNICODE_MSLU - - #if defined( __VISUALC__ ) \ - || ( defined(__MINGW32__) && wxCHECK_W32API_VERSION( 0, 5 ) ) \ - || ( defined(__MWERKS__) && defined(__WXMSW__) ) - WXDLLEXPORT int wxOpen(const wxChar *name, int flags, int mode) - { - if ( wxGetOsVersion() == wxWINDOWS_NT ) - return _wopen(name, flags, mode); - else - return _open(wxConvFile.cWX2MB(name), flags, mode); - } - #endif - -#endif // wxUSE_UNICODE_MSLU