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
739 wxUnix2DosFilename (char *s
)
741 // Yes, I really mean this to happen under DOS only! JACS
753 // Concatenate two files to form third
755 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
757 char *outfile
= wxGetTempFileName("cat");
762 // Open the inputs and outputs
763 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
764 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
765 (fp3
= fopen (outfile
, "wb")) == NULL
)
777 while ((ch
= getc (fp1
)) != EOF
)
778 (void) putc (ch
, fp3
);
781 while ((ch
= getc (fp2
)) != EOF
)
782 (void) putc (ch
, fp3
);
786 bool result
= wxRenameFile(outfile
, file3
);
793 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
799 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
801 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
807 while ((ch
= getc (fd1
)) != EOF
)
808 (void) putc (ch
, fd2
);
816 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
818 // Normal system call
819 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
822 if (wxCopyFile(file1
, file2
)) {
830 bool wxRemoveFile(const wxString
& file
)
832 #if defined(_MSC_VER) || defined(__BORLANDC__)
833 int flag
= remove(WXSTRINGCAST file
);
835 int flag
= unlink(WXSTRINGCAST file
);
840 bool wxMkdir(const wxString
& dir
)
844 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
845 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
847 return (mkdir(WXSTRINGCAST dir
) == 0);
851 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
856 return (rmdir(WXSTRINGCAST dir
) == 0);
861 bool wxDirExists(const wxString
& dir
)
865 #elif !defined(__WXMSW__)
867 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
870 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
871 #if defined(__WIN32__)
872 WIN32_FIND_DATA fileInfo
;
875 struct ffblk fileInfo
;
877 struct find_t fileInfo
;
881 #if defined(__WIN32__)
882 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
884 if (h
==INVALID_HANDLE_VALUE
)
888 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
891 // In Borland findfirst has a different argument
892 // ordering from _dos_findfirst. But _dos_findfirst
893 // _should_ be ok in both MS and Borland... why not?
895 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
897 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
906 // does the path exists? (may have or not '/' or '\\' at the end)
907 bool wxPathExists(const char *pszPathName
)
909 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
910 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
911 wxString
strPath(pszPathName
);
912 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
913 strPath
.Last() = '\0';
916 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
919 // Get a temporary filename, opening and closing the file.
920 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
926 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
929 char tmpPath
[MAX_PATH
];
930 ::GetTempPath(MAX_PATH
, tmpPath
);
931 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
933 if (buf
) strcpy(buf
, tmp
);
934 else buf
= copystring(tmp
);
938 static short last_temp
= 0; // cache last to speed things a bit
939 // At most 1000 temp files to a process! We use a ring count.
942 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
944 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
945 if (!wxFileExists( tmp
))
947 // Touch the file to create it (reserve name)
948 FILE *fd
= fopen (tmp
, "w");
955 buf
= copystring( tmp
);
959 cerr
<< "wxWindows: error finding temporary file name.\n";
965 // Get first file name matching given wild card.
969 // Get first file name matching given wild card.
970 // Flags are reserved for future use.
973 static DIR *wxDirStream
= NULL
;
974 static char *wxFileSpec
= NULL
;
975 static int wxFindFileFlags
= 0;
978 char *wxFindFirstFile(const char *spec
, int flags
)
982 closedir(wxDirStream
); // edz 941103: better housekeping
984 wxFindFileFlags
= flags
;
988 wxFileSpec
= copystring(spec
);
990 // Find path only so we can concatenate
991 // found file onto path
992 char *p
= wxPathOnly(wxFileSpec
);
994 /* MATTHEW: special case: path is really "/" */
995 if (p
&& !*p
&& *wxFileSpec
== '/')
997 /* MATTHEW: p is NULL => Local directory */
1001 if ((wxDirStream
=opendir(p
))==NULL
)
1004 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1005 return wxFindNextFile();
1011 char *wxFindNextFile(void)
1014 static char buf
[400];
1016 /* MATTHEW: [2] Don't crash if we read too many times */
1020 // Find path only so we can concatenate
1021 // found file onto path
1022 char *p
= wxPathOnly(wxFileSpec
);
1023 char *n
= wxFileNameFromPath(wxFileSpec
);
1025 /* MATTHEW: special case: path is really "/" */
1026 if (p
&& !*p
&& *wxFileSpec
== '/')
1030 struct dirent
*nextDir
;
1031 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1034 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1035 directories when flags & wxDIR */
1036 if (wxMatchWild(n
, nextDir
->d_name
)) {
1042 if (strcmp(p
, "/") != 0)
1045 strcat(buf
, nextDir
->d_name
);
1047 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1048 (strcmp(nextDir
->d_name
, "..") == 0)) {
1049 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1053 isdir
= wxDirExists(buf
);
1055 if (!wxFindFileFlags
1056 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1057 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1061 closedir(wxDirStream
);
1069 #elif defined(__WXMSW__)
1072 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1073 WIN32_FIND_DATA wxFileStruc
;
1076 static struct ffblk wxFileStruc
;
1078 static struct _find_t wxFileStruc
;
1081 static wxString wxFileSpec
= "";
1082 static int wxFindFileFlags
;
1084 char *wxFindFirstFile(const char *spec
, int flags
)
1087 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1089 // Find path only so we can concatenate
1090 // found file onto path
1091 wxString
path1(wxFileSpec
);
1092 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1093 if (p
&& (strlen(p
) > 0))
1094 strcpy(wxBuffer
, p
);
1099 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1100 FindClose(wxFileStrucHandle
);
1102 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1104 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1107 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1109 if (isdir
&& !(flags
& wxDIR
))
1110 return wxFindNextFile();
1111 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1112 return wxFindNextFile();
1114 if (wxBuffer
[0] != 0)
1115 strcat(wxBuffer
, "\\");
1116 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1120 int flag
= _A_NORMAL
;
1121 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1125 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1127 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1130 /* MATTHEW: [5] Check directory flag */
1134 attrib
= wxFileStruc
.ff_attrib
;
1136 attrib
= wxFileStruc
.attrib
;
1139 if (attrib
& _A_SUBDIR
) {
1140 if (!(wxFindFileFlags
& wxDIR
))
1141 return wxFindNextFile();
1142 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1143 return wxFindNextFile();
1145 if (wxBuffer
[0] != 0)
1146 strcat(wxBuffer
, "\\");
1149 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1151 strcat(wxBuffer
, wxFileStruc
.name
);
1160 char *wxFindNextFile(void)
1162 // Find path only so we can concatenate
1163 // found file onto path
1164 wxString
p2(wxFileSpec
);
1165 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1166 if (p
&& (strlen(p
) > 0))
1167 strcpy(wxBuffer
, p
);
1174 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1177 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1179 FindClose(wxFileStrucHandle
);
1180 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1184 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1186 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1188 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1191 if (wxBuffer
[0] != 0)
1192 strcat(wxBuffer
, "\\");
1193 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1198 if (findnext(&wxFileStruc
) == 0)
1200 if (_dos_findnext(&wxFileStruc
) == 0)
1203 /* MATTHEW: [5] Check directory flag */
1207 attrib
= wxFileStruc
.ff_attrib
;
1209 attrib
= wxFileStruc
.attrib
;
1212 if (attrib
& _A_SUBDIR
) {
1213 if (!(wxFindFileFlags
& wxDIR
))
1215 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1219 if (wxBuffer
[0] != 0)
1220 strcat(wxBuffer
, "\\");
1222 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1224 strcat(wxBuffer
, wxFileStruc
.name
);
1236 // Get current working directory.
1237 // If buf is NULL, allocates space using new, else
1239 char *wxGetWorkingDirectory(char *buf
, int sz
)
1242 buf
= new char[sz
+1];
1244 if (_getcwd(buf
, sz
) == NULL
) {
1246 if (getcwd(buf
, sz
) == NULL
) {
1254 bool wxSetWorkingDirectory(const wxString
& d
)
1257 return (chdir(d
) == 0);
1258 #elif defined(__WXMSW__)
1261 return (bool)(SetCurrentDirectory(d
) != 0);
1263 // Must change drive, too.
1264 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1267 char firstChar
= d
[0];
1271 firstChar
= firstChar
- 32;
1273 // To a drive number
1274 unsigned int driveNo
= firstChar
- 64;
1277 unsigned int noDrives
;
1278 _dos_setdrive(driveNo
, &noDrives
);
1281 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1289 bool wxEndsWithPathSeparator(const char *pszFileName
)
1291 size_t len
= Strlen(pszFileName
);
1295 return wxIsPathSeparator(pszFileName
[len
- 1]);
1298 // find a file in a list of directories, returns false if not found
1299 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1301 // we assume that it's not empty
1302 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1303 "empty file name in wxFindFileInPath");
1305 // skip path separator in the beginning of the file name if present
1306 if ( wxIsPathSeparator(*pszFile
) )
1309 // copy the path (strtok will modify it)
1310 char *szPath
= new char[strlen(pszPath
) + 1];
1311 strcpy(szPath
, pszPath
);
1315 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok(NULL
, PATH_SEP
) ) {
1316 // search for the file in this directory
1318 if ( !wxEndsWithPathSeparator(pc
) )
1319 strFile
+= FILE_SEP_PATH
;
1322 if ( FileExists(strFile
) ) {
1330 return pc
!= NULL
; // if true => we breaked from the loop
1333 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1338 wxCHECK_RET( pszFileName
, "NULL file name in wxSplitPath" );
1340 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1341 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1342 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1344 // take the last of the two
1345 uint nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1346 uint nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1347 if ( nPosDos
> nPosUnix
)
1349 uint nLen
= Strlen(pszFileName
);
1352 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1354 uint nPosDot
= pDot
- pszFileName
;
1356 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1358 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1362 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);