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