X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/aad5220b823778a271e25ca74e4127034161e280..02e8b2f9fbdfcd25080c42082d637da257c20094:/src/common/filefn.cpp diff --git a/src/common/filefn.cpp b/src/common/filefn.cpp index a85dae4c69..64391f01e3 100644 --- a/src/common/filefn.cpp +++ b/src/common/filefn.cpp @@ -26,6 +26,7 @@ #endif #include "wx/utils.h" +#include #include #include @@ -69,6 +70,11 @@ #include #endif +#include "wx/setup.h" +#ifdef HAVE_FNMATCH_H +#include "fnmatch.h" +#endif + #ifdef __WINDOWS__ #include "windows.h" #endif @@ -107,7 +113,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)); } } @@ -267,10 +273,25 @@ void wxStripExtension(char *buffer) } } +void wxStripExtension(wxString& buffer) +{ + size_t len = buffer.Length(); + size_t i = len-1; + while (i > 0) + { + if (buffer.GetChar(i) == '.') + { + buffer = buffer.Left(i); + break; + } + i --; + } +} + // Destructive removal of /./ and /../ stuff char *wxRealPath (char *path) { -#ifdef __WINDOWS__ +#ifdef __WXMSW__ static const char SEP = '\\'; Unix2DosFilename(path); #else @@ -300,7 +321,7 @@ char *wxRealPath (char *path) path[0] = SEP; path[1] = '\0'; } -#ifdef __WINDOWS__ +#ifdef __WXMSW__ /* Check that path[2] is NULL! */ else if (path[1] == ':' && !path[2]) { @@ -323,14 +344,14 @@ char *wxRealPath (char *path) char *wxCopyAbsolutePath(const wxString& filename) { if (filename == "") - return NULL; + return (char *) NULL; if (! IsAbsolutePath(wxExpandPath(wxBuffer, filename))) { char buf[_MAXPATHLEN]; buf[0] = '\0'; wxGetWorkingDirectory(buf, sizeof(buf)/sizeof(char)); char ch = buf[strlen(buf) - 1]; -#ifdef __WINDOWS__ +#ifdef __WXMSW__ if (ch != '\\' && ch != '/') strcat(buf, "\\"); #else @@ -381,7 +402,7 @@ char *wxExpandPath(char *buf, const char *name) trimchars[2] = '\t'; trimchars[3] = 0; -#ifdef __WINDOWS__ +#ifdef __WXMSW__ const char SEP = '\\'; #else const char SEP = '/'; @@ -402,7 +423,7 @@ char *wxExpandPath(char *buf, const char *name) s = nm; d = lnm; -#ifdef __WINDOWS__ +#ifdef __WXMSW__ q = FALSE; #else q = nm[0] == '\\' && nm[1] == '~'; @@ -410,7 +431,7 @@ char *wxExpandPath(char *buf, const char *name) /* Expand inline environment variables */ while ((*d++ = *s)) { -#ifndef __WINDOWS__ +#ifndef __WXMSW__ if (*s == '\\') { if ((*(d - 1) = *++s)) { s++; @@ -419,14 +440,14 @@ char *wxExpandPath(char *buf, const char *name) break; } else #endif -#ifdef __WINDOWS__ +#ifdef __WXMSW__ if (*s++ == '$' && (*s == '{' || *s == ')')) #else if (*s++ == '$') #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 == '_') ) @@ -507,17 +528,17 @@ 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 __WINDOWS__ +#ifdef __WXMSW__ Unix2DosFilename(dest); #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)); @@ -537,7 +558,7 @@ wxContractPath (const wxString& filename, const wxString& envname, const wxStrin strcpy(wxBuffer, "~"); if (user && *user) strcat(wxBuffer, user); -#ifdef __WINDOWS__ +#ifdef __WXMSW__ // strcat(wxBuffer, "\\"); #else // strcat(wxBuffer, "/"); @@ -568,7 +589,7 @@ char *wxFileNameFromPath (char *path) #endif return tcp + 1; } /* while */ -#ifdef __WINDOWS__ +#ifdef __WXMSW__ if (isalpha (*path) && *(path + 1) == ':') return path + 2; #endif @@ -595,7 +616,7 @@ wxString wxFileNameFromPath (const wxString& path1) #endif return wxString(tcp + 1); } /* while */ -#ifdef __WINDOWS__ +#ifdef __WXMSW__ if (isalpha (*path) && *(path + 1) == ':') return wxString(path + 2); #endif @@ -637,7 +658,7 @@ wxPathOnly (char *path) else i --; } -#ifdef __WINDOWS__ +#ifdef __WXMSW__ // Try Drive specifier if (isalpha (buf[0]) && buf[1] == ':') { @@ -649,7 +670,7 @@ wxPathOnly (char *path) #endif } - return NULL; + return (char *) NULL; } // Return just the directory, or NULL if no directory @@ -685,7 +706,7 @@ wxString wxPathOnly (const wxString& path) else i --; } -#ifdef __WINDOWS__ +#ifdef __WXMSW__ // Try Drive specifier if (isalpha (buf[0]) && buf[1] == ':') { @@ -712,7 +733,7 @@ wxDos2UnixFilename (char *s) { if (*s == '\\') *s = '/'; -#ifdef __WINDOWS__ +#ifdef __WXMSW__ else *s = wxToLower (*s); // Case INDEPENDENT #endif @@ -721,10 +742,14 @@ 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 __WINDOWS__ +#ifdef __WXMSW__ if (s) while (*s) { @@ -741,9 +766,9 @@ 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 if ((fp1 = fopen (WXSTRINGCAST file1, "rb")) == NULL || (fp2 = fopen (WXSTRINGCAST file2, "rb")) == NULL || @@ -824,9 +849,11 @@ bool wxRemoveFile(const wxString& file) bool wxMkdir(const wxString& dir) { -#ifdef __VMS__ +#if defined(__WXSTUBS__) + return FALSE; +#elif defined(__VMS__) return FALSE; -#elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WINDOWS__) +#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 return (mkdir(WXSTRINGCAST dir) == 0); @@ -847,7 +874,7 @@ bool wxDirExists(const wxString& dir) { #ifdef __VMS__ return FALSE; -#elif !defined(__WINDOWS__) +#elif !defined(__WXMSW__) struct stat sbuf; return (stat(dir, &sbuf) != -1) && S_ISDIR(sbuf.st_mode) ? TRUE : FALSE; #else @@ -941,9 +968,9 @@ char *wxGetTempFileName(const wxString& prefix, char *buf) return buf; } } - cerr << "wxWindows: error finding temporary file name.\n"; + cerr << _("wxWindows: error finding temporary file name.\n"); if (buf) buf[0] = 0; - return NULL; + return (char *) NULL; #endif } @@ -955,8 +982,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 @@ -984,13 +1011,13 @@ 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) @@ -1000,7 +1027,7 @@ char *wxFindNextFile(void) /* 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 @@ -1044,14 +1071,14 @@ char *wxFindNextFile(void) } } closedir(wxDirStream); - wxDirStream = NULL; + wxDirStream = (DIR *) NULL; #endif // ifndef __VMS__ - return NULL; + return (char *) NULL; } -#elif defined(__WINDOWS__) +#elif defined(__WXMSW__) #ifdef __WIN32__ HANDLE wxFileStrucHandle = INVALID_HANDLE_VALUE; @@ -1066,7 +1093,7 @@ static struct _find_t wxFileStruc; static wxString wxFileSpec = ""; static int wxFindFileFlags; -char *wxFindFirstFile(const wxString& spec, int flags) +char *wxFindFirstFile(const char *spec, int flags) { wxFileSpec = spec; wxFindFileFlags = flags; /* MATTHEW: [5] Remember flags */ @@ -1216,7 +1243,7 @@ char *wxFindNextFile(void) } #endif - // __WINDOWS__ + // __WXMSW__ // Get current working directory. // If buf is NULL, allocates space using new, else @@ -1284,7 +1311,8 @@ bool wxEndsWithPathSeparator(const char *pszFileName) bool wxFindFileInPath(wxString *pStr, const char *pszPath, const char *pszFile) { // we assume that it's not empty - wxCHECK_RET( Strlen(pszFile) != 0, FALSE); + wxCHECK_MSG( !IsEmpty(pszFile), FALSE, + _("empty file name in wxFindFileInPath")); // skip path separator in the beginning of the file name if present if ( wxIsPathSeparator(*pszFile) ) @@ -1296,7 +1324,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) ) @@ -1314,3 +1342,206 @@ bool wxFindFileInPath(wxString *pStr, const char *pszPath, const char *pszFile) return pc != NULL; // if true => we breaked from the loop } +void WXDLLEXPORT wxSplitPath(const char *pszFileName, + wxString *pstrPath, + wxString *pstrName, + wxString *pstrExt) +{ + 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 + size_t nPosUnix = pSepUnix ? pSepUnix - pszFileName : 0; + size_t nPosDos = pSepDos ? pSepDos - pszFileName : 0; + if ( nPosDos > nPosUnix ) + nPosUnix = nPosDos; +// size_t nLen = Strlen(pszFileName); + + if ( pstrPath ) + *pstrPath = wxString(pszFileName, nPosUnix); + if ( pDot ) { + size_t nPosDot = pDot - pszFileName; + if ( pstrName ) + *pstrName = wxString(pszFileName + nPosUnix + 1, nPosDot - nPosUnix); + if ( pstrExt ) + *pstrExt = wxString(pszFileName + nPosDot + 1); + } + else { + if ( pstrName ) + *pstrName = wxString(pszFileName + nPosUnix + 1); + if ( pstrExt ) + 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; +}; + + +#ifdef HAVE_FNMATCH_H +{ + // dot_special: what is it supposed to do? + return fnmatch(pat.c_str(), text.c_str(), FNM_PERIOD) == 0; +} +#else + +#pragma error Broken implementation of wxMatchWild() -- needs fixing! + /* + * WARNING: this code is broken! + */ +bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) +{ + 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 +