1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <svl/gridprinter.hxx>
11 #include <rtl/ustrbuf.hxx>
13 #include <mdds/multi_type_vector_types.hpp>
14 #include <mdds/multi_type_vector_custom_func1.hpp>
15 #include <mdds/multi_type_vector_macro.hpp>
16 #include <mdds/multi_type_matrix.hpp>
25 const mdds::mtv::element_t element_type_string
= mdds::mtv::element_type_user_start
;
27 typedef mdds::mtv::default_element_block
<element_type_string
, OUString
> string_block
;
33 typedef string_block string_element_block
;
34 typedef mdds::mtv::uint16_element_block integer_element_block
;
36 typedef mdds::mtv::custom_block_func1
<string_block
> element_block_func
;
45 // Callbacks for the string block. This needs to be in the same namespace as
46 // OUString for argument dependent lookup.
47 MDDS_MTV_DEFINE_ELEMENT_CALLBACKS(OUString
, svl::element_type_string
, OUString(), svl::string_block
)
53 typedef mdds::multi_type_matrix
<matrix_trait
> MatrixImplType
;
55 struct GridPrinter::Impl
57 MatrixImplType maMatrix
;
60 Impl( size_t nRows
, size_t nCols
, bool bPrint
) :
61 maMatrix(nRows
, nCols
, OUString()), mbPrint(bPrint
) {}
64 GridPrinter::GridPrinter( size_t nRows
, size_t nCols
, bool bPrint
) :
65 mpImpl(new Impl(nRows
, nCols
, bPrint
)) {}
67 GridPrinter::~GridPrinter()
71 void GridPrinter::set( size_t nRow
, size_t nCol
, const OUString
& rStr
)
73 mpImpl
->maMatrix
.set(nRow
, nCol
, rStr
);
76 void GridPrinter::print( const char* pHeader
) const
82 cout
<< pHeader
<< endl
;
84 MatrixImplType::size_pair_type ns
= mpImpl
->maMatrix
.size();
85 vector
<sal_Int32
> aColWidths(ns
.column
, 0);
87 // Calculate column widths first.
88 for (size_t row
= 0; row
< ns
.row
; ++row
)
90 for (size_t col
= 0; col
< ns
.column
; ++col
)
92 OUString aStr
= mpImpl
->maMatrix
.get_string(row
, col
);
93 if (aColWidths
[col
] < aStr
.getLength())
94 aColWidths
[col
] = aStr
.getLength();
98 // Make the row separator string.
101 for (size_t col
= 0; col
< ns
.column
; ++col
)
104 for (sal_Int32 i
= 0; i
< aColWidths
[col
]; ++i
)
109 OUString aSep
= aBuf
.makeStringAndClear();
111 // Now print to stdout.
112 cout
<< aSep
<< endl
;
113 for (size_t row
= 0; row
< ns
.row
; ++row
)
116 for (size_t col
= 0; col
< ns
.column
; ++col
)
118 OUString aStr
= mpImpl
->maMatrix
.get_string(row
, col
);
119 size_t nPadding
= aColWidths
[col
] - aStr
.getLength();
121 for (size_t i
= 0; i
< nPadding
; ++i
)
123 cout
<< aBuf
.makeStringAndClear() << " | ";
126 cout
<< aSep
<< endl
;
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */