OInterfaceContainerHelper3 needs to be thread-safe
[LibreOffice.git] / svl / source / misc / gridprinter.cxx
blobae910e4f0cf7e9314343958b8f574183213c398a
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_custom_func1.hpp>
15 #include <mdds/multi_type_vector_macro.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 namespace {
31 struct matrix_trait
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;
43 namespace rtl {
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)
51 namespace svl {
53 typedef mdds::multi_type_matrix<matrix_trait> MatrixImplType;
55 struct GridPrinter::Impl
57 MatrixImplType maMatrix;
58 bool mbPrint;
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
78 if (!mpImpl->mbPrint)
79 return;
81 if (pHeader)
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.
99 OUStringBuffer aBuf;
100 aBuf.append("+");
101 for (size_t col = 0; col < ns.column; ++col)
103 aBuf.append("-");
104 for (sal_Int32 i = 0; i < aColWidths[col]; ++i)
105 aBuf.append(u'-');
106 aBuf.append("-+");
109 OUString aSep = aBuf.makeStringAndClear();
111 // Now print to stdout.
112 cout << aSep << endl;
113 for (size_t row = 0; row < ns.row; ++row)
115 cout << "| ";
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();
120 aBuf.append(aStr);
121 for (size_t i = 0; i < nPadding; ++i)
122 aBuf.append(u' ');
123 cout << aBuf.makeStringAndClear() << " | ";
125 cout << endl;
126 cout << aSep << endl;
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */