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 // Destructive removal of /./ and /../ stuff
271 char *wxRealPath (char *path
)
274 static const char SEP
= '\\';
275 Unix2DosFilename(path
);
277 static const char SEP
= '/';
279 if (path
[0] && path
[1]) {
280 /* MATTHEW: special case "/./x" */
282 if (path
[2] == SEP
&& path
[1] == '.')
290 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
293 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
294 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
295 && (q
- 1 <= path
|| q
[-1] != SEP
))
304 /* Check that path[2] is NULL! */
305 else if (path
[1] == ':' && !path
[2])
314 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
323 char *wxCopyAbsolutePath(const wxString
& filename
)
328 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
329 char buf
[_MAXPATHLEN
];
331 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
332 char ch
= buf
[strlen(buf
) - 1];
334 if (ch
!= '\\' && ch
!= '/')
340 strcat(buf
, wxBuffer
);
341 return copystring( wxRealPath(buf
) );
343 return copystring( wxBuffer
);
349 ~user/ => user's home dir
350 If the environment variable a = "foo" and b = "bar" then:
367 /* input name in name, pathname output to buf. */
369 char *wxExpandPath(char *buf
, const char *name
)
371 register char *d
, *s
, *nm
;
372 char lnm
[_MAXPATHLEN
];
375 // Some compilers don't like this line.
376 // const char trimchars[] = "\n \t";
385 const char SEP
= '\\';
387 const char SEP
= '/';
390 if (name
== NULL
|| *name
== '\0')
392 nm
= copystring(name
); // Make a scratch copy
395 /* Skip leading whitespace and cr */
396 while (strchr((char *)trimchars
, *nm
) != NULL
)
398 /* And strip off trailing whitespace and cr */
399 s
= nm
+ (q
= strlen(nm
)) - 1;
400 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
408 q
= nm
[0] == '\\' && nm
[1] == '~';
411 /* Expand inline environment variables */
412 while ((*d
++ = *s
)) {
415 if ((*(d
- 1) = *++s
)) {
423 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
428 register char *start
= d
;
429 register braces
= (*s
== '{' || *s
== '(');
430 register char *value
;
432 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
437 value
= getenv(braces
? start
+ 1 : start
);
439 for ((d
= start
- 1); (*d
++ = *value
++););
447 /* Expand ~ and ~user */
450 if (nm
[0] == '~' && !q
)
453 if (nm
[1] == SEP
|| nm
[1] == 0)
455 if ((s
= wxGetUserHome("")) != NULL
) {
460 { /* ~user/filename */
463 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
464 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
465 was_sep
= (*s
== SEP
);
466 nnm
= *s
? s
+ 1 : s
;
468 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
469 if (was_sep
) /* replace only if it was there: */
480 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
482 while ('\0' != (*d
++ = *s
++))
485 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
489 while ((*d
++ = *s
++));
491 delete[] nm_tmp
; // clean up alloc
492 /* Now clean up the buffer */
493 return wxRealPath(buf
);
497 /* Contract Paths to be build upon an environment variable
500 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
502 The call wxExpandPath can convert these back!
505 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
507 static char dest
[_MAXPATHLEN
];
512 strcpy (dest
, WXSTRINGCAST filename
);
514 Unix2DosFilename(dest
);
517 // Handle environment
520 if (envname
!= NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
521 (tcp
= strstr (dest
, val
)) != NULL
)
523 strcpy (wxBuffer
, tcp
+ strlen (val
));
526 strcpy (tcp
, WXSTRINGCAST envname
);
528 strcat (tcp
, wxBuffer
);
531 // Handle User's home (ignore root homes!)
533 if ((val
= wxGetUserHome (user
)) != NULL
&&
534 (len
= strlen(val
)) > 2 &&
535 strncmp(dest
, val
, len
) == 0)
537 strcpy(wxBuffer
, "~");
539 strcat(wxBuffer
, user
);
541 // strcat(wxBuffer, "\\");
543 // strcat(wxBuffer, "/");
545 strcat(wxBuffer
, dest
+ len
);
546 strcpy (dest
, wxBuffer
);
552 // Return just the filename, not the path
554 char *wxFileNameFromPath (char *path
)
560 tcp
= path
+ strlen (path
);
561 while (--tcp
>= path
)
563 if (*tcp
== '/' || *tcp
== '\\'
565 || *tcp
== ':' || *tcp
== ']')
572 if (isalpha (*path
) && *(path
+ 1) == ':')
579 wxString
wxFileNameFromPath (const wxString
& path1
)
584 char *path
= WXSTRINGCAST path1
;
587 tcp
= path
+ strlen (path
);
588 while (--tcp
>= path
)
590 if (*tcp
== '/' || *tcp
== '\\'
592 || *tcp
== ':' || *tcp
== ']')
596 return wxString(tcp
+ 1);
599 if (isalpha (*path
) && *(path
+ 1) == ':')
600 return wxString(path
+ 2);
606 // Return just the directory, or NULL if no directory
608 wxPathOnly (char *path
)
612 static char buf
[_MAXPATHLEN
];
617 int l
= strlen(path
);
622 // Search backward for a backward or forward slash
623 while (!done
&& i
> -1)
626 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
641 // Try Drive specifier
642 if (isalpha (buf
[0]) && buf
[1] == ':')
644 // A:junk --> A:. (since A:.\junk Not A:\junk)
655 // Return just the directory, or NULL if no directory
656 wxString
wxPathOnly (const wxString
& path
)
660 char buf
[_MAXPATHLEN
];
663 strcpy (buf
, WXSTRINGCAST path
);
665 int l
= path
.Length();
670 // Search backward for a backward or forward slash
671 while (!done
&& i
> -1)
674 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
683 return wxString(buf
);
689 // Try Drive specifier
690 if (isalpha (buf
[0]) && buf
[1] == ':')
692 // A:junk --> A:. (since A:.\junk Not A:\junk)
695 return wxString(buf
);
703 // Utility for converting delimiters in DOS filenames to UNIX style
704 // and back again - or we get nasty problems with delimiters.
705 // Also, convert to lower case, since case is significant in UNIX.
708 wxDos2UnixFilename (char *s
)
717 *s
= wxToLower (*s
); // Case INDEPENDENT
724 wxUnix2DosFilename (char *s
)
726 // Yes, I really mean this to happen under DOS only! JACS
738 // Concatenate two files to form third
740 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
742 char *outfile
= wxGetTempFileName("cat");
747 // Open the inputs and outputs
748 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
749 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
750 (fp3
= fopen (outfile
, "wb")) == NULL
)
762 while ((ch
= getc (fp1
)) != EOF
)
763 (void) putc (ch
, fp3
);
766 while ((ch
= getc (fp2
)) != EOF
)
767 (void) putc (ch
, fp3
);
771 bool result
= wxRenameFile(outfile
, file3
);
778 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
784 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
786 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
792 while ((ch
= getc (fd1
)) != EOF
)
793 (void) putc (ch
, fd2
);
801 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
803 // Normal system call
804 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
807 if (wxCopyFile(file1
, file2
)) {
815 bool wxRemoveFile(const wxString
& file
)
817 #if defined(_MSC_VER) || defined(__BORLANDC__)
818 int flag
= remove(WXSTRINGCAST file
);
820 int flag
= unlink(WXSTRINGCAST file
);
825 bool wxMkdir(const wxString
& dir
)
829 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
830 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
832 return (mkdir(WXSTRINGCAST dir
) == 0);
836 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
841 return (rmdir(WXSTRINGCAST dir
) == 0);
846 bool wxDirExists(const wxString
& dir
)
850 #elif !defined(__WXMSW__)
852 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
855 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
856 #if defined(__WIN32__)
857 WIN32_FIND_DATA fileInfo
;
860 struct ffblk fileInfo
;
862 struct find_t fileInfo
;
866 #if defined(__WIN32__)
867 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
869 if (h
==INVALID_HANDLE_VALUE
)
873 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
876 // In Borland findfirst has a different argument
877 // ordering from _dos_findfirst. But _dos_findfirst
878 // _should_ be ok in both MS and Borland... why not?
880 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
882 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
891 // does the path exists? (may have or not '/' or '\\' at the end)
892 bool wxPathExists(const char *pszPathName
)
894 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
895 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
896 wxString
strPath(pszPathName
);
897 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
898 strPath
.Last() = '\0';
901 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
904 // Get a temporary filename, opening and closing the file.
905 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
911 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
914 char tmpPath
[MAX_PATH
];
915 ::GetTempPath(MAX_PATH
, tmpPath
);
916 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
918 if (buf
) strcpy(buf
, tmp
);
919 else buf
= copystring(tmp
);
923 static short last_temp
= 0; // cache last to speed things a bit
924 // At most 1000 temp files to a process! We use a ring count.
927 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
929 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
930 if (!wxFileExists( tmp
))
932 // Touch the file to create it (reserve name)
933 FILE *fd
= fopen (tmp
, "w");
940 buf
= copystring( tmp
);
944 cerr
<< "wxWindows: error finding temporary file name.\n";
950 // Get first file name matching given wild card.
954 // Get first file name matching given wild card.
955 // Flags are reserved for future use.
958 static DIR *wxDirStream
= NULL
;
959 static char *wxFileSpec
= NULL
;
960 static int wxFindFileFlags
= 0;
963 char *wxFindFirstFile(const char *spec
, int flags
)
967 closedir(wxDirStream
); // edz 941103: better housekeping
969 wxFindFileFlags
= flags
;
973 wxFileSpec
= copystring(spec
);
975 // Find path only so we can concatenate
976 // found file onto path
977 char *p
= wxPathOnly(wxFileSpec
);
979 /* MATTHEW: special case: path is really "/" */
980 if (p
&& !*p
&& *wxFileSpec
== '/')
982 /* MATTHEW: p is NULL => Local directory */
986 if ((wxDirStream
=opendir(p
))==NULL
)
989 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
990 return wxFindNextFile();
996 char *wxFindNextFile(void)
999 static char buf
[400];
1001 /* MATTHEW: [2] Don't crash if we read too many times */
1005 // Find path only so we can concatenate
1006 // found file onto path
1007 char *p
= wxPathOnly(wxFileSpec
);
1008 char *n
= wxFileNameFromPath(wxFileSpec
);
1010 /* MATTHEW: special case: path is really "/" */
1011 if (p
&& !*p
&& *wxFileSpec
== '/')
1015 struct dirent
*nextDir
;
1016 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1019 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1020 directories when flags & wxDIR */
1021 if (wxMatchWild(n
, nextDir
->d_name
)) {
1027 if (strcmp(p
, "/") != 0)
1030 strcat(buf
, nextDir
->d_name
);
1032 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1033 (strcmp(nextDir
->d_name
, "..") == 0)) {
1034 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1038 isdir
= wxDirExists(buf
);
1040 if (!wxFindFileFlags
1041 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1042 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1046 closedir(wxDirStream
);
1054 #elif defined(__WXMSW__)
1057 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1058 WIN32_FIND_DATA wxFileStruc
;
1061 static struct ffblk wxFileStruc
;
1063 static struct _find_t wxFileStruc
;
1066 static wxString wxFileSpec
= "";
1067 static int wxFindFileFlags
;
1069 char *wxFindFirstFile(const char *spec
, int flags
)
1072 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1074 // Find path only so we can concatenate
1075 // found file onto path
1076 wxString
path1(wxFileSpec
);
1077 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1078 if (p
&& (strlen(p
) > 0))
1079 strcpy(wxBuffer
, p
);
1084 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1085 FindClose(wxFileStrucHandle
);
1087 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1089 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1092 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1094 if (isdir
&& !(flags
& wxDIR
))
1095 return wxFindNextFile();
1096 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1097 return wxFindNextFile();
1099 if (wxBuffer
[0] != 0)
1100 strcat(wxBuffer
, "\\");
1101 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1105 int flag
= _A_NORMAL
;
1106 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1110 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1112 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1115 /* MATTHEW: [5] Check directory flag */
1119 attrib
= wxFileStruc
.ff_attrib
;
1121 attrib
= wxFileStruc
.attrib
;
1124 if (attrib
& _A_SUBDIR
) {
1125 if (!(wxFindFileFlags
& wxDIR
))
1126 return wxFindNextFile();
1127 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1128 return wxFindNextFile();
1130 if (wxBuffer
[0] != 0)
1131 strcat(wxBuffer
, "\\");
1134 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1136 strcat(wxBuffer
, wxFileStruc
.name
);
1145 char *wxFindNextFile(void)
1147 // Find path only so we can concatenate
1148 // found file onto path
1149 wxString
p2(wxFileSpec
);
1150 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1151 if (p
&& (strlen(p
) > 0))
1152 strcpy(wxBuffer
, p
);
1159 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1162 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1164 FindClose(wxFileStrucHandle
);
1165 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1169 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1171 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1173 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1176 if (wxBuffer
[0] != 0)
1177 strcat(wxBuffer
, "\\");
1178 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1183 if (findnext(&wxFileStruc
) == 0)
1185 if (_dos_findnext(&wxFileStruc
) == 0)
1188 /* MATTHEW: [5] Check directory flag */
1192 attrib
= wxFileStruc
.ff_attrib
;
1194 attrib
= wxFileStruc
.attrib
;
1197 if (attrib
& _A_SUBDIR
) {
1198 if (!(wxFindFileFlags
& wxDIR
))
1200 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1204 if (wxBuffer
[0] != 0)
1205 strcat(wxBuffer
, "\\");
1207 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1209 strcat(wxBuffer
, wxFileStruc
.name
);
1221 // Get current working directory.
1222 // If buf is NULL, allocates space using new, else
1224 char *wxGetWorkingDirectory(char *buf
, int sz
)
1227 buf
= new char[sz
+1];
1229 if (_getcwd(buf
, sz
) == NULL
) {
1231 if (getcwd(buf
, sz
) == NULL
) {
1239 bool wxSetWorkingDirectory(const wxString
& d
)
1242 return (chdir(d
) == 0);
1243 #elif defined(__WXMSW__)
1246 return (bool)(SetCurrentDirectory(d
) != 0);
1248 // Must change drive, too.
1249 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1252 char firstChar
= d
[0];
1256 firstChar
= firstChar
- 32;
1258 // To a drive number
1259 unsigned int driveNo
= firstChar
- 64;
1262 unsigned int noDrives
;
1263 _dos_setdrive(driveNo
, &noDrives
);
1266 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1274 bool wxEndsWithPathSeparator(const char *pszFileName
)
1276 size_t len
= Strlen(pszFileName
);
1280 return wxIsPathSeparator(pszFileName
[len
- 1]);
1283 // find a file in a list of directories, returns false if not found
1284 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1286 // we assume that it's not empty
1287 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1288 "empty file name in wxFindFileInPath");
1290 // skip path separator in the beginning of the file name if present
1291 if ( wxIsPathSeparator(*pszFile
) )
1294 // copy the path (strtok will modify it)
1295 char *szPath
= new char[strlen(pszPath
) + 1];
1296 strcpy(szPath
, pszPath
);
1300 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok(NULL
, PATH_SEP
) ) {
1301 // search for the file in this directory
1303 if ( !wxEndsWithPathSeparator(pc
) )
1304 strFile
+= FILE_SEP_PATH
;
1307 if ( FileExists(strFile
) ) {
1315 return pc
!= NULL
; // if true => we breaked from the loop
1318 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1323 wxCHECK_RET( pszFileName
, "NULL file name in wxSplitPath" );
1325 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1326 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1327 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1329 // take the last of the two
1330 uint nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1331 uint nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1332 if ( nPosDos
> nPosUnix
)
1334 uint nLen
= Strlen(pszFileName
);
1337 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1339 uint nPosDot
= pDot
- pszFileName
;
1341 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1343 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1347 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);