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>
54 #include <sys/unistd.h>
55 // #include <sys/stat.h>
61 #define stricmp strcasecmp
64 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
65 // this (3.1 I believe) and how to test for it.
66 // If this works for Borland 4.0 as well, then no worries.
74 #define _MAXPATHLEN 500
76 #if !USE_SHARED_LIBRARY
77 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
80 extern char *wxBuffer
;
82 void wxPathList::Add (const wxString
& path
)
84 wxStringList::Add ((char *)(const char *)path
);
87 // Add paths e.g. from the PATH environment variable
88 void wxPathList::AddEnvList (const wxString
& envVariable
)
90 static const char PATH_TOKS
[] =
92 " ;"; // Don't seperate with colon in DOS (used for drive)
97 char *val
= getenv (WXSTRINGCAST envVariable
);
100 char *s
= copystring (val
);
101 char *token
= strtok (s
, PATH_TOKS
);
105 Add (copystring (token
));
108 if ((token
= strtok (NULL
, PATH_TOKS
)) != NULL
)
109 Add (wxString(token
));
116 // Given a full filename (with path), ensure that that file can
117 // be accessed again USING FILENAME ONLY by adding the path
118 // to the list if not already there.
119 void wxPathList::EnsureFileAccessible (const wxString
& path
)
121 wxString
path1(path
);
122 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
125 if (!Member (wxString(path_only
)))
126 Add (wxString(path_only
));
130 bool wxPathList::Member (const wxString
& path
)
132 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
134 wxString
path2((char *) node
->Data ());
136 #if defined(__WINDOWS__) || defined(__VMS__)
138 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
140 // Case sensitive File System
141 path
.CompareTo (path2
) == 0
149 wxString
wxPathList::FindValidPath (const wxString
& file
)
151 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
152 return wxString(wxBuffer
);
154 char buf
[_MAXPATHLEN
];
155 strcpy(buf
, wxBuffer
);
157 char *filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
159 for (wxNode
* node
= First (); node
; node
= node
->Next ())
161 char *path
= (char *) node
->Data ();
162 strcpy (wxBuffer
, path
);
163 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
164 if (ch
!= '\\' && ch
!= '/')
165 strcat (wxBuffer
, "/");
166 strcat (wxBuffer
, filename
);
168 Unix2DosFilename (wxBuffer
);
170 if (wxFileExists (wxBuffer
))
172 return wxString(wxBuffer
); // Found!
176 return wxString(""); // Not found
179 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
181 wxString f
= FindValidPath(file
);
182 if (wxIsAbsolutePath(f
))
187 wxGetWorkingDirectory(buf
, 499);
188 int len
= (int)strlen(buf
);
192 if (lastCh
!= '/' && lastCh
!= '\\')
200 strcat(buf
, (const char *)f
);
201 strcpy(wxBuffer
, buf
);
202 return wxString(wxBuffer
);
207 wxFileExists (const wxString
& filename
)
211 if (filename
&& stat ((char *)(const char *)filename
, &stbuf
) == 0)
216 /* Vadim's alternative implementation
218 // does the file exist?
219 bool wxFileExists(const char *pszFileName)
222 return !access(pszFileName, 0) &&
223 !stat(pszFileName, &st) &&
224 (st.st_mode & S_IFREG);
229 wxIsAbsolutePath (const wxString
& filename
)
233 if (filename
[0] == '/'
235 || (filename
[0] == '[' && filename
[1] != '.')
239 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
248 * Strip off any extension (dot something) from end of file,
249 * IF one exists. Inserts zero into buffer.
253 void wxStripExtension(char *buffer
)
255 int len
= strlen(buffer
);
259 if (buffer
[i
] == '.')
268 // Destructive removal of /./ and /../ stuff
269 char *wxRealPath (char *path
)
272 static const char SEP
= '\\';
273 Unix2DosFilename(path
);
275 static const char SEP
= '/';
277 if (path
[0] && path
[1]) {
278 /* MATTHEW: special case "/./x" */
280 if (path
[2] == SEP
&& path
[1] == '.')
288 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
291 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
292 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
293 && (q
- 1 <= path
|| q
[-1] != SEP
))
302 /* Check that path[2] is NULL! */
303 else if (path
[1] == ':' && !path
[2])
312 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
321 char *wxCopyAbsolutePath(const wxString
& filename
)
326 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
327 char buf
[_MAXPATHLEN
];
329 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
330 char ch
= buf
[strlen(buf
) - 1];
332 if (ch
!= '\\' && ch
!= '/')
338 strcat(buf
, wxBuffer
);
339 return copystring( wxRealPath(buf
) );
341 return copystring( wxBuffer
);
347 ~user/ => user's home dir
348 If the environment variable a = "foo" and b = "bar" then:
365 /* input name in name, pathname output to buf. */
367 char *wxExpandPath(char *buf
, const char *name
)
369 register char *d
, *s
, *nm
;
370 char lnm
[_MAXPATHLEN
];
373 // Some compilers don't like this line.
374 // const char trimchars[] = "\n \t";
383 const char SEP
= '\\';
385 const char SEP
= '/';
388 if (name
== NULL
|| *name
== '\0')
390 nm
= copystring(name
); // Make a scratch copy
393 /* Skip leading whitespace and cr */
394 while (strchr((char *)trimchars
, *nm
) != NULL
)
396 /* And strip off trailing whitespace and cr */
397 s
= nm
+ (q
= strlen(nm
)) - 1;
398 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
406 q
= nm
[0] == '\\' && nm
[1] == '~';
409 /* Expand inline environment variables */
410 while ((*d
++ = *s
)) {
413 if ((*(d
- 1) = *++s
)) {
421 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
426 register char *start
= d
;
427 register braces
= (*s
== '{' || *s
== '(');
428 register char *value
;
430 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
435 value
= getenv(braces
? start
+ 1 : start
);
437 for ((d
= start
- 1); (*d
++ = *value
++););
445 /* Expand ~ and ~user */
448 if (nm
[0] == '~' && !q
)
451 if (nm
[1] == SEP
|| nm
[1] == 0)
453 if ((s
= wxGetUserHome("")) != NULL
) {
458 { /* ~user/filename */
461 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
462 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
463 was_sep
= (*s
== SEP
);
464 nnm
= *s
? s
+ 1 : s
;
466 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
467 if (was_sep
) /* replace only if it was there: */
478 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
480 while ('\0' != (*d
++ = *s
++))
483 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
487 while ((*d
++ = *s
++));
489 delete[] nm_tmp
; // clean up alloc
490 /* Now clean up the buffer */
491 return wxRealPath(buf
);
495 /* Contract Paths to be build upon an environment variable
498 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
500 The call wxExpandPath can convert these back!
503 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
505 static char dest
[_MAXPATHLEN
];
510 strcpy (dest
, WXSTRINGCAST filename
);
512 Unix2DosFilename(dest
);
515 // Handle environment
518 if (envname
!= NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
519 (tcp
= strstr (dest
, val
)) != NULL
)
521 strcpy (wxBuffer
, tcp
+ strlen (val
));
524 strcpy (tcp
, WXSTRINGCAST envname
);
526 strcat (tcp
, wxBuffer
);
529 // Handle User's home (ignore root homes!)
531 if ((val
= wxGetUserHome (user
)) != NULL
&&
532 (len
= strlen(val
)) > 2 &&
533 strncmp(dest
, val
, len
) == 0)
535 strcpy(wxBuffer
, "~");
537 strcat(wxBuffer
, user
);
539 // strcat(wxBuffer, "\\");
541 // strcat(wxBuffer, "/");
543 strcat(wxBuffer
, dest
+ len
);
544 strcpy (dest
, wxBuffer
);
550 // Return just the filename, not the path
552 char *wxFileNameFromPath (char *path
)
558 tcp
= path
+ strlen (path
);
559 while (--tcp
>= path
)
561 if (*tcp
== '/' || *tcp
== '\\'
563 || *tcp
== ':' || *tcp
== ']')
570 if (isalpha (*path
) && *(path
+ 1) == ':')
577 wxString
wxFileNameFromPath (const wxString
& path1
)
582 char *path
= WXSTRINGCAST path1
;
585 tcp
= path
+ strlen (path
);
586 while (--tcp
>= path
)
588 if (*tcp
== '/' || *tcp
== '\\'
590 || *tcp
== ':' || *tcp
== ']')
594 return wxString(tcp
+ 1);
597 if (isalpha (*path
) && *(path
+ 1) == ':')
598 return wxString(path
+ 2);
604 // Return just the directory, or NULL if no directory
606 wxPathOnly (char *path
)
610 static char buf
[_MAXPATHLEN
];
615 int l
= strlen(path
);
620 // Search backward for a backward or forward slash
621 while (!done
&& i
> -1)
624 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
639 // Try Drive specifier
640 if (isalpha (buf
[0]) && buf
[1] == ':')
642 // A:junk --> A:. (since A:.\junk Not A:\junk)
653 // Return just the directory, or NULL if no directory
654 wxString
wxPathOnly (const wxString
& path
)
658 char buf
[_MAXPATHLEN
];
661 strcpy (buf
, WXSTRINGCAST path
);
663 int l
= path
.Length();
668 // Search backward for a backward or forward slash
669 while (!done
&& i
> -1)
672 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
681 return wxString(buf
);
687 // Try Drive specifier
688 if (isalpha (buf
[0]) && buf
[1] == ':')
690 // A:junk --> A:. (since A:.\junk Not A:\junk)
693 return wxString(buf
);
701 // Utility for converting delimiters in DOS filenames to UNIX style
702 // and back again - or we get nasty problems with delimiters.
703 // Also, convert to lower case, since case is significant in UNIX.
706 wxDos2UnixFilename (char *s
)
715 *s
= wxToLower (*s
); // Case INDEPENDENT
722 wxUnix2DosFilename (char *WXUNUSED(s
))
724 // Yes, I really mean this to happen under DOS only! JACS
736 // Concatenate two files to form third
738 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
740 char *outfile
= wxGetTempFileName("cat");
745 // Open the inputs and outputs
746 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
747 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
748 (fp3
= fopen (outfile
, "wb")) == NULL
)
760 while ((ch
= getc (fp1
)) != EOF
)
761 (void) putc (ch
, fp3
);
764 while ((ch
= getc (fp2
)) != EOF
)
765 (void) putc (ch
, fp3
);
769 bool result
= wxRenameFile(outfile
, file3
);
776 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
782 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
784 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
790 while ((ch
= getc (fd1
)) != EOF
)
791 (void) putc (ch
, fd2
);
799 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
801 // Normal system call
802 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
805 if (wxCopyFile(file1
, file2
)) {
813 bool wxRemoveFile(const wxString
& file
)
815 #if defined(_MSC_VER) || defined(__BORLANDC__)
816 int flag
= remove(WXSTRINGCAST file
);
818 int flag
= unlink(WXSTRINGCAST file
);
823 bool wxMkdir(const wxString
& dir
)
827 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WINDOWS__)
828 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
830 return (mkdir(WXSTRINGCAST dir
) == 0);
834 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
839 return (rmdir(WXSTRINGCAST dir
) == 0);
844 bool wxDirExists(const wxString
& dir
)
848 #elif !defined(__WINDOWS__)
850 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
853 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
854 #if defined(__WIN32__)
855 WIN32_FIND_DATA fileInfo
;
858 struct ffblk fileInfo
;
860 struct find_t fileInfo
;
864 #if defined(__WIN32__)
865 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
867 if (h
==INVALID_HANDLE_VALUE
)
871 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
874 // In Borland findfirst has a different argument
875 // ordering from _dos_findfirst. But _dos_findfirst
876 // _should_ be ok in both MS and Borland... why not?
878 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
880 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
889 // does the path exists? (may have or not '/' or '\\' at the end)
890 bool wxPathExists(const char *pszPathName
)
892 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
893 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
894 wxString
strPath(pszPathName
);
895 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
896 strPath
.Last() = '\0';
899 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
902 // Get a temporary filename, opening and closing the file.
903 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
909 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
912 char tmpPath
[MAX_PATH
];
913 ::GetTempPath(MAX_PATH
, tmpPath
);
914 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
916 if (buf
) strcpy(buf
, tmp
);
917 else buf
= copystring(tmp
);
921 static short last_temp
= 0; // cache last to speed things a bit
922 // At most 1000 temp files to a process! We use a ring count.
925 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
927 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
928 if (!wxFileExists( tmp
))
930 // Touch the file to create it (reserve name)
931 FILE *fd
= fopen (tmp
, "w");
938 buf
= copystring( tmp
);
942 cerr
<< "wxWindows: error finding temporary file name.\n";
948 // Get first file name matching given wild card.
952 // Get first file name matching given wild card.
953 // Flags are reserved for future use.
956 static DIR *wxDirStream
= NULL
;
957 static char *wxFileSpec
= NULL
;
958 static int wxFindFileFlags
= 0;
961 char *wxFindFirstFile(const char *spec
, int flags
)
965 closedir(wxDirStream
); // edz 941103: better housekeping
967 wxFindFileFlags
= flags
;
971 wxFileSpec
= copystring(spec
);
973 // Find path only so we can concatenate
974 // found file onto path
975 char *p
= wxPathOnly(wxFileSpec
);
977 /* MATTHEW: special case: path is really "/" */
978 if (p
&& !*p
&& *wxFileSpec
== '/')
980 /* MATTHEW: p is NULL => Local directory */
984 if ((wxDirStream
=opendir(p
))==NULL
)
987 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
988 return wxFindNextFile();
994 char *wxFindNextFile(void)
997 static char buf
[400];
999 /* MATTHEW: [2] Don't crash if we read too many times */
1003 // Find path only so we can concatenate
1004 // found file onto path
1005 char *p
= wxPathOnly(wxFileSpec
);
1006 char *n
= wxFileNameFromPath(wxFileSpec
);
1008 /* MATTHEW: special case: path is really "/" */
1009 if (p
&& !*p
&& *wxFileSpec
== '/')
1013 struct dirent
*nextDir
;
1014 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1017 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1018 directories when flags & wxDIR */
1019 if (wxMatchWild(n
, nextDir
->d_name
)) {
1025 if (strcmp(p
, "/") != 0)
1028 strcat(buf
, nextDir
->d_name
);
1030 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1031 (strcmp(nextDir
->d_name
, "..") == 0)) {
1032 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1036 isdir
= wxDirExists(buf
);
1038 if (!wxFindFileFlags
1039 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1040 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1044 closedir(wxDirStream
);
1052 #elif defined(__WINDOWS__)
1055 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1056 WIN32_FIND_DATA wxFileStruc
;
1059 static struct ffblk wxFileStruc
;
1061 static struct _find_t wxFileStruc
;
1064 static wxString wxFileSpec
= "";
1065 static int wxFindFileFlags
;
1067 char *wxFindFirstFile(const wxString
& spec
, int flags
)
1070 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1072 // Find path only so we can concatenate
1073 // found file onto path
1074 wxString
path1(wxFileSpec
);
1075 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1076 if (p
&& (strlen(p
) > 0))
1077 strcpy(wxBuffer
, p
);
1082 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1083 FindClose(wxFileStrucHandle
);
1085 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1087 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1090 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1092 if (isdir
&& !(flags
& wxDIR
))
1093 return wxFindNextFile();
1094 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1095 return wxFindNextFile();
1097 if (wxBuffer
[0] != 0)
1098 strcat(wxBuffer
, "\\");
1099 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1103 int flag
= _A_NORMAL
;
1104 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1108 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1110 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1113 /* MATTHEW: [5] Check directory flag */
1117 attrib
= wxFileStruc
.ff_attrib
;
1119 attrib
= wxFileStruc
.attrib
;
1122 if (attrib
& _A_SUBDIR
) {
1123 if (!(wxFindFileFlags
& wxDIR
))
1124 return wxFindNextFile();
1125 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1126 return wxFindNextFile();
1128 if (wxBuffer
[0] != 0)
1129 strcat(wxBuffer
, "\\");
1132 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1134 strcat(wxBuffer
, wxFileStruc
.name
);
1143 char *wxFindNextFile(void)
1145 // Find path only so we can concatenate
1146 // found file onto path
1147 wxString
p2(wxFileSpec
);
1148 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1149 if (p
&& (strlen(p
) > 0))
1150 strcpy(wxBuffer
, p
);
1157 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1160 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1162 FindClose(wxFileStrucHandle
);
1163 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1167 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1169 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1171 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1174 if (wxBuffer
[0] != 0)
1175 strcat(wxBuffer
, "\\");
1176 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1181 if (findnext(&wxFileStruc
) == 0)
1183 if (_dos_findnext(&wxFileStruc
) == 0)
1186 /* MATTHEW: [5] Check directory flag */
1190 attrib
= wxFileStruc
.ff_attrib
;
1192 attrib
= wxFileStruc
.attrib
;
1195 if (attrib
& _A_SUBDIR
) {
1196 if (!(wxFindFileFlags
& wxDIR
))
1198 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1202 if (wxBuffer
[0] != 0)
1203 strcat(wxBuffer
, "\\");
1205 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1207 strcat(wxBuffer
, wxFileStruc
.name
);
1219 // Get current working directory.
1220 // If buf is NULL, allocates space using new, else
1222 char *wxGetWorkingDirectory(char *buf
, int sz
)
1225 buf
= new char[sz
+1];
1227 if (_getcwd(buf
, sz
) == NULL
) {
1229 if (getcwd(buf
, sz
) == NULL
) {
1237 bool wxSetWorkingDirectory(const wxString
& d
)
1240 return (chdir(d
) == 0);
1241 #elif defined(__WINDOWS__)
1244 return (bool)(SetCurrentDirectory(d
) != 0);
1246 // Must change drive, too.
1247 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1250 char firstChar
= d
[0];
1254 firstChar
= firstChar
- 32;
1256 // To a drive number
1257 unsigned int driveNo
= firstChar
- 64;
1260 unsigned int noDrives
;
1261 _dos_setdrive(driveNo
, &noDrives
);
1264 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1272 bool wxEndsWithPathSeparator(const char *pszFileName
)
1274 size_t len
= Strlen(pszFileName
);
1278 return wxIsPathSeparator(pszFileName
[len
- 1]);
1281 // find a file in a list of directories, returns false if not found
1282 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1284 // we assume that it's not empty
1285 wxCHECK_RET( Strlen(pszFile
) != 0, FALSE
);
1287 // skip path separator in the beginning of the file name if present
1288 if ( wxIsPathSeparator(*pszFile
) )
1291 // copy the path (strtok will modify it)
1292 char *szPath
= new char[strlen(pszPath
) + 1];
1293 strcpy(szPath
, pszPath
);
1297 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok(NULL
, PATH_SEP
) ) {
1298 // search for the file in this directory
1300 if ( !wxEndsWithPathSeparator(pc
) )
1301 strFile
+= FILE_SEP_PATH
;
1304 if ( FileExists(strFile
) ) {
1312 return pc
!= NULL
; // if true => we breaked from the loop