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