]> git.saurik.com Git - wxWidgets.git/blame - include/wx/filename.h
Renamed wx.FutureCall to wx.CallLater so it is named more like
[wxWidgets.git] / include / wx / filename.h
CommitLineData
df5ddbca 1/////////////////////////////////////////////////////////////////////////////
097ead30
VZ
2// Name: wx/filename.h
3// Purpose: wxFileName - encapsulates a file path
844f90fb 4// Author: Robert Roebling, Vadim Zeitlin
df5ddbca
RR
5// Modified by:
6// Created: 28.12.00
7// RCS-ID: $Id$
8// Copyright: (c) 2000 Robert Roebling
65571936 9// Licence: wxWindows licence
df5ddbca
RR
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_FILENAME_H_
13#define _WX_FILENAME_H_
14
9e8d8607
VZ
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
3ff07edb 22 c) rename()?
9e8d8607
VZ
23 3. SameFileAs() function to compare inodes under Unix
24 */
25
20ceebaa 26#include "wx/arrstr.h"
ee66f092 27#include "wx/filefn.h"
ce16e5d7 28#include "wx/datetime.h"
98080d5c 29#include "wx/intl.h"
df5ddbca 30
68351053 31#if wxUSE_FILE
bddd7a8d 32class WXDLLIMPEXP_BASE wxFile;
68351053 33#endif
df22f860 34
097ead30
VZ
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)
df5ddbca
RR
42enum wxPathFormat
43{
844f90fb 44 wxPATH_NATIVE = 0, // the path format for the current platform
097ead30 45 wxPATH_UNIX,
f363e05c 46 wxPATH_BEOS = wxPATH_UNIX,
097ead30
VZ
47 wxPATH_MAC,
48 wxPATH_DOS,
f363e05c
VZ
49 wxPATH_WIN = wxPATH_DOS,
50 wxPATH_OS2 = wxPATH_DOS,
3c621059 51 wxPATH_VMS,
ee66f092 52
f363e05c 53 wxPATH_MAX // Not a valid value for specifying path format
097ead30 54};
ee66f092 55
097ead30
VZ
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
58enum wxPathNormalize
59{
60 wxPATH_NORM_ENV_VARS = 0x0001, // replace env vars with their values
844f90fb 61 wxPATH_NORM_DOTS = 0x0002, // squeeze all .. and . and prepend cwd
097ead30 62 wxPATH_NORM_TILDE = 0x0004, // Unix only: replace ~ and ~user
844f90fb
VZ
63 wxPATH_NORM_CASE = 0x0008, // if case insensitive => tolower
64 wxPATH_NORM_ABSOLUTE = 0x0010, // make the path absolute
9e9b65c1 65 wxPATH_NORM_LONG = 0x0020, // make the path the long form
21f60945
JS
66 wxPATH_NORM_SHORTCUT = 0x0040, // resolve the shortcut, if it is a shortcut
67 wxPATH_NORM_ALL = 0x00ff & ~wxPATH_NORM_CASE
df5ddbca
RR
68};
69
33b97389
VZ
70// what exactly should GetPath() return?
71enum
72{
73 wxPATH_GET_VOLUME = 0x0001, // include the volume if applicable
74 wxPATH_GET_SEPARATOR = 0x0002 // terminate the path with the separator
75};
76
1527281e
VZ
77// MkDir flags
78enum
79{
80 wxPATH_MKDIR_FULL = 0x0001 // create directories recursively
81};
82
98080d5c
JS
83// error code of wxFileName::GetSize()
84extern wxULongLong wxInvalidSize;
85
86
3ff07edb 87
097ead30
VZ
88// ----------------------------------------------------------------------------
89// wxFileName: encapsulates a file path
90// ----------------------------------------------------------------------------
91
bddd7a8d 92class WXDLLIMPEXP_BASE wxFileName
df5ddbca
RR
93{
94public:
95 // constructors and assignment
844f90fb
VZ
96
97 // the usual stuff
fb969475 98 wxFileName() { Clear(); }
520200fd 99 wxFileName(const wxFileName& filepath) { Assign(filepath); }
844f90fb
VZ
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
81f25632
VZ
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
844f90fb
VZ
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
844f90fb
VZ
128 // the same for delayed initialization
129
844f90fb 130 void Assign(const wxFileName& filepath);
04c943b1 131
844f90fb
VZ
132 void Assign(const wxString& fullpath,
133 wxPathFormat format = wxPATH_NATIVE);
04c943b1
VZ
134
135 void Assign(const wxString& volume,
136 const wxString& path,
137 const wxString& name,
138 const wxString& ext,
dfecbee5 139 bool hasExt,
04c943b1
VZ
140 wxPathFormat format = wxPATH_NATIVE);
141
dfecbee5
VZ
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
844f90fb
VZ
149 void Assign(const wxString& path,
150 const wxString& name,
151 wxPathFormat format = wxPATH_NATIVE);
04c943b1 152
844f90fb
VZ
153 void Assign(const wxString& path,
154 const wxString& name,
155 const wxString& ext,
4c2deb19 156 wxPathFormat format = wxPATH_NATIVE);
04c943b1 157
52dbd056 158 void AssignDir(const wxString& dir, wxPathFormat format = wxPATH_NATIVE);
844f90fb 159
788722ac
JS
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
844f90fb
VZ
168 // reset all components to default, uninitialized state
169 void Clear();
170
171 // static pseudo constructors
520200fd
VZ
172 static wxFileName FileName(const wxString& file,
173 wxPathFormat format = wxPATH_NATIVE);
174 static wxFileName DirName(const wxString& dir,
175 wxPathFormat format = wxPATH_NATIVE);
844f90fb 176
951cd180 177 // file tests
844f90fb
VZ
178
179 // is the filename valid at all?
a96b8d39 180 bool IsOk() const
a62848fd 181 {
a96b8d39 182 // we're fine if we have the path or the name or if we're a root dir
27b2dd53 183 return m_dirs.size() != 0 || !m_name.empty() || !m_relative ||
dfecbee5 184 !m_ext.empty() || m_hasExt;
a96b8d39 185 }
844f90fb
VZ
186
187 // does the file with this name exists?
8e41796c 188 bool FileExists() const;
a35b27b1 189 static bool FileExists( const wxString &file );
097ead30 190
844f90fb 191 // does the directory with this name exists?
8e41796c 192 bool DirExists() const;
a35b27b1 193 static bool DirExists( const wxString &dir );
097ead30 194
3ff07edb
RR
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
844f90fb 217
951cd180 218 // time functions
e2b87f38 219#if wxUSE_DATETIME
6dbb903b 220 // set the file last access/mod and creation times
951cd180 221 // (any of the pointers may be NULL)
6dbb903b
VZ
222 bool SetTimes(const wxDateTime *dtAccess,
223 const wxDateTime *dtMod,
224 const wxDateTime *dtCreate);
951cd180
VZ
225
226 // set the access and modification times to the current moment
227 bool Touch();
228
6dbb903b 229 // return the last access, last modification and create times
951cd180
VZ
230 // (any of the pointers may be NULL)
231 bool GetTimes(wxDateTime *dtAccess,
232 wxDateTime *dtMod,
6dbb903b 233 wxDateTime *dtCreate) const;
951cd180
VZ
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 }
e2b87f38 242#endif // wxUSE_DATETIME
951cd180 243
4dfbcdd5
SC
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
fb969475 254
844f90fb
VZ
255 // various file/dir operations
256
257 // retrieve the value of the current working directory
6f91bc33
VZ
258 void AssignCwd(const wxString& volume = wxEmptyString);
259 static wxString GetCwd(const wxString& volume = wxEmptyString);
097ead30 260
844f90fb 261 // change the current working directory
a35b27b1
RR
262 bool SetCwd();
263 static bool SetCwd( const wxString &cwd );
097ead30 264
844f90fb 265 // get the value of user home (Unix only mainly)
a35b27b1
RR
266 void AssignHomeDir();
267 static wxString GetHomeDir();
097ead30 268
68351053 269#if wxUSE_FILE
df22f860
VZ
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);
68351053 276#endif // wxUSE_FILE
097ead30 277
f0ce3409 278 // directory creation and removal.
1527281e
VZ
279 bool Mkdir( int perm = 0777, int flags = 0);
280 static bool Mkdir( const wxString &dir, int perm = 0777, int flags = 0 );
097ead30 281
a35b27b1
RR
282 bool Rmdir();
283 static bool Rmdir( const wxString &dir );
097ead30 284
844f90fb
VZ
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
32a0d013 292 bool Normalize(int flags = wxPATH_NORM_ALL,
844f90fb
VZ
293 const wxString& cwd = wxEmptyString,
294 wxPathFormat format = wxPATH_NATIVE);
097ead30 295
f7d886af
VZ
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 //
f363e05c 301 // returns true if the file name was modified, false if we failed to do
f7d886af
VZ
302 // anything with it (happens when the file is on a different volume,
303 // for example)
fda7962d 304 bool MakeRelativeTo(const wxString& pathBase = wxEmptyString,
f7d886af
VZ
305 wxPathFormat format = wxPATH_NATIVE);
306
0894707e
VS
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); }
f7d886af 314
ff95fdbb 315#if defined(__WIN32__) && !defined(__WXWINCE__) && wxUSE_OLE
21f60945
JS
316 // if the path is a shortcut, return the target and optionally,
317 // the arguments
ff95fdbb
VS
318 bool GetShortcutTarget(const wxString& shortcutPath,
319 wxString& targetFilename,
320 wxString* arguments = NULL);
21f60945
JS
321#endif
322
df5ddbca 323 // Comparison
844f90fb 324
2b5f62a0
VZ
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)); }
844f90fb 340
04c943b1 341 // are the file names of this type cases sensitive?
844f90fb 342 static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE );
04c943b1
VZ
343
344 // is this filename absolute?
6d3d1c2e 345 bool IsAbsolute(wxPathFormat format = wxPATH_NATIVE) const;
844f90fb 346
04c943b1 347 // is this filename relative?
6d3d1c2e
VZ
348 bool IsRelative(wxPathFormat format = wxPATH_NATIVE) const
349 { return !IsAbsolute(format); }
04c943b1 350
f363e05c
VZ
351 // Returns the characters that aren't allowed in filenames
352 // on the specified platform.
353 static wxString GetForbiddenChars(wxPathFormat format = wxPATH_NATIVE);
354
04c943b1
VZ
355 // Information about path format
356
6d3d1c2e
VZ
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
04c943b1
VZ
360 static wxString GetVolumeSeparator(wxPathFormat format = wxPATH_NATIVE);
361
844f90fb
VZ
362 // get the string of path separators for this format
363 static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE);
364
f1e77933
VZ
365 // get the string of path terminators, i.e. characters which terminate the
366 // path
367 static wxString GetPathTerminators(wxPathFormat format = wxPATH_NATIVE);
368
6d3d1c2e
VZ
369 // get the canonical path separator for this format
370 static wxChar GetPathSeparator(wxPathFormat format = wxPATH_NATIVE)
371 { return GetPathSeparators(format)[0u]; }
372
844f90fb
VZ
373 // is the char a path separator for this format?
374 static bool IsPathSeparator(wxChar ch, wxPathFormat format = wxPATH_NATIVE);
375
df5ddbca 376 // Dir accessors
df5168c4 377 size_t GetDirCount() const { return m_dirs.size(); }
2458d90b
VZ
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); }
097ead30 383
df5ddbca 384 // Other accessors
0b40f3d8
RR
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; }
df5ddbca 388 wxString GetExt() const { return m_ext; }
4d4edca3 389 bool HasExt() const { return m_hasExt; }
097ead30 390
df5ddbca
RR
391 void SetName( const wxString &name ) { m_name = name; }
392 wxString GetName() const { return m_name; }
04c943b1
VZ
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(); }
097ead30 398
7124df9b
VZ
399 // full name is the file name + extension (but without the path)
400 void SetFullName(const wxString& fullname);
844f90fb 401 wxString GetFullName() const;
097ead30 402
04c943b1 403 const wxArrayString& GetDirs() const { return m_dirs; }
097ead30 404
33b97389 405 // flags are combination of wxPATH_GET_XXX flags
93fa67c0
VZ
406 wxString GetPath(int flags = wxPATH_GET_VOLUME,
407 wxPathFormat format = wxPATH_NATIVE) const;
33b97389 408
a7b51bc8
RR
409 // Replace current path with this one
410 void SetPath( const wxString &path, wxPathFormat format = wxPATH_NATIVE );
097ead30 411
df5ddbca
RR
412 // Construct full path with name and ext
413 wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const;
097ead30 414
9e9b65c1
JS
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
2db991f4
VZ
421 // Is this a file or directory (not necessarily an existing one)
422 bool IsDir() const { return m_name.empty() && m_ext.empty(); }
423
9e8d8607
VZ
424 // various helpers
425
426 // get the canonical path format for this platform
df5ddbca 427 static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE );
097ead30 428
04c943b1
VZ
429 // split a fullpath into the volume, path, (base) name and extension
430 // (all of the pointers can be NULL)
9e8d8607 431 static void SplitPath(const wxString& fullpath,
04c943b1 432 wxString *volume,
9e8d8607
VZ
433 wxString *path,
434 wxString *name,
435 wxString *ext,
dfecbee5 436 bool *hasExt = NULL,
9e8d8607
VZ
437 wxPathFormat format = wxPATH_NATIVE);
438
dfecbee5
VZ
439 static void SplitPath(const wxString& fullpath,
440 wxString *volume,
441 wxString *path,
442 wxString *name,
443 wxString *ext,
1a6255e6 444 wxPathFormat format)
dfecbee5
VZ
445 {
446 SplitPath(fullpath, volume, path, name, ext, NULL, format);
447 }
448
449 // compatibility version: volume is part of path
04c943b1
VZ
450 static void SplitPath(const wxString& fullpath,
451 wxString *path,
452 wxString *name,
453 wxString *ext,
6f91bc33 454 wxPathFormat format = wxPATH_NATIVE);
04c943b1 455
f1e77933
VZ
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);
33b97389 461
98080d5c
JS
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
64fb0e20 469 wxString GetHumanReadableSize(const wxString &nullsize = wxGetTranslation(_T("Not available")),
98080d5c
JS
470 int precision = 1) const;
471 static wxString GetHumanReadableSize(const wxULongLong &sz,
64fb0e20 472 const wxString &nullsize = wxGetTranslation(_T("Not available")),
98080d5c
JS
473 int precision = 1);
474
475
33b97389
VZ
476 // deprecated methods, don't use any more
477 // --------------------------------------
478
4ce1efe1 479#ifndef __DIGITALMARS__
33b97389
VZ
480 wxString GetPath( bool withSep, wxPathFormat format = wxPATH_NATIVE ) const
481 { return GetPath(withSep ? wxPATH_GET_SEPARATOR : 0, format); }
4ce1efe1 482#endif
33b97389 483 wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const
ab9a58f4 484 { return GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR, format); }
33b97389 485
df5ddbca 486private:
5bb9aeb2
VZ
487 // check whether this dir is valid for Append/Prepend/InsertDir()
488 static bool IsValidDirComponent(const wxString& dir);
489
04c943b1
VZ
490 // the drive/volume/device specification (always empty for Unix)
491 wxString m_volume;
492
844f90fb 493 // the path components of the file
df5ddbca 494 wxArrayString m_dirs;
844f90fb
VZ
495
496 // the file name and extension (empty for directories)
497 wxString m_name,
498 m_ext;
6d3d1c2e
VZ
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
f363e05c 504 // true in the former and false in the latter
6d3d1c2e 505 //
f363e05c 506 // NB: the path is not absolute just because m_relative is false, it still
6d3d1c2e 507 // needs the drive (i.e. volume) in some formats (Windows)
353f41cb 508 bool m_relative;
dfecbee5
VZ
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;
df5ddbca
RR
516};
517
097ead30 518#endif // _WX_FILENAME_H_
df5ddbca 519