2 Copyright (C) 2006 Paul Davis
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <glib/gstdio.h>
24 #include <glibmm/miscutils.h>
25 #include <glibmm/fileutils.h>
28 #include <pbd/tokenizer.h>
37 Path::Path (const string& path)
41 if(!tokenize ( path, string(":;"), std::back_inserter (tmp))) {
42 g_warning ("%s : %s\n", G_STRLOC, G_STRFUNC);
46 add_readable_directories (tmp);
49 Path::Path (const vector<string>& paths)
51 add_readable_directories (paths);
54 Path::Path (const Path& other)
55 : m_dirs(other.m_dirs)
61 Path::readable_directory (const string& directory_path)
63 if (g_access (directory_path.c_str(), R_OK) == 0) {
64 if (Glib::file_test(directory_path, Glib::FILE_TEST_IS_DIR)) {
67 g_warning (" %s : Path exists but is not a directory\n", G_STRLOC);
70 g_warning ("%s : %s : %s\n", G_STRLOC, directory_path.c_str(), g_strerror(errno));
76 Path::add_readable_directory (const string& directory_path)
78 if(readable_directory(directory_path)) {
79 m_dirs.push_back(directory_path);
84 Path::add_readable_directories (const vector<string>& paths)
86 for(vector<string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
87 add_readable_directory (*i);
92 Path::path_string() const
96 for (vector<string>::const_iterator i = m_dirs.begin(); i != m_dirs.end(); ++i) {
98 path += G_SEARCHPATH_SEPARATOR;
101 g_message ("%s : %s", G_STRLOC, path.c_str());
103 return path.substr (0, path.length() - 1); // drop final colon
107 Path::operator= (const Path& path)
109 m_dirs = path.m_dirs;
114 Path::operator+= (const string& directory_path)
116 add_readable_directory (directory_path);
121 operator+ (const Path& lhs_path, const Path& rhs_path)
123 Path tmp_path(lhs_path); // does this do what I think it does.
124 // concatenate paths into new Path
125 tmp_path.m_dirs.insert(tmp_path.m_dirs.end(), rhs_path.m_dirs.begin(), rhs_path.m_dirs.end());
130 Path::add_subdirectory_to_path (const string& subdir)
133 string directory_path;
135 for (vector<string>::iterator i = m_dirs.begin(); i != m_dirs.end(); ++i) {
136 directory_path = Glib::build_filename (*i, subdir);
137 if(readable_directory(directory_path)) {
138 tmp.push_back(directory_path);
146 find_file_in_path (const Path& path, const string& filename, string& resulting_path)
148 for (vector<string>::const_iterator i = path.dirs().begin(); i != path.dirs().end(); ++i) {
149 resulting_path = Glib::build_filename ((*i), filename);
150 if (g_access (resulting_path.c_str(), R_OK) == 0) {
151 g_message ("File %s found in Path : %s\n", resulting_path.c_str(),
152 path.path_string().c_str());
157 g_warning ("%s : Could not locate file %s in path %s\n", G_STRLOC, filename.c_str(),
158 path.path_string().c_str());