Add SearchPath class to libpbd
git-svn-id: svn://localhost/ardour2/trunk@1974 d708f5d6-7413-0410-9779-e7cbd77b26cf
This commit is contained in:
@@ -35,6 +35,7 @@ pathscanner.cc
|
||||
pool.cc
|
||||
pthread_utils.cc
|
||||
receiver.cc
|
||||
search_path.cc
|
||||
shortpath.cc
|
||||
stacktrace.cc
|
||||
stateful.cc
|
||||
|
||||
173
libs/pbd/pbd/search_path.h
Normal file
173
libs/pbd/pbd/search_path.h
Normal file
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
Copyright (C) 2007 Tim Mayberry
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef PBD_SEARCH_PATH_INCLUDED
|
||||
#define PBD_SEARCH_PATH_INCLUDED
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <pbd/filesystem.h>
|
||||
|
||||
namespace PBD {
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
/**
|
||||
* @class The SearchPath class is a helper class for getting a
|
||||
* vector of paths contained in a search path string where a
|
||||
* "search path string" contains absolute directory paths
|
||||
* separated by a colon(:) or a semi-colon(;) on windows.
|
||||
*
|
||||
* The SearchPath class does not test whether the paths exist
|
||||
* or are directories. It is basically just a container.
|
||||
*/
|
||||
class SearchPath {
|
||||
public:
|
||||
|
||||
typedef std::vector<sys::path>::iterator iterator;
|
||||
typedef std::vector<sys::path>::const_iterator const_iterator;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Create an empty SearchPath.
|
||||
*/
|
||||
SearchPath ();
|
||||
|
||||
/**
|
||||
* Initialize SearchPath from a string each path may or may not
|
||||
* exist.
|
||||
*
|
||||
* @param search_path A path string.
|
||||
*/
|
||||
SearchPath (const string& search_path);
|
||||
|
||||
/**
|
||||
* Initialize SearchPath from a vector of paths that may or may
|
||||
* not exist.
|
||||
*
|
||||
* @param path A path.
|
||||
*/
|
||||
SearchPath (const vector<sys::path>& paths);
|
||||
|
||||
/**
|
||||
* The copy constructor does what you would expect and copies the
|
||||
* vector of paths contained by the SearchPath.
|
||||
*/
|
||||
SearchPath (const SearchPath& search_path);
|
||||
|
||||
/**
|
||||
* Indicate whether there are any directory paths in m_dirs.
|
||||
*
|
||||
* If SearchPath is initialized with an empty string as the
|
||||
* result of for instance the contents of an unset environment
|
||||
* variable.
|
||||
*
|
||||
* @return true if there are any paths in m_paths.
|
||||
*/
|
||||
operator void* () const { return (void*)!m_dirs.empty(); }
|
||||
|
||||
/**
|
||||
* @return a read/write iterator that points to the first
|
||||
* path in the SearchPath. Iteration is done in ordinary
|
||||
* element order.
|
||||
*/
|
||||
iterator begin () { return m_dirs.begin(); }
|
||||
|
||||
/**
|
||||
* @return A read-only (constant) iterator that points to the
|
||||
* first path in the SearchPath.
|
||||
*/
|
||||
const_iterator begin () const { return m_dirs.begin(); }
|
||||
|
||||
/**
|
||||
* @return A read/write iterator that points one past the last
|
||||
* path in the SearchPath.
|
||||
*/
|
||||
iterator end () { return m_dirs.end(); }
|
||||
|
||||
/**
|
||||
* @return A read-only (constant) iterator that points one past
|
||||
* the last path in the SearchPath.
|
||||
*/
|
||||
const_iterator end () const { return m_dirs.end(); }
|
||||
|
||||
/**
|
||||
* @return a search path string.
|
||||
*
|
||||
* The string that is returned contains the platform specific
|
||||
* path separator. The verbosity and "explicitness" of this
|
||||
* method is indicitive that I don't think it should be used
|
||||
* that often.
|
||||
*/
|
||||
const string get_search_path_string () const;
|
||||
|
||||
/**
|
||||
* Assignment of another SearchPath to this.
|
||||
*/
|
||||
SearchPath& operator= (const SearchPath& spath);
|
||||
|
||||
/**
|
||||
* Add all the directories in path to this.
|
||||
*/
|
||||
SearchPath& operator+= (const SearchPath& spath);
|
||||
|
||||
/**
|
||||
* Add another directory path to the search path.
|
||||
*/
|
||||
SearchPath& operator+= (const sys::path& directory_path);
|
||||
|
||||
/**
|
||||
* Concatenate another SearchPath onto this.
|
||||
*/
|
||||
SearchPath& operator+ (const SearchPath& other);
|
||||
|
||||
/**
|
||||
* Add another path to the search path.
|
||||
*/
|
||||
SearchPath& operator+ (const sys::path& directory_path);
|
||||
|
||||
/**
|
||||
* Add a sub-directory to each path in the search path.
|
||||
* @param subdir The directory name, it should not contain
|
||||
* any path separating tokens.
|
||||
*/
|
||||
SearchPath& add_subdirectory_to_paths (const string& subdir);
|
||||
|
||||
/**
|
||||
* Add a sub-directory to each path in the search path.
|
||||
* @see add_subdirectory_to_paths
|
||||
*/
|
||||
SearchPath& operator/= (const string& subdir);
|
||||
|
||||
protected:
|
||||
|
||||
void add_directory (const sys::path& directory_path);
|
||||
|
||||
void add_directories (const vector<sys::path>& paths);
|
||||
|
||||
vector<sys::path> m_dirs;
|
||||
|
||||
};
|
||||
|
||||
} // namespace PBD
|
||||
|
||||
#endif
|
||||
154
libs/pbd/search_path.cc
Normal file
154
libs/pbd/search_path.cc
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
Copyright (C) 2007 Tim Mayberry
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
*/
|
||||
|
||||
#include <pbd/tokenizer.h>
|
||||
#include <pbd/search_path.h>
|
||||
#include <pbd/error.h>
|
||||
|
||||
namespace {
|
||||
|
||||
#ifdef WIN32
|
||||
const char * const path_delimiter = ";";
|
||||
#else
|
||||
const char * const path_delimiter = ":";
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
namespace PBD {
|
||||
|
||||
SearchPath::SearchPath ()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SearchPath::SearchPath (const string& path)
|
||||
{
|
||||
vector<sys::path> tmp;
|
||||
|
||||
if(!tokenize ( path, string(path_delimiter), std::back_inserter (tmp)))
|
||||
{
|
||||
// log warning(info perhaps?) that the path is empty
|
||||
warning << "SearchPath contains no tokens" << endmsg;
|
||||
|
||||
}
|
||||
|
||||
add_directories (tmp);
|
||||
}
|
||||
|
||||
SearchPath::SearchPath (const vector<sys::path>& paths)
|
||||
{
|
||||
add_directories (paths);
|
||||
}
|
||||
|
||||
SearchPath::SearchPath (const SearchPath& other)
|
||||
: m_dirs(other.m_dirs)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
SearchPath::add_directory (const sys::path& directory_path)
|
||||
{
|
||||
// test for existance and warn etc?
|
||||
m_dirs.push_back(directory_path);
|
||||
}
|
||||
|
||||
void
|
||||
SearchPath::add_directories (const vector<sys::path>& paths)
|
||||
{
|
||||
for(vector<sys::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
|
||||
add_directory (*i);
|
||||
}
|
||||
}
|
||||
|
||||
const string
|
||||
SearchPath::get_search_path_string () const
|
||||
{
|
||||
string path;
|
||||
|
||||
for (vector<sys::path>::const_iterator i = m_dirs.begin(); i != m_dirs.end(); ++i) {
|
||||
path += (*i).to_string();
|
||||
path += path_delimiter;
|
||||
}
|
||||
|
||||
path = path.substr (0, path.length() - 1); // drop final separator
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
SearchPath&
|
||||
SearchPath::operator= (const SearchPath& path)
|
||||
{
|
||||
m_dirs = path.m_dirs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
SearchPath&
|
||||
SearchPath::operator+= (const SearchPath& spath)
|
||||
{
|
||||
m_dirs.insert(m_dirs.end(), spath.m_dirs.begin(), spath.m_dirs.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
SearchPath&
|
||||
SearchPath::operator+= (const sys::path& directory_path)
|
||||
{
|
||||
add_directory (directory_path);
|
||||
return *this;
|
||||
}
|
||||
|
||||
SearchPath&
|
||||
SearchPath::operator+ (const sys::path& directory_path)
|
||||
{
|
||||
add_directory (directory_path);
|
||||
return *this;
|
||||
}
|
||||
|
||||
SearchPath&
|
||||
SearchPath::operator+ (const SearchPath& spath)
|
||||
{
|
||||
// concatenate paths into new SearchPath
|
||||
m_dirs.insert(m_dirs.end(), spath.m_dirs.begin(), spath.m_dirs.end());
|
||||
return *this;
|
||||
}
|
||||
|
||||
SearchPath&
|
||||
SearchPath::add_subdirectory_to_paths (const string& subdir)
|
||||
{
|
||||
vector<sys::path> tmp;
|
||||
string directory_path;
|
||||
|
||||
for (vector<sys::path>::iterator i = m_dirs.begin(); i != m_dirs.end(); ++i)
|
||||
{
|
||||
// should these new paths just be added to the end of
|
||||
// the search path rather than replace?
|
||||
*i /= subdir;
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
SearchPath&
|
||||
SearchPath::operator/= (const string& subdir)
|
||||
{
|
||||
return add_subdirectory_to_paths (subdir);
|
||||
}
|
||||
|
||||
} // namespace PBD
|
||||
Reference in New Issue
Block a user