sbuild-format-detail.h

Go to the documentation of this file.
00001 /* Copyright © 2005-2006  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 2 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, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00016  * MA  02111-1307  USA
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       // To ensure 2 spaces of separation between name and value
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 /* SBUILD_FORMAT_DETAIL_H */
00155 
00156 /*
00157  * Local Variables:
00158  * mode:C++
00159  * End:
00160  */

Generated on Thu Jul 20 16:35:17 2006 for schroot by  doxygen 1.4.7