[24167] | 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 | |
---|
| 35 | /** |
---|
| 36 | * Format names and values for output. |
---|
| 37 | */ |
---|
| 38 | class format_detail |
---|
| 39 | { |
---|
| 40 | public: |
---|
| 41 | /** |
---|
| 42 | * The constructor. |
---|
| 43 | * |
---|
| 44 | * @param title the title of the set of name and value pairs. |
---|
| 45 | * @param locale the locale to use for formatting the values. |
---|
| 46 | */ |
---|
| 47 | format_detail (const std::string& title, |
---|
| 48 | std::locale locale); |
---|
| 49 | |
---|
| 50 | virtual ~format_detail (); |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Add a name-value pair (string specialisation). |
---|
| 54 | * |
---|
| 55 | * @param name the name. |
---|
| 56 | * @param value the string value. |
---|
| 57 | * @returns a reference to the format_detail object. |
---|
| 58 | */ |
---|
| 59 | format_detail& |
---|
| 60 | add (std::string const& name, |
---|
| 61 | std::string const& value); |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Add a name-value pair (bool specialisation). |
---|
| 65 | * |
---|
| 66 | * @param name the name. |
---|
| 67 | * @param value the bool value. |
---|
| 68 | * @returns a reference to the format_detail object. |
---|
| 69 | */ |
---|
| 70 | format_detail& |
---|
| 71 | add (std::string const& name, |
---|
| 72 | bool value); |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Add a name-value pair (string_list specialisation). |
---|
| 76 | * |
---|
| 77 | * @param name the name. |
---|
| 78 | * @param value the string_list value. |
---|
| 79 | * @returns a reference to the format_detail object. |
---|
| 80 | */ |
---|
| 81 | format_detail& |
---|
| 82 | add (std::string const& name, |
---|
| 83 | string_list const& value); |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Add a name-value pair. |
---|
| 87 | * |
---|
| 88 | * @param name the name. |
---|
| 89 | * @param value the value. |
---|
| 90 | * @returns a reference to the format_detail object. |
---|
| 91 | */ |
---|
| 92 | template<typename T> |
---|
| 93 | format_detail& |
---|
| 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: |
---|
| 104 | /** |
---|
| 105 | * Get the title of the chroot. The title is formatted for |
---|
| 106 | * output. |
---|
| 107 | * |
---|
| 108 | * @returns the formatted title. |
---|
| 109 | */ |
---|
| 110 | std::string |
---|
| 111 | get_title () const; |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Output the format_detail to an ostream. |
---|
| 115 | * |
---|
| 116 | * @param stream the stream to output to. |
---|
| 117 | * @param rhs the format_detail to output. |
---|
| 118 | * @returns the stream. |
---|
| 119 | */ |
---|
| 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: |
---|
| 164 | /// Name and value pairs. |
---|
| 165 | typedef std::pair<std::string,std::string> value_type; |
---|
| 166 | /// List of name and value pairs. |
---|
| 167 | typedef std::vector<value_type> list_type; |
---|
| 168 | |
---|
| 169 | /// The title of the items to format. |
---|
| 170 | std::string title; |
---|
| 171 | /// The locale to use for output. |
---|
| 172 | std::locale locale; |
---|
| 173 | /// The items to format; |
---|
| 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 | */ |
---|