]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/filename.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileName - encapsulates a file path
4 // Author: Robert Roebling, Vadim Zeitlin
8 // Copyright: (c) 2000 Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_FILENAME_H_
13 #define _WX_FILENAME_H_
16 #pragma interface "filename.h"
20 #include "wx/string.h"
26 1. support for drives under Windows
27 2. more file operations:
29 b) [acm]time() - get and set
31 d) file permissions with readable accessors for most common bits
32 such as IsReadable() &c
34 3. SameFileAs() function to compare inodes under Unix
37 // ridiculously enough, this will replace DirExists with wxDirExists etc
38 #include "wx/filefn.h"
39 #include "wx/datetime.h"
41 class WXDLLEXPORT wxFile
;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // the various values for the path format: this mainly affects the path
48 // separator but also whether or not the path has the drive part (as under
52 wxPATH_NATIVE
= 0, // the path format for the current platform
58 wxPATH_BEOS
= wxPATH_UNIX
,
59 wxPATH_WIN
= wxPATH_DOS
,
60 wxPATH_OS2
= wxPATH_DOS
63 // the kind of normalization to do with the file name: these values can be
64 // or'd together to perform several operations at once
67 wxPATH_NORM_ENV_VARS
= 0x0001, // replace env vars with their values
68 wxPATH_NORM_DOTS
= 0x0002, // squeeze all .. and . and prepend cwd
69 wxPATH_NORM_TILDE
= 0x0004, // Unix only: replace ~ and ~user
70 wxPATH_NORM_CASE
= 0x0008, // if case insensitive => tolower
71 wxPATH_NORM_ABSOLUTE
= 0x0010, // make the path absolute
72 wxPATH_NORM_LONG
= 0x0020, // make the path the long form
73 wxPATH_NORM_ALL
= 0x003f
76 // ----------------------------------------------------------------------------
77 // wxFileName: encapsulates a file path
78 // ----------------------------------------------------------------------------
80 class WXDLLEXPORT wxFileName
83 // constructors and assignment
87 wxFileName( const wxFileName
&filepath
) { Assign(filepath
); }
89 // from a full filename: if it terminates with a '/', a directory path
90 // is contructed (the name will be empty), otherwise a file name and
91 // extension are extracted from it
92 wxFileName( const wxString
& fullpath
, wxPathFormat format
= wxPATH_NATIVE
)
93 { Assign( fullpath
, format
); }
95 // from a directory name and a file name
96 wxFileName(const wxString
& path
,
98 wxPathFormat format
= wxPATH_NATIVE
)
99 { Assign(path
, name
, format
); }
101 // from a volume, directory name, file base name and extension
102 wxFileName(const wxString
& volume
,
103 const wxString
& path
,
104 const wxString
& name
,
106 wxPathFormat format
= wxPATH_NATIVE
)
107 { Assign(volume
, path
, name
, ext
, format
); }
109 // from a directory name, file base name and extension
110 wxFileName(const wxString
& path
,
111 const wxString
& name
,
113 wxPathFormat format
= wxPATH_NATIVE
)
114 { Assign(path
, name
, ext
, format
); }
116 // the same for delayed initialization
118 void Assign(const wxFileName
& filepath
);
120 void Assign(const wxString
& fullpath
,
121 wxPathFormat format
= wxPATH_NATIVE
);
123 void Assign(const wxString
& volume
,
124 const wxString
& path
,
125 const wxString
& name
,
127 wxPathFormat format
= wxPATH_NATIVE
);
129 void Assign(const wxString
& path
,
130 const wxString
& name
,
131 wxPathFormat format
= wxPATH_NATIVE
);
133 void Assign(const wxString
& path
,
134 const wxString
& name
,
136 wxPathFormat format
= wxPATH_NATIVE
)
139 Assign(_T(""), path
, name
, ext
, format
);
142 void AssignDir(const wxString
& dir
, wxPathFormat format
= wxPATH_NATIVE
);
144 // assorted assignment operators
146 wxFileName
& operator=(const wxFileName
& filename
)
147 { Assign(filename
); return *this; }
149 wxFileName
& operator=(const wxString
& filename
)
150 { Assign(filename
); return *this; }
152 // reset all components to default, uninitialized state
155 // static pseudo constructors
156 static wxFileName
FileName(const wxString
& file
);
157 static wxFileName
DirName(const wxString
& dir
);
161 // is the filename valid at all?
162 bool IsOk() const { return !m_dirs
.IsEmpty() || !m_name
.IsEmpty(); }
164 // does the file with this name exists?
166 static bool FileExists( const wxString
&file
);
168 // does the directory with this name exists?
170 static bool DirExists( const wxString
&dir
);
172 // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO)
176 // set the file last access/mod and creation times
177 // (any of the pointers may be NULL)
178 bool SetTimes(const wxDateTime
*dtAccess
,
179 const wxDateTime
*dtMod
,
180 const wxDateTime
*dtCreate
);
182 // set the access and modification times to the current moment
185 // return the last access, last modification and create times
186 // (any of the pointers may be NULL)
187 bool GetTimes(wxDateTime
*dtAccess
,
189 wxDateTime
*dtCreate
) const;
191 // convenience wrapper: get just the last mod time of the file
192 wxDateTime
GetModificationTime() const
195 (void)GetTimes(NULL
, &dtMod
, NULL
);
200 bool MacSetTypeAndCreator( wxUint32 type
, wxUint32 creator
) ;
201 bool MacGetTypeAndCreator( wxUint32
*type
, wxUint32
*creator
) ;
202 // gets the 'common' type and creator for a certain extension
203 static bool MacFindDefaultTypeAndCreator( const wxString
& ext
, wxUint32
*type
, wxUint32
*creator
) ;
204 // registers application defined extensions and their default type and creator
205 static void MacRegisterDefaultTypeAndCreator( const wxString
& ext
, wxUint32 type
, wxUint32 creator
) ;
206 // looks up the appropriate type and creator from the registration and then sets
207 bool MacSetDefaultTypeAndCreator() ;
209 // various file/dir operations
211 // retrieve the value of the current working directory
212 void AssignCwd(const wxString
& volume
= wxEmptyString
);
213 static wxString
GetCwd(const wxString
& volume
= wxEmptyString
);
215 // change the current working directory
217 static bool SetCwd( const wxString
&cwd
);
219 // get the value of user home (Unix only mainly)
220 void AssignHomeDir();
221 static wxString
GetHomeDir();
223 // get a temp file name starting with the specified prefix and open the
224 // file passed to us using this name for writing (atomically if
226 void AssignTempFileName(const wxString
& prefix
, wxFile
*fileTemp
= NULL
);
227 static wxString
CreateTempFileName(const wxString
& prefix
,
228 wxFile
*fileTemp
= NULL
);
230 // directory creation and removal.
231 // if full is TRUE, will try to make each directory in the path.
232 bool Mkdir( int perm
= 0777, bool full
= FALSE
);
233 static bool Mkdir( const wxString
&dir
, int perm
= 0777, bool full
= FALSE
);
236 static bool Rmdir( const wxString
&dir
);
238 // operations on the path
240 // normalize the path: with the default flags value, the path will be
241 // made absolute, without any ".." and "." and all environment
242 // variables will be expanded in it
244 // this may be done using another (than current) value of cwd
245 bool Normalize(int flags
= wxPATH_NORM_ALL
,
246 const wxString
& cwd
= wxEmptyString
,
247 wxPathFormat format
= wxPATH_NATIVE
);
249 // get a path path relative to the given base directory, i.e. opposite
252 // pass an empty string to get a path relative to the working directory
254 // returns TRUE if the file name was modified, FALSE if we failed to do
255 // anything with it (happens when the file is on a different volume,
257 bool MakeRelativeTo(const wxString
& pathBase
= _T(""),
258 wxPathFormat format
= wxPATH_NATIVE
);
263 // compares with the rules of this platform
264 bool SameAs(const wxFileName
&filepath
,
265 wxPathFormat format
= wxPATH_NATIVE
);
267 // uses the current platform settings
268 bool operator==(const wxFileName
& filename
) { return SameAs(filename
); }
269 bool operator==(const wxString
& filename
)
270 { return *this == wxFileName(filename
); }
272 // are the file names of this type cases sensitive?
273 static bool IsCaseSensitive( wxPathFormat format
= wxPATH_NATIVE
);
275 // is this filename absolute?
276 bool IsAbsolute( wxPathFormat format
= wxPATH_NATIVE
) const;
278 // is this filename relative?
279 bool IsRelative() const
280 { return m_relative
; }
282 // forcibly set the flag
284 { m_relative
= FALSE
; }
286 { m_relative
= TRUE
; }
288 // Information about path format
290 // get the string separating the volume from the path for this format
291 static wxString
GetVolumeSeparator(wxPathFormat format
= wxPATH_NATIVE
);
293 // get the string of path separators for this format
294 static wxString
GetPathSeparators(wxPathFormat format
= wxPATH_NATIVE
);
296 // is the char a path separator for this format?
297 static bool IsPathSeparator(wxChar ch
, wxPathFormat format
= wxPATH_NATIVE
);
299 // FIXME: what exactly does this do?
300 bool IsWild( wxPathFormat format
= wxPATH_NATIVE
);
303 void AppendDir( const wxString
&dir
);
304 void PrependDir( const wxString
&dir
);
305 void InsertDir( int before
, const wxString
&dir
);
306 void RemoveDir( int pos
);
307 size_t GetDirCount() const { return m_dirs
.GetCount(); }
310 void SetExt( const wxString
&ext
) { m_ext
= ext
; }
311 wxString
GetExt() const { return m_ext
; }
312 bool HasExt() const { return !m_ext
.empty(); }
314 void SetName( const wxString
&name
) { m_name
= name
; }
315 wxString
GetName() const { return m_name
; }
316 bool HasName() const { return !m_name
.empty(); }
318 void SetVolume( const wxString
&volume
) { m_volume
= volume
; }
319 wxString
GetVolume() const { return m_volume
; }
320 bool HasVolume() const { return !m_volume
.empty(); }
322 // full name is the file name + extension (but without the path)
323 void SetFullName(const wxString
& fullname
);
324 wxString
GetFullName() const;
326 const wxArrayString
& GetDirs() const { return m_dirs
; }
328 // Construct path only - possibly with the trailing separator
329 wxString
GetPath( bool add_separator
= FALSE
,
330 wxPathFormat format
= wxPATH_NATIVE
) const;
331 // Replace current path with this one
332 void SetPath( const wxString
&path
, wxPathFormat format
= wxPATH_NATIVE
);
334 // more readable synonym
335 wxString
GetPathWithSep(wxPathFormat format
= wxPATH_NATIVE
) const
336 { return GetPath(TRUE
/* add separator */, format
); }
338 // Construct full path with name and ext
339 wxString
GetFullPath( wxPathFormat format
= wxPATH_NATIVE
) const;
341 // Return the short form of the path (returns identity on non-Windows platforms)
342 wxString
GetShortPath() const;
344 // Return the long form of the path (returns identity on non-Windows platforms)
345 wxString
GetLongPath() const;
349 // get the canonical path format for this platform
350 static wxPathFormat
GetFormat( wxPathFormat format
= wxPATH_NATIVE
);
352 // split a fullpath into the volume, path, (base) name and extension
353 // (all of the pointers can be NULL)
354 static void SplitPath(const wxString
& fullpath
,
359 wxPathFormat format
= wxPATH_NATIVE
);
361 // compatibility version
362 static void SplitPath(const wxString
& fullpath
,
366 wxPathFormat format
= wxPATH_NATIVE
);
369 // the drive/volume/device specification (always empty for Unix)
372 // the path components of the file
373 wxArrayString m_dirs
;
375 // the file name and extension (empty for directories)
379 // is the path relative
383 #endif // _WX_FILENAME_H_