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"
35 #if !defined(__WATCOMC__)
36 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
41 #include <sys/types.h>
57 #include <sys/unistd.h>
58 #define stricmp strcasecmp
61 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
62 // this (3.1 I believe) and how to test for it.
63 // If this works for Borland 4.0 as well, then no worries.
76 #define _MAXPATHLEN 500
78 #if !USE_SHARED_LIBRARY
79 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
82 extern char *wxBuffer
;
84 void wxPathList::Add (const wxString
& path
)
86 wxStringList::Add ((char *)(const char *)path
);
89 // Add paths e.g. from the PATH environment variable
90 void wxPathList::AddEnvList (const wxString
& envVariable
)
92 static const char PATH_TOKS
[] =
94 " ;"; // Don't seperate with colon in DOS (used for drive)
99 char *val
= getenv (WXSTRINGCAST envVariable
);
102 char *s
= copystring (val
);
103 char *token
= strtok (s
, PATH_TOKS
);
107 Add (copystring (token
));
110 if ((token
= strtok ((char *) NULL
, PATH_TOKS
)) != NULL
)
111 Add (wxString(token
));
118 // Given a full filename (with path), ensure that that file can
119 // be accessed again USING FILENAME ONLY by adding the path
120 // to the list if not already there.
121 void wxPathList::EnsureFileAccessible (const wxString
& path
)
123 wxString
path1(path
);
124 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
127 if (!Member (wxString(path_only
)))
128 Add (wxString(path_only
));
132 bool wxPathList::Member (const wxString
& path
)
134 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
136 wxString
path2((char *) node
->Data ());
138 #if defined(__WINDOWS__) || defined(__VMS__)
140 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
142 // Case sensitive File System
143 path
.CompareTo (path2
) == 0
151 wxString
wxPathList::FindValidPath (const wxString
& file
)
153 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
154 return wxString(wxBuffer
);
156 char buf
[_MAXPATHLEN
];
157 strcpy(buf
, wxBuffer
);
159 char *filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
161 for (wxNode
* node
= First (); node
; node
= node
->Next ())
163 char *path
= (char *) node
->Data ();
164 strcpy (wxBuffer
, path
);
165 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
166 if (ch
!= '\\' && ch
!= '/')
167 strcat (wxBuffer
, "/");
168 strcat (wxBuffer
, filename
);
170 Unix2DosFilename (wxBuffer
);
172 if (wxFileExists (wxBuffer
))
174 return wxString(wxBuffer
); // Found!
178 return wxString(""); // Not found
181 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
183 wxString f
= FindValidPath(file
);
184 if (wxIsAbsolutePath(f
))
189 wxGetWorkingDirectory(buf
, 499);
190 int len
= (int)strlen(buf
);
194 if (lastCh
!= '/' && lastCh
!= '\\')
202 strcat(buf
, (const char *)f
);
203 strcpy(wxBuffer
, buf
);
204 return wxString(wxBuffer
);
209 wxFileExists (const wxString
& filename
)
211 #ifdef __GNUWIN32__ // (fix a B20 bug)
212 if (GetFileAttributes(filename
) == 0xFFFFFFFF)
219 if (filename
&& stat ((char *)(const char *)filename
, &stbuf
) == 0)
225 /* Vadim's alternative implementation
227 // does the file exist?
228 bool wxFileExists(const char *pszFileName)
231 return !access(pszFileName, 0) &&
232 !stat(pszFileName, &st) &&
233 (st.st_mode & S_IFREG);
238 wxIsAbsolutePath (const wxString
& filename
)
242 if (filename
[0] == '/'
244 || (filename
[0] == '[' && filename
[1] != '.')
248 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
257 * Strip off any extension (dot something) from end of file,
258 * IF one exists. Inserts zero into buffer.
262 void wxStripExtension(char *buffer
)
264 int len
= strlen(buffer
);
268 if (buffer
[i
] == '.')
277 void wxStripExtension(wxString
& buffer
)
279 size_t len
= buffer
.Length();
283 if (buffer
.GetChar(i
) == '.')
285 buffer
= buffer
.Left(i
);
292 // Destructive removal of /./ and /../ stuff
293 char *wxRealPath (char *path
)
296 static const char SEP
= '\\';
297 Unix2DosFilename(path
);
299 static const char SEP
= '/';
301 if (path
[0] && path
[1]) {
302 /* MATTHEW: special case "/./x" */
304 if (path
[2] == SEP
&& path
[1] == '.')
312 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
315 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
316 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
317 && (q
- 1 <= path
|| q
[-1] != SEP
))
326 /* Check that path[2] is NULL! */
327 else if (path
[1] == ':' && !path
[2])
336 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
345 char *wxCopyAbsolutePath(const wxString
& filename
)
348 return (char *) NULL
;
350 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
351 char buf
[_MAXPATHLEN
];
353 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
354 char ch
= buf
[strlen(buf
) - 1];
356 if (ch
!= '\\' && ch
!= '/')
362 strcat(buf
, wxBuffer
);
363 return copystring( wxRealPath(buf
) );
365 return copystring( wxBuffer
);
371 ~user/ => user's home dir
372 If the environment variable a = "foo" and b = "bar" then:
389 /* input name in name, pathname output to buf. */
391 char *wxExpandPath(char *buf
, const char *name
)
393 register char *d
, *s
, *nm
;
394 char lnm
[_MAXPATHLEN
];
397 // Some compilers don't like this line.
398 // const char trimchars[] = "\n \t";
407 const char SEP
= '\\';
409 const char SEP
= '/';
412 if (name
== NULL
|| *name
== '\0')
414 nm
= copystring(name
); // Make a scratch copy
417 /* Skip leading whitespace and cr */
418 while (strchr((char *)trimchars
, *nm
) != NULL
)
420 /* And strip off trailing whitespace and cr */
421 s
= nm
+ (q
= strlen(nm
)) - 1;
422 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
430 q
= nm
[0] == '\\' && nm
[1] == '~';
433 /* Expand inline environment variables */
434 while ((*d
++ = *s
)) {
437 if ((*(d
- 1) = *++s
)) {
445 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
450 register char *start
= d
;
451 register int braces
= (*s
== '{' || *s
== '(');
452 register char *value
;
454 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
459 value
= getenv(braces
? start
+ 1 : start
);
461 for ((d
= start
- 1); (*d
++ = *value
++););
469 /* Expand ~ and ~user */
472 if (nm
[0] == '~' && !q
)
475 if (nm
[1] == SEP
|| nm
[1] == 0)
477 if ((s
= wxGetUserHome("")) != NULL
) {
482 { /* ~user/filename */
485 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
486 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
487 was_sep
= (*s
== SEP
);
488 nnm
= *s
? s
+ 1 : s
;
490 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
491 if (was_sep
) /* replace only if it was there: */
502 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
504 while ('\0' != (*d
++ = *s
++))
507 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
511 while ((*d
++ = *s
++));
513 delete[] nm_tmp
; // clean up alloc
514 /* Now clean up the buffer */
515 return wxRealPath(buf
);
519 /* Contract Paths to be build upon an environment variable
522 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
524 The call wxExpandPath can convert these back!
527 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
529 static char dest
[_MAXPATHLEN
];
532 return (char *) NULL
;
534 strcpy (dest
, WXSTRINGCAST filename
);
536 Unix2DosFilename(dest
);
539 // Handle environment
540 char *val
= (char *) NULL
;
541 char *tcp
= (char *) NULL
;
542 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
543 (tcp
= strstr (dest
, val
)) != NULL
)
545 strcpy (wxBuffer
, tcp
+ strlen (val
));
548 strcpy (tcp
, WXSTRINGCAST envname
);
550 strcat (tcp
, wxBuffer
);
553 // Handle User's home (ignore root homes!)
555 if ((val
= wxGetUserHome (user
)) != NULL
&&
556 (len
= strlen(val
)) > 2 &&
557 strncmp(dest
, val
, len
) == 0)
559 strcpy(wxBuffer
, "~");
561 strcat(wxBuffer
, user
);
563 // strcat(wxBuffer, "\\");
565 // strcat(wxBuffer, "/");
567 strcat(wxBuffer
, dest
+ len
);
568 strcpy (dest
, wxBuffer
);
574 // Return just the filename, not the path
576 char *wxFileNameFromPath (char *path
)
582 tcp
= path
+ strlen (path
);
583 while (--tcp
>= path
)
585 if (*tcp
== '/' || *tcp
== '\\'
587 || *tcp
== ':' || *tcp
== ']')
594 if (isalpha (*path
) && *(path
+ 1) == ':')
601 wxString
wxFileNameFromPath (const wxString
& path1
)
606 char *path
= WXSTRINGCAST path1
;
609 tcp
= path
+ strlen (path
);
610 while (--tcp
>= path
)
612 if (*tcp
== '/' || *tcp
== '\\'
614 || *tcp
== ':' || *tcp
== ']')
618 return wxString(tcp
+ 1);
621 if (isalpha (*path
) && *(path
+ 1) == ':')
622 return wxString(path
+ 2);
628 // Return just the directory, or NULL if no directory
630 wxPathOnly (char *path
)
634 static char buf
[_MAXPATHLEN
];
639 int l
= strlen(path
);
644 // Search backward for a backward or forward slash
645 while (!done
&& i
> -1)
648 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
663 // Try Drive specifier
664 if (isalpha (buf
[0]) && buf
[1] == ':')
666 // A:junk --> A:. (since A:.\junk Not A:\junk)
674 return (char *) NULL
;
677 // Return just the directory, or NULL if no directory
678 wxString
wxPathOnly (const wxString
& path
)
682 char buf
[_MAXPATHLEN
];
685 strcpy (buf
, WXSTRINGCAST path
);
687 int l
= path
.Length();
692 // Search backward for a backward or forward slash
693 while (!done
&& i
> -1)
696 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
705 return wxString(buf
);
711 // Try Drive specifier
712 if (isalpha (buf
[0]) && buf
[1] == ':')
714 // A:junk --> A:. (since A:.\junk Not A:\junk)
717 return wxString(buf
);
725 // Utility for converting delimiters in DOS filenames to UNIX style
726 // and back again - or we get nasty problems with delimiters.
727 // Also, convert to lower case, since case is significant in UNIX.
730 wxDos2UnixFilename (char *s
)
739 *s
= wxToLower (*s
); // Case INDEPENDENT
747 wxUnix2DosFilename (char *s
)
749 wxUnix2DosFilename (char *WXUNUSED(s
))
752 // Yes, I really mean this to happen under DOS only! JACS
764 // Concatenate two files to form third
766 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
768 char *outfile
= wxGetTempFileName("cat");
770 FILE *fp1
= (FILE *) NULL
;
771 FILE *fp2
= (FILE *) NULL
;
772 FILE *fp3
= (FILE *) NULL
;
773 // Open the inputs and outputs
774 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
775 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
776 (fp3
= fopen (outfile
, "wb")) == NULL
)
788 while ((ch
= getc (fp1
)) != EOF
)
789 (void) putc (ch
, fp3
);
792 while ((ch
= getc (fp2
)) != EOF
)
793 (void) putc (ch
, fp3
);
797 bool result
= wxRenameFile(outfile
, file3
);
804 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
810 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
812 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
818 while ((ch
= getc (fd1
)) != EOF
)
819 (void) putc (ch
, fd2
);
827 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
829 // Normal system call
830 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
833 if (wxCopyFile(file1
, file2
)) {
841 bool wxRemoveFile(const wxString
& file
)
843 #if defined(_MSC_VER) || defined(__BORLANDC__)
844 int flag
= remove(WXSTRINGCAST file
);
846 int flag
= unlink(WXSTRINGCAST file
);
851 bool wxMkdir(const wxString
& dir
)
853 #if defined(__WXSTUBS__)
855 #elif defined(__VMS__)
857 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
858 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
860 return (mkdir(WXSTRINGCAST dir
) == 0);
864 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
869 return (rmdir(WXSTRINGCAST dir
) == 0);
874 bool wxDirExists(const wxString
& dir
)
878 #elif !defined(__WXMSW__)
880 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
883 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
884 #if defined(__WIN32__)
885 WIN32_FIND_DATA fileInfo
;
888 struct ffblk fileInfo
;
890 struct find_t fileInfo
;
894 #if defined(__WIN32__)
895 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
897 if (h
==INVALID_HANDLE_VALUE
)
901 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
904 // In Borland findfirst has a different argument
905 // ordering from _dos_findfirst. But _dos_findfirst
906 // _should_ be ok in both MS and Borland... why not?
908 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
910 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
919 // does the path exists? (may have or not '/' or '\\' at the end)
920 bool wxPathExists(const char *pszPathName
)
922 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
923 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
924 wxString
strPath(pszPathName
);
925 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
926 strPath
.Last() = '\0';
929 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
932 // Get a temporary filename, opening and closing the file.
933 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
939 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
942 char tmpPath
[MAX_PATH
];
943 ::GetTempPath(MAX_PATH
, tmpPath
);
944 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
946 if (buf
) strcpy(buf
, tmp
);
947 else buf
= copystring(tmp
);
951 static short last_temp
= 0; // cache last to speed things a bit
952 // At most 1000 temp files to a process! We use a ring count.
955 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
957 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
958 if (!wxFileExists( tmp
))
960 // Touch the file to create it (reserve name)
961 FILE *fd
= fopen (tmp
, "w");
968 buf
= copystring( tmp
);
972 cerr
<< _("wxWindows: error finding temporary file name.\n");
974 return (char *) NULL
;
978 // Get first file name matching given wild card.
982 // Get first file name matching given wild card.
983 // Flags are reserved for future use.
986 static DIR *wxDirStream
= (DIR *) NULL
;
987 static char *wxFileSpec
= (char *) NULL
;
988 static int wxFindFileFlags
= 0;
991 char *wxFindFirstFile(const char *spec
, int flags
)
995 closedir(wxDirStream
); // edz 941103: better housekeping
997 wxFindFileFlags
= flags
;
1000 delete[] wxFileSpec
;
1001 wxFileSpec
= copystring(spec
);
1003 // Find path only so we can concatenate
1004 // found file onto path
1005 char *p
= wxPathOnly(wxFileSpec
);
1007 /* MATTHEW: special case: path is really "/" */
1008 if (p
&& !*p
&& *wxFileSpec
== '/')
1010 /* MATTHEW: p is NULL => Local directory */
1014 if ((wxDirStream
=opendir(p
))==NULL
)
1015 return (char *) NULL
;
1017 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1018 return wxFindNextFile();
1021 return (char *) NULL
;
1024 char *wxFindNextFile(void)
1027 static char buf
[400];
1029 /* MATTHEW: [2] Don't crash if we read too many times */
1031 return (char *) NULL
;
1033 // Find path only so we can concatenate
1034 // found file onto path
1035 char *p
= wxPathOnly(wxFileSpec
);
1036 char *n
= wxFileNameFromPath(wxFileSpec
);
1038 /* MATTHEW: special case: path is really "/" */
1039 if (p
&& !*p
&& *wxFileSpec
== '/')
1043 struct dirent
*nextDir
;
1044 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1047 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1048 directories when flags & wxDIR */
1049 if (wxMatchWild(n
, nextDir
->d_name
)) {
1055 if (strcmp(p
, "/") != 0)
1058 strcat(buf
, nextDir
->d_name
);
1060 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1061 (strcmp(nextDir
->d_name
, "..") == 0)) {
1062 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1066 isdir
= wxDirExists(buf
);
1068 if (!wxFindFileFlags
1069 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1070 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1074 closedir(wxDirStream
);
1075 wxDirStream
= (DIR *) NULL
;
1079 return (char *) NULL
;
1082 #elif defined(__WXMSW__)
1085 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1086 WIN32_FIND_DATA wxFileStruc
;
1089 static struct ffblk wxFileStruc
;
1091 static struct _find_t wxFileStruc
;
1094 static wxString wxFileSpec
= "";
1095 static int wxFindFileFlags
;
1097 char *wxFindFirstFile(const char *spec
, int flags
)
1100 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1102 // Find path only so we can concatenate
1103 // found file onto path
1104 wxString
path1(wxFileSpec
);
1105 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1106 if (p
&& (strlen(p
) > 0))
1107 strcpy(wxBuffer
, p
);
1112 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1113 FindClose(wxFileStrucHandle
);
1115 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1117 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1120 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1122 if (isdir
&& !(flags
& wxDIR
))
1123 return wxFindNextFile();
1124 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1125 return wxFindNextFile();
1127 if (wxBuffer
[0] != 0)
1128 strcat(wxBuffer
, "\\");
1129 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1133 int flag
= _A_NORMAL
;
1134 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1138 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1140 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1143 /* MATTHEW: [5] Check directory flag */
1147 attrib
= wxFileStruc
.ff_attrib
;
1149 attrib
= wxFileStruc
.attrib
;
1152 if (attrib
& _A_SUBDIR
) {
1153 if (!(wxFindFileFlags
& wxDIR
))
1154 return wxFindNextFile();
1155 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1156 return wxFindNextFile();
1158 if (wxBuffer
[0] != 0)
1159 strcat(wxBuffer
, "\\");
1162 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1164 strcat(wxBuffer
, wxFileStruc
.name
);
1173 char *wxFindNextFile(void)
1175 // Find path only so we can concatenate
1176 // found file onto path
1177 wxString
p2(wxFileSpec
);
1178 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1179 if (p
&& (strlen(p
) > 0))
1180 strcpy(wxBuffer
, p
);
1187 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1190 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1192 FindClose(wxFileStrucHandle
);
1193 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1197 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1199 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1201 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1204 if (wxBuffer
[0] != 0)
1205 strcat(wxBuffer
, "\\");
1206 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1211 if (findnext(&wxFileStruc
) == 0)
1213 if (_dos_findnext(&wxFileStruc
) == 0)
1216 /* MATTHEW: [5] Check directory flag */
1220 attrib
= wxFileStruc
.ff_attrib
;
1222 attrib
= wxFileStruc
.attrib
;
1225 if (attrib
& _A_SUBDIR
) {
1226 if (!(wxFindFileFlags
& wxDIR
))
1228 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1232 if (wxBuffer
[0] != 0)
1233 strcat(wxBuffer
, "\\");
1235 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1237 strcat(wxBuffer
, wxFileStruc
.name
);
1249 // Get current working directory.
1250 // If buf is NULL, allocates space using new, else
1252 char *wxGetWorkingDirectory(char *buf
, int sz
)
1255 buf
= new char[sz
+1];
1257 if (_getcwd(buf
, sz
) == NULL
) {
1259 if (getcwd(buf
, sz
) == NULL
) {
1267 bool wxSetWorkingDirectory(const wxString
& d
)
1270 return (chdir(d
) == 0);
1271 #elif defined(__WINDOWS__)
1274 return (bool)(SetCurrentDirectory(d
) != 0);
1276 // Must change drive, too.
1277 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1280 char firstChar
= d
[0];
1284 firstChar
= firstChar
- 32;
1286 // To a drive number
1287 unsigned int driveNo
= firstChar
- 64;
1290 unsigned int noDrives
;
1291 _dos_setdrive(driveNo
, &noDrives
);
1294 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1302 bool wxEndsWithPathSeparator(const char *pszFileName
)
1304 size_t len
= Strlen(pszFileName
);
1308 return wxIsPathSeparator(pszFileName
[len
- 1]);
1311 // find a file in a list of directories, returns false if not found
1312 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1314 // we assume that it's not empty
1315 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1316 _("empty file name in wxFindFileInPath"));
1318 // skip path separator in the beginning of the file name if present
1319 if ( wxIsPathSeparator(*pszFile
) )
1322 // copy the path (strtok will modify it)
1323 char *szPath
= new char[strlen(pszPath
) + 1];
1324 strcpy(szPath
, pszPath
);
1328 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1329 // search for the file in this directory
1331 if ( !wxEndsWithPathSeparator(pc
) )
1332 strFile
+= FILE_SEP_PATH
;
1335 if ( FileExists(strFile
) ) {
1343 return pc
!= NULL
; // if true => we breaked from the loop
1346 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1351 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1353 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1354 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1355 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1357 // take the last of the two
1358 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1359 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1360 if ( nPosDos
> nPosUnix
)
1362 // size_t nLen = Strlen(pszFileName);
1365 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1367 size_t nPosDot
= pDot
- pszFileName
;
1369 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1371 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1375 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1381 //------------------------------------------------------------------------
1382 // wild character routines
1383 //------------------------------------------------------------------------
1385 bool wxIsWild( const wxString
& pattern
)
1387 wxString tmp
= pattern
;
1388 char *pat
= WXSTRINGCAST(tmp
);
1391 case '?': case '*': case '[': case '{':
1401 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1402 #ifdef HAVE_FNMATCH_H
1405 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1407 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1411 #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1413 * WARNING: this code is broken!
1416 wxString tmp1
= pat
;
1417 char *pattern
= WXSTRINGCAST(tmp1
);
1418 wxString tmp2
= text
;
1419 char *str
= WXSTRINGCAST(tmp2
);
1422 bool done
= FALSE
, ret_code
, ok
;
1423 // Below is for vi fans
1424 const char OB
= '{', CB
= '}';
1426 // dot_special means '.' only matches '.'
1427 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1430 while ((*pattern
!= '\0') && (!done
)
1431 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1435 if (*pattern
!= '\0')
1442 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1445 while (*str
!= '\0')
1447 while (*pattern
!= '\0')
1454 if ((*pattern
== '\0') || (*pattern
== ']')) {
1458 if (*pattern
== '\\') {
1460 if (*pattern
== '\0') {
1465 if (*(pattern
+ 1) == '-') {
1468 if (*pattern
== ']') {
1472 if (*pattern
== '\\') {
1474 if (*pattern
== '\0') {
1479 if ((*str
< c
) || (*str
> *pattern
)) {
1483 } else if (*pattern
!= *str
) {
1488 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1489 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1493 if (*pattern
!= '\0') {
1503 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1506 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1507 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1508 if (*pattern
== '\\')
1510 ok
= (*pattern
++ == *cp
++);
1512 if (*pattern
== '\0') {
1518 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1519 if (*++pattern
== '\\') {
1520 if (*++pattern
== CB
)
1525 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1526 if (*++pattern
== '\\') {
1527 if (*++pattern
== CB
|| *pattern
== ',')
1532 if (*pattern
!= '\0')
1537 if (*str
== *pattern
) {
1544 while (*pattern
== '*')
1546 return ((*str
== '\0') && (*pattern
== '\0'));