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 #if !USE_SHARED_LIBRARIES
86 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
89 void wxPathList::Add (const wxString
& path
)
91 wxStringList::Add ((char *)(const char *)path
);
94 // Add paths e.g. from the PATH environment variable
95 void wxPathList::AddEnvList (const wxString
& envVariable
)
97 static const char PATH_TOKS
[] =
99 " ;"; // Don't seperate with colon in DOS (used for drive)
104 char *val
= getenv (WXSTRINGCAST envVariable
);
107 char *s
= copystring (val
);
108 char *token
= strtok (s
, PATH_TOKS
);
112 Add (copystring (token
));
115 if ((token
= strtok ((char *) NULL
, PATH_TOKS
)) != NULL
)
116 Add (wxString(token
));
123 // Given a full filename (with path), ensure that that file can
124 // be accessed again USING FILENAME ONLY by adding the path
125 // to the list if not already there.
126 void wxPathList::EnsureFileAccessible (const wxString
& path
)
128 wxString
path1(path
);
129 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
132 if (!Member (wxString(path_only
)))
133 Add (wxString(path_only
));
137 bool wxPathList::Member (const wxString
& path
)
139 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
141 wxString
path2((char *) node
->Data ());
143 #if defined(__WINDOWS__) || defined(__VMS__)
145 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
147 // Case sensitive File System
148 path
.CompareTo (path2
) == 0
156 wxString
wxPathList::FindValidPath (const wxString
& file
)
158 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
159 return wxString(wxBuffer
);
161 char buf
[_MAXPATHLEN
];
162 strcpy(buf
, wxBuffer
);
164 char *filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
166 for (wxNode
* node
= First (); node
; node
= node
->Next ())
168 char *path
= (char *) node
->Data ();
169 strcpy (wxBuffer
, path
);
170 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
171 if (ch
!= '\\' && ch
!= '/')
172 strcat (wxBuffer
, "/");
173 strcat (wxBuffer
, filename
);
175 Unix2DosFilename (wxBuffer
);
177 if (wxFileExists (wxBuffer
))
179 return wxString(wxBuffer
); // Found!
183 return wxString(""); // Not found
186 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
188 wxString f
= FindValidPath(file
);
189 if (wxIsAbsolutePath(f
))
194 wxGetWorkingDirectory(buf
, 499);
195 int len
= (int)strlen(buf
);
199 if (lastCh
!= '/' && lastCh
!= '\\')
207 strcat(buf
, (const char *)f
);
208 strcpy(wxBuffer
, buf
);
209 return wxString(wxBuffer
);
214 wxFileExists (const wxString
& filename
)
216 #ifdef __GNUWIN32__ // (fix a B20 bug)
217 if (GetFileAttributes(filename
) == 0xFFFFFFFF)
224 if (filename
&& stat ((char *)(const char *)filename
, &stbuf
) == 0)
230 /* Vadim's alternative implementation
232 // does the file exist?
233 bool wxFileExists(const char *pszFileName)
236 return !access(pszFileName, 0) &&
237 !stat(pszFileName, &st) &&
238 (st.st_mode & S_IFREG);
243 wxIsAbsolutePath (const wxString
& filename
)
247 if (filename
[0] == '/'
249 || (filename
[0] == '[' && filename
[1] != '.')
253 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
262 * Strip off any extension (dot something) from end of file,
263 * IF one exists. Inserts zero into buffer.
267 void wxStripExtension(char *buffer
)
269 int len
= strlen(buffer
);
273 if (buffer
[i
] == '.')
282 void wxStripExtension(wxString
& buffer
)
284 size_t len
= buffer
.Length();
288 if (buffer
.GetChar(i
) == '.')
290 buffer
= buffer
.Left(i
);
297 // Destructive removal of /./ and /../ stuff
298 char *wxRealPath (char *path
)
301 static const char SEP
= '\\';
302 Unix2DosFilename(path
);
304 static const char SEP
= '/';
306 if (path
[0] && path
[1]) {
307 /* MATTHEW: special case "/./x" */
309 if (path
[2] == SEP
&& path
[1] == '.')
317 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
320 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
321 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
322 && (q
- 1 <= path
|| q
[-1] != SEP
))
331 /* Check that path[2] is NULL! */
332 else if (path
[1] == ':' && !path
[2])
341 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
350 char *wxCopyAbsolutePath(const wxString
& filename
)
353 return (char *) NULL
;
355 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
356 char buf
[_MAXPATHLEN
];
358 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
359 char ch
= buf
[strlen(buf
) - 1];
361 if (ch
!= '\\' && ch
!= '/')
367 strcat(buf
, wxBuffer
);
368 return copystring( wxRealPath(buf
) );
370 return copystring( wxBuffer
);
376 ~user/ => user's home dir
377 If the environment variable a = "foo" and b = "bar" then:
394 /* input name in name, pathname output to buf. */
396 char *wxExpandPath(char *buf
, const char *name
)
398 register char *d
, *s
, *nm
;
399 char lnm
[_MAXPATHLEN
];
402 // Some compilers don't like this line.
403 // const char trimchars[] = "\n \t";
412 const char SEP
= '\\';
414 const char SEP
= '/';
417 if (name
== NULL
|| *name
== '\0')
419 nm
= copystring(name
); // Make a scratch copy
422 /* Skip leading whitespace and cr */
423 while (strchr((char *)trimchars
, *nm
) != NULL
)
425 /* And strip off trailing whitespace and cr */
426 s
= nm
+ (q
= strlen(nm
)) - 1;
427 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
435 q
= nm
[0] == '\\' && nm
[1] == '~';
438 /* Expand inline environment variables */
439 while ((*d
++ = *s
)) {
442 if ((*(d
- 1) = *++s
)) {
450 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
455 register char *start
= d
;
456 register int braces
= (*s
== '{' || *s
== '(');
457 register char *value
;
459 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
464 value
= getenv(braces
? start
+ 1 : start
);
466 for ((d
= start
- 1); (*d
++ = *value
++););
474 /* Expand ~ and ~user */
477 if (nm
[0] == '~' && !q
)
480 if (nm
[1] == SEP
|| nm
[1] == 0)
482 if ((s
= wxGetUserHome("")) != NULL
) {
487 { /* ~user/filename */
490 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
491 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
492 was_sep
= (*s
== SEP
);
493 nnm
= *s
? s
+ 1 : s
;
495 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
496 if (was_sep
) /* replace only if it was there: */
507 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
509 while ('\0' != (*d
++ = *s
++))
512 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
516 while ((*d
++ = *s
++));
518 delete[] nm_tmp
; // clean up alloc
519 /* Now clean up the buffer */
520 return wxRealPath(buf
);
524 /* Contract Paths to be build upon an environment variable
527 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
529 The call wxExpandPath can convert these back!
532 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
534 static char dest
[_MAXPATHLEN
];
537 return (char *) NULL
;
539 strcpy (dest
, WXSTRINGCAST filename
);
541 Unix2DosFilename(dest
);
544 // Handle environment
545 char *val
= (char *) NULL
;
546 char *tcp
= (char *) NULL
;
547 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
548 (tcp
= strstr (dest
, val
)) != NULL
)
550 strcpy (wxBuffer
, tcp
+ strlen (val
));
553 strcpy (tcp
, WXSTRINGCAST envname
);
555 strcat (tcp
, wxBuffer
);
558 // Handle User's home (ignore root homes!)
560 if ((val
= wxGetUserHome (user
)) != NULL
&&
561 (len
= strlen(val
)) > 2 &&
562 strncmp(dest
, val
, len
) == 0)
564 strcpy(wxBuffer
, "~");
566 strcat(wxBuffer
, user
);
568 // strcat(wxBuffer, "\\");
570 // strcat(wxBuffer, "/");
572 strcat(wxBuffer
, dest
+ len
);
573 strcpy (dest
, wxBuffer
);
579 // Return just the filename, not the path
581 char *wxFileNameFromPath (char *path
)
587 tcp
= path
+ strlen (path
);
588 while (--tcp
>= path
)
590 if (*tcp
== '/' || *tcp
== '\\'
592 || *tcp
== ':' || *tcp
== ']')
599 if (isalpha (*path
) && *(path
+ 1) == ':')
606 wxString
wxFileNameFromPath (const wxString
& path1
)
611 char *path
= WXSTRINGCAST path1
;
614 tcp
= path
+ strlen (path
);
615 while (--tcp
>= path
)
617 if (*tcp
== '/' || *tcp
== '\\'
619 || *tcp
== ':' || *tcp
== ']')
623 return wxString(tcp
+ 1);
626 if (isalpha (*path
) && *(path
+ 1) == ':')
627 return wxString(path
+ 2);
633 // Return just the directory, or NULL if no directory
635 wxPathOnly (char *path
)
639 static char buf
[_MAXPATHLEN
];
644 int l
= strlen(path
);
649 // Search backward for a backward or forward slash
650 while (!done
&& i
> -1)
653 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
668 // Try Drive specifier
669 if (isalpha (buf
[0]) && buf
[1] == ':')
671 // A:junk --> A:. (since A:.\junk Not A:\junk)
679 return (char *) NULL
;
682 // Return just the directory, or NULL if no directory
683 wxString
wxPathOnly (const wxString
& path
)
687 char buf
[_MAXPATHLEN
];
690 strcpy (buf
, WXSTRINGCAST path
);
692 int l
= path
.Length();
697 // Search backward for a backward or forward slash
698 while (!done
&& i
> -1)
701 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
710 return wxString(buf
);
716 // Try Drive specifier
717 if (isalpha (buf
[0]) && buf
[1] == ':')
719 // A:junk --> A:. (since A:.\junk Not A:\junk)
722 return wxString(buf
);
730 // Utility for converting delimiters in DOS filenames to UNIX style
731 // and back again - or we get nasty problems with delimiters.
732 // Also, convert to lower case, since case is significant in UNIX.
735 wxDos2UnixFilename (char *s
)
744 *s
= wxToLower (*s
); // Case INDEPENDENT
752 wxUnix2DosFilename (char *s
)
754 wxUnix2DosFilename (char *WXUNUSED(s
))
757 // Yes, I really mean this to happen under DOS only! JACS
769 // Concatenate two files to form third
771 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
773 char *outfile
= wxGetTempFileName("cat");
775 FILE *fp1
= (FILE *) NULL
;
776 FILE *fp2
= (FILE *) NULL
;
777 FILE *fp3
= (FILE *) NULL
;
778 // Open the inputs and outputs
779 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
780 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
781 (fp3
= fopen (outfile
, "wb")) == NULL
)
793 while ((ch
= getc (fp1
)) != EOF
)
794 (void) putc (ch
, fp3
);
797 while ((ch
= getc (fp2
)) != EOF
)
798 (void) putc (ch
, fp3
);
802 bool result
= wxRenameFile(outfile
, file3
);
809 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
815 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
817 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
823 while ((ch
= getc (fd1
)) != EOF
)
824 (void) putc (ch
, fd2
);
832 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
834 // Normal system call
835 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
838 if (wxCopyFile(file1
, file2
)) {
846 bool wxRemoveFile(const wxString
& file
)
848 #if defined(_MSC_VER) || defined(__BORLANDC__)
849 int flag
= remove(WXSTRINGCAST file
);
851 int flag
= unlink(WXSTRINGCAST file
);
856 bool wxMkdir(const wxString
& dir
)
858 #if defined(__WXSTUBS__)
860 #elif defined(__VMS__)
862 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
863 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
865 return (mkdir(WXSTRINGCAST dir
) == 0);
869 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
874 return (rmdir(WXSTRINGCAST dir
) == 0);
879 bool wxDirExists(const wxString
& dir
)
883 #elif !defined(__WXMSW__)
885 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
888 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
889 #if defined(__WIN32__)
890 WIN32_FIND_DATA fileInfo
;
893 struct ffblk fileInfo
;
895 struct find_t fileInfo
;
899 #if defined(__WIN32__)
900 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
902 if (h
==INVALID_HANDLE_VALUE
)
906 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
909 // In Borland findfirst has a different argument
910 // ordering from _dos_findfirst. But _dos_findfirst
911 // _should_ be ok in both MS and Borland... why not?
913 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
915 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
924 // does the path exists? (may have or not '/' or '\\' at the end)
925 bool wxPathExists(const char *pszPathName
)
927 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
928 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
929 wxString
strPath(pszPathName
);
930 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
931 strPath
.Last() = '\0';
934 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
937 // Get a temporary filename, opening and closing the file.
938 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
944 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
947 char tmpPath
[MAX_PATH
];
948 ::GetTempPath(MAX_PATH
, tmpPath
);
949 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
951 if (buf
) strcpy(buf
, tmp
);
952 else buf
= copystring(tmp
);
956 static short last_temp
= 0; // cache last to speed things a bit
957 // At most 1000 temp files to a process! We use a ring count.
958 char tmp
[100]; // FIXME static buffer
960 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
962 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
963 if (!wxFileExists( tmp
))
965 // Touch the file to create it (reserve name)
966 FILE *fd
= fopen (tmp
, "w");
973 buf
= copystring( tmp
);
977 cerr
<< _("wxWindows: error finding temporary file name.\n");
979 return (char *) NULL
;
983 // Get first file name matching given wild card.
987 // Get first file name matching given wild card.
988 // Flags are reserved for future use.
991 static DIR *wxDirStream
= (DIR *) NULL
;
992 static char *wxFileSpec
= (char *) NULL
;
993 static int wxFindFileFlags
= 0;
996 char *wxFindFirstFile(const char *spec
, int flags
)
1000 closedir(wxDirStream
); // edz 941103: better housekeping
1002 wxFindFileFlags
= flags
;
1005 delete[] wxFileSpec
;
1006 wxFileSpec
= copystring(spec
);
1008 // Find path only so we can concatenate
1009 // found file onto path
1010 char *p
= wxPathOnly(wxFileSpec
);
1012 /* MATTHEW: special case: path is really "/" */
1013 if (p
&& !*p
&& *wxFileSpec
== '/')
1015 /* MATTHEW: p is NULL => Local directory */
1019 if ((wxDirStream
=opendir(p
))==NULL
)
1020 return (char *) NULL
;
1022 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1023 return wxFindNextFile();
1026 return (char *) NULL
;
1029 char *wxFindNextFile(void)
1032 static char buf
[400]; // FIXME static buffer
1034 /* MATTHEW: [2] Don't crash if we read too many times */
1036 return (char *) NULL
;
1038 // Find path only so we can concatenate
1039 // found file onto path
1040 char *p
= wxPathOnly(wxFileSpec
);
1041 char *n
= wxFileNameFromPath(wxFileSpec
);
1043 /* MATTHEW: special case: path is really "/" */
1044 if (p
&& !*p
&& *wxFileSpec
== '/')
1048 struct dirent
*nextDir
;
1049 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1052 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1053 directories when flags & wxDIR */
1054 if (wxMatchWild(n
, nextDir
->d_name
)) {
1060 if (strcmp(p
, "/") != 0)
1063 strcat(buf
, nextDir
->d_name
);
1065 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1066 (strcmp(nextDir
->d_name
, "..") == 0)) {
1067 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1071 isdir
= wxDirExists(buf
);
1073 if (!wxFindFileFlags
1074 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1075 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1079 closedir(wxDirStream
);
1080 wxDirStream
= (DIR *) NULL
;
1084 return (char *) NULL
;
1087 #elif defined(__WXMSW__)
1090 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1091 WIN32_FIND_DATA wxFileStruc
;
1094 static struct ffblk wxFileStruc
;
1096 static struct _find_t wxFileStruc
;
1099 static wxString wxFileSpec
= "";
1100 static int wxFindFileFlags
;
1102 char *wxFindFirstFile(const char *spec
, int flags
)
1105 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1107 // Find path only so we can concatenate
1108 // found file onto path
1109 wxString
path1(wxFileSpec
);
1110 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1111 if (p
&& (strlen(p
) > 0))
1112 strcpy(wxBuffer
, p
);
1117 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1118 FindClose(wxFileStrucHandle
);
1120 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1122 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1125 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1127 if (isdir
&& !(flags
& wxDIR
))
1128 return wxFindNextFile();
1129 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1130 return wxFindNextFile();
1132 if (wxBuffer
[0] != 0)
1133 strcat(wxBuffer
, "\\");
1134 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1138 int flag
= _A_NORMAL
;
1139 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1143 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1145 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1148 /* MATTHEW: [5] Check directory flag */
1152 attrib
= wxFileStruc
.ff_attrib
;
1154 attrib
= wxFileStruc
.attrib
;
1157 if (attrib
& _A_SUBDIR
) {
1158 if (!(wxFindFileFlags
& wxDIR
))
1159 return wxFindNextFile();
1160 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1161 return wxFindNextFile();
1163 if (wxBuffer
[0] != 0)
1164 strcat(wxBuffer
, "\\");
1167 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1169 strcat(wxBuffer
, wxFileStruc
.name
);
1178 char *wxFindNextFile(void)
1180 // Find path only so we can concatenate
1181 // found file onto path
1182 wxString
p2(wxFileSpec
);
1183 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1184 if (p
&& (strlen(p
) > 0))
1185 strcpy(wxBuffer
, p
);
1192 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1195 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1197 FindClose(wxFileStrucHandle
);
1198 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1202 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1204 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1206 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1209 if (wxBuffer
[0] != 0)
1210 strcat(wxBuffer
, "\\");
1211 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1216 if (findnext(&wxFileStruc
) == 0)
1218 if (_dos_findnext(&wxFileStruc
) == 0)
1221 /* MATTHEW: [5] Check directory flag */
1225 attrib
= wxFileStruc
.ff_attrib
;
1227 attrib
= wxFileStruc
.attrib
;
1230 if (attrib
& _A_SUBDIR
) {
1231 if (!(wxFindFileFlags
& wxDIR
))
1233 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1237 if (wxBuffer
[0] != 0)
1238 strcat(wxBuffer
, "\\");
1240 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1242 strcat(wxBuffer
, wxFileStruc
.name
);
1254 // Get current working directory.
1255 // If buf is NULL, allocates space using new, else
1257 char *wxGetWorkingDirectory(char *buf
, int sz
)
1260 buf
= new char[sz
+1];
1262 if (_getcwd(buf
, sz
) == NULL
) {
1264 if (getcwd(buf
, sz
) == NULL
) {
1272 bool wxSetWorkingDirectory(const wxString
& d
)
1275 return (chdir(d
) == 0);
1276 #elif defined(__WINDOWS__)
1279 return (bool)(SetCurrentDirectory(d
) != 0);
1281 // Must change drive, too.
1282 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1285 char firstChar
= d
[0];
1289 firstChar
= firstChar
- 32;
1291 // To a drive number
1292 unsigned int driveNo
= firstChar
- 64;
1295 unsigned int noDrives
;
1296 _dos_setdrive(driveNo
, &noDrives
);
1299 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1307 bool wxEndsWithPathSeparator(const char *pszFileName
)
1309 size_t len
= Strlen(pszFileName
);
1313 return wxIsPathSeparator(pszFileName
[len
- 1]);
1316 // find a file in a list of directories, returns false if not found
1317 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1319 // we assume that it's not empty
1320 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1321 _("empty file name in wxFindFileInPath"));
1323 // skip path separator in the beginning of the file name if present
1324 if ( wxIsPathSeparator(*pszFile
) )
1327 // copy the path (strtok will modify it)
1328 char *szPath
= new char[strlen(pszPath
) + 1];
1329 strcpy(szPath
, pszPath
);
1333 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1334 // search for the file in this directory
1336 if ( !wxEndsWithPathSeparator(pc
) )
1337 strFile
+= FILE_SEP_PATH
;
1340 if ( FileExists(strFile
) ) {
1348 return pc
!= NULL
; // if true => we breaked from the loop
1351 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1356 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1358 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1359 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1360 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1362 // take the last of the two
1363 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1364 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1365 if ( nPosDos
> nPosUnix
)
1367 // size_t nLen = Strlen(pszFileName);
1370 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1372 size_t nPosDot
= pDot
- pszFileName
;
1374 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1376 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1380 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1386 //------------------------------------------------------------------------
1387 // wild character routines
1388 //------------------------------------------------------------------------
1390 bool wxIsWild( const wxString
& pattern
)
1392 wxString tmp
= pattern
;
1393 char *pat
= WXSTRINGCAST(tmp
);
1396 case '?': case '*': case '[': case '{':
1406 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1407 #ifdef HAVE_FNMATCH_H
1410 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1412 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1416 #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1418 * WARNING: this code is broken!
1421 wxString tmp1
= pat
;
1422 char *pattern
= WXSTRINGCAST(tmp1
);
1423 wxString tmp2
= text
;
1424 char *str
= WXSTRINGCAST(tmp2
);
1427 bool done
= FALSE
, ret_code
, ok
;
1428 // Below is for vi fans
1429 const char OB
= '{', CB
= '}';
1431 // dot_special means '.' only matches '.'
1432 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1435 while ((*pattern
!= '\0') && (!done
)
1436 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1440 if (*pattern
!= '\0')
1447 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1450 while (*str
!= '\0')
1452 while (*pattern
!= '\0')
1459 if ((*pattern
== '\0') || (*pattern
== ']')) {
1463 if (*pattern
== '\\') {
1465 if (*pattern
== '\0') {
1470 if (*(pattern
+ 1) == '-') {
1473 if (*pattern
== ']') {
1477 if (*pattern
== '\\') {
1479 if (*pattern
== '\0') {
1484 if ((*str
< c
) || (*str
> *pattern
)) {
1488 } else if (*pattern
!= *str
) {
1493 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1494 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1498 if (*pattern
!= '\0') {
1508 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1511 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1512 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1513 if (*pattern
== '\\')
1515 ok
= (*pattern
++ == *cp
++);
1517 if (*pattern
== '\0') {
1523 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1524 if (*++pattern
== '\\') {
1525 if (*++pattern
== CB
)
1530 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1531 if (*++pattern
== '\\') {
1532 if (*++pattern
== CB
|| *pattern
== ',')
1537 if (*pattern
!= '\0')
1542 if (*str
== *pattern
) {
1549 while (*pattern
== '*')
1551 return ((*str
== '\0') && (*pattern
== '\0'));
1557 #pragma warning(default:4706) // assignment within conditional expression