sbuild
1.4.26
|
00001 /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> 00002 * 00003 * schroot is free software: you can redistribute it and/or modify it 00004 * under the terms of the GNU General Public License as published by 00005 * the Free Software Foundation, either version 3 of the License, or 00006 * (at your option) any later version. 00007 * 00008 * schroot is distributed in the hope that it will be useful, but 00009 * WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU General Public License 00014 * along with this program. If not, see 00015 * <http://www.gnu.org/licenses/>. 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 // To ensure 2 spaces of separation between name and value 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 /* SBUILD_FORMAT_DETAIL_H */ 00180 00181 /* 00182 * Local Variables: 00183 * mode:C++ 00184 * End: 00185 */