LCOV - code coverage report
Current view: top level - svl/source/misc - gridprinter.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 16 48 33.3 %
Date: 2015-06-13 12:38:46 Functions: 11 13 84.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  */
       9             : 
      10             : #include <svl/gridprinter.hxx>
      11             : #include <rtl/ustrbuf.hxx>
      12             : 
      13             : #include <mdds/multi_type_vector_types.hpp>
      14             : #include <mdds/multi_type_vector_trait.hpp>
      15             : #include <mdds/multi_type_vector_custom_func1.hpp>
      16             : #include <mdds/multi_type_matrix.hpp>
      17             : 
      18             : #include <iostream>
      19             : 
      20             : using namespace std;
      21             : 
      22             : namespace svl {
      23             : 
      24             : // String ID
      25             : const mdds::mtv::element_t element_type_string = mdds::mtv::element_type_user_start;
      26             : // String block
      27             : typedef mdds::mtv::default_element_block<element_type_string, OUString> string_block;
      28             : 
      29             : struct custom_string_trait
      30             : {
      31             :     typedef OUString string_type;
      32             :     typedef string_block string_element_block;
      33             : 
      34             :     static const mdds::mtv::element_t string_type_identifier = element_type_string;
      35             : 
      36             :     typedef mdds::mtv::custom_block_func1<string_block> element_block_func;
      37             : };
      38             : 
      39             : }
      40             : 
      41             : namespace rtl {
      42             : 
      43             : // Callbacks for the string block. This needs to be in the same namespace as
      44             : // OUString for argument dependent lookup.
      45        4637 : MDDS_MTV_DEFINE_ELEMENT_CALLBACKS(OUString, svl::element_type_string, OUString(), svl::string_block)
      46             : 
      47             : }
      48             : 
      49             : namespace svl {
      50             : 
      51             : typedef mdds::multi_type_matrix<custom_string_trait> MatrixImplType;
      52             : 
      53         127 : struct GridPrinter::Impl
      54             : {
      55             :     MatrixImplType maMatrix;
      56             :     bool mbPrint;
      57             : 
      58         127 :     Impl( size_t nRows, size_t nCols, bool bPrint ) :
      59         127 :         maMatrix(nRows, nCols, OUString()), mbPrint(bPrint) {}
      60             : };
      61             : 
      62         127 : GridPrinter::GridPrinter( size_t nRows, size_t nCols, bool bPrint ) :
      63         127 :     mpImpl(new Impl(nRows, nCols, bPrint)) {}
      64             : 
      65         127 : GridPrinter::~GridPrinter()
      66             : {
      67         127 :     delete mpImpl;
      68         127 : }
      69             : 
      70        2255 : void GridPrinter::set( size_t nRow, size_t nCol, const OUString& rStr )
      71             : {
      72        2255 :     mpImpl->maMatrix.set(nRow, nCol, rStr);
      73        2255 : }
      74             : 
      75         127 : void GridPrinter::print( const char* pHeader ) const
      76             : {
      77         127 :     if (!mpImpl->mbPrint)
      78         254 :         return;
      79             : 
      80           0 :     if (pHeader)
      81           0 :         cout << pHeader << endl;
      82             : 
      83           0 :     MatrixImplType::size_pair_type ns = mpImpl->maMatrix.size();
      84           0 :     vector<sal_Int32> aColWidths(ns.column, 0);
      85             : 
      86             :     // Calculate column widths first.
      87           0 :     for (size_t row = 0; row < ns.row; ++row)
      88             :     {
      89           0 :         for (size_t col = 0; col < ns.column; ++col)
      90             :         {
      91           0 :             OUString aStr = mpImpl->maMatrix.get_string(row, col);
      92           0 :             if (aColWidths[col] < aStr.getLength())
      93           0 :                 aColWidths[col] = aStr.getLength();
      94           0 :         }
      95             :     }
      96             : 
      97             :     // Make the row separator string.
      98           0 :     OUStringBuffer aBuf;
      99           0 :     aBuf.appendAscii("+");
     100           0 :     for (size_t col = 0; col < ns.column; ++col)
     101             :     {
     102           0 :         aBuf.appendAscii("-");
     103           0 :         for (sal_Int32 i = 0; i < aColWidths[col]; ++i)
     104           0 :             aBuf.append(sal_Unicode('-'));
     105           0 :         aBuf.appendAscii("-+");
     106             :     }
     107             : 
     108           0 :     OUString aSep = aBuf.makeStringAndClear();
     109             : 
     110             :     // Now print to stdout.
     111           0 :     cout << aSep << endl;
     112           0 :     for (size_t row = 0; row < ns.row; ++row)
     113             :     {
     114           0 :         cout << "| ";
     115           0 :         for (size_t col = 0; col < ns.column; ++col)
     116             :         {
     117           0 :             OUString aStr = mpImpl->maMatrix.get_string(row, col);
     118           0 :             size_t nPadding = aColWidths[col] - aStr.getLength();
     119           0 :             aBuf.append(aStr);
     120           0 :             for (size_t i = 0; i < nPadding; ++i)
     121           0 :                 aBuf.append(sal_Unicode(' '));
     122           0 :             cout << aBuf.makeStringAndClear() << " | ";
     123           0 :         }
     124           0 :         cout << endl;
     125           0 :         cout << aSep << endl;
     126           0 :     }
     127             : }
     128             : 
     129         819 : }
     130             : 
     131             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11