Applied patch [ 761138 ] Replaces references to wxT("") and _T("") with wxEmptyString
[wxWidgets.git] / include / wx / filename.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/filename.h
3 // Purpose: wxFileName - encapsulates a file path
4 // Author: Robert Roebling, Vadim Zeitlin
5 // Modified by:
6 // Created: 28.12.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_FILENAME_H_
13 #define _WX_FILENAME_H_
14
15 #if defined(__GNUG__) && !defined(__APPLE__)
16 #pragma interface "filename.h"
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/string.h"
21 #include "wx/arrstr.h"
22 #endif
23
24 /*
25 TODO:
26
27 1. support for drives under Windows
28 2. more file operations:
29 a) chmod()
30 b) [acm]time() - get and set
31 c) file size
32 d) file permissions with readable accessors for most common bits
33 such as IsReadable() &c
34 e) rename()?
35 3. SameFileAs() function to compare inodes under Unix
36 */
37
38 // ridiculously enough, this will replace DirExists with wxDirExists etc
39 #include "wx/filefn.h"
40 #include "wx/datetime.h"
41
42 class WXDLLIMPEXP_BASE wxFile;
43
44 // ----------------------------------------------------------------------------
45 // constants
46 // ----------------------------------------------------------------------------
47
48 // the various values for the path format: this mainly affects the path
49 // separator but also whether or not the path has the drive part (as under
50 // Windows)
51 enum wxPathFormat
52 {
53 wxPATH_NATIVE = 0, // the path format for the current platform
54 wxPATH_UNIX,
55 wxPATH_MAC,
56 wxPATH_DOS,
57 wxPATH_VMS,
58
59 wxPATH_BEOS = wxPATH_UNIX,
60 wxPATH_WIN = wxPATH_DOS,
61 wxPATH_OS2 = wxPATH_DOS
62 };
63
64 // the kind of normalization to do with the file name: these values can be
65 // or'd together to perform several operations at once
66 enum wxPathNormalize
67 {
68 wxPATH_NORM_ENV_VARS = 0x0001, // replace env vars with their values
69 wxPATH_NORM_DOTS = 0x0002, // squeeze all .. and . and prepend cwd
70 wxPATH_NORM_TILDE = 0x0004, // Unix only: replace ~ and ~user
71 wxPATH_NORM_CASE = 0x0008, // if case insensitive => tolower
72 wxPATH_NORM_ABSOLUTE = 0x0010, // make the path absolute
73 wxPATH_NORM_LONG = 0x0020, // make the path the long form
74 wxPATH_NORM_ALL = 0x003f
75 };
76
77 // what exactly should GetPath() return?
78 enum
79 {
80 wxPATH_GET_VOLUME = 0x0001, // include the volume if applicable
81 wxPATH_GET_SEPARATOR = 0x0002 // terminate the path with the separator
82 };
83
84 // MkDir flags
85 enum
86 {
87 wxPATH_MKDIR_FULL = 0x0001 // create directories recursively
88 };
89
90 // ----------------------------------------------------------------------------
91 // wxFileName: encapsulates a file path
92 // ----------------------------------------------------------------------------
93
94 class WXDLLIMPEXP_BASE wxFileName
95 {
96 public:
97 // constructors and assignment
98
99 // the usual stuff
100 wxFileName() { Clear(); }
101 wxFileName( const wxFileName &filepath ) { Assign(filepath); }
102
103 // from a full filename: if it terminates with a '/', a directory path
104 // is contructed (the name will be empty), otherwise a file name and
105 // extension are extracted from it
106 wxFileName( const wxString& fullpath, wxPathFormat format = wxPATH_NATIVE )
107 { Assign( fullpath, format ); }
108
109 // from a directory name and a file name
110 wxFileName(const wxString& path,
111 const wxString& name,
112 wxPathFormat format = wxPATH_NATIVE)
113 { Assign(path, name, format); }
114
115 // from a volume, directory name, file base name and extension
116 wxFileName(const wxString& volume,
117 const wxString& path,
118 const wxString& name,
119 const wxString& ext,
120 wxPathFormat format = wxPATH_NATIVE)
121 { Assign(volume, path, name, ext, format); }
122
123 // from a directory name, file base name and extension
124 wxFileName(const wxString& path,
125 const wxString& name,
126 const wxString& ext,
127 wxPathFormat format = wxPATH_NATIVE)
128 { Assign(path, name, ext, format); }
129
130 // the same for delayed initialization
131
132 void Assign(const wxFileName& filepath);
133
134 void Assign(const wxString& fullpath,
135 wxPathFormat format = wxPATH_NATIVE);
136
137 void Assign(const wxString& volume,
138 const wxString& path,
139 const wxString& name,
140 const wxString& ext,
141 wxPathFormat format = wxPATH_NATIVE);
142
143 void Assign(const wxString& path,
144 const wxString& name,
145 wxPathFormat format = wxPATH_NATIVE);
146
147 void Assign(const wxString& path,
148 const wxString& name,
149 const wxString& ext,
150 wxPathFormat format = wxPATH_NATIVE)
151 {
152 // empty volume
153 Assign(wxEmptyString, path, name, ext, format);
154 }
155
156 void AssignDir(const wxString& dir, wxPathFormat format = wxPATH_NATIVE);
157
158 // assorted assignment operators
159
160 wxFileName& operator=(const wxFileName& filename)
161 { Assign(filename); return *this; }
162
163 wxFileName& operator=(const wxString& filename)
164 { Assign(filename); return *this; }
165
166 // reset all components to default, uninitialized state
167 void Clear();
168
169 // static pseudo constructors
170 static wxFileName FileName(const wxString& file);
171 static wxFileName DirName(const wxString& dir);
172
173 // file tests
174
175 // is the filename valid at all?
176 bool IsOk() const { return m_dirs.size() != 0 || !m_name.IsEmpty(); }
177
178 // does the file with this name exists?
179 bool FileExists() const;
180 static bool FileExists( const wxString &file );
181
182 // does the directory with this name exists?
183 bool DirExists() const;
184 static bool DirExists( const wxString &dir );
185
186 // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO)
187
188 // time functions
189 #if wxUSE_DATETIME
190 // set the file last access/mod and creation times
191 // (any of the pointers may be NULL)
192 bool SetTimes(const wxDateTime *dtAccess,
193 const wxDateTime *dtMod,
194 const wxDateTime *dtCreate);
195
196 // set the access and modification times to the current moment
197 bool Touch();
198
199 // return the last access, last modification and create times
200 // (any of the pointers may be NULL)
201 bool GetTimes(wxDateTime *dtAccess,
202 wxDateTime *dtMod,
203 wxDateTime *dtCreate) const;
204
205 // convenience wrapper: get just the last mod time of the file
206 wxDateTime GetModificationTime() const
207 {
208 wxDateTime dtMod;
209 (void)GetTimes(NULL, &dtMod, NULL);
210 return dtMod;
211 }
212 #endif // wxUSE_DATETIME
213
214 #ifdef __WXMAC__
215 bool MacSetTypeAndCreator( wxUint32 type , wxUint32 creator ) ;
216 bool MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator ) ;
217 // gets the 'common' type and creator for a certain extension
218 static bool MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator ) ;
219 // registers application defined extensions and their default type and creator
220 static void MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator ) ;
221 // looks up the appropriate type and creator from the registration and then sets
222 bool MacSetDefaultTypeAndCreator() ;
223 #endif
224
225 // various file/dir operations
226
227 // retrieve the value of the current working directory
228 void AssignCwd(const wxString& volume = wxEmptyString);
229 static wxString GetCwd(const wxString& volume = wxEmptyString);
230
231 // change the current working directory
232 bool SetCwd();
233 static bool SetCwd( const wxString &cwd );
234
235 // get the value of user home (Unix only mainly)
236 void AssignHomeDir();
237 static wxString GetHomeDir();
238
239 // get a temp file name starting with the specified prefix and open the
240 // file passed to us using this name for writing (atomically if
241 // possible)
242 void AssignTempFileName(const wxString& prefix, wxFile *fileTemp = NULL);
243 static wxString CreateTempFileName(const wxString& prefix,
244 wxFile *fileTemp = NULL);
245
246 // directory creation and removal.
247 // if full is TRUE, will try to make each directory in the path.
248 bool Mkdir( int perm = 0777, int flags = 0);
249 static bool Mkdir( const wxString &dir, int perm = 0777, int flags = 0 );
250
251 bool Rmdir();
252 static bool Rmdir( const wxString &dir );
253
254 // operations on the path
255
256 // normalize the path: with the default flags value, the path will be
257 // made absolute, without any ".." and "." and all environment
258 // variables will be expanded in it
259 //
260 // this may be done using another (than current) value of cwd
261 bool Normalize(int flags = wxPATH_NORM_ALL,
262 const wxString& cwd = wxEmptyString,
263 wxPathFormat format = wxPATH_NATIVE);
264
265 // get a path path relative to the given base directory, i.e. opposite
266 // of Normalize
267 //
268 // pass an empty string to get a path relative to the working directory
269 //
270 // returns TRUE if the file name was modified, FALSE if we failed to do
271 // anything with it (happens when the file is on a different volume,
272 // for example)
273 bool MakeRelativeTo(const wxString& pathBase = wxEmptyString,
274 wxPathFormat format = wxPATH_NATIVE);
275
276 // make the path absolute
277 //
278 // this may be done using another (than current) value of cwd
279 bool MakeAbsolute(const wxString& cwd = wxEmptyString,
280 wxPathFormat format = wxPATH_NATIVE)
281 { return Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE |
282 wxPATH_NORM_TILDE, cwd, format); }
283
284 // Comparison
285
286 // compares with the rules of the given platforms format
287 bool SameAs(const wxFileName& filepath,
288 wxPathFormat format = wxPATH_NATIVE) const;
289
290 // compare with another filename object
291 bool operator==(const wxFileName& filename) const
292 { return SameAs(filename); }
293 bool operator!=(const wxFileName& filename) const
294 { return !SameAs(filename); }
295
296 // compare with a filename string interpreted as a native file name
297 bool operator==(const wxString& filename) const
298 { return SameAs(wxFileName(filename)); }
299 bool operator!=(const wxString& filename) const
300 { return !SameAs(wxFileName(filename)); }
301
302 // are the file names of this type cases sensitive?
303 static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE );
304
305 // is this filename absolute?
306 bool IsAbsolute(wxPathFormat format = wxPATH_NATIVE) const;
307
308 // is this filename relative?
309 bool IsRelative(wxPathFormat format = wxPATH_NATIVE) const
310 { return !IsAbsolute(format); }
311
312 // Information about path format
313
314 // get the string separating the volume from the path for this format,
315 // return an empty string if this format doesn't support the notion of
316 // volumes at all
317 static wxString GetVolumeSeparator(wxPathFormat format = wxPATH_NATIVE);
318
319 // get the string of path separators for this format
320 static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE);
321
322 // get the canonical path separator for this format
323 static wxChar GetPathSeparator(wxPathFormat format = wxPATH_NATIVE)
324 { return GetPathSeparators(format)[0u]; }
325
326 // is the char a path separator for this format?
327 static bool IsPathSeparator(wxChar ch, wxPathFormat format = wxPATH_NATIVE);
328
329 // Dir accessors
330 void AppendDir( const wxString &dir );
331 void PrependDir( const wxString &dir );
332 void InsertDir( int before, const wxString &dir );
333 void RemoveDir( int pos );
334 size_t GetDirCount() const { return m_dirs.size(); }
335
336 // Other accessors
337 void SetExt( const wxString &ext ) { m_ext = ext; }
338 wxString GetExt() const { return m_ext; }
339 bool HasExt() const { return !m_ext.empty(); }
340
341 void SetName( const wxString &name ) { m_name = name; }
342 wxString GetName() const { return m_name; }
343 bool HasName() const { return !m_name.empty(); }
344
345 void SetVolume( const wxString &volume ) { m_volume = volume; }
346 wxString GetVolume() const { return m_volume; }
347 bool HasVolume() const { return !m_volume.empty(); }
348
349 // full name is the file name + extension (but without the path)
350 void SetFullName(const wxString& fullname);
351 wxString GetFullName() const;
352
353 const wxArrayString& GetDirs() const { return m_dirs; }
354
355 // flags are combination of wxPATH_GET_XXX flags
356 wxString GetPath(int flags = 0, wxPathFormat format = wxPATH_NATIVE) const;
357
358 // Replace current path with this one
359 void SetPath( const wxString &path, wxPathFormat format = wxPATH_NATIVE );
360
361 // Construct full path with name and ext
362 wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const;
363
364 // Return the short form of the path (returns identity on non-Windows platforms)
365 wxString GetShortPath() const;
366
367 // Return the long form of the path (returns identity on non-Windows platforms)
368 wxString GetLongPath() const;
369
370 // Is this a file or directory (not necessarily an existing one)
371 bool IsDir() const { return m_name.empty() && m_ext.empty(); }
372
373 // various helpers
374
375 // get the canonical path format for this platform
376 static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE );
377
378 // split a fullpath into the volume, path, (base) name and extension
379 // (all of the pointers can be NULL)
380 static void SplitPath(const wxString& fullpath,
381 wxString *volume,
382 wxString *path,
383 wxString *name,
384 wxString *ext,
385 wxPathFormat format = wxPATH_NATIVE);
386
387 // compatibility version
388 static void SplitPath(const wxString& fullpath,
389 wxString *path,
390 wxString *name,
391 wxString *ext,
392 wxPathFormat format = wxPATH_NATIVE);
393
394
395 // deprecated methods, don't use any more
396 // --------------------------------------
397
398 #ifndef __DIGITALMARS__
399 wxString GetPath( bool withSep, wxPathFormat format = wxPATH_NATIVE ) const
400 { return GetPath(withSep ? wxPATH_GET_SEPARATOR : 0, format); }
401 #endif
402 wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const
403 { return GetPath(wxPATH_GET_SEPARATOR, format); }
404
405 private:
406 // the drive/volume/device specification (always empty for Unix)
407 wxString m_volume;
408
409 // the path components of the file
410 wxArrayString m_dirs;
411
412 // the file name and extension (empty for directories)
413 wxString m_name,
414 m_ext;
415
416 // when m_dirs is empty it may mean either that we have no path at all or
417 // that our path is '/', i.e. the root directory
418 //
419 // we use m_relative to distinguish between these two cases, it will be
420 // TRUE in the former and FALSE in the latter
421 //
422 // NB: the path is not absolute just because m_relative is FALSE, it still
423 // needs the drive (i.e. volume) in some formats (Windows)
424 bool m_relative;
425 };
426
427 #endif // _WX_FILENAME_H_
428