can't use xgetbv instruction with apple gcc under OS X Lion
[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 <string>
21
22 #include <glib.h>
23 #include <glibmm/miscutils.h>
24
25 #include "pbd/tokenizer.h"
26 #include "pbd/search_path.h"
27 #include "pbd/error.h"
28
29 using namespace std;
30
31 namespace PBD {
32
33 Searchpath::Searchpath ()
34 {
35
36 }
37
38 Searchpath::Searchpath (const string& path)
39 {
40         vector<std::string> tmp;
41
42         if (tokenize (path, string(G_SEARCHPATH_SEPARATOR_S), std::back_inserter (tmp))) {
43                 add_directories (tmp);
44         }
45 }
46
47 Searchpath::Searchpath (const vector<std::string>& paths)
48 {
49         add_directories (paths);
50 }
51
52 void
53 Searchpath::remove_directory (const std::string& directory_path)
54 {
55         if (directory_path.empty()) {
56                 return;
57         }
58
59         for (vector<std::string>::iterator i = begin(); i != end();) {
60                 if (*i == directory_path) {
61                         i = erase (i);
62                 } else {
63                         ++i;
64                 }
65         }
66 }
67
68 void
69 Searchpath::remove_directories (const vector<std::string>& paths)
70 {
71         for(vector<std::string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
72                 remove_directory (*i);
73         }
74 }
75
76 void
77 Searchpath::add_directory (const std::string& directory_path)
78 {
79         if (directory_path.empty()) {
80                 return;
81         }
82         for (vector<std::string>::const_iterator i = begin(); i != end(); ++i) {
83                 if (*i == directory_path) {
84                         return;
85                 }
86         }
87         push_back(directory_path);
88 }
89
90 void
91 Searchpath::add_directories (const vector<std::string>& paths)
92 {
93         for(vector<std::string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
94                 add_directory (*i);
95         }
96 }
97
98 const string
99 Searchpath::to_string () const
100 {
101         string path;
102
103         for (vector<std::string>::const_iterator i = begin(); i != end(); ++i) {
104                 path += *i;
105                 path += G_SEARCHPATH_SEPARATOR;
106         }
107
108         path = path.substr (0, path.length() - 1); // drop final separator
109
110         return path;
111 }
112
113 Searchpath&
114 Searchpath::operator+= (const Searchpath& spath)
115 {
116         insert(end(), spath.begin(), spath.end());
117         return *this;
118 }
119
120 Searchpath&
121 Searchpath::operator+= (const std::string& directory_path)
122 {
123         add_directory (directory_path);
124         return *this;
125 }
126
127 Searchpath&
128 Searchpath::operator+ (const std::string& directory_path)
129 {
130         add_directory (directory_path);
131         return *this;
132 }
133
134 Searchpath&
135 Searchpath::operator+ (const Searchpath& spath)
136 {
137         // concatenate paths into new Searchpath
138         insert(end(), spath.begin(), spath.end());
139         return *this;
140 }
141
142 Searchpath&
143 Searchpath::operator-= (const Searchpath& spath)
144 {
145         remove_directories (spath);
146         return *this;
147 }
148
149 Searchpath&
150 Searchpath::operator-= (const std::string& directory_path)
151 {
152         remove_directory (directory_path);
153         return *this;
154 }
155
156
157 Searchpath&
158 Searchpath::add_subdirectory_to_paths (const string& subdir)
159 {
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);
164         }
165         
166         return *this;
167 }
168
169 /* This is not part of the Searchpath object, but is closely related to the
170  * whole idea, and we put it here for convenience.
171  */
172
173 void 
174 export_search_path (const string& base_dir, const char* varname, const char* dir)
175 {
176         string path;
177         const char * cstr = g_getenv (varname);
178
179         if (cstr) {
180                 path = cstr;
181                 path += G_SEARCHPATH_SEPARATOR;
182         } else {
183                 path = "";
184         }
185         path += base_dir;
186         path += dir;
187
188         g_setenv (varname, path.c_str(), 1);
189 }
190
191 } // namespace PBD