1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: File- and directory-related functions
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #include "wx/arrstr.h"
19 #include "wx/msw/wince/time.h"
20 #include "wx/msw/private.h"
27 #elif !defined(__MWERKS__)
28 #include <sys/types.h>
32 #include <sys/types.h>
44 // need to check for __OS2__ first since currently both
45 // __OS2__ and __UNIX__ are defined.
47 #include "wx/os2/private.h"
55 #elif defined(__UNIX__)
60 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
61 #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) && !defined(__SALFORDC__) && !defined(__WXWINCE__) && !defined(__CYGWIN__)
66 #endif // native Win compiler
80 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
81 // this (3.1 I believe) and how to test for it.
82 // If this works for Borland 4.0 as well, then no worries.
92 #include <fcntl.h> // O_RDONLY &c
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
102 #if !defined(__WXMAC__) || defined(__UNIX__) || defined(__MACH__)
103 #include <sys/types.h>
109 #if (defined(__VISUALC__) && !defined(__WXWINCE__)) || ( defined(__MWERKS__) && defined( __INTEL__) )
110 typedef _off_t off_t
;
111 #elif defined(__SYMANTEC__)
113 #elif defined(__MWERKS__) && !defined(__INTEL__) && !defined(__MACH__)
127 wxFILE_KIND_DISK
, // a file supporting seeking to arbitrary offsets
128 wxFILE_KIND_TERMINAL
, // a tty
129 wxFILE_KIND_PIPE
// a pipe
132 // ----------------------------------------------------------------------------
133 // declare our versions of low level file functions: some compilers prepend
134 // underscores to the usual names, some also have Unicode versions of them
135 // ----------------------------------------------------------------------------
137 // Wrappers around Win32 api functions like CreateFile, ReadFile and such
138 // Implemented in filefnwce.cpp
139 #if defined( __WXWINCE__)
140 typedef __int64 wxFileOffset
;
141 #define wxFileOffsetFmtSpec _("I64")
142 int wxOpen(const wxChar
*filename
, int oflag
, int WXUNUSED(pmode
));
143 int wxAccess(const wxChar
*name
, int WXUNUSED(how
));
145 int wxFsync(int WXUNUSED(fd
));
146 int wxRead(int fd
, void *buf
, unsigned int count
);
147 int wxWrite(int fd
, const void *buf
, unsigned int count
);
149 wxFileOffset
wxSeek(int fd
, wxFileOffset offset
, int origin
);
150 #define wxLSeek wxSeek
151 wxFileOffset
wxTell(int fd
);
153 // always Unicode under WinCE
154 #define wxMkDir _wmkdir
155 #define wxRmDir _wrmdir
156 #define wxStat _wstat
157 #define wxStructStat struct _stat
158 #elif defined(__WXMSW__) && !defined(__WXPALMOS__) && \
160 defined(__VISUALC__) || \
161 (defined(__MINGW32__) && !defined(__WINE__) && \
162 wxCHECK_W32API_VERSION(0, 5)) || \
163 defined(__MWERKS__) || \
164 defined(__DMC__) || \
165 defined(__WATCOMC__) || \
166 defined(__BORLANDC__) \
169 #undef wxHAS_HUGE_FILES
171 // detect compilers which have support for huge files
172 #if defined(__VISUALC__)
173 #define wxHAS_HUGE_FILES 1
174 #elif defined(__MINGW32__)
175 #define wxHAS_HUGE_FILES 1
176 #elif defined(_LARGE_FILES)
177 #define wxHAS_HUGE_FILES 1
180 // other Windows compilers (DMC, Watcom, Metrowerks and Borland) don't have
181 // huge file support (or at least not all functions needed for it by wx)
186 #if defined(__BORLANDC__) || defined(__WATCOMC__)
190 #ifdef wxHAS_HUGE_FILES
191 typedef wxLongLong_t wxFileOffset
;
192 #define wxFileOffsetFmtSpec wxLongLongFmtSpec
194 typedef off_t wxFileOffset
;
197 #define wxClose _close
199 #if defined(__MWERKS__)
200 #if __MSL__ >= 0x6000
201 #define wxRead(fd, buf, nCount) _read(fd, (void *)buf, nCount)
202 #define wxWrite(fd, buf, nCount) _write(fd, (void *)buf, nCount)
204 #define wxRead(fd, buf, nCount)\
205 _read(fd, (const char *)buf, nCount)
206 #define wxWrite(fd, buf, nCount)\
207 _write(fd, (const char *)buf, nCount)
210 #if defined(__DMC__) || defined(__WATCOMC__)
211 #define wxRead ::read
212 #define wxWrite ::write
215 #define wxWrite _write
218 #ifdef wxHAS_HUGE_FILES
219 #define wxSeek _lseeki64
220 #define wxLseek _lseeki64
221 #define wxTell _telli64
222 #else // !wxHAS_HUGE_FILES
223 #define wxSeek _lseek
224 #define wxLseek _lseek
226 #endif // wxHAS_HUGE_FILES/!wxHAS_HUGE_FILES
228 #define wxFsync _commit
230 #if defined(__WATCOMC__)
237 #if wxUSE_UNICODE_MSLU
238 // implement the missing file functions in Win9x ourselves
239 #if defined( __VISUALC__ ) \
240 || ( defined(__MINGW32__) && wxCHECK_W32API_VERSION( 0, 5 ) ) \
241 || ( defined(__MWERKS__) && defined(__WXMSW__) ) \
242 || ( defined(__BORLANDC__) && (__BORLANDC__ > 0x460) ) \
245 // BCC has _stati64() function but struct stati64
246 #define _stati64 stati64
247 #endif // __BORLANDC__
249 WXDLLIMPEXP_BASE
int wxMSLU__wopen(const wxChar
*name
, int flags
, int mode
);
250 WXDLLIMPEXP_BASE
int wxMSLU__waccess(const wxChar
*name
, int mode
);
251 WXDLLIMPEXP_BASE
int wxMSLU__wmkdir(const wxChar
*name
);
252 WXDLLIMPEXP_BASE
int wxMSLU__wrmdir(const wxChar
*name
);
253 WXDLLIMPEXP_BASE
int wxMSLU__wstat(const wxChar
*name
, struct _stat
*buffer
);
254 WXDLLIMPEXP_BASE
int wxMSLU__wstati64(const wxChar
*name
, struct _stati64
*buffer
);
255 #endif // Windows compilers with MSLU support
257 #define wxOpen wxMSLU__wopen
259 #define wxAccess wxMSLU__waccess
260 #define wxMkDir wxMSLU__wmkdir
261 #define wxRmDir wxMSLU__wrmdir
262 #ifdef wxHAS_HUGE_FILES
263 #define wxStat wxMSLU__wstati64
265 #define wxStat wxMSLU__wstat
268 #define wxOpen _wopen
269 #define wxAccess _waccess
270 #define wxMkDir _wmkdir
271 #define wxRmDir _wrmdir
272 #ifdef wxHAS_HUGE_FILES
273 #define wxStat _wstati64
275 #define wxStat _wstat
278 #else // !wxUSE_UNICODE
284 #define wxAccess _access
285 #define wxMkDir _mkdir
287 #define wxRmDir rmdir
289 #define wxRmDir _rmdir
291 #ifdef wxHAS_HUGE_FILES
292 #define wxStat _stati64
296 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
298 // Types: Notice that Watcom is the only compiler to have a wide char
299 // version of struct stat as well as a wide char stat function variant.
300 // This was droped since OW 1.4 "for consistency across platforms".
301 #ifdef wxHAS_HUGE_FILES
302 #if wxUSE_UNICODE && wxONLY_WATCOM_EARLIER_THAN(1,4)
303 #define wxStructStat struct _wstati64
305 #define wxStructStat struct _stati64
308 #if wxUSE_UNICODE && wxONLY_WATCOM_EARLIER_THAN(1,4)
309 #define wxStructStat struct _wstat
311 #define wxStructStat struct _stat
315 // constants (unless already defined by the user code)
316 #if !defined(__BORLANDC__) && !defined(__WATCOMC__)
318 #define O_RDONLY _O_RDONLY
319 #define O_WRONLY _O_WRONLY
320 #define O_RDWR _O_RDWR
321 #define O_EXCL _O_EXCL
322 #define O_CREAT _O_CREAT
323 #define O_BINARY _O_BINARY
327 #define S_IFMT _S_IFMT
328 #define S_IFDIR _S_IFDIR
329 #define S_IFREG _S_IFREG
333 #ifdef wxHAS_HUGE_FILES
334 // wxFile is present and supports large files. Currently wxFFile
335 // doesn't have large file support with any Windows compiler (even
338 #define wxHAS_LARGE_FILES
342 // it's a private define, undefine it so that nobody gets tempted to use it
343 #undef wxHAS_HUGE_FILES
344 #else // Unix or Windows using unknown compiler, assume POSIX supported
345 typedef off_t wxFileOffset
;
347 #define wxFileOffsetFmtSpec wxLongLongFmtSpec
348 wxCOMPILE_TIME_ASSERT( sizeof(off_t
) == sizeof(wxLongLong_t
),
350 // wxFile is present and supports large files
352 #define wxHAS_LARGE_FILES
354 // wxFFile is present and supports large files
355 #if SIZEOF_LONG == 8 || defined HAVE_FSEEKO
356 #define wxHAS_LARGE_FFILES
359 #define wxFileOffsetFmtSpec _T("")
362 #define wxClose close
363 #define wxRead ::read
364 #define wxWrite ::write
365 #define wxLseek lseek
367 #define wxFsync fsync
370 #define wxMkDir mkdir
371 #define wxRmDir rmdir
373 #define wxTell(fd) lseek(fd, 0, SEEK_CUR)
375 #define wxStructStat struct stat
378 #define wxNEED_WX_UNISTD_H
380 typedef unsigned long mode_t
;
382 WXDLLIMPEXP_BASE
int wxStat( const wxChar
*file_name
, wxStructStat
*buf
);
383 WXDLLIMPEXP_BASE
int wxLstat( const wxChar
*file_name
, wxStructStat
*buf
);
384 WXDLLIMPEXP_BASE
int wxAccess( const wxChar
*pathname
, int mode
);
385 WXDLLIMPEXP_BASE
int wxOpen( const wxChar
*pathname
, int flags
, mode_t mode
);
389 #define wxLstat lstat
390 #define wxAccess access
393 #define wxHAS_NATIVE_LSTAT
397 #define wxO_BINARY O_BINARY
402 // if the platform doesn't have symlinks, define wxLstat to be the same as
403 // wxStat to avoid #ifdefs in the code using it
404 #ifndef wxHAS_NATIVE_LSTAT
405 #define wxLstat wxStat
408 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
410 // VisualAge C++ V4.0 cannot have any external linkage const decs
411 // in headers included by more than one primary source
413 extern const int wxInvalidOffset
;
415 const int wxInvalidOffset
= -1;
418 // ----------------------------------------------------------------------------
420 // ----------------------------------------------------------------------------
421 WXDLLIMPEXP_BASE
bool wxFileExists(const wxString
& filename
);
423 // does the path exist? (may have or not '/' or '\\' at the end)
424 WXDLLIMPEXP_BASE
bool wxDirExists(const wxChar
*pszPathName
);
426 WXDLLIMPEXP_BASE
bool wxIsAbsolutePath(const wxString
& filename
);
429 WXDLLIMPEXP_BASE wxChar
* wxFileNameFromPath(wxChar
*path
);
430 WXDLLIMPEXP_BASE wxString
wxFileNameFromPath(const wxString
& path
);
433 WXDLLIMPEXP_BASE wxString
wxPathOnly(const wxString
& path
);
436 WXDLLIMPEXP_BASE wxString
wxRealPath(const wxString
& path
);
438 WXDLLIMPEXP_BASE
void wxDos2UnixFilename(wxChar
*s
);
440 WXDLLIMPEXP_BASE
void wxUnix2DosFilename(wxChar
*s
);
442 // Strip the extension, in situ
443 WXDLLIMPEXP_BASE
void wxStripExtension(wxChar
*buffer
);
444 WXDLLIMPEXP_BASE
void wxStripExtension(wxString
& buffer
);
446 // Get a temporary filename
447 WXDLLIMPEXP_BASE wxChar
* wxGetTempFileName(const wxString
& prefix
, wxChar
*buf
= (wxChar
*) NULL
);
448 WXDLLIMPEXP_BASE
bool wxGetTempFileName(const wxString
& prefix
, wxString
& buf
);
450 // Expand file name (~/ and ${OPENWINHOME}/ stuff)
451 WXDLLIMPEXP_BASE wxChar
* wxExpandPath(wxChar
*dest
, const wxChar
*path
);
452 WXDLLIMPEXP_BASE
bool wxExpandPath(wxString
& dest
, const wxChar
*path
);
454 // Contract w.r.t environment (</usr/openwin/lib, OPENWHOME> -> ${OPENWINHOME}/lib)
455 // and make (if under the home tree) relative to home
456 // [caller must copy-- volatile]
457 WXDLLIMPEXP_BASE wxChar
* wxContractPath(const wxString
& filename
,
458 const wxString
& envname
= wxEmptyString
,
459 const wxString
& user
= wxEmptyString
);
461 // Destructive removal of /./ and /../ stuff
462 WXDLLIMPEXP_BASE wxChar
* wxRealPath(wxChar
*path
);
464 // Allocate a copy of the full absolute path
465 WXDLLIMPEXP_BASE wxChar
* wxCopyAbsolutePath(const wxString
& path
);
467 // Get first file name matching given wild card.
468 // Flags are reserved for future use.
471 WXDLLIMPEXP_BASE wxString
wxFindFirstFile(const wxChar
*spec
, int flags
= wxFILE
);
472 WXDLLIMPEXP_BASE wxString
wxFindNextFile();
474 // Does the pattern contain wildcards?
475 WXDLLIMPEXP_BASE
bool wxIsWild(const wxString
& pattern
);
477 // Does the pattern match the text (usually a filename)?
478 // If dot_special is true, doesn't match * against . (eliminating
479 // `hidden' dot files)
480 WXDLLIMPEXP_BASE
bool wxMatchWild(const wxString
& pattern
, const wxString
& text
, bool dot_special
= true);
482 // Concatenate two files to form third
483 WXDLLIMPEXP_BASE
bool wxConcatFiles(const wxString
& file1
, const wxString
& file2
, const wxString
& file3
);
485 // Copy file1 to file2
486 WXDLLIMPEXP_BASE
bool wxCopyFile(const wxString
& file1
, const wxString
& file2
,
487 bool overwrite
= true);
490 WXDLLIMPEXP_BASE
bool wxRemoveFile(const wxString
& file
);
493 WXDLLIMPEXP_BASE
bool wxRenameFile(const wxString
& file1
, const wxString
& file2
, bool overwrite
= true);
495 // Get current working directory.
496 #if WXWIN_COMPATIBILITY_2_6
497 // If buf is NULL, allocates space using new, else
499 // IMPORTANT NOTE getcwd is know not to work under some releases
500 // of Win32s 1.3, according to MS release notes!
501 wxDEPRECATED( WXDLLIMPEXP_BASE wxChar
* wxGetWorkingDirectory(wxChar
*buf
= (wxChar
*) NULL
, int sz
= 1000) );
502 // new and preferred version of wxGetWorkingDirectory
503 // NB: can't have the same name because of overloading ambiguity
504 #endif // WXWIN_COMPATIBILITY_2_6
505 WXDLLIMPEXP_BASE wxString
wxGetCwd();
507 // Set working directory
508 WXDLLIMPEXP_BASE
bool wxSetWorkingDirectory(const wxString
& d
);
511 WXDLLIMPEXP_BASE
bool wxMkdir(const wxString
& dir
, int perm
= 0777);
513 // Remove directory. Flags reserved for future use.
514 WXDLLIMPEXP_BASE
bool wxRmdir(const wxString
& dir
, int flags
= 0);
516 // Return the type of an open file
517 WXDLLIMPEXP_BASE wxFileKind
wxGetFileKind(int fd
);
518 WXDLLIMPEXP_BASE wxFileKind
wxGetFileKind(FILE *fp
);
520 #if WXWIN_COMPATIBILITY_2_6
521 // compatibility defines, don't use in new code
522 wxDEPRECATED( inline bool wxPathExists(const wxChar
*pszPathName
) );
523 inline bool wxPathExists(const wxChar
*pszPathName
)
525 return wxDirExists(pszPathName
);
527 #endif //WXWIN_COMPATIBILITY_2_6
529 // permissions; these functions work both on files and directories:
530 WXDLLIMPEXP_BASE
bool wxIsWritable(const wxString
&path
);
531 WXDLLIMPEXP_BASE
bool wxIsReadable(const wxString
&path
);
532 WXDLLIMPEXP_BASE
bool wxIsExecutable(const wxString
&path
);
534 // ----------------------------------------------------------------------------
535 // separators in file names
536 // ----------------------------------------------------------------------------
538 // between file name and extension
539 #define wxFILE_SEP_EXT wxT('.')
541 // between drive/volume name and the path
542 #define wxFILE_SEP_DSK wxT(':')
544 // between the path components
545 #define wxFILE_SEP_PATH_DOS wxT('\\')
546 #define wxFILE_SEP_PATH_UNIX wxT('/')
547 #define wxFILE_SEP_PATH_MAC wxT(':')
548 #define wxFILE_SEP_PATH_VMS wxT('.') // VMS also uses '[' and ']'
550 // separator in the path list (as in PATH environment variable)
551 // there is no PATH variable in Classic Mac OS so just use the
552 // semicolon (it must be different from the file name separator)
553 // NB: these are strings and not characters on purpose!
554 #define wxPATH_SEP_DOS wxT(";")
555 #define wxPATH_SEP_UNIX wxT(":")
556 #define wxPATH_SEP_MAC wxT(";")
558 // platform independent versions
559 #if defined(__UNIX__) && !defined(__OS2__)
560 // CYGWIN also uses UNIX settings
561 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_UNIX
562 #define wxPATH_SEP wxPATH_SEP_UNIX
563 #elif defined(__MAC__)
564 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_MAC
565 #define wxPATH_SEP wxPATH_SEP_MAC
566 #else // Windows and OS/2
567 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_DOS
568 #define wxPATH_SEP wxPATH_SEP_DOS
569 #endif // Unix/Windows
571 // this is useful for wxString::IsSameAs(): to compare two file names use
572 // filename1.IsSameAs(filename2, wxARE_FILENAMES_CASE_SENSITIVE)
573 #if defined(__UNIX__) && !defined(__DARWIN__) && !defined(__OS2__)
574 #define wxARE_FILENAMES_CASE_SENSITIVE true
575 #else // Windows, Mac OS and OS/2
576 #define wxARE_FILENAMES_CASE_SENSITIVE false
577 #endif // Unix/Windows
579 // is the char a path separator?
580 inline bool wxIsPathSeparator(wxChar c
)
582 // under DOS/Windows we should understand both Unix and DOS file separators
583 #if ( defined(__UNIX__) && !defined(__OS2__) )|| defined(__MAC__)
584 return c
== wxFILE_SEP_PATH
;
586 return c
== wxFILE_SEP_PATH_DOS
|| c
== wxFILE_SEP_PATH_UNIX
;
590 // does the string ends with path separator?
591 WXDLLIMPEXP_BASE
bool wxEndsWithPathSeparator(const wxChar
*pszFileName
);
593 // split the full path into path (including drive for DOS), name and extension
594 // (understands both '/' and '\\')
595 WXDLLIMPEXP_BASE
void wxSplitPath(const wxChar
*pszFileName
,
600 // find a file in a list of directories, returns false if not found
601 WXDLLIMPEXP_BASE
bool wxFindFileInPath(wxString
*pStr
, const wxChar
*pszPath
, const wxChar
*pszFile
);
603 // Get the OS directory if appropriate (such as the Windows directory).
604 // On non-Windows platform, probably just return the empty string.
605 WXDLLIMPEXP_BASE wxString
wxGetOSDirectory();
609 // Get file modification time
610 WXDLLIMPEXP_BASE
time_t wxFileModificationTime(const wxString
& filename
);
612 #endif // wxUSE_DATETIME
614 // Parses the wildCard, returning the number of filters.
615 // Returns 0 if none or if there's a problem,
616 // The arrays will contain an equal number of items found before the error.
617 // wildCard is in the form:
618 // "All files (*)|*|Image Files (*.jpeg *.png)|*.jpg;*.png"
619 WXDLLIMPEXP_BASE
int wxParseCommonDialogsFilter(const wxString
& wildCard
, wxArrayString
& descriptions
, wxArrayString
& filters
);
621 // ----------------------------------------------------------------------------
623 // ----------------------------------------------------------------------------
627 // set umask to the given value in ctor and reset it to the old one in dtor
628 class WXDLLIMPEXP_BASE wxUmaskChanger
631 // change the umask to the given one if it is not -1: this allows to write
632 // the same code whether you really want to change umask or not, as is in
633 // wxFileConfig::Flush() for example
634 wxUmaskChanger(int umaskNew
)
636 m_umaskOld
= umaskNew
== -1 ? -1 : (int)umask((mode_t
)umaskNew
);
641 if ( m_umaskOld
!= -1 )
642 umask((mode_t
)m_umaskOld
);
649 // this macro expands to an "anonymous" wxUmaskChanger object under Unix and
651 #define wxCHANGE_UMASK(m) wxUmaskChanger wxMAKE_UNIQUE_NAME(umaskChanger_)(m)
655 #define wxCHANGE_UMASK(m)
657 #endif // __UNIX__/!__UNIX__
661 class WXDLLIMPEXP_BASE wxPathList
: public wxArrayString
665 wxPathList(const wxArrayString
&arr
)
668 // Adds all paths in environment variable
669 void AddEnvList(const wxString
& envVariable
);
671 // Adds given path to this list
672 bool Add(const wxString
& path
);
673 void Add(const wxArrayString
&paths
);
675 // Find the first full path for which the file exists
676 wxString
FindValidPath(const wxString
& filename
) const;
678 // Find the first full path for which the file exists; ensure it's an
679 // absolute path that gets returned.
680 wxString
FindAbsoluteValidPath(const wxString
& filename
) const;
682 // Given full path and filename, add path to list
683 bool EnsureFileAccessible(const wxString
& path
);
685 #if WXWIN_COMPATIBILITY_2_6
686 // Returns true if the path is in the list
687 wxDEPRECATED( bool Member(const wxString
& path
) const );
691 #endif // _WX_FILEFN_H_