Line data Source code
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 : #ifndef SC_DEBUG_HELPER_HXX
11 : #define SC_DEBUG_HELPER_HXX
12 :
13 : /**
14 : * Print nicely formatted sheet content to stdout. Indispensable when
15 : * debugging the unit test code involving testing of sheet contents.
16 : */
17 :
18 : #include <rtl/strbuf.hxx>
19 : #include <rtl/ustring.hxx>
20 :
21 : #ifdef WNT
22 : #if !defined NOMINMAX
23 : #define NOMINMAX
24 : #endif
25 : #include <prewin.h>
26 : #include <postwin.h>
27 : #undef NOMINMAX
28 : #endif
29 :
30 : #define MDDS_HASH_CONTAINER_BOOST 1
31 : #include <mdds/mixed_type_matrix.hpp>
32 :
33 : #include <iostream>
34 :
35 : using namespace ::com::sun::star;
36 : using ::std::cout;
37 : using ::std::cerr;
38 : using ::std::endl;
39 : using ::std::vector;
40 :
41 :
42 61 : class SheetPrinter
43 : {
44 : typedef ::mdds::mixed_type_matrix<OUString, bool> MatrixType;
45 : public:
46 61 : SheetPrinter(size_t rows, size_t cols) :
47 61 : maMatrix(rows, cols, ::mdds::matrix_density_sparse_empty) {}
48 :
49 1325 : void set(size_t row, size_t col, const OUString& aStr)
50 : {
51 1325 : maMatrix.set_string(row, col, new OUString(aStr));
52 1325 : }
53 :
54 : #if CALC_DEBUG_OUTPUT
55 : void print(const char* header) const
56 : {
57 : if (header)
58 : cout << header << endl;
59 :
60 : MatrixType::size_pair_type ns = maMatrix.size();
61 : vector<sal_Int32> aColWidths(ns.second, 0);
62 :
63 : // Calculate column widths first.
64 : for (size_t row = 0; row < ns.first; ++row)
65 : {
66 : for (size_t col = 0; col < ns.second; ++col)
67 : {
68 : const OUString* p = maMatrix.get_string(row, col);
69 : if (aColWidths[col] < p->getLength())
70 : aColWidths[col] = p->getLength();
71 : }
72 : }
73 :
74 : // Make the row separator string.
75 : OUStringBuffer aBuf;
76 : aBuf.appendAscii("+");
77 : for (size_t col = 0; col < ns.second; ++col)
78 : {
79 : aBuf.appendAscii("-");
80 : for (sal_Int32 i = 0; i < aColWidths[col]; ++i)
81 : aBuf.append(sal_Unicode('-'));
82 : aBuf.appendAscii("-+");
83 : }
84 :
85 : OUString aSep = aBuf.makeStringAndClear();
86 :
87 : // Now print to stdout.
88 : cout << aSep << endl;
89 : for (size_t row = 0; row < ns.first; ++row)
90 : {
91 : cout << "| ";
92 : for (size_t col = 0; col < ns.second; ++col)
93 : {
94 : const OUString* p = maMatrix.get_string(row, col);
95 : size_t nPadding = aColWidths[col] - p->getLength();
96 : aBuf.append(*p);
97 : for (size_t i = 0; i < nPadding; ++i)
98 : aBuf.append(sal_Unicode(' '));
99 : cout << aBuf.makeStringAndClear() << " | ";
100 : }
101 : cout << endl;
102 : cout << aSep << endl;
103 : }
104 : }
105 : #else
106 61 : void print(const char*) const {}
107 : #endif
108 :
109 : /**
110 : * Print nested string array which can be copy-n-pasted into the test code
111 : * for content verification.
112 : */
113 : void printArray() const
114 : {
115 : #if CALC_DEBUG_OUTPUT
116 : MatrixType::size_pair_type ns = maMatrix.size();
117 : for (size_t row = 0; row < ns.first; ++row)
118 : {
119 : cout << " { ";
120 : for (size_t col = 0; col < ns.second; ++col)
121 : {
122 : const OUString* p = maMatrix.get_string(row, col);
123 : if (p->getLength())
124 : cout << "\"" << *p << "\"";
125 : else
126 : cout << "0";
127 : if (col < ns.second - 1)
128 : cout << ", ";
129 : }
130 : cout << " }";
131 : if (row < ns.first - 1)
132 : cout << ",";
133 : cout << endl;
134 : }
135 : #endif
136 : }
137 :
138 : void clear() { maMatrix.clear(); }
139 : void resize(size_t rows, size_t cols) { maMatrix.resize(rows, cols); }
140 :
141 : private:
142 : MatrixType maMatrix;
143 : };
144 :
145 : #endif
146 :
147 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|