]>
Commit | Line | Data |
---|---|---|
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 | // functions | |
79 | #ifdef __BORLANDC__ | |
80 | #define _tell tell | |
81 | #endif | |
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 | |
92 | #if wxUSE_UNICODE_MSLU | |
93 | #define wxOpen wxMSLU__wopen | |
94 | ||
95 | #define wxAccess wxMSLU__waccess | |
96 | #define wxMkDir wxMSLU__wmkdir | |
97 | #define wxRmDir wxMSLU__wrmdir | |
98 | #define wxStat wxMSLU__wstat | |
99 | #else | |
100 | #define wxOpen _wopen | |
101 | #define wxAccess _waccess | |
102 | #define wxMkDir _wmkdir | |
103 | #define wxRmDir _wrmdir | |
104 | #define wxStat _wstat | |
105 | #endif | |
106 | #else // !wxUSE_UNICODE | |
107 | #ifdef __BORLANDC__ | |
108 | #define wxOpen open | |
109 | #else | |
110 | #define wxOpen _open | |
111 | #endif | |
112 | #define wxAccess _access | |
113 | #define wxMkDir _mkdir | |
114 | #define wxRmDir _rmdir | |
115 | #define wxStat _stat | |
116 | #endif | |
117 | ||
118 | // types | |
119 | #define wxStructStat struct _stat | |
120 | ||
121 | // constants (unless already defined by the user code) | |
122 | #if !defined(O_RDONLY) && !defined(__BORLANDC__) | |
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 | |
129 | #endif | |
130 | ||
131 | #ifndef __BORLANDC__ | |
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 | |
138 | #define wxClose close | |
139 | #define wxRead read | |
140 | #define wxWrite write | |
141 | #define wxLseek lseek | |
142 | #define wxFsync commit | |
143 | #define wxEof eof | |
144 | ||
145 | #define wxMkDir mkdir | |
146 | #define wxRmDir rmdir | |
147 | ||
148 | #define wxTell(fd) lseek(fd, 0, SEEK_CUR) | |
149 | ||
150 | #define wxStructStat struct stat | |
151 | ||
152 | #if wxUSE_UNICODE | |
153 | # define wxNEED_WX_UNISTD_H | |
154 | #if defined(__MWERKS__) && defined(macintosh) | |
155 | #include <sys/stat.h> | |
156 | #endif | |
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 ); | |
160 | #else | |
161 | #define wxOpen open | |
162 | #define wxStat stat | |
163 | #define wxAccess access | |
164 | #endif | |
165 | ||
166 | #endif // VC++ | |
167 | ||
168 | // ---------------------------------------------------------------------------- | |
169 | // functions | |
170 | // ---------------------------------------------------------------------------- | |
171 | WXDLLIMPEXP_BASE bool wxFileExists(const wxString& filename); | |
172 | ||
173 | // does the path exist? (may have or not '/' or '\\' at the end) | |
174 | WXDLLIMPEXP_BASE bool wxPathExists(const wxChar *pszPathName); | |
175 | ||
176 | WXDLLIMPEXP_BASE bool wxIsAbsolutePath(const wxString& filename); | |
177 | ||
178 | // Get filename | |
179 | WXDLLIMPEXP_BASE wxChar* wxFileNameFromPath(wxChar *path); | |
180 | WXDLLIMPEXP_BASE wxString wxFileNameFromPath(const wxString& path); | |
181 | ||
182 | // Get directory | |
183 | WXDLLIMPEXP_BASE wxString wxPathOnly(const wxString& path); | |
184 | ||
185 | // wxString version | |
186 | WXDLLIMPEXP_BASE wxString wxRealPath(const wxString& path); | |
187 | ||
188 | WXDLLIMPEXP_BASE void wxDos2UnixFilename(wxChar *s); | |
189 | ||
190 | WXDLLIMPEXP_BASE void wxUnix2DosFilename(wxChar *s); | |
191 | ||
192 | // Strip the extension, in situ | |
193 | WXDLLIMPEXP_BASE void wxStripExtension(wxChar *buffer); | |
194 | WXDLLIMPEXP_BASE void wxStripExtension(wxString& buffer); | |
195 | ||
196 | // Get a temporary filename | |
197 | WXDLLIMPEXP_BASE wxChar* wxGetTempFileName(const wxString& prefix, wxChar *buf = (wxChar *) NULL); | |
198 | WXDLLIMPEXP_BASE bool wxGetTempFileName(const wxString& prefix, wxString& buf); | |
199 | ||
200 | // Expand file name (~/ and ${OPENWINHOME}/ stuff) | |
201 | WXDLLIMPEXP_BASE wxChar* wxExpandPath(wxChar *dest, const wxChar *path); | |
202 | WXDLLIMPEXP_BASE bool wxExpandPath(wxString& dest, const wxChar *path); | |
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] | |
207 | WXDLLIMPEXP_BASE wxChar* wxContractPath(const wxString& filename, | |
208 | const wxString& envname = wxEmptyString, | |
209 | const wxString& user = wxEmptyString); | |
210 | ||
211 | // Destructive removal of /./ and /../ stuff | |
212 | WXDLLIMPEXP_BASE wxChar* wxRealPath(wxChar *path); | |
213 | ||
214 | // Allocate a copy of the full absolute path | |
215 | WXDLLIMPEXP_BASE wxChar* wxCopyAbsolutePath(const wxString& path); | |
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 | |
221 | WXDLLIMPEXP_BASE wxString wxFindFirstFile(const wxChar *spec, int flags = wxFILE); | |
222 | WXDLLIMPEXP_BASE wxString wxFindNextFile(); | |
223 | ||
224 | // Does the pattern contain wildcards? | |
225 | WXDLLIMPEXP_BASE bool wxIsWild(const wxString& pattern); | |
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) | |
230 | WXDLLIMPEXP_BASE bool wxMatchWild(const wxString& pattern, const wxString& text, bool dot_special = TRUE); | |
231 | ||
232 | // Concatenate two files to form third | |
233 | WXDLLIMPEXP_BASE bool wxConcatFiles(const wxString& file1, const wxString& file2, const wxString& file3); | |
234 | ||
235 | // Copy file1 to file2 | |
236 | WXDLLIMPEXP_BASE bool wxCopyFile(const wxString& file1, const wxString& file2, | |
237 | bool overwrite = TRUE); | |
238 | ||
239 | // Remove file | |
240 | WXDLLIMPEXP_BASE bool wxRemoveFile(const wxString& file); | |
241 | ||
242 | // Rename file | |
243 | WXDLLIMPEXP_BASE bool wxRenameFile(const wxString& file1, const wxString& file2); | |
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! | |
250 | WXDLLIMPEXP_BASE wxChar* wxGetWorkingDirectory(wxChar *buf = (wxChar *) NULL, int sz = 1000); | |
251 | // new and preferred version of wxGetWorkingDirectory | |
252 | // NB: can't have the same name because of overloading ambiguity | |
253 | WXDLLIMPEXP_BASE wxString wxGetCwd(); | |
254 | ||
255 | // Set working directory | |
256 | WXDLLIMPEXP_BASE bool wxSetWorkingDirectory(const wxString& d); | |
257 | ||
258 | // Make directory | |
259 | WXDLLIMPEXP_BASE bool wxMkdir(const wxString& dir, int perm = 0777); | |
260 | ||
261 | // Remove directory. Flags reserved for future use. | |
262 | WXDLLIMPEXP_BASE bool wxRmdir(const wxString& dir, int flags = 0); | |
263 | ||
264 | // compatibility defines, don't use in new code | |
265 | #define wxDirExists wxPathExists | |
266 | ||
267 | // ---------------------------------------------------------------------------- | |
268 | // separators in file names | |
269 | // ---------------------------------------------------------------------------- | |
270 | ||
271 | // between file name and extension | |
272 | #define wxFILE_SEP_EXT wxT('.') | |
273 | ||
274 | // between drive/volume name and the path | |
275 | #define wxFILE_SEP_DSK wxT(':') | |
276 | ||
277 | // between the path components | |
278 | #define wxFILE_SEP_PATH_DOS wxT('\\') | |
279 | #define wxFILE_SEP_PATH_UNIX wxT('/') | |
280 | #define wxFILE_SEP_PATH_MAC wxT(':') | |
281 | #define wxFILE_SEP_PATH_VMS wxT('.') // VMS also uses '[' and ']' | |
282 | ||
283 | // separator in the path list (as in PATH environment variable) | |
284 | // there is no PATH variable in Classic Mac OS so just use the | |
285 | // semicolon (it must be different from the file name separator) | |
286 | // NB: these are strings and not characters on purpose! | |
287 | #define wxPATH_SEP_DOS wxT(";") | |
288 | #define wxPATH_SEP_UNIX wxT(":") | |
289 | #define wxPATH_SEP_MAC wxT(";") | |
290 | ||
291 | // platform independent versions | |
292 | #if defined(__UNIX__) && !defined(__CYGWIN__) | |
293 | #define wxFILE_SEP_PATH wxFILE_SEP_PATH_UNIX | |
294 | #define wxPATH_SEP wxPATH_SEP_UNIX | |
295 | #elif defined(__MAC__) | |
296 | #define wxFILE_SEP_PATH wxFILE_SEP_PATH_MAC | |
297 | #define wxPATH_SEP wxPATH_SEP_MAC | |
298 | #elif defined(__CYGWIN__) // Cygwin | |
299 | #define wxFILE_SEP_PATH wxFILE_SEP_PATH_DOS | |
300 | #define wxPATH_SEP wxPATH_SEP_UNIX | |
301 | #else // Windows and OS/2 | |
302 | #define wxFILE_SEP_PATH wxFILE_SEP_PATH_DOS | |
303 | #define wxPATH_SEP wxPATH_SEP_DOS | |
304 | #endif // Unix/Windows | |
305 | ||
306 | // this is useful for wxString::IsSameAs(): to compare two file names use | |
307 | // filename1.IsSameAs(filename2, wxARE_FILENAMES_CASE_SENSITIVE) | |
308 | #if defined(__UNIX__) && !defined(__DARWIN__) | |
309 | #define wxARE_FILENAMES_CASE_SENSITIVE TRUE | |
310 | #else // Windows, Mac OS and OS/2 | |
311 | #define wxARE_FILENAMES_CASE_SENSITIVE FALSE | |
312 | #endif // Unix/Windows | |
313 | ||
314 | // is the char a path separator? | |
315 | inline bool wxIsPathSeparator(wxChar c) | |
316 | { | |
317 | // under DOS/Windows we should understand both Unix and DOS file separators | |
318 | #if defined(__UNIX__) || defined(__MAC__) | |
319 | return c == wxFILE_SEP_PATH; | |
320 | #else | |
321 | return c == wxFILE_SEP_PATH_DOS || c == wxFILE_SEP_PATH_UNIX; | |
322 | #endif | |
323 | } | |
324 | ||
325 | // does the string ends with path separator? | |
326 | WXDLLIMPEXP_BASE bool wxEndsWithPathSeparator(const wxChar *pszFileName); | |
327 | ||
328 | // split the full path into path (including drive for DOS), name and extension | |
329 | // (understands both '/' and '\\') | |
330 | WXDLLIMPEXP_BASE void wxSplitPath(const wxChar *pszFileName, | |
331 | wxString *pstrPath, | |
332 | wxString *pstrName, | |
333 | wxString *pstrExt); | |
334 | ||
335 | // find a file in a list of directories, returns false if not found | |
336 | WXDLLIMPEXP_BASE bool wxFindFileInPath(wxString *pStr, const wxChar *pszPath, const wxChar *pszFile); | |
337 | ||
338 | // Get the OS directory if appropriate (such as the Windows directory). | |
339 | // On non-Windows platform, probably just return the empty string. | |
340 | WXDLLIMPEXP_BASE wxString wxGetOSDirectory(); | |
341 | ||
342 | // Get file modification time | |
343 | WXDLLIMPEXP_BASE time_t wxFileModificationTime(const wxString& filename); | |
344 | ||
345 | // ---------------------------------------------------------------------------- | |
346 | // classes | |
347 | // ---------------------------------------------------------------------------- | |
348 | ||
349 | // Path searching | |
350 | class WXDLLIMPEXP_BASE wxPathList : public wxStringList | |
351 | { | |
352 | public: | |
353 | // avoid GCC warning about virtual functions w/o virtual dtor | |
354 | virtual ~wxPathList() {} | |
355 | ||
356 | // Adds all paths in environment variable | |
357 | void AddEnvList(const wxString& envVariable); | |
358 | ||
359 | void Add(const wxString& path); | |
360 | // Find the first full path for which the file exists | |
361 | wxString FindValidPath(const wxString& filename); | |
362 | // Find the first full path for which the file exists; ensure it's an | |
363 | // absolute path that gets returned. | |
364 | wxString FindAbsoluteValidPath(const wxString& filename); | |
365 | // Given full path and filename, add path to list | |
366 | void EnsureFileAccessible(const wxString& path); | |
367 | // Returns TRUE if the path is in the list | |
368 | bool Member(const wxString& path); | |
369 | ||
370 | private: | |
371 | // DECLARE_DYNAMIC_CLASS(wxPathList) | |
372 | }; | |
373 | ||
374 | #endif | |
375 | // _WX_FILEFN_H_ |