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