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);
654 // Return just the directory, or NULL if no directory
656 wxPathOnly (char *path
)
660 static char buf
[_MAXPATHLEN
];
665 int l
= strlen(path
);
670 // Search backward for a backward or forward slash
671 while (!done
&& i
> -1)
674 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
689 // Try Drive specifier
690 if (isalpha (buf
[0]) && buf
[1] == ':')
692 // A:junk --> A:. (since A:.\junk Not A:\junk)
700 return (char *) NULL
;
703 // Return just the directory, or NULL if no directory
704 wxString
wxPathOnly (const wxString
& path
)
708 char buf
[_MAXPATHLEN
];
711 strcpy (buf
, WXSTRINGCAST path
);
713 int l
= path
.Length();
718 // Search backward for a backward or forward slash
719 while (!done
&& i
> -1)
722 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
731 return wxString(buf
);
737 // Try Drive specifier
738 if (isalpha (buf
[0]) && buf
[1] == ':')
740 // A:junk --> A:. (since A:.\junk Not A:\junk)
743 return wxString(buf
);
751 // Utility for converting delimiters in DOS filenames to UNIX style
752 // and back again - or we get nasty problems with delimiters.
753 // Also, convert to lower case, since case is significant in UNIX.
757 wxMac2UnixFilename (char *s
)
761 memmove( s
+1 , s
,strlen( s
) + 1) ;
772 *s
= wxToLower (*s
); // Case INDEPENDENT
779 wxUnix2MacFilename (char *s
)
785 // relative path , since it goes on with slash which is translated to a :
786 memmove( s
, s
+1 ,strlen( s
) ) ;
788 else if ( *s
== '/' )
790 // absolute path -> on mac just start with the drive name
791 memmove( s
, s
+1 ,strlen( s
) ) ;
795 wxASSERT_MSG( 1 , "unkown path beginning" ) ;
799 if (*s
== '/' || *s
== '\\')
808 wxDos2UnixFilename (char *s
)
817 *s
= wxToLower (*s
); // Case INDEPENDENT
825 wxUnix2DosFilename (char *s
)
827 wxUnix2DosFilename (char *WXUNUSED(s
))
830 // Yes, I really mean this to happen under DOS only! JACS
842 // Concatenate two files to form third
844 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
846 char *outfile
= wxGetTempFileName("cat");
848 FILE *fp1
= (FILE *) NULL
;
849 FILE *fp2
= (FILE *) NULL
;
850 FILE *fp3
= (FILE *) NULL
;
851 // Open the inputs and outputs
853 strcpy( gwxMacFileName
, file1
) ;
854 wxUnix2MacFilename( gwxMacFileName
) ;
855 strcpy( gwxMacFileName2
, file2
) ;
856 wxUnix2MacFilename( gwxMacFileName2
) ;
857 strcpy( gwxMacFileName3
, outfile
) ;
858 wxUnix2MacFilename( gwxMacFileName3
) ;
860 if ((fp1
= fopen (gwxMacFileName
, "rb")) == NULL
||
861 (fp2
= fopen (gwxMacFileName2
, "rb")) == NULL
||
862 (fp3
= fopen (gwxMacFileName3
, "wb")) == NULL
)
864 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
865 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
866 (fp3
= fopen (outfile
, "wb")) == NULL
)
879 while ((ch
= getc (fp1
)) != EOF
)
880 (void) putc (ch
, fp3
);
883 while ((ch
= getc (fp2
)) != EOF
)
884 (void) putc (ch
, fp3
);
888 bool result
= wxRenameFile(outfile
, file3
);
895 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
902 strcpy( gwxMacFileName
, file1
) ;
903 wxUnix2MacFilename( gwxMacFileName
) ;
904 strcpy( gwxMacFileName2
, file2
) ;
905 wxUnix2MacFilename( gwxMacFileName2
) ;
907 if ((fd1
= fopen (gwxMacFileName
, "rb")) == NULL
)
909 if ((fd2
= fopen (gwxMacFileName2
, "wb")) == NULL
)
911 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
913 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
920 while ((ch
= getc (fd1
)) != EOF
)
921 (void) putc (ch
, fd2
);
929 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
932 strcpy( gwxMacFileName
, file1
) ;
933 wxUnix2MacFilename( gwxMacFileName
) ;
934 strcpy( gwxMacFileName2
, file2
) ;
935 wxUnix2MacFilename( gwxMacFileName2
) ;
937 if (0 == rename (gwxMacFileName
, gwxMacFileName2
))
940 // Normal system call
941 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
945 if (wxCopyFile(file1
, file2
)) {
953 bool wxRemoveFile(const wxString
& file
)
955 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
956 int flag
= remove(WXSTRINGCAST file
);
957 #elif defined( __WXMAC__ )
958 strcpy( gwxMacFileName
, file
) ;
959 wxUnix2MacFilename( gwxMacFileName
) ;
960 int flag
= unlink(gwxMacFileName
);
962 int flag
= unlink(WXSTRINGCAST file
);
967 bool wxMkdir(const wxString
& dir
)
969 #if defined(__WXSTUBS__)
971 #elif defined(__VMS__)
973 #elif defined( __WXMAC__ )
974 strcpy( gwxMacFileName
, dir
) ;
975 wxUnix2MacFilename( gwxMacFileName
) ;
976 return (mkdir(gwxMacFileName
, 0 ) == 0);
977 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
978 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
980 return (mkdir(WXSTRINGCAST dir
) == 0);
984 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
988 #elif defined( __WXMAC__ )
989 strcpy( gwxMacFileName
, dir
) ;
990 wxUnix2MacFilename( gwxMacFileName
) ;
991 return (rmdir(WXSTRINGCAST gwxMacFileName
) == 0);
993 return (rmdir(WXSTRINGCAST dir
) == 0);
998 bool wxDirExists(const wxString
& dir
)
1002 #elif !defined(__WXMSW__)
1004 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
1007 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
1008 #if defined(__WIN32__)
1009 WIN32_FIND_DATA fileInfo
;
1012 struct ffblk fileInfo
;
1014 struct find_t fileInfo
;
1018 #if defined(__WIN32__)
1019 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
1021 if (h
==INVALID_HANDLE_VALUE
)
1025 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
1028 // In Borland findfirst has a different argument
1029 // ordering from _dos_findfirst. But _dos_findfirst
1030 // _should_ be ok in both MS and Borland... why not?
1032 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
1034 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
1043 // does the path exists? (may have or not '/' or '\\' at the end)
1044 bool wxPathExists(const char *pszPathName
)
1046 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
1047 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
1048 wxString
strPath(pszPathName
);
1049 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
1050 strPath
.Last() = '\0';
1053 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
1056 // Get a temporary filename, opening and closing the file.
1057 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
1063 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
1066 char tmpPath
[MAX_PATH
];
1067 ::GetTempPath(MAX_PATH
, tmpPath
);
1068 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
1070 if (buf
) strcpy(buf
, tmp
);
1071 else buf
= copystring(tmp
);
1075 static short last_temp
= 0; // cache last to speed things a bit
1076 // At most 1000 temp files to a process! We use a ring count.
1077 char tmp
[100]; // FIXME static buffer
1079 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
1081 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
1082 if (!wxFileExists( tmp
))
1084 // Touch the file to create it (reserve name)
1085 FILE *fd
= fopen (tmp
, "w");
1092 buf
= copystring( tmp
);
1096 wxLogError( _("wxWindows: error finding temporary file name.\n") );
1097 if (buf
) buf
[0] = 0;
1098 return (char *) NULL
;
1102 // Get first file name matching given wild card.
1106 // Get first file name matching given wild card.
1107 // Flags are reserved for future use.
1110 static DIR *wxDirStream
= (DIR *) NULL
;
1111 static char *wxFileSpec
= (char *) NULL
;
1112 static int wxFindFileFlags
= 0;
1115 char *wxFindFirstFile(const char *spec
, int flags
)
1119 closedir(wxDirStream
); // edz 941103: better housekeping
1121 wxFindFileFlags
= flags
;
1124 delete[] wxFileSpec
;
1125 wxFileSpec
= copystring(spec
);
1127 // Find path only so we can concatenate
1128 // found file onto path
1129 char *p
= wxPathOnly(wxFileSpec
);
1131 /* MATTHEW: special case: path is really "/" */
1132 if (p
&& !*p
&& *wxFileSpec
== '/')
1134 /* MATTHEW: p is NULL => Local directory */
1138 if ((wxDirStream
=opendir(p
))==NULL
)
1139 return (char *) NULL
;
1141 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1142 return wxFindNextFile();
1145 return (char *) NULL
;
1148 char *wxFindNextFile(void)
1151 static char buf
[400]; // FIXME static buffer
1153 /* MATTHEW: [2] Don't crash if we read too many times */
1155 return (char *) NULL
;
1157 // Find path only so we can concatenate
1158 // found file onto path
1159 char *p
= wxPathOnly(wxFileSpec
);
1160 char *n
= wxFileNameFromPath(wxFileSpec
);
1162 /* MATTHEW: special case: path is really "/" */
1163 if (p
&& !*p
&& *wxFileSpec
== '/')
1167 struct dirent
*nextDir
;
1168 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1171 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1172 directories when flags & wxDIR */
1173 if (wxMatchWild(n
, nextDir
->d_name
)) {
1179 if (strcmp(p
, "/") != 0)
1182 strcat(buf
, nextDir
->d_name
);
1184 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1185 (strcmp(nextDir
->d_name
, "..") == 0)) {
1186 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1190 isdir
= wxDirExists(buf
);
1192 if (!wxFindFileFlags
1193 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1194 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1198 closedir(wxDirStream
);
1199 wxDirStream
= (DIR *) NULL
;
1203 return (char *) NULL
;
1206 #elif defined(__WXMSW__)
1209 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1210 WIN32_FIND_DATA wxFileStruc
;
1213 static struct ffblk wxFileStruc
;
1215 static struct _find_t wxFileStruc
;
1218 static wxString wxFileSpec
= "";
1219 static int wxFindFileFlags
;
1221 char *wxFindFirstFile(const char *spec
, int flags
)
1224 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1226 // Find path only so we can concatenate
1227 // found file onto path
1228 wxString
path1(wxFileSpec
);
1229 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1230 if (p
&& (strlen(p
) > 0))
1231 strcpy(wxBuffer
, p
);
1236 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1237 FindClose(wxFileStrucHandle
);
1239 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1241 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1244 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1246 if (isdir
&& !(flags
& wxDIR
))
1247 return wxFindNextFile();
1248 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1249 return wxFindNextFile();
1251 if (wxBuffer
[0] != 0)
1252 strcat(wxBuffer
, "\\");
1253 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1257 int flag
= _A_NORMAL
;
1258 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1262 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1264 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1267 /* MATTHEW: [5] Check directory flag */
1271 attrib
= wxFileStruc
.ff_attrib
;
1273 attrib
= wxFileStruc
.attrib
;
1276 if (attrib
& _A_SUBDIR
) {
1277 if (!(wxFindFileFlags
& wxDIR
))
1278 return wxFindNextFile();
1279 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1280 return wxFindNextFile();
1282 if (wxBuffer
[0] != 0)
1283 strcat(wxBuffer
, "\\");
1286 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1288 strcat(wxBuffer
, wxFileStruc
.name
);
1297 char *wxFindNextFile(void)
1299 // Find path only so we can concatenate
1300 // found file onto path
1301 wxString
p2(wxFileSpec
);
1302 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1303 if (p
&& (strlen(p
) > 0))
1304 strcpy(wxBuffer
, p
);
1311 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1314 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1316 FindClose(wxFileStrucHandle
);
1317 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1321 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1323 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1325 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1328 if (wxBuffer
[0] != 0)
1329 strcat(wxBuffer
, "\\");
1330 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1335 if (findnext(&wxFileStruc
) == 0)
1337 if (_dos_findnext(&wxFileStruc
) == 0)
1340 /* MATTHEW: [5] Check directory flag */
1344 attrib
= wxFileStruc
.ff_attrib
;
1346 attrib
= wxFileStruc
.attrib
;
1349 if (attrib
& _A_SUBDIR
) {
1350 if (!(wxFindFileFlags
& wxDIR
))
1352 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1356 if (wxBuffer
[0] != 0)
1357 strcat(wxBuffer
, "\\");
1359 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1361 strcat(wxBuffer
, wxFileStruc
.name
);
1373 // Get current working directory.
1374 // If buf is NULL, allocates space using new, else
1376 char *wxGetWorkingDirectory(char *buf
, int sz
)
1379 buf
= new char[sz
+1];
1381 if (_getcwd(buf
, sz
) == NULL
) {
1383 if (getcwd(buf
, sz
) == NULL
) {
1391 bool wxSetWorkingDirectory(const wxString
& d
)
1393 #if defined( __UNIX__ ) || defined( __WXMAC__ )
1394 return (chdir(d
) == 0);
1395 #elif defined(__WINDOWS__)
1398 return (bool)(SetCurrentDirectory(d
) != 0);
1400 // Must change drive, too.
1401 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1404 char firstChar
= d
[0];
1408 firstChar
= firstChar
- 32;
1410 // To a drive number
1411 unsigned int driveNo
= firstChar
- 64;
1414 unsigned int noDrives
;
1415 _dos_setdrive(driveNo
, &noDrives
);
1418 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1426 bool wxEndsWithPathSeparator(const char *pszFileName
)
1428 size_t len
= Strlen(pszFileName
);
1432 return wxIsPathSeparator(pszFileName
[len
- 1]);
1435 // find a file in a list of directories, returns false if not found
1436 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1438 // we assume that it's not empty
1439 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1440 _("empty file name in wxFindFileInPath"));
1442 // skip path separator in the beginning of the file name if present
1443 if ( wxIsPathSeparator(*pszFile
) )
1446 // copy the path (strtok will modify it)
1447 char *szPath
= new char[strlen(pszPath
) + 1];
1448 strcpy(szPath
, pszPath
);
1452 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1453 // search for the file in this directory
1455 if ( !wxEndsWithPathSeparator(pc
) )
1456 strFile
+= FILE_SEP_PATH
;
1459 if ( FileExists(strFile
) ) {
1467 return pc
!= NULL
; // if true => we breaked from the loop
1470 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1475 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1477 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1478 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1479 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1481 // take the last of the two
1482 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1483 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1484 if ( nPosDos
> nPosUnix
)
1486 // size_t nLen = Strlen(pszFileName);
1489 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1491 size_t nPosDot
= pDot
- pszFileName
;
1493 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1495 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1499 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1505 //------------------------------------------------------------------------
1506 // wild character routines
1507 //------------------------------------------------------------------------
1509 bool wxIsWild( const wxString
& pattern
)
1511 wxString tmp
= pattern
;
1512 char *pat
= WXSTRINGCAST(tmp
);
1515 case '?': case '*': case '[': case '{':
1525 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1527 #if defined(HAVE_FNMATCH_H)
1530 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1532 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1536 // #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1539 * WARNING: this code is broken!
1542 wxString tmp1
= pat
;
1543 char *pattern
= WXSTRINGCAST(tmp1
);
1544 wxString tmp2
= text
;
1545 char *str
= WXSTRINGCAST(tmp2
);
1548 bool done
= FALSE
, ret_code
, ok
;
1549 // Below is for vi fans
1550 const char OB
= '{', CB
= '}';
1552 // dot_special means '.' only matches '.'
1553 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1556 while ((*pattern
!= '\0') && (!done
)
1557 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1561 if (*pattern
!= '\0')
1568 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1571 while (*str
!= '\0')
1573 while (*pattern
!= '\0')
1580 if ((*pattern
== '\0') || (*pattern
== ']')) {
1584 if (*pattern
== '\\') {
1586 if (*pattern
== '\0') {
1591 if (*(pattern
+ 1) == '-') {
1594 if (*pattern
== ']') {
1598 if (*pattern
== '\\') {
1600 if (*pattern
== '\0') {
1605 if ((*str
< c
) || (*str
> *pattern
)) {
1609 } else if (*pattern
!= *str
) {
1614 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1615 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1619 if (*pattern
!= '\0') {
1629 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1632 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1633 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1634 if (*pattern
== '\\')
1636 ok
= (*pattern
++ == *cp
++);
1638 if (*pattern
== '\0') {
1644 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1645 if (*++pattern
== '\\') {
1646 if (*++pattern
== CB
)
1651 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1652 if (*++pattern
== '\\') {
1653 if (*++pattern
== CB
|| *pattern
== ',')
1658 if (*pattern
!= '\0')
1663 if (*str
== *pattern
) {
1670 while (*pattern
== '*')
1672 return ((*str
== '\0') && (*pattern
== '\0'));
1678 #pragma warning(default:4706) // assignment within conditional expression