1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: File- and directory-related functions
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filefn.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
31 // there are just too many of those...
33 #pragma warning(disable:4706) // assignment within conditional expression
40 #if !defined(__WATCOMC__)
41 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
47 #include <sys/types.h>
60 #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ )
67 #include <sys/unistd.h>
68 #define stricmp strcasecmp
71 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
72 // this (3.1 I believe) and how to test for it.
73 // If this works for Borland 4.0 as well, then no worries.
80 // No, Cygwin doesn't appear to have fnmatch.h after all.
81 #if defined(HAVE_FNMATCH_H)
89 #define _MAXPATHLEN 500
91 extern char *wxBuffer
;
93 extern char gwxMacFileName
[] ;
94 extern char gwxMacFileName2
[] ;
95 extern char gwxMacFileName3
[] ;
98 #if !USE_SHARED_LIBRARIES
99 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
102 void wxPathList::Add (const wxString
& path
)
104 wxStringList::Add ((char *)(const char *)path
);
107 // Add paths e.g. from the PATH environment variable
108 void wxPathList::AddEnvList (const wxString
& envVariable
)
110 static const char PATH_TOKS
[] =
112 " ;"; // Don't seperate with colon in DOS (used for drive)
117 char *val
= getenv (WXSTRINGCAST envVariable
);
120 char *s
= copystring (val
);
121 char *token
= strtok (s
, PATH_TOKS
);
125 Add (copystring (token
));
128 if ((token
= strtok ((char *) NULL
, PATH_TOKS
)) != NULL
)
129 Add (wxString(token
));
136 // Given a full filename (with path), ensure that that file can
137 // be accessed again USING FILENAME ONLY by adding the path
138 // to the list if not already there.
139 void wxPathList::EnsureFileAccessible (const wxString
& path
)
141 wxString
path1(path
);
142 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
145 if (!Member (wxString(path_only
)))
146 Add (wxString(path_only
));
150 bool wxPathList::Member (const wxString
& path
)
152 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
154 wxString
path2((char *) node
->Data ());
156 #if defined(__WINDOWS__) || defined(__VMS__) || defined (__WXMAC__)
158 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
160 // Case sensitive File System
161 path
.CompareTo (path2
) == 0
169 wxString
wxPathList::FindValidPath (const wxString
& file
)
171 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
172 return wxString(wxBuffer
);
174 char buf
[_MAXPATHLEN
];
175 strcpy(buf
, wxBuffer
);
177 char *filename
= (char*) NULL
; /* shut up buggy egcs warning */
178 filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
180 for (wxNode
* node
= First (); node
; node
= node
->Next ())
182 char *path
= (char *) node
->Data ();
183 strcpy (wxBuffer
, path
);
184 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
185 if (ch
!= '\\' && ch
!= '/')
186 strcat (wxBuffer
, "/");
187 strcat (wxBuffer
, filename
);
189 Unix2DosFilename (wxBuffer
);
191 if (wxFileExists (wxBuffer
))
193 return wxString(wxBuffer
); // Found!
197 return wxString(""); // Not found
200 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
202 wxString f
= FindValidPath(file
);
203 if (wxIsAbsolutePath(f
))
208 wxGetWorkingDirectory(buf
, 499);
209 int len
= (int)strlen(buf
);
213 if (lastCh
!= '/' && lastCh
!= '\\')
221 strcat(buf
, (const char *)f
);
222 strcpy(wxBuffer
, buf
);
223 return wxString(wxBuffer
);
228 wxFileExists (const wxString
& filename
)
230 #ifdef __GNUWIN32__ // (fix a B20 bug)
231 if (GetFileAttributes(filename
) == 0xFFFFFFFF)
235 #elif defined(__WXMAC__)
237 strcpy( gwxMacFileName
, filename
) ;
238 wxUnix2MacFilename( gwxMacFileName
) ;
239 if (gwxMacFileName
&& stat ((char *)(const char *)gwxMacFileName
, &stbuf
) == 0)
245 if ((filename
!= "") && stat ((char *)(const char *)filename
, &stbuf
) == 0)
251 /* Vadim's alternative implementation
253 // does the file exist?
254 bool wxFileExists(const char *pszFileName)
257 return !access(pszFileName, 0) &&
258 !stat(pszFileName, &st) &&
259 (st.st_mode & S_IFREG);
264 wxIsAbsolutePath (const wxString
& filename
)
268 if (filename
[0] == '/'
270 || (filename
[0] == '[' && filename
[1] != '.')
274 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
283 * Strip off any extension (dot something) from end of file,
284 * IF one exists. Inserts zero into buffer.
288 void wxStripExtension(char *buffer
)
290 int len
= strlen(buffer
);
294 if (buffer
[i
] == '.')
303 void wxStripExtension(wxString
& buffer
)
305 size_t len
= buffer
.Length();
309 if (buffer
.GetChar(i
) == '.')
311 buffer
= buffer
.Left(i
);
318 // Destructive removal of /./ and /../ stuff
319 char *wxRealPath (char *path
)
322 static const char SEP
= '\\';
323 Unix2DosFilename(path
);
325 static const char SEP
= '/';
327 if (path
[0] && path
[1]) {
328 /* MATTHEW: special case "/./x" */
330 if (path
[2] == SEP
&& path
[1] == '.')
338 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
341 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
342 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
343 && (q
- 1 <= path
|| q
[-1] != SEP
))
352 /* Check that path[2] is NULL! */
353 else if (path
[1] == ':' && !path
[2])
362 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
371 char *wxCopyAbsolutePath(const wxString
& filename
)
374 return (char *) NULL
;
376 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
377 char buf
[_MAXPATHLEN
];
379 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
380 char ch
= buf
[strlen(buf
) - 1];
382 if (ch
!= '\\' && ch
!= '/')
388 strcat(buf
, wxBuffer
);
389 return copystring( wxRealPath(buf
) );
391 return copystring( wxBuffer
);
397 ~user/ => user's home dir
398 If the environment variable a = "foo" and b = "bar" then:
415 /* input name in name, pathname output to buf. */
417 char *wxExpandPath(char *buf
, const char *name
)
419 register char *d
, *s
, *nm
;
420 char lnm
[_MAXPATHLEN
];
423 // Some compilers don't like this line.
424 // const char trimchars[] = "\n \t";
433 const char SEP
= '\\';
435 const char SEP
= '/';
438 if (name
== NULL
|| *name
== '\0')
440 nm
= copystring(name
); // Make a scratch copy
443 /* Skip leading whitespace and cr */
444 while (strchr((char *)trimchars
, *nm
) != NULL
)
446 /* And strip off trailing whitespace and cr */
447 s
= nm
+ (q
= strlen(nm
)) - 1;
448 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
456 q
= nm
[0] == '\\' && nm
[1] == '~';
459 /* Expand inline environment variables */
460 while ((*d
++ = *s
)) {
463 if ((*(d
- 1) = *++s
)) {
471 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
476 register char *start
= d
;
477 register int braces
= (*s
== '{' || *s
== '(');
478 register char *value
;
480 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
485 value
= getenv(braces
? start
+ 1 : start
);
487 for ((d
= start
- 1); (*d
++ = *value
++););
495 /* Expand ~ and ~user */
498 if (nm
[0] == '~' && !q
)
501 if (nm
[1] == SEP
|| nm
[1] == 0)
503 if ((s
= wxGetUserHome("")) != NULL
) {
508 { /* ~user/filename */
511 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
512 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
513 was_sep
= (*s
== SEP
);
514 nnm
= *s
? s
+ 1 : s
;
516 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
517 if (was_sep
) /* replace only if it was there: */
528 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
530 while ('\0' != (*d
++ = *s
++))
533 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
537 while ((*d
++ = *s
++));
539 delete[] nm_tmp
; // clean up alloc
540 /* Now clean up the buffer */
541 return wxRealPath(buf
);
545 /* Contract Paths to be build upon an environment variable
548 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
550 The call wxExpandPath can convert these back!
553 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
555 static char dest
[_MAXPATHLEN
];
558 return (char *) NULL
;
560 strcpy (dest
, WXSTRINGCAST filename
);
562 Unix2DosFilename(dest
);
565 // Handle environment
566 char *val
= (char *) NULL
;
567 char *tcp
= (char *) NULL
;
568 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
569 (tcp
= strstr (dest
, val
)) != NULL
)
571 strcpy (wxBuffer
, tcp
+ strlen (val
));
574 strcpy (tcp
, WXSTRINGCAST envname
);
576 strcat (tcp
, wxBuffer
);
579 // Handle User's home (ignore root homes!)
581 if ((val
= wxGetUserHome (user
)) != NULL
&&
582 (len
= strlen(val
)) > 2 &&
583 strncmp(dest
, val
, len
) == 0)
585 strcpy(wxBuffer
, "~");
587 strcat(wxBuffer
, (const char*) user
);
589 // strcat(wxBuffer, "\\");
591 // strcat(wxBuffer, "/");
593 strcat(wxBuffer
, dest
+ len
);
594 strcpy (dest
, wxBuffer
);
600 // Return just the filename, not the path
602 char *wxFileNameFromPath (char *path
)
608 tcp
= path
+ strlen (path
);
609 while (--tcp
>= path
)
611 if (*tcp
== '/' || *tcp
== '\\'
613 || *tcp
== ':' || *tcp
== ']')
620 if (isalpha (*path
) && *(path
+ 1) == ':')
627 wxString
wxFileNameFromPath (const wxString
& path1
)
632 char *path
= WXSTRINGCAST path1
;
635 tcp
= path
+ strlen (path
);
636 while (--tcp
>= path
)
638 if (*tcp
== '/' || *tcp
== '\\'
640 || *tcp
== ':' || *tcp
== ']')
644 return wxString(tcp
+ 1);
647 if (isalpha (*path
) && *(path
+ 1) == ':')
648 return wxString(path
+ 2);
651 // Yes, this should return the path, not an empty string, otherwise
652 // we get "thing.txt" -> "".
656 // Return just the directory, or NULL if no directory
658 wxPathOnly (char *path
)
662 static char buf
[_MAXPATHLEN
];
667 int l
= strlen(path
);
672 // Search backward for a backward or forward slash
673 while (!done
&& i
> -1)
676 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
691 // Try Drive specifier
692 if (isalpha (buf
[0]) && buf
[1] == ':')
694 // A:junk --> A:. (since A:.\junk Not A:\junk)
702 return (char *) NULL
;
705 // Return just the directory, or NULL if no directory
706 wxString
wxPathOnly (const wxString
& path
)
710 char buf
[_MAXPATHLEN
];
713 strcpy (buf
, WXSTRINGCAST path
);
715 int l
= path
.Length();
720 // Search backward for a backward or forward slash
721 while (!done
&& i
> -1)
724 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
733 return wxString(buf
);
739 // Try Drive specifier
740 if (isalpha (buf
[0]) && buf
[1] == ':')
742 // A:junk --> A:. (since A:.\junk Not A:\junk)
745 return wxString(buf
);
753 // Utility for converting delimiters in DOS filenames to UNIX style
754 // and back again - or we get nasty problems with delimiters.
755 // Also, convert to lower case, since case is significant in UNIX.
759 wxMac2UnixFilename (char *s
)
763 memmove( s
+1 , s
,strlen( s
) + 1) ;
774 *s
= wxToLower (*s
); // Case INDEPENDENT
781 wxUnix2MacFilename (char *s
)
787 // relative path , since it goes on with slash which is translated to a :
788 memmove( s
, s
+1 ,strlen( s
) ) ;
790 else if ( *s
== '/' )
792 // absolute path -> on mac just start with the drive name
793 memmove( s
, s
+1 ,strlen( s
) ) ;
797 wxASSERT_MSG( 1 , "unkown path beginning" ) ;
801 if (*s
== '/' || *s
== '\\')
810 wxDos2UnixFilename (char *s
)
819 *s
= wxToLower (*s
); // Case INDEPENDENT
827 wxUnix2DosFilename (char *s
)
829 wxUnix2DosFilename (char *WXUNUSED(s
))
832 // Yes, I really mean this to happen under DOS only! JACS
844 // Concatenate two files to form third
846 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
848 char *outfile
= wxGetTempFileName("cat");
850 FILE *fp1
= (FILE *) NULL
;
851 FILE *fp2
= (FILE *) NULL
;
852 FILE *fp3
= (FILE *) NULL
;
853 // Open the inputs and outputs
855 strcpy( gwxMacFileName
, file1
) ;
856 wxUnix2MacFilename( gwxMacFileName
) ;
857 strcpy( gwxMacFileName2
, file2
) ;
858 wxUnix2MacFilename( gwxMacFileName2
) ;
859 strcpy( gwxMacFileName3
, outfile
) ;
860 wxUnix2MacFilename( gwxMacFileName3
) ;
862 if ((fp1
= fopen (gwxMacFileName
, "rb")) == NULL
||
863 (fp2
= fopen (gwxMacFileName2
, "rb")) == NULL
||
864 (fp3
= fopen (gwxMacFileName3
, "wb")) == NULL
)
866 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
867 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
868 (fp3
= fopen (outfile
, "wb")) == NULL
)
881 while ((ch
= getc (fp1
)) != EOF
)
882 (void) putc (ch
, fp3
);
885 while ((ch
= getc (fp2
)) != EOF
)
886 (void) putc (ch
, fp3
);
890 bool result
= wxRenameFile(outfile
, file3
);
897 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
904 strcpy( gwxMacFileName
, file1
) ;
905 wxUnix2MacFilename( gwxMacFileName
) ;
906 strcpy( gwxMacFileName2
, file2
) ;
907 wxUnix2MacFilename( gwxMacFileName2
) ;
909 if ((fd1
= fopen (gwxMacFileName
, "rb")) == NULL
)
911 if ((fd2
= fopen (gwxMacFileName2
, "wb")) == NULL
)
913 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
915 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
922 while ((ch
= getc (fd1
)) != EOF
)
923 (void) putc (ch
, fd2
);
931 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
934 strcpy( gwxMacFileName
, file1
) ;
935 wxUnix2MacFilename( gwxMacFileName
) ;
936 strcpy( gwxMacFileName2
, file2
) ;
937 wxUnix2MacFilename( gwxMacFileName2
) ;
939 if (0 == rename (gwxMacFileName
, gwxMacFileName2
))
942 // Normal system call
943 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
947 if (wxCopyFile(file1
, file2
)) {
955 bool wxRemoveFile(const wxString
& file
)
957 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
958 int flag
= remove(WXSTRINGCAST file
);
959 #elif defined( __WXMAC__ )
960 strcpy( gwxMacFileName
, file
) ;
961 wxUnix2MacFilename( gwxMacFileName
) ;
962 int flag
= unlink(gwxMacFileName
);
964 int flag
= unlink(WXSTRINGCAST file
);
969 bool wxMkdir(const wxString
& dir
)
971 #if defined(__WXSTUBS__)
973 #elif defined(__VMS__)
975 #elif defined( __WXMAC__ )
976 strcpy( gwxMacFileName
, dir
) ;
977 wxUnix2MacFilename( gwxMacFileName
) ;
978 return (mkdir(gwxMacFileName
, 0 ) == 0);
979 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
980 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
982 return (mkdir(WXSTRINGCAST dir
) == 0);
986 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
990 #elif defined( __WXMAC__ )
991 strcpy( gwxMacFileName
, dir
) ;
992 wxUnix2MacFilename( gwxMacFileName
) ;
993 return (rmdir(WXSTRINGCAST gwxMacFileName
) == 0);
995 return (rmdir(WXSTRINGCAST dir
) == 0);
1000 bool wxDirExists(const wxString
& dir
)
1004 #elif !defined(__WXMSW__)
1006 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
1009 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
1010 #if defined(__WIN32__)
1011 WIN32_FIND_DATA fileInfo
;
1014 struct ffblk fileInfo
;
1016 struct find_t fileInfo
;
1020 #if defined(__WIN32__)
1021 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
1023 if (h
==INVALID_HANDLE_VALUE
)
1027 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
1030 // In Borland findfirst has a different argument
1031 // ordering from _dos_findfirst. But _dos_findfirst
1032 // _should_ be ok in both MS and Borland... why not?
1034 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
1036 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
1045 // does the path exists? (may have or not '/' or '\\' at the end)
1046 bool wxPathExists(const char *pszPathName
)
1048 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
1049 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
1050 wxString
strPath(pszPathName
);
1051 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
1052 strPath
.Last() = '\0';
1055 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
1058 // Get a temporary filename, opening and closing the file.
1059 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
1065 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
1068 char tmpPath
[MAX_PATH
];
1069 ::GetTempPath(MAX_PATH
, tmpPath
);
1070 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
1072 if (buf
) strcpy(buf
, tmp
);
1073 else buf
= copystring(tmp
);
1077 static short last_temp
= 0; // cache last to speed things a bit
1078 // At most 1000 temp files to a process! We use a ring count.
1079 char tmp
[100]; // FIXME static buffer
1081 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
1083 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
1084 if (!wxFileExists( tmp
))
1086 // Touch the file to create it (reserve name)
1087 FILE *fd
= fopen (tmp
, "w");
1094 buf
= copystring( tmp
);
1098 wxLogError( _("wxWindows: error finding temporary file name.\n") );
1099 if (buf
) buf
[0] = 0;
1100 return (char *) NULL
;
1104 // Get first file name matching given wild card.
1108 // Get first file name matching given wild card.
1109 // Flags are reserved for future use.
1112 static DIR *wxDirStream
= (DIR *) NULL
;
1113 static char *wxFileSpec
= (char *) NULL
;
1114 static int wxFindFileFlags
= 0;
1117 char *wxFindFirstFile(const char *spec
, int flags
)
1121 closedir(wxDirStream
); // edz 941103: better housekeping
1123 wxFindFileFlags
= flags
;
1126 delete[] wxFileSpec
;
1127 wxFileSpec
= copystring(spec
);
1129 // Find path only so we can concatenate
1130 // found file onto path
1131 char *p
= wxPathOnly(wxFileSpec
);
1133 /* MATTHEW: special case: path is really "/" */
1134 if (p
&& !*p
&& *wxFileSpec
== '/')
1136 /* MATTHEW: p is NULL => Local directory */
1140 if ((wxDirStream
=opendir(p
))==NULL
)
1141 return (char *) NULL
;
1143 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1144 return wxFindNextFile();
1147 return (char *) NULL
;
1150 char *wxFindNextFile(void)
1153 static char buf
[400]; // FIXME static buffer
1155 /* MATTHEW: [2] Don't crash if we read too many times */
1157 return (char *) NULL
;
1159 // Find path only so we can concatenate
1160 // found file onto path
1161 char *p
= wxPathOnly(wxFileSpec
);
1162 char *n
= wxFileNameFromPath(wxFileSpec
);
1164 /* MATTHEW: special case: path is really "/" */
1165 if (p
&& !*p
&& *wxFileSpec
== '/')
1169 struct dirent
*nextDir
;
1170 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1173 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1174 directories when flags & wxDIR */
1175 if (wxMatchWild(n
, nextDir
->d_name
)) {
1181 if (strcmp(p
, "/") != 0)
1184 strcat(buf
, nextDir
->d_name
);
1186 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1187 (strcmp(nextDir
->d_name
, "..") == 0)) {
1188 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1192 isdir
= wxDirExists(buf
);
1194 if (!wxFindFileFlags
1195 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1196 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1200 closedir(wxDirStream
);
1201 wxDirStream
= (DIR *) NULL
;
1205 return (char *) NULL
;
1208 #elif defined(__WXMSW__)
1211 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1212 WIN32_FIND_DATA wxFileStruc
;
1215 static struct ffblk wxFileStruc
;
1217 static struct _find_t wxFileStruc
;
1220 static wxString wxFileSpec
= "";
1221 static int wxFindFileFlags
;
1223 char *wxFindFirstFile(const char *spec
, int flags
)
1226 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1228 // Find path only so we can concatenate
1229 // found file onto path
1230 wxString
path1(wxFileSpec
);
1231 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1232 if (p
&& (strlen(p
) > 0))
1233 strcpy(wxBuffer
, p
);
1238 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1239 FindClose(wxFileStrucHandle
);
1241 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1243 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1246 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1248 if (isdir
&& !(flags
& wxDIR
))
1249 return wxFindNextFile();
1250 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1251 return wxFindNextFile();
1253 if (wxBuffer
[0] != 0)
1254 strcat(wxBuffer
, "\\");
1255 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1259 int flag
= _A_NORMAL
;
1260 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1264 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1266 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1269 /* MATTHEW: [5] Check directory flag */
1273 attrib
= wxFileStruc
.ff_attrib
;
1275 attrib
= wxFileStruc
.attrib
;
1278 if (attrib
& _A_SUBDIR
) {
1279 if (!(wxFindFileFlags
& wxDIR
))
1280 return wxFindNextFile();
1281 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1282 return wxFindNextFile();
1284 if (wxBuffer
[0] != 0)
1285 strcat(wxBuffer
, "\\");
1288 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1290 strcat(wxBuffer
, wxFileStruc
.name
);
1299 char *wxFindNextFile(void)
1301 // Find path only so we can concatenate
1302 // found file onto path
1303 wxString
p2(wxFileSpec
);
1304 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1305 if (p
&& (strlen(p
) > 0))
1306 strcpy(wxBuffer
, p
);
1313 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1316 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1318 FindClose(wxFileStrucHandle
);
1319 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1323 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1325 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1327 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1330 if (wxBuffer
[0] != 0)
1331 strcat(wxBuffer
, "\\");
1332 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1337 if (findnext(&wxFileStruc
) == 0)
1339 if (_dos_findnext(&wxFileStruc
) == 0)
1342 /* MATTHEW: [5] Check directory flag */
1346 attrib
= wxFileStruc
.ff_attrib
;
1348 attrib
= wxFileStruc
.attrib
;
1351 if (attrib
& _A_SUBDIR
) {
1352 if (!(wxFindFileFlags
& wxDIR
))
1354 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1358 if (wxBuffer
[0] != 0)
1359 strcat(wxBuffer
, "\\");
1361 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1363 strcat(wxBuffer
, wxFileStruc
.name
);
1375 // Get current working directory.
1376 // If buf is NULL, allocates space using new, else
1378 char *wxGetWorkingDirectory(char *buf
, int sz
)
1381 buf
= new char[sz
+1];
1383 if (_getcwd(buf
, sz
) == NULL
) {
1385 if (getcwd(buf
, sz
) == NULL
) {
1393 bool wxSetWorkingDirectory(const wxString
& d
)
1395 #if defined( __UNIX__ ) || defined( __WXMAC__ )
1396 return (chdir(d
) == 0);
1397 #elif defined(__WINDOWS__)
1400 return (bool)(SetCurrentDirectory(d
) != 0);
1402 // Must change drive, too.
1403 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1406 char firstChar
= d
[0];
1410 firstChar
= firstChar
- 32;
1412 // To a drive number
1413 unsigned int driveNo
= firstChar
- 64;
1416 unsigned int noDrives
;
1417 _dos_setdrive(driveNo
, &noDrives
);
1420 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1428 bool wxEndsWithPathSeparator(const char *pszFileName
)
1430 size_t len
= Strlen(pszFileName
);
1434 return wxIsPathSeparator(pszFileName
[len
- 1]);
1437 // find a file in a list of directories, returns false if not found
1438 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1440 // we assume that it's not empty
1441 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1442 _("empty file name in wxFindFileInPath"));
1444 // skip path separator in the beginning of the file name if present
1445 if ( wxIsPathSeparator(*pszFile
) )
1448 // copy the path (strtok will modify it)
1449 char *szPath
= new char[strlen(pszPath
) + 1];
1450 strcpy(szPath
, pszPath
);
1454 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1455 // search for the file in this directory
1457 if ( !wxEndsWithPathSeparator(pc
) )
1458 strFile
+= FILE_SEP_PATH
;
1461 if ( FileExists(strFile
) ) {
1469 return pc
!= NULL
; // if true => we breaked from the loop
1472 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1477 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1479 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1480 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1481 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1483 // take the last of the two
1484 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1485 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1486 if ( nPosDos
> nPosUnix
)
1488 // size_t nLen = Strlen(pszFileName);
1491 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1493 size_t nPosDot
= pDot
- pszFileName
;
1495 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1497 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1501 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1507 //------------------------------------------------------------------------
1508 // wild character routines
1509 //------------------------------------------------------------------------
1511 bool wxIsWild( const wxString
& pattern
)
1513 wxString tmp
= pattern
;
1514 char *pat
= WXSTRINGCAST(tmp
);
1517 case '?': case '*': case '[': case '{':
1527 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1529 #if defined(HAVE_FNMATCH_H)
1532 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1534 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1538 // #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1541 * WARNING: this code is broken!
1544 wxString tmp1
= pat
;
1545 char *pattern
= WXSTRINGCAST(tmp1
);
1546 wxString tmp2
= text
;
1547 char *str
= WXSTRINGCAST(tmp2
);
1550 bool done
= FALSE
, ret_code
, ok
;
1551 // Below is for vi fans
1552 const char OB
= '{', CB
= '}';
1554 // dot_special means '.' only matches '.'
1555 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1558 while ((*pattern
!= '\0') && (!done
)
1559 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1563 if (*pattern
!= '\0')
1570 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1573 while (*str
!= '\0')
1575 while (*pattern
!= '\0')
1582 if ((*pattern
== '\0') || (*pattern
== ']')) {
1586 if (*pattern
== '\\') {
1588 if (*pattern
== '\0') {
1593 if (*(pattern
+ 1) == '-') {
1596 if (*pattern
== ']') {
1600 if (*pattern
== '\\') {
1602 if (*pattern
== '\0') {
1607 if ((*str
< c
) || (*str
> *pattern
)) {
1611 } else if (*pattern
!= *str
) {
1616 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1617 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1621 if (*pattern
!= '\0') {
1631 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1634 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1635 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1636 if (*pattern
== '\\')
1638 ok
= (*pattern
++ == *cp
++);
1640 if (*pattern
== '\0') {
1646 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1647 if (*++pattern
== '\\') {
1648 if (*++pattern
== CB
)
1653 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1654 if (*++pattern
== '\\') {
1655 if (*++pattern
== CB
|| *pattern
== ',')
1660 if (*pattern
!= '\0')
1665 if (*str
== *pattern
) {
1672 while (*pattern
== '*')
1674 return ((*str
== '\0') && (*pattern
== '\0'));
1680 #pragma warning(default:4706) // assignment within conditional expression