Replace code for finding ControlProtocols/Surface plugins with a portable equivalent.
[ardour.git] / libs / pbd / search_path.cc
1 /*
2     Copyright (C) 2007 Tim Mayberry 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <pbd/tokenizer.h>
21 #include <pbd/search_path.h>
22 #include <pbd/error.h>
23
24 namespace {
25
26 #ifdef WIN32
27 const char * const path_delimiter = ";";
28 #else
29 const char * const path_delimiter = ":";
30 #endif
31
32 }
33
34 namespace PBD {
35
36 SearchPath::SearchPath ()
37 {
38
39 }
40
41 SearchPath::SearchPath (const string& path)
42 {
43         vector<sys::path> tmp;
44
45         if(tokenize (path, string(path_delimiter), std::back_inserter (tmp)))
46         {
47                 add_directories (tmp);
48         }
49 }
50
51 SearchPath::SearchPath (const sys::path& directory_path)
52 {
53         add_directory (directory_path);
54 }
55
56 SearchPath::SearchPath (const vector<sys::path>& paths)
57 {
58         add_directories (paths);
59 }
60
61 SearchPath::SearchPath (const SearchPath& other)
62         : m_dirs(other.m_dirs)
63 {
64
65 }
66
67 void
68 SearchPath::add_directory (const sys::path& directory_path)
69 {
70         // test for existance and warn etc?
71         m_dirs.push_back(directory_path);
72 }
73
74 void
75 SearchPath::add_directories (const vector<sys::path>& paths)
76 {
77         for(vector<sys::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
78                 add_directory (*i);
79         }
80 }
81
82 const string
83 SearchPath::get_string () const
84 {
85         string path;
86
87         for (vector<sys::path>::const_iterator i = m_dirs.begin(); i != m_dirs.end(); ++i) {
88                 path += (*i).to_string();
89                 path += path_delimiter;
90         }
91
92         path = path.substr (0, path.length() - 1); // drop final separator
93
94         return path;
95 }
96
97 SearchPath&
98 SearchPath::operator= (const SearchPath& path)
99 {
100         m_dirs = path.m_dirs;
101         return *this;
102 }
103
104 SearchPath& 
105 SearchPath::operator+= (const SearchPath& spath)
106 {
107         m_dirs.insert(m_dirs.end(), spath.m_dirs.begin(), spath.m_dirs.end());
108         return *this;
109 }
110
111 SearchPath& 
112 SearchPath::operator+= (const sys::path& directory_path)
113 {
114         add_directory (directory_path);
115         return *this;
116 }
117
118 SearchPath& 
119 SearchPath::operator+ (const sys::path& directory_path)
120 {
121         add_directory (directory_path);
122         return *this;
123 }
124
125 SearchPath& 
126 SearchPath::operator+ (const SearchPath& spath)
127 {
128         // concatenate paths into new SearchPath
129         m_dirs.insert(m_dirs.end(), spath.m_dirs.begin(), spath.m_dirs.end());
130         return *this;
131 }
132
133 SearchPath&
134 SearchPath::add_subdirectory_to_paths (const string& subdir)
135 {
136         for (vector<sys::path>::iterator i = m_dirs.begin(); i != m_dirs.end(); ++i)
137         {
138                 // should these new paths just be added to the end of 
139                 // the search path rather than replace?
140                 *i /= subdir;
141         }
142         
143         return *this;
144 }
145         
146 SearchPath&
147 SearchPath::operator/= (const string& subdir)
148 {
149         return add_subdirectory_to_paths (subdir);
150 }
151
152 } // namespace PBD