sbuild-format-detail.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef SBUILD_FORMAT_DETAIL_H
00020 #define SBUILD_FORMAT_DETAIL_H
00021
00022 #include <sbuild/sbuild-types.h>
00023 #include <sbuild/sbuild-util.h>
00024
00025 #include <cwchar>
00026 #include <iomanip>
00027 #include <locale>
00028 #include <ostream>
00029 #include <sstream>
00030 #include <string>
00031
00032 namespace sbuild
00033 {
00034
00038 class format_detail
00039 {
00040 public:
00047 format_detail (const std::string& title,
00048 std::locale locale);
00049
00050 virtual ~format_detail ();
00051
00059 format_detail&
00060 add (std::string const& name,
00061 std::string const& value);
00062
00070 format_detail&
00071 add (std::string const& name,
00072 bool value);
00073
00081 format_detail&
00082 add (std::string const& name,
00083 string_list const& value);
00084
00092 template<typename T>
00093 format_detail&
00094 add (std::string const& name,
00095 T const& value)
00096 {
00097 std::ostringstream varstring;
00098 varstring.imbue(this->locale);
00099 varstring << value;
00100 return add(name, varstring.str());
00101 }
00102
00103 private:
00110 std::string
00111 get_title () const;
00112
00120 template <class charT, class traits>
00121 friend
00122 std::basic_ostream<charT,traits>&
00123 operator << (std::basic_ostream<charT,traits>& stream,
00124 format_detail const& rhs)
00125 {
00126 std::locale loc = stream.getloc();
00127 int max_width = 0;
00128
00129 for (format_detail::list_type::const_iterator pos = rhs.items.begin();
00130 pos != rhs.items.end();
00131 ++pos)
00132 {
00133 std::wstring wide = widen_string(pos->first, loc);
00134 int width = wcswidth(wide.c_str(), wide.length());
00135
00136 if (max_width < width)
00137 max_width = width;
00138 }
00139
00140 if (max_width < 20)
00141 max_width = 20;
00142
00143 max_width += 2;
00144
00145 stream << " " << rhs.get_title() << '\n';
00146
00147 for (format_detail::list_type::const_iterator pos = rhs.items.begin();
00148 pos != rhs.items.end();
00149 ++pos)
00150 {
00151 std::wostringstream ws;
00152 ws.imbue(loc);
00153
00154 std::wstring wide = widen_string(pos->first, loc);
00155 ws << L" " << std::setw(max_width) << std::left << wide;
00156
00157 stream << narrow_string(ws.str(), loc) << pos->second << '\n';
00158 }
00159
00160 return stream;
00161 }
00162
00163 private:
00165 typedef std::pair<std::string,std::string> value_type;
00167 typedef std::vector<value_type> list_type;
00168
00170 std::string title;
00172 std::locale locale;
00174 list_type items;
00175 };
00176
00177 }
00178
00179 #endif
00180
00181
00182
00183
00184
00185