wxPathList cleanup by Francesco (patch 1521481)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40298 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -6,13 +6,12 @@ in those directories. Storing the filename only in an application's files and
|
|||||||
using a locally-defined list of directories makes the application and its files more
|
using a locally-defined list of directories makes the application and its files more
|
||||||
portable.
|
portable.
|
||||||
|
|
||||||
Use the {\it wxFileNameFromPath} global function to extract the filename
|
Use the \helpref{wxFileName::SplitPath}{wxfilenamesplitpath} global function
|
||||||
from the path.
|
to extract the filename from the path.
|
||||||
|
|
||||||
\wxheading{Derived from}
|
\wxheading{Derived from}
|
||||||
|
|
||||||
\helpref{wxList}{wxlist}\\
|
\helpref{wxArrayString}{wxarraystring}
|
||||||
\helpref{wxObject}{wxobject}
|
|
||||||
|
|
||||||
\wxheading{Include files}
|
\wxheading{Include files}
|
||||||
|
|
||||||
@@ -20,7 +19,7 @@ from the path.
|
|||||||
|
|
||||||
\wxheading{See also}
|
\wxheading{See also}
|
||||||
|
|
||||||
\helpref{wxList}{wxlist}
|
\helpref{wxArrayString}{wxarraystring}
|
||||||
|
|
||||||
\latexignore{\rtfignore{\wxheading{Members}}}
|
\latexignore{\rtfignore{\wxheading{Members}}}
|
||||||
|
|
||||||
@@ -29,7 +28,11 @@ from the path.
|
|||||||
|
|
||||||
\func{}{wxPathList}{\void}
|
\func{}{wxPathList}{\void}
|
||||||
|
|
||||||
Constructor.
|
Empty constructor.
|
||||||
|
|
||||||
|
\func{}{wxPathList}{\param{const wxArrayString\& }{arr}}
|
||||||
|
|
||||||
|
Constructs the object calling the \helpref{Add}{wxpathlistadd} function.
|
||||||
|
|
||||||
|
|
||||||
\membersection{wxPathList::AddEnvList}\label{wxpathlistaddenvlist}
|
\membersection{wxPathList::AddEnvList}\label{wxpathlistaddenvlist}
|
||||||
@@ -45,9 +48,12 @@ example.
|
|||||||
|
|
||||||
\func{void}{Add}{\param{const wxString\& }{path}}
|
\func{void}{Add}{\param{const wxString\& }{path}}
|
||||||
|
|
||||||
Adds the given directory to the path list, but does not
|
\func{void}{Add}{\param{const wxArrayString\& }{arr}}
|
||||||
check if the path was already on the list (use
|
|
||||||
\helpref{wxPathList::Member()}{wxpathlistmember} for this).
|
The first form adds the given directory (the filename if present is removed)
|
||||||
|
to the path list, if the path is not already in the list.
|
||||||
|
|
||||||
|
The second form just calls the first form on all elements of the given array.
|
||||||
|
|
||||||
|
|
||||||
\membersection{wxPathList::EnsureFileAccessible}\label{wxpathlistensurefileaccessible}
|
\membersection{wxPathList::EnsureFileAccessible}\label{wxpathlistensurefileaccessible}
|
||||||
@@ -61,7 +67,7 @@ filename and adding the path to the list if not already there.
|
|||||||
|
|
||||||
\membersection{wxPathList::FindAbsoluteValidPath}\label{wxpathlistfindabsolutepath}
|
\membersection{wxPathList::FindAbsoluteValidPath}\label{wxpathlistfindabsolutepath}
|
||||||
|
|
||||||
\func{wxString}{FindAbsoluteValidPath}{\param{const wxString\& }{file}}
|
\constfunc{wxString}{FindAbsoluteValidPath}{\param{const wxString\& }{file}}
|
||||||
|
|
||||||
Searches for a full path for an existing file by appending {\it file} to
|
Searches for a full path for an existing file by appending {\it file} to
|
||||||
successive members of the path list. If the file wasn't found, an empty
|
successive members of the path list. If the file wasn't found, an empty
|
||||||
@@ -70,16 +76,9 @@ string is returned.
|
|||||||
|
|
||||||
\membersection{wxPathList::FindValidPath}\label{wxpathlistfindvalidpath}
|
\membersection{wxPathList::FindValidPath}\label{wxpathlistfindvalidpath}
|
||||||
|
|
||||||
\func{wxString}{FindValidPath}{\param{const wxString\& }{file}}
|
\constfunc{wxString}{FindValidPath}{\param{const wxString\& }{file}}
|
||||||
|
|
||||||
Searches for a full path for an existing file by appending {\it file} to
|
Searches for a full path for an existing file by appending {\it file} to
|
||||||
successive members of the path list. If the file wasn't found, an empty string
|
successive members of the path list. If the file wasn't found, an empty string
|
||||||
is returned. This path may be relative to the current working directory.
|
is returned. This path may be relative to the current working directory.
|
||||||
|
|
||||||
|
|
||||||
\membersection{wxPathList::Member}\label{wxpathlistmember}
|
|
||||||
|
|
||||||
\func{bool}{Member}{\param{const wxString\& }{file}}
|
|
||||||
|
|
||||||
true if the path is in the path list (ignoring case).
|
|
||||||
|
|
||||||
|
@@ -595,29 +595,35 @@ private:
|
|||||||
|
|
||||||
|
|
||||||
// Path searching
|
// Path searching
|
||||||
class WXDLLIMPEXP_BASE wxPathList : public wxStringList
|
class WXDLLIMPEXP_BASE wxPathList : public wxArrayString
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
wxPathList() {}
|
||||||
|
wxPathList(const wxArrayString &arr)
|
||||||
|
{ Add(arr); }
|
||||||
|
|
||||||
// avoid GCC warning about virtual functions w/o virtual dtor
|
// avoid GCC warning about virtual functions w/o virtual dtor
|
||||||
virtual ~wxPathList() {}
|
virtual ~wxPathList() {}
|
||||||
|
|
||||||
// Adds all paths in environment variable
|
// Adds all paths in environment variable
|
||||||
void AddEnvList(const wxString& envVariable);
|
void AddEnvList(const wxString& envVariable);
|
||||||
|
|
||||||
|
// Adds given path to this list
|
||||||
void Add(const wxString& path);
|
void Add(const wxString& path);
|
||||||
|
void Add(const wxArrayString &paths);
|
||||||
|
|
||||||
// Find the first full path for which the file exists
|
// Find the first full path for which the file exists
|
||||||
wxString FindValidPath(const wxString& filename);
|
wxString FindValidPath(const wxString& filename) const;
|
||||||
|
|
||||||
// Find the first full path for which the file exists; ensure it's an
|
// Find the first full path for which the file exists; ensure it's an
|
||||||
// absolute path that gets returned.
|
// absolute path that gets returned.
|
||||||
wxString FindAbsoluteValidPath(const wxString& filename);
|
wxString FindAbsoluteValidPath(const wxString& filename) const;
|
||||||
|
|
||||||
// Given full path and filename, add path to list
|
// Given full path and filename, add path to list
|
||||||
void EnsureFileAccessible(const wxString& path);
|
void EnsureFileAccessible(const wxString& path);
|
||||||
// Returns true if the path is in the list
|
|
||||||
bool Member(const wxString& path);
|
|
||||||
|
|
||||||
private:
|
// Returns true if the path is in the list
|
||||||
// DECLARE_DYNAMIC_CLASS(wxPathList)
|
wxDEPRECATED( bool Member(const wxString& path) const );
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif // _WX_FILEFN_H_
|
||||||
// _WX_FILEFN_H_
|
|
||||||
|
@@ -34,6 +34,8 @@
|
|||||||
#include "wx/filename.h"
|
#include "wx/filename.h"
|
||||||
#include "wx/dir.h"
|
#include "wx/dir.h"
|
||||||
|
|
||||||
|
#include "wx/tokenzr.h"
|
||||||
|
|
||||||
// there are just too many of those...
|
// there are just too many of those...
|
||||||
#ifdef __VISUALC__
|
#ifdef __VISUALC__
|
||||||
#pragma warning(disable:4706) // assignment within conditional expression
|
#pragma warning(disable:4706) // assignment within conditional expression
|
||||||
@@ -144,23 +146,21 @@ WXDLLEXPORT int wxOpen( const wxChar *pathname, int flags, mode_t mode )
|
|||||||
// wxPathList
|
// wxPathList
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// IMPLEMENT_DYNAMIC_CLASS(wxPathList, wxStringList)
|
|
||||||
|
|
||||||
static inline wxChar* MYcopystring(const wxString& s)
|
|
||||||
{
|
|
||||||
wxChar* copy = new wxChar[s.length() + 1];
|
|
||||||
return wxStrcpy(copy, s.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline wxChar* MYcopystring(const wxChar* s)
|
|
||||||
{
|
|
||||||
wxChar* copy = new wxChar[wxStrlen(s) + 1];
|
|
||||||
return wxStrcpy(copy, s);
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxPathList::Add (const wxString& path)
|
void wxPathList::Add (const wxString& path)
|
||||||
{
|
{
|
||||||
wxStringList::Add (WXSTRINGCAST path);
|
// add only the path part of the given string (not the filename, in case it's present)
|
||||||
|
wxFileName fn(path + wxFileName::GetPathSeparator());
|
||||||
|
fn.Normalize(); // add only normalized paths
|
||||||
|
|
||||||
|
wxString toadd = fn.GetPath();
|
||||||
|
if (Index(toadd) == wxNOT_FOUND)
|
||||||
|
wxArrayString::Add(toadd); // do not add duplicates
|
||||||
|
}
|
||||||
|
|
||||||
|
void wxPathList::Add(const wxArrayString &arr)
|
||||||
|
{
|
||||||
|
for (size_t j=0; j < arr.GetCount(); j++)
|
||||||
|
Add(arr[j]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add paths e.g. from the PATH environment variable
|
// Add paths e.g. from the PATH environment variable
|
||||||
@@ -168,43 +168,24 @@ void wxPathList::AddEnvList (const wxString& WXUNUSED_IN_WINCE(envVariable))
|
|||||||
{
|
{
|
||||||
// No environment variables on WinCE
|
// No environment variables on WinCE
|
||||||
#ifndef __WXWINCE__
|
#ifndef __WXWINCE__
|
||||||
|
|
||||||
|
// The space has been removed from the tokenizers, otherwise a
|
||||||
|
// path such as "C:\Program Files" would be split into 2 paths:
|
||||||
|
// "C:\Program" and "Files"; this is true for both Windows and Unix.
|
||||||
|
|
||||||
static const wxChar PATH_TOKS[] =
|
static const wxChar PATH_TOKS[] =
|
||||||
#if defined(__WINDOWS__) || defined(__OS2__)
|
#if defined(__WINDOWS__) || defined(__OS2__)
|
||||||
/*
|
|
||||||
The space has been removed from the tokenizers, otherwise a
|
|
||||||
path such as "C:\Program Files" would be split into 2 paths:
|
|
||||||
"C:\Program" and "Files"
|
|
||||||
*/
|
|
||||||
// wxT(" ;"); // Don't separate with colon in DOS (used for drive)
|
|
||||||
wxT(";"); // Don't separate with colon in DOS (used for drive)
|
wxT(";"); // Don't separate with colon in DOS (used for drive)
|
||||||
#else
|
#else
|
||||||
wxT(" :;");
|
wxT(":;");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
wxString val ;
|
wxString val;
|
||||||
if (wxGetEnv (WXSTRINGCAST envVariable, &val))
|
if ( wxGetEnv(envVariable, &val) )
|
||||||
{
|
{
|
||||||
wxChar *s = MYcopystring (val);
|
// split into an array of string the value of the env var
|
||||||
wxChar *save_ptr, *token = wxStrtok (s, PATH_TOKS, &save_ptr);
|
wxArrayString arr = wxStringTokenize(val, PATH_TOKS);
|
||||||
|
WX_APPEND_ARRAY(*this, arr);
|
||||||
if (token)
|
|
||||||
{
|
|
||||||
Add(token);
|
|
||||||
while (token)
|
|
||||||
{
|
|
||||||
if ( (token = wxStrtok ((wxChar *) NULL, PATH_TOKS, &save_ptr))
|
|
||||||
!= NULL )
|
|
||||||
{
|
|
||||||
Add(token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// suppress warning about unused variable save_ptr when wxStrtok() is a
|
|
||||||
// macro which throws away its third argument
|
|
||||||
save_ptr = token;
|
|
||||||
|
|
||||||
delete [] s;
|
|
||||||
}
|
}
|
||||||
#endif // !__WXWINCE__
|
#endif // !__WXWINCE__
|
||||||
}
|
}
|
||||||
@@ -217,60 +198,42 @@ void wxPathList::EnsureFileAccessible (const wxString& path)
|
|||||||
wxString path_only(wxPathOnly(path));
|
wxString path_only(wxPathOnly(path));
|
||||||
if ( !path_only.empty() )
|
if ( !path_only.empty() )
|
||||||
{
|
{
|
||||||
if ( !Member(path_only) )
|
if ( Index(path_only) == wxNOT_FOUND )
|
||||||
Add(path_only);
|
Add(path_only);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxPathList::Member (const wxString& path)
|
// deprecated !
|
||||||
|
bool wxPathList::Member (const wxString& path) const
|
||||||
{
|
{
|
||||||
for (wxStringList::compatibility_iterator node = GetFirst(); node; node = node->GetNext())
|
return Index(path) != wxNOT_FOUND;
|
||||||
{
|
|
||||||
wxString path2( node->GetData() );
|
|
||||||
if (
|
|
||||||
#if defined(__WINDOWS__) || defined(__OS2__) || defined(__VMS__) || defined(__WXMAC__)
|
|
||||||
// Case INDEPENDENT
|
|
||||||
path.CompareTo (path2, wxString::ignoreCase) == 0
|
|
||||||
#else
|
|
||||||
// Case sensitive File System
|
|
||||||
path.CompareTo (path2) == 0
|
|
||||||
#endif
|
|
||||||
)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxString wxPathList::FindValidPath (const wxString& file)
|
wxString wxPathList::FindValidPath (const wxString& file) const
|
||||||
{
|
{
|
||||||
wxExpandPath(wxFileFunctionsBuffer, file);
|
wxFileName fn(file);
|
||||||
|
wxString strend;
|
||||||
|
|
||||||
wxChar buf[_MAXPATHLEN];
|
fn.Normalize();
|
||||||
wxStrcpy(buf, wxFileFunctionsBuffer);
|
if (fn.IsAbsolute())
|
||||||
|
strend = fn.GetFullName();
|
||||||
|
else
|
||||||
|
strend = fn.GetFullPath();
|
||||||
|
|
||||||
wxChar *filename = wxIsAbsolutePath (buf) ? wxFileNameFromPath (buf) : (wxChar *)buf;
|
for (size_t i=0; i<GetCount(); i++)
|
||||||
|
|
||||||
for (wxStringList::compatibility_iterator node = GetFirst(); node; node = node->GetNext())
|
|
||||||
{
|
{
|
||||||
const wxString path(node->GetData());
|
wxString strstart = Item(i);
|
||||||
wxStrcpy (wxFileFunctionsBuffer, path);
|
if (!strstart.IsEmpty() && strstart.Last() != wxFileName::GetPathSeparator())
|
||||||
wxChar ch = wxFileFunctionsBuffer[wxStrlen(wxFileFunctionsBuffer)-1];
|
strstart += wxFileName::GetPathSeparator();
|
||||||
if (ch != wxT('\\') && ch != wxT('/'))
|
|
||||||
wxStrcat (wxFileFunctionsBuffer, wxT("/"));
|
|
||||||
wxStrcat (wxFileFunctionsBuffer, filename);
|
|
||||||
#ifdef __WINDOWS__
|
|
||||||
wxUnix2DosFilename (wxFileFunctionsBuffer);
|
|
||||||
#endif
|
|
||||||
if (wxFileExists (wxFileFunctionsBuffer))
|
|
||||||
{
|
|
||||||
return wxString(wxFileFunctionsBuffer); // Found!
|
|
||||||
}
|
|
||||||
} // for()
|
|
||||||
|
|
||||||
return wxEmptyString; // Not found
|
if (wxFileExists(strstart + strend))
|
||||||
|
return strstart + strend; // Found!
|
||||||
|
}
|
||||||
|
|
||||||
|
return wxEmptyString; // Not found
|
||||||
}
|
}
|
||||||
|
|
||||||
wxString wxPathList::FindAbsoluteValidPath (const wxString& file)
|
wxString wxPathList::FindAbsoluteValidPath (const wxString& file) const
|
||||||
{
|
{
|
||||||
wxString f = FindValidPath(file);
|
wxString f = FindValidPath(file);
|
||||||
if ( f.empty() || wxIsAbsolutePath(f) )
|
if ( f.empty() || wxIsAbsolutePath(f) )
|
||||||
@@ -287,6 +250,23 @@ wxString wxPathList::FindAbsoluteValidPath (const wxString& file)
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// miscellaneous global functions (TOFIX!)
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static inline wxChar* MYcopystring(const wxString& s)
|
||||||
|
{
|
||||||
|
wxChar* copy = new wxChar[s.length() + 1];
|
||||||
|
return wxStrcpy(copy, s.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline wxChar* MYcopystring(const wxChar* s)
|
||||||
|
{
|
||||||
|
wxChar* copy = new wxChar[wxStrlen(s) + 1];
|
||||||
|
return wxStrcpy(copy, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
wxFileExists (const wxString& filename)
|
wxFileExists (const wxString& filename)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user