cleanup up cleanup at session destruction; clarify the meaning of 3 signals (DropRefe...
[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 using namespace std;
25
26 namespace {
27
28 #ifdef WIN32
29 const char * const path_delimiter = ";";
30 #else
31 const char * const path_delimiter = ":";
32 #endif
33
34 }
35
36 namespace PBD {
37
38 SearchPath::SearchPath ()
39 {
40
41 }
42
43 SearchPath::SearchPath (const string& path)
44 {
45         vector<sys::path> tmp;
46
47         if (tokenize (path, string(path_delimiter), std::back_inserter (tmp))) {
48                 add_directories (tmp);
49         }
50 }
51
52 SearchPath::SearchPath (const sys::path& directory_path)
53 {
54         add_directory (directory_path);
55 }
56
57 SearchPath::SearchPath (const vector<sys::path>& paths)
58 {
59         add_directories (paths);
60 }
61
62 void
63 SearchPath::add_directory (const sys::path& directory_path)
64 {
65         // test for existance and warn etc?
66         push_back(directory_path);
67 }
68
69 void
70 SearchPath::add_directories (const vector<sys::path>& paths)
71 {
72         for(vector<sys::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
73                 add_directory (*i);
74         }
75 }
76
77 const string
78 SearchPath::to_string () const
79 {
80         string path;
81
82         for (vector<sys::path>::const_iterator i = begin(); i != end(); ++i) {
83                 path += (*i).to_string();
84                 path += path_delimiter;
85         }
86
87         path = path.substr (0, path.length() - 1); // drop final separator
88
89         return path;
90 }
91
92 SearchPath& 
93 SearchPath::operator+= (const SearchPath& spath)
94 {
95         insert(end(), spath.begin(), spath.end());
96         return *this;
97 }
98
99 SearchPath& 
100 SearchPath::operator+= (const sys::path& directory_path)
101 {
102         add_directory (directory_path);
103         return *this;
104 }
105
106 SearchPath& 
107 SearchPath::operator+ (const sys::path& directory_path)
108 {
109         add_directory (directory_path);
110         return *this;
111 }
112
113 SearchPath& 
114 SearchPath::operator+ (const SearchPath& spath)
115 {
116         // concatenate paths into new SearchPath
117         insert(end(), spath.begin(), spath.end());
118         return *this;
119 }
120
121 SearchPath&
122 SearchPath::add_subdirectory_to_paths (const string& subdir)
123 {
124         for (vector<sys::path>::iterator i = begin(); i != end(); ++i) {
125                 // should these new paths just be added to the end of 
126                 // the search path rather than replace?
127                 *i /= subdir;
128         }
129         
130         return *this;
131 }
132
133 } // namespace PBD