Make sure we use the correct style of filepath separator on Windows
[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::add_directory (const std::string& directory_path)
54 {
55         if (!directory_path.empty()) {
56                 push_back(directory_path);
57         }
58 }
59
60 void
61 Searchpath::add_directories (const vector<std::string>& paths)
62 {
63         for(vector<std::string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
64                 add_directory (*i);
65         }
66 }
67
68 const string
69 Searchpath::to_string () const
70 {
71         string path;
72
73         for (vector<std::string>::const_iterator i = begin(); i != end(); ++i) {
74                 path += *i;
75                 path += G_SEARCHPATH_SEPARATOR;
76         }
77
78         path = path.substr (0, path.length() - 1); // drop final separator
79
80         return path;
81 }
82
83 Searchpath&
84 Searchpath::operator+= (const Searchpath& spath)
85 {
86         insert(end(), spath.begin(), spath.end());
87         return *this;
88 }
89
90 Searchpath&
91 Searchpath::operator+= (const std::string& directory_path)
92 {
93         add_directory (directory_path);
94         return *this;
95 }
96
97 Searchpath&
98 Searchpath::operator+ (const std::string& directory_path)
99 {
100         add_directory (directory_path);
101         return *this;
102 }
103
104 Searchpath&
105 Searchpath::operator+ (const Searchpath& spath)
106 {
107         // concatenate paths into new Searchpath
108         insert(end(), spath.begin(), spath.end());
109         return *this;
110 }
111
112 Searchpath&
113 Searchpath::add_subdirectory_to_paths (const string& subdir)
114 {
115         for (vector<std::string>::iterator i = begin(); i != end(); ++i) {
116                 // should these new paths just be added to the end of 
117                 // the search path rather than replace?
118                 *i = Glib::build_filename (*i, subdir);
119         }
120         
121         return *this;
122 }
123
124 /* This is not part of the Searchpath object, but is closely related to the
125  * whole idea, and we put it here for convenience.
126  */
127
128 void 
129 export_search_path (const string& base_dir, const char* varname, const char* dir)
130 {
131         string path;
132         const char * cstr = g_getenv (varname);
133
134         if (cstr) {
135                 path = cstr;
136                 path += G_DIR_SEPARATOR;
137         } else {
138                 path = "";
139         }
140         path += base_dir;
141         path += dir;
142
143         g_setenv (varname, path.c_str(), 1);
144 }
145
146 } // namespace PBD