wxFileName II. It actually works.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9022 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robert Roebling 2000-12-30 00:28:00 +00:00
parent 14a31d6774
commit a35b27b14d
2 changed files with 234 additions and 57 deletions

View File

@ -40,64 +40,81 @@ class WXDLLEXPORT wxFileName
{
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;

View File

@ -27,21 +27,29 @@
#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 )
@ -64,7 +72,7 @@ void wxFileName::Assign( const wxString &path, bool dir_only, wxPathFormat forma
}
else
{
seps = "/"; // or maybe ":" or both ?
seps = ":";
}
wxStringTokenizer tn( path, seps );
@ -92,7 +100,7 @@ void wxFileName::Assign( const wxString &path, bool dir_only, wxPathFormat forma
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);
@ -111,7 +119,7 @@ void wxFileName::Assign( const wxString &path, bool dir_only, wxPathFormat forma
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( "." );
@ -122,54 +130,147 @@ void wxFileName::Assign( const wxString &path, bool dir_only, wxPathFormat forma
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 );
}
void wxFileName::SetCwd()
wxString wxFileName::GetCwd()
{
wxSetWorkingDirectory( GetFullPath() );
return ::wxGetCwd();
}
bool wxFileName::SetCwd()
{
return wxFileName::SetCwd( GetFullPath() );
}
bool wxFileName::SetCwd( const wxString &cwd )
{
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 )
{
wxMkdir( GetFullPath(), perm );
return wxFileName::Mkdir( GetFullPath(), perm );
}
void wxFileName::Rmdir()
bool wxFileName::Mkdir( const wxString &dir, int perm )
{
wxRmdir( GetFullPath() );
return ::wxMkdir( dir, perm );
}
void wxFileName::MakeAbsolute()
bool wxFileName::Rmdir()
{
return wxFileName::Rmdir( GetFullPath() );
}
bool wxFileName::SameAs( const wxFileName &filename, bool upper_on_dos )
bool wxFileName::Rmdir( const wxString &dir )
{
return ::wxRmdir( dir );
}
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);
}
@ -238,7 +339,60 @@ void wxFileName::RemoveDir( int pos )
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 );
@ -248,16 +402,19 @@ wxString wxFileName::GetPath( wxPathFormat format ) const
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
@ -265,7 +422,8 @@ wxString wxFileName::GetPath( wxPathFormat format ) const
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 += ":";
}
}
@ -286,8 +444,9 @@ wxString wxFileName::GetFullPath( wxPathFormat format ) const
}
}
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];
@ -299,7 +458,7 @@ wxString wxFileName::GetFullPath( wxPathFormat format ) const
for (size_t i = 0; i < m_dirs.GetCount(); i++)
{
ret += m_dirs[i];
ret += '/'; // or maybe ":" ?
ret += ':';
}
}
@ -330,3 +489,4 @@ wxPathFormat wxFileName::GetFormat( wxPathFormat format )
}
return format;
}