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"
35 #if !defined(__WATCOMC__)
36 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
41 #include <sys/types.h>
57 #include <sys/unistd.h>
58 // #include <sys/stat.h>
64 #define stricmp strcasecmp
67 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
68 // this (3.1 I believe) and how to test for it.
69 // If this works for Borland 4.0 as well, then no worries.
77 #define _MAXPATHLEN 500
79 #if !USE_SHARED_LIBRARY
80 IMPLEMENT_DYNAMIC_CLASS(wxPathList
, wxStringList
)
83 extern char *wxBuffer
;
85 void wxPathList::Add (const wxString
& path
)
87 wxStringList::Add ((char *)(const char *)path
);
90 // Add paths e.g. from the PATH environment variable
91 void wxPathList::AddEnvList (const wxString
& envVariable
)
93 static const char PATH_TOKS
[] =
95 " ;"; // Don't seperate with colon in DOS (used for drive)
100 char *val
= getenv (WXSTRINGCAST envVariable
);
103 char *s
= copystring (val
);
104 char *token
= strtok (s
, PATH_TOKS
);
108 Add (copystring (token
));
111 if ((token
= strtok (NULL
, PATH_TOKS
)) != NULL
)
112 Add (wxString(token
));
119 // Given a full filename (with path), ensure that that file can
120 // be accessed again USING FILENAME ONLY by adding the path
121 // to the list if not already there.
122 void wxPathList::EnsureFileAccessible (const wxString
& path
)
124 wxString
path1(path
);
125 char *path_only
= wxPathOnly (WXSTRINGCAST path1
);
128 if (!Member (wxString(path_only
)))
129 Add (wxString(path_only
));
133 bool wxPathList::Member (const wxString
& path
)
135 for (wxNode
* node
= First (); node
!= NULL
; node
= node
->Next ())
137 wxString
path2((char *) node
->Data ());
139 #if defined(__WXMSW__) || defined(__VMS__)
141 path
.CompareTo (path2
, wxString::ignoreCase
) == 0
143 // Case sensitive File System
144 path
.CompareTo (path2
) == 0
152 wxString
wxPathList::FindValidPath (const wxString
& file
)
154 if (wxFileExists (wxExpandPath(wxBuffer
, file
)))
155 return wxString(wxBuffer
);
157 char buf
[_MAXPATHLEN
];
158 strcpy(buf
, wxBuffer
);
160 char *filename
= IsAbsolutePath (buf
) ? wxFileNameFromPath (buf
) : (char *)buf
;
162 for (wxNode
* node
= First (); node
; node
= node
->Next ())
164 char *path
= (char *) node
->Data ();
165 strcpy (wxBuffer
, path
);
166 char ch
= wxBuffer
[strlen(wxBuffer
)-1];
167 if (ch
!= '\\' && ch
!= '/')
168 strcat (wxBuffer
, "/");
169 strcat (wxBuffer
, filename
);
171 Unix2DosFilename (wxBuffer
);
173 if (wxFileExists (wxBuffer
))
175 return wxString(wxBuffer
); // Found!
179 return wxString(""); // Not found
182 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
)
184 wxString f
= FindValidPath(file
);
185 if (wxIsAbsolutePath(f
))
190 wxGetWorkingDirectory(buf
, 499);
191 int len
= (int)strlen(buf
);
195 if (lastCh
!= '/' && lastCh
!= '\\')
203 strcat(buf
, (const char *)f
);
204 strcpy(wxBuffer
, buf
);
205 return wxString(wxBuffer
);
210 wxFileExists (const wxString
& filename
)
214 if (filename
&& stat ((char *)(const char *)filename
, &stbuf
) == 0)
219 /* Vadim's alternative implementation
221 // does the file exist?
222 bool wxFileExists(const char *pszFileName)
225 return !access(pszFileName, 0) &&
226 !stat(pszFileName, &st) &&
227 (st.st_mode & S_IFREG);
232 wxIsAbsolutePath (const wxString
& filename
)
236 if (filename
[0] == '/'
238 || (filename
[0] == '[' && filename
[1] != '.')
242 || filename
[0] == '\\' || (isalpha (filename
[0]) && filename
[1] == ':')
251 * Strip off any extension (dot something) from end of file,
252 * IF one exists. Inserts zero into buffer.
256 void wxStripExtension(char *buffer
)
258 int len
= strlen(buffer
);
262 if (buffer
[i
] == '.')
271 void wxStripExtension(wxString
& buffer
)
273 size_t len
= buffer
.Length();
277 if (buffer
.GetChar(i
) == '.')
279 buffer
= buffer
.Left(i
);
286 // Destructive removal of /./ and /../ stuff
287 char *wxRealPath (char *path
)
290 static const char SEP
= '\\';
291 Unix2DosFilename(path
);
293 static const char SEP
= '/';
295 if (path
[0] && path
[1]) {
296 /* MATTHEW: special case "/./x" */
298 if (path
[2] == SEP
&& path
[1] == '.')
306 if (p
[1] == '.' && p
[2] == '.' && (p
[3] == SEP
|| p
[3] == '\0'))
309 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--);
310 if (q
[0] == SEP
&& (q
[1] != '.' || q
[2] != '.' || q
[3] != SEP
)
311 && (q
- 1 <= path
|| q
[-1] != SEP
))
320 /* Check that path[2] is NULL! */
321 else if (path
[1] == ':' && !path
[2])
330 else if (p
[1] == '.' && (p
[2] == SEP
|| p
[2] == '\0'))
339 char *wxCopyAbsolutePath(const wxString
& filename
)
344 if (! IsAbsolutePath(wxExpandPath(wxBuffer
, filename
))) {
345 char buf
[_MAXPATHLEN
];
347 wxGetWorkingDirectory(buf
, sizeof(buf
)/sizeof(char));
348 char ch
= buf
[strlen(buf
) - 1];
350 if (ch
!= '\\' && ch
!= '/')
356 strcat(buf
, wxBuffer
);
357 return copystring( wxRealPath(buf
) );
359 return copystring( wxBuffer
);
365 ~user/ => user's home dir
366 If the environment variable a = "foo" and b = "bar" then:
383 /* input name in name, pathname output to buf. */
385 char *wxExpandPath(char *buf
, const char *name
)
387 register char *d
, *s
, *nm
;
388 char lnm
[_MAXPATHLEN
];
391 // Some compilers don't like this line.
392 // const char trimchars[] = "\n \t";
401 const char SEP
= '\\';
403 const char SEP
= '/';
406 if (name
== NULL
|| *name
== '\0')
408 nm
= copystring(name
); // Make a scratch copy
411 /* Skip leading whitespace and cr */
412 while (strchr((char *)trimchars
, *nm
) != NULL
)
414 /* And strip off trailing whitespace and cr */
415 s
= nm
+ (q
= strlen(nm
)) - 1;
416 while (q
-- && strchr((char *)trimchars
, *s
) != NULL
)
424 q
= nm
[0] == '\\' && nm
[1] == '~';
427 /* Expand inline environment variables */
428 while ((*d
++ = *s
)) {
431 if ((*(d
- 1) = *++s
)) {
439 if (*s
++ == '$' && (*s
== '{' || *s
== ')'))
444 register char *start
= d
;
445 register braces
= (*s
== '{' || *s
== '(');
446 register char *value
;
448 if (braces
? (*s
== '}' || *s
== ')') : !(isalnum(*s
) || *s
== '_') )
453 value
= getenv(braces
? start
+ 1 : start
);
455 for ((d
= start
- 1); (*d
++ = *value
++););
463 /* Expand ~ and ~user */
466 if (nm
[0] == '~' && !q
)
469 if (nm
[1] == SEP
|| nm
[1] == 0)
471 if ((s
= wxGetUserHome("")) != NULL
) {
476 { /* ~user/filename */
479 for (s
= nm
; *s
&& *s
!= SEP
; s
++);
480 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
481 was_sep
= (*s
== SEP
);
482 nnm
= *s
? s
+ 1 : s
;
484 if ((home
= wxGetUserHome(wxString(nm
+ 1))) == NULL
) {
485 if (was_sep
) /* replace only if it was there: */
496 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
498 while ('\0' != (*d
++ = *s
++))
501 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
505 while ((*d
++ = *s
++));
507 delete[] nm_tmp
; // clean up alloc
508 /* Now clean up the buffer */
509 return wxRealPath(buf
);
513 /* Contract Paths to be build upon an environment variable
516 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
518 The call wxExpandPath can convert these back!
521 wxContractPath (const wxString
& filename
, const wxString
& envname
, const wxString
& user
)
523 static char dest
[_MAXPATHLEN
];
528 strcpy (dest
, WXSTRINGCAST filename
);
530 Unix2DosFilename(dest
);
533 // Handle environment
536 if (envname
!= NULL
&& (val
= getenv (WXSTRINGCAST envname
)) != NULL
&&
537 (tcp
= strstr (dest
, val
)) != NULL
)
539 strcpy (wxBuffer
, tcp
+ strlen (val
));
542 strcpy (tcp
, WXSTRINGCAST envname
);
544 strcat (tcp
, wxBuffer
);
547 // Handle User's home (ignore root homes!)
549 if ((val
= wxGetUserHome (user
)) != NULL
&&
550 (len
= strlen(val
)) > 2 &&
551 strncmp(dest
, val
, len
) == 0)
553 strcpy(wxBuffer
, "~");
555 strcat(wxBuffer
, user
);
557 // strcat(wxBuffer, "\\");
559 // strcat(wxBuffer, "/");
561 strcat(wxBuffer
, dest
+ len
);
562 strcpy (dest
, wxBuffer
);
568 // Return just the filename, not the path
570 char *wxFileNameFromPath (char *path
)
576 tcp
= path
+ strlen (path
);
577 while (--tcp
>= path
)
579 if (*tcp
== '/' || *tcp
== '\\'
581 || *tcp
== ':' || *tcp
== ']')
588 if (isalpha (*path
) && *(path
+ 1) == ':')
595 wxString
wxFileNameFromPath (const wxString
& path1
)
600 char *path
= WXSTRINGCAST path1
;
603 tcp
= path
+ strlen (path
);
604 while (--tcp
>= path
)
606 if (*tcp
== '/' || *tcp
== '\\'
608 || *tcp
== ':' || *tcp
== ']')
612 return wxString(tcp
+ 1);
615 if (isalpha (*path
) && *(path
+ 1) == ':')
616 return wxString(path
+ 2);
622 // Return just the directory, or NULL if no directory
624 wxPathOnly (char *path
)
628 static char buf
[_MAXPATHLEN
];
633 int l
= strlen(path
);
638 // Search backward for a backward or forward slash
639 while (!done
&& i
> -1)
642 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
657 // Try Drive specifier
658 if (isalpha (buf
[0]) && buf
[1] == ':')
660 // A:junk --> A:. (since A:.\junk Not A:\junk)
671 // Return just the directory, or NULL if no directory
672 wxString
wxPathOnly (const wxString
& path
)
676 char buf
[_MAXPATHLEN
];
679 strcpy (buf
, WXSTRINGCAST path
);
681 int l
= path
.Length();
686 // Search backward for a backward or forward slash
687 while (!done
&& i
> -1)
690 if (path
[i
] == '/' || path
[i
] == '\\' || path
[i
] == ']')
699 return wxString(buf
);
705 // Try Drive specifier
706 if (isalpha (buf
[0]) && buf
[1] == ':')
708 // A:junk --> A:. (since A:.\junk Not A:\junk)
711 return wxString(buf
);
719 // Utility for converting delimiters in DOS filenames to UNIX style
720 // and back again - or we get nasty problems with delimiters.
721 // Also, convert to lower case, since case is significant in UNIX.
724 wxDos2UnixFilename (char *s
)
733 *s
= wxToLower (*s
); // Case INDEPENDENT
741 wxUnix2DosFilename (char *s
)
743 wxUnix2DosFilename (char *WXUNUSED(s
))
746 // Yes, I really mean this to happen under DOS only! JACS
758 // Concatenate two files to form third
760 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
762 char *outfile
= wxGetTempFileName("cat");
767 // Open the inputs and outputs
768 if ((fp1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
||
769 (fp2
= fopen (WXSTRINGCAST file2
, "rb")) == NULL
||
770 (fp3
= fopen (outfile
, "wb")) == NULL
)
782 while ((ch
= getc (fp1
)) != EOF
)
783 (void) putc (ch
, fp3
);
786 while ((ch
= getc (fp2
)) != EOF
)
787 (void) putc (ch
, fp3
);
791 bool result
= wxRenameFile(outfile
, file3
);
798 wxCopyFile (const wxString
& file1
, const wxString
& file2
)
804 if ((fd1
= fopen (WXSTRINGCAST file1
, "rb")) == NULL
)
806 if ((fd2
= fopen (WXSTRINGCAST file2
, "wb")) == NULL
)
812 while ((ch
= getc (fd1
)) != EOF
)
813 (void) putc (ch
, fd2
);
821 wxRenameFile (const wxString
& file1
, const wxString
& file2
)
823 // Normal system call
824 if (0 == rename (WXSTRINGCAST file1
, WXSTRINGCAST file2
))
827 if (wxCopyFile(file1
, file2
)) {
835 bool wxRemoveFile(const wxString
& file
)
837 #if defined(_MSC_VER) || defined(__BORLANDC__)
838 int flag
= remove(WXSTRINGCAST file
);
840 int flag
= unlink(WXSTRINGCAST file
);
845 bool wxMkdir(const wxString
& dir
)
849 #elif (defined(__GNUWIN32__) && !defined(__MINGW32__)) || !defined(__WXMSW__)
850 return (mkdir (WXSTRINGCAST dir
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
) == 0);
852 return (mkdir(WXSTRINGCAST dir
) == 0);
856 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
861 return (rmdir(WXSTRINGCAST dir
) == 0);
866 bool wxDirExists(const wxString
& dir
)
870 #elif !defined(__WXMSW__)
872 return (stat(dir
, &sbuf
) != -1) && S_ISDIR(sbuf
.st_mode
) ? TRUE
: FALSE
;
875 /* MATTHEW: [6] Always use same code for Win32, call FindClose */
876 #if defined(__WIN32__)
877 WIN32_FIND_DATA fileInfo
;
880 struct ffblk fileInfo
;
882 struct find_t fileInfo
;
886 #if defined(__WIN32__)
887 HANDLE h
= FindFirstFile((LPTSTR
) WXSTRINGCAST dir
,(LPWIN32_FIND_DATA
)&fileInfo
);
889 if (h
==INVALID_HANDLE_VALUE
)
893 return ((fileInfo
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) == FILE_ATTRIBUTE_DIRECTORY
);
896 // In Borland findfirst has a different argument
897 // ordering from _dos_findfirst. But _dos_findfirst
898 // _should_ be ok in both MS and Borland... why not?
900 return ((findfirst(WXSTRINGCAST dir
, &fileInfo
, _A_SUBDIR
) == 0 && (fileInfo
.ff_attrib
& _A_SUBDIR
) != 0));
902 return (((_dos_findfirst(WXSTRINGCAST dir
, _A_SUBDIR
, &fileInfo
) == 0) && (fileInfo
.attrib
& _A_SUBDIR
)) != 0);
911 // does the path exists? (may have or not '/' or '\\' at the end)
912 bool wxPathExists(const char *pszPathName
)
914 // Windows API returns -1 from stat for "c:\dir\" if "c:\dir" exists
915 // OTOH, we should change "d:" to "d:\" and leave "\" as is.
916 wxString
strPath(pszPathName
);
917 if ( wxEndsWithPathSeparator(pszPathName
) && pszPathName
[1] != '\0' )
918 strPath
.Last() = '\0';
921 return stat(strPath
, &st
) == 0 && (st
.st_mode
& S_IFDIR
);
924 // Get a temporary filename, opening and closing the file.
925 char *wxGetTempFileName(const wxString
& prefix
, char *buf
)
931 ::GetTempFileName(0, WXSTRINGCAST prefix
, 0, tmp
);
934 char tmpPath
[MAX_PATH
];
935 ::GetTempPath(MAX_PATH
, tmpPath
);
936 ::GetTempFileName(tmpPath
, WXSTRINGCAST prefix
, 0, tmp
);
938 if (buf
) strcpy(buf
, tmp
);
939 else buf
= copystring(tmp
);
943 static short last_temp
= 0; // cache last to speed things a bit
944 // At most 1000 temp files to a process! We use a ring count.
947 for (short suffix
= last_temp
+ 1; suffix
!= last_temp
; ++suffix
%= 1000)
949 sprintf (tmp
, "/tmp/%s%d.%03x", WXSTRINGCAST prefix
, (int) getpid (), (int) suffix
);
950 if (!wxFileExists( tmp
))
952 // Touch the file to create it (reserve name)
953 FILE *fd
= fopen (tmp
, "w");
960 buf
= copystring( tmp
);
964 cerr
<< _("wxWindows: error finding temporary file name.\n");
970 // Get first file name matching given wild card.
974 // Get first file name matching given wild card.
975 // Flags are reserved for future use.
978 static DIR *wxDirStream
= NULL
;
979 static char *wxFileSpec
= NULL
;
980 static int wxFindFileFlags
= 0;
983 char *wxFindFirstFile(const char *spec
, int flags
)
987 closedir(wxDirStream
); // edz 941103: better housekeping
989 wxFindFileFlags
= flags
;
993 wxFileSpec
= copystring(spec
);
995 // Find path only so we can concatenate
996 // found file onto path
997 char *p
= wxPathOnly(wxFileSpec
);
999 /* MATTHEW: special case: path is really "/" */
1000 if (p
&& !*p
&& *wxFileSpec
== '/')
1002 /* MATTHEW: p is NULL => Local directory */
1006 if ((wxDirStream
=opendir(p
))==NULL
)
1009 /* MATTHEW: [5] wxFindNextFile can do the rest of the work */
1010 return wxFindNextFile();
1016 char *wxFindNextFile(void)
1019 static char buf
[400];
1021 /* MATTHEW: [2] Don't crash if we read too many times */
1025 // Find path only so we can concatenate
1026 // found file onto path
1027 char *p
= wxPathOnly(wxFileSpec
);
1028 char *n
= wxFileNameFromPath(wxFileSpec
);
1030 /* MATTHEW: special case: path is really "/" */
1031 if (p
&& !*p
&& *wxFileSpec
== '/')
1035 struct dirent
*nextDir
;
1036 for (nextDir
= readdir(wxDirStream
); nextDir
!= NULL
; nextDir
= readdir(wxDirStream
))
1039 /* MATTHEW: [5] Only return "." and ".." when they match, and only return
1040 directories when flags & wxDIR */
1041 if (wxMatchWild(n
, nextDir
->d_name
)) {
1047 if (strcmp(p
, "/") != 0)
1050 strcat(buf
, nextDir
->d_name
);
1052 if ((strcmp(nextDir
->d_name
, ".") == 0) ||
1053 (strcmp(nextDir
->d_name
, "..") == 0)) {
1054 if (wxFindFileFlags
&& !(wxFindFileFlags
& wxDIR
))
1058 isdir
= wxDirExists(buf
);
1060 if (!wxFindFileFlags
1061 || ((wxFindFileFlags
& wxDIR
) && isdir
)
1062 || ((wxFindFileFlags
& wxFILE
) && !isdir
))
1066 closedir(wxDirStream
);
1074 #elif defined(__WXMSW__)
1077 HANDLE wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1078 WIN32_FIND_DATA wxFileStruc
;
1081 static struct ffblk wxFileStruc
;
1083 static struct _find_t wxFileStruc
;
1086 static wxString wxFileSpec
= "";
1087 static int wxFindFileFlags
;
1089 char *wxFindFirstFile(const char *spec
, int flags
)
1092 wxFindFileFlags
= flags
; /* MATTHEW: [5] Remember flags */
1094 // Find path only so we can concatenate
1095 // found file onto path
1096 wxString
path1(wxFileSpec
);
1097 char *p
= wxPathOnly(WXSTRINGCAST path1
);
1098 if (p
&& (strlen(p
) > 0))
1099 strcpy(wxBuffer
, p
);
1104 if (wxFileStrucHandle
!= INVALID_HANDLE_VALUE
)
1105 FindClose(wxFileStrucHandle
);
1107 wxFileStrucHandle
= ::FindFirstFile(WXSTRINGCAST spec
, &wxFileStruc
);
1109 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1112 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1114 if (isdir
&& !(flags
& wxDIR
))
1115 return wxFindNextFile();
1116 else if (!isdir
&& flags
&& !(flags
& wxFILE
))
1117 return wxFindNextFile();
1119 if (wxBuffer
[0] != 0)
1120 strcat(wxBuffer
, "\\");
1121 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1125 int flag
= _A_NORMAL
;
1126 if (flags
& wxDIR
) /* MATTHEW: [5] Use & */
1130 if (findfirst(WXSTRINGCAST spec
, &wxFileStruc
, flag
) == 0)
1132 if (_dos_findfirst(WXSTRINGCAST spec
, flag
, &wxFileStruc
) == 0)
1135 /* MATTHEW: [5] Check directory flag */
1139 attrib
= wxFileStruc
.ff_attrib
;
1141 attrib
= wxFileStruc
.attrib
;
1144 if (attrib
& _A_SUBDIR
) {
1145 if (!(wxFindFileFlags
& wxDIR
))
1146 return wxFindNextFile();
1147 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1148 return wxFindNextFile();
1150 if (wxBuffer
[0] != 0)
1151 strcat(wxBuffer
, "\\");
1154 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1156 strcat(wxBuffer
, wxFileStruc
.name
);
1165 char *wxFindNextFile(void)
1167 // Find path only so we can concatenate
1168 // found file onto path
1169 wxString
p2(wxFileSpec
);
1170 char *p
= wxPathOnly(WXSTRINGCAST p2
);
1171 if (p
&& (strlen(p
) > 0))
1172 strcpy(wxBuffer
, p
);
1179 if (wxFileStrucHandle
== INVALID_HANDLE_VALUE
)
1182 bool success
= (FindNextFile(wxFileStrucHandle
, &wxFileStruc
) != 0);
1184 FindClose(wxFileStrucHandle
);
1185 wxFileStrucHandle
= INVALID_HANDLE_VALUE
;
1189 bool isdir
= !!(wxFileStruc
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
);
1191 if (isdir
&& !(wxFindFileFlags
& wxDIR
))
1193 else if (!isdir
&& wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1196 if (wxBuffer
[0] != 0)
1197 strcat(wxBuffer
, "\\");
1198 strcat(wxBuffer
, wxFileStruc
.cFileName
);
1203 if (findnext(&wxFileStruc
) == 0)
1205 if (_dos_findnext(&wxFileStruc
) == 0)
1208 /* MATTHEW: [5] Check directory flag */
1212 attrib
= wxFileStruc
.ff_attrib
;
1214 attrib
= wxFileStruc
.attrib
;
1217 if (attrib
& _A_SUBDIR
) {
1218 if (!(wxFindFileFlags
& wxDIR
))
1220 } else if (wxFindFileFlags
&& !(wxFindFileFlags
& wxFILE
))
1224 if (wxBuffer
[0] != 0)
1225 strcat(wxBuffer
, "\\");
1227 strcat(wxBuffer
, wxFileStruc
.ff_name
);
1229 strcat(wxBuffer
, wxFileStruc
.name
);
1241 // Get current working directory.
1242 // If buf is NULL, allocates space using new, else
1244 char *wxGetWorkingDirectory(char *buf
, int sz
)
1247 buf
= new char[sz
+1];
1249 if (_getcwd(buf
, sz
) == NULL
) {
1251 if (getcwd(buf
, sz
) == NULL
) {
1259 bool wxSetWorkingDirectory(const wxString
& d
)
1262 return (chdir(d
) == 0);
1263 #elif defined(__WXMSW__)
1266 return (bool)(SetCurrentDirectory(d
) != 0);
1268 // Must change drive, too.
1269 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1272 char firstChar
= d
[0];
1276 firstChar
= firstChar
- 32;
1278 // To a drive number
1279 unsigned int driveNo
= firstChar
- 64;
1282 unsigned int noDrives
;
1283 _dos_setdrive(driveNo
, &noDrives
);
1286 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1294 bool wxEndsWithPathSeparator(const char *pszFileName
)
1296 size_t len
= Strlen(pszFileName
);
1300 return wxIsPathSeparator(pszFileName
[len
- 1]);
1303 // find a file in a list of directories, returns false if not found
1304 bool wxFindFileInPath(wxString
*pStr
, const char *pszPath
, const char *pszFile
)
1306 // we assume that it's not empty
1307 wxCHECK_MSG( !IsEmpty(pszFile
), FALSE
,
1308 _("empty file name in wxFindFileInPath"));
1310 // skip path separator in the beginning of the file name if present
1311 if ( wxIsPathSeparator(*pszFile
) )
1314 // copy the path (strtok will modify it)
1315 char *szPath
= new char[strlen(pszPath
) + 1];
1316 strcpy(szPath
, pszPath
);
1320 for ( pc
= strtok(szPath
, PATH_SEP
); pc
; pc
= strtok(NULL
, PATH_SEP
) ) {
1321 // search for the file in this directory
1323 if ( !wxEndsWithPathSeparator(pc
) )
1324 strFile
+= FILE_SEP_PATH
;
1327 if ( FileExists(strFile
) ) {
1335 return pc
!= NULL
; // if true => we breaked from the loop
1338 void WXDLLEXPORT
wxSplitPath(const char *pszFileName
,
1343 wxCHECK_RET( pszFileName
, _("NULL file name in wxSplitPath") );
1345 const char *pDot
= strrchr(pszFileName
, FILE_SEP_EXT
);
1346 const char *pSepUnix
= strrchr(pszFileName
, FILE_SEP_PATH_UNIX
);
1347 const char *pSepDos
= strrchr(pszFileName
, FILE_SEP_PATH_DOS
);
1349 // take the last of the two
1350 uint nPosUnix
= pSepUnix
? pSepUnix
- pszFileName
: 0;
1351 uint nPosDos
= pSepDos
? pSepDos
- pszFileName
: 0;
1352 if ( nPosDos
> nPosUnix
)
1354 // uint nLen = Strlen(pszFileName);
1357 *pstrPath
= wxString(pszFileName
, nPosUnix
);
1359 uint nPosDot
= pDot
- pszFileName
;
1361 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1, nPosDot
- nPosUnix
);
1363 *pstrExt
= wxString(pszFileName
+ nPosDot
+ 1);
1367 *pstrName
= wxString(pszFileName
+ nPosUnix
+ 1);