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