LibreOffice Module svl (master) 1
gridprinter.cxx
Go to the documentation of this file.
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/macro.hpp>
15#include <mdds/multi_type_matrix.hpp>
16
17#include <iostream>
18
19namespace svl {
20
21// String ID
22const mdds::mtv::element_t element_type_string = mdds::mtv::element_type_user_start;
23// String block
24typedef mdds::mtv::default_element_block<element_type_string, OUString> string_block;
25
26namespace {
27
28struct matrix_traits
29{
30 typedef string_block string_element_block;
31 typedef mdds::mtv::uint16_element_block integer_element_block;
32};
33
34}
35
36}
37
38namespace rtl {
39
40// Callbacks for the string block. This needs to be in the same namespace as
41// OUString for argument dependent lookup.
42MDDS_MTV_DEFINE_ELEMENT_CALLBACKS(OUString, svl::element_type_string, OUString(), svl::string_block)
43
44}
45
46namespace svl {
47
48typedef mdds::multi_type_matrix<matrix_traits> MatrixImplType;
49
51{
53 bool mbPrint;
54
55 Impl( size_t nRows, size_t nCols, bool bPrint ) :
56 maMatrix(nRows, nCols, OUString()), mbPrint(bPrint) {}
57};
58
59GridPrinter::GridPrinter( size_t nRows, size_t nCols, bool bPrint ) :
60 mpImpl(new Impl(nRows, nCols, bPrint)) {}
61
63{
64}
65
66void GridPrinter::set( size_t nRow, size_t nCol, const OUString& rStr )
67{
68 mpImpl->maMatrix.set(nRow, nCol, rStr);
69}
70
71void GridPrinter::print( const char* pHeader ) const
72{
73 if (!mpImpl->mbPrint)
74 return;
75
76 if (pHeader)
77 std::cout << pHeader << std::endl;
78
79 MatrixImplType::size_pair_type ns = mpImpl->maMatrix.size();
80 std::vector<sal_Int32> aColWidths(ns.column, 0);
81
82 // Calculate column widths first.
83 for (size_t row = 0; row < ns.row; ++row)
84 {
85 for (size_t col = 0; col < ns.column; ++col)
86 {
87 OUString aStr = mpImpl->maMatrix.get_string(row, col);
88 if (aColWidths[col] < aStr.getLength())
89 aColWidths[col] = aStr.getLength();
90 }
91 }
92
93 // Make the row separator string.
94 OUStringBuffer aBuf("+");
95 for (size_t col = 0; col < ns.column; ++col)
96 {
97 aBuf.append("-");
98 for (sal_Int32 i = 0; i < aColWidths[col]; ++i)
99 aBuf.append(u'-');
100 aBuf.append("-+");
101 }
102
103 OUString aSep = aBuf.makeStringAndClear();
104
105 // Now print to stdout.
106 std::cout << aSep << std::endl;
107 for (size_t row = 0; row < ns.row; ++row)
108 {
109 std::cout << "| ";
110 for (size_t col = 0; col < ns.column; ++col)
111 {
112 OUString aStr = mpImpl->maMatrix.get_string(row, col);
113 size_t nPadding = aColWidths[col] - aStr.getLength();
114 aBuf.append(aStr);
115 for (size_t i = 0; i < nPadding; ++i)
116 aBuf.append(u' ');
117 std::cout << aBuf.makeStringAndClear() << " | ";
118 }
119 std::cout << std::endl;
120 std::cout << aSep << std::endl;
121 }
122}
123
124}
125
126/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void set(size_t nRow, size_t nCol, const OUString &rStr)
Definition: gridprinter.cxx:66
std::unique_ptr< Impl > mpImpl
Definition: gridprinter.hxx:25
void print(const char *pHeader) const
Definition: gridprinter.cxx:71
GridPrinter(size_t nRows, size_t nCols, bool bPrint)
Definition: gridprinter.cxx:59
float u
aStr
aBuf
RttiCompleteObjectLocator col
ns
int i
const mdds::mtv::element_t element_type_string
Definition: gridprinter.cxx:22
mdds::mtv::default_element_block< element_type_string, OUString > string_block
Definition: gridprinter.cxx:24
mdds::multi_type_matrix< matrix_traits > MatrixImplType
Definition: gridprinter.cxx:48
MatrixImplType maMatrix
Definition: gridprinter.cxx:52
Impl(size_t nRows, size_t nCols, bool bPrint)
Definition: gridprinter.cxx:55