]> git.saurik.com Git - wxWidgets.git/blob - include/wx/filename.h
Unicode compilation fixes.
[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
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)
47 enum wxPathFormat
48 {
49 wxPATH_NATIVE = 0, // the path format for the current platform
50 wxPATH_UNIX,
51 wxPATH_MAC,
52 wxPATH_DOS,
53
54 wxPATH_BEOS = wxPATH_UNIX,
55 wxPATH_WIN = wxPATH_DOS,
56 wxPATH_OS2 = wxPATH_DOS
57 };
58
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
64 wxPATH_NORM_DOTS = 0x0002, // squeeze all .. and . and prepend cwd
65 wxPATH_NORM_TILDE = 0x0004, // Unix only: replace ~ and ~user
66 wxPATH_NORM_CASE = 0x0008, // if case insensitive => tolower
67 wxPATH_NORM_ABSOLUTE = 0x0010, // make the path absolute
68 wxPATH_NORM_LONG = 0x0020, // make the path the long form
69 wxPATH_NORM_ALL = 0x003f
70 };
71
72 // ----------------------------------------------------------------------------
73 // wxFileName: encapsulates a file path
74 // ----------------------------------------------------------------------------
75
76 class WXDLLEXPORT wxFileName
77 {
78 public:
79 // constructors and assignment
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?
143 bool FileExists();
144 static bool FileExists( const wxString &file );
145
146 // does the directory with this name exists?
147 bool DirExists();
148 static bool DirExists( const wxString &dir );
149
150 // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO)
151
152 // various file/dir operations
153
154 // retrieve the value of the current working directory
155 void AssignCwd();
156 static wxString GetCwd();
157
158 // change the current working directory
159 bool SetCwd();
160 static bool SetCwd( const wxString &cwd );
161
162 // get the value of user home (Unix only mainly)
163 void AssignHomeDir();
164 static wxString GetHomeDir();
165
166 // get a temp file name starting with thespecified prefix
167 void AssignTempFileName( const wxString &prefix );
168
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 );
173
174 bool Rmdir();
175 static bool Rmdir( const wxString &dir );
176
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);
187
188 // Comparison
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);
198
199 // Tests
200 static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE );
201 bool IsRelative( wxPathFormat format = wxPATH_NATIVE );
202 bool IsAbsolute( wxPathFormat format = wxPATH_NATIVE );
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?
211 bool IsWild( wxPathFormat format = wxPATH_NATIVE );
212
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 );
218 size_t GetDirCount() const { return m_dirs.GetCount(); }
219
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(); }
224
225 void SetName( const wxString &name ) { m_name = name; }
226 wxString GetName() const { return m_name; }
227 bool HasName() const { return !m_name.IsEmpty(); }
228
229 // full name is the file name + extension (but without the path)
230 void SetFullName(const wxString& fullname);
231 wxString GetFullName() const;
232
233 const wxArrayString &GetDirs() const { return m_dirs; }
234
235 // Construct path only - possibly with the trailing separator
236 wxString GetPath( bool add_separator = FALSE,
237 wxPathFormat format = wxPATH_NATIVE ) const;
238
239 // more readable synonym
240 wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const
241 { return GetPath(TRUE /* add separator */, format); }
242
243 // Construct full path with name and ext
244 wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const;
245
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
252 // various helpers
253
254 // get the canonical path format for this platform
255 static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE );
256
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
265 private:
266 // the path components of the file
267 wxArrayString m_dirs;
268
269 // the file name and extension (empty for directories)
270 wxString m_name,
271 m_ext;
272 };
273
274 #endif // _WX_FILENAME_H_
275