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__) ) \
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
115 #define wxRmDir _rmdir
120 #if defined(__WATCOMC__)&& wxUSE_UNICODE
121 #define wxStructStat struct _wstat
123 #define wxStructStat struct _stat
126 // constants (unless already defined by the user code)
127 #if !defined(O_RDONLY) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
128 #define O_RDONLY _O_RDONLY
129 #define O_WRONLY _O_WRONLY
130 #define O_RDWR _O_RDWR
131 #define O_EXCL _O_EXCL
132 #define O_CREAT _O_CREAT
133 #define O_BINARY _O_BINARY
136 #if !defined(__BORLANDC__) && !defined(__WATCOMC__)
137 #define S_IFMT _S_IFMT
138 #define S_IFDIR _S_IFDIR
139 #define S_IFREG _S_IFREG
143 #define wxClose close
145 #define wxWrite write
146 #define wxLseek lseek
147 #define wxFsync commit
150 #define wxMkDir mkdir
151 #define wxRmDir rmdir
153 #define wxTell(fd) lseek(fd, 0, SEEK_CUR)
155 #define wxStructStat struct stat
158 # define wxNEED_WX_UNISTD_H
159 #if defined(__MWERKS__) && defined(macintosh)
160 #include <sys/stat.h>
162 WXDLLIMPEXP_BASE
int wxStat( const wxChar
*file_name
, wxStructStat
*buf
);
163 WXDLLIMPEXP_BASE
int wxAccess( const wxChar
*pathname
, int mode
);
164 WXDLLIMPEXP_BASE
int wxOpen( const wxChar
*pathname
, int flags
, mode_t mode
);
168 #define wxAccess access
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
176 WXDLLIMPEXP_BASE
bool wxFileExists(const wxString
& filename
);
178 // does the path exist? (may have or not '/' or '\\' at the end)
179 WXDLLIMPEXP_BASE
bool wxPathExists(const wxChar
*pszPathName
);
181 WXDLLIMPEXP_BASE
bool wxIsAbsolutePath(const wxString
& filename
);
184 WXDLLIMPEXP_BASE wxChar
* wxFileNameFromPath(wxChar
*path
);
185 WXDLLIMPEXP_BASE wxString
wxFileNameFromPath(const wxString
& path
);
188 WXDLLIMPEXP_BASE wxString
wxPathOnly(const wxString
& path
);
191 WXDLLIMPEXP_BASE wxString
wxRealPath(const wxString
& path
);
193 WXDLLIMPEXP_BASE
void wxDos2UnixFilename(wxChar
*s
);
195 WXDLLIMPEXP_BASE
void wxUnix2DosFilename(wxChar
*s
);
197 // Strip the extension, in situ
198 WXDLLIMPEXP_BASE
void wxStripExtension(wxChar
*buffer
);
199 WXDLLIMPEXP_BASE
void wxStripExtension(wxString
& buffer
);
201 // Get a temporary filename
202 WXDLLIMPEXP_BASE wxChar
* wxGetTempFileName(const wxString
& prefix
, wxChar
*buf
= (wxChar
*) NULL
);
203 WXDLLIMPEXP_BASE
bool wxGetTempFileName(const wxString
& prefix
, wxString
& buf
);
205 // Expand file name (~/ and ${OPENWINHOME}/ stuff)
206 WXDLLIMPEXP_BASE wxChar
* wxExpandPath(wxChar
*dest
, const wxChar
*path
);
207 WXDLLIMPEXP_BASE
bool wxExpandPath(wxString
& dest
, const wxChar
*path
);
209 // Contract w.r.t environment (</usr/openwin/lib, OPENWHOME> -> ${OPENWINHOME}/lib)
210 // and make (if under the home tree) relative to home
211 // [caller must copy-- volatile]
212 WXDLLIMPEXP_BASE wxChar
* wxContractPath(const wxString
& filename
,
213 const wxString
& envname
= wxEmptyString
,
214 const wxString
& user
= wxEmptyString
);
216 // Destructive removal of /./ and /../ stuff
217 WXDLLIMPEXP_BASE wxChar
* wxRealPath(wxChar
*path
);
219 // Allocate a copy of the full absolute path
220 WXDLLIMPEXP_BASE wxChar
* wxCopyAbsolutePath(const wxString
& path
);
222 // Get first file name matching given wild card.
223 // Flags are reserved for future use.
226 WXDLLIMPEXP_BASE wxString
wxFindFirstFile(const wxChar
*spec
, int flags
= wxFILE
);
227 WXDLLIMPEXP_BASE wxString
wxFindNextFile();
229 // Does the pattern contain wildcards?
230 WXDLLIMPEXP_BASE
bool wxIsWild(const wxString
& pattern
);
232 // Does the pattern match the text (usually a filename)?
233 // If dot_special is TRUE, doesn't match * against . (eliminating
234 // `hidden' dot files)
235 WXDLLIMPEXP_BASE
bool wxMatchWild(const wxString
& pattern
, const wxString
& text
, bool dot_special
= TRUE
);
237 // Concatenate two files to form third
238 WXDLLIMPEXP_BASE
bool wxConcatFiles(const wxString
& file1
, const wxString
& file2
, const wxString
& file3
);
240 // Copy file1 to file2
241 WXDLLIMPEXP_BASE
bool wxCopyFile(const wxString
& file1
, const wxString
& file2
,
242 bool overwrite
= TRUE
);
245 WXDLLIMPEXP_BASE
bool wxRemoveFile(const wxString
& file
);
248 WXDLLIMPEXP_BASE
bool wxRenameFile(const wxString
& file1
, const wxString
& file2
);
250 // Get current working directory.
251 // If buf is NULL, allocates space using new, else
253 // IMPORTANT NOTE getcwd is know not to work under some releases
254 // of Win32s 1.3, according to MS release notes!
255 WXDLLIMPEXP_BASE wxChar
* wxGetWorkingDirectory(wxChar
*buf
= (wxChar
*) NULL
, int sz
= 1000);
256 // new and preferred version of wxGetWorkingDirectory
257 // NB: can't have the same name because of overloading ambiguity
258 WXDLLIMPEXP_BASE wxString
wxGetCwd();
260 // Set working directory
261 WXDLLIMPEXP_BASE
bool wxSetWorkingDirectory(const wxString
& d
);
264 WXDLLIMPEXP_BASE
bool wxMkdir(const wxString
& dir
, int perm
= 0777);
266 // Remove directory. Flags reserved for future use.
267 WXDLLIMPEXP_BASE
bool wxRmdir(const wxString
& dir
, int flags
= 0);
269 // compatibility defines, don't use in new code
270 #define wxDirExists wxPathExists
272 // ----------------------------------------------------------------------------
273 // separators in file names
274 // ----------------------------------------------------------------------------
276 // between file name and extension
277 #define wxFILE_SEP_EXT wxT('.')
279 // between drive/volume name and the path
280 #define wxFILE_SEP_DSK wxT(':')
282 // between the path components
283 #define wxFILE_SEP_PATH_DOS wxT('\\')
284 #define wxFILE_SEP_PATH_UNIX wxT('/')
285 #define wxFILE_SEP_PATH_MAC wxT(':')
286 #define wxFILE_SEP_PATH_VMS wxT('.') // VMS also uses '[' and ']'
288 // separator in the path list (as in PATH environment variable)
289 // there is no PATH variable in Classic Mac OS so just use the
290 // semicolon (it must be different from the file name separator)
291 // NB: these are strings and not characters on purpose!
292 #define wxPATH_SEP_DOS wxT(";")
293 #define wxPATH_SEP_UNIX wxT(":")
294 #define wxPATH_SEP_MAC wxT(";")
296 // platform independent versions
297 #if defined(__UNIX__) && !defined(__CYGWIN__)
298 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_UNIX
299 #define wxPATH_SEP wxPATH_SEP_UNIX
300 #elif defined(__MAC__)
301 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_MAC
302 #define wxPATH_SEP wxPATH_SEP_MAC
303 #elif defined(__CYGWIN__) // Cygwin
304 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_DOS
305 #define wxPATH_SEP wxPATH_SEP_UNIX
306 #else // Windows and OS/2
307 #define wxFILE_SEP_PATH wxFILE_SEP_PATH_DOS
308 #define wxPATH_SEP wxPATH_SEP_DOS
309 #endif // Unix/Windows
311 // this is useful for wxString::IsSameAs(): to compare two file names use
312 // filename1.IsSameAs(filename2, wxARE_FILENAMES_CASE_SENSITIVE)
313 #if defined(__UNIX__) && !defined(__DARWIN__)
314 #define wxARE_FILENAMES_CASE_SENSITIVE TRUE
315 #else // Windows, Mac OS and OS/2
316 #define wxARE_FILENAMES_CASE_SENSITIVE FALSE
317 #endif // Unix/Windows
319 // is the char a path separator?
320 inline bool wxIsPathSeparator(wxChar c
)
322 // under DOS/Windows we should understand both Unix and DOS file separators
323 #if defined(__UNIX__) || defined(__MAC__)
324 return c
== wxFILE_SEP_PATH
;
326 return c
== wxFILE_SEP_PATH_DOS
|| c
== wxFILE_SEP_PATH_UNIX
;
330 // does the string ends with path separator?
331 WXDLLIMPEXP_BASE
bool wxEndsWithPathSeparator(const wxChar
*pszFileName
);
333 // split the full path into path (including drive for DOS), name and extension
334 // (understands both '/' and '\\')
335 WXDLLIMPEXP_BASE
void wxSplitPath(const wxChar
*pszFileName
,
340 // find a file in a list of directories, returns false if not found
341 WXDLLIMPEXP_BASE
bool wxFindFileInPath(wxString
*pStr
, const wxChar
*pszPath
, const wxChar
*pszFile
);
343 // Get the OS directory if appropriate (such as the Windows directory).
344 // On non-Windows platform, probably just return the empty string.
345 WXDLLIMPEXP_BASE wxString
wxGetOSDirectory();
347 // Get file modification time
348 WXDLLIMPEXP_BASE
time_t wxFileModificationTime(const wxString
& filename
);
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
355 class WXDLLIMPEXP_BASE wxPathList
: public wxStringList
358 // avoid GCC warning about virtual functions w/o virtual dtor
359 virtual ~wxPathList() {}
361 // Adds all paths in environment variable
362 void AddEnvList(const wxString
& envVariable
);
364 void Add(const wxString
& path
);
365 // Find the first full path for which the file exists
366 wxString
FindValidPath(const wxString
& filename
);
367 // Find the first full path for which the file exists; ensure it's an
368 // absolute path that gets returned.
369 wxString
FindAbsoluteValidPath(const wxString
& filename
);
370 // Given full path and filename, add path to list
371 void EnsureFileAccessible(const wxString
& path
);
372 // Returns TRUE if the path is in the list
373 bool Member(const wxString
& path
);
376 // DECLARE_DYNAMIC_CLASS(wxPathList)