]>
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 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // the various values for the path format: this mainly affects the path
46 // separator but also whether or not the path has the drive part (as under
50 wxPATH_NATIVE
= 0, // the path format for the current platform
56 wxPATH_BEOS
= wxPATH_UNIX
,
57 wxPATH_WIN
= wxPATH_DOS
,
58 wxPATH_OS2
= wxPATH_DOS
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
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_ALL
= 0x003f
74 // ----------------------------------------------------------------------------
75 // wxFileName: encapsulates a file path
76 // ----------------------------------------------------------------------------
78 class WXDLLEXPORT wxFileName
81 // constructors and assignment
85 wxFileName( const wxFileName
&filepath
) { Assign(filepath
); }
87 // from a full filename: if it terminates with a '/', a directory path
88 // is contructed (the name will be empty), otherwise a file name and
89 // extension are extracted from it
90 wxFileName( const wxString
& fullpath
, wxPathFormat format
= wxPATH_NATIVE
)
91 { Assign( fullpath
, format
); }
93 // from a directory name and a file name
94 wxFileName(const wxString
& path
,
96 wxPathFormat format
= wxPATH_NATIVE
)
97 { Assign(path
, name
, format
); }
99 // from a volume, directory name, file base name and extension
100 wxFileName(const wxString
& volume
,
101 const wxString
& path
,
102 const wxString
& name
,
104 wxPathFormat format
= wxPATH_NATIVE
)
105 { Assign(volume
, path
, name
, ext
, format
); }
107 // from a directory name, file base name and extension
108 wxFileName(const wxString
& path
,
109 const wxString
& name
,
111 wxPathFormat format
= wxPATH_NATIVE
)
112 { Assign(path
, name
, ext
, format
); }
114 // the same for delayed initialization
116 void Assign(const wxFileName
& filepath
);
118 void Assign(const wxString
& fullpath
,
119 wxPathFormat format
= wxPATH_NATIVE
);
121 void Assign(const wxString
& volume
,
122 const wxString
& path
,
123 const wxString
& name
,
125 wxPathFormat format
= wxPATH_NATIVE
);
127 void Assign(const wxString
& path
,
128 const wxString
& name
,
129 wxPathFormat format
= wxPATH_NATIVE
);
131 void Assign(const wxString
& path
,
132 const wxString
& name
,
134 wxPathFormat format
= wxPATH_NATIVE
)
137 Assign(_T(""), path
, name
, ext
, format
);
140 void AssignDir(const wxString
& dir
, wxPathFormat format
= wxPATH_NATIVE
);
142 // assorted assignment operators
144 wxFileName
& operator=(const wxFileName
& filename
)
145 { Assign(filename
); return *this; }
147 wxFileName
& operator=(const wxString
& filename
)
148 { Assign(filename
); return *this; }
150 // reset all components to default, uninitialized state
153 // static pseudo constructors
154 static wxFileName
FileName(const wxString
& file
);
155 static wxFileName
DirName(const wxString
& dir
);
159 // is the filename valid at all?
160 bool IsOk() const { return !m_dirs
.IsEmpty() || !m_name
.IsEmpty(); }
162 // does the file with this name exists?
164 static bool FileExists( const wxString
&file
);
166 // does the directory with this name exists?
168 static bool DirExists( const wxString
&dir
);
170 // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO)
174 // set the file creation and last access/mod times
175 // (any of the pointers may be NULL)
176 bool SetTimes(const wxDateTime
*dtCreate
,
177 const wxDateTime
*dtAccess
,
178 const wxDateTime
*dtMod
);
180 // set the access and modification times to the current moment
183 // return the last access, last modification and last change times
184 // (any of the pointers may be NULL)
185 bool GetTimes(wxDateTime
*dtAccess
,
187 wxDateTime
*dtChange
) const;
189 // convenience wrapper: get just the last mod time of the file
190 wxDateTime
GetModificationTime() const
193 (void)GetTimes(NULL
, &dtMod
, NULL
);
197 // various file/dir operations
199 // retrieve the value of the current working directory
200 void AssignCwd(const wxString
& volume
= wxEmptyString
);
201 static wxString
GetCwd(const wxString
& volume
= wxEmptyString
);
203 // change the current working directory
205 static bool SetCwd( const wxString
&cwd
);
207 // get the value of user home (Unix only mainly)
208 void AssignHomeDir();
209 static wxString
GetHomeDir();
211 // get a temp file name starting with the specified prefix
212 void AssignTempFileName(const wxString
& prefix
);
213 static wxString
CreateTempFileName(const wxString
& prefix
);
215 // directory creation and removal.
216 // if full is TRUE, will try to make each directory in the path.
217 bool Mkdir( int perm
= 0777, bool full
= FALSE
);
218 static bool Mkdir( const wxString
&dir
, int perm
= 0777, bool full
= FALSE
);
221 static bool Rmdir( const wxString
&dir
);
223 // operations on the path
225 // normalize the path: with the default flags value, the path will be
226 // made absolute, without any ".." and "." and all environment
227 // variables will be expanded in it
229 // this may be done using another (than current) value of cwd
230 bool Normalize(wxPathNormalize flags
= wxPATH_NORM_ALL
,
231 const wxString
& cwd
= wxEmptyString
,
232 wxPathFormat format
= wxPATH_NATIVE
);
236 // compares with the rules of this platform
237 bool SameAs(const wxFileName
&filepath
,
238 wxPathFormat format
= wxPATH_NATIVE
);
240 // uses the current platform settings
241 bool operator==(const wxFileName
& filename
) { return SameAs(filename
); }
242 bool operator==(const wxString
& filename
)
243 { return *this == wxFileName(filename
); }
247 // are the file names of this type cases sensitive?
248 static bool IsCaseSensitive( wxPathFormat format
= wxPATH_NATIVE
);
250 // is this filename absolute?
251 bool IsAbsolute( wxPathFormat format
= wxPATH_NATIVE
);
253 // is this filename relative?
254 bool IsRelative( wxPathFormat format
= wxPATH_NATIVE
)
255 { return !IsAbsolute(format
); }
257 // Information about path format
259 // get the string separating the volume from the path for this format
260 static wxString
GetVolumeSeparator(wxPathFormat format
= wxPATH_NATIVE
);
262 // get the string of path separators for this format
263 static wxString
GetPathSeparators(wxPathFormat format
= wxPATH_NATIVE
);
265 // is the char a path separator for this format?
266 static bool IsPathSeparator(wxChar ch
, wxPathFormat format
= wxPATH_NATIVE
);
268 // FIXME: what exactly does this do?
269 bool IsWild( wxPathFormat format
= wxPATH_NATIVE
);
272 void AppendDir( const wxString
&dir
);
273 void PrependDir( const wxString
&dir
);
274 void InsertDir( int before
, const wxString
&dir
);
275 void RemoveDir( int pos
);
276 size_t GetDirCount() const { return m_dirs
.GetCount(); }
279 void SetExt( const wxString
&ext
) { m_ext
= ext
; }
280 wxString
GetExt() const { return m_ext
; }
281 bool HasExt() const { return !m_ext
.empty(); }
283 void SetName( const wxString
&name
) { m_name
= name
; }
284 wxString
GetName() const { return m_name
; }
285 bool HasName() const { return !m_name
.empty(); }
287 void SetVolume( const wxString
&volume
) { m_volume
= volume
; }
288 wxString
GetVolume() const { return m_volume
; }
289 bool HasVolume() const { return !m_volume
.empty(); }
291 // full name is the file name + extension (but without the path)
292 void SetFullName(const wxString
& fullname
);
293 wxString
GetFullName() const;
295 const wxArrayString
& GetDirs() const { return m_dirs
; }
297 // Construct path only - possibly with the trailing separator
298 wxString
GetPath( bool add_separator
= FALSE
,
299 wxPathFormat format
= wxPATH_NATIVE
) const;
301 // more readable synonym
302 wxString
GetPathWithSep(wxPathFormat format
= wxPATH_NATIVE
) const
303 { return GetPath(TRUE
/* add separator */, format
); }
305 // Construct full path with name and ext
306 wxString
GetFullPath( wxPathFormat format
= wxPATH_NATIVE
) const;
308 // Return the short form of the path (returns identity on non-Windows platforms)
309 wxString
GetShortPath() const;
311 // Return the long form of the path (returns identity on non-Windows platforms)
312 wxString
GetLongPath() const;
316 // get the canonical path format for this platform
317 static wxPathFormat
GetFormat( wxPathFormat format
= wxPATH_NATIVE
);
319 // split a fullpath into the volume, path, (base) name and extension
320 // (all of the pointers can be NULL)
321 static void SplitPath(const wxString
& fullpath
,
326 wxPathFormat format
= wxPATH_NATIVE
);
328 // compatibility version
329 static void SplitPath(const wxString
& fullpath
,
333 wxPathFormat format
= wxPATH_NATIVE
);
336 // the drive/volume/device specification (always empty for Unix)
339 // the path components of the file
340 wxArrayString m_dirs
;
342 // the file name and extension (empty for directories)
347 #endif // _WX_FILENAME_H_