{
public:
// constructors and assignment
- wxFileName()
+ wxFileName()
{ }
- wxFileName( const wxFileName &filename );
+ wxFileName( const wxFileName &filepath );
wxFileName( const wxString &path, bool dir_only = FALSE, wxPathFormat format = wxPATH_NATIVE )
{ Assign( path, dir_only, format ); }
void Assign( const wxString &path, bool dir_only = FALSE, wxPathFormat format = wxPATH_NATIVE );
-
+ void Assign( const wxFileName &filepath );
+
// Only native form
bool FileExists();
+ static bool FileExists( const wxString &file );
+
bool DirExists();
-
+ static bool DirExists( const wxString &dir );
+
void AssignCwd();
- void SetCwd();
-
+ static wxString GetCwd();
+
+ bool SetCwd();
+ static bool SetCwd( const wxString &cwd );
+
+ void AssignHomeDir();
+ static wxString GetHomeDir();
+
void AssignTempFileName( const wxString &prefix );
-
- void Mkdir( int perm = 0777 );
- void Rmdir();
-
+
+ bool Mkdir( int perm = 0777 );
+ static bool Mkdir( const wxString &dir, int perm = 0777 );
+
+ bool Rmdir();
+ static bool Rmdir( const wxString &dir );
+
// Remove . and .. (under Unix ~ as well)
- void MakeAbsolute();
-
+ bool Normalize( const wxString &cwd = wxEmptyString, const wxString &home = wxEmptyString );
+
// Comparison
- bool SameAs( const wxFileName &filename, bool upper_on_dos = TRUE );
-
+ bool SameAs( const wxFileName &filepath, bool upper_case = TRUE );
+
// Tests
bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE );
bool IsRelative( wxPathFormat format = wxPATH_NATIVE );
bool IsAbsolute( wxPathFormat format = wxPATH_NATIVE );
bool IsWild( wxPathFormat format = wxPATH_NATIVE );
-
+
// Dir accessors
void AppendDir( const wxString &dir );
void PrependDir( const wxString &dir );
void InsertDir( int before, const wxString &dir );
void RemoveDir( int pos );
size_t GetDirCount() { return m_dirs.GetCount(); }
-
+
// Other accessors
void SetExt( const wxString &ext ) { m_ext = ext; }
wxString GetExt() const { return m_ext; }
bool HasExt() const { return !m_ext.IsEmpty(); }
-
+
void SetName( const wxString &name ) { m_name = name; }
wxString GetName() const { return m_name; }
bool HasName() const { return !m_name.IsEmpty(); }
-
+
+ // name and ext
+ void SetFullName( const wxString name, wxPathFormat format = wxPATH_NATIVE );
+ wxString GetFullName();
+
const wxArrayString &GetDirs() const { return m_dirs; }
-
+
// Construct path only
- wxString GetPath( wxPathFormat format = wxPATH_NATIVE ) const;
-
+ wxString GetPath( bool add_separator = FALSE, wxPathFormat format = wxPATH_NATIVE ) const;
+
// Construct full path with name and ext
wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const;
-
-
+
+
static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE );
-
+
private:
wxArrayString m_dirs;
wxString m_name;
#include "wx/filename.h"
#include "wx/tokenzr.h"
-#include "wx/filefn.h"
+#include "wx/utils.h"
//----------------------------------------------------------------------------
// wxFileName
//----------------------------------------------------------------------------
-wxFileName::wxFileName( const wxFileName &filename )
+wxFileName::wxFileName( const wxFileName &filepath )
{
- m_ext = filename.GetExt();
- m_name = filename.GetName();
- const wxArrayString &dirs = filename.GetDirs();
+ m_ext = filepath.GetExt();
+ m_name = filepath.GetName();
+ const wxArrayString &dirs = filepath.GetDirs();
+ for (size_t i = 0; i < dirs.GetCount(); i++)
+ m_dirs.Add( dirs[i] );
+}
+
+void wxFileName::Assign( const wxFileName &filepath )
+{
+ m_dirs.Clear();
+ m_ext = filepath.GetExt();
+ m_name = filepath.GetName();
+ const wxArrayString &dirs = filepath.GetDirs();
for (size_t i = 0; i < dirs.GetCount(); i++)
- {
m_dirs.Add( dirs[i] );
- }
}
void wxFileName::Assign( const wxString &path, bool dir_only, wxPathFormat format )
}
else
{
- seps = "/"; // or maybe ":" or both ?
+ seps = ":";
}
wxStringTokenizer tn( path, seps );
if (pos == -1) return;
bool has_starting_dot = (pos == 0);
- if (has_starting_dot)
+ if (has_starting_dot && (format == wxPATH_UNIX))
{
// remove dot
m_name.Remove(0,1);
m_name.Remove( pos, m_name.Len()-pos );
- if (has_starting_dot)
+ if (has_starting_dot && (format == wxPATH_UNIX))
{
// add dot back
m_name.Prepend( "." );
bool wxFileName::FileExists()
{
- return ::wxFileExists( GetFullPath() );
+ return wxFileName::FileExists( GetFullPath() );
+}
+
+bool wxFileName::FileExists( const wxString &file )
+{
+ return ::wxFileExists( file );
}
bool wxFileName::DirExists()
{
- return ::wxDirExists( GetFullPath() );
+ return wxFileName::DirExists( GetFullPath() );
+}
+
+bool wxFileName::DirExists( const wxString &dir )
+{
+ return ::wxDirExists( dir );
}
void wxFileName::AssignCwd()
{
- Assign( wxGetCwd(), TRUE );
+ Assign( wxFileName::GetCwd(), TRUE );
+}
+
+wxString wxFileName::GetCwd()
+{
+ return ::wxGetCwd();
+}
+
+bool wxFileName::SetCwd()
+{
+ return wxFileName::SetCwd( GetFullPath() );
}
-void wxFileName::SetCwd()
+bool wxFileName::SetCwd( const wxString &cwd )
{
- wxSetWorkingDirectory( GetFullPath() );
+ return ::wxSetWorkingDirectory( cwd );
}
+void wxFileName::AssignHomeDir()
+{
+ Assign( wxFileName::GetHomeDir(), TRUE );
+}
+
+wxString wxFileName::GetHomeDir()
+{
+ return ::wxGetHomeDir();
+}
+
void wxFileName::AssignTempFileName( const wxString &prefix )
{
}
-void wxFileName::Mkdir( int perm )
+bool wxFileName::Mkdir( int perm )
+{
+ return wxFileName::Mkdir( GetFullPath(), perm );
+}
+
+bool wxFileName::Mkdir( const wxString &dir, int perm )
{
- wxMkdir( GetFullPath(), perm );
+ return ::wxMkdir( dir, perm );
}
-void wxFileName::Rmdir()
+bool wxFileName::Rmdir()
{
- wxRmdir( GetFullPath() );
+ return wxFileName::Rmdir( GetFullPath() );
}
-void wxFileName::MakeAbsolute()
+bool wxFileName::Rmdir( const wxString &dir )
{
+ return ::wxRmdir( dir );
}
-bool wxFileName::SameAs( const wxFileName &filename, bool upper_on_dos )
+bool wxFileName::Normalize( const wxString &cwd, const wxString &home )
+{
+ wxFileName tmp( *this );
+ m_dirs.Clear();
+ const wxArrayString &dirs = tmp.GetDirs();
+
+ if (dirs.GetCount() == 0) return FALSE;
+
+ size_t start = 0;
+
+ if (dirs[0] == wxT("."))
+ {
+ if (cwd == wxEmptyString)
+ Assign( wxFileName::GetCwd(), TRUE );
+ else
+ Assign( cwd );
+ start = 1;
+ }
+ else
+ if (dirs[0] == wxT(".."))
+ {
+ if (cwd == wxEmptyString)
+ Assign( wxFileName::GetCwd(), TRUE );
+ else
+ Assign( cwd );
+ m_dirs.Remove( m_dirs.GetCount()-1 );
+ start = 1;
+ }
+ else
+ if (dirs[0] == wxT("~"))
+ {
+ if (home == wxEmptyString)
+ Assign( wxFileName::GetHomeDir(), TRUE );
+ else
+ Assign( home );
+ start = 1;
+ }
+
+ for (size_t i = start; i < dirs.GetCount(); i++)
+ {
+ if (dirs[i] == wxT(".")) continue;
+
+ if (dirs[i] == wxT(".."))
+ {
+ m_dirs.Remove( m_dirs.GetCount()-1 );
+ continue;
+ }
+
+ // expand env vars here ?
+
+ m_dirs.Add( dirs[i] );
+ }
+
+ m_name = tmp.GetName();
+ m_ext = tmp.GetExt();
+
+ return TRUE;
+}
+
+bool wxFileName::SameAs( const wxFileName &filepath, bool upper_case )
{
wxString file1( GetFullPath() );
- wxString file2( filename.GetFullPath() );
+ wxString file2( filepath.GetFullPath() );
-#ifdef __WXMSW__
- if (upper_on_dos)
+ if (upper_case)
{
- file1.MakeUpper();
+ file1.MakeUpper(); // what does MSW do to non-ascii chars etc? native funcs?
file2.MakeUpper();
}
-#endif
return (file1 == file2);
}
m_dirs.Remove( (size_t)pos );
}
-wxString wxFileName::GetPath( wxPathFormat format ) const
+void wxFileName::SetFullName( const wxString name, wxPathFormat format )
+{
+ format = GetFormat( format );
+
+ m_name = name;
+
+ if (m_name == wxT(".")) return;
+ if (m_name == wxT("..")) return;
+
+ // ext?
+ int pos = m_name.Find( wxT('.') );
+ if (pos == -1) return;
+
+ bool has_starting_dot = (pos == 0);
+ if (has_starting_dot && (format == wxPATH_UNIX))
+ {
+ // remove dot
+ m_name.Remove(0,1);
+
+ // search again
+ pos = m_name.Find( wxT('.') );
+ if (pos == -1)
+ {
+ // add dot back
+ m_name.Prepend( "." );
+ return;
+ }
+ }
+
+ m_ext = m_name;
+ m_ext.Remove( 0, pos+1 );
+
+ m_name.Remove( pos, m_name.Len()-pos );
+
+ if (has_starting_dot && (format == wxPATH_UNIX))
+ {
+ // add dot back
+ m_name.Prepend( "." );
+ return;
+ }
+}
+
+wxString wxFileName::GetFullName()
+{
+ wxString ret( m_name );
+ if (!m_ext.IsEmpty())
+ {
+ ret += '.';
+ ret += m_ext;
+ }
+ return ret;
+}
+
+wxString wxFileName::GetPath( bool add_separator, wxPathFormat format ) const
{
format = GetFormat( format );
for (size_t i = 0; i < m_dirs.GetCount(); i++)
{
ret += m_dirs[i];
- if (i != m_dirs.GetCount()-1) ret += '\\';
+ if (add_separator || (i != m_dirs.GetCount()-1))
+ ret += '\\';
}
}
else
- if (format == wxPATH_DOS)
+ if (format == wxPATH_UNIX)
{
+ ret = '/'; // FIXME: must be absolute
for (size_t i = 0; i < m_dirs.GetCount(); i++)
{
ret += m_dirs[i];
- if (i != m_dirs.GetCount()-1) ret += '/';
+ if (add_separator || (i != m_dirs.GetCount()-1))
+ ret += '/';
}
}
else
for (size_t i = 0; i < m_dirs.GetCount(); i++)
{
ret += m_dirs[i];
- if (i != m_dirs.GetCount()-1) ret += "//"; // or maybe ":" ?
+ if (add_separator || (i != m_dirs.GetCount()-1))
+ ret += ":";
}
}
}
}
else
- if (format == wxPATH_DOS)
+ if (format == wxPATH_UNIX)
{
+ ret = '/'; // FIXME: must be absolute
for (size_t i = 0; i < m_dirs.GetCount(); i++)
{
ret += m_dirs[i];
for (size_t i = 0; i < m_dirs.GetCount(); i++)
{
ret += m_dirs[i];
- ret += '/'; // or maybe ":" ?
+ ret += ':';
}
}
}
return format;
}
+