define wxFsync() as fsync() under Unix not as (not existing) commit()
[wxWidgets.git] / include / wx / filefn.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: filefn.h
3 // Purpose: File- and directory-related functions
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _FILEFN_H_
13 #define _FILEFN_H_
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "filefn.h"
17 #endif
18
19 #include "wx/list.h"
20 #include "wx/arrstr.h"
21
22 #ifdef __WXWINCE__
23 #include "wx/msw/wince/time.h"
24 #include "wx/msw/private.h"
25 #else
26 #include <time.h>
27 #endif
28
29 #ifdef __WXWINCE__
30 // Nothing
31 #elif !defined(__MWERKS__)
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #else
35 #ifdef __MACH__
36 #include <sys/types.h>
37 #include <utime.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #else
41 #include <stat.h>
42 #include <unistd.h>
43 #include <unix.h>
44 #endif
45 #endif
46
47 #ifdef __OS2__
48 // need to check for __OS2__ first since currently both
49 // __OS2__ and __UNIX__ are defined.
50 #include <process.h>
51 #include "wx/os2/private.h"
52 #ifdef __WATCOMC__
53 #include <direct.h>
54 #endif
55 #include <io.h>
56 #ifdef __EMX__
57 #include <unistd.h>
58 #endif
59 #elif defined(__UNIX__)
60 #include <unistd.h>
61 #include <dirent.h>
62 #endif
63
64 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
65 #if !defined( __GNUWIN32__ ) && !defined( __MWERKS__ ) && !defined(__SALFORDC__) && !defined(__WXWINCE__) && !defined(__CYGWIN__)
66 #include <direct.h>
67 #include <dos.h>
68 #include <io.h>
69 #endif // __WINDOWS__
70 #endif // native Win compiler
71
72 #if defined(__DOS__)
73 #ifdef __WATCOMC__
74 #include <direct.h>
75 #include <dos.h>
76 #include <io.h>
77 #endif
78 #ifdef __DJGPP__
79 #include <io.h>
80 #include <unistd.h>
81 #endif
82 #endif
83
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.
87 #include <dir.h>
88 #endif
89
90 #ifdef __SALFORDC__
91 #include <dir.h>
92 #include <unix.h>
93 #endif
94
95 #ifndef __WXWINCE__
96 #include <fcntl.h> // O_RDONLY &c
97 #endif
98 // ----------------------------------------------------------------------------
99 // constants
100 // ----------------------------------------------------------------------------
101
102 #ifdef __WXWINCE__
103 typedef long off_t;
104 #else
105 // define off_t
106 #if !defined(__WXMAC__) || defined(__UNIX__) || defined(__MACH__)
107 #include <sys/types.h>
108 #else
109 typedef long off_t;
110 #endif
111 #endif
112
113 #if (defined(__VISUALC__) && !defined(__WXWINCE__)) || ( defined(__MWERKS__) && defined( __INTEL__) )
114 typedef _off_t off_t;
115 #elif defined(__SYMANTEC__)
116 typedef long off_t;
117 #elif defined(__MWERKS__) && !defined(__INTEL__) && !defined(__MACH__)
118 typedef long off_t;
119 #endif
120
121 enum wxSeekMode
122 {
123 wxFromStart,
124 wxFromCurrent,
125 wxFromEnd
126 };
127
128 enum wxFileKind
129 {
130 wxFILE_KIND_UNKNOWN,
131 wxFILE_KIND_DISK, // a file supporting seeking to arbitrary offsets
132 wxFILE_KIND_TERMINAL, // a tty
133 wxFILE_KIND_PIPE // a pipe
134 };
135
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 // ----------------------------------------------------------------------------
140
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));
148 int wxClose(int fd);
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);
152 int wxEof(int fd);
153 wxFileOffset wxSeek(int fd, wxFileOffset offset, int origin);
154 #define wxLSeek wxSeek
155 wxFileOffset wxTell(int fd);
156
157 // always Unicode under WinCE
158 #define wxMkDir _wmkdir
159 #define wxRmDir _wrmdir
160 #define wxStat _wstat
161 #define wxStructStat struct _stat
162
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__) )
169
170 // detect compilers which have support for huge files (currently only
171 // Digital Mars doesn't)
172 #ifndef __WXPALMOS__
173 #include "wx/msw/private.h"
174 #endif
175
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
185 #else
186 #define wxHAS_HUGE_FILES 0
187 #endif
188
189 // functions
190 #if defined(__BORLANDC__) || defined(__WATCOMC__)
191 #define _tell tell
192 #endif
193
194 #if wxHAS_HUGE_FILES
195 typedef wxLongLong_t wxFileOffset;
196 #define wxFileOffsetFmtSpec wxLongLongFmtSpec
197 #else
198 typedef off_t wxFileOffset;
199 #endif
200
201 #define wxClose _close
202
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)
207 #else
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)
212 #endif
213 #else
214 #if defined(__DMC__) || defined(__WATCOMC__)
215 #define wxRead ::read
216 #define wxWrite ::write
217 #else
218 #define wxRead _read
219 #define wxWrite _write
220 #endif
221 #endif
222 #if wxHAS_HUGE_FILES
223 #define wxSeek _lseeki64
224 #define wxLseek _lseeki64
225 #define wxTell _telli64
226 #else
227 #define wxSeek _lseek
228 #define wxLseek _lseek
229 #define wxTell _tell
230 #endif
231 #define wxFsync _commit
232 #if defined(__WATCOMC__)
233 #define wxEof ::eof
234 #else
235 #define wxEof _eof
236 #endif
237
238 #if wxUSE_UNICODE
239 #if wxUSE_UNICODE_MSLU
240 #define wxOpen wxMSLU__wopen
241
242 #define wxAccess wxMSLU__waccess
243 #define wxMkDir wxMSLU__wmkdir
244 #define wxRmDir wxMSLU__wrmdir
245 #if wxHAS_HUGE_FILES
246 #define wxStat wxMSLU__wstati64
247 #else
248 #define wxStat wxMSLU__wstat
249 #endif
250 #else
251 #define wxOpen _wopen
252 #define wxAccess _waccess
253 #define wxMkDir _wmkdir
254 #define wxRmDir _wrmdir
255 #if wxHAS_HUGE_FILES
256 #define wxStat _wstati64
257 #else
258 #define wxStat _wstat
259 #endif
260 #endif
261 #else // !wxUSE_UNICODE
262 #ifdef __BORLANDC__
263 #define wxOpen open
264 #else
265 #define wxOpen _open
266 #endif
267 #define wxAccess _access
268 #define wxMkDir _mkdir
269 #ifdef __WATCOMC__
270 #define wxRmDir rmdir
271 #else
272 #define wxRmDir _rmdir
273 #endif
274 #if wxHAS_HUGE_FILES
275 #define wxStat _stati64
276 #else
277 #define wxStat _stat
278 #endif
279 #endif
280
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
283 #if wxHAS_HUGE_FILES
284 #if wxUSE_UNICODE && defined(__WATCOMC__)
285 #define wxStructStat struct _wstati64
286 #else
287 #define wxStructStat struct _stati64
288 #endif
289 #else
290 #if wxUSE_UNICODE && defined(__WATCOMC__)
291 #define wxStructStat struct _wstat
292 #else
293 #define wxStructStat struct _stat
294 #endif
295 #endif
296
297 // constants (unless already defined by the user code)
298 #if !defined(__BORLANDC__) && !defined(__WATCOMC__) && !defined(__WXPALMOS__)
299 #ifndef O_RDONLY
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
306 #endif
307
308 #ifndef S_IFMT
309 #define S_IFMT _S_IFMT
310 #define S_IFDIR _S_IFDIR
311 #define S_IFREG _S_IFREG
312 #endif
313 #endif
314
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;
319 #ifdef _LARGE_FILES
320 #define wxFileOffsetFmtSpec wxLongLongFmtSpec
321 wxCOMPILE_TIME_ASSERT( sizeof(off_t) == sizeof(wxLongLong_t),
322 BadFileSizeType );
323 #else
324 #define wxFileOffsetFmtSpec _T("")
325 #endif
326 // functions
327 #define wxClose close
328 #define wxRead ::read
329 #define wxWrite ::write
330 #define wxLseek lseek
331 #define wxSeek lseek
332 #define wxFsync fsync
333 #define wxEof eof
334
335 #define wxMkDir mkdir
336 #define wxRmDir rmdir
337
338 #define wxTell(fd) lseek(fd, 0, SEEK_CUR)
339
340 #define wxStructStat struct stat
341
342 #if wxUSE_UNICODE
343 #define wxNEED_WX_UNISTD_H
344 #if defined(__DMC__)
345 typedef unsigned long mode_t;
346 #endif
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 );
350 #else
351 #define wxOpen open
352 #define wxStat stat
353 #define wxAccess access
354 #endif
355 #endif // platforms
356
357 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
358 //
359 // VisualAge C++ V4.0 cannot have any external linkage const decs
360 // in headers included by more than one primary source
361 //
362 extern const int wxInvalidOffset;
363 #else
364 const int wxInvalidOffset = -1;
365 #endif
366
367 // ----------------------------------------------------------------------------
368 // functions
369 // ----------------------------------------------------------------------------
370 WXDLLIMPEXP_BASE bool wxFileExists(const wxString& filename);
371
372 // does the path exist? (may have or not '/' or '\\' at the end)
373 WXDLLIMPEXP_BASE bool wxDirExists(const wxChar *pszPathName);
374
375 WXDLLIMPEXP_BASE bool wxIsAbsolutePath(const wxString& filename);
376
377 // Get filename
378 WXDLLIMPEXP_BASE wxChar* wxFileNameFromPath(wxChar *path);
379 WXDLLIMPEXP_BASE wxString wxFileNameFromPath(const wxString& path);
380
381 // Get directory
382 WXDLLIMPEXP_BASE wxString wxPathOnly(const wxString& path);
383
384 // wxString version
385 WXDLLIMPEXP_BASE wxString wxRealPath(const wxString& path);
386
387 WXDLLIMPEXP_BASE void wxDos2UnixFilename(wxChar *s);
388
389 WXDLLIMPEXP_BASE void wxUnix2DosFilename(wxChar *s);
390
391 // Strip the extension, in situ
392 WXDLLIMPEXP_BASE void wxStripExtension(wxChar *buffer);
393 WXDLLIMPEXP_BASE void wxStripExtension(wxString& buffer);
394
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);
398
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);
402
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);
409
410 // Destructive removal of /./ and /../ stuff
411 WXDLLIMPEXP_BASE wxChar* wxRealPath(wxChar *path);
412
413 // Allocate a copy of the full absolute path
414 WXDLLIMPEXP_BASE wxChar* wxCopyAbsolutePath(const wxString& path);
415
416 // Get first file name matching given wild card.
417 // Flags are reserved for future use.
418 #define wxFILE 1
419 #define wxDIR 2
420 WXDLLIMPEXP_BASE wxString wxFindFirstFile(const wxChar *spec, int flags = wxFILE);
421 WXDLLIMPEXP_BASE wxString wxFindNextFile();
422
423 // Does the pattern contain wildcards?
424 WXDLLIMPEXP_BASE bool wxIsWild(const wxString& pattern);
425
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);
430
431 // Concatenate two files to form third
432 WXDLLIMPEXP_BASE bool wxConcatFiles(const wxString& file1, const wxString& file2, const wxString& file3);
433
434 // Copy file1 to file2
435 WXDLLIMPEXP_BASE bool wxCopyFile(const wxString& file1, const wxString& file2,
436 bool overwrite = true);
437
438 // Remove file
439 WXDLLIMPEXP_BASE bool wxRemoveFile(const wxString& file);
440
441 // Rename file
442 WXDLLIMPEXP_BASE bool wxRenameFile(const wxString& file1, const wxString& file2);
443
444 // Get current working directory.
445 // If buf is NULL, allocates space using new, else
446 // copies into buf.
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();
453
454 // Set working directory
455 WXDLLIMPEXP_BASE bool wxSetWorkingDirectory(const wxString& d);
456
457 // Make directory
458 WXDLLIMPEXP_BASE bool wxMkdir(const wxString& dir, int perm = 0777);
459
460 // Remove directory. Flags reserved for future use.
461 WXDLLIMPEXP_BASE bool wxRmdir(const wxString& dir, int flags = 0);
462
463 // Return the type of an open file
464 WXDLLIMPEXP_BASE wxFileKind wxGetFileKind(int fd);
465 WXDLLIMPEXP_BASE wxFileKind wxGetFileKind(FILE *fp);
466
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)
472 {
473 return wxDirExists(pszPathName);
474 }
475 // #endif //WXWIN_COMPATIBILITY_2_4
476
477 // ----------------------------------------------------------------------------
478 // separators in file names
479 // ----------------------------------------------------------------------------
480
481 // between file name and extension
482 #define wxFILE_SEP_EXT wxT('.')
483
484 // between drive/volume name and the path
485 #define wxFILE_SEP_DSK wxT(':')
486
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 ']'
492
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(";")
500
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
513
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
521
522 // is the char a path separator?
523 inline bool wxIsPathSeparator(wxChar c)
524 {
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;
528 #else
529 return c == wxFILE_SEP_PATH_DOS || c == wxFILE_SEP_PATH_UNIX;
530 #endif
531 }
532
533 // does the string ends with path separator?
534 WXDLLIMPEXP_BASE bool wxEndsWithPathSeparator(const wxChar *pszFileName);
535
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,
539 wxString *pstrPath,
540 wxString *pstrName,
541 wxString *pstrExt);
542
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);
545
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();
549
550 // Get file modification time
551 WXDLLIMPEXP_BASE time_t wxFileModificationTime(const wxString& filename);
552
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);
559
560 // ----------------------------------------------------------------------------
561 // classes
562 // ----------------------------------------------------------------------------
563
564 #ifdef __UNIX__
565
566 // set umask to the given value in ctor and reset it to the old one in dtor
567 class WXDLLIMPEXP_BASE wxUmaskChanger
568 {
569 public:
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)
574 {
575 m_umaskOld = umaskNew == -1 ? -1 : (int)umask((mode_t)umaskNew);
576 }
577
578 ~wxUmaskChanger()
579 {
580 if ( m_umaskOld != -1 )
581 umask((mode_t)m_umaskOld);
582 }
583
584 private:
585 int m_umaskOld;
586 };
587
588 // this macro expands to an "anonymous" wxUmaskChanger object under Unix and
589 // nothing elsewhere
590 #define wxCHANGE_UMASK(m) wxUmaskChanger wxMAKE_UNIQUE_NAME(umaskChanger_)(m)
591
592 #else // !__UNIX__
593
594 #define wxCHANGE_UMASK(m)
595
596 #endif // __UNIX__/!__UNIX__
597
598
599 // Path searching
600 class WXDLLIMPEXP_BASE wxPathList : public wxStringList
601 {
602 public:
603 // avoid GCC warning about virtual functions w/o virtual dtor
604 virtual ~wxPathList() {}
605
606 // Adds all paths in environment variable
607 void AddEnvList(const wxString& envVariable);
608
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);
619
620 private:
621 // DECLARE_DYNAMIC_CLASS(wxPathList)
622 };
623
624 #endif
625 // _WX_FILEFN_H_