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__ ) && !defined(__SALFORDC__)
68 #include <sys/unistd.h>
71 #define stricmp strcasecmp
74 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
75 // this (3.1 I believe) and how to test for it.
76 // If this works for Borland 4.0 as well, then no worries.
88 // No, Cygwin doesn't appear to have fnmatch.h after all.
89 #if defined(HAVE_FNMATCH_H)
97 #define _MAXPATHLEN 500
99 extern char *wxBuffer
;
101 extern char gwxMacFileName
[] ;
102 extern char gwxMacFileName2
[] ;
103 extern char gwxMacFileName3
[] ;
106 #if !USE_SHARED_LIBRARIES
107 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
110 void wxPathList::Add (const wxString
& path
)
112 wxStringList::Add ((char *)(const char *)path
);
115 // Add paths e.g. from the PATH environment variable
116 void wxPathList::AddEnvList (const wxString
& envVariable
)
118 static const char PATH_TOKS
[] =
120 " ;"; // Don't seperate with colon in DOS (used for drive)
125 char *val
= getenv (WXSTRINGCAST envVariable
);
128 char *s
= copystring (val
);
129 char *token
= strtok (s
, PATH_TOKS
);
133 Add (copystring (token
));
136 if ((token
= strtok ((char *) NULL
, PATH_TOKS
)) != NULL
)
137 Add (wxString(token
));
144 // Given a full filename (with path), ensure that that file can
145 // be accessed again USING FILENAME ONLY by adding the path
146 // to the list if not already there.
147 void wxPathList::EnsureFileAccessible (const wxString
& path
)
149 wxString
path1(path
);
150 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
153 if (!Member (wxString(path_only
)))
154 Add (wxString(path_only
));
158 bool wxPathList::Member (const wxString
& path
)
160 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
162 wxString
path2((char *) node
->Data ());
164 #if defined(__WINDOWS__) || defined(__VMS__) || defined (__WXMAC__)
166 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
168 // Case sensitive File System
169 path
.CompareTo (path2
) == 0
177 wxString
wxPathList::FindValidPath (const wxString
& file
)
179 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
180 return wxString(wxBuffer
);
182 char buf
[_MAXPATHLEN
];
183 strcpy(buf
, wxBuffer
);
185 char *filename
= (char*) NULL
; /* shut up buggy egcs warning */
186 filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
188 for (wxNode
* node
= First (); node
; node
= node
->Next ())
190 char *path
= (char *) node
->Data ();
191 strcpy (wxBuffer
, path
);
192 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
193 if (ch
!= '\\' && ch
!= '/')
194 strcat (wxBuffer
, "/");
195 strcat (wxBuffer
, filename
);
197 Unix2DosFilename (wxBuffer
);
199 if (wxFileExists (wxBuffer
))
201 return wxString(wxBuffer
); // Found!
205 return wxString(""); // Not found
208 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
210 wxString f
= FindValidPath(file
);
211 if (wxIsAbsolutePath(f
))
216 wxGetWorkingDirectory(buf
, 499);
217 int len
= (int)strlen(buf
);
221 if (lastCh
!= '/' && lastCh
!= '\\')
229 strcat(buf
, (const char *)f
);
230 strcpy(wxBuffer
, buf
);
231 return wxString(wxBuffer
);
236 wxFileExists (const wxString
& filename
)
238 #ifdef __GNUWIN32__ // (fix a B20 bug)
239 if (GetFileAttributes(filename
) == 0xFFFFFFFF)
243 #elif defined(__WXMAC__)
245 strcpy( gwxMacFileName
, filename
) ;
246 wxUnix2MacFilename( gwxMacFileName
) ;
247 if (gwxMacFileName
&& stat ((char *)(const char *)gwxMacFileName
, &stbuf
) == 0)
258 if ((filename
!= "") && stat ((char *)(const char *)filename
, &stbuf
) == 0)
264 /* Vadim's alternative implementation
266 // does the file exist?
267 bool wxFileExists(const char *pszFileName)
270 return !access(pszFileName, 0) &&
271 !stat(pszFileName, &st) &&
272 (st.st_mode & S_IFREG);
277 wxIsAbsolutePath (const wxString
& filename
)
281 if (filename
[0] == '/'
283 || (filename
[0] == '[' && filename
[1] != '.')
287 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
296 * Strip off any extension (dot something) from end of file,
297 * IF one exists. Inserts zero into buffer.
301 void wxStripExtension(char *buffer
)
303 int len
= strlen(buffer
);
307 if (buffer
[i
] == '.')
316 void wxStripExtension(wxString
& buffer
)
318 size_t len
= buffer
.Length();
322 if (buffer
.GetChar(i
) == '.')
324 buffer
= buffer
.Left(i
);
331 // Destructive removal of /./ and /../ stuff
332 char *wxRealPath (char *path
)
335 static const char SEP
= '\\';
336 Unix2DosFilename(path
);
338 static const char SEP
= '/';
340 if (path
[0] && path
[1]) {
341 /* MATTHEW: special case "/./x" */
343 if (path
[2] == SEP
&& path
[1] == '.')
351 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
354 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
355 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
356 && (q
- 1 <= path
|| q
[-1] != SEP
))
365 /* Check that path[2] is NULL! */
366 else if (path
[1] == ':' && !path
[2])
375 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
384 char *wxCopyAbsolutePath(const wxString
& filename
)
387 return (char *) NULL
;
389 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
390 char buf
[_MAXPATHLEN
];
392 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
393 char ch
= buf
[strlen(buf
) - 1];
395 if (ch
!= '\\' && ch
!= '/')
401 strcat(buf
, wxBuffer
);
402 return copystring( wxRealPath(buf
) );
404 return copystring( wxBuffer
);
410 ~user/ => user's home dir
411 If the environment variable a = "foo" and b = "bar" then:
428 /* input name in name, pathname output to buf. */
430 char *wxExpandPath(char *buf
, const char *name
)
432 register char *d
, *s
, *nm
;
433 char lnm
[_MAXPATHLEN
];
436 // Some compilers don't like this line.
437 // const char trimchars[] = "\n \t";
446 const char SEP
= '\\';
448 const char SEP
= '/';
451 if (name
== NULL
|| *name
== '\0')
453 nm
= copystring(name
); // Make a scratch copy
456 /* Skip leading whitespace and cr */
457 while (strchr((char *)trimchars
, *nm
) != NULL
)
459 /* And strip off trailing whitespace and cr */
460 s
= nm
+ (q
= strlen(nm
)) - 1;
461 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
469 q
= nm
[0] == '\\' && nm
[1] == '~';
472 /* Expand inline environment variables */
473 while ((*d
++ = *s
)) {
476 if ((*(d
- 1) = *++s
)) {
484 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
489 register char *start
= d
;
490 register int braces
= (*s
== '{' || *s
== '(');
491 register char *value
;
493 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
498 value
= getenv(braces
? start
+ 1 : start
);
500 for ((d
= start
- 1); (*d
++ = *value
++););
508 /* Expand ~ and ~user */
511 if (nm
[0] == '~' && !q
)
514 if (nm
[1] == SEP
|| nm
[1] == 0)
516 if ((s
= wxGetUserHome("")) != NULL
) {
521 { /* ~user/filename */
524 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
525 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
526 was_sep
= (*s
== SEP
);
527 nnm
= *s
? s
+ 1 : s
;
529 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
530 if (was_sep
) /* replace only if it was there: */
541 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
543 while ('\0' != (*d
++ = *s
++))
546 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
550 while ((*d
++ = *s
++));
552 delete[] nm_tmp
; // clean up alloc
553 /* Now clean up the buffer */
554 return wxRealPath(buf
);
558 /* Contract Paths to be build upon an environment variable
561 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
563 The call wxExpandPath can convert these back!
566 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
568 static char dest
[_MAXPATHLEN
];
571 return (char *) NULL
;
573 strcpy (dest
, WXSTRINGCAST filename
);
575 Unix2DosFilename(dest
);
578 // Handle environment
579 char *val
= (char *) NULL
;
580 char *tcp
= (char *) NULL
;
581 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
582 (tcp
= strstr (dest
, val
)) != NULL
)
584 strcpy (wxBuffer
, tcp
+ strlen (val
));
587 strcpy (tcp
, WXSTRINGCAST envname
);
589 strcat (tcp
, wxBuffer
);
592 // Handle User's home (ignore root homes!)
594 if ((val
= wxGetUserHome (user
)) != NULL
&&
595 (len
= strlen(val
)) > 2 &&
596 strncmp(dest
, val
, len
) == 0)
598 strcpy(wxBuffer
, "~");
600 strcat(wxBuffer
, (const char*) user
);
602 // strcat(wxBuffer, "\\");
604 // strcat(wxBuffer, "/");
606 strcat(wxBuffer
, dest
+ len
);
607 strcpy (dest
, wxBuffer
);
613 // Return just the filename, not the path
615 char *wxFileNameFromPath (char *path
)
621 tcp
= path
+ strlen (path
);
622 while (--tcp
>= path
)
624 if (*tcp
== '/' || *tcp
== '\\'
626 || *tcp
== ':' || *tcp
== ']')
633 if (isalpha (*path
) && *(path
+ 1) == ':')
640 wxString
wxFileNameFromPath (const wxString
& path1
)
645 char *path
= WXSTRINGCAST path1
;
648 tcp
= path
+ strlen (path
);
649 while (--tcp
>= path
)
651 if (*tcp
== '/' || *tcp
== '\\'
653 || *tcp
== ':' || *tcp
== ']')
657 return wxString(tcp
+ 1);
660 if (isalpha (*path
) && *(path
+ 1) == ':')
661 return wxString(path
+ 2);
664 // Yes, this should return the path, not an empty string, otherwise
665 // we get "thing.txt" -> "".
669 // Return just the directory, or NULL if no directory
671 wxPathOnly (char *path
)
675 static char buf
[_MAXPATHLEN
];
680 int l
= strlen(path
);
685 // Search backward for a backward or forward slash
686 while (!done
&& i
> -1)
689 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
704 // Try Drive specifier
705 if (isalpha (buf
[0]) && buf
[1] == ':')
707 // A:junk --> A:. (since A:.\junk Not A:\junk)
715 return (char *) NULL
;
718 // Return just the directory, or NULL if no directory
719 wxString
wxPathOnly (const wxString
& path
)
723 char buf
[_MAXPATHLEN
];
726 strcpy (buf
, WXSTRINGCAST path
);
728 int l
= path
.Length();
733 // Search backward for a backward or forward slash
734 while (!done
&& i
> -1)
737 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
746 return wxString(buf
);
752 // Try Drive specifier
753 if (isalpha (buf
[0]) && buf
[1] == ':')
755 // A:junk --> A:. (since A:.\junk Not A:\junk)
758 return wxString(buf
);
766 // Utility for converting delimiters in DOS filenames to UNIX style
767 // and back again - or we get nasty problems with delimiters.
768 // Also, convert to lower case, since case is significant in UNIX.
772 wxMac2UnixFilename (char *s
)
776 memmove( s
+1 , s
,strlen( s
) + 1) ;
787 *s
= wxToLower (*s
); // Case INDEPENDENT
794 wxUnix2MacFilename (char *s
)
800 // relative path , since it goes on with slash which is translated to a :
801 memmove( s
, s
+1 ,strlen( s
) ) ;
803 else if ( *s
== '/' )
805 // absolute path -> on mac just start with the drive name
806 memmove( s
, s
+1 ,strlen( s
) ) ;
810 wxASSERT_MSG( 1 , "unkown path beginning" ) ;
814 if (*s
== '/' || *s
== '\\')
823 wxDos2UnixFilename (char *s
)
832 *s
= wxToLower (*s
); // Case INDEPENDENT
840 wxUnix2DosFilename (char *s
)
842 wxUnix2DosFilename (char *WXUNUSED(s
))
845 // Yes, I really mean this to happen under DOS only! JACS
857 // Concatenate two files to form third
859 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
861 char *outfile
= wxGetTempFileName("cat");
863 FILE *fp1
= (FILE *) NULL
;
864 FILE *fp2
= (FILE *) NULL
;
865 FILE *fp3
= (FILE *) NULL
;
866 // Open the inputs and outputs
868 strcpy( gwxMacFileName
, file1
) ;
869 wxUnix2MacFilename( gwxMacFileName
) ;
870 strcpy( gwxMacFileName2
, file2
) ;
871 wxUnix2MacFilename( gwxMacFileName2
) ;
872 strcpy( gwxMacFileName3
, outfile
) ;
873 wxUnix2MacFilename( gwxMacFileName3
) ;
875 if ((fp1
= fopen (gwxMacFileName
, "rb")) == NULL
||
876 (fp2
= fopen (gwxMacFileName2
, "rb")) == NULL
||
877 (fp3
= fopen (gwxMacFileName3
, "wb")) == NULL
)
879 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
880 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
881 (fp3
= fopen (outfile
, "wb")) == NULL
)
894 while ((ch
= getc (fp1
)) != EOF
)
895 (void) putc (ch
, fp3
);
898 while ((ch
= getc (fp2
)) != EOF
)
899 (void) putc (ch
, fp3
);
903 bool result
= wxRenameFile(outfile
, file3
);
910 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
917 strcpy( gwxMacFileName
, file1
) ;
918 wxUnix2MacFilename( gwxMacFileName
) ;
919 strcpy( gwxMacFileName2
, file2
) ;
920 wxUnix2MacFilename( gwxMacFileName2
) ;
922 if ((fd1
= fopen (gwxMacFileName
, "rb")) == NULL
)
924 if ((fd2
= fopen (gwxMacFileName2
, "wb")) == NULL
)
926 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
928 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
935 while ((ch
= getc (fd1
)) != EOF
)
936 (void) putc (ch
, fd2
);
944 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
947 strcpy( gwxMacFileName
, file1
) ;
948 wxUnix2MacFilename( gwxMacFileName
) ;
949 strcpy( gwxMacFileName2
, file2
) ;
950 wxUnix2MacFilename( gwxMacFileName2
) ;
952 if (0 == rename (gwxMacFileName
, gwxMacFileName2
))
955 // Normal system call
956 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
960 if (wxCopyFile(file1
, file2
)) {
968 bool wxRemoveFile(const wxString
& file
)
970 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
971 int flag
= remove(WXSTRINGCAST file
);
972 #elif defined( __WXMAC__ )
973 strcpy( gwxMacFileName
, file
) ;
974 wxUnix2MacFilename( gwxMacFileName
) ;
975 int flag
= unlink(gwxMacFileName
);
977 int flag
= unlink(WXSTRINGCAST file
);
982 bool wxMkdir(const wxString
& dir
)
984 #if defined(__WXSTUBS__)
986 #elif defined(__VMS__)
988 #elif defined( __WXMAC__ )
989 strcpy( gwxMacFileName
, dir
) ;
990 wxUnix2MacFilename( gwxMacFileName
) ;
991 return (mkdir(gwxMacFileName
, 0 ) == 0);
992 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
993 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
995 return (mkdir(WXSTRINGCAST dir
) == 0);
999 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
1003 #elif defined( __WXMAC__ )
1004 strcpy( gwxMacFileName
, dir
) ;
1005 wxUnix2MacFilename( gwxMacFileName
) ;
1006 return (rmdir(WXSTRINGCAST gwxMacFileName
) == 0);
1010 return FALSE
; // What to do?
1012 return (rmdir(WXSTRINGCAST dir
) == 0);
1019 bool wxDirExists(const wxString
& dir
)
1023 #elif !defined(__WXMSW__)
1025 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
1028 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
1029 #if defined(__WIN32__)
1030 WIN32_FIND_DATA fileInfo
;
1033 struct ffblk fileInfo
;
1035 struct find_t fileInfo
;
1039 #if defined(__WIN32__)
1040 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
1042 if (h
==INVALID_HANDLE_VALUE
)
1046 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
1049 // In Borland findfirst has a different argument
1050 // ordering from _dos_findfirst. But _dos_findfirst
1051 // _should_ be ok in both MS and Borland... why not?
1053 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
1055 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
1064 // does the path exists? (may have or not '/' or '\\' at the end)
1065 bool wxPathExists(const char *pszPathName
)
1067 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
1068 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
1069 wxString
strPath(pszPathName
);
1070 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
1071 strPath
.Last() = '\0';
1079 return stat((char*) (const char*) strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
1082 // Get a temporary filename, opening and closing the file.
1083 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
1089 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
1092 char tmpPath
[MAX_PATH
];
1093 ::GetTempPath(MAX_PATH
, tmpPath
);
1094 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
1096 if (buf
) strcpy(buf
, tmp
);
1097 else buf
= copystring(tmp
);
1101 static short last_temp
= 0; // cache last to speed things a bit
1102 // At most 1000 temp files to a process! We use a ring count.
1103 char tmp
[100]; // FIXME static buffer
1105 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
1107 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
1108 if (!wxFileExists( tmp
))
1110 // Touch the file to create it (reserve name)
1111 FILE *fd
= fopen (tmp
, "w");
1118 buf
= copystring( tmp
);
1122 wxLogError( _("wxWindows: error finding temporary file name.\n") );
1123 if (buf
) buf
[0] = 0;
1124 return (char *) NULL
;
1128 // Get first file name matching given wild card.
1132 // Get first file name matching given wild card.
1133 // Flags are reserved for future use.
1136 static DIR *wxDirStream
= (DIR *) NULL
;
1137 static char *wxFileSpec
= (char *) NULL
;
1138 static int wxFindFileFlags
= 0;
1141 char *wxFindFirstFile(const char *spec
, int flags
)
1145 closedir(wxDirStream
); // edz 941103: better housekeping
1147 wxFindFileFlags
= flags
;
1150 delete[] wxFileSpec
;
1151 wxFileSpec
= copystring(spec
);
1153 // Find path only so we can concatenate
1154 // found file onto path
1155 char *p
= wxPathOnly(wxFileSpec
);
1157 /* MATTHEW: special case: path is really "/" */
1158 if (p
&& !*p
&& *wxFileSpec
== '/')
1160 /* MATTHEW: p is NULL => Local directory */
1164 if ((wxDirStream
=opendir(p
))==NULL
)
1165 return (char *) NULL
;
1167 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1168 return wxFindNextFile();
1171 return (char *) NULL
;
1174 char *wxFindNextFile(void)
1177 static char buf
[400]; // FIXME static buffer
1179 /* MATTHEW: [2] Don't crash if we read too many times */
1181 return (char *) NULL
;
1183 // Find path only so we can concatenate
1184 // found file onto path
1185 char *p
= wxPathOnly(wxFileSpec
);
1186 char *n
= wxFileNameFromPath(wxFileSpec
);
1188 /* MATTHEW: special case: path is really "/" */
1189 if (p
&& !*p
&& *wxFileSpec
== '/')
1193 struct dirent
*nextDir
;
1194 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1197 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1198 directories when flags & wxDIR */
1199 if (wxMatchWild(n
, nextDir
->d_name
)) {
1205 if (strcmp(p
, "/") != 0)
1208 strcat(buf
, nextDir
->d_name
);
1210 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1211 (strcmp(nextDir
->d_name
, "..") == 0)) {
1212 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1216 isdir
= wxDirExists(buf
);
1218 if (!wxFindFileFlags
1219 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1220 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1224 closedir(wxDirStream
);
1225 wxDirStream
= (DIR *) NULL
;
1229 return (char *) NULL
;
1232 #elif defined(__WXMSW__)
1235 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1236 WIN32_FIND_DATA wxFileStruc
;
1239 static struct ffblk wxFileStruc
;
1241 static struct _find_t wxFileStruc
;
1244 static wxString wxFileSpec
= "";
1245 static int wxFindFileFlags
;
1247 char *wxFindFirstFile(const char *spec
, int flags
)
1250 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1252 // Find path only so we can concatenate
1253 // found file onto path
1254 wxString
path1(wxFileSpec
);
1255 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1256 if (p
&& (strlen(p
) > 0))
1257 strcpy(wxBuffer
, p
);
1262 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1263 FindClose(wxFileStrucHandle
);
1265 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1267 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1270 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1272 if (isdir
&& !(flags
& wxDIR
))
1273 return wxFindNextFile();
1274 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1275 return wxFindNextFile();
1277 if (wxBuffer
[0] != 0)
1278 strcat(wxBuffer
, "\\");
1279 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1283 int flag
= _A_NORMAL
;
1284 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1288 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1290 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1293 /* MATTHEW: [5] Check directory flag */
1297 attrib
= wxFileStruc
.ff_attrib
;
1299 attrib
= wxFileStruc
.attrib
;
1302 if (attrib
& _A_SUBDIR
) {
1303 if (!(wxFindFileFlags
& wxDIR
))
1304 return wxFindNextFile();
1305 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1306 return wxFindNextFile();
1308 if (wxBuffer
[0] != 0)
1309 strcat(wxBuffer
, "\\");
1312 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1314 strcat(wxBuffer
, wxFileStruc
.name
);
1323 char *wxFindNextFile(void)
1325 // Find path only so we can concatenate
1326 // found file onto path
1327 wxString
p2(wxFileSpec
);
1328 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1329 if (p
&& (strlen(p
) > 0))
1330 strcpy(wxBuffer
, p
);
1337 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1340 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1342 FindClose(wxFileStrucHandle
);
1343 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1347 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1349 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1351 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1354 if (wxBuffer
[0] != 0)
1355 strcat(wxBuffer
, "\\");
1356 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1361 if (findnext(&wxFileStruc
) == 0)
1363 if (_dos_findnext(&wxFileStruc
) == 0)
1366 /* MATTHEW: [5] Check directory flag */
1370 attrib
= wxFileStruc
.ff_attrib
;
1372 attrib
= wxFileStruc
.attrib
;
1375 if (attrib
& _A_SUBDIR
) {
1376 if (!(wxFindFileFlags
& wxDIR
))
1378 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1382 if (wxBuffer
[0] != 0)
1383 strcat(wxBuffer
, "\\");
1385 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1387 strcat(wxBuffer
, wxFileStruc
.name
);
1399 // Get current working directory.
1400 // If buf is NULL, allocates space using new, else
1402 char *wxGetWorkingDirectory(char *buf
, int sz
)
1405 buf
= new char[sz
+1];
1407 if (_getcwd(buf
, sz
) == NULL
) {
1409 if (getcwd(buf
, sz
) == NULL
) {
1417 bool wxSetWorkingDirectory(const wxString
& d
)
1419 #if defined( __UNIX__ ) || defined( __WXMAC__ )
1420 return (chdir(d
) == 0);
1421 #elif defined(__WINDOWS__)
1424 return (bool)(SetCurrentDirectory(d
) != 0);
1426 // Must change drive, too.
1427 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1430 char firstChar
= d
[0];
1434 firstChar
= firstChar
- 32;
1436 // To a drive number
1437 unsigned int driveNo
= firstChar
- 64;
1440 unsigned int noDrives
;
1441 _dos_setdrive(driveNo
, &noDrives
);
1444 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1452 bool wxEndsWithPathSeparator(const char *pszFileName
)
1454 size_t len
= Strlen(pszFileName
);
1458 return wxIsPathSeparator(pszFileName
[len
- 1]);
1461 // find a file in a list of directories, returns false if not found
1462 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1464 // we assume that it's not empty
1465 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1466 _("empty file name in wxFindFileInPath"));
1468 // skip path separator in the beginning of the file name if present
1469 if ( wxIsPathSeparator(*pszFile
) )
1472 // copy the path (strtok will modify it)
1473 char *szPath
= new char[strlen(pszPath
) + 1];
1474 strcpy(szPath
, pszPath
);
1478 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1479 // search for the file in this directory
1481 if ( !wxEndsWithPathSeparator(pc
) )
1482 strFile
+= FILE_SEP_PATH
;
1485 if ( FileExists(strFile
) ) {
1493 return pc
!= NULL
; // if true => we breaked from the loop
1496 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1501 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1503 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1504 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1505 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1507 // take the last of the two
1508 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1509 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1510 if ( nPosDos
> nPosUnix
)
1512 // size_t nLen = Strlen(pszFileName);
1515 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1517 size_t nPosDot
= pDot
- pszFileName
;
1519 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1521 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1525 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1531 //------------------------------------------------------------------------
1532 // wild character routines
1533 //------------------------------------------------------------------------
1535 bool wxIsWild( const wxString
& pattern
)
1537 wxString tmp
= pattern
;
1538 char *pat
= WXSTRINGCAST(tmp
);
1541 case '?': case '*': case '[': case '{':
1551 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1553 #if defined(HAVE_FNMATCH_H)
1556 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1558 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1562 // #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1565 * WARNING: this code is broken!
1568 wxString tmp1
= pat
;
1569 char *pattern
= WXSTRINGCAST(tmp1
);
1570 wxString tmp2
= text
;
1571 char *str
= WXSTRINGCAST(tmp2
);
1574 bool done
= FALSE
, ret_code
, ok
;
1575 // Below is for vi fans
1576 const char OB
= '{', CB
= '}';
1578 // dot_special means '.' only matches '.'
1579 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1582 while ((*pattern
!= '\0') && (!done
)
1583 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1587 if (*pattern
!= '\0')
1594 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1597 while (*str
!= '\0')
1599 while (*pattern
!= '\0')
1606 if ((*pattern
== '\0') || (*pattern
== ']')) {
1610 if (*pattern
== '\\') {
1612 if (*pattern
== '\0') {
1617 if (*(pattern
+ 1) == '-') {
1620 if (*pattern
== ']') {
1624 if (*pattern
== '\\') {
1626 if (*pattern
== '\0') {
1631 if ((*str
< c
) || (*str
> *pattern
)) {
1635 } else if (*pattern
!= *str
) {
1640 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1641 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1645 if (*pattern
!= '\0') {
1655 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1658 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1659 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1660 if (*pattern
== '\\')
1662 ok
= (*pattern
++ == *cp
++);
1664 if (*pattern
== '\0') {
1670 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1671 if (*++pattern
== '\\') {
1672 if (*++pattern
== CB
)
1677 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1678 if (*++pattern
== '\\') {
1679 if (*++pattern
== CB
|| *pattern
== ',')
1684 if (*pattern
!= '\0')
1689 if (*str
== *pattern
) {
1696 while (*pattern
== '*')
1698 return ((*str
== '\0') && (*pattern
== '\0'));
1704 #pragma warning(default:4706) // assignment within conditional expression