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