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))
49 #include <sys/types.h>
62 #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) && !defined(__SALFORDC__)
70 #include <sys/unistd.h>
73 #define stricmp strcasecmp
76 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
77 // this (3.1 I believe) and how to test for it.
78 // If this works for Borland 4.0 as well, then no worries.
90 // No, Cygwin doesn't appear to have fnmatch.h after all.
91 #if defined(HAVE_FNMATCH_H)
99 #define _MAXPATHLEN 500
101 extern char *wxBuffer
;
103 extern char gwxMacFileName
[] ;
104 extern char gwxMacFileName2
[] ;
105 extern char gwxMacFileName3
[] ;
108 #if !USE_SHARED_LIBRARIES
109 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
112 void wxPathList::Add (const wxString
& path
)
114 wxStringList::Add ((char *)(const char *)path
);
117 // Add paths e.g. from the PATH environment variable
118 void wxPathList::AddEnvList (const wxString
& envVariable
)
120 static const char PATH_TOKS
[] =
122 " ;"; // Don't seperate with colon in DOS (used for drive)
127 char *val
= getenv (WXSTRINGCAST envVariable
);
130 char *s
= copystring (val
);
131 char *token
= strtok (s
, PATH_TOKS
);
135 Add (copystring (token
));
138 if ((token
= strtok ((char *) NULL
, PATH_TOKS
)) != NULL
)
139 Add (wxString(token
));
146 // Given a full filename (with path), ensure that that file can
147 // be accessed again USING FILENAME ONLY by adding the path
148 // to the list if not already there.
149 void wxPathList::EnsureFileAccessible (const wxString
& path
)
151 wxString
path1(path
);
152 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
155 if (!Member (wxString(path_only
)))
156 Add (wxString(path_only
));
160 bool wxPathList::Member (const wxString
& path
)
162 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
164 wxString
path2((char *) node
->Data ());
166 #if defined(__WINDOWS__) || defined(__VMS__) || defined (__WXMAC__)
168 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
170 // Case sensitive File System
171 path
.CompareTo (path2
) == 0
179 wxString
wxPathList::FindValidPath (const wxString
& file
)
181 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
182 return wxString(wxBuffer
);
184 char buf
[_MAXPATHLEN
];
185 strcpy(buf
, wxBuffer
);
187 char *filename
= (char*) NULL
; /* shut up buggy egcs warning */
188 filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
190 for (wxNode
* node
= First (); node
; node
= node
->Next ())
192 char *path
= (char *) node
->Data ();
193 strcpy (wxBuffer
, path
);
194 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
195 if (ch
!= '\\' && ch
!= '/')
196 strcat (wxBuffer
, "/");
197 strcat (wxBuffer
, filename
);
199 Unix2DosFilename (wxBuffer
);
201 if (wxFileExists (wxBuffer
))
203 return wxString(wxBuffer
); // Found!
207 return wxString(""); // Not found
210 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
212 wxString f
= FindValidPath(file
);
213 if (wxIsAbsolutePath(f
))
218 wxGetWorkingDirectory(buf
, 499);
219 int len
= (int)strlen(buf
);
223 if (lastCh
!= '/' && lastCh
!= '\\')
231 strcat(buf
, (const char *)f
);
232 strcpy(wxBuffer
, buf
);
233 return wxString(wxBuffer
);
238 wxFileExists (const wxString
& filename
)
240 #ifdef __GNUWIN32__ // (fix a B20 bug)
241 if (GetFileAttributes(filename
) == 0xFFFFFFFF)
245 #elif defined(__WXMAC__)
247 strcpy( gwxMacFileName
, filename
) ;
248 wxUnix2MacFilename( gwxMacFileName
) ;
249 if (gwxMacFileName
&& stat ((char *)(const char *)gwxMacFileName
, &stbuf
) == 0)
260 if ((filename
!= "") && stat ((char *)(const char *)filename
, &stbuf
) == 0)
266 /* Vadim's alternative implementation
268 // does the file exist?
269 bool wxFileExists(const char *pszFileName)
272 return !access(pszFileName, 0) &&
273 !stat(pszFileName, &st) &&
274 (st.st_mode & S_IFREG);
279 wxIsAbsolutePath (const wxString
& filename
)
283 if (filename
[0] == '/'
285 || (filename
[0] == '[' && filename
[1] != '.')
289 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
298 * Strip off any extension (dot something) from end of file,
299 * IF one exists. Inserts zero into buffer.
303 void wxStripExtension(char *buffer
)
305 int len
= strlen(buffer
);
309 if (buffer
[i
] == '.')
318 void wxStripExtension(wxString
& buffer
)
320 size_t len
= buffer
.Length();
324 if (buffer
.GetChar(i
) == '.')
326 buffer
= buffer
.Left(i
);
333 // Destructive removal of /./ and /../ stuff
334 char *wxRealPath (char *path
)
337 static const char SEP
= '\\';
338 Unix2DosFilename(path
);
340 static const char SEP
= '/';
342 if (path
[0] && path
[1]) {
343 /* MATTHEW: special case "/./x" */
345 if (path
[2] == SEP
&& path
[1] == '.')
353 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
356 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
357 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
358 && (q
- 1 <= path
|| q
[-1] != SEP
))
367 /* Check that path[2] is NULL! */
368 else if (path
[1] == ':' && !path
[2])
377 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
386 char *wxCopyAbsolutePath(const wxString
& filename
)
389 return (char *) NULL
;
391 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
392 char buf
[_MAXPATHLEN
];
394 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
395 char ch
= buf
[strlen(buf
) - 1];
397 if (ch
!= '\\' && ch
!= '/')
403 strcat(buf
, wxBuffer
);
404 return copystring( wxRealPath(buf
) );
406 return copystring( wxBuffer
);
412 ~user/ => user's home dir
413 If the environment variable a = "foo" and b = "bar" then:
430 /* input name in name, pathname output to buf. */
432 char *wxExpandPath(char *buf
, const char *name
)
434 register char *d
, *s
, *nm
;
435 char lnm
[_MAXPATHLEN
];
438 // Some compilers don't like this line.
439 // const char trimchars[] = "\n \t";
448 const char SEP
= '\\';
450 const char SEP
= '/';
453 if (name
== NULL
|| *name
== '\0')
455 nm
= copystring(name
); // Make a scratch copy
458 /* Skip leading whitespace and cr */
459 while (strchr((char *)trimchars
, *nm
) != NULL
)
461 /* And strip off trailing whitespace and cr */
462 s
= nm
+ (q
= strlen(nm
)) - 1;
463 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
471 q
= nm
[0] == '\\' && nm
[1] == '~';
474 /* Expand inline environment variables */
475 while ((*d
++ = *s
)) {
478 if ((*(d
- 1) = *++s
)) {
486 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
491 register char *start
= d
;
492 register int braces
= (*s
== '{' || *s
== '(');
493 register char *value
;
495 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
500 value
= getenv(braces
? start
+ 1 : start
);
502 for ((d
= start
- 1); (*d
++ = *value
++););
510 /* Expand ~ and ~user */
513 if (nm
[0] == '~' && !q
)
516 if (nm
[1] == SEP
|| nm
[1] == 0)
518 if ((s
= wxGetUserHome("")) != NULL
) {
523 { /* ~user/filename */
526 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
527 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
528 was_sep
= (*s
== SEP
);
529 nnm
= *s
? s
+ 1 : s
;
531 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
532 if (was_sep
) /* replace only if it was there: */
543 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
545 while ('\0' != (*d
++ = *s
++))
548 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
552 while ((*d
++ = *s
++));
554 delete[] nm_tmp
; // clean up alloc
555 /* Now clean up the buffer */
556 return wxRealPath(buf
);
560 /* Contract Paths to be build upon an environment variable
563 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
565 The call wxExpandPath can convert these back!
568 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
570 static char dest
[_MAXPATHLEN
];
573 return (char *) NULL
;
575 strcpy (dest
, WXSTRINGCAST filename
);
577 Unix2DosFilename(dest
);
580 // Handle environment
581 char *val
= (char *) NULL
;
582 char *tcp
= (char *) NULL
;
583 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
584 (tcp
= strstr (dest
, val
)) != NULL
)
586 strcpy (wxBuffer
, tcp
+ strlen (val
));
589 strcpy (tcp
, WXSTRINGCAST envname
);
591 strcat (tcp
, wxBuffer
);
594 // Handle User's home (ignore root homes!)
596 if ((val
= wxGetUserHome (user
)) != NULL
&&
597 (len
= strlen(val
)) > 2 &&
598 strncmp(dest
, val
, len
) == 0)
600 strcpy(wxBuffer
, "~");
602 strcat(wxBuffer
, (const char*) user
);
604 // strcat(wxBuffer, "\\");
606 // strcat(wxBuffer, "/");
608 strcat(wxBuffer
, dest
+ len
);
609 strcpy (dest
, wxBuffer
);
615 // Return just the filename, not the path
617 char *wxFileNameFromPath (char *path
)
623 tcp
= path
+ strlen (path
);
624 while (--tcp
>= path
)
626 if (*tcp
== '/' || *tcp
== '\\'
628 || *tcp
== ':' || *tcp
== ']')
635 if (isalpha (*path
) && *(path
+ 1) == ':')
642 wxString
wxFileNameFromPath (const wxString
& path1
)
647 char *path
= WXSTRINGCAST path1
;
650 tcp
= path
+ strlen (path
);
651 while (--tcp
>= path
)
653 if (*tcp
== '/' || *tcp
== '\\'
655 || *tcp
== ':' || *tcp
== ']')
659 return wxString(tcp
+ 1);
662 if (isalpha (*path
) && *(path
+ 1) == ':')
663 return wxString(path
+ 2);
666 // Yes, this should return the path, not an empty string, otherwise
667 // we get "thing.txt" -> "".
671 // Return just the directory, or NULL if no directory
673 wxPathOnly (char *path
)
677 static char buf
[_MAXPATHLEN
];
682 int l
= strlen(path
);
687 // Search backward for a backward or forward slash
688 while (!done
&& i
> -1)
691 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
706 // Try Drive specifier
707 if (isalpha (buf
[0]) && buf
[1] == ':')
709 // A:junk --> A:. (since A:.\junk Not A:\junk)
717 return (char *) NULL
;
720 // Return just the directory, or NULL if no directory
721 wxString
wxPathOnly (const wxString
& path
)
725 char buf
[_MAXPATHLEN
];
728 strcpy (buf
, WXSTRINGCAST path
);
730 int l
= path
.Length();
735 // Search backward for a backward or forward slash
736 while (!done
&& i
> -1)
739 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
748 return wxString(buf
);
754 // Try Drive specifier
755 if (isalpha (buf
[0]) && buf
[1] == ':')
757 // A:junk --> A:. (since A:.\junk Not A:\junk)
760 return wxString(buf
);
768 // Utility for converting delimiters in DOS filenames to UNIX style
769 // and back again - or we get nasty problems with delimiters.
770 // Also, convert to lower case, since case is significant in UNIX.
774 wxMac2UnixFilename (char *s
)
778 memmove( s
+1 , s
,strlen( s
) + 1) ;
789 *s
= wxToLower (*s
); // Case INDEPENDENT
796 wxUnix2MacFilename (char *s
)
802 // relative path , since it goes on with slash which is translated to a :
803 memmove( s
, s
+1 ,strlen( s
) ) ;
805 else if ( *s
== '/' )
807 // absolute path -> on mac just start with the drive name
808 memmove( s
, s
+1 ,strlen( s
) ) ;
812 wxASSERT_MSG( 1 , "unkown path beginning" ) ;
816 if (*s
== '/' || *s
== '\\')
825 wxDos2UnixFilename (char *s
)
834 *s
= wxToLower (*s
); // Case INDEPENDENT
842 wxUnix2DosFilename (char *s
)
844 wxUnix2DosFilename (char *WXUNUSED(s
))
847 // Yes, I really mean this to happen under DOS only! JACS
859 // Concatenate two files to form third
861 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
863 char *outfile
= wxGetTempFileName("cat");
865 FILE *fp1
= (FILE *) NULL
;
866 FILE *fp2
= (FILE *) NULL
;
867 FILE *fp3
= (FILE *) NULL
;
868 // Open the inputs and outputs
870 strcpy( gwxMacFileName
, file1
) ;
871 wxUnix2MacFilename( gwxMacFileName
) ;
872 strcpy( gwxMacFileName2
, file2
) ;
873 wxUnix2MacFilename( gwxMacFileName2
) ;
874 strcpy( gwxMacFileName3
, outfile
) ;
875 wxUnix2MacFilename( gwxMacFileName3
) ;
877 if ((fp1
= fopen (gwxMacFileName
, "rb")) == NULL
||
878 (fp2
= fopen (gwxMacFileName2
, "rb")) == NULL
||
879 (fp3
= fopen (gwxMacFileName3
, "wb")) == NULL
)
881 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
882 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
883 (fp3
= fopen (outfile
, "wb")) == NULL
)
896 while ((ch
= getc (fp1
)) != EOF
)
897 (void) putc (ch
, fp3
);
900 while ((ch
= getc (fp2
)) != EOF
)
901 (void) putc (ch
, fp3
);
905 bool result
= wxRenameFile(outfile
, file3
);
912 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
919 strcpy( gwxMacFileName
, file1
) ;
920 wxUnix2MacFilename( gwxMacFileName
) ;
921 strcpy( gwxMacFileName2
, file2
) ;
922 wxUnix2MacFilename( gwxMacFileName2
) ;
924 if ((fd1
= fopen (gwxMacFileName
, "rb")) == NULL
)
926 if ((fd2
= fopen (gwxMacFileName2
, "wb")) == NULL
)
928 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
930 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
937 while ((ch
= getc (fd1
)) != EOF
)
938 (void) putc (ch
, fd2
);
946 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
949 strcpy( gwxMacFileName
, file1
) ;
950 wxUnix2MacFilename( gwxMacFileName
) ;
951 strcpy( gwxMacFileName2
, file2
) ;
952 wxUnix2MacFilename( gwxMacFileName2
) ;
954 if (0 == rename (gwxMacFileName
, gwxMacFileName2
))
957 // Normal system call
958 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
962 if (wxCopyFile(file1
, file2
)) {
970 bool wxRemoveFile(const wxString
& file
)
972 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(__WATCOMC__)
973 int flag
= remove(WXSTRINGCAST file
);
974 #elif defined( __WXMAC__ )
975 strcpy( gwxMacFileName
, file
) ;
976 wxUnix2MacFilename( gwxMacFileName
) ;
977 int flag
= unlink(gwxMacFileName
);
979 int flag
= unlink(WXSTRINGCAST file
);
984 bool wxMkdir(const wxString
& dir
)
986 #if defined(__WXSTUBS__)
988 #elif defined(__VMS__)
990 #elif defined( __WXMAC__ )
991 strcpy( gwxMacFileName
, dir
) ;
992 wxUnix2MacFilename( gwxMacFileName
) ;
993 return (mkdir(gwxMacFileName
, 0 ) == 0);
994 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
995 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
997 return (mkdir(WXSTRINGCAST dir
) == 0);
1001 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
1005 #elif defined( __WXMAC__ )
1006 strcpy( gwxMacFileName
, dir
) ;
1007 wxUnix2MacFilename( gwxMacFileName
) ;
1008 return (rmdir(WXSTRINGCAST gwxMacFileName
) == 0);
1012 return FALSE
; // What to do?
1014 return (rmdir(WXSTRINGCAST dir
) == 0);
1021 bool wxDirExists(const wxString
& dir
)
1025 #elif !defined(__WXMSW__)
1027 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
1030 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
1031 #if defined(__WIN32__)
1032 WIN32_FIND_DATA fileInfo
;
1035 struct ffblk fileInfo
;
1037 struct find_t fileInfo
;
1041 #if defined(__WIN32__)
1042 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
1044 if (h
==INVALID_HANDLE_VALUE
)
1048 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
1051 // In Borland findfirst has a different argument
1052 // ordering from _dos_findfirst. But _dos_findfirst
1053 // _should_ be ok in both MS and Borland... why not?
1055 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
1057 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
1066 // does the path exists? (may have or not '/' or '\\' at the end)
1067 bool wxPathExists(const char *pszPathName
)
1069 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
1070 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
1071 wxString
strPath(pszPathName
);
1072 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
1073 strPath
.Last() = '\0';
1081 return stat((char*) (const char*) strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
1084 // Get a temporary filename, opening and closing the file.
1085 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
1091 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
1094 char tmpPath
[MAX_PATH
];
1095 ::GetTempPath(MAX_PATH
, tmpPath
);
1096 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
1098 if (buf
) strcpy(buf
, tmp
);
1099 else buf
= copystring(tmp
);
1103 static short last_temp
= 0; // cache last to speed things a bit
1104 // At most 1000 temp files to a process! We use a ring count.
1105 char tmp
[100]; // FIXME static buffer
1107 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
1109 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
1110 if (!wxFileExists( tmp
))
1112 // Touch the file to create it (reserve name)
1113 FILE *fd
= fopen (tmp
, "w");
1120 buf
= copystring( tmp
);
1124 wxLogError( _("wxWindows: error finding temporary file name.\n") );
1125 if (buf
) buf
[0] = 0;
1126 return (char *) NULL
;
1130 // Get first file name matching given wild card.
1134 // Get first file name matching given wild card.
1135 // Flags are reserved for future use.
1138 static DIR *wxDirStream
= (DIR *) NULL
;
1139 static char *wxFileSpec
= (char *) NULL
;
1140 static int wxFindFileFlags
= 0;
1143 char *wxFindFirstFile(const char *spec
, int flags
)
1147 closedir(wxDirStream
); // edz 941103: better housekeping
1149 wxFindFileFlags
= flags
;
1152 delete[] wxFileSpec
;
1153 wxFileSpec
= copystring(spec
);
1155 // Find path only so we can concatenate
1156 // found file onto path
1157 char *p
= wxPathOnly(wxFileSpec
);
1159 /* MATTHEW: special case: path is really "/" */
1160 if (p
&& !*p
&& *wxFileSpec
== '/')
1162 /* MATTHEW: p is NULL => Local directory */
1166 if ((wxDirStream
=opendir(p
))==NULL
)
1167 return (char *) NULL
;
1169 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1170 return wxFindNextFile();
1173 return (char *) NULL
;
1176 char *wxFindNextFile(void)
1179 static char buf
[400]; // FIXME static buffer
1181 /* MATTHEW: [2] Don't crash if we read too many times */
1183 return (char *) NULL
;
1185 // Find path only so we can concatenate
1186 // found file onto path
1187 char *p
= wxPathOnly(wxFileSpec
);
1188 char *n
= wxFileNameFromPath(wxFileSpec
);
1190 /* MATTHEW: special case: path is really "/" */
1191 if (p
&& !*p
&& *wxFileSpec
== '/')
1195 struct dirent
*nextDir
;
1196 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1199 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1200 directories when flags & wxDIR */
1201 if (wxMatchWild(n
, nextDir
->d_name
)) {
1207 if (strcmp(p
, "/") != 0)
1210 strcat(buf
, nextDir
->d_name
);
1212 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1213 (strcmp(nextDir
->d_name
, "..") == 0)) {
1214 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1218 isdir
= wxDirExists(buf
);
1220 if (!wxFindFileFlags
1221 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1222 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1226 closedir(wxDirStream
);
1227 wxDirStream
= (DIR *) NULL
;
1231 return (char *) NULL
;
1234 #elif defined(__WXMSW__)
1237 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1238 WIN32_FIND_DATA wxFileStruc
;
1241 static struct ffblk wxFileStruc
;
1243 static struct _find_t wxFileStruc
;
1246 static wxString wxFileSpec
= "";
1247 static int wxFindFileFlags
;
1249 char *wxFindFirstFile(const char *spec
, int flags
)
1252 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1254 // Find path only so we can concatenate
1255 // found file onto path
1256 wxString
path1(wxFileSpec
);
1257 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1258 if (p
&& (strlen(p
) > 0))
1259 strcpy(wxBuffer
, p
);
1264 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1265 FindClose(wxFileStrucHandle
);
1267 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1269 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1272 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1274 if (isdir
&& !(flags
& wxDIR
))
1275 return wxFindNextFile();
1276 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1277 return wxFindNextFile();
1279 if (wxBuffer
[0] != 0)
1280 strcat(wxBuffer
, "\\");
1281 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1285 int flag
= _A_NORMAL
;
1286 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1290 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1292 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1295 /* MATTHEW: [5] Check directory flag */
1299 attrib
= wxFileStruc
.ff_attrib
;
1301 attrib
= wxFileStruc
.attrib
;
1304 if (attrib
& _A_SUBDIR
) {
1305 if (!(wxFindFileFlags
& wxDIR
))
1306 return wxFindNextFile();
1307 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1308 return wxFindNextFile();
1310 if (wxBuffer
[0] != 0)
1311 strcat(wxBuffer
, "\\");
1314 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1316 strcat(wxBuffer
, wxFileStruc
.name
);
1325 char *wxFindNextFile(void)
1327 // Find path only so we can concatenate
1328 // found file onto path
1329 wxString
p2(wxFileSpec
);
1330 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1331 if (p
&& (strlen(p
) > 0))
1332 strcpy(wxBuffer
, p
);
1339 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1342 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1344 FindClose(wxFileStrucHandle
);
1345 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1349 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1351 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1353 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1356 if (wxBuffer
[0] != 0)
1357 strcat(wxBuffer
, "\\");
1358 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1363 if (findnext(&wxFileStruc
) == 0)
1365 if (_dos_findnext(&wxFileStruc
) == 0)
1368 /* MATTHEW: [5] Check directory flag */
1372 attrib
= wxFileStruc
.ff_attrib
;
1374 attrib
= wxFileStruc
.attrib
;
1377 if (attrib
& _A_SUBDIR
) {
1378 if (!(wxFindFileFlags
& wxDIR
))
1380 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1384 if (wxBuffer
[0] != 0)
1385 strcat(wxBuffer
, "\\");
1387 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1389 strcat(wxBuffer
, wxFileStruc
.name
);
1401 // Get current working directory.
1402 // If buf is NULL, allocates space using new, else
1404 char *wxGetWorkingDirectory(char *buf
, int sz
)
1407 buf
= new char[sz
+1];
1409 if (_getcwd(buf
, sz
) == NULL
) {
1411 if (getcwd(buf
, sz
) == NULL
) {
1419 bool wxSetWorkingDirectory(const wxString
& d
)
1421 #if defined( __UNIX__ ) || defined( __WXMAC__ )
1422 return (chdir(d
) == 0);
1423 #elif defined(__WINDOWS__)
1426 return (bool)(SetCurrentDirectory(d
) != 0);
1428 // Must change drive, too.
1429 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1432 char firstChar
= d
[0];
1436 firstChar
= firstChar
- 32;
1438 // To a drive number
1439 unsigned int driveNo
= firstChar
- 64;
1442 unsigned int noDrives
;
1443 _dos_setdrive(driveNo
, &noDrives
);
1446 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1454 // Get the OS directory if appropriate (such as the Windows directory).
1455 // On non-Windows platform, probably just return the empty string.
1456 wxString
wxGetOSDirectory()
1460 GetWindowsDirectory(buf
, 256);
1461 return wxString(buf
);
1463 return wxEmptyString
;
1467 bool wxEndsWithPathSeparator(const char *pszFileName
)
1469 size_t len
= Strlen(pszFileName
);
1473 return wxIsPathSeparator(pszFileName
[len
- 1]);
1476 // find a file in a list of directories, returns false if not found
1477 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1479 // we assume that it's not empty
1480 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1481 _("empty file name in wxFindFileInPath"));
1483 // skip path separator in the beginning of the file name if present
1484 if ( wxIsPathSeparator(*pszFile
) )
1487 // copy the path (strtok will modify it)
1488 char *szPath
= new char[strlen(pszPath
) + 1];
1489 strcpy(szPath
, pszPath
);
1493 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1494 // search for the file in this directory
1496 if ( !wxEndsWithPathSeparator(pc
) )
1497 strFile
+= FILE_SEP_PATH
;
1500 if ( FileExists(strFile
) ) {
1508 return pc
!= NULL
; // if true => we breaked from the loop
1511 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1516 wxCHECK_RET( pszFileName
, "NULL file name in wxSplitPath" );
1518 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1519 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1520 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1522 // take the last of the two: nPosUnix containts the last slash in the
1524 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1525 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1526 if ( nPosDos
> nPosUnix
)
1530 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1534 nPosDot
= pDot
- pszFileName
;
1536 if ( nPosDot
> nPosUnix
) {
1537 // the file name looks like "path/name.ext"
1539 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1541 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1544 // there is either no dot at all or there is a '/' after it
1546 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1552 //------------------------------------------------------------------------
1553 // wild character routines
1554 //------------------------------------------------------------------------
1556 bool wxIsWild( const wxString
& pattern
)
1558 wxString tmp
= pattern
;
1559 char *pat
= WXSTRINGCAST(tmp
);
1562 case '?': case '*': case '[': case '{':
1572 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1574 #if defined(HAVE_FNMATCH_H)
1577 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1579 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1583 // #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1586 * WARNING: this code is broken!
1589 wxString tmp1
= pat
;
1590 char *pattern
= WXSTRINGCAST(tmp1
);
1591 wxString tmp2
= text
;
1592 char *str
= WXSTRINGCAST(tmp2
);
1595 bool done
= FALSE
, ret_code
, ok
;
1596 // Below is for vi fans
1597 const char OB
= '{', CB
= '}';
1599 // dot_special means '.' only matches '.'
1600 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1603 while ((*pattern
!= '\0') && (!done
)
1604 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1608 if (*pattern
!= '\0')
1615 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1618 while (*str
!= '\0')
1620 while (*pattern
!= '\0')
1627 if ((*pattern
== '\0') || (*pattern
== ']')) {
1631 if (*pattern
== '\\') {
1633 if (*pattern
== '\0') {
1638 if (*(pattern
+ 1) == '-') {
1641 if (*pattern
== ']') {
1645 if (*pattern
== '\\') {
1647 if (*pattern
== '\0') {
1652 if ((*str
< c
) || (*str
> *pattern
)) {
1656 } else if (*pattern
!= *str
) {
1661 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1662 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1666 if (*pattern
!= '\0') {
1676 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1679 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1680 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1681 if (*pattern
== '\\')
1683 ok
= (*pattern
++ == *cp
++);
1685 if (*pattern
== '\0') {
1691 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1692 if (*++pattern
== '\\') {
1693 if (*++pattern
== CB
)
1698 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1699 if (*++pattern
== '\\') {
1700 if (*++pattern
== CB
|| *pattern
== ',')
1705 if (*pattern
!= '\0')
1710 if (*str
== *pattern
) {
1717 while (*pattern
== '*')
1719 return ((*str
== '\0') && (*pattern
== '\0'));
1725 #pragma warning(default:4706) // assignment within conditional expression