diff options
author | Tim Mayberry <mojofunk@gmail.com> | 2007-06-07 03:24:46 +0000 |
---|---|---|
committer | Tim Mayberry <mojofunk@gmail.com> | 2007-06-07 03:24:46 +0000 |
commit | 82b884b707ab2662acde9730d388c1942ea853da (patch) | |
tree | d3a40ecabe32ef6a656f4b794dc6782ff8b1aa51 /libs/pbd | |
parent | 1e7ff46a8114b9f73d6d9727737fe9fef65c70e4 (diff) |
Add SearchPath class to libpbd
git-svn-id: svn://localhost/ardour2/trunk@1974 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/pbd')
-rw-r--r-- | libs/pbd/SConscript | 1 | ||||
-rw-r--r-- | libs/pbd/pbd/search_path.h | 173 | ||||
-rw-r--r-- | libs/pbd/search_path.cc | 154 |
3 files changed, 328 insertions, 0 deletions
diff --git a/libs/pbd/SConscript b/libs/pbd/SConscript index 1e43f70589..3dee418398 100644 --- a/libs/pbd/SConscript +++ b/libs/pbd/SConscript @@ -35,6 +35,7 @@ pathscanner.cc pool.cc pthread_utils.cc receiver.cc +search_path.cc shortpath.cc stacktrace.cc stateful.cc diff --git a/libs/pbd/pbd/search_path.h b/libs/pbd/pbd/search_path.h new file mode 100644 index 0000000000..00756873dc --- /dev/null +++ b/libs/pbd/pbd/search_path.h @@ -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 diff --git a/libs/pbd/search_path.cc b/libs/pbd/search_path.cc new file mode 100644 index 0000000000..0546789074 --- /dev/null +++ b/libs/pbd/search_path.cc @@ -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 |