summaryrefslogtreecommitdiff
path: root/libs/pbd/search_path.cc
blob: 2238b233fe3c5ba0f78d28084b87e7dbbcf81af3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
    Copyright (C) 2007 Tim Mayberry 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "pbd/tokenizer.h"
#include "pbd/search_path.h"
#include "pbd/error.h"

namespace {

#ifdef WIN32
const char * const path_delimiter = ";";
#else
const char * const path_delimiter = ":";
#endif

}

namespace PBD {

SearchPath::SearchPath ()
{

}

SearchPath::SearchPath (const string& path)
{
	vector<sys::path> tmp;

	if (tokenize (path, string(path_delimiter), std::back_inserter (tmp))) {
		add_directories (tmp);
	}
}

SearchPath::SearchPath (const sys::path& directory_path)
{
	add_directory (directory_path);
}

SearchPath::SearchPath (const vector<sys::path>& paths)
{
	add_directories (paths);
}

void
SearchPath::add_directory (const sys::path& directory_path)
{
	// test for existance and warn etc?
	push_back(directory_path);
}

void
SearchPath::add_directories (const vector<sys::path>& paths)
{
	for(vector<sys::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
		add_directory (*i);
	}
}

const string
SearchPath::to_string () const
{
	string path;

	for (vector<sys::path>::const_iterator i = begin(); i != end(); ++i) {
		path += (*i).to_string();
		path += path_delimiter;
	}

	path = path.substr (0, path.length() - 1); // drop final separator

	return path;
}

SearchPath& 
SearchPath::operator+= (const SearchPath& spath)
{
	insert(end(), spath.begin(), spath.end());
	return *this;
}

SearchPath& 
SearchPath::operator+= (const sys::path& directory_path)
{
	add_directory (directory_path);
	return *this;
}

SearchPath& 
SearchPath::operator+ (const sys::path& directory_path)
{
	add_directory (directory_path);
	return *this;
}

SearchPath& 
SearchPath::operator+ (const SearchPath& spath)
{
	// concatenate paths into new SearchPath
	insert(end(), spath.begin(), spath.end());
	return *this;
}

SearchPath&
SearchPath::add_subdirectory_to_paths (const string& subdir)
{
	for (vector<sys::path>::iterator i = begin(); i != end(); ++i) {
		// should these new paths just be added to the end of 
		// the search path rather than replace?
		*i /= subdir;
	}
	
	return *this;
}

} // namespace PBD