Added long-filename functions & normalisation; patch to Watcom makefiles
[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 bool Mkdir( int perm = 0777 );
171 static bool Mkdir( const wxString &dir, int perm = 0777 );
172
173 bool Rmdir();
174 static bool Rmdir( const wxString &dir );
175
176 // operations on the path
177
178 // normalize the path: with the default flags value, the path will be
179 // made absolute, without any ".." and "." and all environment
180 // variables will be expanded in it
181 //
182 // this may be done using another (than current) value of cwd
183 bool Normalize(wxPathNormalize flags = wxPATH_NORM_ALL,
184 const wxString& cwd = wxEmptyString,
185 wxPathFormat format = wxPATH_NATIVE);
186
187 // Comparison
188
189 // uses the current platform settings
190 bool operator==(const wxFileName& filename) { return SameAs(filename); }
191 bool operator==(const wxString& filename)
192 { return *this == wxFileName(filename); }
193
194 // compares with the rules of this platform
195 bool SameAs(const wxFileName &filepath,
196 wxPathFormat format = wxPATH_NATIVE);
197
198 // Tests
199 static bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE );
200 bool IsRelative( wxPathFormat format = wxPATH_NATIVE );
201 bool IsAbsolute( wxPathFormat format = wxPATH_NATIVE );
202
203 // get the string of path separators for this format
204 static wxString GetPathSeparators(wxPathFormat format = wxPATH_NATIVE);
205
206 // is the char a path separator for this format?
207 static bool IsPathSeparator(wxChar ch, wxPathFormat format = wxPATH_NATIVE);
208
209 // FIXME: what exactly does this do?
210 bool IsWild( wxPathFormat format = wxPATH_NATIVE );
211
212 // Dir accessors
213 void AppendDir( const wxString &dir );
214 void PrependDir( const wxString &dir );
215 void InsertDir( int before, const wxString &dir );
216 void RemoveDir( int pos );
217 size_t GetDirCount() const { return m_dirs.GetCount(); }
218
219 // Other accessors
220 void SetExt( const wxString &ext ) { m_ext = ext; }
221 wxString GetExt() const { return m_ext; }
222 bool HasExt() const { return !m_ext.IsEmpty(); }
223
224 void SetName( const wxString &name ) { m_name = name; }
225 wxString GetName() const { return m_name; }
226 bool HasName() const { return !m_name.IsEmpty(); }
227
228 // full name is the file name + extension (but without the path)
229 void SetFullName(const wxString& fullname);
230 wxString GetFullName() const;
231
232 const wxArrayString &GetDirs() const { return m_dirs; }
233
234 // Construct path only - possibly with the trailing separator
235 wxString GetPath( bool add_separator = FALSE,
236 wxPathFormat format = wxPATH_NATIVE ) const;
237
238 // more readable synonym
239 wxString GetPathWithSep(wxPathFormat format = wxPATH_NATIVE ) const
240 { return GetPath(TRUE /* add separator */, format); }
241
242 // Construct full path with name and ext
243 wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const;
244
245 // Return the short form of the path (returns identity on non-Windows platforms)
246 wxString GetShortPath() const;
247
248 // Return the long form of the path (returns identity on non-Windows platforms)
249 wxString GetLongPath() const;
250
251 // various helpers
252
253 // get the canonical path format for this platform
254 static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE );
255
256 // split a fullpath into path, (base) name and ext (all of the pointers
257 // can be NULL)
258 static void SplitPath(const wxString& fullpath,
259 wxString *path,
260 wxString *name,
261 wxString *ext,
262 wxPathFormat format = wxPATH_NATIVE);
263
264 private:
265 // the path components of the file
266 wxArrayString m_dirs;
267
268 // the file name and extension (empty for directories)
269 wxString m_name,
270 m_ext;
271 };
272
273 #endif // _WX_FILENAME_H_
274