2 Copyright (C) 2007 Tim Mayberry
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <glibmm/miscutils.h>
25 #include "pbd/tokenizer.h"
26 #include "pbd/search_path.h"
27 #include "pbd/error.h"
33 Searchpath::Searchpath ()
38 Searchpath::Searchpath (const string& path)
40 vector<std::string> tmp;
42 if (tokenize (path, string(G_SEARCHPATH_SEPARATOR_S), std::back_inserter (tmp))) {
43 add_directories (tmp);
47 Searchpath::Searchpath (const vector<std::string>& paths)
49 add_directories (paths);
53 Searchpath::remove_directory (const std::string& directory_path)
55 if (directory_path.empty()) {
59 for (vector<std::string>::iterator i = begin(); i != end();) {
60 if (*i == directory_path) {
69 Searchpath::remove_directories (const vector<std::string>& paths)
71 for(vector<std::string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
72 remove_directory (*i);
77 Searchpath::add_directory (const std::string& directory_path)
79 if (directory_path.empty()) {
82 for (vector<std::string>::const_iterator i = begin(); i != end(); ++i) {
83 if (*i == directory_path) {
87 push_back(directory_path);
91 Searchpath::add_directories (const vector<std::string>& paths)
93 for(vector<std::string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
99 Searchpath::to_string () const
103 for (vector<std::string>::const_iterator i = begin(); i != end(); ++i) {
105 path += G_SEARCHPATH_SEPARATOR;
108 path = path.substr (0, path.length() - 1); // drop final separator
114 Searchpath::operator+= (const Searchpath& spath)
116 insert(end(), spath.begin(), spath.end());
121 Searchpath::operator+= (const std::string& directory_path)
123 add_directory (directory_path);
128 Searchpath::operator+ (const std::string& directory_path)
130 return Searchpath (*this) += directory_path;
134 Searchpath::operator+ (const Searchpath& spath)
136 return Searchpath (*this) += spath;
140 Searchpath::operator-= (const Searchpath& spath)
142 remove_directories (spath);
147 Searchpath::operator-= (const std::string& directory_path)
149 remove_directory (directory_path);
155 Searchpath::add_subdirectory_to_paths (const string& subdir)
157 for (vector<std::string>::iterator i = begin(); i != end(); ++i) {
158 // should these new paths just be added to the end of
159 // the search path rather than replace?
160 *i = Glib::build_filename (*i, subdir);
166 /* This is not part of the Searchpath object, but is closely related to the
167 * whole idea, and we put it here for convenience.
171 export_search_path (const string& base_dir, const char* varname, const char* dir)
174 const char * cstr = g_getenv (varname);
178 path += G_SEARCHPATH_SEPARATOR;
185 g_setenv (varname, path.c_str(), 1);