]>
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" |
df5ddbca | 39 | |
097ead30 VZ |
40 | // ---------------------------------------------------------------------------- |
41 | // constants | |
42 | // ---------------------------------------------------------------------------- | |
43 | ||
44 | // the various values for the path format: this mainly affects the path | |
45 | // separator but also whether or not the path has the drive part (as under | |
46 | // Windows) | |
df5ddbca RR |
47 | enum wxPathFormat |
48 | { | |
844f90fb | 49 | wxPATH_NATIVE = 0, // the path format for the current platform |
097ead30 VZ |
50 | wxPATH_UNIX, |
51 | wxPATH_MAC, | |
52 | wxPATH_DOS, | |
ee66f092 | 53 | |
097ead30 VZ |
54 | wxPATH_BEOS = wxPATH_UNIX, |
55 | wxPATH_WIN = wxPATH_DOS, | |
56 | wxPATH_OS2 = wxPATH_DOS | |
57 | }; | |
ee66f092 | 58 | |
097ead30 VZ |
59 | // the kind of normalization to do with the file name: these values can be |
60 | // or'd together to perform several operations at once | |
61 | enum wxPathNormalize | |
62 | { | |
63 | wxPATH_NORM_ENV_VARS = 0x0001, // replace env vars with their values | |
844f90fb | 64 | wxPATH_NORM_DOTS = 0x0002, // squeeze all .. and . and prepend cwd |
097ead30 | 65 | wxPATH_NORM_TILDE = 0x0004, // Unix only: replace ~ and ~user |
844f90fb VZ |
66 | wxPATH_NORM_CASE = 0x0008, // if case insensitive => tolower |
67 | wxPATH_NORM_ABSOLUTE = 0x0010, // make the path absolute | |
9e9b65c1 JS |
68 | wxPATH_NORM_LONG = 0x0020, // make the path the long form |
69 | wxPATH_NORM_ALL = 0x003f | |
df5ddbca RR |
70 | }; |
71 | ||
097ead30 VZ |
72 | // ---------------------------------------------------------------------------- |
73 | // wxFileName: encapsulates a file path | |
74 | // ---------------------------------------------------------------------------- | |
75 | ||
df5ddbca RR |
76 | class WXDLLEXPORT wxFileName |
77 | { | |
78 | public: | |
79 | // constructors and assignment | |
844f90fb VZ |
80 | |
81 | // the usual stuff | |
82 | wxFileName() { } | |
83 | wxFileName( const wxFileName &filepath ) { Assign(filepath); } | |
84 | ||
85 | // from a full filename: if it terminates with a '/', a directory path | |
86 | // is contructed (the name will be empty), otherwise a file name and | |
87 | // extension are extracted from it | |
88 | wxFileName( const wxString& fullpath, wxPathFormat format = wxPATH_NATIVE ) | |
89 | { Assign( fullpath, format ); } | |
90 | ||
91 | // from a directory name and a file name | |
92 | wxFileName(const wxString& path, | |
93 | const wxString& name, | |
94 | wxPathFormat format = wxPATH_NATIVE) | |
95 | { Assign(path, name, format); } | |
96 | ||
97 | // from a directory name, file base name and extension | |
98 | wxFileName(const wxString& path, | |
99 | const wxString& name, | |
100 | const wxString& ext, | |
101 | wxPathFormat format = wxPATH_NATIVE) | |
102 | { Assign(path, name, ext, format); } | |
103 | ||
104 | // assorted assignment operators | |
105 | ||
106 | wxFileName& operator=(const wxFileName& filename) | |
107 | { Assign(filename); return *this; } | |
108 | ||
109 | wxFileName& operator=(const wxString& filename) | |
110 | { Assign(filename); return *this; } | |
111 | ||
112 | // the same for delayed initialization | |
113 | ||
114 | // VZ: wouldn't it be better to call this Create() for consistency with | |
115 | // all GUI classes? Personally, I like Set() more than Assign() too | |
116 | ||
117 | void Assign(const wxFileName& filepath); | |
118 | void Assign(const wxString& fullpath, | |
119 | wxPathFormat format = wxPATH_NATIVE); | |
120 | void Assign(const wxString& path, | |
121 | const wxString& name, | |
122 | wxPathFormat format = wxPATH_NATIVE); | |
123 | void Assign(const wxString& path, | |
124 | const wxString& name, | |
125 | const wxString& ext, | |
126 | wxPathFormat format = wxPATH_NATIVE); | |
127 | void AssignDir(const wxString& dir, wxPathFormat format = wxPATH_NATIVE) | |
128 | { Assign(dir, _T(""), format); } | |
129 | ||
130 | // reset all components to default, uninitialized state | |
131 | void Clear(); | |
132 | ||
133 | // static pseudo constructors | |
134 | static wxFileName FileName(const wxString& file); | |
135 | static wxFileName DirName(const wxString& dir); | |
136 | ||
137 | // test for existence | |
138 | ||
139 | // is the filename valid at all? | |
140 | bool IsOk() const { return !m_dirs.IsEmpty() || !m_name.IsEmpty(); } | |
141 | ||
142 | // does the file with this name exists? | |
df5ddbca | 143 | bool FileExists(); |
a35b27b1 | 144 | static bool FileExists( const wxString &file ); |
097ead30 | 145 | |
844f90fb | 146 | // does the directory with this name exists? |
df5ddbca | 147 | bool DirExists(); |
a35b27b1 | 148 | static bool DirExists( const wxString &dir ); |
097ead30 | 149 | |
844f90fb VZ |
150 | // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO) |
151 | ||
152 | // various file/dir operations | |
153 | ||
154 | // retrieve the value of the current working directory | |
df5ddbca | 155 | void AssignCwd(); |
a35b27b1 | 156 | static wxString GetCwd(); |
097ead30 | 157 | |
844f90fb | 158 | // change the current working directory |
a35b27b1 RR |
159 | bool SetCwd(); |
160 | static bool SetCwd( const wxString &cwd ); | |
097ead30 | 161 | |
844f90fb | 162 | // get the value of user home (Unix only mainly) |
a35b27b1 RR |
163 | void AssignHomeDir(); |
164 | static wxString GetHomeDir(); | |
097ead30 | 165 | |
844f90fb | 166 | // get a temp file name starting with thespecified prefix |
df5ddbca | 167 | void AssignTempFileName( const wxString &prefix ); |
097ead30 | 168 | |
f0ce3409 JS |
169 | // directory creation and removal. |
170 | // if full is TRUE, will try to make each directory in the path. | |
171 | bool Mkdir( int perm = 0777, bool full = FALSE); | |
172 | static bool Mkdir( const wxString &dir, int perm = 0777, bool full = FALSE ); | |
097ead30 | 173 | |
a35b27b1 RR |
174 | bool Rmdir(); |
175 | static bool Rmdir( const wxString &dir ); | |
097ead30 | 176 | |
844f90fb VZ |
177 | // operations on the path |
178 | ||
179 | // normalize the path: with the default flags value, the path will be | |
180 | // made absolute, without any ".." and "." and all environment | |
181 | // variables will be expanded in it | |
182 | // | |
183 | // this may be done using another (than current) value of cwd | |
184 | bool Normalize(wxPathNormalize flags = wxPATH_NORM_ALL, | |
185 | const wxString& cwd = wxEmptyString, | |
186 | wxPathFormat format = wxPATH_NATIVE); | |
097ead30 | 187 | |
df5ddbca | 188 | // Comparison |
844f90fb VZ |
189 | |
190 | // uses the current platform settings | |
191 | bool operator==(const wxFileName& filename) { return SameAs(filename); } | |
192 | bool operator==(const wxString& filename) | |
193 | { return *this == wxFileName(filename); } | |
194 | ||
195 | // compares with the rules of this platform | |
196 | bool SameAs(const wxFileName &filepath, | |
197 | wxPathFormat format = wxPATH_NATIVE); | |
097ead30 | 198 | |
df5ddbca | 199 | // Tests |
844f90fb | 200 | static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE ); |
df5ddbca RR |
201 | bool IsRelative( wxPathFormat format = wxPATH_NATIVE ); |
202 | bool IsAbsolute( wxPathFormat format = wxPATH_NATIVE ); | |
844f90fb VZ |
203 | |
204 | // get the string of path separators for this format | |
205 | static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE); | |
206 | ||
207 | // is the char a path separator for this format? | |
208 | static bool IsPathSeparator(wxChar ch, wxPathFormat format = wxPATH_NATIVE); | |
209 | ||
210 | // FIXME: what exactly does this do? | |
df5ddbca | 211 | bool IsWild( wxPathFormat format = wxPATH_NATIVE ); |
097ead30 | 212 | |
df5ddbca RR |
213 | // Dir accessors |
214 | void AppendDir( const wxString &dir ); | |
215 | void PrependDir( const wxString &dir ); | |
216 | void InsertDir( int before, const wxString &dir ); | |
217 | void RemoveDir( int pos ); | |
097ead30 VZ |
218 | size_t GetDirCount() const { return m_dirs.GetCount(); } |
219 | ||
df5ddbca RR |
220 | // Other accessors |
221 | void SetExt( const wxString &ext ) { m_ext = ext; } | |
222 | wxString GetExt() const { return m_ext; } | |
223 | bool HasExt() const { return !m_ext.IsEmpty(); } | |
097ead30 | 224 | |
df5ddbca RR |
225 | void SetName( const wxString &name ) { m_name = name; } |
226 | wxString GetName() const { return m_name; } | |
227 | bool HasName() const { return !m_name.IsEmpty(); } | |
097ead30 | 228 | |
7124df9b VZ |
229 | // full name is the file name + extension (but without the path) |
230 | void SetFullName(const wxString& fullname); | |
844f90fb | 231 | wxString GetFullName() const; |
097ead30 | 232 | |
df5ddbca | 233 | const wxArrayString &GetDirs() const { return m_dirs; } |
097ead30 | 234 | |
844f90fb | 235 | // Construct path only - possibly with the trailing separator |
7124df9b VZ |
236 | wxString GetPath( bool add_separator = FALSE, |
237 | wxPathFormat format = wxPATH_NATIVE ) const; | |
097ead30 | 238 | |
844f90fb VZ |
239 | // more readable synonym |
240 | wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const | |
241 | { return GetPath(TRUE /* add separator */, format); } | |
242 | ||
df5ddbca RR |
243 | // Construct full path with name and ext |
244 | wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const; | |
097ead30 | 245 | |
9e9b65c1 JS |
246 | // Return the short form of the path (returns identity on non-Windows platforms) |
247 | wxString GetShortPath() const; | |
248 | ||
249 | // Return the long form of the path (returns identity on non-Windows platforms) | |
250 | wxString GetLongPath() const; | |
251 | ||
9e8d8607 VZ |
252 | // various helpers |
253 | ||
254 | // get the canonical path format for this platform | |
df5ddbca | 255 | static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE ); |
097ead30 | 256 | |
9e8d8607 VZ |
257 | // split a fullpath into path, (base) name and ext (all of the pointers |
258 | // can be NULL) | |
259 | static void SplitPath(const wxString& fullpath, | |
260 | wxString *path, | |
261 | wxString *name, | |
262 | wxString *ext, | |
263 | wxPathFormat format = wxPATH_NATIVE); | |
264 | ||
df5ddbca | 265 | private: |
844f90fb | 266 | // the path components of the file |
df5ddbca | 267 | wxArrayString m_dirs; |
844f90fb VZ |
268 | |
269 | // the file name and extension (empty for directories) | |
270 | wxString m_name, | |
271 | m_ext; | |
df5ddbca RR |
272 | }; |
273 | ||
097ead30 | 274 | #endif // _WX_FILENAME_H_ |
df5ddbca | 275 |