source: trunk/debathena/third/schroot/bin/csbuild/csbuild-debian-changes.cc @ 24167

Revision 24167, 8.5 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
Line 
1/* Copyright © 2005-2007  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 3 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, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 *********************************************************************/
18
19#include <config.h>
20
21#include "csbuild-debian-changes.h"
22
23#include <fstream>
24
25#include <boost/format.hpp>
26
27using sbuild::_;
28using sbuild::N_;
29using boost::format;
30using namespace csbuild;
31
32namespace
33{
34
35  typedef std::pair<debian_changes::error_code,const char *> emap;
36
37  /**
38   * This is a list of the supported error codes.  It's used to
39   * construct the real error codes map.
40   */
41  emap init_errors[] =
42    {
43      // TRANSLATORS: %1% = file
44      emap(debian_changes::BAD_FILE,          N_("Can't open file '%1%'")),
45      // TRANSLATORS: %1% = line number in configuration file
46      // TRANSLATORS: %4% = key name ("keyname=value" in configuration file)
47      emap(debian_changes::DEPRECATED_KEY,    N_("line %1%: Deprecated key '%4%' used")),
48      // TRANSLATORS: %4% = key name ("keyname=value" in configuration file)
49      emap(debian_changes::DEPRECATED_KEY_NL, N_("Deprecated key '%4%' used")),
50      // TRANSLATORS: %1% = line number in configuration file
51      // TRANSLATORS: %4% = key name ("keyname=value" in configuration file)
52      emap(debian_changes::DISALLOWED_KEY,    N_("line %1%: Disallowed key '%4%' used")),
53      // TRANSLATORS: %4% = key name ("keyname=value" in configuration file)
54      emap(debian_changes::DISALLOWED_KEY_NL, N_("Disallowed key '%4%' used")),
55      // TRANSLATORS: %1% = line number in configuration file
56      // TRANSLATORS: %4% = key name ("keyname=value" in configuration file)
57      emap(debian_changes::DUPLICATE_KEY,     N_("line %1%: Duplicate key '%4%'")),
58      // TRANSLATORS: %1% = line number in configuration file
59      // TRANSLATORS: %4% = line contents as read from the configuration file
60      emap(debian_changes::INVALID_LINE,      N_("line %1%: Invalid line: \"%4%\"")),
61      // TRANSLATORS: %1% = line number in configuration file
62      // TRANSLATORS: %4% = key name ("keyname=value" in configuration file)
63      emap(debian_changes::MISSING_KEY,       N_("line %1%: Required key '%4%' is missing")),
64      // TRANSLATORS: %4% = key name ("keyname=value" in configuration file)
65      emap(debian_changes::MISSING_KEY_NL,    N_("Required key '%4%' is missing")),
66      // TRANSLATORS: %1% = line number in configuration file
67      // TRANSLATORS: %4% = line contents as read from the configuration file
68      emap(debian_changes::NO_KEY,            N_("line %1%: No key specified: \"%4%\"")),
69      // TRANSLATORS: %1% = line number in configuration file
70      // TRANSLATORS: %4% = key name ("keyname=value" in configuration file)
71      emap(debian_changes::OBSOLETE_KEY,      N_("line %1%: Obsolete key '%4%' used")),
72      // TRANSLATORS: %4% = key name ("keyname=value" in configuration file)
73      emap(debian_changes::OBSOLETE_KEY_NL,   N_("Obsolete key '%4%' used")),
74      // TRANSLATORS: %2% = key name ("keyname=value" in configuration file)
75      // TRANSLATORS: %4% = additional details
76      emap(debian_changes::PASSTHROUGH_K,    N_("%2%: %4%")),
77      // TRANSLATORS: %1% = line number in configuration file
78      // TRANSLATORS: %3% = key name ("keyname=value" in configuration file)
79      // TRANSLATORS: %4% = additional details
80      emap(debian_changes::PASSTHROUGH_LK,   N_("line %1%: %3%: %4%"))
81    };
82
83}
84
85template<>
86sbuild::error<debian_changes::error_code>::map_type
87sbuild::error<debian_changes::error_code>::error_strings
88(init_errors,
89 init_errors + (sizeof(init_errors) / sizeof(init_errors[0])));
90
91debian_changes::debian_changes ():
92  items()
93{
94}
95
96debian_changes::debian_changes (std::string const& file):
97  items()
98{
99  std::ifstream fs(file.c_str());
100  if (fs)
101    {
102      fs.imbue(std::locale::classic());
103      fs >> *this;
104    }
105  else
106    {
107      throw error(file, BAD_FILE);
108    }
109}
110
111debian_changes::debian_changes (std::istream& stream):
112  items()
113{
114  stream >> *this;
115}
116
117debian_changes::~debian_changes()
118{
119}
120
121sbuild::string_list
122debian_changes::get_keys () const
123{
124  sbuild::string_list ret;
125
126  for (item_map_type::const_iterator pos = items.begin();
127       pos != items.end();
128       ++pos)
129    ret.push_back(pos->first);
130
131  return ret;
132}
133
134bool
135debian_changes::has_key (key_type const& key) const
136{
137  return (find_item(key) != 0);
138}
139
140debian_changes::size_type
141debian_changes::get_line (key_type const& key) const
142{
143  const item_type *found_item = find_item(key);
144  if (found_item)
145      return std::tr1::get<2>(*found_item);
146  else
147    return 0;
148}
149
150bool
151debian_changes::get_value (key_type const& key,
152                           value_type&     value) const
153{
154  sbuild::log_debug(sbuild::DEBUG_INFO)
155    << "Getting debian_changes key=" << key << std::endl;
156  const item_type *found_item = find_item(key);
157  if (found_item)
158    {
159      value_type const& val(std::tr1::get<1>(*found_item));
160      value = val;
161      return true;
162    }
163  sbuild::log_debug(sbuild::DEBUG_NOTICE)
164    << "key not found" << std::endl;
165  return false;
166}
167
168bool
169debian_changes::get_value (key_type const& key,
170                           priority        priority,
171                           value_type&     value) const
172{
173  bool status = get_value(key, value);
174  check_priority(key, priority, status);
175  return status;
176}
177
178void
179debian_changes::set_value (key_type const&   key,
180                           value_type const& value,
181                           size_type         line)
182{
183  item_map_type::iterator pos = items.find(key);
184  if (pos != items.end())
185    items.erase(pos);
186  items.insert
187    (item_map_type::value_type(key,
188                               item_type(key, value, line)));
189}
190
191void
192debian_changes::remove_key (key_type const& key)
193{
194  item_map_type::iterator pos = items.find(key);
195  if (pos != items.end())
196    items.erase(pos);
197}
198
199debian_changes&
200debian_changes::operator += (debian_changes const& rhs)
201{
202  for (item_map_type::const_iterator it = rhs.items.begin();
203       it != rhs.items.end();
204       ++it)
205    {
206      item_type const& item = it->second;
207      key_type const& key(std::tr1::get<0>(item));
208      value_type const& value(std::tr1::get<1>(item));
209      size_type const& line(std::tr1::get<2>(item));
210      set_value(key, value, line);
211    }
212
213  return *this;
214}
215
216debian_changes
217operator + (debian_changes const& lhs,
218            debian_changes const& rhs)
219{
220  debian_changes ret(lhs);
221  ret += rhs;
222  return ret;
223}
224
225const debian_changes::item_type *
226debian_changes::find_item (key_type const& key) const
227{
228  item_map_type::const_iterator pos = items.find(key);
229  if (pos != items.end())
230    return &pos->second;
231
232  return 0;
233}
234
235debian_changes::item_type *
236debian_changes::find_item (key_type const& key)
237{
238  item_map_type::iterator pos = items.find(key);
239  if (pos != items.end())
240    return &pos->second;
241
242  return 0;
243}
244
245void
246debian_changes::check_priority (key_type const& key,
247                                priority        priority,
248                                bool            valid) const
249{
250  if (valid == false)
251    {
252      size_type line = get_line(key);
253
254      switch (priority)
255        {
256        case PRIORITY_REQUIRED:
257          {
258            if (line)
259              throw error(line, MISSING_KEY, key);
260            else
261              throw error(MISSING_KEY_NL, key);
262          }
263          break;
264        default:
265          break;
266        }
267    }
268  else
269    {
270      size_type line = get_line(key);
271
272      switch (priority)
273        {
274        case PRIORITY_DEPRECATED:
275          {
276            if (line)
277              {
278                error e(line, DEPRECATED_KEY, key);
279                e.set_reason(_("This option will be removed in the future"));
280                log_exception_warning(e);
281              }
282            else
283              {
284                error e(DEPRECATED_KEY_NL, key);
285                e.set_reason(_("This option will be removed in the future"));
286                log_exception_warning(e);
287              }
288          }
289          break;
290        case PRIORITY_OBSOLETE:
291          {
292            if (line)
293              {
294                error e(line, OBSOLETE_KEY, key);
295                e.set_reason(_("This option has been removed, and no longer has any effect"));
296                log_exception_warning(e);
297              }
298            else
299              {
300                error e(OBSOLETE_KEY_NL, key);
301                e.set_reason(_("This option has been removed, and no longer has any effect"));
302                log_exception_warning(e);
303              }
304          }
305          break;
306        case PRIORITY_DISALLOWED:
307          {
308            if (line)
309              throw error(line, DISALLOWED_KEY, key);
310            else
311              throw error(DISALLOWED_KEY_NL, key);
312          }
313          break;
314        default:
315          break;
316        }
317    }
318}
Note: See TracBrowser for help on using the repository browser.