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 // #include <sys/stat.h>
64 #define stricmp strcasecmp
67 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
68 // this (3.1 I believe) and how to test for it.
69 // If this works for Borland 4.0 as well, then no worries.
77 #define _MAXPATHLEN 500
79 #if !USE_SHARED_LIBRARY
80 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
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
)
214 if (filename
&& stat ((char *)(const char *)filename
, &stbuf
) == 0)
219 /* Vadim's alternative implementation
221 // does the file exist?
222 bool wxFileExists(const char *pszFileName)
225 return !access(pszFileName, 0) &&
226 !stat(pszFileName, &st) &&
227 (st.st_mode & S_IFREG);
232 wxIsAbsolutePath (const wxString
& filename
)
236 if (filename
[0] == '/'
238 || (filename
[0] == '[' && filename
[1] != '.')
242 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
251 * Strip off any extension (dot something) from end of file,
252 * IF one exists. Inserts zero into buffer.
256 void wxStripExtension(char *buffer
)
258 int len
= strlen(buffer
);
262 if (buffer
[i
] == '.')
271 void wxStripExtension(wxString
& buffer
)
273 size_t len
= buffer
.Length();
277 if (buffer
.GetChar(i
) == '.')
279 buffer
= buffer
.Left(i
);
286 // Destructive removal of /./ and /../ stuff
287 char *wxRealPath (char *path
)
290 static const char SEP
= '\\';
291 Unix2DosFilename(path
);
293 static const char SEP
= '/';
295 if (path
[0] && path
[1]) {
296 /* MATTHEW: special case "/./x" */
298 if (path
[2] == SEP
&& path
[1] == '.')
306 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
309 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
310 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
311 && (q
- 1 <= path
|| q
[-1] != SEP
))
320 /* Check that path[2] is NULL! */
321 else if (path
[1] == ':' && !path
[2])
330 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
339 char *wxCopyAbsolutePath(const wxString
& filename
)
342 return (char *) NULL
;
344 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
345 char buf
[_MAXPATHLEN
];
347 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
348 char ch
= buf
[strlen(buf
) - 1];
350 if (ch
!= '\\' && ch
!= '/')
356 strcat(buf
, wxBuffer
);
357 return copystring( wxRealPath(buf
) );
359 return copystring( wxBuffer
);
365 ~user/ => user's home dir
366 If the environment variable a = "foo" and b = "bar" then:
383 /* input name in name, pathname output to buf. */
385 char *wxExpandPath(char *buf
, const char *name
)
387 register char *d
, *s
, *nm
;
388 char lnm
[_MAXPATHLEN
];
391 // Some compilers don't like this line.
392 // const char trimchars[] = "\n \t";
401 const char SEP
= '\\';
403 const char SEP
= '/';
406 if (name
== NULL
|| *name
== '\0')
408 nm
= copystring(name
); // Make a scratch copy
411 /* Skip leading whitespace and cr */
412 while (strchr((char *)trimchars
, *nm
) != NULL
)
414 /* And strip off trailing whitespace and cr */
415 s
= nm
+ (q
= strlen(nm
)) - 1;
416 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
424 q
= nm
[0] == '\\' && nm
[1] == '~';
427 /* Expand inline environment variables */
428 while ((*d
++ = *s
)) {
431 if ((*(d
- 1) = *++s
)) {
439 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
444 register char *start
= d
;
445 register int braces
= (*s
== '{' || *s
== '(');
446 register char *value
;
448 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
453 value
= getenv(braces
? start
+ 1 : start
);
455 for ((d
= start
- 1); (*d
++ = *value
++););
463 /* Expand ~ and ~user */
466 if (nm
[0] == '~' && !q
)
469 if (nm
[1] == SEP
|| nm
[1] == 0)
471 if ((s
= wxGetUserHome("")) != NULL
) {
476 { /* ~user/filename */
479 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
480 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
481 was_sep
= (*s
== SEP
);
482 nnm
= *s
? s
+ 1 : s
;
484 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
485 if (was_sep
) /* replace only if it was there: */
496 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
498 while ('\0' != (*d
++ = *s
++))
501 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
505 while ((*d
++ = *s
++));
507 delete[] nm_tmp
; // clean up alloc
508 /* Now clean up the buffer */
509 return wxRealPath(buf
);
513 /* Contract Paths to be build upon an environment variable
516 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
518 The call wxExpandPath can convert these back!
521 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
523 static char dest
[_MAXPATHLEN
];
526 return (char *) NULL
;
528 strcpy (dest
, WXSTRINGCAST filename
);
530 Unix2DosFilename(dest
);
533 // Handle environment
534 char *val
= (char *) NULL
;
535 char *tcp
= (char *) NULL
;
536 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
537 (tcp
= strstr (dest
, val
)) != NULL
)
539 strcpy (wxBuffer
, tcp
+ strlen (val
));
542 strcpy (tcp
, WXSTRINGCAST envname
);
544 strcat (tcp
, wxBuffer
);
547 // Handle User's home (ignore root homes!)
549 if ((val
= wxGetUserHome (user
)) != NULL
&&
550 (len
= strlen(val
)) > 2 &&
551 strncmp(dest
, val
, len
) == 0)
553 strcpy(wxBuffer
, "~");
555 strcat(wxBuffer
, user
);
557 // strcat(wxBuffer, "\\");
559 // strcat(wxBuffer, "/");
561 strcat(wxBuffer
, dest
+ len
);
562 strcpy (dest
, wxBuffer
);
568 // Return just the filename, not the path
570 char *wxFileNameFromPath (char *path
)
576 tcp
= path
+ strlen (path
);
577 while (--tcp
>= path
)
579 if (*tcp
== '/' || *tcp
== '\\'
581 || *tcp
== ':' || *tcp
== ']')
588 if (isalpha (*path
) && *(path
+ 1) == ':')
595 wxString
wxFileNameFromPath (const wxString
& path1
)
600 char *path
= WXSTRINGCAST path1
;
603 tcp
= path
+ strlen (path
);
604 while (--tcp
>= path
)
606 if (*tcp
== '/' || *tcp
== '\\'
608 || *tcp
== ':' || *tcp
== ']')
612 return wxString(tcp
+ 1);
615 if (isalpha (*path
) && *(path
+ 1) == ':')
616 return wxString(path
+ 2);
622 // Return just the directory, or NULL if no directory
624 wxPathOnly (char *path
)
628 static char buf
[_MAXPATHLEN
];
633 int l
= strlen(path
);
638 // Search backward for a backward or forward slash
639 while (!done
&& i
> -1)
642 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
657 // Try Drive specifier
658 if (isalpha (buf
[0]) && buf
[1] == ':')
660 // A:junk --> A:. (since A:.\junk Not A:\junk)
668 return (char *) NULL
;
671 // Return just the directory, or NULL if no directory
672 wxString
wxPathOnly (const wxString
& path
)
676 char buf
[_MAXPATHLEN
];
679 strcpy (buf
, WXSTRINGCAST path
);
681 int l
= path
.Length();
686 // Search backward for a backward or forward slash
687 while (!done
&& i
> -1)
690 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
699 return wxString(buf
);
705 // Try Drive specifier
706 if (isalpha (buf
[0]) && buf
[1] == ':')
708 // A:junk --> A:. (since A:.\junk Not A:\junk)
711 return wxString(buf
);
719 // Utility for converting delimiters in DOS filenames to UNIX style
720 // and back again - or we get nasty problems with delimiters.
721 // Also, convert to lower case, since case is significant in UNIX.
724 wxDos2UnixFilename (char *s
)
733 *s
= wxToLower (*s
); // Case INDEPENDENT
741 wxUnix2DosFilename (char *s
)
743 wxUnix2DosFilename (char *WXUNUSED(s
))
746 // Yes, I really mean this to happen under DOS only! JACS
758 // Concatenate two files to form third
760 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
762 char *outfile
= wxGetTempFileName("cat");
764 FILE *fp1
= (FILE *) NULL
;
765 FILE *fp2
= (FILE *) NULL
;
766 FILE *fp3
= (FILE *) NULL
;
767 // Open the inputs and outputs
768 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
769 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
770 (fp3
= fopen (outfile
, "wb")) == NULL
)
782 while ((ch
= getc (fp1
)) != EOF
)
783 (void) putc (ch
, fp3
);
786 while ((ch
= getc (fp2
)) != EOF
)
787 (void) putc (ch
, fp3
);
791 bool result
= wxRenameFile(outfile
, file3
);
798 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
804 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
806 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
812 while ((ch
= getc (fd1
)) != EOF
)
813 (void) putc (ch
, fd2
);
821 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
823 // Normal system call
824 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
827 if (wxCopyFile(file1
, file2
)) {
835 bool wxRemoveFile(const wxString
& file
)
837 #if defined(_MSC_VER) || defined(__BORLANDC__)
838 int flag
= remove(WXSTRINGCAST file
);
840 int flag
= unlink(WXSTRINGCAST file
);
845 bool wxMkdir(const wxString
& dir
)
847 #if defined(__WXSTUBS__)
849 #elif defined(__VMS__)
851 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
852 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
854 return (mkdir(WXSTRINGCAST dir
) == 0);
858 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
863 return (rmdir(WXSTRINGCAST dir
) == 0);
868 bool wxDirExists(const wxString
& dir
)
872 #elif !defined(__WXMSW__)
874 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
877 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
878 #if defined(__WIN32__)
879 WIN32_FIND_DATA fileInfo
;
882 struct ffblk fileInfo
;
884 struct find_t fileInfo
;
888 #if defined(__WIN32__)
889 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
891 if (h
==INVALID_HANDLE_VALUE
)
895 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
898 // In Borland findfirst has a different argument
899 // ordering from _dos_findfirst. But _dos_findfirst
900 // _should_ be ok in both MS and Borland... why not?
902 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
904 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
913 // does the path exists? (may have or not '/' or '\\' at the end)
914 bool wxPathExists(const char *pszPathName
)
916 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
917 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
918 wxString
strPath(pszPathName
);
919 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
920 strPath
.Last() = '\0';
923 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
926 // Get a temporary filename, opening and closing the file.
927 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
933 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
936 char tmpPath
[MAX_PATH
];
937 ::GetTempPath(MAX_PATH
, tmpPath
);
938 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
940 if (buf
) strcpy(buf
, tmp
);
941 else buf
= copystring(tmp
);
945 static short last_temp
= 0; // cache last to speed things a bit
946 // At most 1000 temp files to a process! We use a ring count.
949 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
951 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
952 if (!wxFileExists( tmp
))
954 // Touch the file to create it (reserve name)
955 FILE *fd
= fopen (tmp
, "w");
962 buf
= copystring( tmp
);
966 cerr
<< _("wxWindows: error finding temporary file name.\n");
968 return (char *) NULL
;
972 // Get first file name matching given wild card.
976 // Get first file name matching given wild card.
977 // Flags are reserved for future use.
980 static DIR *wxDirStream
= (DIR *) NULL
;
981 static char *wxFileSpec
= (char *) NULL
;
982 static int wxFindFileFlags
= 0;
985 char *wxFindFirstFile(const char *spec
, int flags
)
989 closedir(wxDirStream
); // edz 941103: better housekeping
991 wxFindFileFlags
= flags
;
995 wxFileSpec
= copystring(spec
);
997 // Find path only so we can concatenate
998 // found file onto path
999 char *p
= wxPathOnly(wxFileSpec
);
1001 /* MATTHEW: special case: path is really "/" */
1002 if (p
&& !*p
&& *wxFileSpec
== '/')
1004 /* MATTHEW: p is NULL => Local directory */
1008 if ((wxDirStream
=opendir(p
))==NULL
)
1009 return (char *) NULL
;
1011 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1012 return wxFindNextFile();
1015 return (char *) NULL
;
1018 char *wxFindNextFile(void)
1021 static char buf
[400];
1023 /* MATTHEW: [2] Don't crash if we read too many times */
1025 return (char *) NULL
;
1027 // Find path only so we can concatenate
1028 // found file onto path
1029 char *p
= wxPathOnly(wxFileSpec
);
1030 char *n
= wxFileNameFromPath(wxFileSpec
);
1032 /* MATTHEW: special case: path is really "/" */
1033 if (p
&& !*p
&& *wxFileSpec
== '/')
1037 struct dirent
*nextDir
;
1038 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1041 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1042 directories when flags & wxDIR */
1043 if (wxMatchWild(n
, nextDir
->d_name
)) {
1049 if (strcmp(p
, "/") != 0)
1052 strcat(buf
, nextDir
->d_name
);
1054 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1055 (strcmp(nextDir
->d_name
, "..") == 0)) {
1056 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1060 isdir
= wxDirExists(buf
);
1062 if (!wxFindFileFlags
1063 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1064 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1068 closedir(wxDirStream
);
1069 wxDirStream
= (DIR *) NULL
;
1073 return (char *) NULL
;
1076 #elif defined(__WXMSW__)
1079 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1080 WIN32_FIND_DATA wxFileStruc
;
1083 static struct ffblk wxFileStruc
;
1085 static struct _find_t wxFileStruc
;
1088 static wxString wxFileSpec
= "";
1089 static int wxFindFileFlags
;
1091 char *wxFindFirstFile(const char *spec
, int flags
)
1094 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1096 // Find path only so we can concatenate
1097 // found file onto path
1098 wxString
path1(wxFileSpec
);
1099 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1100 if (p
&& (strlen(p
) > 0))
1101 strcpy(wxBuffer
, p
);
1106 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1107 FindClose(wxFileStrucHandle
);
1109 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1111 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1114 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1116 if (isdir
&& !(flags
& wxDIR
))
1117 return wxFindNextFile();
1118 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1119 return wxFindNextFile();
1121 if (wxBuffer
[0] != 0)
1122 strcat(wxBuffer
, "\\");
1123 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1127 int flag
= _A_NORMAL
;
1128 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1132 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1134 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1137 /* MATTHEW: [5] Check directory flag */
1141 attrib
= wxFileStruc
.ff_attrib
;
1143 attrib
= wxFileStruc
.attrib
;
1146 if (attrib
& _A_SUBDIR
) {
1147 if (!(wxFindFileFlags
& wxDIR
))
1148 return wxFindNextFile();
1149 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1150 return wxFindNextFile();
1152 if (wxBuffer
[0] != 0)
1153 strcat(wxBuffer
, "\\");
1156 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1158 strcat(wxBuffer
, wxFileStruc
.name
);
1167 char *wxFindNextFile(void)
1169 // Find path only so we can concatenate
1170 // found file onto path
1171 wxString
p2(wxFileSpec
);
1172 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1173 if (p
&& (strlen(p
) > 0))
1174 strcpy(wxBuffer
, p
);
1181 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1184 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1186 FindClose(wxFileStrucHandle
);
1187 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1191 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1193 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1195 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1198 if (wxBuffer
[0] != 0)
1199 strcat(wxBuffer
, "\\");
1200 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1205 if (findnext(&wxFileStruc
) == 0)
1207 if (_dos_findnext(&wxFileStruc
) == 0)
1210 /* MATTHEW: [5] Check directory flag */
1214 attrib
= wxFileStruc
.ff_attrib
;
1216 attrib
= wxFileStruc
.attrib
;
1219 if (attrib
& _A_SUBDIR
) {
1220 if (!(wxFindFileFlags
& wxDIR
))
1222 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1226 if (wxBuffer
[0] != 0)
1227 strcat(wxBuffer
, "\\");
1229 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1231 strcat(wxBuffer
, wxFileStruc
.name
);
1243 // Get current working directory.
1244 // If buf is NULL, allocates space using new, else
1246 char *wxGetWorkingDirectory(char *buf
, int sz
)
1249 buf
= new char[sz
+1];
1251 if (_getcwd(buf
, sz
) == NULL
) {
1253 if (getcwd(buf
, sz
) == NULL
) {
1261 bool wxSetWorkingDirectory(const wxString
& d
)
1264 return (chdir(d
) == 0);
1265 #elif defined(__WINDOWS__)
1268 return (bool)(SetCurrentDirectory(d
) != 0);
1270 // Must change drive, too.
1271 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1274 char firstChar
= d
[0];
1278 firstChar
= firstChar
- 32;
1280 // To a drive number
1281 unsigned int driveNo
= firstChar
- 64;
1284 unsigned int noDrives
;
1285 _dos_setdrive(driveNo
, &noDrives
);
1288 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1296 bool wxEndsWithPathSeparator(const char *pszFileName
)
1298 size_t len
= Strlen(pszFileName
);
1302 return wxIsPathSeparator(pszFileName
[len
- 1]);
1305 // find a file in a list of directories, returns false if not found
1306 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1308 // we assume that it's not empty
1309 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1310 _("empty file name in wxFindFileInPath"));
1312 // skip path separator in the beginning of the file name if present
1313 if ( wxIsPathSeparator(*pszFile
) )
1316 // copy the path (strtok will modify it)
1317 char *szPath
= new char[strlen(pszPath
) + 1];
1318 strcpy(szPath
, pszPath
);
1322 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1323 // search for the file in this directory
1325 if ( !wxEndsWithPathSeparator(pc
) )
1326 strFile
+= FILE_SEP_PATH
;
1329 if ( FileExists(strFile
) ) {
1337 return pc
!= NULL
; // if true => we breaked from the loop
1340 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1345 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1347 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1348 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1349 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1351 // take the last of the two
1352 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1353 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1354 if ( nPosDos
> nPosUnix
)
1356 // size_t nLen = Strlen(pszFileName);
1359 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1361 size_t nPosDot
= pDot
- pszFileName
;
1363 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1365 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1369 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1375 //------------------------------------------------------------------------
1376 // wild character routines
1377 //------------------------------------------------------------------------
1379 bool wxIsWild( const wxString
& pattern
)
1381 wxString tmp
= pattern
;
1382 char *pat
= WXSTRINGCAST(tmp
);
1385 case '?': case '*': case '[': case '{':
1396 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1398 wxString tmp1
= pat
;
1399 char *pattern
= WXSTRINGCAST(tmp1
);
1400 wxString tmp2
= text
;
1401 char *str
= WXSTRINGCAST(tmp2
);
1404 bool done
= FALSE
, ret_code
, ok
;
1405 // Below is for vi fans
1406 const char OB
= '{', CB
= '}';
1408 // dot_special means '.' only matches '.'
1409 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1412 while ((*pattern
!= '\0') && (!done
)
1413 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1417 if (*pattern
!= '\0')
1424 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1427 while (*str
!= '\0')
1429 while (*pattern
!= '\0')
1436 if ((*pattern
== '\0') || (*pattern
== ']')) {
1440 if (*pattern
== '\\') {
1442 if (*pattern
== '\0') {
1447 if (*(pattern
+ 1) == '-') {
1450 if (*pattern
== ']') {
1454 if (*pattern
== '\\') {
1456 if (*pattern
== '\0') {
1461 if ((*str
< c
) || (*str
> *pattern
)) {
1465 } else if (*pattern
!= *str
) {
1470 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1471 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1475 if (*pattern
!= '\0') {
1485 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1488 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1489 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1490 if (*pattern
== '\\')
1492 ok
= (*pattern
++ == *cp
++);
1494 if (*pattern
== '\0') {
1500 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1501 if (*++pattern
== '\\') {
1502 if (*++pattern
== CB
)
1507 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1508 if (*++pattern
== '\\') {
1509 if (*++pattern
== CB
|| *pattern
== ',')
1514 if (*pattern
!= '\0')
1519 if (*str
== *pattern
) {
1526 while (*pattern
== '*')
1528 return ((*str
== '\0') && (*pattern
== '\0'));