sbuild  1.6.10
sbuild-format-detail.h
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 #ifndef SBUILD_FORMAT_DETAIL_H
20 #define SBUILD_FORMAT_DETAIL_H
21 
22 #include <sbuild/sbuild-types.h>
23 #include <sbuild/sbuild-util.h>
24 
25 #include <cwchar>
26 #include <iomanip>
27 #include <locale>
28 #include <ostream>
29 #include <sstream>
30 #include <string>
31 
32 namespace sbuild
33 {
34 
39  {
40  public:
47  format_detail (const std::string& title,
48  std::locale locale);
49 
50  virtual ~format_detail ();
51 
60  add (std::string const& name,
61  std::string const& value);
62 
71  add (std::string const& name,
72  bool value);
73 
82  add (std::string const& name,
83  string_list const& value);
84 
92  template<typename T>
94  add (std::string const& name,
95  T const& value)
96  {
97  std::ostringstream varstring;
98  varstring.imbue(this->locale);
99  varstring << value;
100  return add(name, varstring.str());
101  }
102 
103  private:
110  std::string
111  get_title () const;
112 
120  template <class charT, class traits>
121  friend
122  std::basic_ostream<charT,traits>&
123  operator << (std::basic_ostream<charT,traits>& stream,
124  format_detail const& rhs)
125  {
126  std::locale loc = stream.getloc();
127  int max_width = 0;
128 
129  for (format_detail::list_type::const_iterator pos = rhs.items.begin();
130  pos != rhs.items.end();
131  ++pos)
132  {
133  std::wstring wide = widen_string(pos->first, loc);
134  int width = wcswidth(wide.c_str(), wide.length());
135 
136  if (max_width < width)
137  max_width = width;
138  }
139 
140  if (max_width < 20)
141  max_width = 20;
142  // To ensure 2 spaces of separation between name and value
143  max_width += 2;
144 
145  stream << " " << rhs.get_title() << '\n';
146 
147  for (format_detail::list_type::const_iterator pos = rhs.items.begin();
148  pos != rhs.items.end();
149  ++pos)
150  {
151  std::wostringstream ws;
152  ws.imbue(loc);
153 
154  std::wstring wide = widen_string(pos->first, loc);
155  ws << L" " << std::setw(max_width) << std::left << wide;
156 
157  stream << narrow_string(ws.str(), loc) << pos->second << '\n';
158  }
159 
160  return stream;
161  }
162 
163  private:
165  typedef std::pair<std::string,std::string> value_type;
167  typedef std::vector<value_type> list_type;
168 
170  std::string title;
172  std::locale locale;
174  list_type items;
175  };
176 
177 }
178 
179 #endif /* SBUILD_FORMAT_DETAIL_H */
180 
181 /*
182  * Local Variables:
183  * mode:C++
184  * End:
185  */
std::wstring widen_string(std::string const &str, std::locale locale)
Widen a string.
Definition: sbuild-util.cc:332
Debian source builder components.
Definition: sbuild-auth-null.h:24
std::string title
The title of the items to format.
Definition: sbuild-format-detail.h:170
std::string get_title() const
Get the title of the chroot.
Definition: sbuild-format-detail.cc:87
std::locale locale
The locale to use for output.
Definition: sbuild-format-detail.h:172
format_detail & add(std::string const &name, std::string const &value)
Add a name-value pair (string specialisation).
Definition: sbuild-format-detail.cc:42
std::pair< std::string, std::string > value_type
Name and value pairs.
Definition: sbuild-format-detail.h:165
format_detail(const std::string &title, std::locale locale)
The constructor.
Definition: sbuild-format-detail.cc:29
std::vector< std::string > string_list
A string vector.
Definition: sbuild-types.h:38
Format names and values for output.
Definition: sbuild-format-detail.h:38
std::vector< value_type > list_type
List of name and value pairs.
Definition: sbuild-format-detail.h:167
std::string narrow_string(std::wstring const &str, std::locale locale)
Narrow a string.
Definition: sbuild-util.cc:379
format_detail & add(std::string const &name, T const &value)
Add a name-value pair.
Definition: sbuild-format-detail.h:94
list_type items
The items to format;.
Definition: sbuild-format-detail.h:174