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"
34 #if !defined(__WATCOMC__)
35 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
40 #include <sys/types.h>
56 #include <sys/unistd.h>
57 // #include <sys/stat.h>
63 #define stricmp strcasecmp
66 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
67 // this (3.1 I believe) and how to test for it.
68 // If this works for Borland 4.0 as well, then no worries.
76 #define _MAXPATHLEN 500
78 #if !USE_SHARED_LIBRARY
79 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
82 extern char *wxBuffer
;
84 void wxPathList::Add (const wxString
& path
)
86 wxStringList::Add ((char *)(const char *)path
);
89 // Add paths e.g. from the PATH environment variable
90 void wxPathList::AddEnvList (const wxString
& envVariable
)
92 static const char PATH_TOKS
[] =
94 " ;"; // Don't seperate with colon in DOS (used for drive)
99 char *val
= getenv (WXSTRINGCAST envVariable
);
102 char *s
= copystring (val
);
103 char *token
= strtok (s
, PATH_TOKS
);
107 Add (copystring (token
));
110 if ((token
= strtok (NULL
, PATH_TOKS
)) != NULL
)
111 Add (wxString(token
));
118 // Given a full filename (with path), ensure that that file can
119 // be accessed again USING FILENAME ONLY by adding the path
120 // to the list if not already there.
121 void wxPathList::EnsureFileAccessible (const wxString
& path
)
123 wxString
path1(path
);
124 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
127 if (!Member (wxString(path_only
)))
128 Add (wxString(path_only
));
132 bool wxPathList::Member (const wxString
& path
)
134 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
136 wxString
path2((char *) node
->Data ());
138 #if defined(__WXMSW__) || defined(__VMS__)
140 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
142 // Case sensitive File System
143 path
.CompareTo (path2
) == 0
151 wxString
wxPathList::FindValidPath (const wxString
& file
)
153 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
154 return wxString(wxBuffer
);
156 char buf
[_MAXPATHLEN
];
157 strcpy(buf
, wxBuffer
);
159 char *filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
161 for (wxNode
* node
= First (); node
; node
= node
->Next ())
163 char *path
= (char *) node
->Data ();
164 strcpy (wxBuffer
, path
);
165 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
166 if (ch
!= '\\' && ch
!= '/')
167 strcat (wxBuffer
, "/");
168 strcat (wxBuffer
, filename
);
170 Unix2DosFilename (wxBuffer
);
172 if (wxFileExists (wxBuffer
))
174 return wxString(wxBuffer
); // Found!
178 return wxString(""); // Not found
181 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
183 wxString f
= FindValidPath(file
);
184 if (wxIsAbsolutePath(f
))
189 wxGetWorkingDirectory(buf
, 499);
190 int len
= (int)strlen(buf
);
194 if (lastCh
!= '/' && lastCh
!= '\\')
202 strcat(buf
, (const char *)f
);
203 strcpy(wxBuffer
, buf
);
204 return wxString(wxBuffer
);
209 wxFileExists (const wxString
& filename
)
213 if (filename
&& stat ((char *)(const char *)filename
, &stbuf
) == 0)
218 /* Vadim's alternative implementation
220 // does the file exist?
221 bool wxFileExists(const char *pszFileName)
224 return !access(pszFileName, 0) &&
225 !stat(pszFileName, &st) &&
226 (st.st_mode & S_IFREG);
231 wxIsAbsolutePath (const wxString
& filename
)
235 if (filename
[0] == '/'
237 || (filename
[0] == '[' && filename
[1] != '.')
241 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
250 * Strip off any extension (dot something) from end of file,
251 * IF one exists. Inserts zero into buffer.
255 void wxStripExtension(char *buffer
)
257 int len
= strlen(buffer
);
261 if (buffer
[i
] == '.')
270 void wxStripExtension(wxString
& buffer
)
272 size_t len
= buffer
.Length();
276 if (buffer
.GetChar(i
) == '.')
278 buffer
= buffer
.Left(i
);
285 // Destructive removal of /./ and /../ stuff
286 char *wxRealPath (char *path
)
289 static const char SEP
= '\\';
290 Unix2DosFilename(path
);
292 static const char SEP
= '/';
294 if (path
[0] && path
[1]) {
295 /* MATTHEW: special case "/./x" */
297 if (path
[2] == SEP
&& path
[1] == '.')
305 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
308 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
309 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
310 && (q
- 1 <= path
|| q
[-1] != SEP
))
319 /* Check that path[2] is NULL! */
320 else if (path
[1] == ':' && !path
[2])
329 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
338 char *wxCopyAbsolutePath(const wxString
& filename
)
343 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
344 char buf
[_MAXPATHLEN
];
346 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
347 char ch
= buf
[strlen(buf
) - 1];
349 if (ch
!= '\\' && ch
!= '/')
355 strcat(buf
, wxBuffer
);
356 return copystring( wxRealPath(buf
) );
358 return copystring( wxBuffer
);
364 ~user/ => user's home dir
365 If the environment variable a = "foo" and b = "bar" then:
382 /* input name in name, pathname output to buf. */
384 char *wxExpandPath(char *buf
, const char *name
)
386 register char *d
, *s
, *nm
;
387 char lnm
[_MAXPATHLEN
];
390 // Some compilers don't like this line.
391 // const char trimchars[] = "\n \t";
400 const char SEP
= '\\';
402 const char SEP
= '/';
405 if (name
== NULL
|| *name
== '\0')
407 nm
= copystring(name
); // Make a scratch copy
410 /* Skip leading whitespace and cr */
411 while (strchr((char *)trimchars
, *nm
) != NULL
)
413 /* And strip off trailing whitespace and cr */
414 s
= nm
+ (q
= strlen(nm
)) - 1;
415 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
423 q
= nm
[0] == '\\' && nm
[1] == '~';
426 /* Expand inline environment variables */
427 while ((*d
++ = *s
)) {
430 if ((*(d
- 1) = *++s
)) {
438 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
443 register char *start
= d
;
444 register braces
= (*s
== '{' || *s
== '(');
445 register char *value
;
447 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
452 value
= getenv(braces
? start
+ 1 : start
);
454 for ((d
= start
- 1); (*d
++ = *value
++););
462 /* Expand ~ and ~user */
465 if (nm
[0] == '~' && !q
)
468 if (nm
[1] == SEP
|| nm
[1] == 0)
470 if ((s
= wxGetUserHome("")) != NULL
) {
475 { /* ~user/filename */
478 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
479 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
480 was_sep
= (*s
== SEP
);
481 nnm
= *s
? s
+ 1 : s
;
483 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
484 if (was_sep
) /* replace only if it was there: */
495 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
497 while ('\0' != (*d
++ = *s
++))
500 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
504 while ((*d
++ = *s
++));
506 delete[] nm_tmp
; // clean up alloc
507 /* Now clean up the buffer */
508 return wxRealPath(buf
);
512 /* Contract Paths to be build upon an environment variable
515 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
517 The call wxExpandPath can convert these back!
520 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
522 static char dest
[_MAXPATHLEN
];
527 strcpy (dest
, WXSTRINGCAST filename
);
529 Unix2DosFilename(dest
);
532 // Handle environment
535 if (envname
!= NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
536 (tcp
= strstr (dest
, val
)) != NULL
)
538 strcpy (wxBuffer
, tcp
+ strlen (val
));
541 strcpy (tcp
, WXSTRINGCAST envname
);
543 strcat (tcp
, wxBuffer
);
546 // Handle User's home (ignore root homes!)
548 if ((val
= wxGetUserHome (user
)) != NULL
&&
549 (len
= strlen(val
)) > 2 &&
550 strncmp(dest
, val
, len
) == 0)
552 strcpy(wxBuffer
, "~");
554 strcat(wxBuffer
, user
);
556 // strcat(wxBuffer, "\\");
558 // strcat(wxBuffer, "/");
560 strcat(wxBuffer
, dest
+ len
);
561 strcpy (dest
, wxBuffer
);
567 // Return just the filename, not the path
569 char *wxFileNameFromPath (char *path
)
575 tcp
= path
+ strlen (path
);
576 while (--tcp
>= path
)
578 if (*tcp
== '/' || *tcp
== '\\'
580 || *tcp
== ':' || *tcp
== ']')
587 if (isalpha (*path
) && *(path
+ 1) == ':')
594 wxString
wxFileNameFromPath (const wxString
& path1
)
599 char *path
= WXSTRINGCAST path1
;
602 tcp
= path
+ strlen (path
);
603 while (--tcp
>= path
)
605 if (*tcp
== '/' || *tcp
== '\\'
607 || *tcp
== ':' || *tcp
== ']')
611 return wxString(tcp
+ 1);
614 if (isalpha (*path
) && *(path
+ 1) == ':')
615 return wxString(path
+ 2);
621 // Return just the directory, or NULL if no directory
623 wxPathOnly (char *path
)
627 static char buf
[_MAXPATHLEN
];
632 int l
= strlen(path
);
637 // Search backward for a backward or forward slash
638 while (!done
&& i
> -1)
641 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
656 // Try Drive specifier
657 if (isalpha (buf
[0]) && buf
[1] == ':')
659 // A:junk --> A:. (since A:.\junk Not A:\junk)
670 // Return just the directory, or NULL if no directory
671 wxString
wxPathOnly (const wxString
& path
)
675 char buf
[_MAXPATHLEN
];
678 strcpy (buf
, WXSTRINGCAST path
);
680 int l
= path
.Length();
685 // Search backward for a backward or forward slash
686 while (!done
&& i
> -1)
689 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
698 return wxString(buf
);
704 // Try Drive specifier
705 if (isalpha (buf
[0]) && buf
[1] == ':')
707 // A:junk --> A:. (since A:.\junk Not A:\junk)
710 return wxString(buf
);
718 // Utility for converting delimiters in DOS filenames to UNIX style
719 // and back again - or we get nasty problems with delimiters.
720 // Also, convert to lower case, since case is significant in UNIX.
723 wxDos2UnixFilename (char *s
)
732 *s
= wxToLower (*s
); // Case INDEPENDENT
740 wxUnix2DosFilename (char *s
)
742 wxUnix2DosFilename (char *WXUNUSED(s
))
745 // Yes, I really mean this to happen under DOS only! JACS
757 // Concatenate two files to form third
759 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
761 char *outfile
= wxGetTempFileName("cat");
766 // Open the inputs and outputs
767 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
768 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
769 (fp3
= fopen (outfile
, "wb")) == NULL
)
781 while ((ch
= getc (fp1
)) != EOF
)
782 (void) putc (ch
, fp3
);
785 while ((ch
= getc (fp2
)) != EOF
)
786 (void) putc (ch
, fp3
);
790 bool result
= wxRenameFile(outfile
, file3
);
797 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
803 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
805 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
811 while ((ch
= getc (fd1
)) != EOF
)
812 (void) putc (ch
, fd2
);
820 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
822 // Normal system call
823 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
826 if (wxCopyFile(file1
, file2
)) {
834 bool wxRemoveFile(const wxString
& file
)
836 #if defined(_MSC_VER) || defined(__BORLANDC__)
837 int flag
= remove(WXSTRINGCAST file
);
839 int flag
= unlink(WXSTRINGCAST file
);
844 bool wxMkdir(const wxString
& dir
)
848 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
849 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
851 return (mkdir(WXSTRINGCAST dir
) == 0);
855 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
860 return (rmdir(WXSTRINGCAST dir
) == 0);
865 bool wxDirExists(const wxString
& dir
)
869 #elif !defined(__WXMSW__)
871 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
874 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
875 #if defined(__WIN32__)
876 WIN32_FIND_DATA fileInfo
;
879 struct ffblk fileInfo
;
881 struct find_t fileInfo
;
885 #if defined(__WIN32__)
886 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
888 if (h
==INVALID_HANDLE_VALUE
)
892 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
895 // In Borland findfirst has a different argument
896 // ordering from _dos_findfirst. But _dos_findfirst
897 // _should_ be ok in both MS and Borland... why not?
899 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
901 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
910 // does the path exists? (may have or not '/' or '\\' at the end)
911 bool wxPathExists(const char *pszPathName
)
913 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
914 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
915 wxString
strPath(pszPathName
);
916 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
917 strPath
.Last() = '\0';
920 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
923 // Get a temporary filename, opening and closing the file.
924 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
930 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
933 char tmpPath
[MAX_PATH
];
934 ::GetTempPath(MAX_PATH
, tmpPath
);
935 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
937 if (buf
) strcpy(buf
, tmp
);
938 else buf
= copystring(tmp
);
942 static short last_temp
= 0; // cache last to speed things a bit
943 // At most 1000 temp files to a process! We use a ring count.
946 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
948 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
949 if (!wxFileExists( tmp
))
951 // Touch the file to create it (reserve name)
952 FILE *fd
= fopen (tmp
, "w");
959 buf
= copystring( tmp
);
963 cerr
<< "wxWindows: error finding temporary file name.\n";
969 // Get first file name matching given wild card.
973 // Get first file name matching given wild card.
974 // Flags are reserved for future use.
977 static DIR *wxDirStream
= NULL
;
978 static char *wxFileSpec
= NULL
;
979 static int wxFindFileFlags
= 0;
982 char *wxFindFirstFile(const char *spec
, int flags
)
986 closedir(wxDirStream
); // edz 941103: better housekeping
988 wxFindFileFlags
= flags
;
992 wxFileSpec
= copystring(spec
);
994 // Find path only so we can concatenate
995 // found file onto path
996 char *p
= wxPathOnly(wxFileSpec
);
998 /* MATTHEW: special case: path is really "/" */
999 if (p
&& !*p
&& *wxFileSpec
== '/')
1001 /* MATTHEW: p is NULL => Local directory */
1005 if ((wxDirStream
=opendir(p
))==NULL
)
1008 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1009 return wxFindNextFile();
1015 char *wxFindNextFile(void)
1018 static char buf
[400];
1020 /* MATTHEW: [2] Don't crash if we read too many times */
1024 // Find path only so we can concatenate
1025 // found file onto path
1026 char *p
= wxPathOnly(wxFileSpec
);
1027 char *n
= wxFileNameFromPath(wxFileSpec
);
1029 /* MATTHEW: special case: path is really "/" */
1030 if (p
&& !*p
&& *wxFileSpec
== '/')
1034 struct dirent
*nextDir
;
1035 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1038 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1039 directories when flags & wxDIR */
1040 if (wxMatchWild(n
, nextDir
->d_name
)) {
1046 if (strcmp(p
, "/") != 0)
1049 strcat(buf
, nextDir
->d_name
);
1051 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1052 (strcmp(nextDir
->d_name
, "..") == 0)) {
1053 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1057 isdir
= wxDirExists(buf
);
1059 if (!wxFindFileFlags
1060 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1061 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1065 closedir(wxDirStream
);
1073 #elif defined(__WXMSW__)
1076 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1077 WIN32_FIND_DATA wxFileStruc
;
1080 static struct ffblk wxFileStruc
;
1082 static struct _find_t wxFileStruc
;
1085 static wxString wxFileSpec
= "";
1086 static int wxFindFileFlags
;
1088 char *wxFindFirstFile(const char *spec
, int flags
)
1091 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1093 // Find path only so we can concatenate
1094 // found file onto path
1095 wxString
path1(wxFileSpec
);
1096 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1097 if (p
&& (strlen(p
) > 0))
1098 strcpy(wxBuffer
, p
);
1103 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1104 FindClose(wxFileStrucHandle
);
1106 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1108 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1111 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1113 if (isdir
&& !(flags
& wxDIR
))
1114 return wxFindNextFile();
1115 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1116 return wxFindNextFile();
1118 if (wxBuffer
[0] != 0)
1119 strcat(wxBuffer
, "\\");
1120 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1124 int flag
= _A_NORMAL
;
1125 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1129 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1131 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1134 /* MATTHEW: [5] Check directory flag */
1138 attrib
= wxFileStruc
.ff_attrib
;
1140 attrib
= wxFileStruc
.attrib
;
1143 if (attrib
& _A_SUBDIR
) {
1144 if (!(wxFindFileFlags
& wxDIR
))
1145 return wxFindNextFile();
1146 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1147 return wxFindNextFile();
1149 if (wxBuffer
[0] != 0)
1150 strcat(wxBuffer
, "\\");
1153 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1155 strcat(wxBuffer
, wxFileStruc
.name
);
1164 char *wxFindNextFile(void)
1166 // Find path only so we can concatenate
1167 // found file onto path
1168 wxString
p2(wxFileSpec
);
1169 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1170 if (p
&& (strlen(p
) > 0))
1171 strcpy(wxBuffer
, p
);
1178 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1181 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1183 FindClose(wxFileStrucHandle
);
1184 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1188 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1190 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1192 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1195 if (wxBuffer
[0] != 0)
1196 strcat(wxBuffer
, "\\");
1197 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1202 if (findnext(&wxFileStruc
) == 0)
1204 if (_dos_findnext(&wxFileStruc
) == 0)
1207 /* MATTHEW: [5] Check directory flag */
1211 attrib
= wxFileStruc
.ff_attrib
;
1213 attrib
= wxFileStruc
.attrib
;
1216 if (attrib
& _A_SUBDIR
) {
1217 if (!(wxFindFileFlags
& wxDIR
))
1219 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1223 if (wxBuffer
[0] != 0)
1224 strcat(wxBuffer
, "\\");
1226 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1228 strcat(wxBuffer
, wxFileStruc
.name
);
1240 // Get current working directory.
1241 // If buf is NULL, allocates space using new, else
1243 char *wxGetWorkingDirectory(char *buf
, int sz
)
1246 buf
= new char[sz
+1];
1248 if (_getcwd(buf
, sz
) == NULL
) {
1250 if (getcwd(buf
, sz
) == NULL
) {
1258 bool wxSetWorkingDirectory(const wxString
& d
)
1261 return (chdir(d
) == 0);
1262 #elif defined(__WXMSW__)
1265 return (bool)(SetCurrentDirectory(d
) != 0);
1267 // Must change drive, too.
1268 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1271 char firstChar
= d
[0];
1275 firstChar
= firstChar
- 32;
1277 // To a drive number
1278 unsigned int driveNo
= firstChar
- 64;
1281 unsigned int noDrives
;
1282 _dos_setdrive(driveNo
, &noDrives
);
1285 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1293 bool wxEndsWithPathSeparator(const char *pszFileName
)
1295 size_t len
= Strlen(pszFileName
);
1299 return wxIsPathSeparator(pszFileName
[len
- 1]);
1302 // find a file in a list of directories, returns false if not found
1303 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1305 // we assume that it's not empty
1306 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1307 "empty file name in wxFindFileInPath");
1309 // skip path separator in the beginning of the file name if present
1310 if ( wxIsPathSeparator(*pszFile
) )
1313 // copy the path (strtok will modify it)
1314 char *szPath
= new char[strlen(pszPath
) + 1];
1315 strcpy(szPath
, pszPath
);
1319 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok(NULL
, PATH_SEP
) ) {
1320 // search for the file in this directory
1322 if ( !wxEndsWithPathSeparator(pc
) )
1323 strFile
+= FILE_SEP_PATH
;
1326 if ( FileExists(strFile
) ) {
1334 return pc
!= NULL
; // if true => we breaked from the loop
1337 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1342 wxCHECK_RET( pszFileName
, "NULL file name in wxSplitPath" );
1344 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1345 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1346 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1348 // take the last of the two
1349 uint nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1350 uint nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1351 if ( nPosDos
> nPosUnix
)
1353 // uint nLen = Strlen(pszFileName);
1356 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1358 uint nPosDot
= pDot
- pszFileName
;
1360 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1362 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1366 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);