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))
49 #include <sys/types.h>
62 #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) && !defined(__SALFORDC__)
66 #endif // native Win compiler
70 #include <sys/unistd.h>
73 #define stricmp strcasecmp
76 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
77 // this (3.1 I believe) and how to test for it.
78 // If this works for Borland 4.0 as well, then no worries.
90 // No, Cygwin doesn't appear to have fnmatch.h after all.
91 #if defined(HAVE_FNMATCH_H)
99 #define _MAXPATHLEN 500
101 extern char *wxBuffer
;
103 extern char gwxMacFileName
[] ;
104 extern char gwxMacFileName2
[] ;
105 extern char gwxMacFileName3
[] ;
108 #if !USE_SHARED_LIBRARIES
109 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
112 void wxPathList::Add (const wxString
& path
)
114 wxStringList::Add ((char *)(const char *)path
);
117 // Add paths e.g. from the PATH environment variable
118 void wxPathList::AddEnvList (const wxString
& envVariable
)
120 static const char PATH_TOKS
[] =
122 " ;"; // Don't seperate with colon in DOS (used for drive)
127 char *val
= getenv (WXSTRINGCAST envVariable
);
130 char *s
= copystring (val
);
131 char *token
= strtok (s
, PATH_TOKS
);
135 Add (copystring (token
));
138 if ((token
= strtok ((char *) NULL
, PATH_TOKS
)) != NULL
)
139 Add (wxString(token
));
146 // Given a full filename (with path), ensure that that file can
147 // be accessed again USING FILENAME ONLY by adding the path
148 // to the list if not already there.
149 void wxPathList::EnsureFileAccessible (const wxString
& path
)
151 wxString
path_only(wxPathOnly(path
));
152 if ( !path_only
.IsEmpty() )
154 if ( !Member(path_only
) )
159 bool wxPathList::Member (const wxString
& path
)
161 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
163 wxString
path2((char *) node
->Data ());
165 #if defined(__WINDOWS__) || defined(__VMS__) || defined (__WXMAC__)
167 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
169 // Case sensitive File System
170 path
.CompareTo (path2
) == 0
178 wxString
wxPathList::FindValidPath (const wxString
& file
)
180 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
181 return wxString(wxBuffer
);
183 char buf
[_MAXPATHLEN
];
184 strcpy(buf
, wxBuffer
);
186 char *filename
= (char*) NULL
; /* shut up buggy egcs warning */
187 filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
189 for (wxNode
* node
= First (); node
; node
= node
->Next ())
191 char *path
= (char *) node
->Data ();
192 strcpy (wxBuffer
, path
);
193 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
194 if (ch
!= '\\' && ch
!= '/')
195 strcat (wxBuffer
, "/");
196 strcat (wxBuffer
, filename
);
198 Unix2DosFilename (wxBuffer
);
200 if (wxFileExists (wxBuffer
))
202 return wxString(wxBuffer
); // Found!
206 return wxString(""); // Not found
209 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
211 wxString f
= FindValidPath(file
);
212 if (wxIsAbsolutePath(f
))
217 wxGetWorkingDirectory(buf
, 499);
218 int len
= (int)strlen(buf
);
222 if (lastCh
!= '/' && lastCh
!= '\\')
230 strcat(buf
, (const char *)f
);
231 strcpy(wxBuffer
, buf
);
232 return wxString(wxBuffer
);
237 wxFileExists (const wxString
& filename
)
239 #ifdef __GNUWIN32__ // (fix a B20 bug)
240 if (GetFileAttributes(filename
) == 0xFFFFFFFF)
244 #elif defined(__WXMAC__)
246 strcpy( gwxMacFileName
, filename
) ;
247 wxUnix2MacFilename( gwxMacFileName
) ;
248 if (gwxMacFileName
&& stat ((char *)(const char *)gwxMacFileName
, &stbuf
) == 0)
259 if ((filename
!= "") && stat ((char *)(const char *)filename
, &stbuf
) == 0)
265 /* Vadim's alternative implementation
267 // does the file exist?
268 bool wxFileExists(const char *pszFileName)
271 return !access(pszFileName, 0) &&
272 !stat(pszFileName, &st) &&
273 (st.st_mode & S_IFREG);
277 bool wxDirExists( const wxString
& dir
)
280 return ((stat(dir
, &st
) != -1) && S_ISDIR(st
.st_mode
) ? TRUE
: FALSE
);
284 wxIsAbsolutePath (const wxString
& filename
)
288 if (filename
[0] == '/'
290 || (filename
[0] == '[' && filename
[1] != '.')
294 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
303 * Strip off any extension (dot something) from end of file,
304 * IF one exists. Inserts zero into buffer.
308 void wxStripExtension(char *buffer
)
310 int len
= strlen(buffer
);
314 if (buffer
[i
] == '.')
323 void wxStripExtension(wxString
& buffer
)
325 size_t len
= buffer
.Length();
329 if (buffer
.GetChar(i
) == '.')
331 buffer
= buffer
.Left(i
);
338 // Destructive removal of /./ and /../ stuff
339 char *wxRealPath (char *path
)
342 static const char SEP
= '\\';
343 Unix2DosFilename(path
);
345 static const char SEP
= '/';
347 if (path
[0] && path
[1]) {
348 /* MATTHEW: special case "/./x" */
350 if (path
[2] == SEP
&& path
[1] == '.')
358 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
361 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
362 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
363 && (q
- 1 <= path
|| q
[-1] != SEP
))
372 /* Check that path[2] is NULL! */
373 else if (path
[1] == ':' && !path
[2])
382 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
391 char *wxCopyAbsolutePath(const wxString
& filename
)
394 return (char *) NULL
;
396 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
397 char buf
[_MAXPATHLEN
];
399 wxGetWorkingDirectory(buf
, WXSIZEOF(buf
));
400 char ch
= buf
[strlen(buf
) - 1];
402 if (ch
!= '\\' && ch
!= '/')
408 strcat(buf
, wxBuffer
);
409 return copystring( wxRealPath(buf
) );
411 return copystring( wxBuffer
);
417 ~user/ => user's home dir
418 If the environment variable a = "foo" and b = "bar" then:
435 /* input name in name, pathname output to buf. */
437 char *wxExpandPath(char *buf
, const char *name
)
439 register char *d
, *s
, *nm
;
440 char lnm
[_MAXPATHLEN
];
443 // Some compilers don't like this line.
444 // const char trimchars[] = "\n \t";
453 const char SEP
= '\\';
455 const char SEP
= '/';
458 if (name
== NULL
|| *name
== '\0')
460 nm
= copystring(name
); // Make a scratch copy
463 /* Skip leading whitespace and cr */
464 while (strchr((char *)trimchars
, *nm
) != NULL
)
466 /* And strip off trailing whitespace and cr */
467 s
= nm
+ (q
= strlen(nm
)) - 1;
468 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
476 q
= nm
[0] == '\\' && nm
[1] == '~';
479 /* Expand inline environment variables */
480 while ((*d
++ = *s
)) {
483 if ((*(d
- 1) = *++s
)) {
491 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
496 register char *start
= d
;
497 register int braces
= (*s
== '{' || *s
== '(');
498 register char *value
;
500 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
505 value
= getenv(braces
? start
+ 1 : start
);
507 for ((d
= start
- 1); (*d
++ = *value
++););
515 /* Expand ~ and ~user */
518 if (nm
[0] == '~' && !q
)
521 if (nm
[1] == SEP
|| nm
[1] == 0)
523 if ((s
= wxGetUserHome("")) != NULL
) {
528 { /* ~user/filename */
531 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
532 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
533 was_sep
= (*s
== SEP
);
534 nnm
= *s
? s
+ 1 : s
;
536 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
537 if (was_sep
) /* replace only if it was there: */
548 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
550 while ('\0' != (*d
++ = *s
++))
553 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
557 while ((*d
++ = *s
++));
559 delete[] nm_tmp
; // clean up alloc
560 /* Now clean up the buffer */
561 return wxRealPath(buf
);
565 /* Contract Paths to be build upon an environment variable
568 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
570 The call wxExpandPath can convert these back!
573 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
575 static char dest
[_MAXPATHLEN
];
578 return (char *) NULL
;
580 strcpy (dest
, WXSTRINGCAST filename
);
582 Unix2DosFilename(dest
);
585 // Handle environment
586 char *val
= (char *) NULL
;
587 char *tcp
= (char *) NULL
;
588 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
589 (tcp
= strstr (dest
, val
)) != NULL
)
591 strcpy (wxBuffer
, tcp
+ strlen (val
));
594 strcpy (tcp
, WXSTRINGCAST envname
);
596 strcat (tcp
, wxBuffer
);
599 // Handle User's home (ignore root homes!)
601 if ((val
= wxGetUserHome (user
)) != NULL
&&
602 (len
= strlen(val
)) > 2 &&
603 strncmp(dest
, val
, len
) == 0)
605 strcpy(wxBuffer
, "~");
607 strcat(wxBuffer
, (const char*) user
);
609 // strcat(wxBuffer, "\\");
611 // strcat(wxBuffer, "/");
613 strcat(wxBuffer
, dest
+ len
);
614 strcpy (dest
, wxBuffer
);
620 // Return just the filename, not the path
622 char *wxFileNameFromPath (char *path
)
628 tcp
= path
+ strlen (path
);
629 while (--tcp
>= path
)
631 if (*tcp
== '/' || *tcp
== '\\'
633 || *tcp
== ':' || *tcp
== ']')
640 if (isalpha (*path
) && *(path
+ 1) == ':')
647 wxString
wxFileNameFromPath (const wxString
& path1
)
652 char *path
= WXSTRINGCAST path1
;
655 tcp
= path
+ strlen (path
);
656 while (--tcp
>= path
)
658 if (*tcp
== '/' || *tcp
== '\\'
660 || *tcp
== ':' || *tcp
== ']')
664 return wxString(tcp
+ 1);
667 if (isalpha (*path
) && *(path
+ 1) == ':')
668 return wxString(path
+ 2);
671 // Yes, this should return the path, not an empty string, otherwise
672 // we get "thing.txt" -> "".
676 // Return just the directory, or NULL if no directory
678 wxPathOnly (char *path
)
682 static char buf
[_MAXPATHLEN
];
687 int l
= strlen(path
);
692 // Search backward for a backward or forward slash
693 while (!done
&& i
> -1)
696 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
711 // Try Drive specifier
712 if (isalpha (buf
[0]) && buf
[1] == ':')
714 // A:junk --> A:. (since A:.\junk Not A:\junk)
722 return (char *) NULL
;
725 // Return just the directory, or NULL if no directory
726 wxString
wxPathOnly (const wxString
& path
)
730 char buf
[_MAXPATHLEN
];
733 strcpy (buf
, WXSTRINGCAST path
);
735 int l
= path
.Length();
740 // Search backward for a backward or forward slash
741 while (!done
&& i
> -1)
744 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
753 return wxString(buf
);
759 // Try Drive specifier
760 if (isalpha (buf
[0]) && buf
[1] == ':')
762 // A:junk --> A:. (since A:.\junk Not A:\junk)
765 return wxString(buf
);
773 // Utility for converting delimiters in DOS filenames to UNIX style
774 // and back again - or we get nasty problems with delimiters.
775 // Also, convert to lower case, since case is significant in UNIX.
779 wxMac2UnixFilename (char *s
)
783 memmove( s
+1 , s
,strlen( s
) + 1) ;
794 *s
= tolower(*s
); // Case INDEPENDENT
801 wxUnix2MacFilename (char *s
)
807 // relative path , since it goes on with slash which is translated to a :
808 memmove( s
, s
+1 ,strlen( s
) ) ;
810 else if ( *s
== '/' )
812 // absolute path -> on mac just start with the drive name
813 memmove( s
, s
+1 ,strlen( s
) ) ;
817 wxASSERT_MSG( 1 , "unkown path beginning" ) ;
821 if (*s
== '/' || *s
== '\\')
830 wxDos2UnixFilename (char *s
)
839 *s
= tolower(*s
); // Case INDEPENDENT
847 wxUnix2DosFilename (char *s
)
849 wxUnix2DosFilename (char *WXUNUSED(s
))
852 // Yes, I really mean this to happen under DOS only! JACS
864 // Concatenate two files to form third
866 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
868 char *outfile
= wxGetTempFileName("cat");
870 FILE *fp1
= (FILE *) NULL
;
871 FILE *fp2
= (FILE *) NULL
;
872 FILE *fp3
= (FILE *) NULL
;
873 // Open the inputs and outputs
875 strcpy( gwxMacFileName
, file1
) ;
876 wxUnix2MacFilename( gwxMacFileName
) ;
877 strcpy( gwxMacFileName2
, file2
) ;
878 wxUnix2MacFilename( gwxMacFileName2
) ;
879 strcpy( gwxMacFileName3
, outfile
) ;
880 wxUnix2MacFilename( gwxMacFileName3
) ;
882 if ((fp1
= fopen (gwxMacFileName
, "rb")) == NULL
||
883 (fp2
= fopen (gwxMacFileName2
, "rb")) == NULL
||
884 (fp3
= fopen (gwxMacFileName3
, "wb")) == NULL
)
886 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
887 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
888 (fp3
= fopen (outfile
, "wb")) == NULL
)
901 while ((ch
= getc (fp1
)) != EOF
)
902 (void) putc (ch
, fp3
);
905 while ((ch
= getc (fp2
)) != EOF
)
906 (void) putc (ch
, fp3
);
910 bool result
= wxRenameFile(outfile
, file3
);
917 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
924 strcpy( gwxMacFileName
, file1
) ;
925 wxUnix2MacFilename( gwxMacFileName
) ;
926 strcpy( gwxMacFileName2
, file2
) ;
927 wxUnix2MacFilename( gwxMacFileName2
) ;
929 if ((fd1
= fopen (gwxMacFileName
, "rb")) == NULL
)
931 if ((fd2
= fopen (gwxMacFileName2
, "wb")) == NULL
)
933 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
935 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
942 while ((ch
= getc (fd1
)) != EOF
)
943 (void) putc (ch
, fd2
);
951 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
954 strcpy( gwxMacFileName
, file1
) ;
955 wxUnix2MacFilename( gwxMacFileName
) ;
956 strcpy( gwxMacFileName2
, file2
) ;
957 wxUnix2MacFilename( gwxMacFileName2
) ;
959 if (0 == rename (gwxMacFileName
, gwxMacFileName2
))
962 // Normal system call
963 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
967 if (wxCopyFile(file1
, file2
)) {
975 bool wxRemoveFile(const wxString
& file
)
977 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(__WATCOMC__)
978 int flag
= remove(WXSTRINGCAST file
);
979 #elif defined( __WXMAC__ )
980 strcpy( gwxMacFileName
, file
) ;
981 wxUnix2MacFilename( gwxMacFileName
) ;
982 int flag
= unlink(gwxMacFileName
);
984 int flag
= unlink(WXSTRINGCAST file
);
989 bool wxMkdir(const wxString
& dir
)
991 #if defined(__WXSTUBS__)
993 #elif defined(__VMS__)
995 #elif defined( __WXMAC__ )
996 strcpy( gwxMacFileName
, dir
) ;
997 wxUnix2MacFilename( gwxMacFileName
) ;
998 return (mkdir(gwxMacFileName
, 0 ) == 0);
999 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
1000 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
1002 return (mkdir(WXSTRINGCAST dir
) == 0);
1006 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
1010 #elif defined( __WXMAC__ )
1011 strcpy( gwxMacFileName
, dir
) ;
1012 wxUnix2MacFilename( gwxMacFileName
) ;
1013 return (rmdir(WXSTRINGCAST gwxMacFileName
) == 0);
1017 return FALSE
; // What to do?
1019 return (rmdir(WXSTRINGCAST dir
) == 0);
1026 bool wxDirExists(const wxString
& dir
)
1030 #elif !defined(__WXMSW__)
1032 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
1035 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
1036 #if defined(__WIN32__)
1037 WIN32_FIND_DATA fileInfo
;
1040 struct ffblk fileInfo
;
1042 struct find_t fileInfo
;
1046 #if defined(__WIN32__)
1047 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
1049 if (h
==INVALID_HANDLE_VALUE
)
1053 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
1056 // In Borland findfirst has a different argument
1057 // ordering from _dos_findfirst. But _dos_findfirst
1058 // _should_ be ok in both MS and Borland... why not?
1060 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
1062 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
1071 // does the path exists? (may have or not '/' or '\\' at the end)
1072 bool wxPathExists(const char *pszPathName
)
1074 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
1075 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
1076 wxString
strPath(pszPathName
);
1077 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
1078 strPath
.Last() = '\0';
1086 return stat((char*) (const char*) strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
1089 // Get a temporary filename, opening and closing the file.
1090 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
1096 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
1099 char tmpPath
[MAX_PATH
];
1100 ::GetTempPath(MAX_PATH
, tmpPath
);
1101 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
1103 if (buf
) strcpy(buf
, tmp
);
1104 else buf
= copystring(tmp
);
1108 static short last_temp
= 0; // cache last to speed things a bit
1109 // At most 1000 temp files to a process! We use a ring count.
1110 char tmp
[100]; // FIXME static buffer
1112 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
1114 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
1115 if (!wxFileExists( tmp
))
1117 // Touch the file to create it (reserve name)
1118 FILE *fd
= fopen (tmp
, "w");
1125 buf
= copystring( tmp
);
1129 wxLogError( _("wxWindows: error finding temporary file name.\n") );
1130 if (buf
) buf
[0] = 0;
1131 return (char *) NULL
;
1135 // Get first file name matching given wild card.
1139 // Get first file name matching given wild card.
1140 // Flags are reserved for future use.
1143 static DIR *gs_dirStream
= (DIR *) NULL
;
1144 static wxString gs_strFileSpec
;
1145 static int gs_findFlags
= 0;
1148 wxString
wxFindFirstFile(const char *spec
, int flags
)
1154 closedir(gs_dirStream
); // edz 941103: better housekeping
1156 gs_findFlags
= flags
;
1158 gs_strFileSpec
= spec
;
1160 // Find path only so we can concatenate
1161 // found file onto path
1162 wxString
path(wxPathOnly(gs_strFileSpec
));
1164 // special case: path is really "/"
1165 if ( !path
&& gs_strFileSpec
[0u] == '/' )
1167 // path is empty => Local directory
1171 gs_dirStream
= opendir(path
);
1172 if ( !gs_dirStream
)
1174 wxLogSysError(_("Can not enumerate files in directory '%s'"),
1179 result
= wxFindNextFile();
1186 wxString
wxFindNextFile()
1191 wxCHECK_MSG( gs_dirStream
, result
, "must call wxFindFirstFile first" );
1193 // Find path only so we can concatenate
1194 // found file onto path
1195 wxString
path(wxPathOnly(gs_strFileSpec
));
1196 wxString
name(wxFileNameFromPath(gs_strFileSpec
));
1198 /* MATTHEW: special case: path is really "/" */
1199 if ( !path
&& gs_strFileSpec
[0u] == '/')
1203 struct dirent
*nextDir
;
1204 for ( nextDir
= readdir(gs_dirStream
);
1206 nextDir
= readdir(gs_dirStream
) )
1208 if (wxMatchWild(name
, nextDir
->d_name
))
1211 if ( !path
.IsEmpty() )
1218 result
+= nextDir
->d_name
;
1220 // Only return "." and ".." when they match
1222 if ( (strcmp(nextDir
->d_name
, ".") == 0) ||
1223 (strcmp(nextDir
->d_name
, "..") == 0))
1225 if ( (gs_findFlags
& wxDIR
) != 0 )
1231 isdir
= wxDirExists(result
);
1233 // and only return directories when flags & wxDIR
1234 if ( !gs_findFlags
||
1235 ((gs_findFlags
& wxDIR
) && isdir
) ||
1236 ((gs_findFlags
& wxFILE
) && !isdir
) )
1243 result
.Empty(); // not found
1245 closedir(gs_dirStream
);
1246 gs_dirStream
= (DIR *) NULL
;
1252 #elif defined(__WXMSW__)
1255 static HANDLE gs_hFileStruct
= INVALID_HANDLE_VALUE
;
1256 static WIN32_FIND_DATA gs_findDataStruct
;
1259 static struct ffblk gs_findDataStruct
;
1261 static struct _find_t gs_findDataStruct
;
1265 static wxString gs_strFileSpec
;
1266 static int gs_findFlags
= 0;
1268 wxString
wxFindFirstFile(const char *spec
, int flags
)
1272 gs_strFileSpec
= spec
;
1273 gs_findFlags
= flags
; /* MATTHEW: [5] Remember flags */
1275 // Find path only so we can concatenate found file onto path
1276 wxString
path(wxPathOnly(gs_strFileSpec
));
1277 if ( !path
.IsEmpty() )
1278 result
<< path
<< '\\';
1281 if ( gs_hFileStruct
!= INVALID_HANDLE_VALUE
)
1282 FindClose(gs_hFileStruct
);
1284 gs_hFileStruct
= ::FindFirstFile(WXSTRINGCAST spec
, &gs_findDataStruct
);
1286 if ( gs_hFileStruct
== INVALID_HANDLE_VALUE
)
1293 bool isdir
= !!(gs_findDataStruct
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1295 if (isdir
&& !(flags
& wxDIR
))
1296 return wxFindNextFile();
1297 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1298 return wxFindNextFile();
1300 result
+= gs_findDataStruct
.cFileName
;
1304 int flag
= _A_NORMAL
;
1305 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1309 if (findfirst(WXSTRINGCAST spec
, &gs_findDataStruct
, flag
) == 0)
1311 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &gs_findDataStruct
) == 0)
1314 /* MATTHEW: [5] Check directory flag */
1318 attrib
= gs_findDataStruct
.ff_attrib
;
1320 attrib
= gs_findDataStruct
.attrib
;
1323 if (attrib
& _A_SUBDIR
) {
1324 if (!(gs_findFlags
& wxDIR
))
1325 return wxFindNextFile();
1326 } else if (gs_findFlags
&& !(gs_findFlags
& wxFILE
))
1327 return wxFindNextFile();
1331 gs_findDataStruct
.ff_name
1333 gs_findDataStruct
.name
1342 wxString
wxFindNextFile()
1346 // Find path only so we can concatenate found file onto path
1347 wxString
path(wxPathOnly(gs_strFileSpec
));
1352 if (gs_hFileStruct
== INVALID_HANDLE_VALUE
)
1355 bool success
= (FindNextFile(gs_hFileStruct
, &gs_findDataStruct
) != 0);
1358 FindClose(gs_hFileStruct
);
1359 gs_hFileStruct
= INVALID_HANDLE_VALUE
;
1363 bool isdir
= !!(gs_findDataStruct
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1365 if (isdir
&& !(gs_findFlags
& wxDIR
))
1367 else if (!isdir
&& gs_findFlags
&& !(gs_findFlags
& wxFILE
))
1370 if ( !path
.IsEmpty() )
1371 result
<< path
<< '\\';
1372 result
<< gs_findDataStruct
.cFileName
;
1379 if (findnext(&gs_findDataStruct
) == 0)
1381 if (_dos_findnext(&gs_findDataStruct
) == 0)
1384 /* MATTHEW: [5] Check directory flag */
1388 attrib
= gs_findDataStruct
.ff_attrib
;
1390 attrib
= gs_findDataStruct
.attrib
;
1393 if (attrib
& _A_SUBDIR
) {
1394 if (!(gs_findFlags
& wxDIR
))
1396 } else if (gs_findFlags
&& !(gs_findFlags
& wxFILE
))
1402 gs_findDataStruct
.ff_name
1404 gs_findDataStruct
.name
1413 #endif // Unix/Windows
1415 // Get current working directory.
1416 // If buf is NULL, allocates space using new, else
1418 char *wxGetWorkingDirectory(char *buf
, int sz
)
1421 buf
= new char[sz
+1];
1423 if (_getcwd(buf
, sz
) == NULL
) {
1425 if (getcwd(buf
, sz
) == NULL
) {
1435 return wxString(wxGetWorkingDirectory());
1438 bool wxSetWorkingDirectory(const wxString
& d
)
1440 #if defined( __UNIX__ ) || defined( __WXMAC__ )
1441 return (chdir(d
) == 0);
1442 #elif defined(__WINDOWS__)
1445 return (bool)(SetCurrentDirectory(d
) != 0);
1447 // Must change drive, too.
1448 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1451 char firstChar
= d
[0];
1455 firstChar
= firstChar
- 32;
1457 // To a drive number
1458 unsigned int driveNo
= firstChar
- 64;
1461 unsigned int noDrives
;
1462 _dos_setdrive(driveNo
, &noDrives
);
1465 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1473 // Get the OS directory if appropriate (such as the Windows directory).
1474 // On non-Windows platform, probably just return the empty string.
1475 wxString
wxGetOSDirectory()
1479 GetWindowsDirectory(buf
, 256);
1480 return wxString(buf
);
1482 return wxEmptyString
;
1486 bool wxEndsWithPathSeparator(const char *pszFileName
)
1488 size_t len
= Strlen(pszFileName
);
1492 return wxIsPathSeparator(pszFileName
[len
- 1]);
1495 // find a file in a list of directories, returns false if not found
1496 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1498 // we assume that it's not empty
1499 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1500 _("empty file name in wxFindFileInPath"));
1502 // skip path separator in the beginning of the file name if present
1503 if ( wxIsPathSeparator(*pszFile
) )
1506 // copy the path (strtok will modify it)
1507 char *szPath
= new char[strlen(pszPath
) + 1];
1508 strcpy(szPath
, pszPath
);
1512 for ( pc
= strtok(szPath
, wxPATH_SEP
);
1514 pc
= strtok((char *) NULL
, wxPATH_SEP
) )
1516 // search for the file in this directory
1518 if ( !wxEndsWithPathSeparator(pc
) )
1519 strFile
+= wxFILE_SEP_PATH
;
1522 if ( FileExists(strFile
) ) {
1530 return pc
!= NULL
; // if true => we breaked from the loop
1533 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1538 // it can be empty, but it shouldn't be NULL
1539 wxCHECK_RET( pszFileName
, "NULL file name in wxSplitPath" );
1541 const char *pDot
= strrchr(pszFileName
, wxFILE_SEP_EXT
);
1544 // under Windows we understand both separators
1545 const char *pSepUnix
= strrchr(pszFileName
, wxFILE_SEP_PATH_UNIX
);
1546 const char *pSepDos
= strrchr(pszFileName
, wxFILE_SEP_PATH_DOS
);
1547 const char *pLastSeparator
= pSepUnix
> pSepDos
? pSepUnix
: pSepDos
;
1548 #else // assume Unix
1549 const char *pLastSeparator
= strrchr(pszFileName
, wxFILE_SEP_PATH_UNIX
);
1551 if ( pDot
== pszFileName
)
1553 // under Unix files like .profile are treated in a special way
1558 if ( pDot
< pLastSeparator
)
1560 // the dot is part of the path, not the start of the extension
1566 if ( pLastSeparator
)
1567 *pstrPath
= wxString(pszFileName
, pLastSeparator
- pszFileName
);
1574 const char *start
= pLastSeparator
? pLastSeparator
+ 1 : pszFileName
;
1575 const char *end
= pDot
? pDot
: pszFileName
+ strlen(pszFileName
);
1577 *pstrName
= wxString(start
, end
- start
);
1583 *pstrExt
= wxString(pDot
+ 1);
1589 //------------------------------------------------------------------------
1590 // wild character routines
1591 //------------------------------------------------------------------------
1593 bool wxIsWild( const wxString
& pattern
)
1595 wxString tmp
= pattern
;
1596 char *pat
= WXSTRINGCAST(tmp
);
1599 case '?': case '*': case '[': case '{':
1609 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1611 #if defined(HAVE_FNMATCH_H)
1614 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1616 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1620 // #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1623 * WARNING: this code is broken!
1626 wxString tmp1
= pat
;
1627 char *pattern
= WXSTRINGCAST(tmp1
);
1628 wxString tmp2
= text
;
1629 char *str
= WXSTRINGCAST(tmp2
);
1632 bool done
= FALSE
, ret_code
, ok
;
1633 // Below is for vi fans
1634 const char OB
= '{', CB
= '}';
1636 // dot_special means '.' only matches '.'
1637 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1640 while ((*pattern
!= '\0') && (!done
)
1641 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1645 if (*pattern
!= '\0')
1652 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1655 while (*str
!= '\0')
1657 while (*pattern
!= '\0')
1664 if ((*pattern
== '\0') || (*pattern
== ']')) {
1668 if (*pattern
== '\\') {
1670 if (*pattern
== '\0') {
1675 if (*(pattern
+ 1) == '-') {
1678 if (*pattern
== ']') {
1682 if (*pattern
== '\\') {
1684 if (*pattern
== '\0') {
1689 if ((*str
< c
) || (*str
> *pattern
)) {
1693 } else if (*pattern
!= *str
) {
1698 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1699 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1703 if (*pattern
!= '\0') {
1713 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1716 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1717 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1718 if (*pattern
== '\\')
1720 ok
= (*pattern
++ == *cp
++);
1722 if (*pattern
== '\0') {
1728 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1729 if (*++pattern
== '\\') {
1730 if (*++pattern
== CB
)
1735 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1736 if (*++pattern
== '\\') {
1737 if (*++pattern
== CB
|| *pattern
== ',')
1742 if (*pattern
!= '\0')
1747 if (*str
== *pattern
) {
1754 while (*pattern
== '*')
1756 return ((*str
== '\0') && (*pattern
== '\0'));
1762 #pragma warning(default:4706) // assignment within conditional expression