1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: File- and directory-related functions
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "filefn.h"
20 #include "wx/arrstr.h"
23 #include "wx/msw/wince/time.h"
24 #include "wx/msw/private.h"
31 #elif !defined(__MWERKS__)
32 #include <sys/types.h>
36 #include <sys/types.h>
48 // need to check for __OS2__ first since currently both
49 // __OS2__ and __UNIX__ are defined.
51 #include "wx/os2/private.h"
59 #elif defined(__UNIX__)
64 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
65 #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) && !defined(__SALFORDC__) && !defined(__WXWINCE__) && !defined(__CYGWIN__)
70 #endif // native Win compiler
84 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
85 // this (3.1 I believe) and how to test for it.
86 // If this works for Borland 4.0 as well, then no worries.
96 #include <fcntl.h> // O_RDONLY &c
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
106 #if !defined(__WXMAC__) || defined(__UNIX__) || defined(__MACH__)
107 #include <sys/types.h>
113 #if (defined(__VISUALC__) && !defined(__WXWINCE__)) || ( defined(__MWERKS__) && defined( __INTEL__) )
114 typedef _off_t off_t
;
115 #elif defined(__SYMANTEC__)
117 #elif defined(__MWERKS__) && !defined(__INTEL__) && !defined(__MACH__)
131 wxFILE_KIND_DISK
, // a file supporting seeking to arbitrary offsets
132 wxFILE_KIND_TERMINAL
, // a tty
133 wxFILE_KIND_PIPE
// a pipe
136 // ----------------------------------------------------------------------------
137 // declare our versions of low level file functions: some compilers prepend
138 // underscores to the usual names, some also have Unicode versions of them
139 // ----------------------------------------------------------------------------
141 // Wrappers around Win32 api functions like CreateFile, ReadFile and such
142 // Implemented in filefnwce.cpp
143 #if defined( __WXWINCE__)
144 typedef __int64 wxFileOffset
;
145 #define wxFileOffsetFmtSpec _("I64")
146 int wxOpen(const wxChar
*filename
, int oflag
, int WXUNUSED(pmode
));
147 int wxAccess(const wxChar
*name
, int WXUNUSED(how
));
149 int wxFsync(int WXUNUSED(fd
));
150 int wxRead(int fd
, void *buf
, unsigned int count
);
151 int wxWrite(int fd
, const void *buf
, unsigned int count
);
153 wxFileOffset
wxSeek(int fd
, wxFileOffset offset
, int origin
);
154 #define wxLSeek wxSeek
155 wxFileOffset
wxTell(int fd
);
157 // always Unicode under WinCE
158 #define wxMkDir _wmkdir
159 #define wxRmDir _wrmdir
160 #define wxStat _wstat
161 #define wxStructStat struct _stat
163 // Microsoft compiler loves underscores, feed them to it
164 #elif defined( __VISUALC__ ) \
165 || ( defined(__MINGW32__) && !defined(__WINE__) && wxCHECK_W32API_VERSION( 0, 5 ) ) \
166 || ( defined(__MWERKS__) && defined(__WXMSW__) ) \
167 || ( defined(__DMC__) && defined(__WXMSW__) ) \
168 || ( defined(__WATCOMC__) && defined(__WXMSW__) )
170 // detect compilers which have support for huge files (currently only
171 // Digital Mars doesn't)
173 #include "wx/msw/private.h"
176 #undef wxHAS_HUGE_FILES
177 #if defined(__MINGW32__)
178 #define wxHAS_HUGE_FILES 1
179 #elif defined(__MWERKS__)
180 #define wxHAS_HUGE_FILES 0
181 #elif defined(__DMC__)
182 #define wxHAS_HUGE_FILES 0
183 #elif ((_INTEGRAL_MAX_BITS >= 64) || defined(_LARGE_FILES))
184 #define wxHAS_HUGE_FILES 1
186 #define wxHAS_HUGE_FILES 0
190 #if defined(__BORLANDC__) || defined(__WATCOMC__)
195 typedef wxLongLong_t wxFileOffset
;
196 #define wxFileOffsetFmtSpec wxLongLongFmtSpec
198 typedef off_t wxFileOffset
;
201 #define wxClose _close
203 #if defined(__MWERKS__)
204 #if __MSL__ >= 0x6000
205 #define wxRead(fd, buf, nCount) _read(fd, (void *)buf, nCount)
206 #define wxWrite(fd, buf, nCount) _write(fd, (void *)buf, nCount)
208 #define wxRead(fd, buf, nCount)\
209 _read(fd, (const char *)buf, nCount)
210 #define wxWrite(fd, buf, nCount)\
211 _write(fd, (const char *)buf, nCount)
214 #if defined(__DMC__) || defined(__WATCOMC__)
215 #define wxRead ::read
216 #define wxWrite ::write
219 #define wxWrite _write
223 #define wxSeek _lseeki64
224 #define wxLseek _lseeki64
225 #define wxTell _telli64
227 #define wxSeek _lseek
228 #define wxLseek _lseek
231 #define wxFsync _commit
232 #if defined(__WATCOMC__)
239 #if wxUSE_UNICODE_MSLU
240 #define wxOpen wxMSLU__wopen
242 #define wxAccess wxMSLU__waccess
243 #define wxMkDir wxMSLU__wmkdir
244 #define wxRmDir wxMSLU__wrmdir
246 #define wxStat wxMSLU__wstati64
248 #define wxStat wxMSLU__wstat
251 #define wxOpen _wopen
252 #define wxAccess _waccess
253 #define wxMkDir _wmkdir
254 #define wxRmDir _wrmdir
256 #define wxStat _wstati64
258 #define wxStat _wstat
261 #else // !wxUSE_UNICODE
267 #define wxAccess _access
268 #define wxMkDir _mkdir
270 #define wxRmDir rmdir
272 #define wxRmDir _rmdir
275 #define wxStat _stati64
281 // types: notice that Watcom is the only compiler to have a wide char
282 // version of struct stat as well as a wide char stat function variant
284 #if wxUSE_UNICODE && defined(__WATCOMC__)
285 #define wxStructStat struct _wstati64
287 #define wxStructStat struct _stati64
290 #if wxUSE_UNICODE && defined(__WATCOMC__)
291 #define wxStructStat struct _wstat
293 #define wxStructStat struct _stat
297 // constants (unless already defined by the user code)
298 #if !defined(__BORLANDC__) && !defined(__WATCOMC__) && !defined(__WXPALMOS__)
300 #define O_RDONLY _O_RDONLY
301 #define O_WRONLY _O_WRONLY
302 #define O_RDWR _O_RDWR
303 #define O_EXCL _O_EXCL
304 #define O_CREAT _O_CREAT
305 #define O_BINARY _O_BINARY
309 #define S_IFMT _S_IFMT
310 #define S_IFDIR _S_IFDIR
311 #define S_IFREG _S_IFREG
315 // It's a private define, undefine it so nobody gets tempted to use it
316 #undef wxHAS_HUGE_FILES
317 #else // Unix platforms using configure
318 typedef off_t wxFileOffset
;
320 #define wxFileOffsetFmtSpec wxLongLongFmtSpec
321 wxCOMPILE_TIME_ASSERT( sizeof(off_t
) == sizeof(wxLongLong_t
),
324 #define wxFileOffsetFmtSpec _T("")
327 #define wxClose close
328 #define wxRead ::read
329 #define wxWrite ::write
330 #define wxLseek lseek
332 #define wxFsync commit
335 #define wxMkDir mkdir
336 #define wxRmDir rmdir
338 #define wxTell(fd) lseek(fd, 0, SEEK_CUR)
340 #define wxStructStat struct stat
343 #define wxNEED_WX_UNISTD_H
345 typedef unsigned long mode_t
;
347 WXDLLIMPEXP_BASE
int wxStat( const wxChar
*file_name
, wxStructStat
*buf
);
348 WXDLLIMPEXP_BASE
int wxAccess( const wxChar
*pathname
, int mode
);
349 WXDLLIMPEXP_BASE
int wxOpen( const wxChar
*pathname
, int flags
, mode_t mode
);
353 #define wxAccess access
357 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
359 // VisualAge C++ V4.0 cannot have any external linkage const decs
360 // in headers included by more than one primary source
362 extern const int wxInvalidOffset
;
364 const int wxInvalidOffset
= -1;
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
370 WXDLLIMPEXP_BASE
bool wxFileExists(const wxString
& filename
);
372 // does the path exist? (may have or not '/' or '\\' at the end)
373 WXDLLIMPEXP_BASE
bool wxDirExists(const wxChar
*pszPathName
);
375 WXDLLIMPEXP_BASE
bool wxIsAbsolutePath(const wxString
& filename
);
378 WXDLLIMPEXP_BASE wxChar
* wxFileNameFromPath(wxChar
*path
);
379 WXDLLIMPEXP_BASE wxString
wxFileNameFromPath(const wxString
& path
);
382 WXDLLIMPEXP_BASE wxString
wxPathOnly(const wxString
& path
);
385 WXDLLIMPEXP_BASE wxString
wxRealPath(const wxString
& path
);
387 WXDLLIMPEXP_BASE
void wxDos2UnixFilename(wxChar
*s
);
389 WXDLLIMPEXP_BASE
void wxUnix2DosFilename(wxChar
*s
);
391 // Strip the extension, in situ
392 WXDLLIMPEXP_BASE
void wxStripExtension(wxChar
*buffer
);
393 WXDLLIMPEXP_BASE
void wxStripExtension(wxString
& buffer
);
395 // Get a temporary filename
396 WXDLLIMPEXP_BASE wxChar
* wxGetTempFileName(const wxString
& prefix
, wxChar
*buf
= (wxChar
*) NULL
);
397 WXDLLIMPEXP_BASE
bool wxGetTempFileName(const wxString
& prefix
, wxString
& buf
);
399 // Expand file name (~/ and ${OPENWINHOME}/ stuff)
400 WXDLLIMPEXP_BASE wxChar
* wxExpandPath(wxChar
*dest
, const wxChar
*path
);
401 WXDLLIMPEXP_BASE
bool wxExpandPath(wxString
& dest
, const wxChar
*path
);
403 // Contract w.r.t environment (</usr/openwin/lib, OPENWHOME> -> ${OPENWINHOME}/lib)
404 // and make (if under the home tree) relative to home
405 // [caller must copy-- volatile]
406 WXDLLIMPEXP_BASE wxChar
* wxContractPath(const wxString
& filename
,
407 const wxString
& envname
= wxEmptyString
,
408 const wxString
& user
= wxEmptyString
);
410 // Destructive removal of /./ and /../ stuff
411 WXDLLIMPEXP_BASE wxChar
* wxRealPath(wxChar
*path
);
413 // Allocate a copy of the full absolute path
414 WXDLLIMPEXP_BASE wxChar
* wxCopyAbsolutePath(const wxString
& path
);
416 // Get first file name matching given wild card.
417 // Flags are reserved for future use.
420 WXDLLIMPEXP_BASE wxString
wxFindFirstFile(const wxChar
*spec
, int flags
= wxFILE
);
421 WXDLLIMPEXP_BASE wxString
wxFindNextFile();
423 // Does the pattern contain wildcards?
424 WXDLLIMPEXP_BASE
bool wxIsWild(const wxString
& pattern
);
426 // Does the pattern match the text (usually a filename)?
427 // If dot_special is true, doesn't match * against . (eliminating
428 // `hidden' dot files)
429 WXDLLIMPEXP_BASE
bool wxMatchWild(const wxString
& pattern
, const wxString
& text
, bool dot_special
= true);
431 // Concatenate two files to form third
432 WXDLLIMPEXP_BASE
bool wxConcatFiles(const wxString
& file1
, const wxString
& file2
, const wxString
& file3
);
434 // Copy file1 to file2
435 WXDLLIMPEXP_BASE
bool wxCopyFile(const wxString
& file1
, const wxString
& file2
,
436 bool overwrite
= true);
439 WXDLLIMPEXP_BASE
bool wxRemoveFile(const wxString
& file
);
442 WXDLLIMPEXP_BASE
bool wxRenameFile(const wxString
& file1
, const wxString
& file2
);
444 // Get current working directory.
445 // If buf is NULL, allocates space using new, else
447 // IMPORTANT NOTE getcwd is know not to work under some releases
448 // of Win32s 1.3, according to MS release notes!
449 WXDLLIMPEXP_BASE wxChar
* wxGetWorkingDirectory(wxChar
*buf
= (wxChar
*) NULL
, int sz
= 1000);
450 // new and preferred version of wxGetWorkingDirectory
451 // NB: can't have the same name because of overloading ambiguity
452 WXDLLIMPEXP_BASE wxString
wxGetCwd();
454 // Set working directory
455 WXDLLIMPEXP_BASE
bool wxSetWorkingDirectory(const wxString
& d
);
458 WXDLLIMPEXP_BASE
bool wxMkdir(const wxString
& dir
, int perm
= 0777);
460 // Remove directory. Flags reserved for future use.
461 WXDLLIMPEXP_BASE
bool wxRmdir(const wxString
& dir
, int flags
= 0);
463 // Return the type of an open file
464 WXDLLIMPEXP_BASE wxFileKind
wxGetFileKind(int fd
);
465 WXDLLIMPEXP_BASE wxFileKind
wxGetFileKind(FILE *fp
);
467 // compatibility defines, don't use in new code
468 // consider removal droping 2.4 compatibility
469 // #if WXWIN_COMPATIBILITY_2_4
470 wxDEPRECATED( inline bool wxPathExists(const wxChar
*pszPathName
) );
471 inline bool wxPathExists(const wxChar
*pszPathName
)
473 return wxDirExists(pszPathName
);
475 // #endif //WXWIN_COMPATIBILITY_2_4
477 // ----------------------------------------------------------------------------
478 // separators in file names
479 // ----------------------------------------------------------------------------
481 // between file name and extension
482 #define wxFILE_SEP_EXT wxT('.')
484 // between drive/volume name and the path
485 #define wxFILE_SEP_DSK wxT(':')
487 // between the path components
488 #define wxFILE_SEP_PATH_DOS wxT('\\')
489 #define wxFILE_SEP_PATH_UNIX wxT('/')
490 #define wxFILE_SEP_PATH_MAC wxT(':')
491 #define wxFILE_SEP_PATH_VMS wxT('.') // VMS also uses '[' and ']'
493 // separator in the path list (as in PATH environment variable)
494 // there is no PATH variable in Classic Mac OS so just use the
495 // semicolon (it must be different from the file name separator)
496 // NB: these are strings and not characters on purpose!
497 #define wxPATH_SEP_DOS wxT(";")
498 #define wxPATH_SEP_UNIX wxT(":")
499 #define wxPATH_SEP_MAC wxT(";")
501 // platform independent versions
502 #if defined(__UNIX__) && !defined(__OS2__)
503 // CYGWIN also uses UNIX settings
504 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_UNIX
505 #define wxPATH_SEP wxPATH_SEP_UNIX
506 #elif defined(__MAC__)
507 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_MAC
508 #define wxPATH_SEP wxPATH_SEP_MAC
509 #else // Windows and OS/2
510 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_DOS
511 #define wxPATH_SEP wxPATH_SEP_DOS
512 #endif // Unix/Windows
514 // this is useful for wxString::IsSameAs(): to compare two file names use
515 // filename1.IsSameAs(filename2, wxARE_FILENAMES_CASE_SENSITIVE)
516 #if defined(__UNIX__) && !defined(__DARWIN__) && !defined(__OS2__)
517 #define wxARE_FILENAMES_CASE_SENSITIVE true
518 #else // Windows, Mac OS and OS/2
519 #define wxARE_FILENAMES_CASE_SENSITIVE false
520 #endif // Unix/Windows
522 // is the char a path separator?
523 inline bool wxIsPathSeparator(wxChar c
)
525 // under DOS/Windows we should understand both Unix and DOS file separators
526 #if ( defined(__UNIX__) && !defined(__OS2__) )|| defined(__MAC__)
527 return c
== wxFILE_SEP_PATH
;
529 return c
== wxFILE_SEP_PATH_DOS
|| c
== wxFILE_SEP_PATH_UNIX
;
533 // does the string ends with path separator?
534 WXDLLIMPEXP_BASE
bool wxEndsWithPathSeparator(const wxChar
*pszFileName
);
536 // split the full path into path (including drive for DOS), name and extension
537 // (understands both '/' and '\\')
538 WXDLLIMPEXP_BASE
void wxSplitPath(const wxChar
*pszFileName
,
543 // find a file in a list of directories, returns false if not found
544 WXDLLIMPEXP_BASE
bool wxFindFileInPath(wxString
*pStr
, const wxChar
*pszPath
, const wxChar
*pszFile
);
546 // Get the OS directory if appropriate (such as the Windows directory).
547 // On non-Windows platform, probably just return the empty string.
548 WXDLLIMPEXP_BASE wxString
wxGetOSDirectory();
550 // Get file modification time
551 WXDLLIMPEXP_BASE
time_t wxFileModificationTime(const wxString
& filename
);
553 // Parses the wildCard, returning the number of filters.
554 // Returns 0 if none or if there's a problem,
555 // The arrays will contain an equal number of items found before the error.
556 // wildCard is in the form:
557 // "All files (*)|*|Image Files (*.jpeg *.png)|*.jpg;*.png"
558 WXDLLIMPEXP_BASE
int wxParseCommonDialogsFilter(const wxString
& wildCard
, wxArrayString
& descriptions
, wxArrayString
& filters
);
560 // ----------------------------------------------------------------------------
562 // ----------------------------------------------------------------------------
566 // set umask to the given value in ctor and reset it to the old one in dtor
567 class WXDLLIMPEXP_BASE wxUmaskChanger
570 // change the umask to the given one if it is not -1: this allows to write
571 // the same code whether you really want to change umask or not, as is in
572 // wxFileConfig::Flush() for example
573 wxUmaskChanger(int umaskNew
)
575 m_umaskOld
= umaskNew
== -1 ? -1 : (int)umask((mode_t
)umaskNew
);
580 if ( m_umaskOld
!= -1 )
581 umask((mode_t
)m_umaskOld
);
588 // this macro expands to an "anonymous" wxUmaskChanger object under Unix and
590 #define wxCHANGE_UMASK(m) wxUmaskChanger wxMAKE_UNIQUE_NAME(umaskChanger_)(m)
594 #define wxCHANGE_UMASK(m)
596 #endif // __UNIX__/!__UNIX__
600 class WXDLLIMPEXP_BASE wxPathList
: public wxStringList
603 // avoid GCC warning about virtual functions w/o virtual dtor
604 virtual ~wxPathList() {}
606 // Adds all paths in environment variable
607 void AddEnvList(const wxString
& envVariable
);
609 void Add(const wxString
& path
);
610 // Find the first full path for which the file exists
611 wxString
FindValidPath(const wxString
& filename
);
612 // Find the first full path for which the file exists; ensure it's an
613 // absolute path that gets returned.
614 wxString
FindAbsoluteValidPath(const wxString
& filename
);
615 // Given full path and filename, add path to list
616 void EnsureFileAccessible(const wxString
& path
);
617 // Returns true if the path is in the list
618 bool Member(const wxString
& path
);
621 // DECLARE_DYNAMIC_CLASS(wxPathList)