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