bump product version to 5.0.4.1
[LibreOffice.git] / svl / source / misc / gridprinter.cxx
blob2cbadca0a8a959d2eaf8f3075e28c6aadb17aeea
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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>
18 #include <iostream>
20 using namespace std;
22 namespace svl {
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;
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;
41 namespace rtl {
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)
49 namespace svl {
51 typedef mdds::multi_type_matrix<custom_string_trait> MatrixImplType;
53 struct GridPrinter::Impl
55 MatrixImplType maMatrix;
56 bool mbPrint;
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()
67 delete mpImpl;
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
77 if (!mpImpl->mbPrint)
78 return;
80 if (pHeader)
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.
98 OUStringBuffer aBuf;
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)
114 cout << "| ";
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();
119 aBuf.append(aStr);
120 for (size_t i = 0; i < nPadding; ++i)
121 aBuf.append(sal_Unicode(' '));
122 cout << aBuf.makeStringAndClear() << " | ";
124 cout << endl;
125 cout << aSep << endl;
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */