1
/* Copyright © 2005-2006 Roger Leigh <rleigh@debian.org>
3
* schroot is free software; you can redistribute it and/or modify it
4
* under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* schroot is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18
*********************************************************************/
24
#include <sys/types.h>
28
using namespace sbuild;
34
* Remove duplicate adjacent characters from a string.
36
* @param str the string to check.
37
* @param dup the duplicate character to check for.
38
* @returns a string with any duplicates removed.
40
std::string remove_duplicates (std::string const& str,
45
for (std::string::size_type pos = 0;
52
while (pos + 1 < str.length() &&
64
sbuild::basename (std::string name,
67
// Remove trailing separators
68
std::string::size_type cur = name.length();
69
while (cur - 1 != 0 && name[cur - 1] == separator)
73
// Find last separator
74
std::string::size_type pos = name.rfind(separator);
77
if (pos == std::string::npos)
78
ret = name; // No separators
79
else if (pos == 0 && name.length() == 1 && name[0] == separator)
80
ret = separator; // Only separators
82
ret = name.substr(pos + 1); // Basename only
84
return remove_duplicates(ret, separator);
88
sbuild::dirname (std::string name,
91
// Remove trailing separators
92
std::string::size_type cur = name.length();
93
while (cur - 1 != 0 && name[cur - 1] == separator)
97
// Find last separator
98
std::string::size_type pos = name.rfind(separator);
101
if (pos == std::string::npos)
102
ret = "."; // No directory components
106
ret = name.substr(0, pos); // Dirname part
108
return remove_duplicates(ret, separator);
112
sbuild::normalname (std::string name,
115
// Remove trailing separators
116
std::string::size_type cur = name.length();
117
while (cur - 1 != 0 && name[cur - 1] == separator)
121
return remove_duplicates(name, separator);
125
sbuild::string_list_to_string (sbuild::string_list const& list,
126
std::string const& separator)
130
for (string_list::const_iterator cur = list.begin();
135
if (cur + 1 != list.end())
143
sbuild::split_string (std::string const& value,
148
// Skip any separators at the start
149
std::string::size_type last_pos =
150
value.find_first_not_of(separator, 0);
151
// Find first separator.
152
std::string::size_type pos = value.find_first_of(separator, last_pos);
154
while (pos !=std::string::npos || last_pos != std::string::npos)
157
ret.push_back(value.substr(last_pos, pos - last_pos));
159
last_pos = value.find_first_not_of(separator, pos);
160
pos = value.find_first_of(separator, last_pos);
167
sbuild::find_program_in_path (std::string const& program,
168
std::string const& path,
169
std::string const& prefix)
171
if (program.find_first_of('/') != std::string::npos)
174
string_list dirs = split_string(path, ':');
176
for (string_list::const_iterator dir = dirs.begin();
180
std::string realname = *dir + '/' + program;
182
if (prefix.length() > 0)
185
if (dir->length() > 0 && (*dir)[0] != '/')
191
if (stat(absname.c_str(), &statbuf) == 0)
193
if (S_ISREG(statbuf.st_mode) &&
194
access (absname.c_str(), X_OK) == 0)
203
sbuild::string_list_to_strv (string_list const& str)
205
char **ret = new char *[str.size() + 1];
207
for (string_list::size_type i = 0;
211
ret[i] = new char[str[i].length() + 1];
212
std::strcpy(ret[i], str[i].c_str());
221
sbuild::strv_delete (char **strv)
223
for (char **pos = strv; pos != 0 && *pos != 0; ++pos)