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"
20 #include "wx/arrstr.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
35 #if !defined(__WXMAC__) || defined(__UNIX__) || defined(__MACH__)
36 #include <sys/types.h>
41 #if defined(__VISUALC__) || ( defined(__MWERKS__) && defined( __INTEL__) )
43 #elif defined(__SYMANTEC__)
45 #elif defined(__MWERKS__) && !defined(__INTEL__) && !defined(__MACH__)
51 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
53 // VisualAge C++ V4.0 cannot have any external linkage const decs
54 // in headers included by more than one primary source
56 extern const off_t wxInvalidOffset
;
58 const off_t wxInvalidOffset
= (off_t
)-1;
68 // ----------------------------------------------------------------------------
69 // declare our versions of low level file functions: some compilers prepend
70 // underscores to the usual names, some also have Unicode versions of them
71 // ----------------------------------------------------------------------------
73 // Microsoft compiler loves underscores, feed them to it
74 #if defined( __VISUALC__ ) \
75 || ( defined(__MINGW32__) && !defined(__WINE__) && wxCHECK_W32API_VERSION( 0, 5 ) ) \
76 || ( defined(__MWERKS__) && defined(__WXMSW__) ) \
77 || ( defined(__DMC__) && defined(__WXMSW__) ) \
78 || ( defined(__WATCOMC__) && defined(__WXMSW__) )
80 #if defined(__BORLANDC__) || defined(__WATCOMC__)
83 #define wxClose _close
85 #define wxWrite _write
86 #define wxLseek _lseek
87 #define wxFsync _commit
93 #if wxUSE_UNICODE_MSLU
94 #define wxOpen wxMSLU__wopen
96 #define wxAccess wxMSLU__waccess
97 #define wxMkDir wxMSLU__wmkdir
98 #define wxRmDir wxMSLU__wrmdir
99 #define wxStat wxMSLU__wstat
101 #define wxOpen _wopen
102 #define wxAccess _waccess
103 #define wxMkDir _wmkdir
104 #define wxRmDir _wrmdir
105 #define wxStat _wstat
107 #else // !wxUSE_UNICODE
113 #define wxAccess _access
114 #define wxMkDir _mkdir
116 #define wxRmDir rmdir
118 #define wxRmDir _rmdir
124 #if defined(__WATCOMC__)&& wxUSE_UNICODE
125 #define wxStructStat struct _wstat
127 #define wxStructStat struct _stat
130 // constants (unless already defined by the user code)
131 #if !defined(O_RDONLY) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
132 #define O_RDONLY _O_RDONLY
133 #define O_WRONLY _O_WRONLY
134 #define O_RDWR _O_RDWR
135 #define O_EXCL _O_EXCL
136 #define O_CREAT _O_CREAT
137 #define O_BINARY _O_BINARY
140 #if !defined(__BORLANDC__) && !defined(__WATCOMC__)
141 #define S_IFMT _S_IFMT
142 #define S_IFDIR _S_IFDIR
143 #define S_IFREG _S_IFREG
147 #define wxClose close
149 #define wxWrite write
150 #define wxLseek lseek
151 #define wxFsync commit
154 #define wxMkDir mkdir
155 #define wxRmDir rmdir
157 #define wxTell(fd) lseek(fd, 0, SEEK_CUR)
159 #define wxStructStat struct stat
162 # define wxNEED_WX_UNISTD_H
163 #if defined(__MWERKS__) && defined(macintosh)
164 #include <sys/stat.h>
167 typedef unsigned long mode_t
;
169 WXDLLIMPEXP_BASE
int wxStat( const wxChar
*file_name
, wxStructStat
*buf
);
170 WXDLLIMPEXP_BASE
int wxAccess( const wxChar
*pathname
, int mode
);
171 WXDLLIMPEXP_BASE
int wxOpen( const wxChar
*pathname
, int flags
, mode_t mode
);
175 #define wxAccess access
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
183 WXDLLIMPEXP_BASE
bool wxFileExists(const wxString
& filename
);
185 // does the path exist? (may have or not '/' or '\\' at the end)
186 WXDLLIMPEXP_BASE
bool wxPathExists(const wxChar
*pszPathName
);
188 WXDLLIMPEXP_BASE
bool wxIsAbsolutePath(const wxString
& filename
);
191 WXDLLIMPEXP_BASE wxChar
* wxFileNameFromPath(wxChar
*path
);
192 WXDLLIMPEXP_BASE wxString
wxFileNameFromPath(const wxString
& path
);
195 WXDLLIMPEXP_BASE wxString
wxPathOnly(const wxString
& path
);
198 WXDLLIMPEXP_BASE wxString
wxRealPath(const wxString
& path
);
200 WXDLLIMPEXP_BASE
void wxDos2UnixFilename(wxChar
*s
);
202 WXDLLIMPEXP_BASE
void wxUnix2DosFilename(wxChar
*s
);
204 // Strip the extension, in situ
205 WXDLLIMPEXP_BASE
void wxStripExtension(wxChar
*buffer
);
206 WXDLLIMPEXP_BASE
void wxStripExtension(wxString
& buffer
);
208 // Get a temporary filename
209 WXDLLIMPEXP_BASE wxChar
* wxGetTempFileName(const wxString
& prefix
, wxChar
*buf
= (wxChar
*) NULL
);
210 WXDLLIMPEXP_BASE
bool wxGetTempFileName(const wxString
& prefix
, wxString
& buf
);
212 // Expand file name (~/ and ${OPENWINHOME}/ stuff)
213 WXDLLIMPEXP_BASE wxChar
* wxExpandPath(wxChar
*dest
, const wxChar
*path
);
214 WXDLLIMPEXP_BASE
bool wxExpandPath(wxString
& dest
, const wxChar
*path
);
216 // Contract w.r.t environment (</usr/openwin/lib, OPENWHOME> -> ${OPENWINHOME}/lib)
217 // and make (if under the home tree) relative to home
218 // [caller must copy-- volatile]
219 WXDLLIMPEXP_BASE wxChar
* wxContractPath(const wxString
& filename
,
220 const wxString
& envname
= wxEmptyString
,
221 const wxString
& user
= wxEmptyString
);
223 // Destructive removal of /./ and /../ stuff
224 WXDLLIMPEXP_BASE wxChar
* wxRealPath(wxChar
*path
);
226 // Allocate a copy of the full absolute path
227 WXDLLIMPEXP_BASE wxChar
* wxCopyAbsolutePath(const wxString
& path
);
229 // Get first file name matching given wild card.
230 // Flags are reserved for future use.
233 WXDLLIMPEXP_BASE wxString
wxFindFirstFile(const wxChar
*spec
, int flags
= wxFILE
);
234 WXDLLIMPEXP_BASE wxString
wxFindNextFile();
236 // Does the pattern contain wildcards?
237 WXDLLIMPEXP_BASE
bool wxIsWild(const wxString
& pattern
);
239 // Does the pattern match the text (usually a filename)?
240 // If dot_special is TRUE, doesn't match * against . (eliminating
241 // `hidden' dot files)
242 WXDLLIMPEXP_BASE
bool wxMatchWild(const wxString
& pattern
, const wxString
& text
, bool dot_special
= TRUE
);
244 // Concatenate two files to form third
245 WXDLLIMPEXP_BASE
bool wxConcatFiles(const wxString
& file1
, const wxString
& file2
, const wxString
& file3
);
247 // Copy file1 to file2
248 WXDLLIMPEXP_BASE
bool wxCopyFile(const wxString
& file1
, const wxString
& file2
,
249 bool overwrite
= TRUE
);
252 WXDLLIMPEXP_BASE
bool wxRemoveFile(const wxString
& file
);
255 WXDLLIMPEXP_BASE
bool wxRenameFile(const wxString
& file1
, const wxString
& file2
);
257 // Get current working directory.
258 // If buf is NULL, allocates space using new, else
260 // IMPORTANT NOTE getcwd is know not to work under some releases
261 // of Win32s 1.3, according to MS release notes!
262 WXDLLIMPEXP_BASE wxChar
* wxGetWorkingDirectory(wxChar
*buf
= (wxChar
*) NULL
, int sz
= 1000);
263 // new and preferred version of wxGetWorkingDirectory
264 // NB: can't have the same name because of overloading ambiguity
265 WXDLLIMPEXP_BASE wxString
wxGetCwd();
267 // Set working directory
268 WXDLLIMPEXP_BASE
bool wxSetWorkingDirectory(const wxString
& d
);
271 WXDLLIMPEXP_BASE
bool wxMkdir(const wxString
& dir
, int perm
= 0777);
273 // Remove directory. Flags reserved for future use.
274 WXDLLIMPEXP_BASE
bool wxRmdir(const wxString
& dir
, int flags
= 0);
276 // compatibility defines, don't use in new code
277 #define wxDirExists wxPathExists
279 // ----------------------------------------------------------------------------
280 // separators in file names
281 // ----------------------------------------------------------------------------
283 // between file name and extension
284 #define wxFILE_SEP_EXT wxT('.')
286 // between drive/volume name and the path
287 #define wxFILE_SEP_DSK wxT(':')
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 ']'
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(";")
303 // platform independent versions
304 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__OS2__)
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
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__) && !defined(__OS2__)
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
326 // is the char a path separator?
327 inline bool wxIsPathSeparator(wxChar c
)
329 // under DOS/Windows we should understand both Unix and DOS file separators
330 #if ( defined(__UNIX__) && !defined(__OS2__) )|| defined(__MAC__)
331 return c
== wxFILE_SEP_PATH
;
333 return c
== wxFILE_SEP_PATH_DOS
|| c
== wxFILE_SEP_PATH_UNIX
;
337 // does the string ends with path separator?
338 WXDLLIMPEXP_BASE
bool wxEndsWithPathSeparator(const wxChar
*pszFileName
);
340 // split the full path into path (including drive for DOS), name and extension
341 // (understands both '/' and '\\')
342 WXDLLIMPEXP_BASE
void wxSplitPath(const wxChar
*pszFileName
,
347 // find a file in a list of directories, returns false if not found
348 WXDLLIMPEXP_BASE
bool wxFindFileInPath(wxString
*pStr
, const wxChar
*pszPath
, const wxChar
*pszFile
);
350 // Get the OS directory if appropriate (such as the Windows directory).
351 // On non-Windows platform, probably just return the empty string.
352 WXDLLIMPEXP_BASE wxString
wxGetOSDirectory();
354 // Get file modification time
355 WXDLLIMPEXP_BASE
time_t wxFileModificationTime(const wxString
& filename
);
357 // Parses the wildCard, returning the number of filters.
358 // Returns 0 if none or if there's a problem,
359 // The arrays will contain an equal number of items found before the error.
360 // wildCard is in the form:
361 // "All files (*)|*|Image Files (*.jpeg *.png)|*.jpg;*.png"
362 WXDLLIMPEXP_BASE
int wxParseWildcard(const wxString
& wildCard
, wxArrayString
& descriptions
, wxArrayString
& filters
);
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
369 class WXDLLIMPEXP_BASE wxPathList
: public wxStringList
372 // avoid GCC warning about virtual functions w/o virtual dtor
373 virtual ~wxPathList() {}
375 // Adds all paths in environment variable
376 void AddEnvList(const wxString
& envVariable
);
378 void Add(const wxString
& path
);
379 // Find the first full path for which the file exists
380 wxString
FindValidPath(const wxString
& filename
);
381 // Find the first full path for which the file exists; ensure it's an
382 // absolute path that gets returned.
383 wxString
FindAbsoluteValidPath(const wxString
& filename
);
384 // Given full path and filename, add path to list
385 void EnsureFileAccessible(const wxString
& path
);
386 // Returns TRUE if the path is in the list
387 bool Member(const wxString
& path
);
390 // DECLARE_DYNAMIC_CLASS(wxPathList)