added and partly implemented the time functions for wxFileName
[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 license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_FILENAME_H_
13 #define _WX_FILENAME_H_
14
15 #ifdef __GNUG__
16 #pragma interface "filename.h"
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/string.h"
21 #endif
22
23 /*
24 TODO:
25
26 1. support for drives under Windows
27 2. more file operations:
28 a) chmod()
29 b) [acm]time() - get and set
30 c) file size
31 d) file permissions with readable accessors for most common bits
32 such as IsReadable() &c
33 e) rename()?
34 3. SameFileAs() function to compare inodes under Unix
35 */
36
37 // ridiculously enough, this will replace DirExists with wxDirExists etc
38 #include "wx/filefn.h"
39 #include "wx/datetime.h"
40
41 // ----------------------------------------------------------------------------
42 // constants
43 // ----------------------------------------------------------------------------
44
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
47 // Windows)
48 enum wxPathFormat
49 {
50 wxPATH_NATIVE = 0, // the path format for the current platform
51 wxPATH_UNIX,
52 wxPATH_MAC,
53 wxPATH_DOS,
54
55 wxPATH_BEOS = wxPATH_UNIX,
56 wxPATH_WIN = wxPATH_DOS,
57 wxPATH_OS2 = wxPATH_DOS
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_ALL = 0x003f
71 };
72
73 // ----------------------------------------------------------------------------
74 // wxFileName: encapsulates a file path
75 // ----------------------------------------------------------------------------
76
77 class WXDLLEXPORT wxFileName
78 {
79 public:
80 // constructors and assignment
81
82 // the usual stuff
83 wxFileName() { }
84 wxFileName( const wxFileName &filepath ) { Assign(filepath); }
85
86 // from a full filename: if it terminates with a '/', a directory path
87 // is contructed (the name will be empty), otherwise a file name and
88 // extension are extracted from it
89 wxFileName( const wxString& fullpath, wxPathFormat format = wxPATH_NATIVE )
90 { Assign( fullpath, format ); }
91
92 // from a directory name and a file name
93 wxFileName(const wxString& path,
94 const wxString& name,
95 wxPathFormat format = wxPATH_NATIVE)
96 { Assign(path, name, format); }
97
98 // from a directory name, file base name and extension
99 wxFileName(const wxString& path,
100 const wxString& name,
101 const wxString& ext,
102 wxPathFormat format = wxPATH_NATIVE)
103 { Assign(path, name, ext, format); }
104
105 // the same for delayed initialization
106
107 // VZ: wouldn't it be better to call this Create() for consistency with
108 // all GUI classes? Personally, I like Set() more than Assign() too
109
110 void Assign(const wxFileName& filepath);
111 void Assign(const wxString& fullpath,
112 wxPathFormat format = wxPATH_NATIVE);
113 void Assign(const wxString& path,
114 const wxString& name,
115 wxPathFormat format = wxPATH_NATIVE);
116 void Assign(const wxString& path,
117 const wxString& name,
118 const wxString& ext,
119 wxPathFormat format = wxPATH_NATIVE);
120 void AssignDir(const wxString& dir, wxPathFormat format = wxPATH_NATIVE)
121 { Assign(dir, _T(""), format); }
122
123 // assorted assignment operators
124
125 wxFileName& operator=(const wxFileName& filename)
126 { Assign(filename); return *this; }
127
128 wxFileName& operator=(const wxString& filename)
129 { Assign(filename); return *this; }
130
131 // reset all components to default, uninitialized state
132 void Clear();
133
134 // static pseudo constructors
135 static wxFileName FileName(const wxString& file);
136 static wxFileName DirName(const wxString& dir);
137
138 // file tests
139
140 // is the filename valid at all?
141 bool IsOk() const { return !m_dirs.IsEmpty() || !m_name.IsEmpty(); }
142
143 // does the file with this name exists?
144 bool FileExists();
145 static bool FileExists( const wxString &file );
146
147 // does the directory with this name exists?
148 bool DirExists();
149 static bool DirExists( const wxString &dir );
150
151 // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO)
152
153 // time functions
154
155 // set the file creation and last access/mod times
156 // (any of the pointers may be NULL)
157 bool SetTimes(const wxDateTime *dtCreate,
158 const wxDateTime *dtAccess,
159 const wxDateTime *dtMod);
160
161 // set the access and modification times to the current moment
162 bool Touch();
163
164 // return the last access, last modification and last change times
165 // (any of the pointers may be NULL)
166 bool GetTimes(wxDateTime *dtAccess,
167 wxDateTime *dtMod,
168 wxDateTime *dtChange) const;
169
170 // convenience wrapper: get just the last mod time of the file
171 wxDateTime GetModificationTime() const
172 {
173 wxDateTime dtMod;
174 (void)GetTimes(NULL, &dtMod, NULL);
175 return dtMod;
176 }
177
178 // various file/dir operations
179
180 // retrieve the value of the current working directory
181 void AssignCwd();
182 static wxString GetCwd();
183
184 // change the current working directory
185 bool SetCwd();
186 static bool SetCwd( const wxString &cwd );
187
188 // get the value of user home (Unix only mainly)
189 void AssignHomeDir();
190 static wxString GetHomeDir();
191
192 // get a temp file name starting with thespecified prefix
193 void AssignTempFileName( const wxString &prefix );
194
195 // directory creation and removal.
196 // if full is TRUE, will try to make each directory in the path.
197 bool Mkdir( int perm = 0777, bool full = FALSE);
198 static bool Mkdir( const wxString &dir, int perm = 0777, bool full = FALSE );
199
200 bool Rmdir();
201 static bool Rmdir( const wxString &dir );
202
203 // operations on the path
204
205 // normalize the path: with the default flags value, the path will be
206 // made absolute, without any ".." and "." and all environment
207 // variables will be expanded in it
208 //
209 // this may be done using another (than current) value of cwd
210 bool Normalize(wxPathNormalize flags = wxPATH_NORM_ALL,
211 const wxString& cwd = wxEmptyString,
212 wxPathFormat format = wxPATH_NATIVE);
213
214 // Comparison
215
216 // compares with the rules of this platform
217 bool SameAs(const wxFileName &filepath,
218 wxPathFormat format = wxPATH_NATIVE);
219
220 // uses the current platform settings
221 bool operator==(const wxFileName& filename) { return SameAs(filename); }
222 bool operator==(const wxString& filename)
223 { return *this == wxFileName(filename); }
224
225 // Tests
226 static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE );
227 bool IsRelative( wxPathFormat format = wxPATH_NATIVE );
228 bool IsAbsolute( wxPathFormat format = wxPATH_NATIVE );
229
230 // get the string of path separators for this format
231 static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE);
232
233 // is the char a path separator for this format?
234 static bool IsPathSeparator(wxChar ch, wxPathFormat format = wxPATH_NATIVE);
235
236 // FIXME: what exactly does this do?
237 bool IsWild( wxPathFormat format = wxPATH_NATIVE );
238
239 // Dir accessors
240 void AppendDir( const wxString &dir );
241 void PrependDir( const wxString &dir );
242 void InsertDir( int before, const wxString &dir );
243 void RemoveDir( int pos );
244 size_t GetDirCount() const { return m_dirs.GetCount(); }
245
246 // Other accessors
247 void SetExt( const wxString &ext ) { m_ext = ext; }
248 wxString GetExt() const { return m_ext; }
249 bool HasExt() const { return !m_ext.IsEmpty(); }
250
251 void SetName( const wxString &name ) { m_name = name; }
252 wxString GetName() const { return m_name; }
253 bool HasName() const { return !m_name.IsEmpty(); }
254
255 // full name is the file name + extension (but without the path)
256 void SetFullName(const wxString& fullname);
257 wxString GetFullName() const;
258
259 const wxArrayString &GetDirs() const { return m_dirs; }
260
261 // Construct path only - possibly with the trailing separator
262 wxString GetPath( bool add_separator = FALSE,
263 wxPathFormat format = wxPATH_NATIVE ) const;
264
265 // more readable synonym
266 wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const
267 { return GetPath(TRUE /* add separator */, format); }
268
269 // Construct full path with name and ext
270 wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const;
271
272 // Return the short form of the path (returns identity on non-Windows platforms)
273 wxString GetShortPath() const;
274
275 // Return the long form of the path (returns identity on non-Windows platforms)
276 wxString GetLongPath() const;
277
278 // various helpers
279
280 // get the canonical path format for this platform
281 static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE );
282
283 // split a fullpath into path, (base) name and ext (all of the pointers
284 // can be NULL)
285 static void SplitPath(const wxString& fullpath,
286 wxString *path,
287 wxString *name,
288 wxString *ext,
289 wxPathFormat format = wxPATH_NATIVE);
290
291 private:
292 // the path components of the file
293 wxArrayString m_dirs;
294
295 // the file name and extension (empty for directories)
296 wxString m_name,
297 m_ext;
298 };
299
300 #endif // _WX_FILENAME_H_
301