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: filefn.h,v 1.1 2006/12/02 15:58:23 scara Exp $
8 // Copyright:   (c) 1998 Julian Smart
9 // Licence:     wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef   _FILEFN_H_
13 #define   _FILEFN_H_
14 
15 #if defined(__GNUG__) && !defined(__APPLE__)
16     #pragma interface "filefn.h"
17 #endif
18 
19 #include "wx/list.h"
20 
21 #include <time.h>
22 
23 // ----------------------------------------------------------------------------
24 // constants
25 // ----------------------------------------------------------------------------
26 
27 // define off_t
28 #if !defined(__WXMAC__) || defined(__UNIX__)
29     #include  <sys/types.h>
30 #else
31     typedef long off_t;
32 #endif
33 
34 #if defined(__VISUALC__) || ( defined(__MWERKS__) && defined( __INTEL__) )
35     typedef _off_t off_t;
36 #elif defined(__BORLANDC__) && defined(__WIN16__)
37     typedef long off_t;
38 #elif defined(__SC__)
39     typedef long off_t;
40 #elif defined(__MWERKS__) && !defined(__INTEL__)
41     typedef long off_t;
42 #endif
43 
44 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
45 //
46 // VisualAge C++ V4.0 cannot have any external linkage const decs
47 // in headers included by more than one primary source
48 //
49 extern const off_t wxInvalidOffset;
50 #else
51 const off_t wxInvalidOffset = (off_t)-1;
52 #endif
53 
54 enum wxSeekMode
55 {
56   wxFromStart,
57   wxFromCurrent,
58   wxFromEnd
59 };
60 
61 WXDLLEXPORT_DATA(extern const wxChar*) wxEmptyString;
62 
63 // ----------------------------------------------------------------------------
64 // declare our versions of low level file functions: some compilers prepend
65 // underscores to the usual names, some also have Unicode versions of them
66 // ----------------------------------------------------------------------------
67 
68 // Microsoft compiler loves underscores, feed them to it
69 #if defined( __VISUALC__ ) \
70     || ( defined(__MINGW32__) && wxCHECK_W32API_VERSION( 0, 5 ) ) \
71     || ( defined(__MWERKS__) && defined(__WXMSW__) )
72     // functions
73     #define   wxClose      _close
74     #define   wxRead       _read
75     #define   wxWrite      _write
76     #define   wxLseek      _lseek
77     #define   wxFsync      _commit
78     #define   wxEof        _eof
79 
80     #define   wxTell       _tell
81 
82     #if wxUSE_UNICODE
83         #if wxUSE_UNICODE_MSLU
84             #define   wxOpen       wxMSLU__wopen
85             #define   wxAccess     wxMSLU__waccess
86             #define   wxMkDir      wxMSLU__wmkdir
87             #define   wxRmDir      wxMSLU__wrmdir
88             #define   wxStat       wxMSLU__wstat
89         #else
90             #define   wxOpen       _wopen
91             #define   wxAccess     _waccess
92             #define   wxMkDir      _wmkdir
93             #define   wxRmDir      _wrmdir
94             #define   wxStat       _wstat
95         #endif
96     #else // !wxUSE_UNICODE
97         #define   wxOpen       _open
98         #define   wxAccess     _access
99         #define   wxMkDir      _mkdir
100         #define   wxRmDir      _rmdir
101         #define   wxStat       _stat
102     #endif
103 
104     // types
105     #define   wxStructStat struct _stat
106 
107     // constants (unless already defined by the user code)
108     #ifndef O_RDONLY
109         #define   O_RDONLY    _O_RDONLY
110         #define   O_WRONLY    _O_WRONLY
111         #define   O_RDWR      _O_RDWR
112         #define   O_EXCL      _O_EXCL
113         #define   O_CREAT     _O_CREAT
114         #define   O_BINARY    _O_BINARY
115 
116         #define   S_IFMT      _S_IFMT
117         #define   S_IFDIR     _S_IFDIR
118         #define   S_IFREG     _S_IFREG
119     #endif // O_RDONLY
120 #else
121     // functions
122     #define   wxClose      close
123     #define   wxRead       read
124     #define   wxWrite      write
125     #define   wxLseek      lseek
126     #define   wxFsync      commit
127     #define   wxEof        eof
128 
129     #define   wxMkDir      mkdir
130     #define   wxRmDir      rmdir
131 
132     #define   wxTell(fd)   lseek(fd, 0, SEEK_CUR)
133 
134     #define   wxStructStat struct stat
135 
136 #if wxUSE_UNICODE
137 #   define wxNEED_WX_UNISTD_H
138 WXDLLEXPORT int wxStat( const wxChar *file_name, wxStructStat *buf );
139 WXDLLEXPORT int wxAccess( const wxChar *pathname, int mode );
140 WXDLLEXPORT int wxOpen( const wxChar *pathname, int flags, mode_t mode );
141 #else
142     #define   wxOpen       open
143     #define   wxStat       stat
144     #define   wxAccess     access
145 #endif
146 
147 #endif  // VC++
148 
149 // ----------------------------------------------------------------------------
150 // functions
151 // ----------------------------------------------------------------------------
152 WXDLLEXPORT bool wxFileExists(const wxString& filename);
153 
154 // does the path exist? (may have or not '/' or '\\' at the end)
155 WXDLLEXPORT bool wxPathExists(const wxChar *pszPathName);
156 
157 WXDLLEXPORT bool wxIsAbsolutePath(const wxString& filename);
158 
159 // Get filename
160 WXDLLEXPORT wxChar* wxFileNameFromPath(wxChar *path);
161 WXDLLEXPORT wxString wxFileNameFromPath(const wxString& path);
162 
163 // Get directory
164 WXDLLEXPORT wxString wxPathOnly(const wxString& path);
165 
166 // wxString version
167 WXDLLEXPORT wxString wxRealPath(const wxString& path);
168 
169 WXDLLEXPORT void wxDos2UnixFilename(wxChar *s);
170 
171 WXDLLEXPORT void wxUnix2DosFilename(wxChar *s);
172 
173 // Strip the extension, in situ
174 WXDLLEXPORT void wxStripExtension(wxChar *buffer);
175 WXDLLEXPORT void wxStripExtension(wxString& buffer);
176 
177 // Get a temporary filename
178 WXDLLEXPORT wxChar* wxGetTempFileName(const wxString& prefix, wxChar *buf = (wxChar *) NULL);
179 WXDLLEXPORT bool wxGetTempFileName(const wxString& prefix, wxString& buf);
180 
181 // Expand file name (~/ and ${OPENWINHOME}/ stuff)
182 WXDLLEXPORT wxChar* wxExpandPath(wxChar *dest, const wxChar *path);
183 WXDLLEXPORT bool wxExpandPath(wxString& dest, const wxChar *path);
184 
185 // Contract w.r.t environment (</usr/openwin/lib, OPENWHOME> -> ${OPENWINHOME}/lib)
186 // and make (if under the home tree) relative to home
187 // [caller must copy-- volatile]
188 WXDLLEXPORT wxChar* wxContractPath(const wxString& filename,
189                                    const wxString& envname = wxEmptyString,
190                                    const wxString& user = wxEmptyString);
191 
192 // Destructive removal of /./ and /../ stuff
193 WXDLLEXPORT wxChar* wxRealPath(wxChar *path);
194 
195 // Allocate a copy of the full absolute path
196 WXDLLEXPORT wxChar* wxCopyAbsolutePath(const wxString& path);
197 
198 // Get first file name matching given wild card.
199 // Flags are reserved for future use.
200 #define wxFILE  1
201 #define wxDIR   2
202 WXDLLEXPORT wxString wxFindFirstFile(const wxChar *spec, int flags = wxFILE);
203 WXDLLEXPORT wxString wxFindNextFile();
204 
205 // Does the pattern contain wildcards?
206 WXDLLEXPORT bool wxIsWild(const wxString& pattern);
207 
208 // Does the pattern match the text (usually a filename)?
209 // If dot_special is TRUE, doesn't match * against . (eliminating
210 // `hidden' dot files)
211 WXDLLEXPORT bool wxMatchWild(const wxString& pattern,  const wxString& text, bool dot_special = TRUE);
212 
213 // Concatenate two files to form third
214 WXDLLEXPORT bool wxConcatFiles(const wxString& file1, const wxString& file2, const wxString& file3);
215 
216 // Copy file1 to file2
217 WXDLLEXPORT bool wxCopyFile(const wxString& file1, const wxString& file2,
218                             bool overwrite = TRUE);
219 
220 // Remove file
221 WXDLLEXPORT bool wxRemoveFile(const wxString& file);
222 
223 // Rename file
224 WXDLLEXPORT bool wxRenameFile(const wxString& file1, const wxString& file2);
225 
226 // Get current working directory.
227 // If buf is NULL, allocates space using new, else
228 // copies into buf.
229 // IMPORTANT NOTE getcwd is know not to work under some releases
230 // of Win32s 1.3, according to MS release notes!
231 WXDLLEXPORT wxChar* wxGetWorkingDirectory(wxChar *buf = (wxChar *) NULL, int sz = 1000);
232 // new and preferred version of wxGetWorkingDirectory
233 // NB: can't have the same name because of overloading ambiguity
234 WXDLLEXPORT wxString wxGetCwd();
235 
236 // Set working directory
237 WXDLLEXPORT bool wxSetWorkingDirectory(const wxString& d);
238 
239 // Make directory
240 WXDLLEXPORT bool wxMkdir(const wxString& dir, int perm = 0777);
241 
242 // Remove directory. Flags reserved for future use.
243 WXDLLEXPORT bool wxRmdir(const wxString& dir, int flags = 0);
244 
245 // compatibility defines, don't use in new code
246 #define wxDirExists wxPathExists
247 
248 #if WXWIN_COMPATIBILITY_2
249     #define FileExists wxFileExists
250     #define DirExists wxDirExists
251     #define IsAbsolutePath wxIsAbsolutePath
252     #define FileNameFromPath wxFileNameFromPath
253     #define PathOnly wxPathOnly
254     #define Dos2UnixFilename wxDos2UnixFilename
255     #define Unix2DosFilename wxUnix2DosFilename
256 #endif
257 
258 // ----------------------------------------------------------------------------
259 // separators in file names
260 // ----------------------------------------------------------------------------
261 
262 // between file name and extension
263 #define wxFILE_SEP_EXT        wxT('.')
264 
265 // between drive/volume name and the path
266 #define wxFILE_SEP_DSK        wxT(':')
267 
268 // between the path components
269 #define wxFILE_SEP_PATH_DOS   wxT('\\')
270 #define wxFILE_SEP_PATH_UNIX  wxT('/')
271 #define wxFILE_SEP_PATH_MAC   wxT(':')
272 #define wxFILE_SEP_PATH_VMS   wxT('.') // VMS also uses '[' and ']'
273 
274 // separator in the path list (as in PATH environment variable)
275 // there is no PATH variable in Classic Mac OS so just use the
276 // semicolon (it must be different from the file name separator)
277 // NB: these are strings and not characters on purpose!
278 #define wxPATH_SEP_DOS        wxT(";")
279 #define wxPATH_SEP_UNIX       wxT(":")
280 #define wxPATH_SEP_MAC        wxT(";")
281 
282 // platform independent versions
283 #if defined(__UNIX__) && !defined(__CYGWIN__)
284   #define wxFILE_SEP_PATH     wxFILE_SEP_PATH_UNIX
285   #define wxPATH_SEP          wxPATH_SEP_UNIX
286 #elif defined(__MAC__)
287   #define wxFILE_SEP_PATH     wxFILE_SEP_PATH_MAC
288   #define wxPATH_SEP          wxPATH_SEP_MAC
289 #elif defined(__CYGWIN__) // Cygwin
290   #define wxFILE_SEP_PATH     wxFILE_SEP_PATH_DOS
291   #define wxPATH_SEP          wxPATH_SEP_UNIX
292 #else   // Windows and OS/2
293   #define wxFILE_SEP_PATH     wxFILE_SEP_PATH_DOS
294   #define wxPATH_SEP          wxPATH_SEP_DOS
295 #endif  // Unix/Windows
296 
297 // this is useful for wxString::IsSameAs(): to compare two file names use
298 // filename1.IsSameAs(filename2, wxARE_FILENAMES_CASE_SENSITIVE)
299 #if defined(__UNIX__) && !defined(__DARWIN__)
300   #define wxARE_FILENAMES_CASE_SENSITIVE  TRUE
301 #else   // Windows, Mac OS and OS/2
302   #define wxARE_FILENAMES_CASE_SENSITIVE  FALSE
303 #endif  // Unix/Windows
304 
305 // is the char a path separator?
wxIsPathSeparator(wxChar c)306 inline bool wxIsPathSeparator(wxChar c)
307 {
308     // under DOS/Windows we should understand both Unix and DOS file separators
309 #if defined(__UNIX__) || defined(__MAC__)
310     return c == wxFILE_SEP_PATH;
311 #else
312     return c == wxFILE_SEP_PATH_DOS || c == wxFILE_SEP_PATH_UNIX;
313 #endif
314 }
315 
316 // does the string ends with path separator?
317 WXDLLEXPORT bool wxEndsWithPathSeparator(const wxChar *pszFileName);
318 
319 // split the full path into path (including drive for DOS), name and extension
320 // (understands both '/' and '\\')
321 WXDLLEXPORT void wxSplitPath(const wxChar *pszFileName,
322                              wxString *pstrPath,
323                              wxString *pstrName,
324                              wxString *pstrExt);
325 
326 // find a file in a list of directories, returns false if not found
327 WXDLLEXPORT bool wxFindFileInPath(wxString *pStr, const wxChar *pszPath, const wxChar *pszFile);
328 
329 // Get the OS directory if appropriate (such as the Windows directory).
330 // On non-Windows platform, probably just return the empty string.
331 WXDLLEXPORT wxString wxGetOSDirectory();
332 
333 // Get file modification time
334 WXDLLEXPORT time_t wxFileModificationTime(const wxString& filename);
335 
336 // ----------------------------------------------------------------------------
337 // classes
338 // ----------------------------------------------------------------------------
339 
340 // Path searching
341 class WXDLLEXPORT wxPathList : public wxStringList
342 {
343 public:
344     // Adds all paths in environment variable
345     void AddEnvList(const wxString& envVariable);
346 
347     void Add(const wxString& path);
348     // Avoid compiler warning
Add(const wxChar * s)349     wxNode *Add(const wxChar *s) { return wxStringList::Add(s); }
350     // Find the first full path for which the file exists
351     wxString FindValidPath(const wxString& filename);
352     // Find the first full path for which the file exists; ensure it's an
353     // absolute path that gets returned.
354     wxString FindAbsoluteValidPath(const wxString& filename);
355     // Given full path and filename, add path to list
356     void EnsureFileAccessible(const wxString& path);
357     // Returns TRUE if the path is in the list
358     bool Member(const wxString& path);
359 
360 private:
361     DECLARE_DYNAMIC_CLASS(wxPathList)
362 };
363 
364 #endif
365   // _WX_FILEFN_H_
366