- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages

- File List
- File Members

# 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
    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       // To ensure 2 spaces of separation between name and value
    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 /* SBUILD_FORMAT_DETAIL_H */
    00181
    00182 /*
    00183  * Local Variables:
    00184  * mode:C++
    00185  * End:
    00186  */