X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/47fa796902df5cfaeda065e31f2eb399cd4bca60..06cfab17389f4bbd38560dd86ee39931e979bb1e:/src/common/filefn.cpp diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index c146f4ec4c..7716488573 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -26,6 +26,12 @@ #endif #include "wx/utils.h" +#include + +// there are just too many of those... +#ifdef _MSC_VER + #pragma warning(disable:4706) // assignment within conditional expression +#endif // VC++ #include #include @@ -37,16 +43,21 @@ #endif #endif #include +#ifndef __MWERKS__ #include #include +#else +#include +#include +#endif #ifdef __UNIX__ #include #include #endif -#ifdef __WXMSW__ -#ifndef __GNUWIN32__ +#ifdef __WINDOWS__ +#if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) #include #include #endif @@ -54,12 +65,6 @@ #ifdef __GNUWIN32__ #include -// #include - -#ifndef __MINGW32__ -#include -#endif - #define stricmp strcasecmp #endif @@ -69,17 +74,30 @@ #include #endif -#ifdef __WXMSW__ +#include "wx/setup.h" +#include "wx/log.h" + +// No, Cygwin doesn't appear to have fnmatch.h after all. +#if defined(HAVE_FNMATCH_H) +#include "fnmatch.h" +#endif + +#ifdef __WINDOWS__ #include "windows.h" #endif #define _MAXPATHLEN 500 -#if !USE_SHARED_LIBRARY -IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList) +extern char *wxBuffer; +#ifdef __WXMAC__ +extern char gwxMacFileName[] ; +extern char gwxMacFileName2[] ; +extern char gwxMacFileName3[] ; #endif -extern char *wxBuffer; +#if !USE_SHARED_LIBRARIES +IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList) +#endif void wxPathList::Add (const wxString& path) { @@ -90,7 +108,7 @@ void wxPathList::Add (const wxString& path) void wxPathList::AddEnvList (const wxString& envVariable) { static const char PATH_TOKS[] = -#ifdef __WXMSW__ +#ifdef __WINDOWS__ " ;"; // Don't seperate with colon in DOS (used for drive) #else " :;"; @@ -107,7 +125,7 @@ void wxPathList::AddEnvList (const wxString& envVariable) Add (copystring (token)); while (token) { - if ((token = strtok (NULL, PATH_TOKS)) != NULL) + if ((token = strtok ((char *) NULL, PATH_TOKS)) != NULL) Add (wxString(token)); } } @@ -135,7 +153,7 @@ bool wxPathList::Member (const wxString& path) { wxString path2((char *) node->Data ()); if ( -#if defined(__WXMSW__) || defined(__VMS__) +#if defined(__WINDOWS__) || defined(__VMS__) || defined (__WXMAC__) // Case INDEPENDENT path.CompareTo (path2, wxString::ignoreCase) == 0 #else @@ -166,7 +184,7 @@ wxString wxPathList::FindValidPath (const wxString& file) if (ch != '\\' && ch != '/') strcat (wxBuffer, "/"); strcat (wxBuffer, filename); -#ifdef __WXMSW__ +#ifdef __WINDOWS__ Unix2DosFilename (wxBuffer); #endif if (wxFileExists (wxBuffer)) @@ -193,7 +211,7 @@ wxString wxPathList::FindAbsoluteValidPath (const wxString& file) lastCh = buf[len-1]; if (lastCh != '/' && lastCh != '\\') { -#ifdef __WXMSW__ +#ifdef __WINDOWS__ strcat(buf, "\\"); #else strcat(buf, "/"); @@ -208,11 +226,25 @@ wxString wxPathList::FindAbsoluteValidPath (const wxString& file) bool wxFileExists (const wxString& filename) { +#ifdef __GNUWIN32__ // (fix a B20 bug) + if (GetFileAttributes(filename) == 0xFFFFFFFF) + return FALSE; + else + return TRUE; +#elif defined(__WXMAC__) + struct stat stbuf; + strcpy( gwxMacFileName , filename ) ; + wxUnix2MacFilename( gwxMacFileName ) ; + if (gwxMacFileName && stat ((char *)(const char *)gwxMacFileName, &stbuf) == 0) + return TRUE; + return FALSE ; +#else struct stat stbuf; if (filename && stat ((char *)(const char *)filename, &stbuf) == 0) return TRUE; return FALSE; +#endif } /* Vadim's alternative implementation @@ -236,7 +268,7 @@ wxIsAbsolutePath (const wxString& filename) #ifdef __VMS__ || (filename[0] == '[' && filename[1] != '.') #endif -#ifdef __WXMSW__ +#ifdef __WINDOWS__ /* MSDOS */ || filename[0] == '\\' || (isalpha (filename[0]) && filename[1] == ':') #endif @@ -338,7 +370,7 @@ char *wxRealPath (char *path) char *wxCopyAbsolutePath(const wxString& filename) { if (filename == "") - return NULL; + return (char *) NULL; if (! IsAbsolutePath(wxExpandPath(wxBuffer, filename))) { char buf[_MAXPATHLEN]; @@ -441,7 +473,7 @@ char *wxExpandPath(char *buf, const char *name) #endif { register char *start = d; - register braces = (*s == '{' || *s == '('); + register int braces = (*s == '{' || *s == '('); register char *value; while ((*d++ = *s)) if (braces ? (*s == '}' || *s == ')') : !(isalnum(*s) || *s == '_') ) @@ -522,7 +554,7 @@ wxContractPath (const wxString& filename, const wxString& envname, const wxStrin static char dest[_MAXPATHLEN]; if (filename == "") - return NULL; + return (char *) NULL; strcpy (dest, WXSTRINGCAST filename); #ifdef __WXMSW__ @@ -530,9 +562,9 @@ wxContractPath (const wxString& filename, const wxString& envname, const wxStrin #endif // Handle environment - char *val = NULL; - char *tcp = NULL; - if (envname != NULL && (val = getenv (WXSTRINGCAST envname)) != NULL && + char *val = (char *) NULL; + char *tcp = (char *) NULL; + if (envname != WXSTRINGCAST NULL && (val = getenv (WXSTRINGCAST envname)) != NULL && (tcp = strstr (dest, val)) != NULL) { strcpy (wxBuffer, tcp + strlen (val)); @@ -664,7 +696,7 @@ wxPathOnly (char *path) #endif } - return NULL; + return (char *) NULL; } // Return just the directory, or NULL if no directory @@ -719,6 +751,58 @@ wxString wxPathOnly (const wxString& path) // and back again - or we get nasty problems with delimiters. // Also, convert to lower case, since case is significant in UNIX. +#ifdef __WXMAC__ +void +wxMac2UnixFilename (char *s) +{ + if (s) + { + memmove( s+1 , s ,strlen( s ) + 1) ; + if ( *s == ':' ) + *s = '.' ; + else + *s = '/' ; + + while (*s) + { + if (*s == ':') + *s = '/'; + else + *s = wxToLower (*s); // Case INDEPENDENT + s++; + } + } +} + +void +wxUnix2MacFilename (char *s) +{ + if (s) + { + if ( *s == '.' ) + { + // relative path , since it goes on with slash which is translated to a : + memmove( s , s+1 ,strlen( s ) ) ; + } + else if ( *s == '/' ) + { + // absolute path -> on mac just start with the drive name + memmove( s , s+1 ,strlen( s ) ) ; + } + else + { + wxASSERT_MSG( 1 , "unkown path beginning" ) ; + } + while (*s) + { + if (*s == '/' || *s == '\\') + *s = ':'; + + s++ ; + } + } +} +#endif void wxDos2UnixFilename (char *s) { @@ -736,7 +820,11 @@ wxDos2UnixFilename (char *s) } void +#ifdef __WXMSW__ wxUnix2DosFilename (char *s) +#else +wxUnix2DosFilename (char *WXUNUSED(s)) +#endif { // Yes, I really mean this to happen under DOS only! JACS #ifdef __WXMSW__ @@ -756,13 +844,26 @@ wxConcatFiles (const wxString& file1, const wxString& file2, const wxString& fil { char *outfile = wxGetTempFileName("cat"); - FILE *fp1 = NULL; - FILE *fp2 = NULL; - FILE *fp3 = NULL; + FILE *fp1 = (FILE *) NULL; + FILE *fp2 = (FILE *) NULL; + FILE *fp3 = (FILE *) NULL; // Open the inputs and outputs +#ifdef __WXMAC__ + strcpy( gwxMacFileName , file1 ) ; + wxUnix2MacFilename( gwxMacFileName ) ; + strcpy( gwxMacFileName2 , file2) ; + wxUnix2MacFilename( gwxMacFileName2 ) ; + strcpy( gwxMacFileName3 , outfile) ; + wxUnix2MacFilename( gwxMacFileName3 ) ; + + if ((fp1 = fopen (gwxMacFileName, "rb")) == NULL || + (fp2 = fopen (gwxMacFileName2, "rb")) == NULL || + (fp3 = fopen (gwxMacFileName3, "wb")) == NULL) +#else if ((fp1 = fopen (WXSTRINGCAST file1, "rb")) == NULL || (fp2 = fopen (WXSTRINGCAST file2, "rb")) == NULL || (fp3 = fopen (outfile, "wb")) == NULL) +#endif { if (fp1) fclose (fp1); @@ -796,9 +897,20 @@ wxCopyFile (const wxString& file1, const wxString& file2) FILE *fd2; int ch; +#ifdef __WXMAC__ + strcpy( gwxMacFileName , file1 ) ; + wxUnix2MacFilename( gwxMacFileName ) ; + strcpy( gwxMacFileName2 , file2) ; + wxUnix2MacFilename( gwxMacFileName2 ) ; + + if ((fd1 = fopen (gwxMacFileName, "rb")) == NULL) + return FALSE; + if ((fd2 = fopen (gwxMacFileName2, "wb")) == NULL) +#else if ((fd1 = fopen (WXSTRINGCAST file1, "rb")) == NULL) return FALSE; if ((fd2 = fopen (WXSTRINGCAST file2, "wb")) == NULL) +#endif { fclose (fd1); return FALSE; @@ -815,9 +927,19 @@ wxCopyFile (const wxString& file1, const wxString& file2) bool wxRenameFile (const wxString& file1, const wxString& file2) { +#ifdef __WXMAC__ + strcpy( gwxMacFileName , file1 ) ; + wxUnix2MacFilename( gwxMacFileName ) ; + strcpy( gwxMacFileName2 , file2) ; + wxUnix2MacFilename( gwxMacFileName2 ) ; + + if (0 == rename (gwxMacFileName, gwxMacFileName2)) + return TRUE; +#else // Normal system call if (0 == rename (WXSTRINGCAST file1, WXSTRINGCAST file2)) return TRUE; +#endif // Try to copy if (wxCopyFile(file1, file2)) { wxRemoveFile(file1); @@ -829,8 +951,12 @@ wxRenameFile (const wxString& file1, const wxString& file2) bool wxRemoveFile(const wxString& file) { -#if defined(_MSC_VER) || defined(__BORLANDC__) +#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__) int flag = remove(WXSTRINGCAST file); +#elif defined( __WXMAC__ ) + strcpy( gwxMacFileName , file ) ; + wxUnix2MacFilename( gwxMacFileName ) ; + int flag = unlink(gwxMacFileName); #else int flag = unlink(WXSTRINGCAST file); #endif @@ -839,8 +965,14 @@ bool wxRemoveFile(const wxString& file) bool wxMkdir(const wxString& dir) { -#ifdef __VMS__ +#if defined(__WXSTUBS__) + return FALSE; +#elif defined(__VMS__) return FALSE; +#elif defined( __WXMAC__ ) + strcpy( gwxMacFileName , dir ) ; + wxUnix2MacFilename( gwxMacFileName ) ; + return (mkdir(gwxMacFileName , 0 ) == 0); #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__) return (mkdir (WXSTRINGCAST dir, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0); #else @@ -852,6 +984,10 @@ bool wxRmdir(const wxString& dir, int WXUNUSED(flags)) { #ifdef __VMS__ return FALSE; +#elif defined( __WXMAC__ ) + strcpy( gwxMacFileName , dir ) ; + wxUnix2MacFilename( gwxMacFileName ) ; + return (rmdir(WXSTRINGCAST gwxMacFileName) == 0); #else return (rmdir(WXSTRINGCAST dir) == 0); #endif @@ -919,7 +1055,7 @@ bool wxPathExists(const char *pszPathName) // Get a temporary filename, opening and closing the file. char *wxGetTempFileName(const wxString& prefix, char *buf) { -#ifdef __WXMSW__ +#ifdef __WINDOWS__ #ifndef __WIN32__ char tmp[144]; @@ -937,7 +1073,7 @@ char *wxGetTempFileName(const wxString& prefix, char *buf) #else static short last_temp = 0; // cache last to speed things a bit // At most 1000 temp files to a process! We use a ring count. - char tmp[100]; + char tmp[100]; // FIXME static buffer for (short suffix = last_temp + 1; suffix != last_temp; ++suffix %= 1000) { @@ -956,9 +1092,9 @@ char *wxGetTempFileName(const wxString& prefix, char *buf) return buf; } } - cerr << "wxWindows: error finding temporary file name.\n"; + wxLogError( _("wxWindows: error finding temporary file name.\n") ); if (buf) buf[0] = 0; - return NULL; + return (char *) NULL; #endif } @@ -970,8 +1106,8 @@ char *wxGetTempFileName(const wxString& prefix, char *buf) // Flags are reserved for future use. #ifndef __VMS__ -static DIR *wxDirStream = NULL; -static char *wxFileSpec = NULL; +static DIR *wxDirStream = (DIR *) NULL; +static char *wxFileSpec = (char *) NULL; static int wxFindFileFlags = 0; #endif @@ -999,23 +1135,23 @@ char *wxFindFirstFile(const char *spec, int flags) p = "."; if ((wxDirStream=opendir(p))==NULL) - return NULL; + return (char *) NULL; /* MATTHEW: [5] wxFindNextFile can do the rest of the work */ return wxFindNextFile(); #endif // ifndef __VMS__ - return NULL; + return (char *) NULL; } char *wxFindNextFile(void) { #ifndef __VMS__ - static char buf[400]; + static char buf[400]; // FIXME static buffer /* MATTHEW: [2] Don't crash if we read too many times */ if (!wxDirStream) - return NULL; + return (char *) NULL; // Find path only so we can concatenate // found file onto path @@ -1059,11 +1195,11 @@ char *wxFindNextFile(void) } } closedir(wxDirStream); - wxDirStream = NULL; + wxDirStream = (DIR *) NULL; #endif // ifndef __VMS__ - return NULL; + return (char *) NULL; } #elif defined(__WXMSW__) @@ -1240,7 +1376,7 @@ char *wxGetWorkingDirectory(char *buf, int sz) { if (!buf) buf = new char[sz+1]; -#ifdef _MSC_VER +#ifdef _MSC_VER if (_getcwd(buf, sz) == NULL) { #else if (getcwd(buf, sz) == NULL) { @@ -1253,9 +1389,9 @@ char *wxGetWorkingDirectory(char *buf, int sz) bool wxSetWorkingDirectory(const wxString& d) { -#ifdef __UNIX__ +#if defined( __UNIX__ ) || defined( __WXMAC__ ) return (chdir(d) == 0); -#elif defined(__WXMSW__) +#elif defined(__WINDOWS__) #ifdef __WIN32__ return (bool)(SetCurrentDirectory(d) != 0); @@ -1300,7 +1436,7 @@ bool wxFindFileInPath(wxString *pStr, const char *pszPath, const char *pszFile) { // we assume that it's not empty wxCHECK_MSG( !IsEmpty(pszFile), FALSE, - "empty file name in wxFindFileInPath"); + _("empty file name in wxFindFileInPath")); // skip path separator in the beginning of the file name if present if ( wxIsPathSeparator(*pszFile) ) @@ -1312,7 +1448,7 @@ bool wxFindFileInPath(wxString *pStr, const char *pszPath, const char *pszFile) wxString strFile; char *pc; - for ( pc = strtok(szPath, PATH_SEP); pc; pc = strtok(NULL, PATH_SEP) ) { + for ( pc = strtok(szPath, PATH_SEP); pc; pc = strtok((char *) NULL, PATH_SEP) ) { // search for the file in this directory strFile = pc; if ( !wxEndsWithPathSeparator(pc) ) @@ -1335,23 +1471,23 @@ void WXDLLEXPORT wxSplitPath(const char *pszFileName, wxString *pstrName, wxString *pstrExt) { - wxCHECK_RET( pszFileName, "NULL file name in wxSplitPath" ); + wxCHECK_RET( pszFileName, _("NULL file name in wxSplitPath") ); const char *pDot = strrchr(pszFileName, FILE_SEP_EXT); const char *pSepUnix = strrchr(pszFileName, FILE_SEP_PATH_UNIX); const char *pSepDos = strrchr(pszFileName, FILE_SEP_PATH_DOS); // take the last of the two - uint nPosUnix = pSepUnix ? pSepUnix - pszFileName : 0; - uint nPosDos = pSepDos ? pSepDos - pszFileName : 0; + size_t nPosUnix = pSepUnix ? pSepUnix - pszFileName : 0; + size_t nPosDos = pSepDos ? pSepDos - pszFileName : 0; if ( nPosDos > nPosUnix ) nPosUnix = nPosDos; - uint nLen = Strlen(pszFileName); +// size_t nLen = Strlen(pszFileName); if ( pstrPath ) *pstrPath = wxString(pszFileName, nPosUnix); if ( pDot ) { - uint nPosDot = pDot - pszFileName; + size_t nPosDot = pDot - pszFileName; if ( pstrName ) *pstrName = wxString(pszFileName + nPosUnix + 1, nPosDot - nPosUnix); if ( pstrExt ) @@ -1364,3 +1500,179 @@ void WXDLLEXPORT wxSplitPath(const char *pszFileName, pstrExt->Empty(); } } + +//------------------------------------------------------------------------ +// wild character routines +//------------------------------------------------------------------------ + +bool wxIsWild( const wxString& pattern ) +{ + wxString tmp = pattern; + char *pat = WXSTRINGCAST(tmp); + while (*pat) { + switch (*pat++) { + case '?': case '*': case '[': case '{': + return TRUE; + case '\\': + if (!*pat++) + return FALSE; + } + } + return FALSE; +}; + +bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) + +#if defined(HAVE_FNMATCH_H) +{ + if(dot_special) + return fnmatch(pat.c_str(), text.c_str(), FNM_PERIOD) == 0; + else + return fnmatch(pat.c_str(), text.c_str(), 0) == 0; +} +#else + +// #pragma error Broken implementation of wxMatchWild() -- needs fixing! + + /* + * WARNING: this code is broken! + */ +{ + wxString tmp1 = pat; + char *pattern = WXSTRINGCAST(tmp1); + wxString tmp2 = text; + char *str = WXSTRINGCAST(tmp2); + char c; + char *cp; + bool done = FALSE, ret_code, ok; + // Below is for vi fans + const char OB = '{', CB = '}'; + + // dot_special means '.' only matches '.' + if (dot_special && *str == '.' && *pattern != *str) + return FALSE; + + while ((*pattern != '\0') && (!done) + && (((*str=='\0')&&((*pattern==OB)||(*pattern=='*')))||(*str!='\0'))) { + switch (*pattern) { + case '\\': + pattern++; + if (*pattern != '\0') + pattern++; + break; + case '*': + pattern++; + ret_code = FALSE; + while ((*str!='\0') + && (!(ret_code=wxMatchWild(pattern, str++, FALSE)))) + /*loop*/; + if (ret_code) { + while (*str != '\0') + str++; + while (*pattern != '\0') + pattern++; + } + break; + case '[': + pattern++; + repeat: + if ((*pattern == '\0') || (*pattern == ']')) { + done = TRUE; + break; + } + if (*pattern == '\\') { + pattern++; + if (*pattern == '\0') { + done = TRUE; + break; + } + } + if (*(pattern + 1) == '-') { + c = *pattern; + pattern += 2; + if (*pattern == ']') { + done = TRUE; + break; + } + if (*pattern == '\\') { + pattern++; + if (*pattern == '\0') { + done = TRUE; + break; + } + } + if ((*str < c) || (*str > *pattern)) { + pattern++; + goto repeat; + } + } else if (*pattern != *str) { + pattern++; + goto repeat; + } + pattern++; + while ((*pattern != ']') && (*pattern != '\0')) { + if ((*pattern == '\\') && (*(pattern + 1) != '\0')) + pattern++; + pattern++; + } + if (*pattern != '\0') { + pattern++, str++; + } + break; + case '?': + pattern++; + str++; + break; + case OB: + pattern++; + while ((*pattern != CB) && (*pattern != '\0')) { + cp = str; + ok = TRUE; + while (ok && (*cp != '\0') && (*pattern != '\0') + && (*pattern != ',') && (*pattern != CB)) { + if (*pattern == '\\') + pattern++; + ok = (*pattern++ == *cp++); + } + if (*pattern == '\0') { + ok = FALSE; + done = TRUE; + break; + } else if (ok) { + str = cp; + while ((*pattern != CB) && (*pattern != '\0')) { + if (*++pattern == '\\') { + if (*++pattern == CB) + pattern++; + } + } + } else { + while (*pattern!=CB && *pattern!=',' && *pattern!='\0') { + if (*++pattern == '\\') { + if (*++pattern == CB || *pattern == ',') + pattern++; + } + } + } + if (*pattern != '\0') + pattern++; + } + break; + default: + if (*str == *pattern) { + str++, pattern++; + } else { + done = TRUE; + } + } + } + while (*pattern == '*') + pattern++; + return ((*str == '\0') && (*pattern == '\0')); +}; + +#endif + +#ifdef _MSC_VER + #pragma warning(default:4706) // assignment within conditional expression +#endif // VC++