1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: File- and directory-related functions
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "filefn.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
34 #if !defined(__WXMAC__) || defined(__UNIX__) || defined(__MACH__)
35 #include <sys/types.h>
40 #if defined(__VISUALC__) || ( defined(__MWERKS__) && defined( __INTEL__) )
42 #elif defined(__BORLANDC__) && defined(__WIN16__)
44 #elif defined(__SYMANTEC__)
46 #elif defined(__MWERKS__) && !defined(__INTEL__) && !defined(__MACH__)
52 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
54 // VisualAge C++ V4.0 cannot have any external linkage const decs
55 // in headers included by more than one primary source
57 extern const off_t wxInvalidOffset
;
59 const off_t wxInvalidOffset
= (off_t
)-1;
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 // ----------------------------------------------------------------------------
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__) )
82 #define wxClose _close
84 #define wxWrite _write
85 #define wxLseek _lseek
86 #define wxFsync _commit
92 #if wxUSE_UNICODE_MSLU
93 #define wxOpen wxMSLU__wopen
95 #define wxAccess wxMSLU__waccess
96 #define wxMkDir wxMSLU__wmkdir
97 #define wxRmDir wxMSLU__wrmdir
98 #define wxStat wxMSLU__wstat
100 #define wxOpen _wopen
101 #define wxAccess _waccess
102 #define wxMkDir _wmkdir
103 #define wxRmDir _wrmdir
104 #define wxStat _wstat
106 #else // !wxUSE_UNICODE
112 #define wxAccess _access
113 #define wxMkDir _mkdir
114 #define wxRmDir _rmdir
119 #define wxStructStat struct _stat
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
132 #define S_IFMT _S_IFMT
133 #define S_IFDIR _S_IFDIR
134 #define S_IFREG _S_IFREG
138 #define wxClose close
140 #define wxWrite write
141 #define wxLseek lseek
142 #define wxFsync commit
145 #define wxMkDir mkdir
146 #define wxRmDir rmdir
148 #define wxTell(fd) lseek(fd, 0, SEEK_CUR)
150 #define wxStructStat struct stat
153 # define wxNEED_WX_UNISTD_H
154 #if defined(__MWERKS__) && defined(macintosh)
155 #include <sys/stat.h>
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
);
161 #if defined(__WXMAC__) && !defined(__DARWIN__)
162 #include <sys/stat.h>
163 WXDLLIMPEXP_BASE
int wxStat( const wxChar
*file_name
, wxStructStat
*buf
);
164 WXDLLIMPEXP_BASE
int wxAccess( const wxChar
*pathname
, int mode
);
165 WXDLLIMPEXP_BASE
int wxOpen( const wxChar
*pathname
, int flags
, mode_t mode
);
169 #define wxAccess access
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
178 WXDLLIMPEXP_BASE
bool wxFileExists(const wxString
& filename
);
180 // does the path exist? (may have or not '/' or '\\' at the end)
181 WXDLLIMPEXP_BASE
bool wxPathExists(const wxChar
*pszPathName
);
183 WXDLLIMPEXP_BASE
bool wxIsAbsolutePath(const wxString
& filename
);
186 WXDLLIMPEXP_BASE wxChar
* wxFileNameFromPath(wxChar
*path
);
187 WXDLLIMPEXP_BASE wxString
wxFileNameFromPath(const wxString
& path
);
190 WXDLLIMPEXP_BASE wxString
wxPathOnly(const wxString
& path
);
193 WXDLLIMPEXP_BASE wxString
wxRealPath(const wxString
& path
);
195 WXDLLIMPEXP_BASE
void wxDos2UnixFilename(wxChar
*s
);
197 WXDLLIMPEXP_BASE
void wxUnix2DosFilename(wxChar
*s
);
199 // Strip the extension, in situ
200 WXDLLIMPEXP_BASE
void wxStripExtension(wxChar
*buffer
);
201 WXDLLIMPEXP_BASE
void wxStripExtension(wxString
& buffer
);
203 // Get a temporary filename
204 WXDLLIMPEXP_BASE wxChar
* wxGetTempFileName(const wxString
& prefix
, wxChar
*buf
= (wxChar
*) NULL
);
205 WXDLLIMPEXP_BASE
bool wxGetTempFileName(const wxString
& prefix
, wxString
& buf
);
207 // Expand file name (~/ and ${OPENWINHOME}/ stuff)
208 WXDLLIMPEXP_BASE wxChar
* wxExpandPath(wxChar
*dest
, const wxChar
*path
);
209 WXDLLIMPEXP_BASE
bool wxExpandPath(wxString
& dest
, const wxChar
*path
);
211 // Contract w.r.t environment (</usr/openwin/lib, OPENWHOME> -> ${OPENWINHOME}/lib)
212 // and make (if under the home tree) relative to home
213 // [caller must copy-- volatile]
214 WXDLLIMPEXP_BASE wxChar
* wxContractPath(const wxString
& filename
,
215 const wxString
& envname
= wxEmptyString
,
216 const wxString
& user
= wxEmptyString
);
218 // Destructive removal of /./ and /../ stuff
219 WXDLLIMPEXP_BASE wxChar
* wxRealPath(wxChar
*path
);
221 // Allocate a copy of the full absolute path
222 WXDLLIMPEXP_BASE wxChar
* wxCopyAbsolutePath(const wxString
& path
);
224 // Get first file name matching given wild card.
225 // Flags are reserved for future use.
228 WXDLLIMPEXP_BASE wxString
wxFindFirstFile(const wxChar
*spec
, int flags
= wxFILE
);
229 WXDLLIMPEXP_BASE wxString
wxFindNextFile();
231 // Does the pattern contain wildcards?
232 WXDLLIMPEXP_BASE
bool wxIsWild(const wxString
& pattern
);
234 // Does the pattern match the text (usually a filename)?
235 // If dot_special is TRUE, doesn't match * against . (eliminating
236 // `hidden' dot files)
237 WXDLLIMPEXP_BASE
bool wxMatchWild(const wxString
& pattern
, const wxString
& text
, bool dot_special
= TRUE
);
239 // Concatenate two files to form third
240 WXDLLIMPEXP_BASE
bool wxConcatFiles(const wxString
& file1
, const wxString
& file2
, const wxString
& file3
);
242 // Copy file1 to file2
243 WXDLLIMPEXP_BASE
bool wxCopyFile(const wxString
& file1
, const wxString
& file2
,
244 bool overwrite
= TRUE
);
247 WXDLLIMPEXP_BASE
bool wxRemoveFile(const wxString
& file
);
250 WXDLLIMPEXP_BASE
bool wxRenameFile(const wxString
& file1
, const wxString
& file2
);
252 // Get current working directory.
253 // If buf is NULL, allocates space using new, else
255 // IMPORTANT NOTE getcwd is know not to work under some releases
256 // of Win32s 1.3, according to MS release notes!
257 WXDLLIMPEXP_BASE wxChar
* wxGetWorkingDirectory(wxChar
*buf
= (wxChar
*) NULL
, int sz
= 1000);
258 // new and preferred version of wxGetWorkingDirectory
259 // NB: can't have the same name because of overloading ambiguity
260 WXDLLIMPEXP_BASE wxString
wxGetCwd();
262 // Set working directory
263 WXDLLIMPEXP_BASE
bool wxSetWorkingDirectory(const wxString
& d
);
266 WXDLLIMPEXP_BASE
bool wxMkdir(const wxString
& dir
, int perm
= 0777);
268 // Remove directory. Flags reserved for future use.
269 WXDLLIMPEXP_BASE
bool wxRmdir(const wxString
& dir
, int flags
= 0);
271 // compatibility defines, don't use in new code
272 #define wxDirExists wxPathExists
274 // ----------------------------------------------------------------------------
275 // separators in file names
276 // ----------------------------------------------------------------------------
278 // between file name and extension
279 #define wxFILE_SEP_EXT wxT('.')
281 // between drive/volume name and the path
282 #define wxFILE_SEP_DSK wxT(':')
284 // between the path components
285 #define wxFILE_SEP_PATH_DOS wxT('\\')
286 #define wxFILE_SEP_PATH_UNIX wxT('/')
287 #define wxFILE_SEP_PATH_MAC wxT(':')
288 #define wxFILE_SEP_PATH_VMS wxT('.') // VMS also uses '[' and ']'
290 // separator in the path list (as in PATH environment variable)
291 // there is no PATH variable in Classic Mac OS so just use the
292 // semicolon (it must be different from the file name separator)
293 // NB: these are strings and not characters on purpose!
294 #define wxPATH_SEP_DOS wxT(";")
295 #define wxPATH_SEP_UNIX wxT(":")
296 #define wxPATH_SEP_MAC wxT(";")
298 // platform independent versions
299 #if defined(__UNIX__) && !defined(__CYGWIN__)
300 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_UNIX
301 #define wxPATH_SEP wxPATH_SEP_UNIX
302 #elif defined(__MAC__)
303 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_MAC
304 #define wxPATH_SEP wxPATH_SEP_MAC
305 #elif defined(__CYGWIN__) // Cygwin
306 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_DOS
307 #define wxPATH_SEP wxPATH_SEP_UNIX
308 #else // Windows and OS/2
309 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_DOS
310 #define wxPATH_SEP wxPATH_SEP_DOS
311 #endif // Unix/Windows
313 // this is useful for wxString::IsSameAs(): to compare two file names use
314 // filename1.IsSameAs(filename2, wxARE_FILENAMES_CASE_SENSITIVE)
315 #if defined(__UNIX__) && !defined(__DARWIN__)
316 #define wxARE_FILENAMES_CASE_SENSITIVE TRUE
317 #else // Windows, Mac OS and OS/2
318 #define wxARE_FILENAMES_CASE_SENSITIVE FALSE
319 #endif // Unix/Windows
321 // is the char a path separator?
322 inline bool wxIsPathSeparator(wxChar c
)
324 // under DOS/Windows we should understand both Unix and DOS file separators
325 #if defined(__UNIX__) || defined(__MAC__)
326 return c
== wxFILE_SEP_PATH
;
328 return c
== wxFILE_SEP_PATH_DOS
|| c
== wxFILE_SEP_PATH_UNIX
;
332 // does the string ends with path separator?
333 WXDLLIMPEXP_BASE
bool wxEndsWithPathSeparator(const wxChar
*pszFileName
);
335 // split the full path into path (including drive for DOS), name and extension
336 // (understands both '/' and '\\')
337 WXDLLIMPEXP_BASE
void wxSplitPath(const wxChar
*pszFileName
,
342 // find a file in a list of directories, returns false if not found
343 WXDLLIMPEXP_BASE
bool wxFindFileInPath(wxString
*pStr
, const wxChar
*pszPath
, const wxChar
*pszFile
);
345 // Get the OS directory if appropriate (such as the Windows directory).
346 // On non-Windows platform, probably just return the empty string.
347 WXDLLIMPEXP_BASE wxString
wxGetOSDirectory();
349 // Get file modification time
350 WXDLLIMPEXP_BASE
time_t wxFileModificationTime(const wxString
& filename
);
352 // ----------------------------------------------------------------------------
354 // ----------------------------------------------------------------------------
357 class WXDLLIMPEXP_BASE wxPathList
: public wxStringList
360 // avoid GCC warning about virtual functions w/o virtual dtor
361 virtual ~wxPathList() {}
363 // Adds all paths in environment variable
364 void AddEnvList(const wxString
& envVariable
);
366 void Add(const wxString
& path
);
367 // Find the first full path for which the file exists
368 wxString
FindValidPath(const wxString
& filename
);
369 // Find the first full path for which the file exists; ensure it's an
370 // absolute path that gets returned.
371 wxString
FindAbsoluteValidPath(const wxString
& filename
);
372 // Given full path and filename, add path to list
373 void EnsureFileAccessible(const wxString
& path
);
374 // Returns TRUE if the path is in the list
375 bool Member(const wxString
& path
);
378 // DECLARE_DYNAMIC_CLASS(wxPathList)