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