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);
278 wxIsAbsolutePath (const wxString
& filename
)
282 if (filename
[0] == '/'
284 || (filename
[0] == '[' && filename
[1] != '.')
288 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
297 * Strip off any extension (dot something) from end of file,
298 * IF one exists. Inserts zero into buffer.
302 void wxStripExtension(char *buffer
)
304 int len
= strlen(buffer
);
308 if (buffer
[i
] == '.')
317 void wxStripExtension(wxString
& buffer
)
319 size_t len
= buffer
.Length();
323 if (buffer
.GetChar(i
) == '.')
325 buffer
= buffer
.Left(i
);
332 // Destructive removal of /./ and /../ stuff
333 char *wxRealPath (char *path
)
336 static const char SEP
= '\\';
337 Unix2DosFilename(path
);
339 static const char SEP
= '/';
341 if (path
[0] && path
[1]) {
342 /* MATTHEW: special case "/./x" */
344 if (path
[2] == SEP
&& path
[1] == '.')
352 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
355 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
356 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
357 && (q
- 1 <= path
|| q
[-1] != SEP
))
366 /* Check that path[2] is NULL! */
367 else if (path
[1] == ':' && !path
[2])
376 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
385 char *wxCopyAbsolutePath(const wxString
& filename
)
388 return (char *) NULL
;
390 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
391 char buf
[_MAXPATHLEN
];
393 wxGetWorkingDirectory(buf
, WXSIZEOF(buf
));
394 char ch
= buf
[strlen(buf
) - 1];
396 if (ch
!= '\\' && ch
!= '/')
402 strcat(buf
, wxBuffer
);
403 return copystring( wxRealPath(buf
) );
405 return copystring( wxBuffer
);
411 ~user/ => user's home dir
412 If the environment variable a = "foo" and b = "bar" then:
429 /* input name in name, pathname output to buf. */
431 char *wxExpandPath(char *buf
, const char *name
)
433 register char *d
, *s
, *nm
;
434 char lnm
[_MAXPATHLEN
];
437 // Some compilers don't like this line.
438 // const char trimchars[] = "\n \t";
447 const char SEP
= '\\';
449 const char SEP
= '/';
452 if (name
== NULL
|| *name
== '\0')
454 nm
= copystring(name
); // Make a scratch copy
457 /* Skip leading whitespace and cr */
458 while (strchr((char *)trimchars
, *nm
) != NULL
)
460 /* And strip off trailing whitespace and cr */
461 s
= nm
+ (q
= strlen(nm
)) - 1;
462 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
470 q
= nm
[0] == '\\' && nm
[1] == '~';
473 /* Expand inline environment variables */
474 while ((*d
++ = *s
)) {
477 if ((*(d
- 1) = *++s
)) {
485 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
490 register char *start
= d
;
491 register int braces
= (*s
== '{' || *s
== '(');
492 register char *value
;
494 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
499 value
= getenv(braces
? start
+ 1 : start
);
501 for ((d
= start
- 1); (*d
++ = *value
++););
509 /* Expand ~ and ~user */
512 if (nm
[0] == '~' && !q
)
515 if (nm
[1] == SEP
|| nm
[1] == 0)
517 if ((s
= wxGetUserHome("")) != NULL
) {
522 { /* ~user/filename */
525 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
526 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
527 was_sep
= (*s
== SEP
);
528 nnm
= *s
? s
+ 1 : s
;
530 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
531 if (was_sep
) /* replace only if it was there: */
542 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
544 while ('\0' != (*d
++ = *s
++))
547 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
551 while ((*d
++ = *s
++));
553 delete[] nm_tmp
; // clean up alloc
554 /* Now clean up the buffer */
555 return wxRealPath(buf
);
559 /* Contract Paths to be build upon an environment variable
562 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
564 The call wxExpandPath can convert these back!
567 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
569 static char dest
[_MAXPATHLEN
];
572 return (char *) NULL
;
574 strcpy (dest
, WXSTRINGCAST filename
);
576 Unix2DosFilename(dest
);
579 // Handle environment
580 char *val
= (char *) NULL
;
581 char *tcp
= (char *) NULL
;
582 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
583 (tcp
= strstr (dest
, val
)) != NULL
)
585 strcpy (wxBuffer
, tcp
+ strlen (val
));
588 strcpy (tcp
, WXSTRINGCAST envname
);
590 strcat (tcp
, wxBuffer
);
593 // Handle User's home (ignore root homes!)
595 if ((val
= wxGetUserHome (user
)) != NULL
&&
596 (len
= strlen(val
)) > 2 &&
597 strncmp(dest
, val
, len
) == 0)
599 strcpy(wxBuffer
, "~");
601 strcat(wxBuffer
, (const char*) user
);
603 // strcat(wxBuffer, "\\");
605 // strcat(wxBuffer, "/");
607 strcat(wxBuffer
, dest
+ len
);
608 strcpy (dest
, wxBuffer
);
614 // Return just the filename, not the path
616 char *wxFileNameFromPath (char *path
)
622 tcp
= path
+ strlen (path
);
623 while (--tcp
>= path
)
625 if (*tcp
== '/' || *tcp
== '\\'
627 || *tcp
== ':' || *tcp
== ']')
634 if (isalpha (*path
) && *(path
+ 1) == ':')
641 wxString
wxFileNameFromPath (const wxString
& path1
)
646 char *path
= WXSTRINGCAST path1
;
649 tcp
= path
+ strlen (path
);
650 while (--tcp
>= path
)
652 if (*tcp
== '/' || *tcp
== '\\'
654 || *tcp
== ':' || *tcp
== ']')
658 return wxString(tcp
+ 1);
661 if (isalpha (*path
) && *(path
+ 1) == ':')
662 return wxString(path
+ 2);
665 // Yes, this should return the path, not an empty string, otherwise
666 // we get "thing.txt" -> "".
670 // Return just the directory, or NULL if no directory
672 wxPathOnly (char *path
)
676 static char buf
[_MAXPATHLEN
];
681 int l
= strlen(path
);
686 // Search backward for a backward or forward slash
687 while (!done
&& i
> -1)
690 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
705 // Try Drive specifier
706 if (isalpha (buf
[0]) && buf
[1] == ':')
708 // A:junk --> A:. (since A:.\junk Not A:\junk)
716 return (char *) NULL
;
719 // Return just the directory, or NULL if no directory
720 wxString
wxPathOnly (const wxString
& path
)
724 char buf
[_MAXPATHLEN
];
727 strcpy (buf
, WXSTRINGCAST path
);
729 int l
= path
.Length();
734 // Search backward for a backward or forward slash
735 while (!done
&& i
> -1)
738 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
747 return wxString(buf
);
753 // Try Drive specifier
754 if (isalpha (buf
[0]) && buf
[1] == ':')
756 // A:junk --> A:. (since A:.\junk Not A:\junk)
759 return wxString(buf
);
767 // Utility for converting delimiters in DOS filenames to UNIX style
768 // and back again - or we get nasty problems with delimiters.
769 // Also, convert to lower case, since case is significant in UNIX.
773 wxMac2UnixFilename (char *s
)
777 memmove( s
+1 , s
,strlen( s
) + 1) ;
788 *s
= wxToLower (*s
); // Case INDEPENDENT
795 wxUnix2MacFilename (char *s
)
801 // relative path , since it goes on with slash which is translated to a :
802 memmove( s
, s
+1 ,strlen( s
) ) ;
804 else if ( *s
== '/' )
806 // absolute path -> on mac just start with the drive name
807 memmove( s
, s
+1 ,strlen( s
) ) ;
811 wxASSERT_MSG( 1 , "unkown path beginning" ) ;
815 if (*s
== '/' || *s
== '\\')
824 wxDos2UnixFilename (char *s
)
833 *s
= wxToLower (*s
); // Case INDEPENDENT
841 wxUnix2DosFilename (char *s
)
843 wxUnix2DosFilename (char *WXUNUSED(s
))
846 // Yes, I really mean this to happen under DOS only! JACS
858 // Concatenate two files to form third
860 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
862 char *outfile
= wxGetTempFileName("cat");
864 FILE *fp1
= (FILE *) NULL
;
865 FILE *fp2
= (FILE *) NULL
;
866 FILE *fp3
= (FILE *) NULL
;
867 // Open the inputs and outputs
869 strcpy( gwxMacFileName
, file1
) ;
870 wxUnix2MacFilename( gwxMacFileName
) ;
871 strcpy( gwxMacFileName2
, file2
) ;
872 wxUnix2MacFilename( gwxMacFileName2
) ;
873 strcpy( gwxMacFileName3
, outfile
) ;
874 wxUnix2MacFilename( gwxMacFileName3
) ;
876 if ((fp1
= fopen (gwxMacFileName
, "rb")) == NULL
||
877 (fp2
= fopen (gwxMacFileName2
, "rb")) == NULL
||
878 (fp3
= fopen (gwxMacFileName3
, "wb")) == NULL
)
880 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
881 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
882 (fp3
= fopen (outfile
, "wb")) == NULL
)
895 while ((ch
= getc (fp1
)) != EOF
)
896 (void) putc (ch
, fp3
);
899 while ((ch
= getc (fp2
)) != EOF
)
900 (void) putc (ch
, fp3
);
904 bool result
= wxRenameFile(outfile
, file3
);
911 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
918 strcpy( gwxMacFileName
, file1
) ;
919 wxUnix2MacFilename( gwxMacFileName
) ;
920 strcpy( gwxMacFileName2
, file2
) ;
921 wxUnix2MacFilename( gwxMacFileName2
) ;
923 if ((fd1
= fopen (gwxMacFileName
, "rb")) == NULL
)
925 if ((fd2
= fopen (gwxMacFileName2
, "wb")) == NULL
)
927 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
929 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
936 while ((ch
= getc (fd1
)) != EOF
)
937 (void) putc (ch
, fd2
);
945 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
948 strcpy( gwxMacFileName
, file1
) ;
949 wxUnix2MacFilename( gwxMacFileName
) ;
950 strcpy( gwxMacFileName2
, file2
) ;
951 wxUnix2MacFilename( gwxMacFileName2
) ;
953 if (0 == rename (gwxMacFileName
, gwxMacFileName2
))
956 // Normal system call
957 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
961 if (wxCopyFile(file1
, file2
)) {
969 bool wxRemoveFile(const wxString
& file
)
971 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(__WATCOMC__)
972 int flag
= remove(WXSTRINGCAST file
);
973 #elif defined( __WXMAC__ )
974 strcpy( gwxMacFileName
, file
) ;
975 wxUnix2MacFilename( gwxMacFileName
) ;
976 int flag
= unlink(gwxMacFileName
);
978 int flag
= unlink(WXSTRINGCAST file
);
983 bool wxMkdir(const wxString
& dir
)
985 #if defined(__WXSTUBS__)
987 #elif defined(__VMS__)
989 #elif defined( __WXMAC__ )
990 strcpy( gwxMacFileName
, dir
) ;
991 wxUnix2MacFilename( gwxMacFileName
) ;
992 return (mkdir(gwxMacFileName
, 0 ) == 0);
993 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
994 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
996 return (mkdir(WXSTRINGCAST dir
) == 0);
1000 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
1004 #elif defined( __WXMAC__ )
1005 strcpy( gwxMacFileName
, dir
) ;
1006 wxUnix2MacFilename( gwxMacFileName
) ;
1007 return (rmdir(WXSTRINGCAST gwxMacFileName
) == 0);
1011 return FALSE
; // What to do?
1013 return (rmdir(WXSTRINGCAST dir
) == 0);
1020 bool wxDirExists(const wxString
& dir
)
1024 #elif !defined(__WXMSW__)
1026 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
1029 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
1030 #if defined(__WIN32__)
1031 WIN32_FIND_DATA fileInfo
;
1034 struct ffblk fileInfo
;
1036 struct find_t fileInfo
;
1040 #if defined(__WIN32__)
1041 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
1043 if (h
==INVALID_HANDLE_VALUE
)
1047 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
1050 // In Borland findfirst has a different argument
1051 // ordering from _dos_findfirst. But _dos_findfirst
1052 // _should_ be ok in both MS and Borland... why not?
1054 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
1056 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
1065 // does the path exists? (may have or not '/' or '\\' at the end)
1066 bool wxPathExists(const char *pszPathName
)
1068 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
1069 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
1070 wxString
strPath(pszPathName
);
1071 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
1072 strPath
.Last() = '\0';
1080 return stat((char*) (const char*) strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
1083 // Get a temporary filename, opening and closing the file.
1084 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
1090 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
1093 char tmpPath
[MAX_PATH
];
1094 ::GetTempPath(MAX_PATH
, tmpPath
);
1095 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
1097 if (buf
) strcpy(buf
, tmp
);
1098 else buf
= copystring(tmp
);
1102 static short last_temp
= 0; // cache last to speed things a bit
1103 // At most 1000 temp files to a process! We use a ring count.
1104 char tmp
[100]; // FIXME static buffer
1106 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
1108 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
1109 if (!wxFileExists( tmp
))
1111 // Touch the file to create it (reserve name)
1112 FILE *fd
= fopen (tmp
, "w");
1119 buf
= copystring( tmp
);
1123 wxLogError( _("wxWindows: error finding temporary file name.\n") );
1124 if (buf
) buf
[0] = 0;
1125 return (char *) NULL
;
1129 // Get first file name matching given wild card.
1133 // Get first file name matching given wild card.
1134 // Flags are reserved for future use.
1137 static DIR *gs_dirStream
= (DIR *) NULL
;
1138 static wxString gs_strFileSpec
;
1139 static int gs_findFlags
= 0;
1142 wxString
wxFindFirstFile(const char *spec
, int flags
)
1148 closedir(gs_dirStream
); // edz 941103: better housekeping
1150 gs_findFlags
= flags
;
1152 gs_strFileSpec
= spec
;
1154 // Find path only so we can concatenate
1155 // found file onto path
1156 wxString
path(wxPathOnly(gs_strFileSpec
));
1158 // special case: path is really "/"
1159 if ( !path
&& gs_strFileSpec
[0u] == '/' )
1161 // path is empty => Local directory
1165 gs_dirStream
= opendir(path
);
1166 if ( !gs_dirStream
)
1168 wxLogSysError(_("Can not enumerate files in directory '%s'"),
1173 result
= wxFindNextFile();
1180 wxString
wxFindNextFile()
1185 wxCHECK_MSG( gs_dirStream
, result
, "must call wxFindFirstFile first" );
1187 // Find path only so we can concatenate
1188 // found file onto path
1189 wxString
path(wxPathOnly(gs_strFileSpec
));
1190 wxString
name(wxFileNameFromPath(gs_strFileSpec
));
1192 /* MATTHEW: special case: path is really "/" */
1193 if ( !path
&& gs_strFileSpec
[0u] == '/')
1197 struct dirent
*nextDir
;
1198 for ( nextDir
= readdir(gs_dirStream
);
1200 nextDir
= readdir(gs_dirStream
) )
1202 if (wxMatchWild(name
, nextDir
->d_name
))
1205 if ( !path
.IsEmpty() )
1212 result
+= nextDir
->d_name
;
1214 // Only return "." and ".." when they match
1216 if ( (strcmp(nextDir
->d_name
, ".") == 0) ||
1217 (strcmp(nextDir
->d_name
, "..") == 0))
1219 if ( (gs_findFlags
& wxDIR
) != 0 )
1225 isdir
= wxDirExists(result
);
1227 // and only return directories when flags & wxDIR
1228 if ( !gs_findFlags
||
1229 ((gs_findFlags
& wxDIR
) && isdir
) ||
1230 ((gs_findFlags
& wxFILE
) && !isdir
) )
1237 result
.Empty(); // not found
1239 closedir(gs_dirStream
);
1240 gs_dirStream
= (DIR *) NULL
;
1246 #elif defined(__WXMSW__)
1249 static HANDLE gs_hFileStruct
= INVALID_HANDLE_VALUE
;
1250 static WIN32_FIND_DATA gs_findDataStruct
;
1253 static struct ffblk gs_findDataStruct
;
1255 static struct _find_t gs_findDataStruct
;
1259 static wxString gs_strFileSpec
;
1260 static int gs_findFlags
= 0;
1262 wxString
wxFindFirstFile(const char *spec
, int flags
)
1266 gs_strFileSpec
= spec
;
1267 gs_findFlags
= flags
; /* MATTHEW: [5] Remember flags */
1269 // Find path only so we can concatenate found file onto path
1270 wxString
path(wxPathOnly(gs_strFileSpec
));
1271 if ( !path
.IsEmpty() )
1272 result
<< path
<< '\\';
1275 if ( gs_hFileStruct
!= INVALID_HANDLE_VALUE
)
1276 FindClose(gs_hFileStruct
);
1278 gs_hFileStruct
= ::FindFirstFile(WXSTRINGCAST spec
, &gs_findDataStruct
);
1280 if ( gs_hFileStruct
== INVALID_HANDLE_VALUE
)
1282 wxLogSysError(_("Can not enumerate files in directory '%s'"),
1290 bool isdir
= !!(gs_findDataStruct
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1292 if (isdir
&& !(flags
& wxDIR
))
1293 return wxFindNextFile();
1294 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1295 return wxFindNextFile();
1297 result
+= gs_findDataStruct
.cFileName
;
1301 int flag
= _A_NORMAL
;
1302 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1306 if (findfirst(WXSTRINGCAST spec
, &gs_findDataStruct
, flag
) == 0)
1308 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &gs_findDataStruct
) == 0)
1311 /* MATTHEW: [5] Check directory flag */
1315 attrib
= gs_findDataStruct
.ff_attrib
;
1317 attrib
= gs_findDataStruct
.attrib
;
1320 if (attrib
& _A_SUBDIR
) {
1321 if (!(gs_findFlags
& wxDIR
))
1322 return wxFindNextFile();
1323 } else if (gs_findFlags
&& !(gs_findFlags
& wxFILE
))
1324 return wxFindNextFile();
1328 gs_findDataStruct
.ff_name
1330 gs_findDataStruct
.name
1339 wxString
wxFindNextFile()
1343 // Find path only so we can concatenate found file onto path
1344 wxString
path(wxPathOnly(gs_strFileSpec
));
1349 if (gs_hFileStruct
== INVALID_HANDLE_VALUE
)
1352 bool success
= (FindNextFile(gs_hFileStruct
, &gs_findDataStruct
) != 0);
1355 FindClose(gs_hFileStruct
);
1356 gs_hFileStruct
= INVALID_HANDLE_VALUE
;
1360 bool isdir
= !!(gs_findDataStruct
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1362 if (isdir
&& !(gs_findFlags
& wxDIR
))
1364 else if (!isdir
&& gs_findFlags
&& !(gs_findFlags
& wxFILE
))
1367 if ( !path
.IsEmpty() )
1368 result
<< path
<< '\\';
1369 result
<< gs_findDataStruct
.cFileName
;
1376 if (findnext(&gs_findDataStruct
) == 0)
1378 if (_dos_findnext(&gs_findDataStruct
) == 0)
1381 /* MATTHEW: [5] Check directory flag */
1385 attrib
= gs_findDataStruct
.ff_attrib
;
1387 attrib
= gs_findDataStruct
.attrib
;
1390 if (attrib
& _A_SUBDIR
) {
1391 if (!(gs_findFlags
& wxDIR
))
1393 } else if (gs_findFlags
&& !(gs_findFlags
& wxFILE
))
1399 gs_findDataStruct
.ff_name
1401 gs_findDataStruct
.name
1410 #endif // Unix/Windows
1412 // Get current working directory.
1413 // If buf is NULL, allocates space using new, else
1415 char *wxGetWorkingDirectory(char *buf
, int sz
)
1418 buf
= new char[sz
+1];
1420 if (_getcwd(buf
, sz
) == NULL
) {
1422 if (getcwd(buf
, sz
) == NULL
) {
1432 return wxString(wxGetWorkingDirectory());
1435 bool wxSetWorkingDirectory(const wxString
& d
)
1437 #if defined( __UNIX__ ) || defined( __WXMAC__ )
1438 return (chdir(d
) == 0);
1439 #elif defined(__WINDOWS__)
1442 return (bool)(SetCurrentDirectory(d
) != 0);
1444 // Must change drive, too.
1445 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1448 char firstChar
= d
[0];
1452 firstChar
= firstChar
- 32;
1454 // To a drive number
1455 unsigned int driveNo
= firstChar
- 64;
1458 unsigned int noDrives
;
1459 _dos_setdrive(driveNo
, &noDrives
);
1462 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1470 // Get the OS directory if appropriate (such as the Windows directory).
1471 // On non-Windows platform, probably just return the empty string.
1472 wxString
wxGetOSDirectory()
1476 GetWindowsDirectory(buf
, 256);
1477 return wxString(buf
);
1479 return wxEmptyString
;
1483 bool wxEndsWithPathSeparator(const char *pszFileName
)
1485 size_t len
= Strlen(pszFileName
);
1489 return wxIsPathSeparator(pszFileName
[len
- 1]);
1492 // find a file in a list of directories, returns false if not found
1493 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1495 // we assume that it's not empty
1496 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1497 _("empty file name in wxFindFileInPath"));
1499 // skip path separator in the beginning of the file name if present
1500 if ( wxIsPathSeparator(*pszFile
) )
1503 // copy the path (strtok will modify it)
1504 char *szPath
= new char[strlen(pszPath
) + 1];
1505 strcpy(szPath
, pszPath
);
1509 for ( pc
= strtok(szPath
, wxPATH_SEP
);
1511 pc
= strtok((char *) NULL
, wxPATH_SEP
) )
1513 // search for the file in this directory
1515 if ( !wxEndsWithPathSeparator(pc
) )
1516 strFile
+= wxFILE_SEP_PATH
;
1519 if ( FileExists(strFile
) ) {
1527 return pc
!= NULL
; // if true => we breaked from the loop
1530 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1535 // it can be empty, but it shouldn't be NULL
1536 wxCHECK_RET( pszFileName
, "NULL file name in wxSplitPath" );
1538 const char *pDot
= strrchr(pszFileName
, wxFILE_SEP_EXT
);
1541 // under Windows we understand both separators
1542 const char *pSepUnix
= strrchr(pszFileName
, wxFILE_SEP_PATH_UNIX
);
1543 const char *pSepDos
= strrchr(pszFileName
, wxFILE_SEP_PATH_DOS
);
1544 const char *pLastSeparator
= pSepUnix
> pSepDos
? pSepUnix
: pSepDos
;
1545 #else // assume Unix
1546 const char *pLastSeparator
= strrchr(pszFileName
, wxFILE_SEP_PATH_UNIX
);
1548 if ( pDot
== pszFileName
)
1550 // under Unix files like .profile are treated in a special way
1555 if ( pDot
< pLastSeparator
)
1557 // the dot is part of the path, not the start of the extension
1563 if ( pLastSeparator
)
1564 *pstrPath
= wxString(pszFileName
, pLastSeparator
- pszFileName
);
1571 const char *start
= pLastSeparator
? pLastSeparator
+ 1 : pszFileName
;
1572 const char *end
= pDot
? pDot
: pszFileName
+ strlen(pszFileName
);
1574 *pstrName
= wxString(start
, end
- start
);
1580 *pstrExt
= wxString(pDot
+ 1);
1586 //------------------------------------------------------------------------
1587 // wild character routines
1588 //------------------------------------------------------------------------
1590 bool wxIsWild( const wxString
& pattern
)
1592 wxString tmp
= pattern
;
1593 char *pat
= WXSTRINGCAST(tmp
);
1596 case '?': case '*': case '[': case '{':
1606 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1608 #if defined(HAVE_FNMATCH_H)
1611 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1613 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1617 // #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1620 * WARNING: this code is broken!
1623 wxString tmp1
= pat
;
1624 char *pattern
= WXSTRINGCAST(tmp1
);
1625 wxString tmp2
= text
;
1626 char *str
= WXSTRINGCAST(tmp2
);
1629 bool done
= FALSE
, ret_code
, ok
;
1630 // Below is for vi fans
1631 const char OB
= '{', CB
= '}';
1633 // dot_special means '.' only matches '.'
1634 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1637 while ((*pattern
!= '\0') && (!done
)
1638 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1642 if (*pattern
!= '\0')
1649 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1652 while (*str
!= '\0')
1654 while (*pattern
!= '\0')
1661 if ((*pattern
== '\0') || (*pattern
== ']')) {
1665 if (*pattern
== '\\') {
1667 if (*pattern
== '\0') {
1672 if (*(pattern
+ 1) == '-') {
1675 if (*pattern
== ']') {
1679 if (*pattern
== '\\') {
1681 if (*pattern
== '\0') {
1686 if ((*str
< c
) || (*str
> *pattern
)) {
1690 } else if (*pattern
!= *str
) {
1695 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1696 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1700 if (*pattern
!= '\0') {
1710 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1713 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1714 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1715 if (*pattern
== '\\')
1717 ok
= (*pattern
++ == *cp
++);
1719 if (*pattern
== '\0') {
1725 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1726 if (*++pattern
== '\\') {
1727 if (*++pattern
== CB
)
1732 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1733 if (*++pattern
== '\\') {
1734 if (*++pattern
== CB
|| *pattern
== ',')
1739 if (*pattern
!= '\0')
1744 if (*str
== *pattern
) {
1751 while (*pattern
== '*')
1753 return ((*str
== '\0') && (*pattern
== '\0'));
1759 #pragma warning(default:4706) // assignment within conditional expression