]> git.saurik.com Git - wxWidgets.git/blob - include/wx/filename.h
added wxDC::Set/GetLayoutDirection() and implemented it for wxMSW (patch from Tim...
[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 /*
16 TODO:
17
18 1. support for drives under Windows
19 2. more file operations:
20 a) chmod()
21 b) [acm]time() - get and set
22 c) rename()?
23 3. SameFileAs() function to compare inodes under Unix
24 */
25
26 #include "wx/arrstr.h"
27 #include "wx/filefn.h"
28 #include "wx/datetime.h"
29 #include "wx/intl.h"
30
31 #if wxUSE_FILE
32 class WXDLLIMPEXP_BASE wxFile;
33 #endif
34
35 // ----------------------------------------------------------------------------
36 // constants
37 // ----------------------------------------------------------------------------
38
39 // the various values for the path format: this mainly affects the path
40 // separator but also whether or not the path has the drive part (as under
41 // Windows)
42 enum wxPathFormat
43 {
44 wxPATH_NATIVE = 0, // the path format for the current platform
45 wxPATH_UNIX,
46 wxPATH_BEOS = wxPATH_UNIX,
47 wxPATH_MAC,
48 wxPATH_DOS,
49 wxPATH_WIN = wxPATH_DOS,
50 wxPATH_OS2 = wxPATH_DOS,
51 wxPATH_VMS,
52
53 wxPATH_MAX // Not a valid value for specifying path format
54 };
55
56 // the kind of normalization to do with the file name: these values can be
57 // or'd together to perform several operations at once
58 enum wxPathNormalize
59 {
60 wxPATH_NORM_ENV_VARS = 0x0001, // replace env vars with their values
61 wxPATH_NORM_DOTS = 0x0002, // squeeze all .. and . and prepend cwd
62 wxPATH_NORM_TILDE = 0x0004, // Unix only: replace ~ and ~user
63 wxPATH_NORM_CASE = 0x0008, // if case insensitive => tolower
64 wxPATH_NORM_ABSOLUTE = 0x0010, // make the path absolute
65 wxPATH_NORM_LONG = 0x0020, // make the path the long form
66 wxPATH_NORM_SHORTCUT = 0x0040, // resolve the shortcut, if it is a shortcut
67 wxPATH_NORM_ALL = 0x00ff & ~wxPATH_NORM_CASE
68 };
69
70 // what exactly should GetPath() return?
71 enum
72 {
73 wxPATH_GET_VOLUME = 0x0001, // include the volume if applicable
74 wxPATH_GET_SEPARATOR = 0x0002 // terminate the path with the separator
75 };
76
77 // MkDir flags
78 enum
79 {
80 wxPATH_MKDIR_FULL = 0x0001 // create directories recursively
81 };
82
83 // error code of wxFileName::GetSize()
84 extern wxULongLong wxInvalidSize;
85
86
87
88 // ----------------------------------------------------------------------------
89 // wxFileName: encapsulates a file path
90 // ----------------------------------------------------------------------------
91
92 class WXDLLIMPEXP_BASE wxFileName
93 {
94 public:
95 // constructors and assignment
96
97 // the usual stuff
98 wxFileName() { Clear(); }
99 wxFileName(const wxFileName& filepath) { Assign(filepath); }
100
101 // from a full filename: if it terminates with a '/', a directory path
102 // is contructed (the name will be empty), otherwise a file name and
103 // extension are extracted from it
104 wxFileName( const wxString& fullpath, wxPathFormat format = wxPATH_NATIVE )
105 { Assign( fullpath, format ); }
106
107 // from a directory name and a file name
108 wxFileName(const wxString& path,
109 const wxString& name,
110 wxPathFormat format = wxPATH_NATIVE)
111 { Assign(path, name, format); }
112
113 // from a volume, directory name, file base name and extension
114 wxFileName(const wxString& volume,
115 const wxString& path,
116 const wxString& name,
117 const wxString& ext,
118 wxPathFormat format = wxPATH_NATIVE)
119 { Assign(volume, path, name, ext, format); }
120
121 // from a directory name, file base name and extension
122 wxFileName(const wxString& path,
123 const wxString& name,
124 const wxString& ext,
125 wxPathFormat format = wxPATH_NATIVE)
126 { Assign(path, name, ext, format); }
127
128 // the same for delayed initialization
129
130 void Assign(const wxFileName& filepath);
131
132 void Assign(const wxString& fullpath,
133 wxPathFormat format = wxPATH_NATIVE);
134
135 void Assign(const wxString& volume,
136 const wxString& path,
137 const wxString& name,
138 const wxString& ext,
139 bool hasExt,
140 wxPathFormat format = wxPATH_NATIVE);
141
142 void Assign(const wxString& volume,
143 const wxString& path,
144 const wxString& name,
145 const wxString& ext,
146 wxPathFormat format = wxPATH_NATIVE)
147 { Assign(volume, path, name, ext, !ext.empty(), format); }
148
149 void Assign(const wxString& path,
150 const wxString& name,
151 wxPathFormat format = wxPATH_NATIVE);
152
153 void Assign(const wxString& path,
154 const wxString& name,
155 const wxString& ext,
156 wxPathFormat format = wxPATH_NATIVE);
157
158 void AssignDir(const wxString& dir, wxPathFormat format = wxPATH_NATIVE);
159
160 // assorted assignment operators
161
162 wxFileName& operator=(const wxFileName& filename)
163 { Assign(filename); return *this; }
164
165 wxFileName& operator=(const wxString& filename)
166 { Assign(filename); return *this; }
167
168 // reset all components to default, uninitialized state
169 void Clear();
170
171 // static pseudo constructors
172 static wxFileName FileName(const wxString& file,
173 wxPathFormat format = wxPATH_NATIVE);
174 static wxFileName DirName(const wxString& dir,
175 wxPathFormat format = wxPATH_NATIVE);
176
177 // file tests
178
179 // is the filename valid at all?
180 bool IsOk() const
181 {
182 // we're fine if we have the path or the name or if we're a root dir
183 return m_dirs.size() != 0 || !m_name.empty() || !m_relative ||
184 !m_ext.empty() || m_hasExt;
185 }
186
187 // does the file with this name exists?
188 bool FileExists() const;
189 static bool FileExists( const wxString &file );
190
191 // does the directory with this name exists?
192 bool DirExists() const;
193 static bool DirExists( const wxString &dir );
194
195 // checks on most common flags for files/directories;
196 // more platform-specific features (like e.g. Unix permissions) are not
197 // available in wxFileName
198
199 bool IsDirWritable() const { return wxIsWritable(GetPath()); }
200 static bool IsDirWritable(const wxString &path) { return wxDirExists(path) && wxIsWritable(path); }
201
202 bool IsDirReadable() const { return wxIsReadable(GetPath()); }
203 static bool IsDirReadable(const wxString &path) { return wxDirExists(path) && wxIsReadable(path); }
204
205 // NOTE: IsDirExecutable() is not present because the meaning of "executable"
206 // directory is very platform-dependent and also not so useful
207
208 bool IsFileWritable() const { return wxIsWritable(GetFullPath()); }
209 static bool IsFileWritable(const wxString &path) { return wxFileExists(path) && wxIsWritable(path); }
210
211 bool IsFileReadable() const { return wxIsReadable(GetFullPath()); }
212 static bool IsFileReadable(const wxString &path) { return wxFileExists(path) && wxIsReadable(path); }
213
214 bool IsFileExecutable() const { return wxIsExecutable(GetFullPath()); }
215 static bool IsFileExecutable(const wxString &path) { return wxFileExists(path) && wxIsExecutable(path); }
216
217
218 // time functions
219 #if wxUSE_DATETIME
220 // set the file last access/mod and creation times
221 // (any of the pointers may be NULL)
222 bool SetTimes(const wxDateTime *dtAccess,
223 const wxDateTime *dtMod,
224 const wxDateTime *dtCreate);
225
226 // set the access and modification times to the current moment
227 bool Touch();
228
229 // return the last access, last modification and create times
230 // (any of the pointers may be NULL)
231 bool GetTimes(wxDateTime *dtAccess,
232 wxDateTime *dtMod,
233 wxDateTime *dtCreate) const;
234
235 // convenience wrapper: get just the last mod time of the file
236 wxDateTime GetModificationTime() const
237 {
238 wxDateTime dtMod;
239 (void)GetTimes(NULL, &dtMod, NULL);
240 return dtMod;
241 }
242 #endif // wxUSE_DATETIME
243
244 #ifdef __WXMAC__
245 bool MacSetTypeAndCreator( wxUint32 type , wxUint32 creator ) ;
246 bool MacGetTypeAndCreator( wxUint32 *type , wxUint32 *creator ) ;
247 // gets the 'common' type and creator for a certain extension
248 static bool MacFindDefaultTypeAndCreator( const wxString& ext , wxUint32 *type , wxUint32 *creator ) ;
249 // registers application defined extensions and their default type and creator
250 static void MacRegisterDefaultTypeAndCreator( const wxString& ext , wxUint32 type , wxUint32 creator ) ;
251 // looks up the appropriate type and creator from the registration and then sets
252 bool MacSetDefaultTypeAndCreator() ;
253 #endif
254
255 // various file/dir operations
256
257 // retrieve the value of the current working directory
258 void AssignCwd(const wxString& volume = wxEmptyString);
259 static wxString GetCwd(const wxString& volume = wxEmptyString);
260
261 // change the current working directory
262 bool SetCwd();
263 static bool SetCwd( const wxString &cwd );
264
265 // get the value of user home (Unix only mainly)
266 void AssignHomeDir();
267 static wxString GetHomeDir();
268
269 #if wxUSE_FILE
270 // get a temp file name starting with the specified prefix and open the
271 // file passed to us using this name for writing (atomically if
272 // possible)
273 void AssignTempFileName(const wxString& prefix, wxFile *fileTemp = NULL);
274 static wxString CreateTempFileName(const wxString& prefix,
275 wxFile *fileTemp = NULL);
276 #endif // wxUSE_FILE
277
278 // directory creation and removal.
279 bool Mkdir( int perm = 0777, int flags = 0);
280 static bool Mkdir( const wxString &dir, int perm = 0777, int flags = 0 );
281
282 bool Rmdir();
283 static bool Rmdir( const wxString &dir );
284
285 // operations on the path
286
287 // normalize the path: with the default flags value, the path will be
288 // made absolute, without any ".." and "." and all environment
289 // variables will be expanded in it
290 //
291 // this may be done using another (than current) value of cwd
292 bool Normalize(int flags = wxPATH_NORM_ALL,
293 const wxString& cwd = wxEmptyString,
294 wxPathFormat format = wxPATH_NATIVE);
295
296 // get a path path relative to the given base directory, i.e. opposite
297 // of Normalize
298 //
299 // pass an empty string to get a path relative to the working directory
300 //
301 // returns true if the file name was modified, false if we failed to do
302 // anything with it (happens when the file is on a different volume,
303 // for example)
304 bool MakeRelativeTo(const wxString& pathBase = wxEmptyString,
305 wxPathFormat format = wxPATH_NATIVE);
306
307 // make the path absolute
308 //
309 // this may be done using another (than current) value of cwd
310 bool MakeAbsolute(const wxString& cwd = wxEmptyString,
311 wxPathFormat format = wxPATH_NATIVE)
312 { return Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE |
313 wxPATH_NORM_TILDE, cwd, format); }
314
315 #if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
316 // if the path is a shortcut, return the target and optionally,
317 // the arguments
318 bool GetShortcutTarget(const wxString& shortcutPath,
319 wxString& targetFilename,
320 wxString* arguments = NULL);
321 #endif
322
323 // Comparison
324
325 // compares with the rules of the given platforms format
326 bool SameAs(const wxFileName& filepath,
327 wxPathFormat format = wxPATH_NATIVE) const;
328
329 // compare with another filename object
330 bool operator==(const wxFileName& filename) const
331 { return SameAs(filename); }
332 bool operator!=(const wxFileName& filename) const
333 { return !SameAs(filename); }
334
335 // compare with a filename string interpreted as a native file name
336 bool operator==(const wxString& filename) const
337 { return SameAs(wxFileName(filename)); }
338 bool operator!=(const wxString& filename) const
339 { return !SameAs(wxFileName(filename)); }
340
341 // are the file names of this type cases sensitive?
342 static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE );
343
344 // is this filename absolute?
345 bool IsAbsolute(wxPathFormat format = wxPATH_NATIVE) const;
346
347 // is this filename relative?
348 bool IsRelative(wxPathFormat format = wxPATH_NATIVE) const
349 { return !IsAbsolute(format); }
350
351 // Returns the characters that aren't allowed in filenames
352 // on the specified platform.
353 static wxString GetForbiddenChars(wxPathFormat format = wxPATH_NATIVE);
354
355 // Information about path format
356
357 // get the string separating the volume from the path for this format,
358 // return an empty string if this format doesn't support the notion of
359 // volumes at all
360 static wxString GetVolumeSeparator(wxPathFormat format = wxPATH_NATIVE);
361
362 // get the string of path separators for this format
363 static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE);
364
365 // get the string of path terminators, i.e. characters which terminate the
366 // path
367 static wxString GetPathTerminators(wxPathFormat format = wxPATH_NATIVE);
368
369 // get the canonical path separator for this format
370 static wxChar GetPathSeparator(wxPathFormat format = wxPATH_NATIVE)
371 { return GetPathSeparators(format)[0u]; }
372
373 // is the char a path separator for this format?
374 static bool IsPathSeparator(wxChar ch, wxPathFormat format = wxPATH_NATIVE);
375
376 // Dir accessors
377 size_t GetDirCount() const { return m_dirs.size(); }
378 void AppendDir(const wxString& dir);
379 void PrependDir(const wxString& dir);
380 void InsertDir(size_t before, const wxString& dir);
381 void RemoveDir(size_t pos);
382 void RemoveLastDir() { RemoveDir(GetDirCount() - 1); }
383
384 // Other accessors
385 void SetExt( const wxString &ext ) { m_ext = ext; m_hasExt = !m_ext.empty(); }
386 void ClearExt() { m_ext = wxEmptyString; m_hasExt = false; }
387 void SetEmptyExt() { m_ext = wxT(""); m_hasExt = true; }
388 wxString GetExt() const { return m_ext; }
389 bool HasExt() const { return m_hasExt; }
390
391 void SetName( const wxString &name ) { m_name = name; }
392 wxString GetName() const { return m_name; }
393 bool HasName() const { return !m_name.empty(); }
394
395 void SetVolume( const wxString &volume ) { m_volume = volume; }
396 wxString GetVolume() const { return m_volume; }
397 bool HasVolume() const { return !m_volume.empty(); }
398
399 // full name is the file name + extension (but without the path)
400 void SetFullName(const wxString& fullname);
401 wxString GetFullName() const;
402
403 const wxArrayString& GetDirs() const { return m_dirs; }
404
405 // flags are combination of wxPATH_GET_XXX flags
406 wxString GetPath(int flags = wxPATH_GET_VOLUME,
407 wxPathFormat format = wxPATH_NATIVE) const;
408
409 // Replace current path with this one
410 void SetPath( const wxString &path, wxPathFormat format = wxPATH_NATIVE );
411
412 // Construct full path with name and ext
413 wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const;
414
415 // Return the short form of the path (returns identity on non-Windows platforms)
416 wxString GetShortPath() const;
417
418 // Return the long form of the path (returns identity on non-Windows platforms)
419 wxString GetLongPath() const;
420
421 // Is this a file or directory (not necessarily an existing one)
422 bool IsDir() const { return m_name.empty() && m_ext.empty(); }
423
424 // various helpers
425
426 // get the canonical path format for this platform
427 static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE );
428
429 // split a fullpath into the volume, path, (base) name and extension
430 // (all of the pointers can be NULL)
431 static void SplitPath(const wxString& fullpath,
432 wxString *volume,
433 wxString *path,
434 wxString *name,
435 wxString *ext,
436 bool *hasExt = NULL,
437 wxPathFormat format = wxPATH_NATIVE);
438
439 static void SplitPath(const wxString& fullpath,
440 wxString *volume,
441 wxString *path,
442 wxString *name,
443 wxString *ext,
444 wxPathFormat format)
445 {
446 SplitPath(fullpath, volume, path, name, ext, NULL, format);
447 }
448
449 // compatibility version: volume is part of path
450 static void SplitPath(const wxString& fullpath,
451 wxString *path,
452 wxString *name,
453 wxString *ext,
454 wxPathFormat format = wxPATH_NATIVE);
455
456 // split a path into volume and pure path part
457 static void SplitVolume(const wxString& fullpathWithVolume,
458 wxString *volume,
459 wxString *path,
460 wxPathFormat format = wxPATH_NATIVE);
461
462 // Filesize
463
464 // returns the size of the given filename
465 wxULongLong GetSize() const;
466 static wxULongLong GetSize(const wxString &file);
467
468 // returns the size in a human readable form
469 wxString GetHumanReadableSize(const wxString &nullsize = wxGetTranslation(_T("Not available")),
470 int precision = 1) const;
471 static wxString GetHumanReadableSize(const wxULongLong &sz,
472 const wxString &nullsize = wxGetTranslation(_T("Not available")),
473 int precision = 1);
474
475
476 // deprecated methods, don't use any more
477 // --------------------------------------
478
479 #ifndef __DIGITALMARS__
480 wxString GetPath( bool withSep, wxPathFormat format = wxPATH_NATIVE ) const
481 { return GetPath(withSep ? wxPATH_GET_SEPARATOR : 0, format); }
482 #endif
483 wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const
484 { return GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR, format); }
485
486 private:
487 // check whether this dir is valid for Append/Prepend/InsertDir()
488 static bool IsValidDirComponent(const wxString& dir);
489
490 // the drive/volume/device specification (always empty for Unix)
491 wxString m_volume;
492
493 // the path components of the file
494 wxArrayString m_dirs;
495
496 // the file name and extension (empty for directories)
497 wxString m_name,
498 m_ext;
499
500 // when m_dirs is empty it may mean either that we have no path at all or
501 // that our path is '/', i.e. the root directory
502 //
503 // we use m_relative to distinguish between these two cases, it will be
504 // true in the former and false in the latter
505 //
506 // NB: the path is not absolute just because m_relative is false, it still
507 // needs the drive (i.e. volume) in some formats (Windows)
508 bool m_relative;
509
510 // when m_ext is empty, it may be because we don't have any extension or
511 // because we have an empty extension
512 //
513 // the difference is important as file with name "foo" and without
514 // extension has full name "foo" while with empty extension it is "foo."
515 bool m_hasExt;
516 };
517
518 #endif // _WX_FILENAME_H_
519