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
00053 format_detail&
00054 add (std::string const& name,
00055 std::string const& value);
00056
00057 format_detail&
00058 add (std::string const& name,
00059 bool value);
00060
00061 format_detail&
00062 add (std::string const& name,
00063 string_list const& value);
00064
00065 template<typename T>
00066 format_detail&
00067 add (std::string const& name,
00068 T const& value)
00069 {
00070 std::ostringstream varstring;
00071 varstring.imbue(this->locale);
00072 varstring << value;
00073 return add(name, varstring.str());
00074 }
00075
00076 private:
00083 std::string
00084 get_title () const;
00085
00093 template <class charT, class traits>
00094 friend
00095 std::basic_ostream<charT,traits>&
00096 operator << (std::basic_ostream<charT,traits>& stream,
00097 format_detail const& rhs)
00098 {
00099 std::locale loc = stream.getloc();
00100 int max_width = 0;
00101
00102 for (format_detail::list_type::const_iterator pos = rhs.items.begin();
00103 pos != rhs.items.end();
00104 ++pos)
00105 {
00106 std::wstring wide = widen_string(pos->first, loc);
00107 int width = wcswidth(wide.c_str(), wide.length());
00108
00109 if (max_width < width)
00110 max_width = width;
00111 }
00112
00113 if (max_width < 20)
00114 max_width = 20;
00115
00116 max_width += 2;
00117
00118 stream << " " << rhs.get_title() << '\n';
00119
00120 for (format_detail::list_type::const_iterator pos = rhs.items.begin();
00121 pos != rhs.items.end();
00122 ++pos)
00123 {
00124 std::wostringstream ws;
00125 ws.imbue(loc);
00126
00127 std::wstring wide = widen_string(pos->first, loc);
00128 ws << L" " << std::setw(max_width) << std::left << wide;
00129
00130 stream << narrow_string(ws.str(), loc) << pos->second << '\n';
00131 }
00132
00133 return stream;
00134 }
00135
00136 private:
00138 typedef std::pair<std::string,std::string> value_type;
00140 typedef std::vector<value_type> list_type;
00141
00143 std::string title;
00144
00146 std::locale locale;
00147
00149 list_type items;
00150 };
00151
00152 }
00153
00154 #endif
00155
00156
00157
00158
00159
00160