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