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/macro.hpp>
15 #include <mdds/multi_type_matrix.hpp>
22 const mdds::mtv::element_t element_type_string
= mdds::mtv::element_type_user_start
;
24 typedef mdds::mtv::default_element_block
<element_type_string
, OUString
> string_block
;
30 typedef string_block string_element_block
;
31 typedef mdds::mtv::uint16_element_block integer_element_block
;
40 // Callbacks for the string block. This needs to be in the same namespace as
41 // OUString for argument dependent lookup.
42 MDDS_MTV_DEFINE_ELEMENT_CALLBACKS(OUString
, svl::element_type_string
, OUString(), svl::string_block
)
48 typedef mdds::multi_type_matrix
<matrix_traits
> MatrixImplType
;
50 struct GridPrinter::Impl
52 MatrixImplType maMatrix
;
55 Impl( size_t nRows
, size_t nCols
, bool bPrint
) :
56 maMatrix(nRows
, nCols
, OUString()), mbPrint(bPrint
) {}
59 GridPrinter::GridPrinter( size_t nRows
, size_t nCols
, bool bPrint
) :
60 mpImpl(new Impl(nRows
, nCols
, bPrint
)) {}
62 GridPrinter::~GridPrinter()
66 void GridPrinter::set( size_t nRow
, size_t nCol
, const OUString
& rStr
)
68 #if defined __GNUC__ && !defined __clang__ && __GNUC__ == 12 && __cplusplus == 202002L
69 #pragma GCC diagnostic push
70 #pragma GCC diagnostic ignored "-Warray-bounds"
72 mpImpl
->maMatrix
.set(nRow
, nCol
, rStr
);
73 #if defined __GNUC__ && !defined __clang__ && __GNUC__ == 12 && __cplusplus == 202002L
74 #pragma GCC diagnostic pop
78 void GridPrinter::print( const char* pHeader
) const
84 std::cout
<< pHeader
<< std::endl
;
86 MatrixImplType::size_pair_type ns
= mpImpl
->maMatrix
.size();
87 std::vector
<sal_Int32
> aColWidths(ns
.column
, 0);
89 // Calculate column widths first.
90 for (size_t row
= 0; row
< ns
.row
; ++row
)
92 for (size_t col
= 0; col
< ns
.column
; ++col
)
94 OUString aStr
= mpImpl
->maMatrix
.get_string(row
, col
);
95 if (aColWidths
[col
] < aStr
.getLength())
96 aColWidths
[col
] = aStr
.getLength();
100 // Make the row separator string.
101 OUStringBuffer
aBuf("+");
102 for (size_t col
= 0; col
< ns
.column
; ++col
)
105 for (sal_Int32 i
= 0; i
< aColWidths
[col
]; ++i
)
110 OUString aSep
= aBuf
.makeStringAndClear();
112 // Now print to stdout.
113 std::cout
<< aSep
<< std::endl
;
114 for (size_t row
= 0; row
< ns
.row
; ++row
)
117 for (size_t col
= 0; col
< ns
.column
; ++col
)
119 OUString aStr
= mpImpl
->maMatrix
.get_string(row
, col
);
120 size_t nPadding
= aColWidths
[col
] - aStr
.getLength();
122 for (size_t i
= 0; i
< nPadding
; ++i
)
124 std::cout
<< aBuf
.makeStringAndClear() << " | ";
126 std::cout
<< std::endl
;
127 std::cout
<< aSep
<< std::endl
;
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */