2 * Copyright (C) 2007-2015 Tim Mayberry <mojofunk@gmail.com>
3 * Copyright (C) 2008-2009 David Robillard <d@drobilla.net>
4 * Copyright (C) 2008-2015 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2013-2014 John Emmas <john@creativepost.co.uk>
6 * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <glibmm/miscutils.h>
28 #include "pbd/tokenizer.h"
29 #include "pbd/search_path.h"
30 #include "pbd/error.h"
36 Searchpath::Searchpath ()
41 Searchpath::Searchpath (const string& path)
43 vector<std::string> tmp;
45 if (tokenize (path, string(G_SEARCHPATH_SEPARATOR_S), std::back_inserter (tmp))) {
46 add_directories (tmp);
50 Searchpath::Searchpath (const vector<std::string>& paths)
52 add_directories (paths);
56 Searchpath::remove_directory (const std::string& directory_path)
58 if (directory_path.empty()) {
62 for (vector<std::string>::iterator i = begin(); i != end();) {
63 if (*i == directory_path) {
72 Searchpath::remove_directories (const vector<std::string>& paths)
74 for(vector<std::string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
75 remove_directory (*i);
80 Searchpath::add_directory (const std::string& directory_path)
82 if (directory_path.empty()) {
85 for (vector<std::string>::const_iterator i = begin(); i != end(); ++i) {
86 if (*i == directory_path) {
90 push_back(directory_path);
94 Searchpath::add_directories (const vector<std::string>& paths)
96 for(vector<std::string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
102 Searchpath::to_string () const
106 for (vector<std::string>::const_iterator i = begin(); i != end(); ++i) {
108 path += G_SEARCHPATH_SEPARATOR;
111 path = path.substr (0, path.length() - 1); // drop final separator
117 Searchpath::operator+= (const Searchpath& spath)
119 insert(end(), spath.begin(), spath.end());
124 Searchpath::operator+= (const std::string& directory_path)
126 add_directory (directory_path);
131 Searchpath::operator+ (const std::string& directory_path)
133 return Searchpath (*this) += directory_path;
137 Searchpath::operator+ (const Searchpath& spath)
139 return Searchpath (*this) += spath;
143 Searchpath::operator-= (const Searchpath& spath)
145 remove_directories (spath);
150 Searchpath::operator-= (const std::string& directory_path)
152 remove_directory (directory_path);
158 Searchpath::add_subdirectory_to_paths (const string& subdir)
160 for (vector<std::string>::iterator i = begin(); i != end(); ++i) {
161 // should these new paths just be added to the end of
162 // the search path rather than replace?
163 *i = Glib::build_filename (*i, subdir);
170 Searchpath::contains (const string& path) const
172 std::vector<std::string>::const_iterator i = find(begin(), end(), path);
180 /* This is not part of the Searchpath object, but is closely related to the
181 * whole idea, and we put it here for convenience.
185 export_search_path (const string& base_dir, const char* varname, const char* dir)
188 const char * cstr = g_getenv (varname);
192 path += G_SEARCHPATH_SEPARATOR;
199 g_setenv (varname, path.c_str(), 1);