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