readded back SetFullName
[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_ALL = 0x001f
69 };
70
71 // ----------------------------------------------------------------------------
72 // wxFileName: encapsulates a file path
73 // ----------------------------------------------------------------------------
74
75 class WXDLLEXPORT wxFileName
76 {
77 public:
78 // constructors and assignment
79
80 // the usual stuff
81 wxFileName() { }
82 wxFileName( const wxFileName &filepath ) { Assign(filepath); }
83
84 // from a full filename: if it terminates with a '/', a directory path
85 // is contructed (the name will be empty), otherwise a file name and
86 // extension are extracted from it
87 wxFileName( const wxString& fullpath, wxPathFormat format = wxPATH_NATIVE )
88 { Assign( fullpath, format ); }
89
90 // from a directory name and a file name
91 wxFileName(const wxString& path,
92 const wxString& name,
93 wxPathFormat format = wxPATH_NATIVE)
94 { Assign(path, name, format); }
95
96 // from a directory name, file base name and extension
97 wxFileName(const wxString& path,
98 const wxString& name,
99 const wxString& ext,
100 wxPathFormat format = wxPATH_NATIVE)
101 { Assign(path, name, ext, format); }
102
103 // assorted assignment operators
104
105 wxFileName& operator=(const wxFileName& filename)
106 { Assign(filename); return *this; }
107
108 wxFileName& operator=(const wxString& filename)
109 { Assign(filename); return *this; }
110
111 // the same for delayed initialization
112
113 // VZ: wouldn't it be better to call this Create() for consistency with
114 // all GUI classes? Personally, I like Set() more than Assign() too
115
116 void Assign(const wxFileName& filepath);
117 void Assign(const wxString& fullpath,
118 wxPathFormat format = wxPATH_NATIVE);
119 void Assign(const wxString& path,
120 const wxString& name,
121 wxPathFormat format = wxPATH_NATIVE);
122 void Assign(const wxString& path,
123 const wxString& name,
124 const wxString& ext,
125 wxPathFormat format = wxPATH_NATIVE);
126 void AssignDir(const wxString& dir, wxPathFormat format = wxPATH_NATIVE)
127 { Assign(dir, _T(""), format); }
128
129 // reset all components to default, uninitialized state
130 void Clear();
131
132 // static pseudo constructors
133 static wxFileName FileName(const wxString& file);
134 static wxFileName DirName(const wxString& dir);
135
136 // test for existence
137
138 // is the filename valid at all?
139 bool IsOk() const { return !m_dirs.IsEmpty() || !m_name.IsEmpty(); }
140
141 // does the file with this name exists?
142 bool FileExists();
143 static bool FileExists( const wxString &file );
144
145 // does the directory with this name exists?
146 bool DirExists();
147 static bool DirExists( const wxString &dir );
148
149 // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO)
150
151 // various file/dir operations
152
153 // retrieve the value of the current working directory
154 void AssignCwd();
155 static wxString GetCwd();
156
157 // change the current working directory
158 bool SetCwd();
159 static bool SetCwd( const wxString &cwd );
160
161 // get the value of user home (Unix only mainly)
162 void AssignHomeDir();
163 static wxString GetHomeDir();
164
165 // get a temp file name starting with thespecified prefix
166 void AssignTempFileName( const wxString &prefix );
167
168 // directory creation and removal
169 bool Mkdir( int perm = 0777 );
170 static bool Mkdir( const wxString &dir, int perm = 0777 );
171
172 bool Rmdir();
173 static bool Rmdir( const wxString &dir );
174
175 // operations on the path
176
177 // normalize the path: with the default flags value, the path will be
178 // made absolute, without any ".." and "." and all environment
179 // variables will be expanded in it
180 //
181 // this may be done using another (than current) value of cwd
182 bool Normalize(wxPathNormalize flags = wxPATH_NORM_ALL,
183 const wxString& cwd = wxEmptyString,
184 wxPathFormat format = wxPATH_NATIVE);
185
186 // Comparison
187
188 // uses the current platform settings
189 bool operator==(const wxFileName& filename) { return SameAs(filename); }
190 bool operator==(const wxString& filename)
191 { return *this == wxFileName(filename); }
192
193 // compares with the rules of this platform
194 bool SameAs(const wxFileName &filepath,
195 wxPathFormat format = wxPATH_NATIVE);
196
197 // Tests
198 static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE );
199 bool IsRelative( wxPathFormat format = wxPATH_NATIVE );
200 bool IsAbsolute( wxPathFormat format = wxPATH_NATIVE );
201
202 // get the string of path separators for this format
203 static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE);
204
205 // is the char a path separator for this format?
206 static bool IsPathSeparator(wxChar ch, wxPathFormat format = wxPATH_NATIVE);
207
208 // FIXME: what exactly does this do?
209 bool IsWild( wxPathFormat format = wxPATH_NATIVE );
210
211 // Dir accessors
212 void AppendDir( const wxString &dir );
213 void PrependDir( const wxString &dir );
214 void InsertDir( int before, const wxString &dir );
215 void RemoveDir( int pos );
216 size_t GetDirCount() const { return m_dirs.GetCount(); }
217
218 // Other accessors
219 void SetExt( const wxString &ext ) { m_ext = ext; }
220 wxString GetExt() const { return m_ext; }
221 bool HasExt() const { return !m_ext.IsEmpty(); }
222
223 void SetName( const wxString &name ) { m_name = name; }
224 wxString GetName() const { return m_name; }
225 bool HasName() const { return !m_name.IsEmpty(); }
226
227 // full name is the file name + extension (but without the path)
228 void SetFullName(const wxString& fullname);
229 wxString GetFullName() const;
230
231 const wxArrayString &GetDirs() const { return m_dirs; }
232
233 // Construct path only - possibly with the trailing separator
234 wxString GetPath( bool add_separator = FALSE,
235 wxPathFormat format = wxPATH_NATIVE ) const;
236
237 // more readable synonym
238 wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const
239 { return GetPath(TRUE /* add separator */, format); }
240
241 // Construct full path with name and ext
242 wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const;
243
244 // various helpers
245
246 // get the canonical path format for this platform
247 static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE );
248
249 // split a fullpath into path, (base) name and ext (all of the pointers
250 // can be NULL)
251 static void SplitPath(const wxString& fullpath,
252 wxString *path,
253 wxString *name,
254 wxString *ext,
255 wxPathFormat format = wxPATH_NATIVE);
256
257 private:
258 // the path components of the file
259 wxArrayString m_dirs;
260
261 // the file name and extension (empty for directories)
262 wxString m_name,
263 m_ext;
264 };
265
266 #endif // _WX_FILENAME_H_
267