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))
47 #include <sys/types.h>
60 #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ )
67 #include <sys/unistd.h>
68 #define stricmp strcasecmp
71 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
72 // this (3.1 I believe) and how to test for it.
73 // If this works for Borland 4.0 as well, then no worries.
79 // No, Cygwin doesn't appear to have fnmatch.h after all.
80 #if defined(HAVE_FNMATCH_H)
88 #define _MAXPATHLEN 500
90 extern char *wxBuffer
;
92 extern char gwxMacFileName
[] ;
93 extern char gwxMacFileName2
[] ;
94 extern char gwxMacFileName3
[] ;
97 #if !USE_SHARED_LIBRARIES
98 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
101 void wxPathList::Add (const wxString
& path
)
103 wxStringList::Add ((char *)(const char *)path
);
106 // Add paths e.g. from the PATH environment variable
107 void wxPathList::AddEnvList (const wxString
& envVariable
)
109 static const char PATH_TOKS
[] =
111 " ;"; // Don't seperate with colon in DOS (used for drive)
116 char *val
= getenv (WXSTRINGCAST envVariable
);
119 char *s
= copystring (val
);
120 char *token
= strtok (s
, PATH_TOKS
);
124 Add (copystring (token
));
127 if ((token
= strtok ((char *) NULL
, PATH_TOKS
)) != NULL
)
128 Add (wxString(token
));
135 // Given a full filename (with path), ensure that that file can
136 // be accessed again USING FILENAME ONLY by adding the path
137 // to the list if not already there.
138 void wxPathList::EnsureFileAccessible (const wxString
& path
)
140 wxString
path1(path
);
141 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
144 if (!Member (wxString(path_only
)))
145 Add (wxString(path_only
));
149 bool wxPathList::Member (const wxString
& path
)
151 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
153 wxString
path2((char *) node
->Data ());
155 #if defined(__WINDOWS__) || defined(__VMS__) || defined (__WXMAC__)
157 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
159 // Case sensitive File System
160 path
.CompareTo (path2
) == 0
168 wxString
wxPathList::FindValidPath (const wxString
& file
)
170 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
171 return wxString(wxBuffer
);
173 char buf
[_MAXPATHLEN
];
174 strcpy(buf
, wxBuffer
);
176 char *filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
178 for (wxNode
* node
= First (); node
; node
= node
->Next ())
180 char *path
= (char *) node
->Data ();
181 strcpy (wxBuffer
, path
);
182 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
183 if (ch
!= '\\' && ch
!= '/')
184 strcat (wxBuffer
, "/");
185 strcat (wxBuffer
, filename
);
187 Unix2DosFilename (wxBuffer
);
189 if (wxFileExists (wxBuffer
))
191 return wxString(wxBuffer
); // Found!
195 return wxString(""); // Not found
198 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
200 wxString f
= FindValidPath(file
);
201 if (wxIsAbsolutePath(f
))
206 wxGetWorkingDirectory(buf
, 499);
207 int len
= (int)strlen(buf
);
211 if (lastCh
!= '/' && lastCh
!= '\\')
219 strcat(buf
, (const char *)f
);
220 strcpy(wxBuffer
, buf
);
221 return wxString(wxBuffer
);
226 wxFileExists (const wxString
& filename
)
228 #ifdef __GNUWIN32__ // (fix a B20 bug)
229 if (GetFileAttributes(filename
) == 0xFFFFFFFF)
233 #elif defined(__WXMAC__)
235 strcpy( gwxMacFileName
, filename
) ;
236 wxUnix2MacFilename( gwxMacFileName
) ;
237 if (gwxMacFileName
&& stat ((char *)(const char *)gwxMacFileName
, &stbuf
) == 0)
243 if (filename
&& stat ((char *)(const char *)filename
, &stbuf
) == 0)
249 /* Vadim's alternative implementation
251 // does the file exist?
252 bool wxFileExists(const char *pszFileName)
255 return !access(pszFileName, 0) &&
256 !stat(pszFileName, &st) &&
257 (st.st_mode & S_IFREG);
262 wxIsAbsolutePath (const wxString
& filename
)
266 if (filename
[0] == '/'
268 || (filename
[0] == '[' && filename
[1] != '.')
272 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
281 * Strip off any extension (dot something) from end of file,
282 * IF one exists. Inserts zero into buffer.
286 void wxStripExtension(char *buffer
)
288 int len
= strlen(buffer
);
292 if (buffer
[i
] == '.')
301 void wxStripExtension(wxString
& buffer
)
303 size_t len
= buffer
.Length();
307 if (buffer
.GetChar(i
) == '.')
309 buffer
= buffer
.Left(i
);
316 // Destructive removal of /./ and /../ stuff
317 char *wxRealPath (char *path
)
320 static const char SEP
= '\\';
321 Unix2DosFilename(path
);
323 static const char SEP
= '/';
325 if (path
[0] && path
[1]) {
326 /* MATTHEW: special case "/./x" */
328 if (path
[2] == SEP
&& path
[1] == '.')
336 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
339 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
340 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
341 && (q
- 1 <= path
|| q
[-1] != SEP
))
350 /* Check that path[2] is NULL! */
351 else if (path
[1] == ':' && !path
[2])
360 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
369 char *wxCopyAbsolutePath(const wxString
& filename
)
372 return (char *) NULL
;
374 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
375 char buf
[_MAXPATHLEN
];
377 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
378 char ch
= buf
[strlen(buf
) - 1];
380 if (ch
!= '\\' && ch
!= '/')
386 strcat(buf
, wxBuffer
);
387 return copystring( wxRealPath(buf
) );
389 return copystring( wxBuffer
);
395 ~user/ => user's home dir
396 If the environment variable a = "foo" and b = "bar" then:
413 /* input name in name, pathname output to buf. */
415 char *wxExpandPath(char *buf
, const char *name
)
417 register char *d
, *s
, *nm
;
418 char lnm
[_MAXPATHLEN
];
421 // Some compilers don't like this line.
422 // const char trimchars[] = "\n \t";
431 const char SEP
= '\\';
433 const char SEP
= '/';
436 if (name
== NULL
|| *name
== '\0')
438 nm
= copystring(name
); // Make a scratch copy
441 /* Skip leading whitespace and cr */
442 while (strchr((char *)trimchars
, *nm
) != NULL
)
444 /* And strip off trailing whitespace and cr */
445 s
= nm
+ (q
= strlen(nm
)) - 1;
446 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
454 q
= nm
[0] == '\\' && nm
[1] == '~';
457 /* Expand inline environment variables */
458 while ((*d
++ = *s
)) {
461 if ((*(d
- 1) = *++s
)) {
469 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
474 register char *start
= d
;
475 register int braces
= (*s
== '{' || *s
== '(');
476 register char *value
;
478 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
483 value
= getenv(braces
? start
+ 1 : start
);
485 for ((d
= start
- 1); (*d
++ = *value
++););
493 /* Expand ~ and ~user */
496 if (nm
[0] == '~' && !q
)
499 if (nm
[1] == SEP
|| nm
[1] == 0)
501 if ((s
= wxGetUserHome("")) != NULL
) {
506 { /* ~user/filename */
509 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
510 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
511 was_sep
= (*s
== SEP
);
512 nnm
= *s
? s
+ 1 : s
;
514 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
515 if (was_sep
) /* replace only if it was there: */
526 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
528 while ('\0' != (*d
++ = *s
++))
531 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
535 while ((*d
++ = *s
++));
537 delete[] nm_tmp
; // clean up alloc
538 /* Now clean up the buffer */
539 return wxRealPath(buf
);
543 /* Contract Paths to be build upon an environment variable
546 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
548 The call wxExpandPath can convert these back!
551 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
553 static char dest
[_MAXPATHLEN
];
556 return (char *) NULL
;
558 strcpy (dest
, WXSTRINGCAST filename
);
560 Unix2DosFilename(dest
);
563 // Handle environment
564 char *val
= (char *) NULL
;
565 char *tcp
= (char *) NULL
;
566 if (envname
!= WXSTRINGCAST NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
567 (tcp
= strstr (dest
, val
)) != NULL
)
569 strcpy (wxBuffer
, tcp
+ strlen (val
));
572 strcpy (tcp
, WXSTRINGCAST envname
);
574 strcat (tcp
, wxBuffer
);
577 // Handle User's home (ignore root homes!)
579 if ((val
= wxGetUserHome (user
)) != NULL
&&
580 (len
= strlen(val
)) > 2 &&
581 strncmp(dest
, val
, len
) == 0)
583 strcpy(wxBuffer
, "~");
585 strcat(wxBuffer
, user
);
587 // strcat(wxBuffer, "\\");
589 // strcat(wxBuffer, "/");
591 strcat(wxBuffer
, dest
+ len
);
592 strcpy (dest
, wxBuffer
);
598 // Return just the filename, not the path
600 char *wxFileNameFromPath (char *path
)
606 tcp
= path
+ strlen (path
);
607 while (--tcp
>= path
)
609 if (*tcp
== '/' || *tcp
== '\\'
611 || *tcp
== ':' || *tcp
== ']')
618 if (isalpha (*path
) && *(path
+ 1) == ':')
625 wxString
wxFileNameFromPath (const wxString
& path1
)
630 char *path
= WXSTRINGCAST path1
;
633 tcp
= path
+ strlen (path
);
634 while (--tcp
>= path
)
636 if (*tcp
== '/' || *tcp
== '\\'
638 || *tcp
== ':' || *tcp
== ']')
642 return wxString(tcp
+ 1);
645 if (isalpha (*path
) && *(path
+ 1) == ':')
646 return wxString(path
+ 2);
652 // Return just the directory, or NULL if no directory
654 wxPathOnly (char *path
)
658 static char buf
[_MAXPATHLEN
];
663 int l
= strlen(path
);
668 // Search backward for a backward or forward slash
669 while (!done
&& i
> -1)
672 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
687 // Try Drive specifier
688 if (isalpha (buf
[0]) && buf
[1] == ':')
690 // A:junk --> A:. (since A:.\junk Not A:\junk)
698 return (char *) NULL
;
701 // Return just the directory, or NULL if no directory
702 wxString
wxPathOnly (const wxString
& path
)
706 char buf
[_MAXPATHLEN
];
709 strcpy (buf
, WXSTRINGCAST path
);
711 int l
= path
.Length();
716 // Search backward for a backward or forward slash
717 while (!done
&& i
> -1)
720 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
729 return wxString(buf
);
735 // Try Drive specifier
736 if (isalpha (buf
[0]) && buf
[1] == ':')
738 // A:junk --> A:. (since A:.\junk Not A:\junk)
741 return wxString(buf
);
749 // Utility for converting delimiters in DOS filenames to UNIX style
750 // and back again - or we get nasty problems with delimiters.
751 // Also, convert to lower case, since case is significant in UNIX.
755 wxMac2UnixFilename (char *s
)
759 memmove( s
+1 , s
,strlen( s
) + 1) ;
770 *s
= wxToLower (*s
); // Case INDEPENDENT
777 wxUnix2MacFilename (char *s
)
783 // relative path , since it goes on with slash which is translated to a :
784 memmove( s
, s
+1 ,strlen( s
) ) ;
786 else if ( *s
== '/' )
788 // absolute path -> on mac just start with the drive name
789 memmove( s
, s
+1 ,strlen( s
) ) ;
793 wxASSERT_MSG( 1 , "unkown path beginning" ) ;
797 if (*s
== '/' || *s
== '\\')
806 wxDos2UnixFilename (char *s
)
815 *s
= wxToLower (*s
); // Case INDEPENDENT
823 wxUnix2DosFilename (char *s
)
825 wxUnix2DosFilename (char *WXUNUSED(s
))
828 // Yes, I really mean this to happen under DOS only! JACS
840 // Concatenate two files to form third
842 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
844 char *outfile
= wxGetTempFileName("cat");
846 FILE *fp1
= (FILE *) NULL
;
847 FILE *fp2
= (FILE *) NULL
;
848 FILE *fp3
= (FILE *) NULL
;
849 // Open the inputs and outputs
851 strcpy( gwxMacFileName
, file1
) ;
852 wxUnix2MacFilename( gwxMacFileName
) ;
853 strcpy( gwxMacFileName2
, file2
) ;
854 wxUnix2MacFilename( gwxMacFileName2
) ;
855 strcpy( gwxMacFileName3
, outfile
) ;
856 wxUnix2MacFilename( gwxMacFileName3
) ;
858 if ((fp1
= fopen (gwxMacFileName
, "rb")) == NULL
||
859 (fp2
= fopen (gwxMacFileName2
, "rb")) == NULL
||
860 (fp3
= fopen (gwxMacFileName3
, "wb")) == NULL
)
862 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
863 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
864 (fp3
= fopen (outfile
, "wb")) == NULL
)
877 while ((ch
= getc (fp1
)) != EOF
)
878 (void) putc (ch
, fp3
);
881 while ((ch
= getc (fp2
)) != EOF
)
882 (void) putc (ch
, fp3
);
886 bool result
= wxRenameFile(outfile
, file3
);
893 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
900 strcpy( gwxMacFileName
, file1
) ;
901 wxUnix2MacFilename( gwxMacFileName
) ;
902 strcpy( gwxMacFileName2
, file2
) ;
903 wxUnix2MacFilename( gwxMacFileName2
) ;
905 if ((fd1
= fopen (gwxMacFileName
, "rb")) == NULL
)
907 if ((fd2
= fopen (gwxMacFileName2
, "wb")) == NULL
)
909 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
911 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
918 while ((ch
= getc (fd1
)) != EOF
)
919 (void) putc (ch
, fd2
);
927 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
930 strcpy( gwxMacFileName
, file1
) ;
931 wxUnix2MacFilename( gwxMacFileName
) ;
932 strcpy( gwxMacFileName2
, file2
) ;
933 wxUnix2MacFilename( gwxMacFileName2
) ;
935 if (0 == rename (gwxMacFileName
, gwxMacFileName2
))
938 // Normal system call
939 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
943 if (wxCopyFile(file1
, file2
)) {
951 bool wxRemoveFile(const wxString
& file
)
953 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
954 int flag
= remove(WXSTRINGCAST file
);
955 #elif defined( __WXMAC__ )
956 strcpy( gwxMacFileName
, file
) ;
957 wxUnix2MacFilename( gwxMacFileName
) ;
958 int flag
= unlink(gwxMacFileName
);
960 int flag
= unlink(WXSTRINGCAST file
);
965 bool wxMkdir(const wxString
& dir
)
967 #if defined(__WXSTUBS__)
969 #elif defined(__VMS__)
971 #elif defined( __WXMAC__ )
972 strcpy( gwxMacFileName
, dir
) ;
973 wxUnix2MacFilename( gwxMacFileName
) ;
974 return (mkdir(gwxMacFileName
, 0 ) == 0);
975 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
976 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
978 return (mkdir(WXSTRINGCAST dir
) == 0);
982 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
986 #elif defined( __WXMAC__ )
987 strcpy( gwxMacFileName
, dir
) ;
988 wxUnix2MacFilename( gwxMacFileName
) ;
989 return (rmdir(WXSTRINGCAST gwxMacFileName
) == 0);
991 return (rmdir(WXSTRINGCAST dir
) == 0);
996 bool wxDirExists(const wxString
& dir
)
1000 #elif !defined(__WXMSW__)
1002 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
1005 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
1006 #if defined(__WIN32__)
1007 WIN32_FIND_DATA fileInfo
;
1010 struct ffblk fileInfo
;
1012 struct find_t fileInfo
;
1016 #if defined(__WIN32__)
1017 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
1019 if (h
==INVALID_HANDLE_VALUE
)
1023 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
1026 // In Borland findfirst has a different argument
1027 // ordering from _dos_findfirst. But _dos_findfirst
1028 // _should_ be ok in both MS and Borland... why not?
1030 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
1032 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
1041 // does the path exists? (may have or not '/' or '\\' at the end)
1042 bool wxPathExists(const char *pszPathName
)
1044 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
1045 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
1046 wxString
strPath(pszPathName
);
1047 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
1048 strPath
.Last() = '\0';
1051 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
1054 // Get a temporary filename, opening and closing the file.
1055 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
1061 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
1064 char tmpPath
[MAX_PATH
];
1065 ::GetTempPath(MAX_PATH
, tmpPath
);
1066 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
1068 if (buf
) strcpy(buf
, tmp
);
1069 else buf
= copystring(tmp
);
1073 static short last_temp
= 0; // cache last to speed things a bit
1074 // At most 1000 temp files to a process! We use a ring count.
1075 char tmp
[100]; // FIXME static buffer
1077 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
1079 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
1080 if (!wxFileExists( tmp
))
1082 // Touch the file to create it (reserve name)
1083 FILE *fd
= fopen (tmp
, "w");
1090 buf
= copystring( tmp
);
1094 cerr
<< _("wxWindows: error finding temporary file name.\n");
1095 if (buf
) buf
[0] = 0;
1096 return (char *) NULL
;
1100 // Get first file name matching given wild card.
1104 // Get first file name matching given wild card.
1105 // Flags are reserved for future use.
1108 static DIR *wxDirStream
= (DIR *) NULL
;
1109 static char *wxFileSpec
= (char *) NULL
;
1110 static int wxFindFileFlags
= 0;
1113 char *wxFindFirstFile(const char *spec
, int flags
)
1117 closedir(wxDirStream
); // edz 941103: better housekeping
1119 wxFindFileFlags
= flags
;
1122 delete[] wxFileSpec
;
1123 wxFileSpec
= copystring(spec
);
1125 // Find path only so we can concatenate
1126 // found file onto path
1127 char *p
= wxPathOnly(wxFileSpec
);
1129 /* MATTHEW: special case: path is really "/" */
1130 if (p
&& !*p
&& *wxFileSpec
== '/')
1132 /* MATTHEW: p is NULL => Local directory */
1136 if ((wxDirStream
=opendir(p
))==NULL
)
1137 return (char *) NULL
;
1139 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1140 return wxFindNextFile();
1143 return (char *) NULL
;
1146 char *wxFindNextFile(void)
1149 static char buf
[400]; // FIXME static buffer
1151 /* MATTHEW: [2] Don't crash if we read too many times */
1153 return (char *) NULL
;
1155 // Find path only so we can concatenate
1156 // found file onto path
1157 char *p
= wxPathOnly(wxFileSpec
);
1158 char *n
= wxFileNameFromPath(wxFileSpec
);
1160 /* MATTHEW: special case: path is really "/" */
1161 if (p
&& !*p
&& *wxFileSpec
== '/')
1165 struct dirent
*nextDir
;
1166 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1169 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1170 directories when flags & wxDIR */
1171 if (wxMatchWild(n
, nextDir
->d_name
)) {
1177 if (strcmp(p
, "/") != 0)
1180 strcat(buf
, nextDir
->d_name
);
1182 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1183 (strcmp(nextDir
->d_name
, "..") == 0)) {
1184 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1188 isdir
= wxDirExists(buf
);
1190 if (!wxFindFileFlags
1191 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1192 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1196 closedir(wxDirStream
);
1197 wxDirStream
= (DIR *) NULL
;
1201 return (char *) NULL
;
1204 #elif defined(__WXMSW__)
1207 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1208 WIN32_FIND_DATA wxFileStruc
;
1211 static struct ffblk wxFileStruc
;
1213 static struct _find_t wxFileStruc
;
1216 static wxString wxFileSpec
= "";
1217 static int wxFindFileFlags
;
1219 char *wxFindFirstFile(const char *spec
, int flags
)
1222 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1224 // Find path only so we can concatenate
1225 // found file onto path
1226 wxString
path1(wxFileSpec
);
1227 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1228 if (p
&& (strlen(p
) > 0))
1229 strcpy(wxBuffer
, p
);
1234 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1235 FindClose(wxFileStrucHandle
);
1237 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1239 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1242 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1244 if (isdir
&& !(flags
& wxDIR
))
1245 return wxFindNextFile();
1246 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1247 return wxFindNextFile();
1249 if (wxBuffer
[0] != 0)
1250 strcat(wxBuffer
, "\\");
1251 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1255 int flag
= _A_NORMAL
;
1256 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1260 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1262 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1265 /* MATTHEW: [5] Check directory flag */
1269 attrib
= wxFileStruc
.ff_attrib
;
1271 attrib
= wxFileStruc
.attrib
;
1274 if (attrib
& _A_SUBDIR
) {
1275 if (!(wxFindFileFlags
& wxDIR
))
1276 return wxFindNextFile();
1277 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1278 return wxFindNextFile();
1280 if (wxBuffer
[0] != 0)
1281 strcat(wxBuffer
, "\\");
1284 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1286 strcat(wxBuffer
, wxFileStruc
.name
);
1295 char *wxFindNextFile(void)
1297 // Find path only so we can concatenate
1298 // found file onto path
1299 wxString
p2(wxFileSpec
);
1300 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1301 if (p
&& (strlen(p
) > 0))
1302 strcpy(wxBuffer
, p
);
1309 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1312 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1314 FindClose(wxFileStrucHandle
);
1315 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1319 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1321 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1323 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1326 if (wxBuffer
[0] != 0)
1327 strcat(wxBuffer
, "\\");
1328 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1333 if (findnext(&wxFileStruc
) == 0)
1335 if (_dos_findnext(&wxFileStruc
) == 0)
1338 /* MATTHEW: [5] Check directory flag */
1342 attrib
= wxFileStruc
.ff_attrib
;
1344 attrib
= wxFileStruc
.attrib
;
1347 if (attrib
& _A_SUBDIR
) {
1348 if (!(wxFindFileFlags
& wxDIR
))
1350 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1354 if (wxBuffer
[0] != 0)
1355 strcat(wxBuffer
, "\\");
1357 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1359 strcat(wxBuffer
, wxFileStruc
.name
);
1371 // Get current working directory.
1372 // If buf is NULL, allocates space using new, else
1374 char *wxGetWorkingDirectory(char *buf
, int sz
)
1377 buf
= new char[sz
+1];
1379 if (_getcwd(buf
, sz
) == NULL
) {
1381 if (getcwd(buf
, sz
) == NULL
) {
1389 bool wxSetWorkingDirectory(const wxString
& d
)
1391 #if defined( __UNIX__ ) || defined( __WXMAC__ )
1392 return (chdir(d
) == 0);
1393 #elif defined(__WINDOWS__)
1396 return (bool)(SetCurrentDirectory(d
) != 0);
1398 // Must change drive, too.
1399 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1402 char firstChar
= d
[0];
1406 firstChar
= firstChar
- 32;
1408 // To a drive number
1409 unsigned int driveNo
= firstChar
- 64;
1412 unsigned int noDrives
;
1413 _dos_setdrive(driveNo
, &noDrives
);
1416 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1424 bool wxEndsWithPathSeparator(const char *pszFileName
)
1426 size_t len
= Strlen(pszFileName
);
1430 return wxIsPathSeparator(pszFileName
[len
- 1]);
1433 // find a file in a list of directories, returns false if not found
1434 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1436 // we assume that it's not empty
1437 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1438 _("empty file name in wxFindFileInPath"));
1440 // skip path separator in the beginning of the file name if present
1441 if ( wxIsPathSeparator(*pszFile
) )
1444 // copy the path (strtok will modify it)
1445 char *szPath
= new char[strlen(pszPath
) + 1];
1446 strcpy(szPath
, pszPath
);
1450 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok((char *) NULL
, PATH_SEP
) ) {
1451 // search for the file in this directory
1453 if ( !wxEndsWithPathSeparator(pc
) )
1454 strFile
+= FILE_SEP_PATH
;
1457 if ( FileExists(strFile
) ) {
1465 return pc
!= NULL
; // if true => we breaked from the loop
1468 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1473 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1475 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1476 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1477 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1479 // take the last of the two
1480 size_t nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1481 size_t nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1482 if ( nPosDos
> nPosUnix
)
1484 // size_t nLen = Strlen(pszFileName);
1487 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1489 size_t nPosDot
= pDot
- pszFileName
;
1491 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1493 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1497 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);
1503 //------------------------------------------------------------------------
1504 // wild character routines
1505 //------------------------------------------------------------------------
1507 bool wxIsWild( const wxString
& pattern
)
1509 wxString tmp
= pattern
;
1510 char *pat
= WXSTRINGCAST(tmp
);
1513 case '?': case '*': case '[': case '{':
1523 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1525 #if defined(HAVE_FNMATCH_H)
1528 return fnmatch(pat
.c_str(), text
.c_str(), FNM_PERIOD
) == 0;
1530 return fnmatch(pat
.c_str(), text
.c_str(), 0) == 0;
1534 // #pragma error Broken implementation of wxMatchWild() -- needs fixing!
1537 * WARNING: this code is broken!
1540 wxString tmp1
= pat
;
1541 char *pattern
= WXSTRINGCAST(tmp1
);
1542 wxString tmp2
= text
;
1543 char *str
= WXSTRINGCAST(tmp2
);
1546 bool done
= FALSE
, ret_code
, ok
;
1547 // Below is for vi fans
1548 const char OB
= '{', CB
= '}';
1550 // dot_special means '.' only matches '.'
1551 if (dot_special
&& *str
== '.' && *pattern
!= *str
)
1554 while ((*pattern
!= '\0') && (!done
)
1555 && (((*str
=='\0')&&((*pattern
==OB
)||(*pattern
=='*')))||(*str
!='\0'))) {
1559 if (*pattern
!= '\0')
1566 && (!(ret_code
=wxMatchWild(pattern
, str
++, FALSE
))))
1569 while (*str
!= '\0')
1571 while (*pattern
!= '\0')
1578 if ((*pattern
== '\0') || (*pattern
== ']')) {
1582 if (*pattern
== '\\') {
1584 if (*pattern
== '\0') {
1589 if (*(pattern
+ 1) == '-') {
1592 if (*pattern
== ']') {
1596 if (*pattern
== '\\') {
1598 if (*pattern
== '\0') {
1603 if ((*str
< c
) || (*str
> *pattern
)) {
1607 } else if (*pattern
!= *str
) {
1612 while ((*pattern
!= ']') && (*pattern
!= '\0')) {
1613 if ((*pattern
== '\\') && (*(pattern
+ 1) != '\0'))
1617 if (*pattern
!= '\0') {
1627 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1630 while (ok
&& (*cp
!= '\0') && (*pattern
!= '\0')
1631 && (*pattern
!= ',') && (*pattern
!= CB
)) {
1632 if (*pattern
== '\\')
1634 ok
= (*pattern
++ == *cp
++);
1636 if (*pattern
== '\0') {
1642 while ((*pattern
!= CB
) && (*pattern
!= '\0')) {
1643 if (*++pattern
== '\\') {
1644 if (*++pattern
== CB
)
1649 while (*pattern
!=CB
&& *pattern
!=',' && *pattern
!='\0') {
1650 if (*++pattern
== '\\') {
1651 if (*++pattern
== CB
|| *pattern
== ',')
1656 if (*pattern
!= '\0')
1661 if (*str
== *pattern
) {
1668 while (*pattern
== '*')
1670 return ((*str
== '\0') && (*pattern
== '\0'));
1676 #pragma warning(default:4706) // assignment within conditional expression