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_trait.hpp>
15 #include <mdds/multi_type_vector_custom_func1.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
;
29 struct custom_string_trait
31 typedef OUString string_type
;
32 typedef string_block string_element_block
;
34 static const mdds::mtv::element_t string_type_identifier
= element_type_string
;
36 typedef mdds::mtv::custom_block_func1
<string_block
> element_block_func
;
43 // Callbacks for the string block. This needs to be in the same namespace as
44 // OUString for argument dependent lookup.
45 MDDS_MTV_DEFINE_ELEMENT_CALLBACKS(OUString
, svl::element_type_string
, OUString(), svl::string_block
)
51 typedef mdds::multi_type_matrix
<custom_string_trait
> MatrixImplType
;
53 struct GridPrinter::Impl
55 MatrixImplType maMatrix
;
58 Impl( size_t nRows
, size_t nCols
, bool bPrint
) :
59 maMatrix(nRows
, nCols
, OUString()), mbPrint(bPrint
) {}
62 GridPrinter::GridPrinter( size_t nRows
, size_t nCols
, bool bPrint
) :
63 mpImpl(new Impl(nRows
, nCols
, bPrint
)) {}
65 GridPrinter::~GridPrinter()
70 void GridPrinter::set( size_t nRow
, size_t nCol
, const OUString
& rStr
)
72 mpImpl
->maMatrix
.set(nRow
, nCol
, rStr
);
75 void GridPrinter::print( const char* pHeader
) const
81 cout
<< pHeader
<< endl
;
83 MatrixImplType::size_pair_type ns
= mpImpl
->maMatrix
.size();
84 vector
<sal_Int32
> aColWidths(ns
.column
, 0);
86 // Calculate column widths first.
87 for (size_t row
= 0; row
< ns
.row
; ++row
)
89 for (size_t col
= 0; col
< ns
.column
; ++col
)
91 OUString aStr
= mpImpl
->maMatrix
.get_string(row
, col
);
92 if (aColWidths
[col
] < aStr
.getLength())
93 aColWidths
[col
] = aStr
.getLength();
97 // Make the row separator string.
99 aBuf
.appendAscii("+");
100 for (size_t col
= 0; col
< ns
.column
; ++col
)
102 aBuf
.appendAscii("-");
103 for (sal_Int32 i
= 0; i
< aColWidths
[col
]; ++i
)
104 aBuf
.append(sal_Unicode('-'));
105 aBuf
.appendAscii("-+");
108 OUString aSep
= aBuf
.makeStringAndClear();
110 // Now print to stdout.
111 cout
<< aSep
<< endl
;
112 for (size_t row
= 0; row
< ns
.row
; ++row
)
115 for (size_t col
= 0; col
< ns
.column
; ++col
)
117 OUString aStr
= mpImpl
->maMatrix
.get_string(row
, col
);
118 size_t nPadding
= aColWidths
[col
] - aStr
.getLength();
120 for (size_t i
= 0; i
< nPadding
; ++i
)
121 aBuf
.append(sal_Unicode(' '));
122 cout
<< aBuf
.makeStringAndClear() << " | ";
125 cout
<< aSep
<< endl
;
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */