]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/filename.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileName - encapsulates a file path
4 // Author: Robert Roebling, Vadim Zeitlin
8 // Copyright: (c) 2000 Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_FILENAME_H_
13 #define _WX_FILENAME_H_
16 #pragma interface "filename.h"
20 #include "wx/string.h"
26 1. support for drives under Windows
27 2. more file operations:
29 b) [acm]time() - get and set
31 d) file permissions with readable accessors for most common bits
32 such as IsReadable() &c
34 3. SameFileAs() function to compare inodes under Unix
37 // ridiculously enough, this will replace DirExists with wxDirExists etc
38 #include "wx/filefn.h"
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
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
49 wxPATH_NATIVE
= 0, // the path format for the current platform
54 wxPATH_BEOS
= wxPATH_UNIX
,
55 wxPATH_WIN
= wxPATH_DOS
,
56 wxPATH_OS2
= wxPATH_DOS
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
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
72 // ----------------------------------------------------------------------------
73 // wxFileName: encapsulates a file path
74 // ----------------------------------------------------------------------------
76 class WXDLLEXPORT wxFileName
79 // constructors and assignment
83 wxFileName( const wxFileName
&filepath
) { Assign(filepath
); }
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
); }
91 // from a directory name and a file name
92 wxFileName(const wxString
& path
,
94 wxPathFormat format
= wxPATH_NATIVE
)
95 { Assign(path
, name
, format
); }
97 // from a directory name, file base name and extension
98 wxFileName(const wxString
& path
,
101 wxPathFormat format
= wxPATH_NATIVE
)
102 { Assign(path
, name
, ext
, format
); }
104 // assorted assignment operators
106 wxFileName
& operator=(const wxFileName
& filename
)
107 { Assign(filename
); return *this; }
109 wxFileName
& operator=(const wxString
& filename
)
110 { Assign(filename
); return *this; }
112 // the same for delayed initialization
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
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
,
126 wxPathFormat format
= wxPATH_NATIVE
);
127 void AssignDir(const wxString
& dir
, wxPathFormat format
= wxPATH_NATIVE
)
128 { Assign(dir
, _T(""), format
); }
130 // reset all components to default, uninitialized state
133 // static pseudo constructors
134 static wxFileName
FileName(const wxString
& file
);
135 static wxFileName
DirName(const wxString
& dir
);
137 // test for existence
139 // is the filename valid at all?
140 bool IsOk() const { return !m_dirs
.IsEmpty() || !m_name
.IsEmpty(); }
142 // does the file with this name exists?
144 static bool FileExists( const wxString
&file
);
146 // does the directory with this name exists?
148 static bool DirExists( const wxString
&dir
);
150 // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO)
152 // various file/dir operations
154 // retrieve the value of the current working directory
156 static wxString
GetCwd();
158 // change the current working directory
160 static bool SetCwd( const wxString
&cwd
);
162 // get the value of user home (Unix only mainly)
163 void AssignHomeDir();
164 static wxString
GetHomeDir();
166 // get a temp file name starting with thespecified prefix
167 void AssignTempFileName( const wxString
&prefix
);
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
);
175 static bool Rmdir( const wxString
&dir
);
177 // operations on the path
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
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
);
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
); }
195 // compares with the rules of this platform
196 bool SameAs(const wxFileName
&filepath
,
197 wxPathFormat format
= wxPATH_NATIVE
);
200 static bool IsCaseSensitive( wxPathFormat format
= wxPATH_NATIVE
);
201 bool IsRelative( wxPathFormat format
= wxPATH_NATIVE
);
202 bool IsAbsolute( wxPathFormat format
= wxPATH_NATIVE
);
204 // get the string of path separators for this format
205 static wxString
GetPathSeparators(wxPathFormat format
= wxPATH_NATIVE
);
207 // is the char a path separator for this format?
208 static bool IsPathSeparator(wxChar ch
, wxPathFormat format
= wxPATH_NATIVE
);
210 // FIXME: what exactly does this do?
211 bool IsWild( wxPathFormat format
= wxPATH_NATIVE
);
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(); }
221 void SetExt( const wxString
&ext
) { m_ext
= ext
; }
222 wxString
GetExt() const { return m_ext
; }
223 bool HasExt() const { return !m_ext
.IsEmpty(); }
225 void SetName( const wxString
&name
) { m_name
= name
; }
226 wxString
GetName() const { return m_name
; }
227 bool HasName() const { return !m_name
.IsEmpty(); }
229 // full name is the file name + extension (but without the path)
230 void SetFullName(const wxString
& fullname
);
231 wxString
GetFullName() const;
233 const wxArrayString
&GetDirs() const { return m_dirs
; }
235 // Construct path only - possibly with the trailing separator
236 wxString
GetPath( bool add_separator
= FALSE
,
237 wxPathFormat format
= wxPATH_NATIVE
) const;
239 // more readable synonym
240 wxString
GetPathWithSep(wxPathFormat format
= wxPATH_NATIVE
) const
241 { return GetPath(TRUE
/* add separator */, format
); }
243 // Construct full path with name and ext
244 wxString
GetFullPath( wxPathFormat format
= wxPATH_NATIVE
) const;
246 // Return the short form of the path (returns identity on non-Windows platforms)
247 wxString
GetShortPath() const;
249 // Return the long form of the path (returns identity on non-Windows platforms)
250 wxString
GetLongPath() const;
254 // get the canonical path format for this platform
255 static wxPathFormat
GetFormat( wxPathFormat format
= wxPATH_NATIVE
);
257 // split a fullpath into path, (base) name and ext (all of the pointers
259 static void SplitPath(const wxString
& fullpath
,
263 wxPathFormat format
= wxPATH_NATIVE
);
266 // the path components of the file
267 wxArrayString m_dirs
;
269 // the file name and extension (empty for directories)
274 #endif // _WX_FILENAME_H_