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
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
179 for (wxNode
* node
= First (); node
; node
= node
->Next ())
181 char *path
= (char *) node
->Data ();
182 strcpy (wxBuffer
, path
);
183 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
184 if (ch
!= '\\' && ch
!= '/')
185 strcat (wxBuffer
, "/");
186 strcat (wxBuffer
, filename
);
188 Unix2DosFilename (wxBuffer
);
190 if (wxFileExists (wxBuffer
))
192 return wxString(wxBuffer
); // Found!
196 return wxString(""); // Not found
199 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
201 wxString f
= FindValidPath(file
);
202 if (wxIsAbsolutePath(f
))
207 wxGetWorkingDirectory(buf
, 499);
208 int len
= (int)strlen(buf
);
212 if (lastCh
!= '/' && lastCh
!= '\\')
220 strcat(buf
, (const char *)f
);
221 strcpy(wxBuffer
, buf
);
222 return wxString(wxBuffer
);
227 wxFileExists (const wxString
& filename
)
229 #ifdef __GNUWIN32__ // (fix a B20 bug)
230 if (GetFileAttributes(filename
) == 0xFFFFFFFF)
234 #elif defined(__WXMAC__)
236 strcpy( gwxMacFileName
, filename
) ;
237 wxUnix2MacFilename( gwxMacFileName
) ;
238 if (gwxMacFileName
&& stat ((char *)(const char *)gwxMacFileName
, &stbuf
) == 0)
244 if (filename
&& stat ((char *)(const char *)filename
, &stbuf
) == 0)
250 /* Vadim's alternative implementation
252 // does the file exist?
253 bool wxFileExists(const char *pszFileName)
256 return !access(pszFileName, 0) &&
257 !stat(pszFileName, &st) &&
258 (st.st_mode & S_IFREG);
263 wxIsAbsolutePath (const wxString
& filename
)
267 if (filename
[0] == '/'
269 || (filename
[0] == '[' && filename
[1] != '.')
273 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
282 * Strip off any extension (dot something) from end of file,
283 * IF one exists. Inserts zero into buffer.
287 void wxStripExtension(char *buffer
)
289 int len
= strlen(buffer
);
293 if (buffer
[i
] == '.')
302 void wxStripExtension(wxString
& buffer
)
304 size_t len
= buffer
.Length();
308 if (buffer
.GetChar(i
) == '.')
310 buffer
= buffer
.Left(i
);
317 // Destructive removal of /./ and /../ stuff
318 char *wxRealPath (char *path
)
321 static const char SEP
= '\\';
322 Unix2DosFilename(path
);
324 static const char SEP
= '/';
326 if (path
[0] && path
[1]) {
327 /* MATTHEW: special case "/./x" */
329 if (path
[2] == SEP
&& path
[1] == '.')
337 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
340 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
341 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
342 && (q
- 1 <= path
|| q
[-1] != SEP
))
351 /* Check that path[2] is NULL! */
352 else if (path
[1] == ':' && !path
[2])
361 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
370 char *wxCopyAbsolutePath(const wxString
& filename
)
373 return (char *) NULL
;
375 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
376 char buf
[_MAXPATHLEN
];
378 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
379 char ch
= buf
[strlen(buf
) - 1];
381 if (ch
!= '\\' && ch
!= '/')
387 strcat(buf
, wxBuffer
);
388 return copystring( wxRealPath(buf
) );
390 return copystring( wxBuffer
);
396 ~user/ => user's home dir
397 If the environment variable a = "foo" and b = "bar" then:
414 /* input name in name, pathname output to buf. */
416 char *wxExpandPath(char *buf
, const char *name
)
418 register char *d
, *s
, *nm
;
419 char lnm
[_MAXPATHLEN
];
422 // Some compilers don't like this line.
423 // const char trimchars[] = "\n \t";
432 const char SEP
= '\\';
434 const char SEP
= '/';
437 if (name
== NULL
|| *name
== '\0')
439 nm
= copystring(name
); // Make a scratch copy
442 /* Skip leading whitespace and cr */
443 while (strchr((char *)trimchars
, *nm
) != NULL
)
445 /* And strip off trailing whitespace and cr */
446 s
= nm
+ (q
= strlen(nm
)) - 1;
447 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
455 q
= nm
[0] == '\\' && nm
[1] == '~';
458 /* Expand inline environment variables */
459 while ((*d
++ = *s
)) {
462 if ((*(d
- 1) = *++s
)) {
470 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
475 register char *start
= d
;
476 register int braces
= (*s
== '{' || *s
== '(');
477 register char *value
;
479 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
484 value
= getenv(braces
? start
+ 1 : start
);
486 for ((d
= start
- 1); (*d
++ = *value
++););
494 /* Expand ~ and ~user */
497 if (nm
[0] == '~' && !q
)
500 if (nm
[1] == SEP
|| nm
[1] == 0)
502 if ((s
= wxGetUserHome("")) != NULL
) {
507 { /* ~user/filename */
510 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
511 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
512 was_sep
= (*s
== SEP
);
513 nnm
= *s
? s
+ 1 : s
;
515 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
516 if (was_sep
) /* replace only if it was there: */
527 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
529 while ('\0' != (*d
++ = *s
++))
532 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
536 while ((*d
++ = *s
++));
538 delete[] nm_tmp
; // clean up alloc
539 /* Now clean up the buffer */
540 return wxRealPath(buf
);
544 /* Contract Paths to be build upon an environment variable
547 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
549 The call wxExpandPath can convert these back!
552 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
554 static char dest
[_MAXPATHLEN
];
557 return (char *) NULL
;
559 strcpy (dest
, WXSTRINGCAST filename
);
561 Unix2DosFilename(dest
);
564 // Handle environment
565 char *val
= (char *) NULL
;
566 char *tcp
= (char *) NULL
;
567 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
568 (tcp
= strstr (dest
, val
)) != NULL
)
570 strcpy (wxBuffer
, tcp
+ strlen (val
));
573 strcpy (tcp
, WXSTRINGCAST envname
);
575 strcat (tcp
, wxBuffer
);
578 // Handle User's home (ignore root homes!)
580 if ((val
= wxGetUserHome (user
)) != NULL
&&
581 (len
= strlen(val
)) > 2 &&
582 strncmp(dest
, val
, len
) == 0)
584 strcpy(wxBuffer
, "~");
586 strcat(wxBuffer
, user
);
588 // strcat(wxBuffer, "\\");
590 // strcat(wxBuffer, "/");
592 strcat(wxBuffer
, dest
+ len
);
593 strcpy (dest
, wxBuffer
);
599 // Return just the filename, not the path
601 char *wxFileNameFromPath (char *path
)
607 tcp
= path
+ strlen (path
);
608 while (--tcp
>= path
)
610 if (*tcp
== '/' || *tcp
== '\\'
612 || *tcp
== ':' || *tcp
== ']')
619 if (isalpha (*path
) && *(path
+ 1) == ':')
626 wxString
wxFileNameFromPath (const wxString
& path1
)
631 char *path
= WXSTRINGCAST path1
;
634 tcp
= path
+ strlen (path
);
635 while (--tcp
>= path
)
637 if (*tcp
== '/' || *tcp
== '\\'
639 || *tcp
== ':' || *tcp
== ']')
643 return wxString(tcp
+ 1);
646 if (isalpha (*path
) && *(path
+ 1) == ':')
647 return wxString(path
+ 2);
653 // Return just the directory, or NULL if no directory
655 wxPathOnly (char *path
)
659 static char buf
[_MAXPATHLEN
];
664 int l
= strlen(path
);
669 // Search backward for a backward or forward slash
670 while (!done
&& i
> -1)
673 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
688 // Try Drive specifier
689 if (isalpha (buf
[0]) && buf
[1] == ':')
691 // A:junk --> A:. (since A:.\junk Not A:\junk)
699 return (char *) NULL
;
702 // Return just the directory, or NULL if no directory
703 wxString
wxPathOnly (const wxString
& path
)
707 char buf
[_MAXPATHLEN
];
710 strcpy (buf
, WXSTRINGCAST path
);
712 int l
= path
.Length();
717 // Search backward for a backward or forward slash
718 while (!done
&& i
> -1)
721 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
730 return wxString(buf
);
736 // Try Drive specifier
737 if (isalpha (buf
[0]) && buf
[1] == ':')
739 // A:junk --> A:. (since A:.\junk Not A:\junk)
742 return wxString(buf
);
750 // Utility for converting delimiters in DOS filenames to UNIX style
751 // and back again - or we get nasty problems with delimiters.
752 // Also, convert to lower case, since case is significant in UNIX.
756 wxMac2UnixFilename (char *s
)
760 memmove( s
+1 , s
,strlen( s
) + 1) ;
771 *s
= wxToLower (*s
); // Case INDEPENDENT
778 wxUnix2MacFilename (char *s
)
784 // relative path , since it goes on with slash which is translated to a :
785 memmove( s
, s
+1 ,strlen( s
) ) ;
787 else if ( *s
== '/' )
789 // absolute path -> on mac just start with the drive name
790 memmove( s
, s
+1 ,strlen( s
) ) ;
794 wxASSERT_MSG( 1 , "unkown path beginning" ) ;
798 if (*s
== '/' || *s
== '\\')
807 wxDos2UnixFilename (char *s
)
816 *s
= wxToLower (*s
); // Case INDEPENDENT
824 wxUnix2DosFilename (char *s
)
826 wxUnix2DosFilename (char *WXUNUSED(s
))
829 // Yes, I really mean this to happen under DOS only! JACS
841 // Concatenate two files to form third
843 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
845 char *outfile
= wxGetTempFileName("cat");
847 FILE *fp1
= (FILE *) NULL
;
848 FILE *fp2
= (FILE *) NULL
;
849 FILE *fp3
= (FILE *) NULL
;
850 // Open the inputs and outputs
852 strcpy( gwxMacFileName
, file1
) ;
853 wxUnix2MacFilename( gwxMacFileName
) ;
854 strcpy( gwxMacFileName2
, file2
) ;
855 wxUnix2MacFilename( gwxMacFileName2
) ;
856 strcpy( gwxMacFileName3
, outfile
) ;
857 wxUnix2MacFilename( gwxMacFileName3
) ;
859 if ((fp1
= fopen (gwxMacFileName
, "rb")) == NULL
||
860 (fp2
= fopen (gwxMacFileName2
, "rb")) == NULL
||
861 (fp3
= fopen (gwxMacFileName3
, "wb")) == NULL
)
863 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
864 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
865 (fp3
= fopen (outfile
, "wb")) == NULL
)
878 while ((ch
= getc (fp1
)) != EOF
)
879 (void) putc (ch
, fp3
);
882 while ((ch
= getc (fp2
)) != EOF
)
883 (void) putc (ch
, fp3
);
887 bool result
= wxRenameFile(outfile
, file3
);
894 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
901 strcpy( gwxMacFileName
, file1
) ;
902 wxUnix2MacFilename( gwxMacFileName
) ;
903 strcpy( gwxMacFileName2
, file2
) ;
904 wxUnix2MacFilename( gwxMacFileName2
) ;
906 if ((fd1
= fopen (gwxMacFileName
, "rb")) == NULL
)
908 if ((fd2
= fopen (gwxMacFileName2
, "wb")) == NULL
)
910 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
912 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
919 while ((ch
= getc (fd1
)) != EOF
)
920 (void) putc (ch
, fd2
);
928 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
931 strcpy( gwxMacFileName
, file1
) ;
932 wxUnix2MacFilename( gwxMacFileName
) ;
933 strcpy( gwxMacFileName2
, file2
) ;
934 wxUnix2MacFilename( gwxMacFileName2
) ;
936 if (0 == rename (gwxMacFileName
, gwxMacFileName2
))
939 // Normal system call
940 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
944 if (wxCopyFile(file1
, file2
)) {
952 bool wxRemoveFile(const wxString
& file
)
954 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
955 int flag
= remove(WXSTRINGCAST file
);
956 #elif defined( __WXMAC__ )
957 strcpy( gwxMacFileName
, file
) ;
958 wxUnix2MacFilename( gwxMacFileName
) ;
959 int flag
= unlink(gwxMacFileName
);
961 int flag
= unlink(WXSTRINGCAST file
);
966 bool wxMkdir(const wxString
& dir
)
968 #if defined(__WXSTUBS__)
970 #elif defined(__VMS__)
972 #elif defined( __WXMAC__ )
973 strcpy( gwxMacFileName
, dir
) ;
974 wxUnix2MacFilename( gwxMacFileName
) ;
975 return (mkdir(gwxMacFileName
, 0 ) == 0);
976 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
977 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
979 return (mkdir(WXSTRINGCAST dir
) == 0);
983 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
987 #elif defined( __WXMAC__ )
988 strcpy( gwxMacFileName
, dir
) ;
989 wxUnix2MacFilename( gwxMacFileName
) ;
990 return (rmdir(WXSTRINGCAST gwxMacFileName
) == 0);
992 return (rmdir(WXSTRINGCAST dir
) == 0);
997 bool wxDirExists(const wxString
& dir
)
1001 #elif !defined(__WXMSW__)
1003 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
1006 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
1007 #if defined(__WIN32__)
1008 WIN32_FIND_DATA fileInfo
;
1011 struct ffblk fileInfo
;
1013 struct find_t fileInfo
;
1017 #if defined(__WIN32__)
1018 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
1020 if (h
==INVALID_HANDLE_VALUE
)
1024 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
1027 // In Borland findfirst has a different argument
1028 // ordering from _dos_findfirst. But _dos_findfirst
1029 // _should_ be ok in both MS and Borland... why not?
1031 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
1033 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
1042 // does the path exists? (may have or not '/' or '\\' at the end)
1043 bool wxPathExists(const char *pszPathName
)
1045 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
1046 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
1047 wxString
strPath(pszPathName
);
1048 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
1049 strPath
.Last() = '\0';
1052 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
1055 // Get a temporary filename, opening and closing the file.
1056 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
1062 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
1065 char tmpPath
[MAX_PATH
];
1066 ::GetTempPath(MAX_PATH
, tmpPath
);
1067 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
1069 if (buf
) strcpy(buf
, tmp
);
1070 else buf
= copystring(tmp
);
1074 static short last_temp
= 0; // cache last to speed things a bit
1075 // At most 1000 temp files to a process! We use a ring count.
1076 char tmp
[100]; // FIXME static buffer
1078 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
1080 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
1081 if (!wxFileExists( tmp
))
1083 // Touch the file to create it (reserve name)
1084 FILE *fd
= fopen (tmp
, "w");
1091 buf
= copystring( tmp
);
1095 wxLogError( _("wxWindows: error finding temporary file name.\n") );
1096 if (buf
) buf
[0] = 0;
1097 return (char *) NULL
;
1101 // Get first file name matching given wild card.
1105 // Get first file name matching given wild card.
1106 // Flags are reserved for future use.
1109 static DIR *wxDirStream
= (DIR *) NULL
;
1110 static char *wxFileSpec
= (char *) NULL
;
1111 static int wxFindFileFlags
= 0;
1114 char *wxFindFirstFile(const char *spec
, int flags
)
1118 closedir(wxDirStream
); // edz 941103: better housekeping
1120 wxFindFileFlags
= flags
;
1123 delete[] wxFileSpec
;
1124 wxFileSpec
= copystring(spec
);
1126 // Find path only so we can concatenate
1127 // found file onto path
1128 char *p
= wxPathOnly(wxFileSpec
);
1130 /* MATTHEW: special case: path is really "/" */
1131 if (p
&& !*p
&& *wxFileSpec
== '/')
1133 /* MATTHEW: p is NULL => Local directory */
1137 if ((wxDirStream
=opendir(p
))==NULL
)
1138 return (char *) NULL
;
1140 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1141 return wxFindNextFile();
1144 return (char *) NULL
;
1147 char *wxFindNextFile(void)
1150 static char buf
[400]; // FIXME static buffer
1152 /* MATTHEW: [2] Don't crash if we read too many times */
1154 return (char *) NULL
;
1156 // Find path only so we can concatenate
1157 // found file onto path
1158 char *p
= wxPathOnly(wxFileSpec
);
1159 char *n
= wxFileNameFromPath(wxFileSpec
);
1161 /* MATTHEW: special case: path is really "/" */
1162 if (p
&& !*p
&& *wxFileSpec
== '/')
1166 struct dirent
*nextDir
;
1167 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1170 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1171 directories when flags & wxDIR */
1172 if (wxMatchWild(n
, nextDir
->d_name
)) {
1178 if (strcmp(p
, "/") != 0)
1181 strcat(buf
, nextDir
->d_name
);
1183 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1184 (strcmp(nextDir
->d_name
, "..") == 0)) {
1185 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1189 isdir
= wxDirExists(buf
);
1191 if (!wxFindFileFlags
1192 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1193 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1197 closedir(wxDirStream
);
1198 wxDirStream
= (DIR *) NULL
;
1202 return (char *) NULL
;
1205 #elif defined(__WXMSW__)
1208 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1209 WIN32_FIND_DATA wxFileStruc
;
1212 static struct ffblk wxFileStruc
;
1214 static struct _find_t wxFileStruc
;
1217 static wxString wxFileSpec
= "";
1218 static int wxFindFileFlags
;
1220 char *wxFindFirstFile(const char *spec
, int flags
)
1223 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1225 // Find path only so we can concatenate
1226 // found file onto path
1227 wxString
path1(wxFileSpec
);
1228 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1229 if (p
&& (strlen(p
) > 0))
1230 strcpy(wxBuffer
, p
);
1235 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1236 FindClose(wxFileStrucHandle
);
1238 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1240 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1243 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1245 if (isdir
&& !(flags
& wxDIR
))
1246 return wxFindNextFile();
1247 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1248 return wxFindNextFile();
1250 if (wxBuffer
[0] != 0)
1251 strcat(wxBuffer
, "\\");
1252 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1256 int flag
= _A_NORMAL
;
1257 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1261 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1263 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1266 /* MATTHEW: [5] Check directory flag */
1270 attrib
= wxFileStruc
.ff_attrib
;
1272 attrib
= wxFileStruc
.attrib
;
1275 if (attrib
& _A_SUBDIR
) {
1276 if (!(wxFindFileFlags
& wxDIR
))
1277 return wxFindNextFile();
1278 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1279 return wxFindNextFile();
1281 if (wxBuffer
[0] != 0)
1282 strcat(wxBuffer
, "\\");
1285 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1287 strcat(wxBuffer
, wxFileStruc
.name
);
1296 char *wxFindNextFile(void)
1298 // Find path only so we can concatenate
1299 // found file onto path
1300 wxString
p2(wxFileSpec
);
1301 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1302 if (p
&& (strlen(p
) > 0))
1303 strcpy(wxBuffer
, p
);
1310 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1313 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1315 FindClose(wxFileStrucHandle
);
1316 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1320 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1322 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1324 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1327 if (wxBuffer
[0] != 0)
1328 strcat(wxBuffer
, "\\");
1329 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1334 if (findnext(&wxFileStruc
) == 0)
1336 if (_dos_findnext(&wxFileStruc
) == 0)
1339 /* MATTHEW: [5] Check directory flag */
1343 attrib
= wxFileStruc
.ff_attrib
;
1345 attrib
= wxFileStruc
.attrib
;
1348 if (attrib
& _A_SUBDIR
) {
1349 if (!(wxFindFileFlags
& wxDIR
))
1351 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1355 if (wxBuffer
[0] != 0)
1356 strcat(wxBuffer
, "\\");
1358 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1360 strcat(wxBuffer
, wxFileStruc
.name
);
1372 // Get current working directory.
1373 // If buf is NULL, allocates space using new, else
1375 char *wxGetWorkingDirectory(char *buf
, int sz
)
1378 buf
= new char[sz
+1];
1380 if (_getcwd(buf
, sz
) == NULL
) {
1382 if (getcwd(buf
, sz
) == NULL
) {
1390 bool wxSetWorkingDirectory(const wxString
& d
)
1392 #if defined( __UNIX__ ) || defined( __WXMAC__ )
1393 return (chdir(d
) == 0);
1394 #elif defined(__WINDOWS__)
1397 return (bool)(SetCurrentDirectory(d
) != 0);
1399 // Must change drive, too.
1400 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1403 char firstChar
= d
[0];
1407 firstChar
= firstChar
- 32;
1409 // To a drive number
1410 unsigned int driveNo
= firstChar
- 64;
1413 unsigned int noDrives
;
1414 _dos_setdrive(driveNo
, &noDrives
);
1417 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1425 bool wxEndsWithPathSeparator(const char *pszFileName
)
1427 size_t len
= Strlen(pszFileName
);
1431 return wxIsPathSeparator(pszFileName
[len
- 1]);
1434 // find a file in a list of directories, returns false if not found
1435 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1437 // we assume that it's not empty
1438 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1439 _("empty file name in wxFindFileInPath"));
1441 // skip path separator in the beginning of the file name if present
1442 if ( wxIsPathSeparator(*pszFile
) )
1445 // copy the path (strtok will modify it)
1446 char *szPath
= new char[strlen(pszPath
) + 1];
1447 strcpy(szPath
, pszPath
);
1451 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1452 // search for the file in this directory
1454 if ( !wxEndsWithPathSeparator(pc
) )
1455 strFile
+= FILE_SEP_PATH
;
1458 if ( FileExists(strFile
) ) {
1466 return pc
!= NULL
; // if true => we breaked from the loop
1469 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1474 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1476 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1477 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1478 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1480 // take the last of the two
1481 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1482 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1483 if ( nPosDos
> nPosUnix
)
1485 // size_t nLen = Strlen(pszFileName);
1488 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1490 size_t nPosDot
= pDot
- pszFileName
;
1492 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1494 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1498 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1504 //------------------------------------------------------------------------
1505 // wild character routines
1506 //------------------------------------------------------------------------
1508 bool wxIsWild( const wxString
& pattern
)
1510 wxString tmp
= pattern
;
1511 char *pat
= WXSTRINGCAST(tmp
);
1514 case '?': case '*': case '[': case '{':
1524 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1526 #if defined(HAVE_FNMATCH_H)
1529 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1531 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1535 // #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1538 * WARNING: this code is broken!
1541 wxString tmp1
= pat
;
1542 char *pattern
= WXSTRINGCAST(tmp1
);
1543 wxString tmp2
= text
;
1544 char *str
= WXSTRINGCAST(tmp2
);
1547 bool done
= FALSE
, ret_code
, ok
;
1548 // Below is for vi fans
1549 const char OB
= '{', CB
= '}';
1551 // dot_special means '.' only matches '.'
1552 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1555 while ((*pattern
!= '\0') && (!done
)
1556 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1560 if (*pattern
!= '\0')
1567 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1570 while (*str
!= '\0')
1572 while (*pattern
!= '\0')
1579 if ((*pattern
== '\0') || (*pattern
== ']')) {
1583 if (*pattern
== '\\') {
1585 if (*pattern
== '\0') {
1590 if (*(pattern
+ 1) == '-') {
1593 if (*pattern
== ']') {
1597 if (*pattern
== '\\') {
1599 if (*pattern
== '\0') {
1604 if ((*str
< c
) || (*str
> *pattern
)) {
1608 } else if (*pattern
!= *str
) {
1613 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1614 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1618 if (*pattern
!= '\0') {
1628 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1631 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1632 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1633 if (*pattern
== '\\')
1635 ok
= (*pattern
++ == *cp
++);
1637 if (*pattern
== '\0') {
1643 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1644 if (*++pattern
== '\\') {
1645 if (*++pattern
== CB
)
1650 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1651 if (*++pattern
== '\\') {
1652 if (*++pattern
== CB
|| *pattern
== ',')
1657 if (*pattern
!= '\0')
1662 if (*str
== *pattern
) {
1669 while (*pattern
== '*')
1671 return ((*str
== '\0') && (*pattern
== '\0'));
1677 #pragma warning(default:4706) // assignment within conditional expression