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.
74 // No, Cygwin doesn't appear to have fnmatch.h after all.
75 #if defined(HAVE_FNMATCH_H)
83 #define _MAXPATHLEN 500
85 extern char *wxBuffer
;
87 #if !USE_SHARED_LIBRARIES
88 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
91 void wxPathList::Add (const wxString
& path
)
93 wxStringList::Add ((char *)(const char *)path
);
96 // Add paths e.g. from the PATH environment variable
97 void wxPathList::AddEnvList (const wxString
& envVariable
)
99 static const char PATH_TOKS
[] =
101 " ;"; // Don't seperate with colon in DOS (used for drive)
106 char *val
= getenv (WXSTRINGCAST envVariable
);
109 char *s
= copystring (val
);
110 char *token
= strtok (s
, PATH_TOKS
);
114 Add (copystring (token
));
117 if ((token
= strtok ((char *) NULL
, PATH_TOKS
)) != NULL
)
118 Add (wxString(token
));
125 // Given a full filename (with path), ensure that that file can
126 // be accessed again USING FILENAME ONLY by adding the path
127 // to the list if not already there.
128 void wxPathList::EnsureFileAccessible (const wxString
& path
)
130 wxString
path1(path
);
131 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
134 if (!Member (wxString(path_only
)))
135 Add (wxString(path_only
));
139 bool wxPathList::Member (const wxString
& path
)
141 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
143 wxString
path2((char *) node
->Data ());
145 #if defined(__WINDOWS__) || defined(__VMS__)
147 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
149 // Case sensitive File System
150 path
.CompareTo (path2
) == 0
158 wxString
wxPathList::FindValidPath (const wxString
& file
)
160 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
161 return wxString(wxBuffer
);
163 char buf
[_MAXPATHLEN
];
164 strcpy(buf
, wxBuffer
);
166 char *filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
168 for (wxNode
* node
= First (); node
; node
= node
->Next ())
170 char *path
= (char *) node
->Data ();
171 strcpy (wxBuffer
, path
);
172 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
173 if (ch
!= '\\' && ch
!= '/')
174 strcat (wxBuffer
, "/");
175 strcat (wxBuffer
, filename
);
177 Unix2DosFilename (wxBuffer
);
179 if (wxFileExists (wxBuffer
))
181 return wxString(wxBuffer
); // Found!
185 return wxString(""); // Not found
188 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
190 wxString f
= FindValidPath(file
);
191 if (wxIsAbsolutePath(f
))
196 wxGetWorkingDirectory(buf
, 499);
197 int len
= (int)strlen(buf
);
201 if (lastCh
!= '/' && lastCh
!= '\\')
209 strcat(buf
, (const char *)f
);
210 strcpy(wxBuffer
, buf
);
211 return wxString(wxBuffer
);
216 wxFileExists (const wxString
& filename
)
218 #ifdef __GNUWIN32__ // (fix a B20 bug)
219 if (GetFileAttributes(filename
) == 0xFFFFFFFF)
226 if (filename
&& stat ((char *)(const char *)filename
, &stbuf
) == 0)
232 /* Vadim's alternative implementation
234 // does the file exist?
235 bool wxFileExists(const char *pszFileName)
238 return !access(pszFileName, 0) &&
239 !stat(pszFileName, &st) &&
240 (st.st_mode & S_IFREG);
245 wxIsAbsolutePath (const wxString
& filename
)
249 if (filename
[0] == '/'
251 || (filename
[0] == '[' && filename
[1] != '.')
255 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
264 * Strip off any extension (dot something) from end of file,
265 * IF one exists. Inserts zero into buffer.
269 void wxStripExtension(char *buffer
)
271 int len
= strlen(buffer
);
275 if (buffer
[i
] == '.')
284 void wxStripExtension(wxString
& buffer
)
286 size_t len
= buffer
.Length();
290 if (buffer
.GetChar(i
) == '.')
292 buffer
= buffer
.Left(i
);
299 // Destructive removal of /./ and /../ stuff
300 char *wxRealPath (char *path
)
303 static const char SEP
= '\\';
304 Unix2DosFilename(path
);
306 static const char SEP
= '/';
308 if (path
[0] && path
[1]) {
309 /* MATTHEW: special case "/./x" */
311 if (path
[2] == SEP
&& path
[1] == '.')
319 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
322 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
323 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
324 && (q
- 1 <= path
|| q
[-1] != SEP
))
333 /* Check that path[2] is NULL! */
334 else if (path
[1] == ':' && !path
[2])
343 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
352 char *wxCopyAbsolutePath(const wxString
& filename
)
355 return (char *) NULL
;
357 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
358 char buf
[_MAXPATHLEN
];
360 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
361 char ch
= buf
[strlen(buf
) - 1];
363 if (ch
!= '\\' && ch
!= '/')
369 strcat(buf
, wxBuffer
);
370 return copystring( wxRealPath(buf
) );
372 return copystring( wxBuffer
);
378 ~user/ => user's home dir
379 If the environment variable a = "foo" and b = "bar" then:
396 /* input name in name, pathname output to buf. */
398 char *wxExpandPath(char *buf
, const char *name
)
400 register char *d
, *s
, *nm
;
401 char lnm
[_MAXPATHLEN
];
404 // Some compilers don't like this line.
405 // const char trimchars[] = "\n \t";
414 const char SEP
= '\\';
416 const char SEP
= '/';
419 if (name
== NULL
|| *name
== '\0')
421 nm
= copystring(name
); // Make a scratch copy
424 /* Skip leading whitespace and cr */
425 while (strchr((char *)trimchars
, *nm
) != NULL
)
427 /* And strip off trailing whitespace and cr */
428 s
= nm
+ (q
= strlen(nm
)) - 1;
429 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
437 q
= nm
[0] == '\\' && nm
[1] == '~';
440 /* Expand inline environment variables */
441 while ((*d
++ = *s
)) {
444 if ((*(d
- 1) = *++s
)) {
452 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
457 register char *start
= d
;
458 register int braces
= (*s
== '{' || *s
== '(');
459 register char *value
;
461 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
466 value
= getenv(braces
? start
+ 1 : start
);
468 for ((d
= start
- 1); (*d
++ = *value
++););
476 /* Expand ~ and ~user */
479 if (nm
[0] == '~' && !q
)
482 if (nm
[1] == SEP
|| nm
[1] == 0)
484 if ((s
= wxGetUserHome("")) != NULL
) {
489 { /* ~user/filename */
492 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
493 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
494 was_sep
= (*s
== SEP
);
495 nnm
= *s
? s
+ 1 : s
;
497 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
498 if (was_sep
) /* replace only if it was there: */
509 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
511 while ('\0' != (*d
++ = *s
++))
514 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
518 while ((*d
++ = *s
++));
520 delete[] nm_tmp
; // clean up alloc
521 /* Now clean up the buffer */
522 return wxRealPath(buf
);
526 /* Contract Paths to be build upon an environment variable
529 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
531 The call wxExpandPath can convert these back!
534 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
536 static char dest
[_MAXPATHLEN
];
539 return (char *) NULL
;
541 strcpy (dest
, WXSTRINGCAST filename
);
543 Unix2DosFilename(dest
);
546 // Handle environment
547 char *val
= (char *) NULL
;
548 char *tcp
= (char *) NULL
;
549 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
550 (tcp
= strstr (dest
, val
)) != NULL
)
552 strcpy (wxBuffer
, tcp
+ strlen (val
));
555 strcpy (tcp
, WXSTRINGCAST envname
);
557 strcat (tcp
, wxBuffer
);
560 // Handle User's home (ignore root homes!)
562 if ((val
= wxGetUserHome (user
)) != NULL
&&
563 (len
= strlen(val
)) > 2 &&
564 strncmp(dest
, val
, len
) == 0)
566 strcpy(wxBuffer
, "~");
568 strcat(wxBuffer
, user
);
570 // strcat(wxBuffer, "\\");
572 // strcat(wxBuffer, "/");
574 strcat(wxBuffer
, dest
+ len
);
575 strcpy (dest
, wxBuffer
);
581 // Return just the filename, not the path
583 char *wxFileNameFromPath (char *path
)
589 tcp
= path
+ strlen (path
);
590 while (--tcp
>= path
)
592 if (*tcp
== '/' || *tcp
== '\\'
594 || *tcp
== ':' || *tcp
== ']')
601 if (isalpha (*path
) && *(path
+ 1) == ':')
608 wxString
wxFileNameFromPath (const wxString
& path1
)
613 char *path
= WXSTRINGCAST path1
;
616 tcp
= path
+ strlen (path
);
617 while (--tcp
>= path
)
619 if (*tcp
== '/' || *tcp
== '\\'
621 || *tcp
== ':' || *tcp
== ']')
625 return wxString(tcp
+ 1);
628 if (isalpha (*path
) && *(path
+ 1) == ':')
629 return wxString(path
+ 2);
635 // Return just the directory, or NULL if no directory
637 wxPathOnly (char *path
)
641 static char buf
[_MAXPATHLEN
];
646 int l
= strlen(path
);
651 // Search backward for a backward or forward slash
652 while (!done
&& i
> -1)
655 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
670 // Try Drive specifier
671 if (isalpha (buf
[0]) && buf
[1] == ':')
673 // A:junk --> A:. (since A:.\junk Not A:\junk)
681 return (char *) NULL
;
684 // Return just the directory, or NULL if no directory
685 wxString
wxPathOnly (const wxString
& path
)
689 char buf
[_MAXPATHLEN
];
692 strcpy (buf
, WXSTRINGCAST path
);
694 int l
= path
.Length();
699 // Search backward for a backward or forward slash
700 while (!done
&& i
> -1)
703 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
712 return wxString(buf
);
718 // Try Drive specifier
719 if (isalpha (buf
[0]) && buf
[1] == ':')
721 // A:junk --> A:. (since A:.\junk Not A:\junk)
724 return wxString(buf
);
732 // Utility for converting delimiters in DOS filenames to UNIX style
733 // and back again - or we get nasty problems with delimiters.
734 // Also, convert to lower case, since case is significant in UNIX.
737 wxDos2UnixFilename (char *s
)
746 *s
= wxToLower (*s
); // Case INDEPENDENT
754 wxUnix2DosFilename (char *s
)
756 wxUnix2DosFilename (char *WXUNUSED(s
))
759 // Yes, I really mean this to happen under DOS only! JACS
771 // Concatenate two files to form third
773 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
775 char *outfile
= wxGetTempFileName("cat");
777 FILE *fp1
= (FILE *) NULL
;
778 FILE *fp2
= (FILE *) NULL
;
779 FILE *fp3
= (FILE *) NULL
;
780 // Open the inputs and outputs
781 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
782 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
783 (fp3
= fopen (outfile
, "wb")) == NULL
)
795 while ((ch
= getc (fp1
)) != EOF
)
796 (void) putc (ch
, fp3
);
799 while ((ch
= getc (fp2
)) != EOF
)
800 (void) putc (ch
, fp3
);
804 bool result
= wxRenameFile(outfile
, file3
);
811 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
817 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
819 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
825 while ((ch
= getc (fd1
)) != EOF
)
826 (void) putc (ch
, fd2
);
834 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
836 // Normal system call
837 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
840 if (wxCopyFile(file1
, file2
)) {
848 bool wxRemoveFile(const wxString
& file
)
850 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
851 int flag
= remove(WXSTRINGCAST file
);
853 int flag
= unlink(WXSTRINGCAST file
);
858 bool wxMkdir(const wxString
& dir
)
860 #if defined(__WXSTUBS__)
862 #elif defined(__VMS__)
864 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
865 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
867 return (mkdir(WXSTRINGCAST dir
) == 0);
871 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
876 return (rmdir(WXSTRINGCAST dir
) == 0);
881 bool wxDirExists(const wxString
& dir
)
885 #elif !defined(__WXMSW__)
887 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
890 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
891 #if defined(__WIN32__)
892 WIN32_FIND_DATA fileInfo
;
895 struct ffblk fileInfo
;
897 struct find_t fileInfo
;
901 #if defined(__WIN32__)
902 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
904 if (h
==INVALID_HANDLE_VALUE
)
908 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
911 // In Borland findfirst has a different argument
912 // ordering from _dos_findfirst. But _dos_findfirst
913 // _should_ be ok in both MS and Borland... why not?
915 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
917 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
926 // does the path exists? (may have or not '/' or '\\' at the end)
927 bool wxPathExists(const char *pszPathName
)
929 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
930 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
931 wxString
strPath(pszPathName
);
932 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
933 strPath
.Last() = '\0';
936 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
939 // Get a temporary filename, opening and closing the file.
940 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
946 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
949 char tmpPath
[MAX_PATH
];
950 ::GetTempPath(MAX_PATH
, tmpPath
);
951 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
953 if (buf
) strcpy(buf
, tmp
);
954 else buf
= copystring(tmp
);
958 static short last_temp
= 0; // cache last to speed things a bit
959 // At most 1000 temp files to a process! We use a ring count.
960 char tmp
[100]; // FIXME static buffer
962 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
964 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
965 if (!wxFileExists( tmp
))
967 // Touch the file to create it (reserve name)
968 FILE *fd
= fopen (tmp
, "w");
975 buf
= copystring( tmp
);
979 cerr
<< _("wxWindows: error finding temporary file name.\n");
981 return (char *) NULL
;
985 // Get first file name matching given wild card.
989 // Get first file name matching given wild card.
990 // Flags are reserved for future use.
993 static DIR *wxDirStream
= (DIR *) NULL
;
994 static char *wxFileSpec
= (char *) NULL
;
995 static int wxFindFileFlags
= 0;
998 char *wxFindFirstFile(const char *spec
, int flags
)
1002 closedir(wxDirStream
); // edz 941103: better housekeping
1004 wxFindFileFlags
= flags
;
1007 delete[] wxFileSpec
;
1008 wxFileSpec
= copystring(spec
);
1010 // Find path only so we can concatenate
1011 // found file onto path
1012 char *p
= wxPathOnly(wxFileSpec
);
1014 /* MATTHEW: special case: path is really "/" */
1015 if (p
&& !*p
&& *wxFileSpec
== '/')
1017 /* MATTHEW: p is NULL => Local directory */
1021 if ((wxDirStream
=opendir(p
))==NULL
)
1022 return (char *) NULL
;
1024 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1025 return wxFindNextFile();
1028 return (char *) NULL
;
1031 char *wxFindNextFile(void)
1034 static char buf
[400]; // FIXME static buffer
1036 /* MATTHEW: [2] Don't crash if we read too many times */
1038 return (char *) NULL
;
1040 // Find path only so we can concatenate
1041 // found file onto path
1042 char *p
= wxPathOnly(wxFileSpec
);
1043 char *n
= wxFileNameFromPath(wxFileSpec
);
1045 /* MATTHEW: special case: path is really "/" */
1046 if (p
&& !*p
&& *wxFileSpec
== '/')
1050 struct dirent
*nextDir
;
1051 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1054 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1055 directories when flags & wxDIR */
1056 if (wxMatchWild(n
, nextDir
->d_name
)) {
1062 if (strcmp(p
, "/") != 0)
1065 strcat(buf
, nextDir
->d_name
);
1067 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1068 (strcmp(nextDir
->d_name
, "..") == 0)) {
1069 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1073 isdir
= wxDirExists(buf
);
1075 if (!wxFindFileFlags
1076 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1077 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1081 closedir(wxDirStream
);
1082 wxDirStream
= (DIR *) NULL
;
1086 return (char *) NULL
;
1089 #elif defined(__WXMSW__)
1092 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1093 WIN32_FIND_DATA wxFileStruc
;
1096 static struct ffblk wxFileStruc
;
1098 static struct _find_t wxFileStruc
;
1101 static wxString wxFileSpec
= "";
1102 static int wxFindFileFlags
;
1104 char *wxFindFirstFile(const char *spec
, int flags
)
1107 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1109 // Find path only so we can concatenate
1110 // found file onto path
1111 wxString
path1(wxFileSpec
);
1112 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1113 if (p
&& (strlen(p
) > 0))
1114 strcpy(wxBuffer
, p
);
1119 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1120 FindClose(wxFileStrucHandle
);
1122 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1124 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1127 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1129 if (isdir
&& !(flags
& wxDIR
))
1130 return wxFindNextFile();
1131 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1132 return wxFindNextFile();
1134 if (wxBuffer
[0] != 0)
1135 strcat(wxBuffer
, "\\");
1136 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1140 int flag
= _A_NORMAL
;
1141 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1145 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1147 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1150 /* MATTHEW: [5] Check directory flag */
1154 attrib
= wxFileStruc
.ff_attrib
;
1156 attrib
= wxFileStruc
.attrib
;
1159 if (attrib
& _A_SUBDIR
) {
1160 if (!(wxFindFileFlags
& wxDIR
))
1161 return wxFindNextFile();
1162 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1163 return wxFindNextFile();
1165 if (wxBuffer
[0] != 0)
1166 strcat(wxBuffer
, "\\");
1169 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1171 strcat(wxBuffer
, wxFileStruc
.name
);
1180 char *wxFindNextFile(void)
1182 // Find path only so we can concatenate
1183 // found file onto path
1184 wxString
p2(wxFileSpec
);
1185 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1186 if (p
&& (strlen(p
) > 0))
1187 strcpy(wxBuffer
, p
);
1194 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1197 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1199 FindClose(wxFileStrucHandle
);
1200 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1204 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1206 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1208 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1211 if (wxBuffer
[0] != 0)
1212 strcat(wxBuffer
, "\\");
1213 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1218 if (findnext(&wxFileStruc
) == 0)
1220 if (_dos_findnext(&wxFileStruc
) == 0)
1223 /* MATTHEW: [5] Check directory flag */
1227 attrib
= wxFileStruc
.ff_attrib
;
1229 attrib
= wxFileStruc
.attrib
;
1232 if (attrib
& _A_SUBDIR
) {
1233 if (!(wxFindFileFlags
& wxDIR
))
1235 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1239 if (wxBuffer
[0] != 0)
1240 strcat(wxBuffer
, "\\");
1242 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1244 strcat(wxBuffer
, wxFileStruc
.name
);
1256 // Get current working directory.
1257 // If buf is NULL, allocates space using new, else
1259 char *wxGetWorkingDirectory(char *buf
, int sz
)
1262 buf
= new char[sz
+1];
1264 if (_getcwd(buf
, sz
) == NULL
) {
1266 if (getcwd(buf
, sz
) == NULL
) {
1274 bool wxSetWorkingDirectory(const wxString
& d
)
1277 return (chdir(d
) == 0);
1278 #elif defined(__WINDOWS__)
1281 return (bool)(SetCurrentDirectory(d
) != 0);
1283 // Must change drive, too.
1284 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1287 char firstChar
= d
[0];
1291 firstChar
= firstChar
- 32;
1293 // To a drive number
1294 unsigned int driveNo
= firstChar
- 64;
1297 unsigned int noDrives
;
1298 _dos_setdrive(driveNo
, &noDrives
);
1301 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1309 bool wxEndsWithPathSeparator(const char *pszFileName
)
1311 size_t len
= Strlen(pszFileName
);
1315 return wxIsPathSeparator(pszFileName
[len
- 1]);
1318 // find a file in a list of directories, returns false if not found
1319 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1321 // we assume that it's not empty
1322 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1323 _("empty file name in wxFindFileInPath"));
1325 // skip path separator in the beginning of the file name if present
1326 if ( wxIsPathSeparator(*pszFile
) )
1329 // copy the path (strtok will modify it)
1330 char *szPath
= new char[strlen(pszPath
) + 1];
1331 strcpy(szPath
, pszPath
);
1335 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1336 // search for the file in this directory
1338 if ( !wxEndsWithPathSeparator(pc
) )
1339 strFile
+= FILE_SEP_PATH
;
1342 if ( FileExists(strFile
) ) {
1350 return pc
!= NULL
; // if true => we breaked from the loop
1353 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1358 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1360 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1361 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1362 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1364 // take the last of the two
1365 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1366 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1367 if ( nPosDos
> nPosUnix
)
1369 // size_t nLen = Strlen(pszFileName);
1372 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1374 size_t nPosDot
= pDot
- pszFileName
;
1376 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1378 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1382 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1388 //------------------------------------------------------------------------
1389 // wild character routines
1390 //------------------------------------------------------------------------
1392 bool wxIsWild( const wxString
& pattern
)
1394 wxString tmp
= pattern
;
1395 char *pat
= WXSTRINGCAST(tmp
);
1398 case '?': case '*': case '[': case '{':
1408 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1410 #if defined(HAVE_FNMATCH_H)
1413 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1415 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1419 // #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1422 * WARNING: this code is broken!
1425 wxString tmp1
= pat
;
1426 char *pattern
= WXSTRINGCAST(tmp1
);
1427 wxString tmp2
= text
;
1428 char *str
= WXSTRINGCAST(tmp2
);
1431 bool done
= FALSE
, ret_code
, ok
;
1432 // Below is for vi fans
1433 const char OB
= '{', CB
= '}';
1435 // dot_special means '.' only matches '.'
1436 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1439 while ((*pattern
!= '\0') && (!done
)
1440 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1444 if (*pattern
!= '\0')
1451 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1454 while (*str
!= '\0')
1456 while (*pattern
!= '\0')
1463 if ((*pattern
== '\0') || (*pattern
== ']')) {
1467 if (*pattern
== '\\') {
1469 if (*pattern
== '\0') {
1474 if (*(pattern
+ 1) == '-') {
1477 if (*pattern
== ']') {
1481 if (*pattern
== '\\') {
1483 if (*pattern
== '\0') {
1488 if ((*str
< c
) || (*str
> *pattern
)) {
1492 } else if (*pattern
!= *str
) {
1497 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1498 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1502 if (*pattern
!= '\0') {
1512 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1515 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1516 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1517 if (*pattern
== '\\')
1519 ok
= (*pattern
++ == *cp
++);
1521 if (*pattern
== '\0') {
1527 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1528 if (*++pattern
== '\\') {
1529 if (*++pattern
== CB
)
1534 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1535 if (*++pattern
== '\\') {
1536 if (*++pattern
== CB
|| *pattern
== ',')
1541 if (*pattern
!= '\0')
1546 if (*str
== *pattern
) {
1553 while (*pattern
== '*')
1555 return ((*str
== '\0') && (*pattern
== '\0'));
1561 #pragma warning(default:4706) // assignment within conditional expression