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:
@@ -40,64 +40,81 @@ class WXDLLEXPORT wxFileName
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// constructors and assignment
|
// 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 )
|
wxFileName( const wxString &path, bool dir_only = FALSE, wxPathFormat format = wxPATH_NATIVE )
|
||||||
{ Assign( path, dir_only, format ); }
|
{ Assign( path, dir_only, format ); }
|
||||||
void Assign( const wxString &path, bool dir_only = FALSE, wxPathFormat format = wxPATH_NATIVE );
|
void Assign( const wxString &path, bool dir_only = FALSE, wxPathFormat format = wxPATH_NATIVE );
|
||||||
|
void Assign( const wxFileName &filepath );
|
||||||
|
|
||||||
// Only native form
|
// Only native form
|
||||||
bool FileExists();
|
bool FileExists();
|
||||||
|
static bool FileExists( const wxString &file );
|
||||||
|
|
||||||
bool DirExists();
|
bool DirExists();
|
||||||
|
static bool DirExists( const wxString &dir );
|
||||||
|
|
||||||
void AssignCwd();
|
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 AssignTempFileName( const wxString &prefix );
|
||||||
|
|
||||||
void Mkdir( int perm = 0777 );
|
bool Mkdir( int perm = 0777 );
|
||||||
void Rmdir();
|
static bool Mkdir( const wxString &dir, int perm = 0777 );
|
||||||
|
|
||||||
|
bool Rmdir();
|
||||||
|
static bool Rmdir( const wxString &dir );
|
||||||
|
|
||||||
// Remove . and .. (under Unix ~ as well)
|
// Remove . and .. (under Unix ~ as well)
|
||||||
void MakeAbsolute();
|
bool Normalize( const wxString &cwd = wxEmptyString, const wxString &home = wxEmptyString );
|
||||||
|
|
||||||
// Comparison
|
// Comparison
|
||||||
bool SameAs( const wxFileName &filename, bool upper_on_dos = TRUE );
|
bool SameAs( const wxFileName &filepath, bool upper_case = TRUE );
|
||||||
|
|
||||||
// Tests
|
// Tests
|
||||||
bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE );
|
bool IsCaseSensitive( wxPathFormat format = wxPATH_NATIVE );
|
||||||
bool IsRelative( wxPathFormat format = wxPATH_NATIVE );
|
bool IsRelative( wxPathFormat format = wxPATH_NATIVE );
|
||||||
bool IsAbsolute( wxPathFormat format = wxPATH_NATIVE );
|
bool IsAbsolute( wxPathFormat format = wxPATH_NATIVE );
|
||||||
bool IsWild( wxPathFormat format = wxPATH_NATIVE );
|
bool IsWild( wxPathFormat format = wxPATH_NATIVE );
|
||||||
|
|
||||||
// Dir accessors
|
// Dir accessors
|
||||||
void AppendDir( const wxString &dir );
|
void AppendDir( const wxString &dir );
|
||||||
void PrependDir( const wxString &dir );
|
void PrependDir( const wxString &dir );
|
||||||
void InsertDir( int before, const wxString &dir );
|
void InsertDir( int before, const wxString &dir );
|
||||||
void RemoveDir( int pos );
|
void RemoveDir( int pos );
|
||||||
size_t GetDirCount() { return m_dirs.GetCount(); }
|
size_t GetDirCount() { return m_dirs.GetCount(); }
|
||||||
|
|
||||||
// Other accessors
|
// Other accessors
|
||||||
void SetExt( const wxString &ext ) { m_ext = ext; }
|
void SetExt( const wxString &ext ) { m_ext = ext; }
|
||||||
wxString GetExt() const { return m_ext; }
|
wxString GetExt() const { return m_ext; }
|
||||||
bool HasExt() const { return !m_ext.IsEmpty(); }
|
bool HasExt() const { return !m_ext.IsEmpty(); }
|
||||||
|
|
||||||
void SetName( const wxString &name ) { m_name = name; }
|
void SetName( const wxString &name ) { m_name = name; }
|
||||||
wxString GetName() const { return m_name; }
|
wxString GetName() const { return m_name; }
|
||||||
bool HasName() const { return !m_name.IsEmpty(); }
|
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; }
|
const wxArrayString &GetDirs() const { return m_dirs; }
|
||||||
|
|
||||||
// Construct path only
|
// 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
|
// Construct full path with name and ext
|
||||||
wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const;
|
wxString GetFullPath( wxPathFormat format = wxPATH_NATIVE ) const;
|
||||||
|
|
||||||
|
|
||||||
static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE );
|
static wxPathFormat GetFormat( wxPathFormat format = wxPATH_NATIVE );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxArrayString m_dirs;
|
wxArrayString m_dirs;
|
||||||
wxString m_name;
|
wxString m_name;
|
||||||
|
@@ -27,21 +27,29 @@
|
|||||||
|
|
||||||
#include "wx/filename.h"
|
#include "wx/filename.h"
|
||||||
#include "wx/tokenzr.h"
|
#include "wx/tokenzr.h"
|
||||||
#include "wx/filefn.h"
|
#include "wx/utils.h"
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
// wxFileName
|
// wxFileName
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
|
||||||
wxFileName::wxFileName( const wxFileName &filename )
|
wxFileName::wxFileName( const wxFileName &filepath )
|
||||||
{
|
{
|
||||||
m_ext = filename.GetExt();
|
m_ext = filepath.GetExt();
|
||||||
m_name = filename.GetName();
|
m_name = filepath.GetName();
|
||||||
const wxArrayString &dirs = filename.GetDirs();
|
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++)
|
for (size_t i = 0; i < dirs.GetCount(); i++)
|
||||||
{
|
|
||||||
m_dirs.Add( dirs[i] );
|
m_dirs.Add( dirs[i] );
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxFileName::Assign( const wxString &path, bool dir_only, wxPathFormat format )
|
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
|
else
|
||||||
{
|
{
|
||||||
seps = "/"; // or maybe ":" or both ?
|
seps = ":";
|
||||||
}
|
}
|
||||||
|
|
||||||
wxStringTokenizer tn( path, seps );
|
wxStringTokenizer tn( path, seps );
|
||||||
@@ -92,7 +100,7 @@ void wxFileName::Assign( const wxString &path, bool dir_only, wxPathFormat forma
|
|||||||
if (pos == -1) return;
|
if (pos == -1) return;
|
||||||
|
|
||||||
bool has_starting_dot = (pos == 0);
|
bool has_starting_dot = (pos == 0);
|
||||||
if (has_starting_dot)
|
if (has_starting_dot && (format == wxPATH_UNIX))
|
||||||
{
|
{
|
||||||
// remove dot
|
// remove dot
|
||||||
m_name.Remove(0,1);
|
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 );
|
m_name.Remove( pos, m_name.Len()-pos );
|
||||||
|
|
||||||
if (has_starting_dot)
|
if (has_starting_dot && (format == wxPATH_UNIX))
|
||||||
{
|
{
|
||||||
// add dot back
|
// add dot back
|
||||||
m_name.Prepend( "." );
|
m_name.Prepend( "." );
|
||||||
@@ -122,54 +130,147 @@ void wxFileName::Assign( const wxString &path, bool dir_only, wxPathFormat forma
|
|||||||
|
|
||||||
bool wxFileName::FileExists()
|
bool wxFileName::FileExists()
|
||||||
{
|
{
|
||||||
return ::wxFileExists( GetFullPath() );
|
return wxFileName::FileExists( GetFullPath() );
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wxFileName::FileExists( const wxString &file )
|
||||||
|
{
|
||||||
|
return ::wxFileExists( file );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxFileName::DirExists()
|
bool wxFileName::DirExists()
|
||||||
{
|
{
|
||||||
return ::wxDirExists( GetFullPath() );
|
return wxFileName::DirExists( GetFullPath() );
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wxFileName::DirExists( const wxString &dir )
|
||||||
|
{
|
||||||
|
return ::wxDirExists( dir );
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxFileName::AssignCwd()
|
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::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 file1( GetFullPath() );
|
||||||
wxString file2( filename.GetFullPath() );
|
wxString file2( filepath.GetFullPath() );
|
||||||
|
|
||||||
#ifdef __WXMSW__
|
if (upper_case)
|
||||||
if (upper_on_dos)
|
|
||||||
{
|
{
|
||||||
file1.MakeUpper();
|
file1.MakeUpper(); // what does MSW do to non-ascii chars etc? native funcs?
|
||||||
file2.MakeUpper();
|
file2.MakeUpper();
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
return (file1 == file2);
|
return (file1 == file2);
|
||||||
}
|
}
|
||||||
@@ -238,7 +339,60 @@ void wxFileName::RemoveDir( int pos )
|
|||||||
m_dirs.Remove( (size_t)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 );
|
format = GetFormat( format );
|
||||||
|
|
||||||
@@ -248,16 +402,19 @@ wxString wxFileName::GetPath( wxPathFormat format ) const
|
|||||||
for (size_t i = 0; i < m_dirs.GetCount(); i++)
|
for (size_t i = 0; i < m_dirs.GetCount(); i++)
|
||||||
{
|
{
|
||||||
ret += m_dirs[i];
|
ret += m_dirs[i];
|
||||||
if (i != m_dirs.GetCount()-1) ret += '\\';
|
if (add_separator || (i != m_dirs.GetCount()-1))
|
||||||
|
ret += '\\';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
if (format == wxPATH_DOS)
|
if (format == wxPATH_UNIX)
|
||||||
{
|
{
|
||||||
|
ret = '/'; // FIXME: must be absolute
|
||||||
for (size_t i = 0; i < m_dirs.GetCount(); i++)
|
for (size_t i = 0; i < m_dirs.GetCount(); i++)
|
||||||
{
|
{
|
||||||
ret += m_dirs[i];
|
ret += m_dirs[i];
|
||||||
if (i != m_dirs.GetCount()-1) ret += '/';
|
if (add_separator || (i != m_dirs.GetCount()-1))
|
||||||
|
ret += '/';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -265,7 +422,8 @@ wxString wxFileName::GetPath( wxPathFormat format ) const
|
|||||||
for (size_t i = 0; i < m_dirs.GetCount(); i++)
|
for (size_t i = 0; i < m_dirs.GetCount(); i++)
|
||||||
{
|
{
|
||||||
ret += m_dirs[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
|
else
|
||||||
if (format == wxPATH_DOS)
|
if (format == wxPATH_UNIX)
|
||||||
{
|
{
|
||||||
|
ret = '/'; // FIXME: must be absolute
|
||||||
for (size_t i = 0; i < m_dirs.GetCount(); i++)
|
for (size_t i = 0; i < m_dirs.GetCount(); i++)
|
||||||
{
|
{
|
||||||
ret += m_dirs[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++)
|
for (size_t i = 0; i < m_dirs.GetCount(); i++)
|
||||||
{
|
{
|
||||||
ret += m_dirs[i];
|
ret += m_dirs[i];
|
||||||
ret += '/'; // or maybe ":" ?
|
ret += ':';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,3 +489,4 @@ wxPathFormat wxFileName::GetFormat( wxPathFormat format )
|
|||||||
}
|
}
|
||||||
return format;
|
return format;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user