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))
46 #include <sys/types.h>
62 #include <sys/unistd.h>
63 #define stricmp strcasecmp
66 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
67 // this (3.1 I believe) and how to test for it.
68 // If this works for Borland 4.0 as well, then no worries.
81 #define _MAXPATHLEN 500
83 extern char *wxBuffer
;
85 void wxPathList::Add (const wxString
& path
)
87 wxStringList::Add ((char *)(const char *)path
);
90 // Add paths e.g. from the PATH environment variable
91 void wxPathList::AddEnvList (const wxString
& envVariable
)
93 static const char PATH_TOKS
[] =
95 " ;"; // Don't seperate with colon in DOS (used for drive)
100 char *val
= getenv (WXSTRINGCAST envVariable
);
103 char *s
= copystring (val
);
104 char *token
= strtok (s
, PATH_TOKS
);
108 Add (copystring (token
));
111 if ((token
= strtok ((char *) NULL
, PATH_TOKS
)) != NULL
)
112 Add (wxString(token
));
119 // Given a full filename (with path), ensure that that file can
120 // be accessed again USING FILENAME ONLY by adding the path
121 // to the list if not already there.
122 void wxPathList::EnsureFileAccessible (const wxString
& path
)
124 wxString
path1(path
);
125 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
128 if (!Member (wxString(path_only
)))
129 Add (wxString(path_only
));
133 bool wxPathList::Member (const wxString
& path
)
135 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
137 wxString
path2((char *) node
->Data ());
139 #if defined(__WINDOWS__) || defined(__VMS__)
141 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
143 // Case sensitive File System
144 path
.CompareTo (path2
) == 0
152 wxString
wxPathList::FindValidPath (const wxString
& file
)
154 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
155 return wxString(wxBuffer
);
157 char buf
[_MAXPATHLEN
];
158 strcpy(buf
, wxBuffer
);
160 char *filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
162 for (wxNode
* node
= First (); node
; node
= node
->Next ())
164 char *path
= (char *) node
->Data ();
165 strcpy (wxBuffer
, path
);
166 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
167 if (ch
!= '\\' && ch
!= '/')
168 strcat (wxBuffer
, "/");
169 strcat (wxBuffer
, filename
);
171 Unix2DosFilename (wxBuffer
);
173 if (wxFileExists (wxBuffer
))
175 return wxString(wxBuffer
); // Found!
179 return wxString(""); // Not found
182 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
184 wxString f
= FindValidPath(file
);
185 if (wxIsAbsolutePath(f
))
190 wxGetWorkingDirectory(buf
, 499);
191 int len
= (int)strlen(buf
);
195 if (lastCh
!= '/' && lastCh
!= '\\')
203 strcat(buf
, (const char *)f
);
204 strcpy(wxBuffer
, buf
);
205 return wxString(wxBuffer
);
210 wxFileExists (const wxString
& filename
)
212 #ifdef __GNUWIN32__ // (fix a B20 bug)
213 if (GetFileAttributes(filename
) == 0xFFFFFFFF)
220 if (filename
&& stat ((char *)(const char *)filename
, &stbuf
) == 0)
226 /* Vadim's alternative implementation
228 // does the file exist?
229 bool wxFileExists(const char *pszFileName)
232 return !access(pszFileName, 0) &&
233 !stat(pszFileName, &st) &&
234 (st.st_mode & S_IFREG);
239 wxIsAbsolutePath (const wxString
& filename
)
243 if (filename
[0] == '/'
245 || (filename
[0] == '[' && filename
[1] != '.')
249 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
258 * Strip off any extension (dot something) from end of file,
259 * IF one exists. Inserts zero into buffer.
263 void wxStripExtension(char *buffer
)
265 int len
= strlen(buffer
);
269 if (buffer
[i
] == '.')
278 void wxStripExtension(wxString
& buffer
)
280 size_t len
= buffer
.Length();
284 if (buffer
.GetChar(i
) == '.')
286 buffer
= buffer
.Left(i
);
293 // Destructive removal of /./ and /../ stuff
294 char *wxRealPath (char *path
)
297 static const char SEP
= '\\';
298 Unix2DosFilename(path
);
300 static const char SEP
= '/';
302 if (path
[0] && path
[1]) {
303 /* MATTHEW: special case "/./x" */
305 if (path
[2] == SEP
&& path
[1] == '.')
313 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
316 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
317 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
318 && (q
- 1 <= path
|| q
[-1] != SEP
))
327 /* Check that path[2] is NULL! */
328 else if (path
[1] == ':' && !path
[2])
337 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
346 char *wxCopyAbsolutePath(const wxString
& filename
)
349 return (char *) NULL
;
351 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
352 char buf
[_MAXPATHLEN
];
354 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
355 char ch
= buf
[strlen(buf
) - 1];
357 if (ch
!= '\\' && ch
!= '/')
363 strcat(buf
, wxBuffer
);
364 return copystring( wxRealPath(buf
) );
366 return copystring( wxBuffer
);
372 ~user/ => user's home dir
373 If the environment variable a = "foo" and b = "bar" then:
390 /* input name in name, pathname output to buf. */
392 char *wxExpandPath(char *buf
, const char *name
)
394 register char *d
, *s
, *nm
;
395 char lnm
[_MAXPATHLEN
];
398 // Some compilers don't like this line.
399 // const char trimchars[] = "\n \t";
408 const char SEP
= '\\';
410 const char SEP
= '/';
413 if (name
== NULL
|| *name
== '\0')
415 nm
= copystring(name
); // Make a scratch copy
418 /* Skip leading whitespace and cr */
419 while (strchr((char *)trimchars
, *nm
) != NULL
)
421 /* And strip off trailing whitespace and cr */
422 s
= nm
+ (q
= strlen(nm
)) - 1;
423 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
431 q
= nm
[0] == '\\' && nm
[1] == '~';
434 /* Expand inline environment variables */
435 while ((*d
++ = *s
)) {
438 if ((*(d
- 1) = *++s
)) {
446 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
451 register char *start
= d
;
452 register int braces
= (*s
== '{' || *s
== '(');
453 register char *value
;
455 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
460 value
= getenv(braces
? start
+ 1 : start
);
462 for ((d
= start
- 1); (*d
++ = *value
++););
470 /* Expand ~ and ~user */
473 if (nm
[0] == '~' && !q
)
476 if (nm
[1] == SEP
|| nm
[1] == 0)
478 if ((s
= wxGetUserHome("")) != NULL
) {
483 { /* ~user/filename */
486 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
487 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
488 was_sep
= (*s
== SEP
);
489 nnm
= *s
? s
+ 1 : s
;
491 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
492 if (was_sep
) /* replace only if it was there: */
503 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
505 while ('\0' != (*d
++ = *s
++))
508 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
512 while ((*d
++ = *s
++));
514 delete[] nm_tmp
; // clean up alloc
515 /* Now clean up the buffer */
516 return wxRealPath(buf
);
520 /* Contract Paths to be build upon an environment variable
523 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
525 The call wxExpandPath can convert these back!
528 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
530 static char dest
[_MAXPATHLEN
];
533 return (char *) NULL
;
535 strcpy (dest
, WXSTRINGCAST filename
);
537 Unix2DosFilename(dest
);
540 // Handle environment
541 char *val
= (char *) NULL
;
542 char *tcp
= (char *) NULL
;
543 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
544 (tcp
= strstr (dest
, val
)) != NULL
)
546 strcpy (wxBuffer
, tcp
+ strlen (val
));
549 strcpy (tcp
, WXSTRINGCAST envname
);
551 strcat (tcp
, wxBuffer
);
554 // Handle User's home (ignore root homes!)
556 if ((val
= wxGetUserHome (user
)) != NULL
&&
557 (len
= strlen(val
)) > 2 &&
558 strncmp(dest
, val
, len
) == 0)
560 strcpy(wxBuffer
, "~");
562 strcat(wxBuffer
, user
);
564 // strcat(wxBuffer, "\\");
566 // strcat(wxBuffer, "/");
568 strcat(wxBuffer
, dest
+ len
);
569 strcpy (dest
, wxBuffer
);
575 // Return just the filename, not the path
577 char *wxFileNameFromPath (char *path
)
583 tcp
= path
+ strlen (path
);
584 while (--tcp
>= path
)
586 if (*tcp
== '/' || *tcp
== '\\'
588 || *tcp
== ':' || *tcp
== ']')
595 if (isalpha (*path
) && *(path
+ 1) == ':')
602 wxString
wxFileNameFromPath (const wxString
& path1
)
607 char *path
= WXSTRINGCAST path1
;
610 tcp
= path
+ strlen (path
);
611 while (--tcp
>= path
)
613 if (*tcp
== '/' || *tcp
== '\\'
615 || *tcp
== ':' || *tcp
== ']')
619 return wxString(tcp
+ 1);
622 if (isalpha (*path
) && *(path
+ 1) == ':')
623 return wxString(path
+ 2);
629 // Return just the directory, or NULL if no directory
631 wxPathOnly (char *path
)
635 static char buf
[_MAXPATHLEN
];
640 int l
= strlen(path
);
645 // Search backward for a backward or forward slash
646 while (!done
&& i
> -1)
649 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
664 // Try Drive specifier
665 if (isalpha (buf
[0]) && buf
[1] == ':')
667 // A:junk --> A:. (since A:.\junk Not A:\junk)
675 return (char *) NULL
;
678 // Return just the directory, or NULL if no directory
679 wxString
wxPathOnly (const wxString
& path
)
683 char buf
[_MAXPATHLEN
];
686 strcpy (buf
, WXSTRINGCAST path
);
688 int l
= path
.Length();
693 // Search backward for a backward or forward slash
694 while (!done
&& i
> -1)
697 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
706 return wxString(buf
);
712 // Try Drive specifier
713 if (isalpha (buf
[0]) && buf
[1] == ':')
715 // A:junk --> A:. (since A:.\junk Not A:\junk)
718 return wxString(buf
);
726 // Utility for converting delimiters in DOS filenames to UNIX style
727 // and back again - or we get nasty problems with delimiters.
728 // Also, convert to lower case, since case is significant in UNIX.
731 wxDos2UnixFilename (char *s
)
740 *s
= wxToLower (*s
); // Case INDEPENDENT
748 wxUnix2DosFilename (char *s
)
750 wxUnix2DosFilename (char *WXUNUSED(s
))
753 // Yes, I really mean this to happen under DOS only! JACS
765 // Concatenate two files to form third
767 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
769 char *outfile
= wxGetTempFileName("cat");
771 FILE *fp1
= (FILE *) NULL
;
772 FILE *fp2
= (FILE *) NULL
;
773 FILE *fp3
= (FILE *) NULL
;
774 // Open the inputs and outputs
775 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
776 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
777 (fp3
= fopen (outfile
, "wb")) == NULL
)
789 while ((ch
= getc (fp1
)) != EOF
)
790 (void) putc (ch
, fp3
);
793 while ((ch
= getc (fp2
)) != EOF
)
794 (void) putc (ch
, fp3
);
798 bool result
= wxRenameFile(outfile
, file3
);
805 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
811 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
813 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
819 while ((ch
= getc (fd1
)) != EOF
)
820 (void) putc (ch
, fd2
);
828 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
830 // Normal system call
831 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
834 if (wxCopyFile(file1
, file2
)) {
842 bool wxRemoveFile(const wxString
& file
)
844 #if defined(_MSC_VER) || defined(__BORLANDC__)
845 int flag
= remove(WXSTRINGCAST file
);
847 int flag
= unlink(WXSTRINGCAST file
);
852 bool wxMkdir(const wxString
& dir
)
854 #if defined(__WXSTUBS__)
856 #elif defined(__VMS__)
858 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
859 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
861 return (mkdir(WXSTRINGCAST dir
) == 0);
865 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
870 return (rmdir(WXSTRINGCAST dir
) == 0);
875 bool wxDirExists(const wxString
& dir
)
879 #elif !defined(__WXMSW__)
881 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
884 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
885 #if defined(__WIN32__)
886 WIN32_FIND_DATA fileInfo
;
889 struct ffblk fileInfo
;
891 struct find_t fileInfo
;
895 #if defined(__WIN32__)
896 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
898 if (h
==INVALID_HANDLE_VALUE
)
902 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
905 // In Borland findfirst has a different argument
906 // ordering from _dos_findfirst. But _dos_findfirst
907 // _should_ be ok in both MS and Borland... why not?
909 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
911 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
920 // does the path exists? (may have or not '/' or '\\' at the end)
921 bool wxPathExists(const char *pszPathName
)
923 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
924 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
925 wxString
strPath(pszPathName
);
926 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
927 strPath
.Last() = '\0';
930 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
933 // Get a temporary filename, opening and closing the file.
934 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
940 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
943 char tmpPath
[MAX_PATH
];
944 ::GetTempPath(MAX_PATH
, tmpPath
);
945 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
947 if (buf
) strcpy(buf
, tmp
);
948 else buf
= copystring(tmp
);
952 static short last_temp
= 0; // cache last to speed things a bit
953 // At most 1000 temp files to a process! We use a ring count.
954 char tmp
[100]; // FIXME static buffer
956 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
958 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
959 if (!wxFileExists( tmp
))
961 // Touch the file to create it (reserve name)
962 FILE *fd
= fopen (tmp
, "w");
969 buf
= copystring( tmp
);
973 cerr
<< _("wxWindows: error finding temporary file name.\n");
975 return (char *) NULL
;
979 // Get first file name matching given wild card.
983 // Get first file name matching given wild card.
984 // Flags are reserved for future use.
987 static DIR *wxDirStream
= (DIR *) NULL
;
988 static char *wxFileSpec
= (char *) NULL
;
989 static int wxFindFileFlags
= 0;
992 char *wxFindFirstFile(const char *spec
, int flags
)
996 closedir(wxDirStream
); // edz 941103: better housekeping
998 wxFindFileFlags
= flags
;
1001 delete[] wxFileSpec
;
1002 wxFileSpec
= copystring(spec
);
1004 // Find path only so we can concatenate
1005 // found file onto path
1006 char *p
= wxPathOnly(wxFileSpec
);
1008 /* MATTHEW: special case: path is really "/" */
1009 if (p
&& !*p
&& *wxFileSpec
== '/')
1011 /* MATTHEW: p is NULL => Local directory */
1015 if ((wxDirStream
=opendir(p
))==NULL
)
1016 return (char *) NULL
;
1018 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1019 return wxFindNextFile();
1022 return (char *) NULL
;
1025 char *wxFindNextFile(void)
1028 static char buf
[400]; // FIXME static buffer
1030 /* MATTHEW: [2] Don't crash if we read too many times */
1032 return (char *) NULL
;
1034 // Find path only so we can concatenate
1035 // found file onto path
1036 char *p
= wxPathOnly(wxFileSpec
);
1037 char *n
= wxFileNameFromPath(wxFileSpec
);
1039 /* MATTHEW: special case: path is really "/" */
1040 if (p
&& !*p
&& *wxFileSpec
== '/')
1044 struct dirent
*nextDir
;
1045 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1048 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1049 directories when flags & wxDIR */
1050 if (wxMatchWild(n
, nextDir
->d_name
)) {
1056 if (strcmp(p
, "/") != 0)
1059 strcat(buf
, nextDir
->d_name
);
1061 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1062 (strcmp(nextDir
->d_name
, "..") == 0)) {
1063 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1067 isdir
= wxDirExists(buf
);
1069 if (!wxFindFileFlags
1070 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1071 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1075 closedir(wxDirStream
);
1076 wxDirStream
= (DIR *) NULL
;
1080 return (char *) NULL
;
1083 #elif defined(__WXMSW__)
1086 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1087 WIN32_FIND_DATA wxFileStruc
;
1090 static struct ffblk wxFileStruc
;
1092 static struct _find_t wxFileStruc
;
1095 static wxString wxFileSpec
= "";
1096 static int wxFindFileFlags
;
1098 char *wxFindFirstFile(const char *spec
, int flags
)
1101 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1103 // Find path only so we can concatenate
1104 // found file onto path
1105 wxString
path1(wxFileSpec
);
1106 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1107 if (p
&& (strlen(p
) > 0))
1108 strcpy(wxBuffer
, p
);
1113 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1114 FindClose(wxFileStrucHandle
);
1116 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1118 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1121 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1123 if (isdir
&& !(flags
& wxDIR
))
1124 return wxFindNextFile();
1125 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1126 return wxFindNextFile();
1128 if (wxBuffer
[0] != 0)
1129 strcat(wxBuffer
, "\\");
1130 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1134 int flag
= _A_NORMAL
;
1135 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1139 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1141 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1144 /* MATTHEW: [5] Check directory flag */
1148 attrib
= wxFileStruc
.ff_attrib
;
1150 attrib
= wxFileStruc
.attrib
;
1153 if (attrib
& _A_SUBDIR
) {
1154 if (!(wxFindFileFlags
& wxDIR
))
1155 return wxFindNextFile();
1156 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1157 return wxFindNextFile();
1159 if (wxBuffer
[0] != 0)
1160 strcat(wxBuffer
, "\\");
1163 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1165 strcat(wxBuffer
, wxFileStruc
.name
);
1174 char *wxFindNextFile(void)
1176 // Find path only so we can concatenate
1177 // found file onto path
1178 wxString
p2(wxFileSpec
);
1179 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1180 if (p
&& (strlen(p
) > 0))
1181 strcpy(wxBuffer
, p
);
1188 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1191 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1193 FindClose(wxFileStrucHandle
);
1194 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1198 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1200 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1202 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1205 if (wxBuffer
[0] != 0)
1206 strcat(wxBuffer
, "\\");
1207 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1212 if (findnext(&wxFileStruc
) == 0)
1214 if (_dos_findnext(&wxFileStruc
) == 0)
1217 /* MATTHEW: [5] Check directory flag */
1221 attrib
= wxFileStruc
.ff_attrib
;
1223 attrib
= wxFileStruc
.attrib
;
1226 if (attrib
& _A_SUBDIR
) {
1227 if (!(wxFindFileFlags
& wxDIR
))
1229 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1233 if (wxBuffer
[0] != 0)
1234 strcat(wxBuffer
, "\\");
1236 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1238 strcat(wxBuffer
, wxFileStruc
.name
);
1250 // Get current working directory.
1251 // If buf is NULL, allocates space using new, else
1253 char *wxGetWorkingDirectory(char *buf
, int sz
)
1256 buf
= new char[sz
+1];
1258 if (_getcwd(buf
, sz
) == NULL
) {
1260 if (getcwd(buf
, sz
) == NULL
) {
1268 bool wxSetWorkingDirectory(const wxString
& d
)
1271 return (chdir(d
) == 0);
1272 #elif defined(__WINDOWS__)
1275 return (bool)(SetCurrentDirectory(d
) != 0);
1277 // Must change drive, too.
1278 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1281 char firstChar
= d
[0];
1285 firstChar
= firstChar
- 32;
1287 // To a drive number
1288 unsigned int driveNo
= firstChar
- 64;
1291 unsigned int noDrives
;
1292 _dos_setdrive(driveNo
, &noDrives
);
1295 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1303 bool wxEndsWithPathSeparator(const char *pszFileName
)
1305 size_t len
= Strlen(pszFileName
);
1309 return wxIsPathSeparator(pszFileName
[len
- 1]);
1312 // find a file in a list of directories, returns false if not found
1313 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1315 // we assume that it's not empty
1316 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1317 _("empty file name in wxFindFileInPath"));
1319 // skip path separator in the beginning of the file name if present
1320 if ( wxIsPathSeparator(*pszFile
) )
1323 // copy the path (strtok will modify it)
1324 char *szPath
= new char[strlen(pszPath
) + 1];
1325 strcpy(szPath
, pszPath
);
1329 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1330 // search for the file in this directory
1332 if ( !wxEndsWithPathSeparator(pc
) )
1333 strFile
+= FILE_SEP_PATH
;
1336 if ( FileExists(strFile
) ) {
1344 return pc
!= NULL
; // if true => we breaked from the loop
1347 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1352 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1354 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1355 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1356 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1358 // take the last of the two
1359 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1360 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1361 if ( nPosDos
> nPosUnix
)
1363 // size_t nLen = Strlen(pszFileName);
1366 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1368 size_t nPosDot
= pDot
- pszFileName
;
1370 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1372 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1376 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1382 //------------------------------------------------------------------------
1383 // wild character routines
1384 //------------------------------------------------------------------------
1386 bool wxIsWild( const wxString
& pattern
)
1388 wxString tmp
= pattern
;
1389 char *pat
= WXSTRINGCAST(tmp
);
1392 case '?': case '*': case '[': case '{':
1402 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1403 #ifdef HAVE_FNMATCH_H
1406 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1408 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1412 #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1414 * WARNING: this code is broken!
1417 wxString tmp1
= pat
;
1418 char *pattern
= WXSTRINGCAST(tmp1
);
1419 wxString tmp2
= text
;
1420 char *str
= WXSTRINGCAST(tmp2
);
1423 bool done
= FALSE
, ret_code
, ok
;
1424 // Below is for vi fans
1425 const char OB
= '{', CB
= '}';
1427 // dot_special means '.' only matches '.'
1428 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1431 while ((*pattern
!= '\0') && (!done
)
1432 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1436 if (*pattern
!= '\0')
1443 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1446 while (*str
!= '\0')
1448 while (*pattern
!= '\0')
1455 if ((*pattern
== '\0') || (*pattern
== ']')) {
1459 if (*pattern
== '\\') {
1461 if (*pattern
== '\0') {
1466 if (*(pattern
+ 1) == '-') {
1469 if (*pattern
== ']') {
1473 if (*pattern
== '\\') {
1475 if (*pattern
== '\0') {
1480 if ((*str
< c
) || (*str
> *pattern
)) {
1484 } else if (*pattern
!= *str
) {
1489 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1490 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1494 if (*pattern
!= '\0') {
1504 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1507 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1508 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1509 if (*pattern
== '\\')
1511 ok
= (*pattern
++ == *cp
++);
1513 if (*pattern
== '\0') {
1519 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1520 if (*++pattern
== '\\') {
1521 if (*++pattern
== CB
)
1526 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1527 if (*++pattern
== '\\') {
1528 if (*++pattern
== CB
|| *pattern
== ',')
1533 if (*pattern
!= '\0')
1538 if (*str
== *pattern
) {
1545 while (*pattern
== '*')
1547 return ((*str
== '\0') && (*pattern
== '\0'));
1553 #pragma warning(default:4706) // assignment within conditional expression