~ubuntu-branches/ubuntu/intrepid/schroot/intrepid

« back to all changes in this revision

Viewing changes to schroot/sbuild-util.cc

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2006-07-08 18:33:28 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060708183328-rlo4mpldmyoda55q
Tags: 0.99.2-2ubuntu1
* remerge ubuntu changes:
  + debian/control: libpam-dev (>> 0.79-3ubuntu6)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright © 2005-2006  Roger Leigh <rleigh@debian.org>
2
 
 *
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.
7
 
 *
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.
12
 
 *
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,
16
 
 * MA  02111-1307  USA
17
 
 *
18
 
 *********************************************************************/
19
 
 
20
 
#include <config.h>
21
 
 
22
 
#include "sbuild.h"
23
 
 
24
 
#include <sys/types.h>
25
 
#include <sys/stat.h>
26
 
#include <unistd.h>
27
 
 
28
 
using namespace sbuild;
29
 
 
30
 
namespace
31
 
{
32
 
 
33
 
  /**
34
 
   * Remove duplicate adjacent characters from a string.
35
 
   *
36
 
   * @param str the string to check.
37
 
   * @param dup the duplicate character to check for.
38
 
   * @returns a string with any duplicates removed.
39
 
   */
40
 
  std::string remove_duplicates (std::string const& str,
41
 
                                 char               dup)
42
 
  {
43
 
    std::string ret;
44
 
 
45
 
    for (std::string::size_type pos = 0;
46
 
         pos < str.length();
47
 
         ++pos)
48
 
      {
49
 
        ret += str[pos];
50
 
        if (str[pos] == dup)
51
 
          {
52
 
            while (pos + 1 < str.length() &&
53
 
                   str[pos + 1] == dup)
54
 
              ++pos;
55
 
          }
56
 
      }
57
 
 
58
 
    return ret;
59
 
  }
60
 
 
61
 
}
62
 
 
63
 
std::string
64
 
sbuild::basename (std::string name,
65
 
                  char        separator)
66
 
{
67
 
  // Remove trailing separators
68
 
  std::string::size_type cur = name.length();
69
 
  while (cur - 1 != 0 && name[cur - 1] == separator)
70
 
    --cur;
71
 
  name.resize(cur);
72
 
 
73
 
  // Find last separator
74
 
  std::string::size_type pos = name.rfind(separator);
75
 
 
76
 
  std::string ret;
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
81
 
  else
82
 
    ret = name.substr(pos + 1); // Basename only
83
 
 
84
 
  return remove_duplicates(ret, separator);
85
 
}
86
 
 
87
 
std::string
88
 
sbuild::dirname (std::string name,
89
 
                 char        separator)
90
 
{
91
 
  // Remove trailing separators
92
 
  std::string::size_type cur = name.length();
93
 
  while (cur - 1 != 0 && name[cur - 1] == separator)
94
 
    --cur;
95
 
  name.resize(cur);
96
 
 
97
 
  // Find last separator
98
 
  std::string::size_type pos = name.rfind(separator);
99
 
 
100
 
  std::string ret;
101
 
  if (pos == std::string::npos)
102
 
    ret = "."; // No directory components
103
 
  else if (pos == 0)
104
 
    ret = separator;
105
 
  else
106
 
    ret = name.substr(0, pos); // Dirname part
107
 
 
108
 
  return remove_duplicates(ret, separator);
109
 
}
110
 
 
111
 
std::string
112
 
sbuild::normalname (std::string name,
113
 
                    char        separator)
114
 
{
115
 
  // Remove trailing separators
116
 
  std::string::size_type cur = name.length();
117
 
  while (cur - 1 != 0 && name[cur - 1] == separator)
118
 
    --cur;
119
 
  name.resize(cur);
120
 
 
121
 
  return remove_duplicates(name, separator);
122
 
}
123
 
 
124
 
std::string
125
 
sbuild::string_list_to_string (sbuild::string_list const& list,
126
 
                               std::string const&         separator)
127
 
{
128
 
  std::string ret;
129
 
 
130
 
  for (string_list::const_iterator cur = list.begin();
131
 
       cur != list.end();
132
 
       ++cur)
133
 
    {
134
 
      ret += *cur;
135
 
      if (cur + 1 != list.end())
136
 
        ret += separator;
137
 
    }
138
 
 
139
 
  return ret;
140
 
}
141
 
 
142
 
string_list
143
 
sbuild::split_string (std::string const& value,
144
 
                      char               separator)
145
 
{
146
 
  string_list ret;
147
 
 
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);
153
 
 
154
 
  while (pos !=std::string::npos || last_pos != std::string::npos)
155
 
    {
156
 
      // Add to list
157
 
      ret.push_back(value.substr(last_pos, pos - last_pos));
158
 
      // Find next
159
 
      last_pos = value.find_first_not_of(separator, pos);
160
 
      pos = value.find_first_of(separator, last_pos);
161
 
    }
162
 
 
163
 
  return ret;
164
 
}
165
 
 
166
 
std::string
167
 
sbuild::find_program_in_path (std::string const& program,
168
 
                              std::string const& path,
169
 
                              std::string const& prefix)
170
 
{
171
 
  if (program.find_first_of('/') != std::string::npos)
172
 
    return program;
173
 
 
174
 
  string_list dirs = split_string(path, ':');
175
 
 
176
 
  for (string_list::const_iterator dir = dirs.begin();
177
 
       dir != dirs.end();
178
 
       ++dir)
179
 
    {
180
 
      std::string realname = *dir + '/' + program;
181
 
      std::string absname;
182
 
      if (prefix.length() > 0)
183
 
        {
184
 
          absname = prefix;
185
 
          if (dir->length() > 0 && (*dir)[0] != '/')
186
 
            absname += '/';
187
 
        }
188
 
      absname += realname;
189
 
 
190
 
      struct stat statbuf;
191
 
      if (stat(absname.c_str(), &statbuf) == 0)
192
 
        {
193
 
          if (S_ISREG(statbuf.st_mode) &&
194
 
              access (absname.c_str(), X_OK) == 0)
195
 
            return realname;
196
 
        }
197
 
    }
198
 
 
199
 
  return "";
200
 
}
201
 
 
202
 
char **
203
 
sbuild::string_list_to_strv (string_list const& str)
204
 
{
205
 
  char **ret = new char *[str.size() + 1];
206
 
 
207
 
  for (string_list::size_type i = 0;
208
 
       i < str.size();
209
 
       ++i)
210
 
    {
211
 
      ret[i] = new char[str[i].length() + 1];
212
 
      std::strcpy(ret[i], str[i].c_str());
213
 
    }
214
 
  ret[str.size()] = 0;
215
 
 
216
 
  return ret;
217
 
}
218
 
 
219
 
 
220
 
void
221
 
sbuild::strv_delete (char **strv)
222
 
{
223
 
  for (char **pos = strv; pos != 0 && *pos != 0; ++pos)
224
 
    delete *pos;
225
 
  delete[] strv;
226
 
}
227
 
 
228
 
/*
229
 
 * Local Variables:
230
 
 * mode:C++
231
 
 * End:
232
 
 */