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__)
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.
85 // No, Cygwin doesn't appear to have fnmatch.h after all.
86 #if defined(HAVE_FNMATCH_H)
94 #define _MAXPATHLEN 500
96 extern char *wxBuffer
;
98 extern char gwxMacFileName
[] ;
99 extern char gwxMacFileName2
[] ;
100 extern char gwxMacFileName3
[] ;
103 #if !USE_SHARED_LIBRARIES
104 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
107 void wxPathList::Add (const wxString
& path
)
109 wxStringList::Add ((char *)(const char *)path
);
112 // Add paths e.g. from the PATH environment variable
113 void wxPathList::AddEnvList (const wxString
& envVariable
)
115 static const char PATH_TOKS
[] =
117 " ;"; // Don't seperate with colon in DOS (used for drive)
122 char *val
= getenv (WXSTRINGCAST envVariable
);
125 char *s
= copystring (val
);
126 char *token
= strtok (s
, PATH_TOKS
);
130 Add (copystring (token
));
133 if ((token
= strtok ((char *) NULL
, PATH_TOKS
)) != NULL
)
134 Add (wxString(token
));
141 // Given a full filename (with path), ensure that that file can
142 // be accessed again USING FILENAME ONLY by adding the path
143 // to the list if not already there.
144 void wxPathList::EnsureFileAccessible (const wxString
& path
)
146 wxString
path1(path
);
147 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
150 if (!Member (wxString(path_only
)))
151 Add (wxString(path_only
));
155 bool wxPathList::Member (const wxString
& path
)
157 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
159 wxString
path2((char *) node
->Data ());
161 #if defined(__WINDOWS__) || defined(__VMS__) || defined (__WXMAC__)
163 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
165 // Case sensitive File System
166 path
.CompareTo (path2
) == 0
174 wxString
wxPathList::FindValidPath (const wxString
& file
)
176 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
177 return wxString(wxBuffer
);
179 char buf
[_MAXPATHLEN
];
180 strcpy(buf
, wxBuffer
);
182 char *filename
= (char*) NULL
; /* shut up buggy egcs warning */
183 filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
185 for (wxNode
* node
= First (); node
; node
= node
->Next ())
187 char *path
= (char *) node
->Data ();
188 strcpy (wxBuffer
, path
);
189 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
190 if (ch
!= '\\' && ch
!= '/')
191 strcat (wxBuffer
, "/");
192 strcat (wxBuffer
, filename
);
194 Unix2DosFilename (wxBuffer
);
196 if (wxFileExists (wxBuffer
))
198 return wxString(wxBuffer
); // Found!
202 return wxString(""); // Not found
205 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
207 wxString f
= FindValidPath(file
);
208 if (wxIsAbsolutePath(f
))
213 wxGetWorkingDirectory(buf
, 499);
214 int len
= (int)strlen(buf
);
218 if (lastCh
!= '/' && lastCh
!= '\\')
226 strcat(buf
, (const char *)f
);
227 strcpy(wxBuffer
, buf
);
228 return wxString(wxBuffer
);
233 wxFileExists (const wxString
& filename
)
235 #ifdef __GNUWIN32__ // (fix a B20 bug)
236 if (GetFileAttributes(filename
) == 0xFFFFFFFF)
240 #elif defined(__WXMAC__)
242 strcpy( gwxMacFileName
, filename
) ;
243 wxUnix2MacFilename( gwxMacFileName
) ;
244 if (gwxMacFileName
&& stat ((char *)(const char *)gwxMacFileName
, &stbuf
) == 0)
255 if ((filename
!= "") && stat ((char *)(const char *)filename
, &stbuf
) == 0)
261 /* Vadim's alternative implementation
263 // does the file exist?
264 bool wxFileExists(const char *pszFileName)
267 return !access(pszFileName, 0) &&
268 !stat(pszFileName, &st) &&
269 (st.st_mode & S_IFREG);
274 wxIsAbsolutePath (const wxString
& filename
)
278 if (filename
[0] == '/'
280 || (filename
[0] == '[' && filename
[1] != '.')
284 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
293 * Strip off any extension (dot something) from end of file,
294 * IF one exists. Inserts zero into buffer.
298 void wxStripExtension(char *buffer
)
300 int len
= strlen(buffer
);
304 if (buffer
[i
] == '.')
313 void wxStripExtension(wxString
& buffer
)
315 size_t len
= buffer
.Length();
319 if (buffer
.GetChar(i
) == '.')
321 buffer
= buffer
.Left(i
);
328 // Destructive removal of /./ and /../ stuff
329 char *wxRealPath (char *path
)
332 static const char SEP
= '\\';
333 Unix2DosFilename(path
);
335 static const char SEP
= '/';
337 if (path
[0] && path
[1]) {
338 /* MATTHEW: special case "/./x" */
340 if (path
[2] == SEP
&& path
[1] == '.')
348 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
351 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
352 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
353 && (q
- 1 <= path
|| q
[-1] != SEP
))
362 /* Check that path[2] is NULL! */
363 else if (path
[1] == ':' && !path
[2])
372 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
381 char *wxCopyAbsolutePath(const wxString
& filename
)
384 return (char *) NULL
;
386 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
387 char buf
[_MAXPATHLEN
];
389 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
390 char ch
= buf
[strlen(buf
) - 1];
392 if (ch
!= '\\' && ch
!= '/')
398 strcat(buf
, wxBuffer
);
399 return copystring( wxRealPath(buf
) );
401 return copystring( wxBuffer
);
407 ~user/ => user's home dir
408 If the environment variable a = "foo" and b = "bar" then:
425 /* input name in name, pathname output to buf. */
427 char *wxExpandPath(char *buf
, const char *name
)
429 register char *d
, *s
, *nm
;
430 char lnm
[_MAXPATHLEN
];
433 // Some compilers don't like this line.
434 // const char trimchars[] = "\n \t";
443 const char SEP
= '\\';
445 const char SEP
= '/';
448 if (name
== NULL
|| *name
== '\0')
450 nm
= copystring(name
); // Make a scratch copy
453 /* Skip leading whitespace and cr */
454 while (strchr((char *)trimchars
, *nm
) != NULL
)
456 /* And strip off trailing whitespace and cr */
457 s
= nm
+ (q
= strlen(nm
)) - 1;
458 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
466 q
= nm
[0] == '\\' && nm
[1] == '~';
469 /* Expand inline environment variables */
470 while ((*d
++ = *s
)) {
473 if ((*(d
- 1) = *++s
)) {
481 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
486 register char *start
= d
;
487 register int braces
= (*s
== '{' || *s
== '(');
488 register char *value
;
490 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
495 value
= getenv(braces
? start
+ 1 : start
);
497 for ((d
= start
- 1); (*d
++ = *value
++););
505 /* Expand ~ and ~user */
508 if (nm
[0] == '~' && !q
)
511 if (nm
[1] == SEP
|| nm
[1] == 0)
513 if ((s
= wxGetUserHome("")) != NULL
) {
518 { /* ~user/filename */
521 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
522 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
523 was_sep
= (*s
== SEP
);
524 nnm
= *s
? s
+ 1 : s
;
526 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
527 if (was_sep
) /* replace only if it was there: */
538 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
540 while ('\0' != (*d
++ = *s
++))
543 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
547 while ((*d
++ = *s
++));
549 delete[] nm_tmp
; // clean up alloc
550 /* Now clean up the buffer */
551 return wxRealPath(buf
);
555 /* Contract Paths to be build upon an environment variable
558 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
560 The call wxExpandPath can convert these back!
563 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
565 static char dest
[_MAXPATHLEN
];
568 return (char *) NULL
;
570 strcpy (dest
, WXSTRINGCAST filename
);
572 Unix2DosFilename(dest
);
575 // Handle environment
576 char *val
= (char *) NULL
;
577 char *tcp
= (char *) NULL
;
578 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
579 (tcp
= strstr (dest
, val
)) != NULL
)
581 strcpy (wxBuffer
, tcp
+ strlen (val
));
584 strcpy (tcp
, WXSTRINGCAST envname
);
586 strcat (tcp
, wxBuffer
);
589 // Handle User's home (ignore root homes!)
591 if ((val
= wxGetUserHome (user
)) != NULL
&&
592 (len
= strlen(val
)) > 2 &&
593 strncmp(dest
, val
, len
) == 0)
595 strcpy(wxBuffer
, "~");
597 strcat(wxBuffer
, (const char*) user
);
599 // strcat(wxBuffer, "\\");
601 // strcat(wxBuffer, "/");
603 strcat(wxBuffer
, dest
+ len
);
604 strcpy (dest
, wxBuffer
);
610 // Return just the filename, not the path
612 char *wxFileNameFromPath (char *path
)
618 tcp
= path
+ strlen (path
);
619 while (--tcp
>= path
)
621 if (*tcp
== '/' || *tcp
== '\\'
623 || *tcp
== ':' || *tcp
== ']')
630 if (isalpha (*path
) && *(path
+ 1) == ':')
637 wxString
wxFileNameFromPath (const wxString
& path1
)
642 char *path
= WXSTRINGCAST path1
;
645 tcp
= path
+ strlen (path
);
646 while (--tcp
>= path
)
648 if (*tcp
== '/' || *tcp
== '\\'
650 || *tcp
== ':' || *tcp
== ']')
654 return wxString(tcp
+ 1);
657 if (isalpha (*path
) && *(path
+ 1) == ':')
658 return wxString(path
+ 2);
661 // Yes, this should return the path, not an empty string, otherwise
662 // we get "thing.txt" -> "".
666 // Return just the directory, or NULL if no directory
668 wxPathOnly (char *path
)
672 static char buf
[_MAXPATHLEN
];
677 int l
= strlen(path
);
682 // Search backward for a backward or forward slash
683 while (!done
&& i
> -1)
686 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
701 // Try Drive specifier
702 if (isalpha (buf
[0]) && buf
[1] == ':')
704 // A:junk --> A:. (since A:.\junk Not A:\junk)
712 return (char *) NULL
;
715 // Return just the directory, or NULL if no directory
716 wxString
wxPathOnly (const wxString
& path
)
720 char buf
[_MAXPATHLEN
];
723 strcpy (buf
, WXSTRINGCAST path
);
725 int l
= path
.Length();
730 // Search backward for a backward or forward slash
731 while (!done
&& i
> -1)
734 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
743 return wxString(buf
);
749 // Try Drive specifier
750 if (isalpha (buf
[0]) && buf
[1] == ':')
752 // A:junk --> A:. (since A:.\junk Not A:\junk)
755 return wxString(buf
);
763 // Utility for converting delimiters in DOS filenames to UNIX style
764 // and back again - or we get nasty problems with delimiters.
765 // Also, convert to lower case, since case is significant in UNIX.
769 wxMac2UnixFilename (char *s
)
773 memmove( s
+1 , s
,strlen( s
) + 1) ;
784 *s
= wxToLower (*s
); // Case INDEPENDENT
791 wxUnix2MacFilename (char *s
)
797 // relative path , since it goes on with slash which is translated to a :
798 memmove( s
, s
+1 ,strlen( s
) ) ;
800 else if ( *s
== '/' )
802 // absolute path -> on mac just start with the drive name
803 memmove( s
, s
+1 ,strlen( s
) ) ;
807 wxASSERT_MSG( 1 , "unkown path beginning" ) ;
811 if (*s
== '/' || *s
== '\\')
820 wxDos2UnixFilename (char *s
)
829 *s
= wxToLower (*s
); // Case INDEPENDENT
837 wxUnix2DosFilename (char *s
)
839 wxUnix2DosFilename (char *WXUNUSED(s
))
842 // Yes, I really mean this to happen under DOS only! JACS
854 // Concatenate two files to form third
856 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
858 char *outfile
= wxGetTempFileName("cat");
860 FILE *fp1
= (FILE *) NULL
;
861 FILE *fp2
= (FILE *) NULL
;
862 FILE *fp3
= (FILE *) NULL
;
863 // Open the inputs and outputs
865 strcpy( gwxMacFileName
, file1
) ;
866 wxUnix2MacFilename( gwxMacFileName
) ;
867 strcpy( gwxMacFileName2
, file2
) ;
868 wxUnix2MacFilename( gwxMacFileName2
) ;
869 strcpy( gwxMacFileName3
, outfile
) ;
870 wxUnix2MacFilename( gwxMacFileName3
) ;
872 if ((fp1
= fopen (gwxMacFileName
, "rb")) == NULL
||
873 (fp2
= fopen (gwxMacFileName2
, "rb")) == NULL
||
874 (fp3
= fopen (gwxMacFileName3
, "wb")) == NULL
)
876 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
877 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
878 (fp3
= fopen (outfile
, "wb")) == NULL
)
891 while ((ch
= getc (fp1
)) != EOF
)
892 (void) putc (ch
, fp3
);
895 while ((ch
= getc (fp2
)) != EOF
)
896 (void) putc (ch
, fp3
);
900 bool result
= wxRenameFile(outfile
, file3
);
907 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
914 strcpy( gwxMacFileName
, file1
) ;
915 wxUnix2MacFilename( gwxMacFileName
) ;
916 strcpy( gwxMacFileName2
, file2
) ;
917 wxUnix2MacFilename( gwxMacFileName2
) ;
919 if ((fd1
= fopen (gwxMacFileName
, "rb")) == NULL
)
921 if ((fd2
= fopen (gwxMacFileName2
, "wb")) == NULL
)
923 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
925 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
932 while ((ch
= getc (fd1
)) != EOF
)
933 (void) putc (ch
, fd2
);
941 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
944 strcpy( gwxMacFileName
, file1
) ;
945 wxUnix2MacFilename( gwxMacFileName
) ;
946 strcpy( gwxMacFileName2
, file2
) ;
947 wxUnix2MacFilename( gwxMacFileName2
) ;
949 if (0 == rename (gwxMacFileName
, gwxMacFileName2
))
952 // Normal system call
953 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
957 if (wxCopyFile(file1
, file2
)) {
965 bool wxRemoveFile(const wxString
& file
)
967 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
968 int flag
= remove(WXSTRINGCAST file
);
969 #elif defined( __WXMAC__ )
970 strcpy( gwxMacFileName
, file
) ;
971 wxUnix2MacFilename( gwxMacFileName
) ;
972 int flag
= unlink(gwxMacFileName
);
974 int flag
= unlink(WXSTRINGCAST file
);
979 bool wxMkdir(const wxString
& dir
)
981 #if defined(__WXSTUBS__)
983 #elif defined(__VMS__)
985 #elif defined( __WXMAC__ )
986 strcpy( gwxMacFileName
, dir
) ;
987 wxUnix2MacFilename( gwxMacFileName
) ;
988 return (mkdir(gwxMacFileName
, 0 ) == 0);
989 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
990 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
992 return (mkdir(WXSTRINGCAST dir
) == 0);
996 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
1000 #elif defined( __WXMAC__ )
1001 strcpy( gwxMacFileName
, dir
) ;
1002 wxUnix2MacFilename( gwxMacFileName
) ;
1003 return (rmdir(WXSTRINGCAST gwxMacFileName
) == 0);
1007 return FALSE
; // What to do?
1009 return (rmdir(WXSTRINGCAST dir
) == 0);
1016 bool wxDirExists(const wxString
& dir
)
1020 #elif !defined(__WXMSW__)
1022 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
1025 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
1026 #if defined(__WIN32__)
1027 WIN32_FIND_DATA fileInfo
;
1030 struct ffblk fileInfo
;
1032 struct find_t fileInfo
;
1036 #if defined(__WIN32__)
1037 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
1039 if (h
==INVALID_HANDLE_VALUE
)
1043 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
1046 // In Borland findfirst has a different argument
1047 // ordering from _dos_findfirst. But _dos_findfirst
1048 // _should_ be ok in both MS and Borland... why not?
1050 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
1052 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
1061 // does the path exists? (may have or not '/' or '\\' at the end)
1062 bool wxPathExists(const char *pszPathName
)
1064 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
1065 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
1066 wxString
strPath(pszPathName
);
1067 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
1068 strPath
.Last() = '\0';
1076 return stat((char*) (const char*) strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
1079 // Get a temporary filename, opening and closing the file.
1080 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
1086 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
1089 char tmpPath
[MAX_PATH
];
1090 ::GetTempPath(MAX_PATH
, tmpPath
);
1091 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
1093 if (buf
) strcpy(buf
, tmp
);
1094 else buf
= copystring(tmp
);
1098 static short last_temp
= 0; // cache last to speed things a bit
1099 // At most 1000 temp files to a process! We use a ring count.
1100 char tmp
[100]; // FIXME static buffer
1102 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
1104 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
1105 if (!wxFileExists( tmp
))
1107 // Touch the file to create it (reserve name)
1108 FILE *fd
= fopen (tmp
, "w");
1115 buf
= copystring( tmp
);
1119 wxLogError( _("wxWindows: error finding temporary file name.\n") );
1120 if (buf
) buf
[0] = 0;
1121 return (char *) NULL
;
1125 // Get first file name matching given wild card.
1129 // Get first file name matching given wild card.
1130 // Flags are reserved for future use.
1133 static DIR *wxDirStream
= (DIR *) NULL
;
1134 static char *wxFileSpec
= (char *) NULL
;
1135 static int wxFindFileFlags
= 0;
1138 char *wxFindFirstFile(const char *spec
, int flags
)
1142 closedir(wxDirStream
); // edz 941103: better housekeping
1144 wxFindFileFlags
= flags
;
1147 delete[] wxFileSpec
;
1148 wxFileSpec
= copystring(spec
);
1150 // Find path only so we can concatenate
1151 // found file onto path
1152 char *p
= wxPathOnly(wxFileSpec
);
1154 /* MATTHEW: special case: path is really "/" */
1155 if (p
&& !*p
&& *wxFileSpec
== '/')
1157 /* MATTHEW: p is NULL => Local directory */
1161 if ((wxDirStream
=opendir(p
))==NULL
)
1162 return (char *) NULL
;
1164 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1165 return wxFindNextFile();
1168 return (char *) NULL
;
1171 char *wxFindNextFile(void)
1174 static char buf
[400]; // FIXME static buffer
1176 /* MATTHEW: [2] Don't crash if we read too many times */
1178 return (char *) NULL
;
1180 // Find path only so we can concatenate
1181 // found file onto path
1182 char *p
= wxPathOnly(wxFileSpec
);
1183 char *n
= wxFileNameFromPath(wxFileSpec
);
1185 /* MATTHEW: special case: path is really "/" */
1186 if (p
&& !*p
&& *wxFileSpec
== '/')
1190 struct dirent
*nextDir
;
1191 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1194 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1195 directories when flags & wxDIR */
1196 if (wxMatchWild(n
, nextDir
->d_name
)) {
1202 if (strcmp(p
, "/") != 0)
1205 strcat(buf
, nextDir
->d_name
);
1207 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1208 (strcmp(nextDir
->d_name
, "..") == 0)) {
1209 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1213 isdir
= wxDirExists(buf
);
1215 if (!wxFindFileFlags
1216 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1217 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1221 closedir(wxDirStream
);
1222 wxDirStream
= (DIR *) NULL
;
1226 return (char *) NULL
;
1229 #elif defined(__WXMSW__)
1232 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1233 WIN32_FIND_DATA wxFileStruc
;
1236 static struct ffblk wxFileStruc
;
1238 static struct _find_t wxFileStruc
;
1241 static wxString wxFileSpec
= "";
1242 static int wxFindFileFlags
;
1244 char *wxFindFirstFile(const char *spec
, int flags
)
1247 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1249 // Find path only so we can concatenate
1250 // found file onto path
1251 wxString
path1(wxFileSpec
);
1252 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1253 if (p
&& (strlen(p
) > 0))
1254 strcpy(wxBuffer
, p
);
1259 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1260 FindClose(wxFileStrucHandle
);
1262 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1264 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1267 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1269 if (isdir
&& !(flags
& wxDIR
))
1270 return wxFindNextFile();
1271 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1272 return wxFindNextFile();
1274 if (wxBuffer
[0] != 0)
1275 strcat(wxBuffer
, "\\");
1276 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1280 int flag
= _A_NORMAL
;
1281 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1285 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1287 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1290 /* MATTHEW: [5] Check directory flag */
1294 attrib
= wxFileStruc
.ff_attrib
;
1296 attrib
= wxFileStruc
.attrib
;
1299 if (attrib
& _A_SUBDIR
) {
1300 if (!(wxFindFileFlags
& wxDIR
))
1301 return wxFindNextFile();
1302 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1303 return wxFindNextFile();
1305 if (wxBuffer
[0] != 0)
1306 strcat(wxBuffer
, "\\");
1309 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1311 strcat(wxBuffer
, wxFileStruc
.name
);
1320 char *wxFindNextFile(void)
1322 // Find path only so we can concatenate
1323 // found file onto path
1324 wxString
p2(wxFileSpec
);
1325 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1326 if (p
&& (strlen(p
) > 0))
1327 strcpy(wxBuffer
, p
);
1334 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1337 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1339 FindClose(wxFileStrucHandle
);
1340 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1344 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1346 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1348 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1351 if (wxBuffer
[0] != 0)
1352 strcat(wxBuffer
, "\\");
1353 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1358 if (findnext(&wxFileStruc
) == 0)
1360 if (_dos_findnext(&wxFileStruc
) == 0)
1363 /* MATTHEW: [5] Check directory flag */
1367 attrib
= wxFileStruc
.ff_attrib
;
1369 attrib
= wxFileStruc
.attrib
;
1372 if (attrib
& _A_SUBDIR
) {
1373 if (!(wxFindFileFlags
& wxDIR
))
1375 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1379 if (wxBuffer
[0] != 0)
1380 strcat(wxBuffer
, "\\");
1382 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1384 strcat(wxBuffer
, wxFileStruc
.name
);
1396 // Get current working directory.
1397 // If buf is NULL, allocates space using new, else
1399 char *wxGetWorkingDirectory(char *buf
, int sz
)
1402 buf
= new char[sz
+1];
1404 if (_getcwd(buf
, sz
) == NULL
) {
1406 if (getcwd(buf
, sz
) == NULL
) {
1414 bool wxSetWorkingDirectory(const wxString
& d
)
1416 #if defined( __UNIX__ ) || defined( __WXMAC__ )
1417 return (chdir(d
) == 0);
1418 #elif defined(__WINDOWS__)
1421 return (bool)(SetCurrentDirectory(d
) != 0);
1423 // Must change drive, too.
1424 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1427 char firstChar
= d
[0];
1431 firstChar
= firstChar
- 32;
1433 // To a drive number
1434 unsigned int driveNo
= firstChar
- 64;
1437 unsigned int noDrives
;
1438 _dos_setdrive(driveNo
, &noDrives
);
1441 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1449 bool wxEndsWithPathSeparator(const char *pszFileName
)
1451 size_t len
= Strlen(pszFileName
);
1455 return wxIsPathSeparator(pszFileName
[len
- 1]);
1458 // find a file in a list of directories, returns false if not found
1459 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1461 // we assume that it's not empty
1462 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1463 _("empty file name in wxFindFileInPath"));
1465 // skip path separator in the beginning of the file name if present
1466 if ( wxIsPathSeparator(*pszFile
) )
1469 // copy the path (strtok will modify it)
1470 char *szPath
= new char[strlen(pszPath
) + 1];
1471 strcpy(szPath
, pszPath
);
1475 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1476 // search for the file in this directory
1478 if ( !wxEndsWithPathSeparator(pc
) )
1479 strFile
+= FILE_SEP_PATH
;
1482 if ( FileExists(strFile
) ) {
1490 return pc
!= NULL
; // if true => we breaked from the loop
1493 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1498 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1500 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1501 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1502 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1504 // take the last of the two
1505 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1506 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1507 if ( nPosDos
> nPosUnix
)
1509 // size_t nLen = Strlen(pszFileName);
1512 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1514 size_t nPosDot
= pDot
- pszFileName
;
1516 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1518 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1522 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1528 //------------------------------------------------------------------------
1529 // wild character routines
1530 //------------------------------------------------------------------------
1532 bool wxIsWild( const wxString
& pattern
)
1534 wxString tmp
= pattern
;
1535 char *pat
= WXSTRINGCAST(tmp
);
1538 case '?': case '*': case '[': case '{':
1548 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1550 #if defined(HAVE_FNMATCH_H)
1553 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1555 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1559 // #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1562 * WARNING: this code is broken!
1565 wxString tmp1
= pat
;
1566 char *pattern
= WXSTRINGCAST(tmp1
);
1567 wxString tmp2
= text
;
1568 char *str
= WXSTRINGCAST(tmp2
);
1571 bool done
= FALSE
, ret_code
, ok
;
1572 // Below is for vi fans
1573 const char OB
= '{', CB
= '}';
1575 // dot_special means '.' only matches '.'
1576 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1579 while ((*pattern
!= '\0') && (!done
)
1580 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1584 if (*pattern
!= '\0')
1591 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1594 while (*str
!= '\0')
1596 while (*pattern
!= '\0')
1603 if ((*pattern
== '\0') || (*pattern
== ']')) {
1607 if (*pattern
== '\\') {
1609 if (*pattern
== '\0') {
1614 if (*(pattern
+ 1) == '-') {
1617 if (*pattern
== ']') {
1621 if (*pattern
== '\\') {
1623 if (*pattern
== '\0') {
1628 if ((*str
< c
) || (*str
> *pattern
)) {
1632 } else if (*pattern
!= *str
) {
1637 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1638 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1642 if (*pattern
!= '\0') {
1652 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1655 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1656 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1657 if (*pattern
== '\\')
1659 ok
= (*pattern
++ == *cp
++);
1661 if (*pattern
== '\0') {
1667 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1668 if (*++pattern
== '\\') {
1669 if (*++pattern
== CB
)
1674 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1675 if (*++pattern
== '\\') {
1676 if (*++pattern
== CB
|| *pattern
== ',')
1681 if (*pattern
!= '\0')
1686 if (*str
== *pattern
) {
1693 while (*pattern
== '*')
1695 return ((*str
== '\0') && (*pattern
== '\0'));
1701 #pragma warning(default:4706) // assignment within conditional expression