for windows build, add fallback_folders.cc to libpbd source list
[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 <glibmm/miscutils.h>
21
22 #include "pbd/tokenizer.h"
23 #include "pbd/search_path.h"
24 #include "pbd/error.h"
25
26 using namespace std;
27
28 namespace PBD {
29
30 Searchpath::Searchpath ()
31 {
32
33 }
34
35 Searchpath::Searchpath (const string& path)
36 {
37         vector<std::string> tmp;
38
39         if (tokenize (path, string(G_SEARCHPATH_SEPARATOR_S), std::back_inserter (tmp))) {
40                 add_directories (tmp);
41         }
42 }
43
44 Searchpath::Searchpath (const vector<std::string>& paths)
45 {
46         add_directories (paths);
47 }
48
49 void
50 Searchpath::add_directory (const std::string& directory_path)
51 {
52         if (!directory_path.empty()) {
53                 push_back(directory_path);
54         }
55 }
56
57 void
58 Searchpath::add_directories (const vector<std::string>& paths)
59 {
60         for(vector<std::string>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
61                 add_directory (*i);
62         }
63 }
64
65 const string
66 Searchpath::to_string () const
67 {
68         string path;
69
70         for (vector<std::string>::const_iterator i = begin(); i != end(); ++i) {
71                 path += *i;
72                 path += G_SEARCHPATH_SEPARATOR;
73         }
74
75         path = path.substr (0, path.length() - 1); // drop final separator
76
77         return path;
78 }
79
80 Searchpath&
81 Searchpath::operator+= (const Searchpath& spath)
82 {
83         insert(end(), spath.begin(), spath.end());
84         return *this;
85 }
86
87 Searchpath&
88 Searchpath::operator+= (const std::string& directory_path)
89 {
90         add_directory (directory_path);
91         return *this;
92 }
93
94 Searchpath&
95 Searchpath::operator+ (const std::string& directory_path)
96 {
97         add_directory (directory_path);
98         return *this;
99 }
100
101 Searchpath&
102 Searchpath::operator+ (const Searchpath& spath)
103 {
104         // concatenate paths into new Searchpath
105         insert(end(), spath.begin(), spath.end());
106         return *this;
107 }
108
109 Searchpath&
110 Searchpath::add_subdirectory_to_paths (const string& subdir)
111 {
112         for (vector<std::string>::iterator i = begin(); i != end(); ++i) {
113                 // should these new paths just be added to the end of 
114                 // the search path rather than replace?
115                 *i = Glib::build_filename (*i, subdir);
116         }
117         
118         return *this;
119 }
120
121 } // namespace PBD