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.
82 #define _MAXPATHLEN 500
84 #if !USE_SHARED_LIBRARY
85 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
88 extern char *wxBuffer
;
90 void wxPathList::Add (const wxString
& path
)
92 wxStringList::Add ((char *)(const char *)path
);
95 // Add paths e.g. from the PATH environment variable
96 void wxPathList::AddEnvList (const wxString
& envVariable
)
98 static const char PATH_TOKS
[] =
100 " ;"; // Don't seperate with colon in DOS (used for drive)
105 char *val
= getenv (WXSTRINGCAST envVariable
);
108 char *s
= copystring (val
);
109 char *token
= strtok (s
, PATH_TOKS
);
113 Add (copystring (token
));
116 if ((token
= strtok ((char *) NULL
, PATH_TOKS
)) != NULL
)
117 Add (wxString(token
));
124 // Given a full filename (with path), ensure that that file can
125 // be accessed again USING FILENAME ONLY by adding the path
126 // to the list if not already there.
127 void wxPathList::EnsureFileAccessible (const wxString
& path
)
129 wxString
path1(path
);
130 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
133 if (!Member (wxString(path_only
)))
134 Add (wxString(path_only
));
138 bool wxPathList::Member (const wxString
& path
)
140 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
142 wxString
path2((char *) node
->Data ());
144 #if defined(__WINDOWS__) || defined(__VMS__)
146 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
148 // Case sensitive File System
149 path
.CompareTo (path2
) == 0
157 wxString
wxPathList::FindValidPath (const wxString
& file
)
159 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
160 return wxString(wxBuffer
);
162 char buf
[_MAXPATHLEN
];
163 strcpy(buf
, wxBuffer
);
165 char *filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
167 for (wxNode
* node
= First (); node
; node
= node
->Next ())
169 char *path
= (char *) node
->Data ();
170 strcpy (wxBuffer
, path
);
171 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
172 if (ch
!= '\\' && ch
!= '/')
173 strcat (wxBuffer
, "/");
174 strcat (wxBuffer
, filename
);
176 Unix2DosFilename (wxBuffer
);
178 if (wxFileExists (wxBuffer
))
180 return wxString(wxBuffer
); // Found!
184 return wxString(""); // Not found
187 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
189 wxString f
= FindValidPath(file
);
190 if (wxIsAbsolutePath(f
))
195 wxGetWorkingDirectory(buf
, 499);
196 int len
= (int)strlen(buf
);
200 if (lastCh
!= '/' && lastCh
!= '\\')
208 strcat(buf
, (const char *)f
);
209 strcpy(wxBuffer
, buf
);
210 return wxString(wxBuffer
);
215 wxFileExists (const wxString
& filename
)
219 if (filename
&& stat ((char *)(const char *)filename
, &stbuf
) == 0)
224 /* Vadim's alternative implementation
226 // does the file exist?
227 bool wxFileExists(const char *pszFileName)
230 return !access(pszFileName, 0) &&
231 !stat(pszFileName, &st) &&
232 (st.st_mode & S_IFREG);
237 wxIsAbsolutePath (const wxString
& filename
)
241 if (filename
[0] == '/'
243 || (filename
[0] == '[' && filename
[1] != '.')
247 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
256 * Strip off any extension (dot something) from end of file,
257 * IF one exists. Inserts zero into buffer.
261 void wxStripExtension(char *buffer
)
263 int len
= strlen(buffer
);
267 if (buffer
[i
] == '.')
276 void wxStripExtension(wxString
& buffer
)
278 size_t len
= buffer
.Length();
282 if (buffer
.GetChar(i
) == '.')
284 buffer
= buffer
.Left(i
);
291 // Destructive removal of /./ and /../ stuff
292 char *wxRealPath (char *path
)
295 static const char SEP
= '\\';
296 Unix2DosFilename(path
);
298 static const char SEP
= '/';
300 if (path
[0] && path
[1]) {
301 /* MATTHEW: special case "/./x" */
303 if (path
[2] == SEP
&& path
[1] == '.')
311 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
314 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
315 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
316 && (q
- 1 <= path
|| q
[-1] != SEP
))
325 /* Check that path[2] is NULL! */
326 else if (path
[1] == ':' && !path
[2])
335 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
344 char *wxCopyAbsolutePath(const wxString
& filename
)
347 return (char *) NULL
;
349 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
350 char buf
[_MAXPATHLEN
];
352 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
353 char ch
= buf
[strlen(buf
) - 1];
355 if (ch
!= '\\' && ch
!= '/')
361 strcat(buf
, wxBuffer
);
362 return copystring( wxRealPath(buf
) );
364 return copystring( wxBuffer
);
370 ~user/ => user's home dir
371 If the environment variable a = "foo" and b = "bar" then:
388 /* input name in name, pathname output to buf. */
390 char *wxExpandPath(char *buf
, const char *name
)
392 register char *d
, *s
, *nm
;
393 char lnm
[_MAXPATHLEN
];
396 // Some compilers don't like this line.
397 // const char trimchars[] = "\n \t";
406 const char SEP
= '\\';
408 const char SEP
= '/';
411 if (name
== NULL
|| *name
== '\0')
413 nm
= copystring(name
); // Make a scratch copy
416 /* Skip leading whitespace and cr */
417 while (strchr((char *)trimchars
, *nm
) != NULL
)
419 /* And strip off trailing whitespace and cr */
420 s
= nm
+ (q
= strlen(nm
)) - 1;
421 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
429 q
= nm
[0] == '\\' && nm
[1] == '~';
432 /* Expand inline environment variables */
433 while ((*d
++ = *s
)) {
436 if ((*(d
- 1) = *++s
)) {
444 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
449 register char *start
= d
;
450 register int braces
= (*s
== '{' || *s
== '(');
451 register char *value
;
453 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
458 value
= getenv(braces
? start
+ 1 : start
);
460 for ((d
= start
- 1); (*d
++ = *value
++););
468 /* Expand ~ and ~user */
471 if (nm
[0] == '~' && !q
)
474 if (nm
[1] == SEP
|| nm
[1] == 0)
476 if ((s
= wxGetUserHome("")) != NULL
) {
481 { /* ~user/filename */
484 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
485 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
486 was_sep
= (*s
== SEP
);
487 nnm
= *s
? s
+ 1 : s
;
489 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
490 if (was_sep
) /* replace only if it was there: */
501 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
503 while ('\0' != (*d
++ = *s
++))
506 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
510 while ((*d
++ = *s
++));
512 delete[] nm_tmp
; // clean up alloc
513 /* Now clean up the buffer */
514 return wxRealPath(buf
);
518 /* Contract Paths to be build upon an environment variable
521 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
523 The call wxExpandPath can convert these back!
526 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
528 static char dest
[_MAXPATHLEN
];
531 return (char *) NULL
;
533 strcpy (dest
, WXSTRINGCAST filename
);
535 Unix2DosFilename(dest
);
538 // Handle environment
539 char *val
= (char *) NULL
;
540 char *tcp
= (char *) NULL
;
541 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
542 (tcp
= strstr (dest
, val
)) != NULL
)
544 strcpy (wxBuffer
, tcp
+ strlen (val
));
547 strcpy (tcp
, WXSTRINGCAST envname
);
549 strcat (tcp
, wxBuffer
);
552 // Handle User's home (ignore root homes!)
554 if ((val
= wxGetUserHome (user
)) != NULL
&&
555 (len
= strlen(val
)) > 2 &&
556 strncmp(dest
, val
, len
) == 0)
558 strcpy(wxBuffer
, "~");
560 strcat(wxBuffer
, user
);
562 // strcat(wxBuffer, "\\");
564 // strcat(wxBuffer, "/");
566 strcat(wxBuffer
, dest
+ len
);
567 strcpy (dest
, wxBuffer
);
573 // Return just the filename, not the path
575 char *wxFileNameFromPath (char *path
)
581 tcp
= path
+ strlen (path
);
582 while (--tcp
>= path
)
584 if (*tcp
== '/' || *tcp
== '\\'
586 || *tcp
== ':' || *tcp
== ']')
593 if (isalpha (*path
) && *(path
+ 1) == ':')
600 wxString
wxFileNameFromPath (const wxString
& path1
)
605 char *path
= WXSTRINGCAST path1
;
608 tcp
= path
+ strlen (path
);
609 while (--tcp
>= path
)
611 if (*tcp
== '/' || *tcp
== '\\'
613 || *tcp
== ':' || *tcp
== ']')
617 return wxString(tcp
+ 1);
620 if (isalpha (*path
) && *(path
+ 1) == ':')
621 return wxString(path
+ 2);
627 // Return just the directory, or NULL if no directory
629 wxPathOnly (char *path
)
633 static char buf
[_MAXPATHLEN
];
638 int l
= strlen(path
);
643 // Search backward for a backward or forward slash
644 while (!done
&& i
> -1)
647 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
662 // Try Drive specifier
663 if (isalpha (buf
[0]) && buf
[1] == ':')
665 // A:junk --> A:. (since A:.\junk Not A:\junk)
673 return (char *) NULL
;
676 // Return just the directory, or NULL if no directory
677 wxString
wxPathOnly (const wxString
& path
)
681 char buf
[_MAXPATHLEN
];
684 strcpy (buf
, WXSTRINGCAST path
);
686 int l
= path
.Length();
691 // Search backward for a backward or forward slash
692 while (!done
&& i
> -1)
695 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
704 return wxString(buf
);
710 // Try Drive specifier
711 if (isalpha (buf
[0]) && buf
[1] == ':')
713 // A:junk --> A:. (since A:.\junk Not A:\junk)
716 return wxString(buf
);
724 // Utility for converting delimiters in DOS filenames to UNIX style
725 // and back again - or we get nasty problems with delimiters.
726 // Also, convert to lower case, since case is significant in UNIX.
729 wxDos2UnixFilename (char *s
)
738 *s
= wxToLower (*s
); // Case INDEPENDENT
746 wxUnix2DosFilename (char *s
)
748 wxUnix2DosFilename (char *WXUNUSED(s
))
751 // Yes, I really mean this to happen under DOS only! JACS
763 // Concatenate two files to form third
765 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
767 char *outfile
= wxGetTempFileName("cat");
769 FILE *fp1
= (FILE *) NULL
;
770 FILE *fp2
= (FILE *) NULL
;
771 FILE *fp3
= (FILE *) NULL
;
772 // Open the inputs and outputs
773 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
774 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
775 (fp3
= fopen (outfile
, "wb")) == NULL
)
787 while ((ch
= getc (fp1
)) != EOF
)
788 (void) putc (ch
, fp3
);
791 while ((ch
= getc (fp2
)) != EOF
)
792 (void) putc (ch
, fp3
);
796 bool result
= wxRenameFile(outfile
, file3
);
803 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
809 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
811 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
817 while ((ch
= getc (fd1
)) != EOF
)
818 (void) putc (ch
, fd2
);
826 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
828 // Normal system call
829 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
832 if (wxCopyFile(file1
, file2
)) {
840 bool wxRemoveFile(const wxString
& file
)
842 #if defined(_MSC_VER) || defined(__BORLANDC__)
843 int flag
= remove(WXSTRINGCAST file
);
845 int flag
= unlink(WXSTRINGCAST file
);
850 bool wxMkdir(const wxString
& dir
)
852 #if defined(__WXSTUBS__)
854 #elif defined(__VMS__)
856 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
857 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
859 return (mkdir(WXSTRINGCAST dir
) == 0);
863 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
868 return (rmdir(WXSTRINGCAST dir
) == 0);
873 bool wxDirExists(const wxString
& dir
)
877 #elif !defined(__WXMSW__)
879 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
882 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
883 #if defined(__WIN32__)
884 WIN32_FIND_DATA fileInfo
;
887 struct ffblk fileInfo
;
889 struct find_t fileInfo
;
893 #if defined(__WIN32__)
894 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
896 if (h
==INVALID_HANDLE_VALUE
)
900 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
903 // In Borland findfirst has a different argument
904 // ordering from _dos_findfirst. But _dos_findfirst
905 // _should_ be ok in both MS and Borland... why not?
907 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
909 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
918 // does the path exists? (may have or not '/' or '\\' at the end)
919 bool wxPathExists(const char *pszPathName
)
921 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
922 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
923 wxString
strPath(pszPathName
);
924 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
925 strPath
.Last() = '\0';
928 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
931 // Get a temporary filename, opening and closing the file.
932 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
938 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
941 char tmpPath
[MAX_PATH
];
942 ::GetTempPath(MAX_PATH
, tmpPath
);
943 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
945 if (buf
) strcpy(buf
, tmp
);
946 else buf
= copystring(tmp
);
950 static short last_temp
= 0; // cache last to speed things a bit
951 // At most 1000 temp files to a process! We use a ring count.
954 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
956 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
957 if (!wxFileExists( tmp
))
959 // Touch the file to create it (reserve name)
960 FILE *fd
= fopen (tmp
, "w");
967 buf
= copystring( tmp
);
971 cerr
<< _("wxWindows: error finding temporary file name.\n");
973 return (char *) NULL
;
977 // Get first file name matching given wild card.
981 // Get first file name matching given wild card.
982 // Flags are reserved for future use.
985 static DIR *wxDirStream
= (DIR *) NULL
;
986 static char *wxFileSpec
= (char *) NULL
;
987 static int wxFindFileFlags
= 0;
990 char *wxFindFirstFile(const char *spec
, int flags
)
994 closedir(wxDirStream
); // edz 941103: better housekeping
996 wxFindFileFlags
= flags
;
1000 wxFileSpec
= copystring(spec
);
1002 // Find path only so we can concatenate
1003 // found file onto path
1004 char *p
= wxPathOnly(wxFileSpec
);
1006 /* MATTHEW: special case: path is really "/" */
1007 if (p
&& !*p
&& *wxFileSpec
== '/')
1009 /* MATTHEW: p is NULL => Local directory */
1013 if ((wxDirStream
=opendir(p
))==NULL
)
1014 return (char *) NULL
;
1016 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1017 return wxFindNextFile();
1020 return (char *) NULL
;
1023 char *wxFindNextFile(void)
1026 static char buf
[400];
1028 /* MATTHEW: [2] Don't crash if we read too many times */
1030 return (char *) NULL
;
1032 // Find path only so we can concatenate
1033 // found file onto path
1034 char *p
= wxPathOnly(wxFileSpec
);
1035 char *n
= wxFileNameFromPath(wxFileSpec
);
1037 /* MATTHEW: special case: path is really "/" */
1038 if (p
&& !*p
&& *wxFileSpec
== '/')
1042 struct dirent
*nextDir
;
1043 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1046 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1047 directories when flags & wxDIR */
1048 if (wxMatchWild(n
, nextDir
->d_name
)) {
1054 if (strcmp(p
, "/") != 0)
1057 strcat(buf
, nextDir
->d_name
);
1059 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1060 (strcmp(nextDir
->d_name
, "..") == 0)) {
1061 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1065 isdir
= wxDirExists(buf
);
1067 if (!wxFindFileFlags
1068 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1069 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1073 closedir(wxDirStream
);
1074 wxDirStream
= (DIR *) NULL
;
1078 return (char *) NULL
;
1081 #elif defined(__WXMSW__)
1084 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1085 WIN32_FIND_DATA wxFileStruc
;
1088 static struct ffblk wxFileStruc
;
1090 static struct _find_t wxFileStruc
;
1093 static wxString wxFileSpec
= "";
1094 static int wxFindFileFlags
;
1096 char *wxFindFirstFile(const char *spec
, int flags
)
1099 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1101 // Find path only so we can concatenate
1102 // found file onto path
1103 wxString
path1(wxFileSpec
);
1104 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1105 if (p
&& (strlen(p
) > 0))
1106 strcpy(wxBuffer
, p
);
1111 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1112 FindClose(wxFileStrucHandle
);
1114 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1116 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1119 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1121 if (isdir
&& !(flags
& wxDIR
))
1122 return wxFindNextFile();
1123 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1124 return wxFindNextFile();
1126 if (wxBuffer
[0] != 0)
1127 strcat(wxBuffer
, "\\");
1128 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1132 int flag
= _A_NORMAL
;
1133 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1137 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1139 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1142 /* MATTHEW: [5] Check directory flag */
1146 attrib
= wxFileStruc
.ff_attrib
;
1148 attrib
= wxFileStruc
.attrib
;
1151 if (attrib
& _A_SUBDIR
) {
1152 if (!(wxFindFileFlags
& wxDIR
))
1153 return wxFindNextFile();
1154 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1155 return wxFindNextFile();
1157 if (wxBuffer
[0] != 0)
1158 strcat(wxBuffer
, "\\");
1161 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1163 strcat(wxBuffer
, wxFileStruc
.name
);
1172 char *wxFindNextFile(void)
1174 // Find path only so we can concatenate
1175 // found file onto path
1176 wxString
p2(wxFileSpec
);
1177 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1178 if (p
&& (strlen(p
) > 0))
1179 strcpy(wxBuffer
, p
);
1186 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1189 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1191 FindClose(wxFileStrucHandle
);
1192 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1196 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1198 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1200 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1203 if (wxBuffer
[0] != 0)
1204 strcat(wxBuffer
, "\\");
1205 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1210 if (findnext(&wxFileStruc
) == 0)
1212 if (_dos_findnext(&wxFileStruc
) == 0)
1215 /* MATTHEW: [5] Check directory flag */
1219 attrib
= wxFileStruc
.ff_attrib
;
1221 attrib
= wxFileStruc
.attrib
;
1224 if (attrib
& _A_SUBDIR
) {
1225 if (!(wxFindFileFlags
& wxDIR
))
1227 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1231 if (wxBuffer
[0] != 0)
1232 strcat(wxBuffer
, "\\");
1234 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1236 strcat(wxBuffer
, wxFileStruc
.name
);
1248 // Get current working directory.
1249 // If buf is NULL, allocates space using new, else
1251 char *wxGetWorkingDirectory(char *buf
, int sz
)
1254 buf
= new char[sz
+1];
1256 if (_getcwd(buf
, sz
) == NULL
) {
1258 if (getcwd(buf
, sz
) == NULL
) {
1266 bool wxSetWorkingDirectory(const wxString
& d
)
1269 return (chdir(d
) == 0);
1270 #elif defined(__WINDOWS__)
1273 return (bool)(SetCurrentDirectory(d
) != 0);
1275 // Must change drive, too.
1276 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1279 char firstChar
= d
[0];
1283 firstChar
= firstChar
- 32;
1285 // To a drive number
1286 unsigned int driveNo
= firstChar
- 64;
1289 unsigned int noDrives
;
1290 _dos_setdrive(driveNo
, &noDrives
);
1293 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1301 bool wxEndsWithPathSeparator(const char *pszFileName
)
1303 size_t len
= Strlen(pszFileName
);
1307 return wxIsPathSeparator(pszFileName
[len
- 1]);
1310 // find a file in a list of directories, returns false if not found
1311 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1313 // we assume that it's not empty
1314 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1315 _("empty file name in wxFindFileInPath"));
1317 // skip path separator in the beginning of the file name if present
1318 if ( wxIsPathSeparator(*pszFile
) )
1321 // copy the path (strtok will modify it)
1322 char *szPath
= new char[strlen(pszPath
) + 1];
1323 strcpy(szPath
, pszPath
);
1327 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1328 // search for the file in this directory
1330 if ( !wxEndsWithPathSeparator(pc
) )
1331 strFile
+= FILE_SEP_PATH
;
1334 if ( FileExists(strFile
) ) {
1342 return pc
!= NULL
; // if true => we breaked from the loop
1345 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1350 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1352 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1353 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1354 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1356 // take the last of the two
1357 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1358 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1359 if ( nPosDos
> nPosUnix
)
1361 // size_t nLen = Strlen(pszFileName);
1364 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1366 size_t nPosDot
= pDot
- pszFileName
;
1368 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1370 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1374 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1380 //------------------------------------------------------------------------
1381 // wild character routines
1382 //------------------------------------------------------------------------
1384 bool wxIsWild( const wxString
& pattern
)
1386 wxString tmp
= pattern
;
1387 char *pat
= WXSTRINGCAST(tmp
);
1390 case '?': case '*': case '[': case '{':
1400 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1401 #ifdef HAVE_FNMATCH_H
1404 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1406 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1410 #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1412 * WARNING: this code is broken!
1415 wxString tmp1
= pat
;
1416 char *pattern
= WXSTRINGCAST(tmp1
);
1417 wxString tmp2
= text
;
1418 char *str
= WXSTRINGCAST(tmp2
);
1421 bool done
= FALSE
, ret_code
, ok
;
1422 // Below is for vi fans
1423 const char OB
= '{', CB
= '}';
1425 // dot_special means '.' only matches '.'
1426 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1429 while ((*pattern
!= '\0') && (!done
)
1430 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1434 if (*pattern
!= '\0')
1441 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1444 while (*str
!= '\0')
1446 while (*pattern
!= '\0')
1453 if ((*pattern
== '\0') || (*pattern
== ']')) {
1457 if (*pattern
== '\\') {
1459 if (*pattern
== '\0') {
1464 if (*(pattern
+ 1) == '-') {
1467 if (*pattern
== ']') {
1471 if (*pattern
== '\\') {
1473 if (*pattern
== '\0') {
1478 if ((*str
< c
) || (*str
> *pattern
)) {
1482 } else if (*pattern
!= *str
) {
1487 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1488 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1492 if (*pattern
!= '\0') {
1502 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1505 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1506 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1507 if (*pattern
== '\\')
1509 ok
= (*pattern
++ == *cp
++);
1511 if (*pattern
== '\0') {
1517 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1518 if (*++pattern
== '\\') {
1519 if (*++pattern
== CB
)
1524 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1525 if (*++pattern
== '\\') {
1526 if (*++pattern
== CB
|| *pattern
== ',')
1531 if (*pattern
!= '\0')
1536 if (*str
== *pattern
) {
1543 while (*pattern
== '*')
1545 return ((*str
== '\0') && (*pattern
== '\0'));