Confdump Agent  1.4.0
TextFormatter.cpp
Go to the documentation of this file.
00001 /* 
00002  * Confdump-Agent - Dump static and runtime system configuration
00003  * Copyright (C) 2009-2012  Straton IT, SAS
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include "boost/foreach.hpp"
00019 
00020 #include "QVariant"
00021 #include "QTextStream"
00022 
00023 #include "Confdump/Result.hpp"
00024 #include "Confdump/TextFormatter.hpp"
00025 
00026 using namespace std;
00027 
00028 namespace
00029 {
00030 
00031 const size_t TAB_SPACES = 4;
00032 
00033 void printValueEndl( QTextStream &out, const QVariant &value, unsigned indent = 1 )
00034 {
00035     switch ( value.type() )
00036     {
00037     case QVariant::ByteArray:
00038         out << "<binary data, " << value.toByteArray().size() << " B>" << endl;
00039         break;
00040 
00041     case QVariant::List:
00042         out << endl;
00043         BOOST_FOREACH( QVariant element, value.toList() )
00044         {
00045             out << QString( indent * TAB_SPACES, ' ' ) << "- ";
00046             printValueEndl( out, element, indent + 1 );
00047         }
00048         break;
00049 
00050         case QVariant::Invalid:
00051         out << "<undefined>" << endl;
00052         break;
00053 
00054     default:
00055         if ( value.canConvert( QVariant::String ) )
00056             out << value.toString() << endl;
00057         else
00058             out << "<data of unhandled " << value.typeName() << " type>" << endl;
00059         break;
00060     }
00061 }
00062 
00063 } // ns
00064 
00065 namespace Confdump
00066 {
00067 
00068 TextFormatter::TextFormatter()
00069 {
00070 }
00071 
00072 TextFormatter::~TextFormatter()
00073 {
00074 }
00075 
00076 void TextFormatter::doPrintTable( QString title, const Result &result )
00077 {
00078     QTextStream out( device().get() );
00079 
00080     out << endl << endl << title << ": " << endl;
00081     BOOST_FOREACH( Result::Row row, result )
00082     {
00083         QStringList names( result.propertyNames() );
00084         QList<QVariant> values( result.propertyValues( row ) );
00085 
00086         out << "-" << endl;
00087 
00088         for ( int i = 0; i < names.size(); ++i )
00089         {
00090             out << QString( TAB_SPACES, ' ' ) << names[i] << ": ";
00091             printValueEndl( out, values[i] );
00092         }
00093     }
00094 }
00095 
00096 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends