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"
33 #include "wx/file.h" // This does include filefn.h
34 #include "wx/filename.h"
37 #include "wx/tokenzr.h"
39 // there are just too many of those...
41 #pragma warning(disable:4706) // assignment within conditional expression
48 #if !wxONLY_WATCOM_EARLIER_THAN(1,4)
49 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
54 #if defined(__WXMAC__)
55 #include "wx/mac/private.h" // includes mac headers
59 #include "wx/msw/private.h"
60 #include "wx/msw/mslu.h"
62 // sys/cygwin.h is needed for cygwin_conv_to_full_win32_path()
64 // note that it must be included after <windows.h>
67 #include <sys/cygwin.h>
69 #endif // __GNUWIN32__
71 // io.h is needed for _get_osfhandle()
72 // Already included by filefn.h for many Windows compilers
73 #if defined __MWERKS__ || defined __CYGWIN__
82 // TODO: Borland probably has _wgetcwd as well?
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
92 #define _MAXPATHLEN 1024
96 # include "MoreFilesX.h"
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 // MT-FIXME: get rid of this horror and all code using it
104 static wxChar wxFileFunctionsBuffer
[4*_MAXPATHLEN
];
106 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
108 // VisualAge C++ V4.0 cannot have any external linkage const decs
109 // in headers included by more than one primary source
111 const int wxInvalidOffset
= -1;
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 // translate the filenames before passing them to OS functions
119 #define OS_FILENAME(s) (s.fn_str())
121 // ============================================================================
123 // ============================================================================
125 // ----------------------------------------------------------------------------
126 // wrappers around standard POSIX functions
127 // ----------------------------------------------------------------------------
129 #ifdef wxNEED_WX_UNISTD_H
131 WXDLLEXPORT
int wxStat( const wxChar
*file_name
, wxStructStat
*buf
)
133 return stat( wxConvFile
.cWX2MB( file_name
), buf
);
136 WXDLLEXPORT
int wxLstat( const wxChar
*file_name
, wxStructStat
*buf
)
138 return lstat( wxConvFile
.cWX2MB( file_name
), buf
);
141 WXDLLEXPORT
int wxAccess( const wxChar
*pathname
, int mode
)
143 return access( wxConvFile
.cWX2MB( pathname
), mode
);
146 WXDLLEXPORT
int wxOpen( const wxChar
*pathname
, int flags
, mode_t mode
)
148 return open( wxConvFile
.cWX2MB( pathname
), flags
, mode
);
151 #endif // wxNEED_WX_UNISTD_H
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 void wxPathList::Add(const wxString
& path
)
159 // add a path separator to force wxFileName to interpret it always as a directory
160 // (i.e. if we are called with '/home/user' we want to consider it a folder and
161 // not, as wxFileName would consider, a filename).
162 wxFileName
fn(path
+ wxFileName::GetPathSeparator());
164 // add only normalized relative/absolute paths
165 fn
.Normalize(wxPATH_NORM_DOTS
|wxPATH_NORM_TILDE
|wxPATH_NORM_LONG
|wxPATH_NORM_ENV_VARS
);
167 wxString toadd
= fn
.GetPath();
168 if (Index(toadd
) == wxNOT_FOUND
)
169 wxArrayString::Add(toadd
); // do not add duplicates
172 void wxPathList::Add(const wxArrayString
&arr
)
174 for (size_t j
=0; j
< arr
.GetCount(); j
++)
178 // Add paths e.g. from the PATH environment variable
179 void wxPathList::AddEnvList (const wxString
& WXUNUSED_IN_WINCE(envVariable
))
181 // No environment variables on WinCE
184 // The space has been removed from the tokenizers, otherwise a
185 // path such as "C:\Program Files" would be split into 2 paths:
186 // "C:\Program" and "Files"; this is true for both Windows and Unix.
188 static const wxChar PATH_TOKS
[] =
189 #if defined(__WINDOWS__) || defined(__OS2__)
190 wxT(";"); // Don't separate with colon in DOS (used for drive)
196 if ( wxGetEnv(envVariable
, &val
) )
198 // split into an array of string the value of the env var
199 wxArrayString arr
= wxStringTokenize(val
, PATH_TOKS
);
200 WX_APPEND_ARRAY(*this, arr
);
202 #endif // !__WXWINCE__
205 // Given a full filename (with path), ensure that that file can
206 // be accessed again USING FILENAME ONLY by adding the path
207 // to the list if not already there.
208 void wxPathList::EnsureFileAccessible (const wxString
& path
)
210 wxString
path_only(wxPathOnly(path
));
211 if ( !path_only
.empty() )
213 if ( Index(path_only
) == wxNOT_FOUND
)
219 bool wxPathList::Member (const wxString
& path
) const
221 return Index(path
) != wxNOT_FOUND
;
224 wxString
wxPathList::FindValidPath (const wxString
& file
) const
226 // normalize the given string as it could be a path + a filename
227 // and not only a filename
231 // NB: normalize without making absolute !
232 fn
.Normalize(wxPATH_NORM_DOTS
|wxPATH_NORM_TILDE
|wxPATH_NORM_LONG
|wxPATH_NORM_ENV_VARS
);
234 wxASSERT_MSG(!fn
.IsDir(), wxT("Cannot search for directories; only for files"));
236 strend
= fn
.GetFullName(); // search for the file name and ignore the path part
238 strend
= fn
.GetFullPath();
240 for (size_t i
=0; i
<GetCount(); i
++)
242 wxString strstart
= Item(i
);
243 if (!strstart
.IsEmpty() && strstart
.Last() != wxFileName::GetPathSeparator())
244 strstart
+= wxFileName::GetPathSeparator();
246 if (wxFileExists(strstart
+ strend
))
247 return strstart
+ strend
; // Found!
250 return wxEmptyString
; // Not found
253 wxString
wxPathList::FindAbsoluteValidPath (const wxString
& file
) const
255 wxString f
= FindValidPath(file
);
256 if ( f
.empty() || wxIsAbsolutePath(f
) )
259 wxString buf
= ::wxGetCwd();
261 if ( !wxEndsWithPathSeparator(buf
) )
263 buf
+= wxFILE_SEP_PATH
;
270 // ----------------------------------------------------------------------------
271 // miscellaneous global functions (TOFIX!)
272 // ----------------------------------------------------------------------------
274 static inline wxChar
* MYcopystring(const wxString
& s
)
276 wxChar
* copy
= new wxChar
[s
.length() + 1];
277 return wxStrcpy(copy
, s
.c_str());
280 static inline wxChar
* MYcopystring(const wxChar
* s
)
282 wxChar
* copy
= new wxChar
[wxStrlen(s
) + 1];
283 return wxStrcpy(copy
, s
);
288 wxFileExists (const wxString
& filename
)
290 #if defined(__WXPALMOS__)
292 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
293 // we must use GetFileAttributes() instead of the ANSI C functions because
294 // it can cope with network (UNC) paths unlike them
295 DWORD ret
= ::GetFileAttributes(filename
);
297 return (ret
!= (DWORD
)-1) && !(ret
& FILE_ATTRIBUTE_DIRECTORY
);
300 #define S_ISREG(mode) ((mode) & S_IFREG)
303 #ifndef wxNEED_WX_UNISTD_H
304 return (wxStat( filename
.fn_str() , &st
) == 0 && S_ISREG(st
.st_mode
))
306 || (errno
== EACCES
) // if access is denied something with that name
307 // exists and is opened in exclusive mode.
311 return wxStat( filename
, &st
) == 0 && S_ISREG(st
.st_mode
);
313 #endif // __WIN32__/!__WIN32__
317 wxIsAbsolutePath (const wxString
& filename
)
319 if (!filename
.empty())
321 #if defined(__WXMAC__) && !defined(__DARWIN__)
322 // Classic or Carbon CodeWarrior like
323 // Carbon with Apple DevTools is Unix like
325 // This seems wrong to me, but there is no fix. since
326 // "MacOS:MyText.txt" is absolute whereas "MyDir:MyText.txt"
327 // is not. Or maybe ":MyDir:MyText.txt" has to be used? RR.
328 if (filename
.Find(':') != wxNOT_FOUND
&& filename
[0] != ':')
331 // Unix like or Windows
332 if (filename
[0] == wxT('/'))
336 if ((filename
[0] == wxT('[') && filename
[1] != wxT('.')))
339 #if defined(__WINDOWS__) || defined(__OS2__)
341 if (filename
[0] == wxT('\\') || (wxIsalpha (filename
[0]) && filename
[1] == wxT(':')))
349 * Strip off any extension (dot something) from end of file,
350 * IF one exists. Inserts zero into buffer.
354 void wxStripExtension(wxChar
*buffer
)
356 int len
= wxStrlen(buffer
);
360 if (buffer
[i
] == wxT('.'))
369 void wxStripExtension(wxString
& buffer
)
371 //RN: Be careful about the handling the case where
372 //buffer.length() == 0
373 for(size_t i
= buffer
.length() - 1; i
!= wxString::npos
; --i
)
375 if (buffer
.GetChar(i
) == wxT('.'))
377 buffer
= buffer
.Left(i
);
383 // Destructive removal of /./ and /../ stuff
384 wxChar
*wxRealPath (wxChar
*path
)
387 static const wxChar SEP
= wxT('\\');
388 wxUnix2DosFilename(path
);
390 static const wxChar SEP
= wxT('/');
392 if (path
[0] && path
[1]) {
393 /* MATTHEW: special case "/./x" */
395 if (path
[2] == SEP
&& path
[1] == wxT('.'))
403 if (p
[1] == wxT('.') && p
[2] == wxT('.') && (p
[3] == SEP
|| p
[3] == wxT('\0')))
406 for (q
= p
- 1; q
>= path
&& *q
!= SEP
; q
--)
411 if (q
[0] == SEP
&& (q
[1] != wxT('.') || q
[2] != wxT('.') || q
[3] != SEP
)
412 && (q
- 1 <= path
|| q
[-1] != SEP
))
415 if (path
[0] == wxT('\0'))
420 #if defined(__WXMSW__) || defined(__OS2__)
421 /* Check that path[2] is NULL! */
422 else if (path
[1] == wxT(':') && !path
[2])
431 else if (p
[1] == wxT('.') && (p
[2] == SEP
|| p
[2] == wxT('\0')))
439 wxString
wxRealPath(const wxString
& path
)
441 wxChar
*buf1
=MYcopystring(path
);
442 wxChar
*buf2
=wxRealPath(buf1
);
450 wxChar
*wxCopyAbsolutePath(const wxString
& filename
)
452 if (filename
.empty())
453 return (wxChar
*) NULL
;
455 if (! wxIsAbsolutePath(wxExpandPath(wxFileFunctionsBuffer
, filename
)))
457 wxString buf
= ::wxGetCwd();
458 wxChar ch
= buf
.Last();
460 if (ch
!= wxT('\\') && ch
!= wxT('/'))
466 buf
<< wxFileFunctionsBuffer
;
467 buf
= wxRealPath( buf
);
468 return MYcopystring( buf
);
470 return MYcopystring( wxFileFunctionsBuffer
);
476 ~user/ => user's home dir
477 If the environment variable a = "foo" and b = "bar" then:
494 /* input name in name, pathname output to buf. */
496 wxChar
*wxExpandPath(wxChar
*buf
, const wxChar
*name
)
498 register wxChar
*d
, *s
, *nm
;
499 wxChar lnm
[_MAXPATHLEN
];
502 // Some compilers don't like this line.
503 // const wxChar trimchars[] = wxT("\n \t");
506 trimchars
[0] = wxT('\n');
507 trimchars
[1] = wxT(' ');
508 trimchars
[2] = wxT('\t');
512 const wxChar SEP
= wxT('\\');
514 const wxChar SEP
= wxT('/');
517 if (name
== NULL
|| *name
== wxT('\0'))
519 nm
= MYcopystring(name
); // Make a scratch copy
522 /* Skip leading whitespace and cr */
523 while (wxStrchr((wxChar
*)trimchars
, *nm
) != NULL
)
525 /* And strip off trailing whitespace and cr */
526 s
= nm
+ (q
= wxStrlen(nm
)) - 1;
527 while (q
-- && wxStrchr((wxChar
*)trimchars
, *s
) != NULL
)
535 q
= nm
[0] == wxT('\\') && nm
[1] == wxT('~');
538 /* Expand inline environment variables */
556 while ((*d
++ = *s
) != 0) {
558 if (*s
== wxT('\\')) {
559 if ((*(d
- 1) = *++s
)!=0) {
567 // No env variables on WinCE
570 if (*s
++ == wxT('$') && (*s
== wxT('{') || *s
== wxT(')')))
572 if (*s
++ == wxT('$'))
575 register wxChar
*start
= d
;
576 register int braces
= (*s
== wxT('{') || *s
== wxT('('));
577 register wxChar
*value
;
578 while ((*d
++ = *s
) != 0)
579 if (braces
? (*s
== wxT('}') || *s
== wxT(')')) : !(wxIsalnum(*s
) || *s
== wxT('_')) )
584 value
= wxGetenv(braces
? start
+ 1 : start
);
586 for ((d
= start
- 1); (*d
++ = *value
++) != 0;)
600 /* Expand ~ and ~user */
602 if (nm
[0] == wxT('~') && !q
)
605 if (nm
[1] == SEP
|| nm
[1] == 0)
607 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
608 if ((s
= WXSTRINGCAST
wxGetUserHome(wxEmptyString
)) != NULL
) {
613 { /* ~user/filename */
614 register wxChar
*nnm
;
615 register wxChar
*home
;
616 for (s
= nm
; *s
&& *s
!= SEP
; s
++)
620 int was_sep
; /* MATTHEW: Was there a separator, or NULL? */
621 was_sep
= (*s
== SEP
);
622 nnm
= *s
? s
+ 1 : s
;
624 // FIXME: wxGetUserHome could return temporary storage in Unicode mode
625 if ((home
= WXSTRINGCAST
wxGetUserHome(wxString(nm
+ 1))) == NULL
)
627 if (was_sep
) /* replace only if it was there: */
640 if (s
&& *s
) { /* MATTHEW: s could be NULL if user '~' didn't exist */
642 while (wxT('\0') != (*d
++ = *s
++))
645 if (d
- 1 > buf
&& *(d
- 2) != SEP
)
649 while ((*d
++ = *s
++) != 0)
653 delete[] nm_tmp
; // clean up alloc
654 /* Now clean up the buffer */
655 return wxRealPath(buf
);
658 /* Contract Paths to be build upon an environment variable
661 example: "/usr/openwin/lib", OPENWINHOME --> ${OPENWINHOME}/lib
663 The call wxExpandPath can convert these back!
666 wxContractPath (const wxString
& filename
,
667 const wxString
& WXUNUSED_IN_WINCE(envname
),
668 const wxString
& user
)
670 static wxChar dest
[_MAXPATHLEN
];
672 if (filename
.empty())
673 return (wxChar
*) NULL
;
675 wxStrcpy (dest
, WXSTRINGCAST filename
);
677 wxUnix2DosFilename(dest
);
680 // Handle environment
684 if (!envname
.empty() && (val
= wxGetenv (WXSTRINGCAST envname
)) != NULL
&&
685 (tcp
= wxStrstr (dest
, val
)) != NULL
)
687 wxStrcpy (wxFileFunctionsBuffer
, tcp
+ wxStrlen (val
));
690 wxStrcpy (tcp
, WXSTRINGCAST envname
);
691 wxStrcat (tcp
, wxT("}"));
692 wxStrcat (tcp
, wxFileFunctionsBuffer
);
696 // Handle User's home (ignore root homes!)
697 val
= wxGetUserHome (user
);
701 const size_t len
= wxStrlen(val
);
705 if (wxStrncmp(dest
, val
, len
) == 0)
707 wxStrcpy(wxFileFunctionsBuffer
, wxT("~"));
709 wxStrcat(wxFileFunctionsBuffer
, (const wxChar
*) user
);
710 wxStrcat(wxFileFunctionsBuffer
, dest
+ len
);
711 wxStrcpy (dest
, wxFileFunctionsBuffer
);
717 // Return just the filename, not the path (basename)
718 wxChar
*wxFileNameFromPath (wxChar
*path
)
721 wxString n
= wxFileNameFromPath(p
);
723 return path
+ p
.length() - n
.length();
726 wxString
wxFileNameFromPath (const wxString
& path
)
729 wxFileName::SplitPath(path
, NULL
, &name
, &ext
);
731 wxString fullname
= name
;
734 fullname
<< wxFILE_SEP_EXT
<< ext
;
740 // Return just the directory, or NULL if no directory
742 wxPathOnly (wxChar
*path
)
746 static wxChar buf
[_MAXPATHLEN
];
749 wxStrcpy (buf
, path
);
751 int l
= wxStrlen(path
);
754 // Search backward for a backward or forward slash
757 #if defined(__WXMAC__) && !defined(__DARWIN__)
758 // Classic or Carbon CodeWarrior like
759 // Carbon with Apple DevTools is Unix like
760 if (path
[i
] == wxT(':') )
766 // Unix like or Windows
767 if (path
[i
] == wxT('/') || path
[i
] == wxT('\\'))
774 if (path
[i
] == wxT(']'))
783 #if defined(__WXMSW__) || defined(__OS2__)
784 // Try Drive specifier
785 if (wxIsalpha (buf
[0]) && buf
[1] == wxT(':'))
787 // A:junk --> A:. (since A:.\junk Not A:\junk)
794 return (wxChar
*) NULL
;
797 // Return just the directory, or NULL if no directory
798 wxString
wxPathOnly (const wxString
& path
)
802 wxChar buf
[_MAXPATHLEN
];
805 wxStrcpy (buf
, WXSTRINGCAST path
);
807 int l
= path
.length();
810 // Search backward for a backward or forward slash
813 #if defined(__WXMAC__) && !defined(__DARWIN__)
814 // Classic or Carbon CodeWarrior like
815 // Carbon with Apple DevTools is Unix like
816 if (path
[i
] == wxT(':') )
819 return wxString(buf
);
822 // Unix like or Windows
823 if (path
[i
] == wxT('/') || path
[i
] == wxT('\\'))
825 // Don't return an empty string
829 return wxString(buf
);
833 if (path
[i
] == wxT(']'))
836 return wxString(buf
);
842 #if defined(__WXMSW__) || defined(__OS2__)
843 // Try Drive specifier
844 if (wxIsalpha (buf
[0]) && buf
[1] == wxT(':'))
846 // A:junk --> A:. (since A:.\junk Not A:\junk)
849 return wxString(buf
);
853 return wxEmptyString
;
856 // Utility for converting delimiters in DOS filenames to UNIX style
857 // and back again - or we get nasty problems with delimiters.
858 // Also, convert to lower case, since case is significant in UNIX.
860 #if defined(__WXMAC__)
862 #if TARGET_API_MAC_OSX
863 #define kDefaultPathStyle kCFURLPOSIXPathStyle
865 #define kDefaultPathStyle kCFURLHFSPathStyle
868 wxString
wxMacFSRefToPath( const FSRef
*fsRef
, CFStringRef additionalPathComponent
)
871 fullURLRef
= CFURLCreateFromFSRef(NULL
, fsRef
);
872 if ( additionalPathComponent
)
874 CFURLRef parentURLRef
= fullURLRef
;
875 fullURLRef
= CFURLCreateCopyAppendingPathComponent(NULL
, parentURLRef
,
876 additionalPathComponent
,false);
877 CFRelease( parentURLRef
) ;
879 CFStringRef cfString
= CFURLCopyFileSystemPath(fullURLRef
, kDefaultPathStyle
);
880 CFRelease( fullURLRef
) ;
881 CFMutableStringRef cfMutableString
= CFStringCreateMutableCopy(NULL
, 0, cfString
);
882 CFRelease( cfString
);
883 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormC
);
884 return wxMacCFStringHolder(cfMutableString
).AsString();
887 OSStatus
wxMacPathToFSRef( const wxString
&path
, FSRef
*fsRef
)
889 OSStatus err
= noErr
;
890 CFMutableStringRef cfMutableString
= CFStringCreateMutableCopy(NULL
, 0, wxMacCFStringHolder(path
));
891 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormD
);
892 CFURLRef url
= CFURLCreateWithFileSystemPath(kCFAllocatorDefault
, cfMutableString
, kDefaultPathStyle
, false);
893 CFRelease( cfMutableString
);
896 if ( CFURLGetFSRef(url
, fsRef
) == false )
907 wxString
wxMacHFSUniStrToString( ConstHFSUniStr255Param uniname
)
909 CFStringRef cfname
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
912 CFMutableStringRef cfMutableString
= CFStringCreateMutableCopy(NULL
, 0, cfname
);
914 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormC
);
915 return wxMacCFStringHolder(cfMutableString
).AsString() ;
920 wxString
wxMacFSSpec2MacFilename( const FSSpec
*spec
)
923 if ( FSpMakeFSRef( spec
, &fsRef
) == noErr
)
925 return wxMacFSRefToPath( &fsRef
) ;
927 return wxEmptyString
;
930 void wxMacFilename2FSSpec( const wxString
& path
, FSSpec
*spec
)
932 OSStatus err
= noErr
;
934 wxMacPathToFSRef( path
, &fsRef
) ;
935 err
= FSRefMakeFSSpec( &fsRef
, spec
) ;
942 wxDos2UnixFilename (wxChar
*s
)
951 *s
= (wxChar
)wxTolower (*s
); // Case INDEPENDENT
958 #if defined(__WXMSW__) || defined(__OS2__)
959 wxUnix2DosFilename (wxChar
*s
)
961 wxUnix2DosFilename (wxChar
*WXUNUSED(s
) )
964 // Yes, I really mean this to happen under DOS only! JACS
965 #if defined(__WXMSW__) || defined(__OS2__)
976 // Concatenate two files to form third
978 wxConcatFiles (const wxString
& file1
, const wxString
& file2
, const wxString
& file3
)
982 wxFile
in1(file1
), in2(file2
);
983 wxTempFile
out(file3
);
985 if ( !in1
.IsOpened() || !in2
.IsOpened() || !out
.IsOpened() )
989 unsigned char buf
[1024];
991 for( int i
=0; i
<2; i
++)
993 wxFile
*in
= i
==0 ? &in1
: &in2
;
995 if ( (ofs
= in
->Read(buf
,WXSIZEOF(buf
))) == wxInvalidOffset
) return false;
997 if ( !out
.Write(buf
,ofs
) )
999 } while ( ofs
== (ssize_t
)WXSIZEOF(buf
) );
1002 return out
.Commit();
1016 wxCopyFile (const wxString
& file1
, const wxString
& file2
, bool overwrite
)
1018 #if defined(__WIN32__) && !defined(__WXMICROWIN__)
1019 // CopyFile() copies file attributes and modification time too, so use it
1020 // instead of our code if available
1022 // NB: 3rd parameter is bFailIfExists i.e. the inverse of overwrite
1023 if ( !::CopyFile(file1
, file2
, !overwrite
) )
1025 wxLogSysError(_("Failed to copy the file '%s' to '%s'"),
1026 file1
.c_str(), file2
.c_str());
1030 #elif defined(__OS2__)
1031 if ( ::DosCopy((PSZ
)file1
.c_str(), (PSZ
)file2
.c_str(), overwrite
? DCPY_EXISTING
: 0) != 0 )
1033 #elif defined(__PALMOS__)
1034 // TODO with http://www.palmos.com/dev/support/docs/protein_books/Memory_Databases_Files/
1036 #elif wxUSE_FILE // !Win32
1039 // get permissions of file1
1040 if ( wxStat( file1
.c_str(), &fbuf
) != 0 )
1042 // the file probably doesn't exist or we haven't the rights to read
1044 wxLogSysError(_("Impossible to get permissions for file '%s'"),
1049 // open file1 for reading
1050 wxFile
fileIn(file1
, wxFile::read
);
1051 if ( !fileIn
.IsOpened() )
1054 // remove file2, if it exists. This is needed for creating
1055 // file2 with the correct permissions in the next step
1056 if ( wxFileExists(file2
) && (!overwrite
|| !wxRemoveFile(file2
)))
1058 wxLogSysError(_("Impossible to overwrite the file '%s'"),
1063 // reset the umask as we want to create the file with exactly the same
1064 // permissions as the original one
1067 // create file2 with the same permissions than file1 and open it for
1071 if ( !fileOut
.Create(file2
, overwrite
, fbuf
.st_mode
& 0777) )
1074 // copy contents of file1 to file2
1079 count
= fileIn
.Read(buf
, WXSIZEOF(buf
));
1080 if ( fileIn
.Error() )
1087 if ( fileOut
.Write(buf
, count
) < count
)
1091 // we can expect fileIn to be closed successfully, but we should ensure
1092 // that fileOut was closed as some write errors (disk full) might not be
1093 // detected before doing this
1094 if ( !fileIn
.Close() || !fileOut
.Close() )
1097 #if !defined(__VISAGECPP__) && !defined(__WXMAC__) || defined(__UNIX__)
1098 // no chmod in VA. Should be some permission API for HPFS386 partitions
1100 if ( chmod(OS_FILENAME(file2
), fbuf
.st_mode
) != 0 )
1102 wxLogSysError(_("Impossible to set permissions for the file '%s'"),
1106 #endif // OS/2 || Mac
1108 #else // !Win32 && ! wxUSE_FILE
1110 // impossible to simulate with wxWidgets API
1113 wxUnusedVar(overwrite
);
1116 #endif // __WXMSW__ && __WIN32__
1122 wxRenameFile(const wxString
& file1
, const wxString
& file2
, bool overwrite
)
1124 if ( !overwrite
&& wxFileExists(file2
) )
1128 _("Failed to rename the file '%s' to '%s' because the destination file already exists."),
1129 file1
.c_str(), file2
.c_str()
1135 #if !defined(__WXWINCE__) && !defined(__WXPALMOS__)
1136 // Normal system call
1137 if ( wxRename (file1
, file2
) == 0 )
1142 if (wxCopyFile(file1
, file2
, overwrite
)) {
1143 wxRemoveFile(file1
);
1150 bool wxRemoveFile(const wxString
& file
)
1152 #if defined(__VISUALC__) \
1153 || defined(__BORLANDC__) \
1154 || defined(__WATCOMC__) \
1155 || defined(__DMC__) \
1156 || defined(__GNUWIN32__) \
1157 || (defined(__MWERKS__) && defined(__MSL__))
1158 int res
= wxRemove(file
);
1159 #elif defined(__WXMAC__)
1160 int res
= unlink(wxFNCONV(file
));
1161 #elif defined(__WXPALMOS__)
1163 // TODO with VFSFileDelete()
1165 int res
= unlink(OS_FILENAME(file
));
1171 bool wxMkdir(const wxString
& dir
, int perm
)
1173 #if defined(__WXPALMOS__)
1175 #elif defined(__WXMAC__) && !defined(__UNIX__)
1176 return (mkdir( wxFNCONV(dir
) , 0 ) == 0);
1178 const wxChar
*dirname
= dir
.c_str();
1180 // assume mkdir() has 2 args on non Windows-OS/2 platforms and on Windows too
1181 // for the GNU compiler
1182 #if (!(defined(__WXMSW__) || defined(__OS2__) || defined(__DOS__))) || (defined(__GNUWIN32__) && !defined(__MINGW32__)) || defined(__WINE__) || defined(__WXMICROWIN__)
1185 if ( mkdir(wxFNCONV(dirname
)) != 0 )
1187 if ( mkdir(wxFNCONV(dirname
), perm
) != 0 )
1189 #elif defined(__OS2__)
1191 if (::DosCreateDir((PSZ
)dirname
, NULL
) != 0) // enhance for EAB's??
1192 #elif defined(__DOS__)
1193 #if defined(__WATCOMC__)
1195 if ( wxMkDir(wxFNSTRINGCAST
wxFNCONV(dirname
)) != 0 )
1196 #elif defined(__DJGPP__)
1197 if ( mkdir(wxFNCONV(dirname
), perm
) != 0 )
1199 #error "Unsupported DOS compiler!"
1201 #else // !MSW, !DOS and !OS/2 VAC++
1204 if ( !CreateDirectory(dirname
, NULL
) )
1206 if ( wxMkDir(dir
.fn_str()) != 0 )
1210 wxLogSysError(_("Directory '%s' couldn't be created"), dirname
);
1219 bool wxRmdir(const wxString
& dir
, int WXUNUSED(flags
))
1221 #if defined(__VMS__)
1222 return false; //to be changed since rmdir exists in VMS7.x
1223 #elif defined(__OS2__)
1224 return (::DosDeleteDir((PSZ
)dir
.c_str()) == 0);
1225 #elif defined(__WXWINCE__)
1226 return (CreateDirectory(dir
, NULL
) != 0);
1227 #elif defined(__WXPALMOS__)
1228 // TODO with VFSFileRename()
1231 return (wxRmDir(OS_FILENAME(dir
)) == 0);
1235 // does the path exists? (may have or not '/' or '\\' at the end)
1236 bool wxDirExists(const wxChar
*pszPathName
)
1238 wxString
strPath(pszPathName
);
1240 #if defined(__WINDOWS__) || defined(__OS2__)
1241 // Windows fails to find directory named "c:\dir\" even if "c:\dir" exists,
1242 // so remove all trailing backslashes from the path - but don't do this for
1243 // the paths "d:\" (which are different from "d:") nor for just "\"
1244 while ( wxEndsWithPathSeparator(strPath
) )
1246 size_t len
= strPath
.length();
1247 if ( len
== 1 || (len
== 3 && strPath
[len
- 2] == _T(':')) )
1250 strPath
.Truncate(len
- 1);
1252 #endif // __WINDOWS__
1255 // OS/2 can't handle "d:", it wants either "d:\" or "d:."
1256 if (strPath
.length() == 2 && strPath
[1u] == _T(':'))
1260 #if defined(__WXPALMOS__)
1262 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
1263 // stat() can't cope with network paths
1264 DWORD ret
= ::GetFileAttributes(strPath
);
1266 return (ret
!= (DWORD
)-1) && (ret
& FILE_ATTRIBUTE_DIRECTORY
);
1267 #elif defined(__OS2__)
1268 FILESTATUS3 Info
= {{0}};
1269 APIRET rc
= ::DosQueryPathInfo((PSZ
)(WXSTRINGCAST strPath
), FIL_STANDARD
,
1270 (void*) &Info
, sizeof(FILESTATUS3
));
1272 return ((rc
== NO_ERROR
) && (Info
.attrFile
& FILE_DIRECTORY
)) ||
1273 (rc
== ERROR_SHARING_VIOLATION
);
1274 // If we got a sharing violation, there must be something with this name.
1278 #ifndef __VISAGECPP__
1279 return wxStat(strPath
.c_str(), &st
) == 0 && ((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1281 // S_IFMT not supported in VA compilers.. st_mode is a 2byte value only
1282 return wxStat(pszPathName
, &st
) == 0 && (st
.st_mode
== S_IFDIR
);
1285 #endif // __WIN32__/!__WIN32__
1288 // Get a temporary filename, opening and closing the file.
1289 wxChar
*wxGetTempFileName(const wxString
& prefix
, wxChar
*buf
)
1292 if ( !wxGetTempFileName(prefix
, filename
) )
1296 wxStrcpy(buf
, filename
);
1298 buf
= MYcopystring(filename
);
1303 bool wxGetTempFileName(const wxString
& prefix
, wxString
& buf
)
1306 buf
= wxFileName::CreateTempFileName(prefix
);
1308 return !buf
.empty();
1309 #else // !wxUSE_FILE
1310 wxUnusedVar(prefix
);
1314 #endif // wxUSE_FILE/!wxUSE_FILE
1317 // Get first file name matching given wild card.
1319 static wxDir
*gs_dir
= NULL
;
1320 static wxString gs_dirPath
;
1322 wxString
wxFindFirstFile(const wxChar
*spec
, int flags
)
1324 wxSplitPath(spec
, &gs_dirPath
, NULL
, NULL
);
1325 if ( gs_dirPath
.empty() )
1326 gs_dirPath
= wxT(".");
1327 if ( !wxEndsWithPathSeparator(gs_dirPath
) )
1328 gs_dirPath
<< wxFILE_SEP_PATH
;
1332 gs_dir
= new wxDir(gs_dirPath
);
1334 if ( !gs_dir
->IsOpened() )
1336 wxLogSysError(_("Can not enumerate files '%s'"), spec
);
1337 return wxEmptyString
;
1343 case wxDIR
: dirFlags
= wxDIR_DIRS
; break;
1344 case wxFILE
: dirFlags
= wxDIR_FILES
; break;
1345 default: dirFlags
= wxDIR_DIRS
| wxDIR_FILES
; break;
1349 gs_dir
->GetFirst(&result
, wxFileNameFromPath(wxString(spec
)), dirFlags
);
1350 if ( result
.empty() )
1356 return gs_dirPath
+ result
;
1359 wxString
wxFindNextFile()
1361 wxASSERT_MSG( gs_dir
, wxT("You must call wxFindFirstFile before!") );
1364 gs_dir
->GetNext(&result
);
1366 if ( result
.empty() )
1372 return gs_dirPath
+ result
;
1376 // Get current working directory.
1377 // If buf is NULL, allocates space using new, else copies into buf.
1378 // wxGetWorkingDirectory() is obsolete, use wxGetCwd()
1379 // wxDoGetCwd() is their common core to be moved
1380 // to wxGetCwd() once wxGetWorkingDirectory() will be removed.
1381 // Do not expose wxDoGetCwd in headers!
1383 wxChar
*wxDoGetCwd(wxChar
*buf
, int sz
)
1385 #if defined(__WXPALMOS__)
1387 if(buf
&& sz
>0) buf
[0] = _T('\0');
1389 #elif defined(__WXWINCE__)
1391 if(buf
&& sz
>0) buf
[0] = _T('\0');
1396 buf
= new wxChar
[sz
+ 1];
1399 bool ok
wxDUMMY_INITIALIZE(false);
1401 // for the compilers which have Unicode version of _getcwd(), call it
1402 // directly, for the others call the ANSI version and do the translation
1405 #else // wxUSE_UNICODE
1406 bool needsANSI
= true;
1408 #if !defined(HAVE_WGETCWD) || wxUSE_UNICODE_MSLU
1409 char cbuf
[_MAXPATHLEN
];
1413 #if wxUSE_UNICODE_MSLU
1414 if ( wxGetOsVersion() != wxOS_WINDOWS_9X
)
1416 char *cbuf
= NULL
; // never really used because needsANSI will always be false
1419 ok
= _wgetcwd(buf
, sz
) != NULL
;
1425 #endif // wxUSE_UNICODE
1427 #if defined(_MSC_VER) || defined(__MINGW32__)
1428 ok
= _getcwd(cbuf
, sz
) != NULL
;
1429 #elif defined(__WXMAC__) && !defined(__DARWIN__)
1431 if ( getcwd( lbuf
, sizeof( lbuf
) ) )
1433 wxString
res( lbuf
, *wxConvCurrent
) ;
1434 wxStrcpy( buf
, res
) ;
1439 #elif defined(__OS2__)
1441 ULONG ulDriveNum
= 0;
1442 ULONG ulDriveMap
= 0;
1443 rc
= ::DosQueryCurrentDisk(&ulDriveNum
, &ulDriveMap
);
1448 rc
= ::DosQueryCurrentDir( 0 // current drive
1452 cbuf
[0] = char('A' + (ulDriveNum
- 1));
1457 #else // !Win32/VC++ !Mac !OS2
1458 ok
= getcwd(cbuf
, sz
) != NULL
;
1461 #if wxUSE_UNICODE && !(defined(__WXMAC__) && !defined(__DARWIN__))
1462 // finally convert the result to Unicode if needed
1463 wxConvFile
.MB2WC(buf
, cbuf
, sz
);
1464 #endif // wxUSE_UNICODE
1469 wxLogSysError(_("Failed to get the working directory"));
1471 // VZ: the old code used to return "." on error which didn't make any
1472 // sense at all to me - empty string is a better error indicator
1473 // (NULL might be even better but I'm afraid this could lead to
1474 // problems with the old code assuming the return is never NULL)
1477 else // ok, but we might need to massage the path into the right format
1480 // VS: DJGPP is a strange mix of DOS and UNIX API and returns paths
1481 // with / deliminers. We don't like that.
1482 for (wxChar
*ch
= buf
; *ch
; ch
++)
1484 if (*ch
== wxT('/'))
1489 // MBN: we hope that in the case the user is compiling a GTK+/Motif app,
1490 // he needs Unix as opposed to Win32 pathnames
1491 #if defined( __CYGWIN__ ) && defined( __WINDOWS__ )
1492 // another example of DOS/Unix mix (Cygwin)
1493 wxString pathUnix
= buf
;
1495 char bufA
[_MAXPATHLEN
];
1496 cygwin_conv_to_full_win32_path(pathUnix
.mb_str(wxConvFile
), bufA
);
1497 wxConvFile
.MB2WC(buf
, bufA
, sz
);
1499 cygwin_conv_to_full_win32_path(pathUnix
, buf
);
1500 #endif // wxUSE_UNICODE
1501 #endif // __CYGWIN__
1514 #if WXWIN_COMPATIBILITY_2_6
1515 wxChar
*wxGetWorkingDirectory(wxChar
*buf
, int sz
)
1517 return wxDoGetCwd(buf
,sz
);
1519 #endif // WXWIN_COMPATIBILITY_2_6
1524 wxDoGetCwd(wxStringBuffer(str
, _MAXPATHLEN
), _MAXPATHLEN
);
1528 bool wxSetWorkingDirectory(const wxString
& d
)
1530 #if defined(__OS2__)
1531 return (::DosSetCurrentDir((PSZ
)d
.c_str()) == 0);
1532 #elif defined(__UNIX__) || defined(__WXMAC__) || defined(__DOS__)
1533 return (chdir(wxFNSTRINGCAST d
.fn_str()) == 0);
1534 #elif defined(__WINDOWS__)
1538 // No equivalent in WinCE
1542 return (bool)(SetCurrentDirectory(d
) != 0);
1545 // Must change drive, too.
1546 bool isDriveSpec
= ((strlen(d
) > 1) && (d
[1] == ':'));
1549 wxChar firstChar
= d
[0];
1553 firstChar
= firstChar
- 32;
1555 // To a drive number
1556 unsigned int driveNo
= firstChar
- 64;
1559 unsigned int noDrives
;
1560 _dos_setdrive(driveNo
, &noDrives
);
1563 bool success
= (chdir(WXSTRINGCAST d
) == 0);
1571 // Get the OS directory if appropriate (such as the Windows directory).
1572 // On non-Windows platform, probably just return the empty string.
1573 wxString
wxGetOSDirectory()
1576 return wxString(wxT("\\Windows"));
1577 #elif defined(__WINDOWS__) && !defined(__WXMICROWIN__)
1579 GetWindowsDirectory(buf
, 256);
1580 return wxString(buf
);
1581 #elif defined(__WXMAC__)
1582 return wxMacFindFolder(kOnSystemDisk
, 'macs', false);
1584 return wxEmptyString
;
1588 bool wxEndsWithPathSeparator(const wxChar
*pszFileName
)
1590 size_t len
= wxStrlen(pszFileName
);
1592 return len
&& wxIsPathSeparator(pszFileName
[len
- 1]);
1595 // find a file in a list of directories, returns false if not found
1596 bool wxFindFileInPath(wxString
*pStr
, const wxChar
*pszPath
, const wxChar
*pszFile
)
1598 // we assume that it's not empty
1599 wxCHECK_MSG( !wxIsEmpty(pszFile
), false,
1600 _T("empty file name in wxFindFileInPath"));
1602 // skip path separator in the beginning of the file name if present
1603 if ( wxIsPathSeparator(*pszFile
) )
1606 // copy the path (strtok will modify it)
1607 wxChar
*szPath
= new wxChar
[wxStrlen(pszPath
) + 1];
1608 wxStrcpy(szPath
, pszPath
);
1611 wxChar
*pc
, *save_ptr
;
1612 for ( pc
= wxStrtok(szPath
, wxPATH_SEP
, &save_ptr
);
1614 pc
= wxStrtok((wxChar
*) NULL
, wxPATH_SEP
, &save_ptr
) )
1616 // search for the file in this directory
1618 if ( !wxEndsWithPathSeparator(pc
) )
1619 strFile
+= wxFILE_SEP_PATH
;
1622 if ( wxFileExists(strFile
) ) {
1628 // suppress warning about unused variable save_ptr when wxStrtok() is a
1629 // macro which throws away its third argument
1634 return pc
!= NULL
; // if true => we breaked from the loop
1637 void WXDLLEXPORT
wxSplitPath(const wxChar
*pszFileName
,
1642 // it can be empty, but it shouldn't be NULL
1643 wxCHECK_RET( pszFileName
, wxT("NULL file name in wxSplitPath") );
1645 wxFileName::SplitPath(pszFileName
, pstrPath
, pstrName
, pstrExt
);
1650 time_t WXDLLEXPORT
wxFileModificationTime(const wxString
& filename
)
1653 if ( !wxFileName(filename
).GetTimes(NULL
, &mtime
, NULL
) )
1656 return mtime
.GetTicks();
1659 #endif // wxUSE_DATETIME
1662 // Parses the filterStr, returning the number of filters.
1663 // Returns 0 if none or if there's a problem.
1664 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpeg"
1666 int WXDLLEXPORT
wxParseCommonDialogsFilter(const wxString
& filterStr
,
1667 wxArrayString
& descriptions
,
1668 wxArrayString
& filters
)
1670 descriptions
.Clear();
1673 wxString
str(filterStr
);
1675 wxString description
, filter
;
1677 while( pos
!= wxNOT_FOUND
)
1679 pos
= str
.Find(wxT('|'));
1680 if ( pos
== wxNOT_FOUND
)
1682 // if there are no '|'s at all in the string just take the entire
1683 // string as filter and make description empty for later autocompletion
1684 if ( filters
.IsEmpty() )
1686 descriptions
.Add(wxEmptyString
);
1687 filters
.Add(filterStr
);
1691 wxFAIL_MSG( _T("missing '|' in the wildcard string!") );
1697 description
= str
.Left(pos
);
1698 str
= str
.Mid(pos
+ 1);
1699 pos
= str
.Find(wxT('|'));
1700 if ( pos
== wxNOT_FOUND
)
1706 filter
= str
.Left(pos
);
1707 str
= str
.Mid(pos
+ 1);
1710 descriptions
.Add(description
);
1711 filters
.Add(filter
);
1714 #if defined(__WXMOTIF__)
1715 // split it so there is one wildcard per entry
1716 for( size_t i
= 0 ; i
< descriptions
.GetCount() ; i
++ )
1718 pos
= filters
[i
].Find(wxT(';'));
1719 if (pos
!= wxNOT_FOUND
)
1721 // first split only filters
1722 descriptions
.Insert(descriptions
[i
],i
+1);
1723 filters
.Insert(filters
[i
].Mid(pos
+1),i
+1);
1724 filters
[i
]=filters
[i
].Left(pos
);
1726 // autoreplace new filter in description with pattern:
1727 // C/C++ Files(*.cpp;*.c;*.h)|*.cpp;*.c;*.h
1728 // cause split into:
1729 // C/C++ Files(*.cpp)|*.cpp
1730 // C/C++ Files(*.c;*.h)|*.c;*.h
1731 // and next iteration cause another split into:
1732 // C/C++ Files(*.cpp)|*.cpp
1733 // C/C++ Files(*.c)|*.c
1734 // C/C++ Files(*.h)|*.h
1735 for ( size_t k
=i
;k
<i
+2;k
++ )
1737 pos
= descriptions
[k
].Find(filters
[k
]);
1738 if (pos
!= wxNOT_FOUND
)
1740 wxString before
= descriptions
[k
].Left(pos
);
1741 wxString after
= descriptions
[k
].Mid(pos
+filters
[k
].Len());
1742 pos
= before
.Find(_T('('),true);
1743 if (pos
>before
.Find(_T(')'),true))
1745 before
= before
.Left(pos
+1);
1746 before
<< filters
[k
];
1747 pos
= after
.Find(_T(')'));
1748 int pos1
= after
.Find(_T('('));
1749 if (pos
!= wxNOT_FOUND
&& (pos
<pos1
|| pos1
==wxNOT_FOUND
))
1751 before
<< after
.Mid(pos
);
1752 descriptions
[k
] = before
;
1762 for( size_t j
= 0 ; j
< descriptions
.GetCount() ; j
++ )
1764 if ( descriptions
[j
].empty() && !filters
[j
].empty() )
1766 descriptions
[j
].Printf(_("Files (%s)"), filters
[j
].c_str());
1770 return filters
.GetCount();
1773 #if defined( __WINDOWS__ )
1774 bool wxCheckGenericPermission(const wxString
&path
, DWORD access
)
1776 // quoting the MSDN: "To obtain a handle to a directory, call the
1777 // CreateFile function with the FILE_FLAG_BACKUP_SEMANTICS flag"
1778 wxWinVersion ver
= wxGetWinVersion();
1779 bool isdir
= wxDirExists(path
);
1780 if (isdir
&& (ver
== wxWinVersion_95
|| ver
== wxWinVersion_98
|| ver
== wxWinVersion_ME
))
1782 // however Win95/98/ME do not support FILE_FLAG_BACKUP_SEMANTICS...
1783 if (access
== GENERIC_READ
)
1785 WIN32_FILE_ATTRIBUTE_DATA data
;
1786 if (GetFileAttributesEx(path
.c_str(), GetFileExInfoStandard
, &data
) == 0)
1787 return false; // cannot query attributes
1788 return (data
.dwFileAttributes
& FILE_ATTRIBUTE_READONLY
) == 0;
1791 // FIXME: is it true that directories are always writable & executable on Win9X family ?
1796 HANDLE h
= CreateFile(path
.c_str(), access
,
1797 FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
, NULL
,
1798 OPEN_EXISTING
, isdir
? FILE_FLAG_BACKUP_SEMANTICS
: 0, NULL
);
1799 if (h
!= INVALID_HANDLE_VALUE
)
1802 return h
!= INVALID_HANDLE_VALUE
;
1807 bool wxIsWritable(const wxString
&path
)
1809 #if defined( __UNIX__ )
1810 // access() will take in count also symbolic links
1811 return access(wxConvFile
.cWX2MB(path
), W_OK
) == 0;
1812 #elif defined( __WINDOWS__ )
1813 return wxCheckGenericPermission(path
, GENERIC_WRITE
);
1821 bool wxIsReadable(const wxString
&path
)
1823 #if defined( __UNIX__ )
1824 // access() will take in count also symbolic links
1825 return access(wxConvFile
.cWX2MB(path
), R_OK
) == 0;
1826 #elif defined( __WINDOWS__ )
1827 return wxCheckGenericPermission(path
, GENERIC_READ
);
1835 bool wxIsExecutable(const wxString
&path
)
1837 #if defined( __UNIX__ )
1838 // access() will take in count also symbolic links
1839 return access(wxConvFile
.cWX2MB(path
), X_OK
) == 0;
1840 #elif defined( __WINDOWS__ )
1841 return wxCheckGenericPermission(path
, GENERIC_EXECUTE
);
1850 //------------------------------------------------------------------------
1851 // wild character routines
1852 //------------------------------------------------------------------------
1854 bool wxIsWild( const wxString
& pattern
)
1856 wxString tmp
= pattern
;
1857 wxChar
*pat
= WXSTRINGCAST(tmp
);
1862 case wxT('?'): case wxT('*'): case wxT('['): case wxT('{'):
1873 * Written By Douglas A. Lewis <dalewis@cs.Buffalo.EDU>
1875 * The match procedure is public domain code (from ircII's reg.c)
1876 * but modified to suit our tastes (RN: No "%" syntax I guess)
1879 bool wxMatchWild( const wxString
& pat
, const wxString
& text
, bool dot_special
)
1883 /* Match if both are empty. */
1887 const wxChar
*m
= pat
.c_str(),
1895 if (dot_special
&& (*n
== wxT('.')))
1897 /* Never match so that hidden Unix files
1898 * are never found. */
1911 else if (*m
== wxT('?'))
1919 if (*m
== wxT('\\'))
1922 /* Quoting "nothing" is a bad thing */
1929 * If we are out of both strings or we just
1930 * saw a wildcard, then we can say we have a
1941 * We could check for *n == NULL at this point, but
1942 * since it's more common to have a character there,
1943 * check to see if they match first (m and n) and
1944 * then if they don't match, THEN we can check for
1960 * If there are no more characters in the
1961 * string, but we still need to find another
1962 * character (*m != NULL), then it will be
1963 * impossible to match it
1981 // Return the type of an open file
1983 // Some file types on some platforms seem seekable but in fact are not.
1984 // The main use of this function is to allow such cases to be detected
1985 // (IsSeekable() is implemented as wxGetFileKind() == wxFILE_KIND_DISK).
1987 // This is important for the archive streams, which benefit greatly from
1988 // being able to seek on a stream, but which will produce corrupt archives
1989 // if they unknowingly seek on a non-seekable stream.
1991 // wxFILE_KIND_DISK is a good catch all return value, since other values
1992 // disable features of the archive streams. Some other value must be returned
1993 // for a file type that appears seekable but isn't.
1996 // * Pipes on Windows
1997 // * Files on VMS with a record format other than StreamLF
1999 wxFileKind
wxGetFileKind(int fd
)
2001 #if defined __WXMSW__ && !defined __WXWINCE__ && defined wxGetOSFHandle
2002 switch (::GetFileType(wxGetOSFHandle(fd
)) & ~FILE_TYPE_REMOTE
)
2004 case FILE_TYPE_CHAR
:
2005 return wxFILE_KIND_TERMINAL
;
2006 case FILE_TYPE_DISK
:
2007 return wxFILE_KIND_DISK
;
2008 case FILE_TYPE_PIPE
:
2009 return wxFILE_KIND_PIPE
;
2012 return wxFILE_KIND_UNKNOWN
;
2014 #elif defined(__UNIX__)
2016 return wxFILE_KIND_TERMINAL
;
2021 if (S_ISFIFO(st
.st_mode
))
2022 return wxFILE_KIND_PIPE
;
2023 if (!S_ISREG(st
.st_mode
))
2024 return wxFILE_KIND_UNKNOWN
;
2026 #if defined(__VMS__)
2027 if (st
.st_fab_rfm
!= FAB$C_STMLF
)
2028 return wxFILE_KIND_UNKNOWN
;
2031 return wxFILE_KIND_DISK
;
2034 #define wxFILEKIND_STUB
2036 return wxFILE_KIND_DISK
;
2040 wxFileKind
wxGetFileKind(FILE *fp
)
2042 // Note: The watcom rtl dll doesn't have fileno (the static lib does).
2043 // Should be fixed in version 1.4.
2044 #if defined(wxFILEKIND_STUB) || wxONLY_WATCOM_EARLIER_THAN(1,4)
2046 return wxFILE_KIND_DISK
;
2048 return fp
? wxGetFileKind(fileno(fp
)) : wxFILE_KIND_UNKNOWN
;
2053 #pragma warning(default:4706) // assignment within conditional expression