1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/filefn.cpp
3 // Purpose: File- and directory-related functions
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/filefn.h"
36 #include "wx/filename.h"
39 #include "wx/tokenzr.h"
41 // there are just too many of those...
43 #pragma warning(disable:4706) // assignment within conditional expression
50 #if !wxONLY_WATCOM_EARLIER_THAN(1,4)
51 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
56 #if defined(__WXMAC__)
57 #include "wx/mac/private.h" // includes mac headers
61 #include "wx/msw/private.h"
62 #include "wx/msw/mslu.h"
64 // sys/cygwin.h is needed for cygwin_conv_to_full_win32_path()
66 // note that it must be included after <windows.h>
69 #include <sys/cygwin.h>
71 #endif // __GNUWIN32__
73 // io.h is needed for _get_osfhandle()
74 // Already included by filefn.h for many Windows compilers
75 #if defined __MWERKS__ || defined __CYGWIN__
84 // TODO: Borland probably has _wgetcwd as well?
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
94 #define _MAXPATHLEN 1024
98 # include "MoreFilesX.h"
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 // MT-FIXME: get rid of this horror and all code using it
106 static wxChar wxFileFunctionsBuffer
[4*_MAXPATHLEN
];
108 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
110 // VisualAge C++ V4.0 cannot have any external linkage const decs
111 // in headers included by more than one primary source
113 const int wxInvalidOffset
= -1;
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 // translate the filenames before passing them to OS functions
121 #define OS_FILENAME(s) (s.fn_str())
123 // ============================================================================
125 // ============================================================================
127 // ----------------------------------------------------------------------------
128 // wrappers around standard POSIX functions
129 // ----------------------------------------------------------------------------
131 #ifdef wxNEED_WX_UNISTD_H
133 WXDLLEXPORT
int wxStat( const wxChar
*file_name
, wxStructStat
*buf
)
135 return stat( wxConvFile
.cWX2MB( file_name
), buf
);
138 WXDLLEXPORT
int wxLstat( const wxChar
*file_name
, wxStructStat
*buf
)
140 return lstat( wxConvFile
.cWX2MB( file_name
), buf
);
143 WXDLLEXPORT
int wxAccess( const wxChar
*pathname
, int mode
)
145 return access( wxConvFile
.cWX2MB( pathname
), mode
);
148 WXDLLEXPORT
int wxOpen( const wxChar
*pathname
, int flags
, mode_t mode
)
150 return open( wxConvFile
.cWX2MB( pathname
), flags
, mode
);
153 #endif // wxNEED_WX_UNISTD_H
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 void wxPathList::Add(const wxString
& path
)
161 // add a path separator to force wxFileName to interpret it always as a directory
162 // (i.e. if we are called with '/home/user' we want to consider it a folder and
163 // not, as wxFileName would consider, a filename).
164 wxFileName
fn(path
+ wxFileName::GetPathSeparator());
166 // add only normalized relative/absolute paths
167 fn
.Normalize(wxPATH_NORM_DOTS
|wxPATH_NORM_TILDE
|wxPATH_NORM_LONG
|wxPATH_NORM_ENV_VARS
);
169 wxString toadd
= fn
.GetPath();
170 if (Index(toadd
) == wxNOT_FOUND
)
171 wxArrayString::Add(toadd
); // do not add duplicates
174 void wxPathList::Add(const wxArrayString
&arr
)
176 for (size_t j
=0; j
< arr
.GetCount(); j
++)
180 // Add paths e.g. from the PATH environment variable
181 void wxPathList::AddEnvList (const wxString
& WXUNUSED_IN_WINCE(envVariable
))
183 // No environment variables on WinCE
186 // The space has been removed from the tokenizers, otherwise a
187 // path such as "C:\Program Files" would be split into 2 paths:
188 // "C:\Program" and "Files"; this is true for both Windows and Unix.
190 static const wxChar PATH_TOKS
[] =
191 #if defined(__WINDOWS__) || defined(__OS2__)
192 wxT(";"); // Don't separate with colon in DOS (used for drive)
198 if ( wxGetEnv(envVariable
, &val
) )
200 // split into an array of string the value of the env var
201 wxArrayString arr
= wxStringTokenize(val
, PATH_TOKS
);
202 WX_APPEND_ARRAY(*this, arr
);
204 #endif // !__WXWINCE__
207 // Given a full filename (with path), ensure that that file can
208 // be accessed again USING FILENAME ONLY by adding the path
209 // to the list if not already there.
210 void wxPathList::EnsureFileAccessible (const wxString
& path
)
212 wxString
path_only(wxPathOnly(path
));
213 if ( !path_only
.empty() )
215 if ( Index(path_only
) == wxNOT_FOUND
)
221 bool wxPathList::Member (const wxString
& path
) const
223 return Index(path
) != wxNOT_FOUND
;
226 wxString
wxPathList::FindValidPath (const wxString
& file
) const
228 // normalize the given string as it could be a path + a filename
229 // and not only a filename
233 // NB: normalize without making absolute !
234 fn
.Normalize(wxPATH_NORM_DOTS
|wxPATH_NORM_TILDE
|wxPATH_NORM_LONG
|wxPATH_NORM_ENV_VARS
);
236 wxASSERT_MSG(!fn
.IsDir(), wxT("Cannot search for directories; only for files"));
238 strend
= fn
.GetFullName(); // search for the file name and ignore the path part
240 strend
= fn
.GetFullPath();
242 for (size_t i
=0; i
<GetCount(); i
++)
244 wxString strstart
= Item(i
);
245 if (!strstart
.IsEmpty() && strstart
.Last() != wxFileName::GetPathSeparator())
246 strstart
+= wxFileName::GetPathSeparator();
248 if (wxFileExists(strstart
+ strend
))
249 return strstart
+ strend
; // Found!
252 return wxEmptyString
; // Not found
255 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
) const
257 wxString f
= FindValidPath(file
);
258 if ( f
.empty() || wxIsAbsolutePath(f
) )
261 wxString buf
= ::wxGetCwd();
263 if ( !wxEndsWithPathSeparator(buf
) )
265 buf
+= wxFILE_SEP_PATH
;
272 // ----------------------------------------------------------------------------
273 // miscellaneous global functions (TOFIX!)
274 // ----------------------------------------------------------------------------
276 static inline wxChar
* MYcopystring(const wxString
& s
)
278 wxChar
* copy
= new wxChar
[s
.length() + 1];
279 return wxStrcpy(copy
, s
.c_str());
282 static inline wxChar
* MYcopystring(const wxChar
* s
)
284 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
285 return wxStrcpy(copy
, s
);
290 wxFileExists (const wxString
& filename
)
292 #if defined(__WXPALMOS__)
294 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
295 // we must use GetFileAttributes() instead of the ANSI C functions because
296 // it can cope with network (UNC) paths unlike them
297 DWORD ret
= ::GetFileAttributes(filename
);
299 return (ret
!= (DWORD
)-1) && !(ret
& FILE_ATTRIBUTE_DIRECTORY
);
302 #define S_ISREG(mode) ((mode) & S_IFREG)
305 #ifndef wxNEED_WX_UNISTD_H
306 return (wxStat( filename
.fn_str() , &st
) == 0 && S_ISREG(st
.st_mode
))
308 || (errno
== EACCES
) // if access is denied something with that name
309 // exists and is opened in exclusive mode.
313 return wxStat( filename
, &st
) == 0 && S_ISREG(st
.st_mode
);
315 #endif // __WIN32__/!__WIN32__
319 wxIsAbsolutePath (const wxString
& filename
)
321 if (!filename
.empty())
323 #if defined(__WXMAC__) && !defined(__DARWIN__)
324 // Classic or Carbon CodeWarrior like
325 // Carbon with Apple DevTools is Unix like
327 // This seems wrong to me, but there is no fix. since
328 // "MacOS:MyText.txt" is absolute whereas "MyDir:MyText.txt"
329 // is not. Or maybe ":MyDir:MyText.txt" has to be used? RR.
330 if (filename
.Find(':') != wxNOT_FOUND
&& filename
[0] != ':')
333 // Unix like or Windows
334 if (filename
[0] == wxT('/'))
338 if ((filename
[0] == wxT('[') && filename
[1] != wxT('.')))
341 #if defined(__WINDOWS__) || defined(__OS2__)
343 if (filename
[0] == wxT('\\') || (wxIsalpha (filename
[0]) && filename
[1] == wxT(':')))
351 * Strip off any extension (dot something) from end of file,
352 * IF one exists. Inserts zero into buffer.
356 void wxStripExtension(wxChar
*buffer
)
358 int len
= wxStrlen(buffer
);
362 if (buffer
[i
] == wxT('.'))
371 void wxStripExtension(wxString
& buffer
)
373 //RN: Be careful about the handling the case where
374 //buffer.length() == 0
375 for(size_t i
= buffer
.length() - 1; i
!= wxString::npos
; --i
)
377 if (buffer
.GetChar(i
) == wxT('.'))
379 buffer
= buffer
.Left(i
);
385 // Destructive removal of /./ and /../ stuff
386 wxChar
*wxRealPath (wxChar
*path
)
389 static const wxChar SEP
= wxT('\\');
390 wxUnix2DosFilename(path
);
392 static const wxChar SEP
= wxT('/');
394 if (path
[0] && path
[1]) {
395 /* MATTHEW: special case "/./x" */
397 if (path
[2] == SEP
&& path
[1] == wxT('.'))
405 if (p
[1] == wxT('.') && p
[2] == wxT('.') && (p
[3] == SEP
|| p
[3] == wxT('\0')))
408 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--)
413 if (q
[0] == SEP
&& (q
[1] != wxT('.') || q
[2] != wxT('.') || q
[3] != SEP
)
414 && (q
- 1 <= path
|| q
[-1] != SEP
))
417 if (path
[0] == wxT('\0'))
422 #if defined(__WXMSW__) || defined(__OS2__)
423 /* Check that path[2] is NULL! */
424 else if (path
[1] == wxT(':') && !path
[2])
433 else if (p
[1] == wxT('.') && (p
[2] == SEP
|| p
[2] == wxT('\0')))
441 wxString
wxRealPath(const wxString
& path
)
443 wxChar
*buf1
=MYcopystring(path
);
444 wxChar
*buf2
=wxRealPath(buf1
);
452 wxChar
*wxCopyAbsolutePath(const wxString
& filename
)
454 if (filename
.empty())
455 return (wxChar
*) NULL
;
457 if (! wxIsAbsolutePath(wxExpandPath(wxFileFunctionsBuffer
, filename
)))
459 wxString buf
= ::wxGetCwd();
460 wxChar ch
= buf
.Last();
462 if (ch
!= wxT('\\') && ch
!= wxT('/'))
468 buf
<< wxFileFunctionsBuffer
;
469 buf
= wxRealPath( buf
);
470 return MYcopystring( buf
);
472 return MYcopystring( wxFileFunctionsBuffer
);
478 ~user/ => user's home dir
479 If the environment variable a = "foo" and b = "bar" then:
496 /* input name in name, pathname output to buf. */
498 wxChar
*wxExpandPath(wxChar
*buf
, const wxChar
*name
)
500 register wxChar
*d
, *s
, *nm
;
501 wxChar lnm
[_MAXPATHLEN
];
504 // Some compilers don't like this line.
505 // const wxChar trimchars[] = wxT("\n \t");
508 trimchars
[0] = wxT('\n');
509 trimchars
[1] = wxT(' ');
510 trimchars
[2] = wxT('\t');
514 const wxChar SEP
= wxT('\\');
516 const wxChar SEP
= wxT('/');
519 if (name
== NULL
|| *name
== wxT('\0'))
521 nm
= MYcopystring(name
); // Make a scratch copy
524 /* Skip leading whitespace and cr */
525 while (wxStrchr((wxChar
*)trimchars
, *nm
) != NULL
)
527 /* And strip off trailing whitespace and cr */
528 s
= nm
+ (q
= wxStrlen(nm
)) - 1;
529 while (q
-- && wxStrchr((wxChar
*)trimchars
, *s
) != NULL
)
537 q
= nm
[0] == wxT('\\') && nm
[1] == wxT('~');
540 /* Expand inline environment variables */
558 while ((*d
++ = *s
) != 0) {
560 if (*s
== wxT('\\')) {
561 if ((*(d
- 1) = *++s
)!=0) {
569 // No env variables on WinCE
572 if (*s
++ == wxT('$') && (*s
== wxT('{') || *s
== wxT(')')))
574 if (*s
++ == wxT('$'))
577 register wxChar
*start
= d
;
578 register int braces
= (*s
== wxT('{') || *s
== wxT('('));
579 register wxChar
*value
;
580 while ((*d
++ = *s
) != 0)
581 if (braces
? (*s
== wxT('}') || *s
== wxT(')')) : !(wxIsalnum(*s
) || *s
== wxT('_')) )
586 value
= wxGetenv(braces
? start
+ 1 : start
);
588 for ((d
= start
- 1); (*d
++ = *value
++) != 0;)
602 /* Expand ~ and ~user */
604 if (nm
[0] == wxT('~') && !q
)
607 if (nm
[1] == SEP
|| nm
[1] == 0)
609 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
610 if ((s
= WXSTRINGCAST
wxGetUserHome(wxEmptyString
)) != NULL
) {
615 { /* ~user/filename */
616 register wxChar
*nnm
;
617 register wxChar
*home
;
618 for (s
= nm
; *s
&& *s
!= SEP
; s
++)
622 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
623 was_sep
= (*s
== SEP
);
624 nnm
= *s
? s
+ 1 : s
;
626 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
627 if ((home
= WXSTRINGCAST
wxGetUserHome(wxString(nm
+ 1))) == NULL
)
629 if (was_sep
) /* replace only if it was there: */
642 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
644 while (wxT('\0') != (*d
++ = *s
++))
647 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
651 while ((*d
++ = *s
++) != 0)
655 delete[] nm_tmp
; // clean up alloc
656 /* Now clean up the buffer */
657 return wxRealPath(buf
);
660 /* Contract Paths to be build upon an environment variable
663 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
665 The call wxExpandPath can convert these back!
668 wxContractPath (const wxString
& filename
,
669 const wxString
& WXUNUSED_IN_WINCE(envname
),
670 const wxString
& user
)
672 static wxChar dest
[_MAXPATHLEN
];
674 if (filename
.empty())
675 return (wxChar
*) NULL
;
677 wxStrcpy (dest
, WXSTRINGCAST filename
);
679 wxUnix2DosFilename(dest
);
682 // Handle environment
686 if (!envname
.empty() && (val
= wxGetenv (WXSTRINGCAST envname
)) != NULL
&&
687 (tcp
= wxStrstr (dest
, val
)) != NULL
)
689 wxStrcpy (wxFileFunctionsBuffer
, tcp
+ wxStrlen (val
));
692 wxStrcpy (tcp
, WXSTRINGCAST envname
);
693 wxStrcat (tcp
, wxT("}"));
694 wxStrcat (tcp
, wxFileFunctionsBuffer
);
698 // Handle User's home (ignore root homes!)
699 val
= wxGetUserHome (user
);
703 const size_t len
= wxStrlen(val
);
707 if (wxStrncmp(dest
, val
, len
) == 0)
709 wxStrcpy(wxFileFunctionsBuffer
, wxT("~"));
711 wxStrcat(wxFileFunctionsBuffer
, (const wxChar
*) user
);
712 wxStrcat(wxFileFunctionsBuffer
, dest
+ len
);
713 wxStrcpy (dest
, wxFileFunctionsBuffer
);
719 // Return just the filename, not the path (basename)
720 wxChar
*wxFileNameFromPath (wxChar
*path
)
723 wxString n
= wxFileNameFromPath(p
);
725 return path
+ p
.length() - n
.length();
728 wxString
wxFileNameFromPath (const wxString
& path
)
731 wxFileName::SplitPath(path
, NULL
, &name
, &ext
);
733 wxString fullname
= name
;
736 fullname
<< wxFILE_SEP_EXT
<< ext
;
742 // Return just the directory, or NULL if no directory
744 wxPathOnly (wxChar
*path
)
748 static wxChar buf
[_MAXPATHLEN
];
751 wxStrcpy (buf
, path
);
753 int l
= wxStrlen(path
);
756 // Search backward for a backward or forward slash
759 #if defined(__WXMAC__) && !defined(__DARWIN__)
760 // Classic or Carbon CodeWarrior like
761 // Carbon with Apple DevTools is Unix like
762 if (path
[i
] == wxT(':') )
768 // Unix like or Windows
769 if (path
[i
] == wxT('/') || path
[i
] == wxT('\\'))
776 if (path
[i
] == wxT(']'))
785 #if defined(__WXMSW__) || defined(__OS2__)
786 // Try Drive specifier
787 if (wxIsalpha (buf
[0]) && buf
[1] == wxT(':'))
789 // A:junk --> A:. (since A:.\junk Not A:\junk)
796 return (wxChar
*) NULL
;
799 // Return just the directory, or NULL if no directory
800 wxString
wxPathOnly (const wxString
& path
)
804 wxChar buf
[_MAXPATHLEN
];
807 wxStrcpy (buf
, WXSTRINGCAST path
);
809 int l
= path
.length();
812 // Search backward for a backward or forward slash
815 #if defined(__WXMAC__) && !defined(__DARWIN__)
816 // Classic or Carbon CodeWarrior like
817 // Carbon with Apple DevTools is Unix like
818 if (path
[i
] == wxT(':') )
821 return wxString(buf
);
824 // Unix like or Windows
825 if (path
[i
] == wxT('/') || path
[i
] == wxT('\\'))
827 // Don't return an empty string
831 return wxString(buf
);
835 if (path
[i
] == wxT(']'))
838 return wxString(buf
);
844 #if defined(__WXMSW__) || defined(__OS2__)
845 // Try Drive specifier
846 if (wxIsalpha (buf
[0]) && buf
[1] == wxT(':'))
848 // A:junk --> A:. (since A:.\junk Not A:\junk)
851 return wxString(buf
);
855 return wxEmptyString
;
858 // Utility for converting delimiters in DOS filenames to UNIX style
859 // and back again - or we get nasty problems with delimiters.
860 // Also, convert to lower case, since case is significant in UNIX.
862 #if defined(__WXMAC__)
864 #if TARGET_API_MAC_OSX
865 #define kDefaultPathStyle kCFURLPOSIXPathStyle
867 #define kDefaultPathStyle kCFURLHFSPathStyle
870 wxString
wxMacFSRefToPath( const FSRef
*fsRef
, CFStringRef additionalPathComponent
)
873 fullURLRef
= CFURLCreateFromFSRef(NULL
, fsRef
);
874 if ( additionalPathComponent
)
876 CFURLRef parentURLRef
= fullURLRef
;
877 fullURLRef
= CFURLCreateCopyAppendingPathComponent(NULL
, parentURLRef
,
878 additionalPathComponent
,false);
879 CFRelease( parentURLRef
) ;
881 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, kDefaultPathStyle
);
882 CFRelease( fullURLRef
) ;
883 CFMutableStringRef cfMutableString
= CFStringCreateMutableCopy(NULL
, 0, cfString
);
884 CFRelease( cfString
);
885 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormC
);
886 return wxMacCFStringHolder(cfMutableString
).AsString();
889 OSStatus
wxMacPathToFSRef( const wxString
&path
, FSRef
*fsRef
)
891 OSStatus err
= noErr
;
892 CFMutableStringRef cfMutableString
= CFStringCreateMutableCopy(NULL
, 0, wxMacCFStringHolder(path
));
893 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
894 CFURLRef url
= CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kDefaultPathStyle
, false);
895 CFRelease( cfMutableString
);
898 if ( CFURLGetFSRef(url
, fsRef
) == false )
909 wxString
wxMacHFSUniStrToString( ConstHFSUniStr255Param uniname
)
911 CFStringRef cfname
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
914 CFMutableStringRef cfMutableString
= CFStringCreateMutableCopy(NULL
, 0, cfname
);
916 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormC
);
917 return wxMacCFStringHolder(cfMutableString
).AsString() ;
922 wxString
wxMacFSSpec2MacFilename( const FSSpec
*spec
)
925 if ( FSpMakeFSRef( spec
, &fsRef
) == noErr
)
927 return wxMacFSRefToPath( &fsRef
) ;
929 return wxEmptyString
;
932 void wxMacFilename2FSSpec( const wxString
& path
, FSSpec
*spec
)
934 OSStatus err
= noErr
;
936 wxMacPathToFSRef( path
, &fsRef
) ;
937 err
= FSRefMakeFSSpec( &fsRef
, spec
) ;
944 wxDos2UnixFilename (wxChar
*s
)
953 *s
= (wxChar
)wxTolower (*s
); // Case INDEPENDENT
960 #if defined(__WXMSW__) || defined(__OS2__)
961 wxUnix2DosFilename (wxChar
*s
)
963 wxUnix2DosFilename (wxChar
*WXUNUSED(s
) )
966 // Yes, I really mean this to happen under DOS only! JACS
967 #if defined(__WXMSW__) || defined(__OS2__)
978 // Concatenate two files to form third
980 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
984 wxFile
in1(file1
), in2(file2
);
985 wxTempFile
out(file3
);
987 if ( !in1
.IsOpened() || !in2
.IsOpened() || !out
.IsOpened() )
991 unsigned char buf
[1024];
993 for( int i
=0; i
<2; i
++)
995 wxFile
*in
= i
==0 ? &in1
: &in2
;
997 if ( (ofs
= in
->Read(buf
,WXSIZEOF(buf
))) == wxInvalidOffset
) return false;
999 if ( !out
.Write(buf
,ofs
) )
1001 } while ( ofs
== (ssize_t
)WXSIZEOF(buf
) );
1004 return out
.Commit();
1018 wxCopyFile (const wxString
& file1
, const wxString
& file2
, bool overwrite
)
1020 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1021 // CopyFile() copies file attributes and modification time too, so use it
1022 // instead of our code if available
1024 // NB: 3rd parameter is bFailIfExists i.e. the inverse of overwrite
1025 if ( !::CopyFile(file1
, file2
, !overwrite
) )
1027 wxLogSysError(_("Failed to copy the file '%s' to '%s'"),
1028 file1
.c_str(), file2
.c_str());
1032 #elif defined(__OS2__)
1033 if ( ::DosCopy((PSZ
)file1
.c_str(), (PSZ
)file2
.c_str(), overwrite
? DCPY_EXISTING
: 0) != 0 )
1035 #elif defined(__PALMOS__)
1036 // TODO with http://www.palmos.com/dev/support/docs/protein_books/Memory_Databases_Files/
1038 #elif wxUSE_FILE // !Win32
1041 // get permissions of file1
1042 if ( wxStat( file1
.c_str(), &fbuf
) != 0 )
1044 // the file probably doesn't exist or we haven't the rights to read
1046 wxLogSysError(_("Impossible to get permissions for file '%s'"),
1051 // open file1 for reading
1052 wxFile
fileIn(file1
, wxFile::read
);
1053 if ( !fileIn
.IsOpened() )
1056 // remove file2, if it exists. This is needed for creating
1057 // file2 with the correct permissions in the next step
1058 if ( wxFileExists(file2
) && (!overwrite
|| !wxRemoveFile(file2
)))
1060 wxLogSysError(_("Impossible to overwrite the file '%s'"),
1065 // reset the umask as we want to create the file with exactly the same
1066 // permissions as the original one
1069 // create file2 with the same permissions than file1 and open it for
1073 if ( !fileOut
.Create(file2
, overwrite
, fbuf
.st_mode
& 0777) )
1076 // copy contents of file1 to file2
1081 count
= fileIn
.Read(buf
, WXSIZEOF(buf
));
1082 if ( fileIn
.Error() )
1089 if ( fileOut
.Write(buf
, count
) < count
)
1093 // we can expect fileIn to be closed successfully, but we should ensure
1094 // that fileOut was closed as some write errors (disk full) might not be
1095 // detected before doing this
1096 if ( !fileIn
.Close() || !fileOut
.Close() )
1099 #if !defined(__VISAGECPP__) && !defined(__WXMAC__) || defined(__UNIX__)
1100 // no chmod in VA. Should be some permission API for HPFS386 partitions
1102 if ( chmod(OS_FILENAME(file2
), fbuf
.st_mode
) != 0 )
1104 wxLogSysError(_("Impossible to set permissions for the file '%s'"),
1108 #endif // OS/2 || Mac
1110 #else // !Win32 && ! wxUSE_FILE
1112 // impossible to simulate with wxWidgets API
1115 wxUnusedVar(overwrite
);
1118 #endif // __WXMSW__ && __WIN32__
1124 wxRenameFile(const wxString
& file1
, const wxString
& file2
, bool overwrite
)
1126 if ( !overwrite
&& wxFileExists(file2
) )
1130 _("Failed to rename the file '%s' to '%s' because the destination file already exists."),
1131 file1
.c_str(), file2
.c_str()
1137 #if !defined(__WXWINCE__) && !defined(__WXPALMOS__)
1138 // Normal system call
1139 if ( wxRename (file1
, file2
) == 0 )
1144 if (wxCopyFile(file1
, file2
, overwrite
)) {
1145 wxRemoveFile(file1
);
1152 bool wxRemoveFile(const wxString
& file
)
1154 #if defined(__VISUALC__) \
1155 || defined(__BORLANDC__) \
1156 || defined(__WATCOMC__) \
1157 || defined(__DMC__) \
1158 || defined(__GNUWIN32__) \
1159 || (defined(__MWERKS__) && defined(__MSL__))
1160 int res
= wxRemove(file
);
1161 #elif defined(__WXMAC__)
1162 int res
= unlink(wxFNCONV(file
));
1163 #elif defined(__WXPALMOS__)
1165 // TODO with VFSFileDelete()
1167 int res
= unlink(OS_FILENAME(file
));
1173 bool wxMkdir(const wxString
& dir
, int perm
)
1175 #if defined(__WXPALMOS__)
1177 #elif defined(__WXMAC__) && !defined(__UNIX__)
1178 return (mkdir( wxFNCONV(dir
) , 0 ) == 0);
1180 const wxChar
*dirname
= dir
.c_str();
1182 // assume mkdir() has 2 args on non Windows-OS/2 platforms and on Windows too
1183 // for the GNU compiler
1184 #if (!(defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__))) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) || defined(__WINE__) || defined(__WXMICROWIN__)
1187 if ( mkdir(wxFNCONV(dirname
)) != 0 )
1189 if ( mkdir(wxFNCONV(dirname
), perm
) != 0 )
1191 #elif defined(__OS2__)
1193 if (::DosCreateDir((PSZ
)dirname
, NULL
) != 0) // enhance for EAB's??
1194 #elif defined(__DOS__)
1195 #if defined(__WATCOMC__)
1197 if ( wxMkDir(wxFNSTRINGCAST
wxFNCONV(dirname
)) != 0 )
1198 #elif defined(__DJGPP__)
1199 if ( mkdir(wxFNCONV(dirname
), perm
) != 0 )
1201 #error "Unsupported DOS compiler!"
1203 #else // !MSW, !DOS and !OS/2 VAC++
1206 if ( !CreateDirectory(dirname
, NULL
) )
1208 if ( wxMkDir(dir
.fn_str()) != 0 )
1212 wxLogSysError(_("Directory '%s' couldn't be created"), dirname
);
1221 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
1223 #if defined(__VMS__)
1224 return false; //to be changed since rmdir exists in VMS7.x
1225 #elif defined(__OS2__)
1226 return (::DosDeleteDir((PSZ
)dir
.c_str()) == 0);
1227 #elif defined(__WXWINCE__)
1228 return (CreateDirectory(dir
, NULL
) != 0);
1229 #elif defined(__WXPALMOS__)
1230 // TODO with VFSFileRename()
1233 return (wxRmDir(OS_FILENAME(dir
)) == 0);
1237 // does the path exists? (may have or not '/' or '\\' at the end)
1238 bool wxDirExists(const wxChar
*pszPathName
)
1240 wxString
strPath(pszPathName
);
1242 #if defined(__WINDOWS__) || defined(__OS2__)
1243 // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists,
1244 // so remove all trailing backslashes from the path - but don't do this for
1245 // the paths "d:\" (which are different from "d:") nor for just "\"
1246 while ( wxEndsWithPathSeparator(strPath
) )
1248 size_t len
= strPath
.length();
1249 if ( len
== 1 || (len
== 3 && strPath
[len
- 2] == _T(':')) )
1252 strPath
.Truncate(len
- 1);
1254 #endif // __WINDOWS__
1257 // OS/2 can't handle "d:", it wants either "d:\" or "d:."
1258 if (strPath
.length() == 2 && strPath
[1u] == _T(':'))
1262 #if defined(__WXPALMOS__)
1264 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
1265 // stat() can't cope with network paths
1266 DWORD ret
= ::GetFileAttributes(strPath
);
1268 return (ret
!= (DWORD
)-1) && (ret
& FILE_ATTRIBUTE_DIRECTORY
);
1269 #elif defined(__OS2__)
1270 FILESTATUS3 Info
= {{0}};
1271 APIRET rc
= ::DosQueryPathInfo((PSZ
)(WXSTRINGCAST strPath
), FIL_STANDARD
,
1272 (void*) &Info
, sizeof(FILESTATUS3
));
1274 return ((rc
== NO_ERROR
) && (Info
.attrFile
& FILE_DIRECTORY
)) ||
1275 (rc
== ERROR_SHARING_VIOLATION
);
1276 // If we got a sharing violation, there must be something with this name.
1280 #ifndef __VISAGECPP__
1281 return wxStat(strPath
.c_str(), &st
) == 0 && ((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1283 // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only
1284 return wxStat(pszPathName
, &st
) == 0 && (st
.st_mode
== S_IFDIR
);
1287 #endif // __WIN32__/!__WIN32__
1290 // Get a temporary filename, opening and closing the file.
1291 wxChar
*wxGetTempFileName(const wxString
& prefix
, wxChar
*buf
)
1294 if ( !wxGetTempFileName(prefix
, filename
) )
1298 wxStrcpy(buf
, filename
);
1300 buf
= MYcopystring(filename
);
1305 bool wxGetTempFileName(const wxString
& prefix
, wxString
& buf
)
1308 buf
= wxFileName::CreateTempFileName(prefix
);
1310 return !buf
.empty();
1311 #else // !wxUSE_FILE
1312 wxUnusedVar(prefix
);
1316 #endif // wxUSE_FILE/!wxUSE_FILE
1319 // Get first file name matching given wild card.
1321 static wxDir
*gs_dir
= NULL
;
1322 static wxString gs_dirPath
;
1324 wxString
wxFindFirstFile(const wxChar
*spec
, int flags
)
1326 wxSplitPath(spec
, &gs_dirPath
, NULL
, NULL
);
1327 if ( gs_dirPath
.empty() )
1328 gs_dirPath
= wxT(".");
1329 if ( !wxEndsWithPathSeparator(gs_dirPath
) )
1330 gs_dirPath
<< wxFILE_SEP_PATH
;
1334 gs_dir
= new wxDir(gs_dirPath
);
1336 if ( !gs_dir
->IsOpened() )
1338 wxLogSysError(_("Can not enumerate files '%s'"), spec
);
1339 return wxEmptyString
;
1345 case wxDIR
: dirFlags
= wxDIR_DIRS
; break;
1346 case wxFILE
: dirFlags
= wxDIR_FILES
; break;
1347 default: dirFlags
= wxDIR_DIRS
| wxDIR_FILES
; break;
1351 gs_dir
->GetFirst(&result
, wxFileNameFromPath(wxString(spec
)), dirFlags
);
1352 if ( result
.empty() )
1358 return gs_dirPath
+ result
;
1361 wxString
wxFindNextFile()
1363 wxASSERT_MSG( gs_dir
, wxT("You must call wxFindFirstFile before!") );
1366 gs_dir
->GetNext(&result
);
1368 if ( result
.empty() )
1374 return gs_dirPath
+ result
;
1378 // Get current working directory.
1379 // If buf is NULL, allocates space using new, else copies into buf.
1380 // wxGetWorkingDirectory() is obsolete, use wxGetCwd()
1381 // wxDoGetCwd() is their common core to be moved
1382 // to wxGetCwd() once wxGetWorkingDirectory() will be removed.
1383 // Do not expose wxDoGetCwd in headers!
1385 wxChar
*wxDoGetCwd(wxChar
*buf
, int sz
)
1387 #if defined(__WXPALMOS__)
1389 if(buf
&& sz
>0) buf
[0] = _T('\0');
1391 #elif defined(__WXWINCE__)
1393 if(buf
&& sz
>0) buf
[0] = _T('\0');
1398 buf
= new wxChar
[sz
+ 1];
1401 bool ok
wxDUMMY_INITIALIZE(false);
1403 // for the compilers which have Unicode version of _getcwd(), call it
1404 // directly, for the others call the ANSI version and do the translation
1407 #else // wxUSE_UNICODE
1408 bool needsANSI
= true;
1410 #if !defined(HAVE_WGETCWD) || wxUSE_UNICODE_MSLU
1411 char cbuf
[_MAXPATHLEN
];
1415 #if wxUSE_UNICODE_MSLU
1416 if ( wxGetOsVersion() != wxOS_WINDOWS_9X
)
1418 char *cbuf
= NULL
; // never really used because needsANSI will always be false
1421 ok
= _wgetcwd(buf
, sz
) != NULL
;
1427 #endif // wxUSE_UNICODE
1429 #if defined(_MSC_VER) || defined(__MINGW32__)
1430 ok
= _getcwd(cbuf
, sz
) != NULL
;
1431 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1433 if ( getcwd( lbuf
, sizeof( lbuf
) ) )
1435 wxString
res( lbuf
, *wxConvCurrent
) ;
1436 wxStrcpy( buf
, res
) ;
1441 #elif defined(__OS2__)
1443 ULONG ulDriveNum
= 0;
1444 ULONG ulDriveMap
= 0;
1445 rc
= ::DosQueryCurrentDisk(&ulDriveNum
, &ulDriveMap
);
1450 rc
= ::DosQueryCurrentDir( 0 // current drive
1454 cbuf
[0] = char('A' + (ulDriveNum
- 1));
1459 #else // !Win32/VC++ !Mac !OS2
1460 ok
= getcwd(cbuf
, sz
) != NULL
;
1463 #if wxUSE_UNICODE && !(defined(__WXMAC__) && !defined(__DARWIN__))
1464 // finally convert the result to Unicode if needed
1465 wxConvFile
.MB2WC(buf
, cbuf
, sz
);
1466 #endif // wxUSE_UNICODE
1471 wxLogSysError(_("Failed to get the working directory"));
1473 // VZ: the old code used to return "." on error which didn't make any
1474 // sense at all to me - empty string is a better error indicator
1475 // (NULL might be even better but I'm afraid this could lead to
1476 // problems with the old code assuming the return is never NULL)
1479 else // ok, but we might need to massage the path into the right format
1482 // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths
1483 // with / deliminers. We don't like that.
1484 for (wxChar
*ch
= buf
; *ch
; ch
++)
1486 if (*ch
== wxT('/'))
1491 // MBN: we hope that in the case the user is compiling a GTK+/Motif app,
1492 // he needs Unix as opposed to Win32 pathnames
1493 #if defined( __CYGWIN__ ) && defined( __WINDOWS__ )
1494 // another example of DOS/Unix mix (Cygwin)
1495 wxString pathUnix
= buf
;
1497 char bufA
[_MAXPATHLEN
];
1498 cygwin_conv_to_full_win32_path(pathUnix
.mb_str(wxConvFile
), bufA
);
1499 wxConvFile
.MB2WC(buf
, bufA
, sz
);
1501 cygwin_conv_to_full_win32_path(pathUnix
, buf
);
1502 #endif // wxUSE_UNICODE
1503 #endif // __CYGWIN__
1516 #if WXWIN_COMPATIBILITY_2_6
1517 wxChar
*wxGetWorkingDirectory(wxChar
*buf
, int sz
)
1519 return wxDoGetCwd(buf
,sz
);
1521 #endif // WXWIN_COMPATIBILITY_2_6
1526 wxDoGetCwd(wxStringBuffer(str
, _MAXPATHLEN
), _MAXPATHLEN
);
1530 bool wxSetWorkingDirectory(const wxString
& d
)
1532 #if defined(__OS2__)
1533 return (::DosSetCurrentDir((PSZ
)d
.c_str()) == 0);
1534 #elif defined(__UNIX__) || defined(__WXMAC__) || defined(__DOS__)
1535 return (chdir(wxFNSTRINGCAST d
.fn_str()) == 0);
1536 #elif defined(__WINDOWS__)
1540 // No equivalent in WinCE
1544 return (bool)(SetCurrentDirectory(d
) != 0);
1547 // Must change drive, too.
1548 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1551 wxChar firstChar
= d
[0];
1555 firstChar
= firstChar
- 32;
1557 // To a drive number
1558 unsigned int driveNo
= firstChar
- 64;
1561 unsigned int noDrives
;
1562 _dos_setdrive(driveNo
, &noDrives
);
1565 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1573 // Get the OS directory if appropriate (such as the Windows directory).
1574 // On non-Windows platform, probably just return the empty string.
1575 wxString
wxGetOSDirectory()
1578 return wxString(wxT("\\Windows"));
1579 #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
1581 GetWindowsDirectory(buf
, 256);
1582 return wxString(buf
);
1583 #elif defined(__WXMAC__)
1584 return wxMacFindFolder(kOnSystemDisk
, 'macs', false);
1586 return wxEmptyString
;
1590 bool wxEndsWithPathSeparator(const wxChar
*pszFileName
)
1592 size_t len
= wxStrlen(pszFileName
);
1594 return len
&& wxIsPathSeparator(pszFileName
[len
- 1]);
1597 // find a file in a list of directories, returns false if not found
1598 bool wxFindFileInPath(wxString
*pStr
, const wxChar
*pszPath
, const wxChar
*pszFile
)
1600 // we assume that it's not empty
1601 wxCHECK_MSG( !wxIsEmpty(pszFile
), false,
1602 _T("empty file name in wxFindFileInPath"));
1604 // skip path separator in the beginning of the file name if present
1605 if ( wxIsPathSeparator(*pszFile
) )
1608 // copy the path (strtok will modify it)
1609 wxChar
*szPath
= new wxChar
[wxStrlen(pszPath
) + 1];
1610 wxStrcpy(szPath
, pszPath
);
1613 wxChar
*pc
, *save_ptr
;
1614 for ( pc
= wxStrtok(szPath
, wxPATH_SEP
, &save_ptr
);
1616 pc
= wxStrtok((wxChar
*) NULL
, wxPATH_SEP
, &save_ptr
) )
1618 // search for the file in this directory
1620 if ( !wxEndsWithPathSeparator(pc
) )
1621 strFile
+= wxFILE_SEP_PATH
;
1624 if ( wxFileExists(strFile
) ) {
1630 // suppress warning about unused variable save_ptr when wxStrtok() is a
1631 // macro which throws away its third argument
1636 return pc
!= NULL
; // if true => we breaked from the loop
1639 void WXDLLEXPORT
wxSplitPath(const wxChar
*pszFileName
,
1644 // it can be empty, but it shouldn't be NULL
1645 wxCHECK_RET( pszFileName
, wxT("NULL file name in wxSplitPath") );
1647 wxFileName::SplitPath(pszFileName
, pstrPath
, pstrName
, pstrExt
);
1652 time_t WXDLLEXPORT
wxFileModificationTime(const wxString
& filename
)
1655 if ( !wxFileName(filename
).GetTimes(NULL
, &mtime
, NULL
) )
1658 return mtime
.GetTicks();
1661 #endif // wxUSE_DATETIME
1664 // Parses the filterStr, returning the number of filters.
1665 // Returns 0 if none or if there's a problem.
1666 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpeg"
1668 int WXDLLEXPORT
wxParseCommonDialogsFilter(const wxString
& filterStr
,
1669 wxArrayString
& descriptions
,
1670 wxArrayString
& filters
)
1672 descriptions
.Clear();
1675 wxString
str(filterStr
);
1677 wxString description
, filter
;
1679 while( pos
!= wxNOT_FOUND
)
1681 pos
= str
.Find(wxT('|'));
1682 if ( pos
== wxNOT_FOUND
)
1684 // if there are no '|'s at all in the string just take the entire
1685 // string as filter and make description empty for later autocompletion
1686 if ( filters
.IsEmpty() )
1688 descriptions
.Add(wxEmptyString
);
1689 filters
.Add(filterStr
);
1693 wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
1699 description
= str
.Left(pos
);
1700 str
= str
.Mid(pos
+ 1);
1701 pos
= str
.Find(wxT('|'));
1702 if ( pos
== wxNOT_FOUND
)
1708 filter
= str
.Left(pos
);
1709 str
= str
.Mid(pos
+ 1);
1712 descriptions
.Add(description
);
1713 filters
.Add(filter
);
1716 #if defined(__WXMOTIF__)
1717 // split it so there is one wildcard per entry
1718 for( size_t i
= 0 ; i
< descriptions
.GetCount() ; i
++ )
1720 pos
= filters
[i
].Find(wxT(';'));
1721 if (pos
!= wxNOT_FOUND
)
1723 // first split only filters
1724 descriptions
.Insert(descriptions
[i
],i
+1);
1725 filters
.Insert(filters
[i
].Mid(pos
+1),i
+1);
1726 filters
[i
]=filters
[i
].Left(pos
);
1728 // autoreplace new filter in description with pattern:
1729 // C/C++ Files(*.cpp;*.c;*.h)|*.cpp;*.c;*.h
1730 // cause split into:
1731 // C/C++ Files(*.cpp)|*.cpp
1732 // C/C++ Files(*.c;*.h)|*.c;*.h
1733 // and next iteration cause another split into:
1734 // C/C++ Files(*.cpp)|*.cpp
1735 // C/C++ Files(*.c)|*.c
1736 // C/C++ Files(*.h)|*.h
1737 for ( size_t k
=i
;k
<i
+2;k
++ )
1739 pos
= descriptions
[k
].Find(filters
[k
]);
1740 if (pos
!= wxNOT_FOUND
)
1742 wxString before
= descriptions
[k
].Left(pos
);
1743 wxString after
= descriptions
[k
].Mid(pos
+filters
[k
].Len());
1744 pos
= before
.Find(_T('('),true);
1745 if (pos
>before
.Find(_T(')'),true))
1747 before
= before
.Left(pos
+1);
1748 before
<< filters
[k
];
1749 pos
= after
.Find(_T(')'));
1750 int pos1
= after
.Find(_T('('));
1751 if (pos
!= wxNOT_FOUND
&& (pos
<pos1
|| pos1
==wxNOT_FOUND
))
1753 before
<< after
.Mid(pos
);
1754 descriptions
[k
] = before
;
1764 for( size_t j
= 0 ; j
< descriptions
.GetCount() ; j
++ )
1766 if ( descriptions
[j
].empty() && !filters
[j
].empty() )
1768 descriptions
[j
].Printf(_("Files (%s)"), filters
[j
].c_str());
1772 return filters
.GetCount();
1775 #if defined( __WINDOWS__ )
1776 static bool wxCheckWin32Permission(const wxString
& path
, DWORD access
)
1778 // quoting the MSDN: "To obtain a handle to a directory, call the
1779 // CreateFile function with the FILE_FLAG_BACKUP_SEMANTICS flag", but this
1780 // doesn't work under Win9x/ME but then it's not needed there anyhow
1781 bool isdir
= wxDirExists(path
);
1782 if ( isdir
&& wxGetOsVersion() == wxOS_WINDOWS_9X
)
1784 // FAT directories always allow all access, even if they have the
1785 // readonly flag set
1789 HANDLE h
= ::CreateFile
1793 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
1796 isdir
? FILE_FLAG_BACKUP_SEMANTICS
: 0,
1799 if ( h
!= INVALID_HANDLE_VALUE
)
1802 return h
!= INVALID_HANDLE_VALUE
;
1804 #endif // __WINDOWS__
1806 bool wxIsWritable(const wxString
&path
)
1808 #if defined( __UNIX__ ) || defined(__OS2__)
1809 // access() will take in count also symbolic links
1810 return access(wxConvFile
.cWX2MB(path
), W_OK
) == 0;
1811 #elif defined( __WINDOWS__ )
1812 return wxCheckWin32Permission(path
, GENERIC_WRITE
);
1820 bool wxIsReadable(const wxString
&path
)
1822 #if defined( __UNIX__ ) || defined(__OS2__)
1823 // access() will take in count also symbolic links
1824 return access(wxConvFile
.cWX2MB(path
), R_OK
) == 0;
1825 #elif defined( __WINDOWS__ )
1826 return wxCheckWin32Permission(path
, GENERIC_READ
);
1834 bool wxIsExecutable(const wxString
&path
)
1836 #if defined( __UNIX__ ) || defined(__OS2__)
1837 // access() will take in count also symbolic links
1838 return access(wxConvFile
.cWX2MB(path
), X_OK
) == 0;
1839 #elif defined( __WINDOWS__ )
1840 return wxCheckWin32Permission(path
, GENERIC_EXECUTE
);
1848 // Return the type of an open file
1850 // Some file types on some platforms seem seekable but in fact are not.
1851 // The main use of this function is to allow such cases to be detected
1852 // (IsSeekable() is implemented as wxGetFileKind() == wxFILE_KIND_DISK).
1854 // This is important for the archive streams, which benefit greatly from
1855 // being able to seek on a stream, but which will produce corrupt archives
1856 // if they unknowingly seek on a non-seekable stream.
1858 // wxFILE_KIND_DISK is a good catch all return value, since other values
1859 // disable features of the archive streams. Some other value must be returned
1860 // for a file type that appears seekable but isn't.
1863 // * Pipes on Windows
1864 // * Files on VMS with a record format other than StreamLF
1866 wxFileKind
wxGetFileKind(int fd
)
1868 #if defined __WXMSW__ && !defined __WXWINCE__ && defined wxGetOSFHandle
1869 switch (::GetFileType(wxGetOSFHandle(fd
)) & ~FILE_TYPE_REMOTE
)
1871 case FILE_TYPE_CHAR
:
1872 return wxFILE_KIND_TERMINAL
;
1873 case FILE_TYPE_DISK
:
1874 return wxFILE_KIND_DISK
;
1875 case FILE_TYPE_PIPE
:
1876 return wxFILE_KIND_PIPE
;
1879 return wxFILE_KIND_UNKNOWN
;
1881 #elif defined(__UNIX__)
1883 return wxFILE_KIND_TERMINAL
;
1888 if (S_ISFIFO(st
.st_mode
))
1889 return wxFILE_KIND_PIPE
;
1890 if (!S_ISREG(st
.st_mode
))
1891 return wxFILE_KIND_UNKNOWN
;
1893 #if defined(__VMS__)
1894 if (st
.st_fab_rfm
!= FAB$C_STMLF
)
1895 return wxFILE_KIND_UNKNOWN
;
1898 return wxFILE_KIND_DISK
;
1901 #define wxFILEKIND_STUB
1903 return wxFILE_KIND_DISK
;
1907 wxFileKind
wxGetFileKind(FILE *fp
)
1909 // Note: The watcom rtl dll doesn't have fileno (the static lib does).
1910 // Should be fixed in version 1.4.
1911 #if defined(wxFILEKIND_STUB) || wxONLY_WATCOM_EARLIER_THAN(1,4)
1913 return wxFILE_KIND_DISK
;
1914 #elif defined(__WINDOWS__) && !defined(__CYGWIN__) && !defined(__WATCOMC__)
1915 return fp
? wxGetFileKind(_fileno(fp
)) : wxFILE_KIND_UNKNOWN
;
1917 return fp
? wxGetFileKind(fileno(fp
)) : wxFILE_KIND_UNKNOWN
;
1922 //------------------------------------------------------------------------
1923 // wild character routines
1924 //------------------------------------------------------------------------
1926 bool wxIsWild( const wxString
& pattern
)
1928 wxString tmp
= pattern
;
1929 wxChar
*pat
= WXSTRINGCAST(tmp
);
1934 case wxT('?'): case wxT('*'): case wxT('['): case wxT('{'):
1945 * Written By Douglas A. Lewis <dalewis@cs.Buffalo.EDU>
1947 * The match procedure is public domain code (from ircII's reg.c)
1948 * but modified to suit our tastes (RN: No "%" syntax I guess)
1951 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1955 /* Match if both are empty. */
1959 const wxChar
*m
= pat
.c_str(),
1967 if (dot_special
&& (*n
== wxT('.')))
1969 /* Never match so that hidden Unix files
1970 * are never found. */
1983 else if (*m
== wxT('?'))
1991 if (*m
== wxT('\\'))
1994 /* Quoting "nothing" is a bad thing */
2001 * If we are out of both strings or we just
2002 * saw a wildcard, then we can say we have a
2013 * We could check for *n == NULL at this point, but
2014 * since it's more common to have a character there,
2015 * check to see if they match first (m and n) and
2016 * then if they don't match, THEN we can check for
2032 * If there are no more characters in the
2033 * string, but we still need to find another
2034 * character (*m != NULL), then it will be
2035 * impossible to match it
2054 #pragma warning(default:4706) // assignment within conditional expression