compilation fix for Watcom (part of patch 813225)
[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
21 #ifndef __WXWINCE__
22 #include <time.h>
23 #endif
24
25 // ----------------------------------------------------------------------------
26 // constants
27 // ----------------------------------------------------------------------------
28
29 #ifdef __WXWINCE__
30 typedef long off_t;
31 #else
32
33 // define off_t
34 #if !defined(__WXMAC__) || defined(__UNIX__) || defined(__MACH__)
35 #include <sys/types.h>
36 #else
37 typedef long off_t;
38 #endif
39
40 #if defined(__VISUALC__) || ( defined(__MWERKS__) && defined( __INTEL__) )
41 typedef _off_t off_t;
42 #elif defined(__BORLANDC__) && defined(__WIN16__)
43 typedef long off_t;
44 #elif defined(__SYMANTEC__)
45 typedef long off_t;
46 #elif defined(__MWERKS__) && !defined(__INTEL__) && !defined(__MACH__)
47 typedef long off_t;
48 #endif
49
50 #endif
51
52 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
53 //
54 // VisualAge C++ V4.0 cannot have any external linkage const decs
55 // in headers included by more than one primary source
56 //
57 extern const off_t wxInvalidOffset;
58 #else
59 const off_t wxInvalidOffset = (off_t)-1;
60 #endif
61
62 enum wxSeekMode
63 {
64 wxFromStart,
65 wxFromCurrent,
66 wxFromEnd
67 };
68
69 // ----------------------------------------------------------------------------
70 // declare our versions of low level file functions: some compilers prepend
71 // underscores to the usual names, some also have Unicode versions of them
72 // ----------------------------------------------------------------------------
73
74 // Microsoft compiler loves underscores, feed them to it
75 #if defined( __VISUALC__ ) \
76 || ( defined(__MINGW32__) && !defined(__WINE__) && wxCHECK_W32API_VERSION( 0, 5 ) ) \
77 || ( defined(__MWERKS__) && defined(__WXMSW__) ) \
78 || ( defined(__WATCOMC__) && defined(__WXMSW__) )
79 // functions
80 #if defined(__BORLANDC__) || defined(__WATCOMC__)
81 #define _tell tell
82 #endif
83 #define wxClose _close
84 #define wxRead _read
85 #define wxWrite _write
86 #define wxLseek _lseek
87 #define wxFsync _commit
88 #define wxEof _eof
89
90 #define wxTell _tell
91
92 #if wxUSE_UNICODE
93 #if wxUSE_UNICODE_MSLU
94 #define wxOpen wxMSLU__wopen
95
96 #define wxAccess wxMSLU__waccess
97 #define wxMkDir wxMSLU__wmkdir
98 #define wxRmDir wxMSLU__wrmdir
99 #define wxStat wxMSLU__wstat
100 #else
101 #define wxOpen _wopen
102 #define wxAccess _waccess
103 #define wxMkDir _wmkdir
104 #define wxRmDir _wrmdir
105 #define wxStat _wstat
106 #endif
107 #else // !wxUSE_UNICODE
108 #ifdef __BORLANDC__
109 #define wxOpen open
110 #else
111 #define wxOpen _open
112 #endif
113 #define wxAccess _access
114 #define wxMkDir _mkdir
115 #ifdef __WATCOMC__
116 #define wxRmDir rmdir
117 #else
118 #define wxRmDir _rmdir
119 #endif
120 #define wxStat _stat
121 #endif
122
123 // types
124 #if defined(__WATCOMC__)&& wxUSE_UNICODE
125 #define wxStructStat struct _wstat
126 #else
127 #define wxStructStat struct _stat
128 #endif
129
130 // constants (unless already defined by the user code)
131 #if !defined(O_RDONLY) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
132 #define O_RDONLY _O_RDONLY
133 #define O_WRONLY _O_WRONLY
134 #define O_RDWR _O_RDWR
135 #define O_EXCL _O_EXCL
136 #define O_CREAT _O_CREAT
137 #define O_BINARY _O_BINARY
138 #endif
139
140 #if !defined(__BORLANDC__) && !defined(__WATCOMC__)
141 #define S_IFMT _S_IFMT
142 #define S_IFDIR _S_IFDIR
143 #define S_IFREG _S_IFREG
144 #endif // O_RDONLY
145 #else
146 // functions
147 #define wxClose close
148 #define wxRead read
149 #define wxWrite write
150 #define wxLseek lseek
151 #define wxFsync commit
152 #define wxEof eof
153
154 #define wxMkDir mkdir
155 #define wxRmDir rmdir
156
157 #define wxTell(fd) lseek(fd, 0, SEEK_CUR)
158
159 #define wxStructStat struct stat
160
161 #if wxUSE_UNICODE
162 # define wxNEED_WX_UNISTD_H
163 #if defined(__MWERKS__) && defined(macintosh)
164 #include <sys/stat.h>
165 #endif
166 WXDLLIMPEXP_BASE int wxStat( const wxChar *file_name, wxStructStat *buf );
167 WXDLLIMPEXP_BASE int wxAccess( const wxChar *pathname, int mode );
168 WXDLLIMPEXP_BASE int wxOpen( const wxChar *pathname, int flags, mode_t mode );
169 #else
170 #define wxOpen open
171 #define wxStat stat
172 #define wxAccess access
173 #endif
174
175 #endif // VC++
176
177 // ----------------------------------------------------------------------------
178 // functions
179 // ----------------------------------------------------------------------------
180 WXDLLIMPEXP_BASE bool wxFileExists(const wxString& filename);
181
182 // does the path exist? (may have or not '/' or '\\' at the end)
183 WXDLLIMPEXP_BASE bool wxPathExists(const wxChar *pszPathName);
184
185 WXDLLIMPEXP_BASE bool wxIsAbsolutePath(const wxString& filename);
186
187 // Get filename
188 WXDLLIMPEXP_BASE wxChar* wxFileNameFromPath(wxChar *path);
189 WXDLLIMPEXP_BASE wxString wxFileNameFromPath(const wxString& path);
190
191 // Get directory
192 WXDLLIMPEXP_BASE wxString wxPathOnly(const wxString& path);
193
194 // wxString version
195 WXDLLIMPEXP_BASE wxString wxRealPath(const wxString& path);
196
197 WXDLLIMPEXP_BASE void wxDos2UnixFilename(wxChar *s);
198
199 WXDLLIMPEXP_BASE void wxUnix2DosFilename(wxChar *s);
200
201 // Strip the extension, in situ
202 WXDLLIMPEXP_BASE void wxStripExtension(wxChar *buffer);
203 WXDLLIMPEXP_BASE void wxStripExtension(wxString& buffer);
204
205 // Get a temporary filename
206 WXDLLIMPEXP_BASE wxChar* wxGetTempFileName(const wxString& prefix, wxChar *buf = (wxChar *) NULL);
207 WXDLLIMPEXP_BASE bool wxGetTempFileName(const wxString& prefix, wxString& buf);
208
209 // Expand file name (~/ and ${OPENWINHOME}/ stuff)
210 WXDLLIMPEXP_BASE wxChar* wxExpandPath(wxChar *dest, const wxChar *path);
211 WXDLLIMPEXP_BASE bool wxExpandPath(wxString& dest, const wxChar *path);
212
213 // Contract w.r.t environment (</usr/openwin/lib, OPENWHOME> -> ${OPENWINHOME}/lib)
214 // and make (if under the home tree) relative to home
215 // [caller must copy-- volatile]
216 WXDLLIMPEXP_BASE wxChar* wxContractPath(const wxString& filename,
217 const wxString& envname = wxEmptyString,
218 const wxString& user = wxEmptyString);
219
220 // Destructive removal of /./ and /../ stuff
221 WXDLLIMPEXP_BASE wxChar* wxRealPath(wxChar *path);
222
223 // Allocate a copy of the full absolute path
224 WXDLLIMPEXP_BASE wxChar* wxCopyAbsolutePath(const wxString& path);
225
226 // Get first file name matching given wild card.
227 // Flags are reserved for future use.
228 #define wxFILE 1
229 #define wxDIR 2
230 WXDLLIMPEXP_BASE wxString wxFindFirstFile(const wxChar *spec, int flags = wxFILE);
231 WXDLLIMPEXP_BASE wxString wxFindNextFile();
232
233 // Does the pattern contain wildcards?
234 WXDLLIMPEXP_BASE bool wxIsWild(const wxString& pattern);
235
236 // Does the pattern match the text (usually a filename)?
237 // If dot_special is TRUE, doesn't match * against . (eliminating
238 // `hidden' dot files)
239 WXDLLIMPEXP_BASE bool wxMatchWild(const wxString& pattern, const wxString& text, bool dot_special = TRUE);
240
241 // Concatenate two files to form third
242 WXDLLIMPEXP_BASE bool wxConcatFiles(const wxString& file1, const wxString& file2, const wxString& file3);
243
244 // Copy file1 to file2
245 WXDLLIMPEXP_BASE bool wxCopyFile(const wxString& file1, const wxString& file2,
246 bool overwrite = TRUE);
247
248 // Remove file
249 WXDLLIMPEXP_BASE bool wxRemoveFile(const wxString& file);
250
251 // Rename file
252 WXDLLIMPEXP_BASE bool wxRenameFile(const wxString& file1, const wxString& file2);
253
254 // Get current working directory.
255 // If buf is NULL, allocates space using new, else
256 // copies into buf.
257 // IMPORTANT NOTE getcwd is know not to work under some releases
258 // of Win32s 1.3, according to MS release notes!
259 WXDLLIMPEXP_BASE wxChar* wxGetWorkingDirectory(wxChar *buf = (wxChar *) NULL, int sz = 1000);
260 // new and preferred version of wxGetWorkingDirectory
261 // NB: can't have the same name because of overloading ambiguity
262 WXDLLIMPEXP_BASE wxString wxGetCwd();
263
264 // Set working directory
265 WXDLLIMPEXP_BASE bool wxSetWorkingDirectory(const wxString& d);
266
267 // Make directory
268 WXDLLIMPEXP_BASE bool wxMkdir(const wxString& dir, int perm = 0777);
269
270 // Remove directory. Flags reserved for future use.
271 WXDLLIMPEXP_BASE bool wxRmdir(const wxString& dir, int flags = 0);
272
273 // compatibility defines, don't use in new code
274 #define wxDirExists wxPathExists
275
276 // ----------------------------------------------------------------------------
277 // separators in file names
278 // ----------------------------------------------------------------------------
279
280 // between file name and extension
281 #define wxFILE_SEP_EXT wxT('.')
282
283 // between drive/volume name and the path
284 #define wxFILE_SEP_DSK wxT(':')
285
286 // between the path components
287 #define wxFILE_SEP_PATH_DOS wxT('\\')
288 #define wxFILE_SEP_PATH_UNIX wxT('/')
289 #define wxFILE_SEP_PATH_MAC wxT(':')
290 #define wxFILE_SEP_PATH_VMS wxT('.') // VMS also uses '[' and ']'
291
292 // separator in the path list (as in PATH environment variable)
293 // there is no PATH variable in Classic Mac OS so just use the
294 // semicolon (it must be different from the file name separator)
295 // NB: these are strings and not characters on purpose!
296 #define wxPATH_SEP_DOS wxT(";")
297 #define wxPATH_SEP_UNIX wxT(":")
298 #define wxPATH_SEP_MAC wxT(";")
299
300 // platform independent versions
301 #if defined(__UNIX__) && !defined(__CYGWIN__)
302 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_UNIX
303 #define wxPATH_SEP wxPATH_SEP_UNIX
304 #elif defined(__MAC__)
305 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_MAC
306 #define wxPATH_SEP wxPATH_SEP_MAC
307 #elif defined(__CYGWIN__) // Cygwin
308 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_DOS
309 #define wxPATH_SEP wxPATH_SEP_UNIX
310 #else // Windows and OS/2
311 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_DOS
312 #define wxPATH_SEP wxPATH_SEP_DOS
313 #endif // Unix/Windows
314
315 // this is useful for wxString::IsSameAs(): to compare two file names use
316 // filename1.IsSameAs(filename2, wxARE_FILENAMES_CASE_SENSITIVE)
317 #if defined(__UNIX__) && !defined(__DARWIN__)
318 #define wxARE_FILENAMES_CASE_SENSITIVE TRUE
319 #else // Windows, Mac OS and OS/2
320 #define wxARE_FILENAMES_CASE_SENSITIVE FALSE
321 #endif // Unix/Windows
322
323 // is the char a path separator?
324 inline bool wxIsPathSeparator(wxChar c)
325 {
326 // under DOS/Windows we should understand both Unix and DOS file separators
327 #if defined(__UNIX__) || defined(__MAC__)
328 return c == wxFILE_SEP_PATH;
329 #else
330 return c == wxFILE_SEP_PATH_DOS || c == wxFILE_SEP_PATH_UNIX;
331 #endif
332 }
333
334 // does the string ends with path separator?
335 WXDLLIMPEXP_BASE bool wxEndsWithPathSeparator(const wxChar *pszFileName);
336
337 // split the full path into path (including drive for DOS), name and extension
338 // (understands both '/' and '\\')
339 WXDLLIMPEXP_BASE void wxSplitPath(const wxChar *pszFileName,
340 wxString *pstrPath,
341 wxString *pstrName,
342 wxString *pstrExt);
343
344 // find a file in a list of directories, returns false if not found
345 WXDLLIMPEXP_BASE bool wxFindFileInPath(wxString *pStr, const wxChar *pszPath, const wxChar *pszFile);
346
347 // Get the OS directory if appropriate (such as the Windows directory).
348 // On non-Windows platform, probably just return the empty string.
349 WXDLLIMPEXP_BASE wxString wxGetOSDirectory();
350
351 // Get file modification time
352 WXDLLIMPEXP_BASE time_t wxFileModificationTime(const wxString& filename);
353
354 // ----------------------------------------------------------------------------
355 // classes
356 // ----------------------------------------------------------------------------
357
358 // Path searching
359 class WXDLLIMPEXP_BASE wxPathList : public wxStringList
360 {
361 public:
362 // avoid GCC warning about virtual functions w/o virtual dtor
363 virtual ~wxPathList() {}
364
365 // Adds all paths in environment variable
366 void AddEnvList(const wxString& envVariable);
367
368 void Add(const wxString& path);
369 // Find the first full path for which the file exists
370 wxString FindValidPath(const wxString& filename);
371 // Find the first full path for which the file exists; ensure it's an
372 // absolute path that gets returned.
373 wxString FindAbsoluteValidPath(const wxString& filename);
374 // Given full path and filename, add path to list
375 void EnsureFileAccessible(const wxString& path);
376 // Returns TRUE if the path is in the list
377 bool Member(const wxString& path);
378
379 private:
380 // DECLARE_DYNAMIC_CLASS(wxPathList)
381 };
382
383 #endif
384 // _WX_FILEFN_H_