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

« back to all changes in this revision

Viewing changes to sbuild/sbuild-environment.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-environment.h"
 
23
 
 
24
using boost::format;
 
25
using namespace sbuild;
 
26
 
 
27
environment::environment ():
 
28
  std::map<std::string,std::string>()
 
29
{
 
30
}
 
31
 
 
32
environment::environment (char **environment):
 
33
  std::map<std::string,std::string>()
 
34
{
 
35
  add(environment);
 
36
}
 
37
 
 
38
environment::~environment ()
 
39
{
 
40
}
 
41
 
 
42
void
 
43
environment::add (char **environment)
 
44
{
 
45
  if (environment)
 
46
    {
 
47
      for (char **ev = environment; ev != 0 && *ev != 0; ++ev)
 
48
        add(std::string(*ev));
 
49
    }
 
50
}
 
51
 
 
52
void
 
53
environment::add (environment const& environment)
 
54
{
 
55
  for (const_iterator pos = environment.begin();
 
56
       pos != environment.end();
 
57
       ++pos)
 
58
    add(*pos);
 
59
}
 
60
 
 
61
void
 
62
environment::add (std::string const& value)
 
63
{
 
64
  std::string::size_type pos = value.find('=');
 
65
  if (pos != std::string::npos && pos != 0)
 
66
    {
 
67
      std::string key = value.substr(0, pos);
 
68
      std::string val;
 
69
      if (pos < value.length())
 
70
        val = value.substr(pos + 1);
 
71
      add(std::make_pair(key, val));
 
72
    }
 
73
  else
 
74
    {
 
75
      add(std::make_pair(value, std::string()));
 
76
    }
 
77
}
 
78
 
 
79
void
 
80
environment::add (value_type const& value)
 
81
{
 
82
  remove(value);
 
83
  if (!value.second.empty())
 
84
    insert(value);
 
85
}
 
86
 
 
87
void
 
88
environment::remove (char **environment)
 
89
{
 
90
  if (environment)
 
91
    {
 
92
      for (char **ev = environment; ev != 0 && *ev != 0; ++ev)
 
93
        remove(std::string(*ev));
 
94
    }
 
95
}
 
96
 
 
97
void
 
98
environment::remove (environment const& environment)
 
99
{
 
100
  for (const_iterator pos = environment.begin();
 
101
       pos != environment.end();
 
102
       ++pos)
 
103
    remove(*pos);
 
104
}
 
105
 
 
106
void
 
107
environment::remove (std::string const& value)
 
108
{
 
109
  std::string::size_type pos = value.find('=');
 
110
  if (pos != std::string::npos && pos != 0)
 
111
    {
 
112
      std::string key = value.substr(0, pos);
 
113
      std::string val;
 
114
      if (pos < value.length())
 
115
        val = value.substr(pos + 1);
 
116
      remove(std::make_pair(key, val));
 
117
    }
 
118
  else
 
119
    {
 
120
      remove(std::make_pair(value, std::string()));
 
121
    }
 
122
}
 
123
 
 
124
void
 
125
environment::remove (value_type const& value)
 
126
{
 
127
  iterator pos = find(value.first);
 
128
  if (pos != end())
 
129
    erase(pos);
 
130
}
 
131
 
 
132
char **
 
133
environment::get_strv () const
 
134
{
 
135
  char **ret = new char *[size() + 1];
 
136
 
 
137
  size_type idx = 0;
 
138
  for (const_iterator pos = begin(); pos != end(); ++pos, ++idx)
 
139
    {
 
140
      std::string envitem = pos->first + "=" + pos->second;
 
141
      ret[idx] = new char[envitem.length() + 1];
 
142
      std::strcpy(ret[idx], envitem.c_str());
 
143
    }
 
144
  ret[size()] = 0;
 
145
 
 
146
  return ret;
 
147
}
 
148
 
 
149
/*
 
150
 * Local Variables:
 
151
 * mode:C++
 
152
 * End:
 
153
 */