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"
23 // ridiculously enough, this will replace DirExists with wxDirExists etc
24 #include "wx/filefn.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
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
35 wxPATH_NATIVE
= 0, // the path format for the current platform
40 wxPATH_BEOS
= wxPATH_UNIX
,
41 wxPATH_WIN
= wxPATH_DOS
,
42 wxPATH_OS2
= wxPATH_DOS
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
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
57 // ----------------------------------------------------------------------------
58 // wxFileName: encapsulates a file path
59 // ----------------------------------------------------------------------------
61 class WXDLLEXPORT wxFileName
64 // constructors and assignment
68 wxFileName( const wxFileName
&filepath
) { Assign(filepath
); }
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
); }
76 // from a directory name and a file name
77 wxFileName(const wxString
& path
,
79 wxPathFormat format
= wxPATH_NATIVE
)
80 { Assign(path
, name
, format
); }
82 // from a directory name, file base name and extension
83 wxFileName(const wxString
& path
,
86 wxPathFormat format
= wxPATH_NATIVE
)
87 { Assign(path
, name
, ext
, format
); }
89 // assorted assignment operators
91 wxFileName
& operator=(const wxFileName
& filename
)
92 { Assign(filename
); return *this; }
94 wxFileName
& operator=(const wxString
& filename
)
95 { Assign(filename
); return *this; }
97 // the same for delayed initialization
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
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
,
111 wxPathFormat format
= wxPATH_NATIVE
);
112 void AssignDir(const wxString
& dir
, wxPathFormat format
= wxPATH_NATIVE
)
113 { Assign(dir
, _T(""), format
); }
115 // reset all components to default, uninitialized state
118 // static pseudo constructors
119 static wxFileName
FileName(const wxString
& file
);
120 static wxFileName
DirName(const wxString
& dir
);
122 // test for existence
124 // is the filename valid at all?
125 bool IsOk() const { return !m_dirs
.IsEmpty() || !m_name
.IsEmpty(); }
127 // does the file with this name exists?
129 static bool FileExists( const wxString
&file
);
131 // does the directory with this name exists?
133 static bool DirExists( const wxString
&dir
);
135 // VZ: also need: IsDirWritable(), IsFileExecutable() &c (TODO)
137 // various file/dir operations
139 // retrieve the value of the current working directory
141 static wxString
GetCwd();
143 // change the current working directory
145 static bool SetCwd( const wxString
&cwd
);
147 // get the value of user home (Unix only mainly)
148 void AssignHomeDir();
149 static wxString
GetHomeDir();
151 // get a temp file name starting with thespecified prefix
152 void AssignTempFileName( const wxString
&prefix
);
154 // directory creation and removal
155 bool Mkdir( int perm
= 0777 );
156 static bool Mkdir( const wxString
&dir
, int perm
= 0777 );
159 static bool Rmdir( const wxString
&dir
);
161 // operations on the path
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
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
);
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
); }
179 // compares with the rules of this platform
180 bool SameAs(const wxFileName
&filepath
,
181 wxPathFormat format
= wxPATH_NATIVE
);
184 static bool IsCaseSensitive( wxPathFormat format
= wxPATH_NATIVE
);
185 bool IsRelative( wxPathFormat format
= wxPATH_NATIVE
);
186 bool IsAbsolute( wxPathFormat format
= wxPATH_NATIVE
);
188 // get the string of path separators for this format
189 static wxString
GetPathSeparators(wxPathFormat format
= wxPATH_NATIVE
);
191 // is the char a path separator for this format?
192 static bool IsPathSeparator(wxChar ch
, wxPathFormat format
= wxPATH_NATIVE
);
194 // FIXME: what exactly does this do?
195 bool IsWild( wxPathFormat format
= wxPATH_NATIVE
);
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(); }
205 void SetExt( const wxString
&ext
) { m_ext
= ext
; }
206 wxString
GetExt() const { return m_ext
; }
207 bool HasExt() const { return !m_ext
.IsEmpty(); }
209 void SetName( const wxString
&name
) { m_name
= name
; }
210 wxString
GetName() const { return m_name
; }
211 bool HasName() const { return !m_name
.IsEmpty(); }
213 wxString
GetFullName() const;
215 const wxArrayString
&GetDirs() const { return m_dirs
; }
217 // Construct path only - possibly with the trailing separator
218 wxString
GetPath( bool add_separator
= FALSE
, wxPathFormat format
= wxPATH_NATIVE
) const;
220 // more readable synonym
221 wxString
GetPathWithSep(wxPathFormat format
= wxPATH_NATIVE
) const
222 { return GetPath(TRUE
/* add separator */, format
); }
224 // Construct full path with name and ext
225 wxString
GetFullPath( wxPathFormat format
= wxPATH_NATIVE
) const;
227 static wxPathFormat
GetFormat( wxPathFormat format
= wxPATH_NATIVE
);
230 // the path components of the file
231 wxArrayString m_dirs
;
233 // the file name and extension (empty for directories)
238 #endif // _WX_FILENAME_H_