Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * [ Copyright (C) 2011 Markus Mohrhard <markus.mohrhard@googlemail.com> (initial developer) ]
17 : *
18 : * All Rights Reserved.
19 : *
20 : * For minor contributions see the git repository.
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : * instead of those above.
27 : */
28 :
29 : #ifndef SC_DEBUG_HELPER_HXX
30 : #define SC_DEBUG_HELPER_HXX
31 :
32 : /**
33 : * Print nicely formatted sheet content to stdout. Indispensable when
34 : * debugging the unit test code involving testing of sheet contents.
35 : */
36 :
37 : #include <rtl/strbuf.hxx>
38 : #include <rtl/ustring.hxx>
39 : #include "document.hxx"
40 :
41 : #ifdef WNT
42 : #if !defined NOMINMAX
43 : #define NOMINMAX
44 : #endif
45 : #include <prewin.h>
46 : #include <postwin.h>
47 : #undef NOMINMAX
48 : #endif
49 :
50 : #define MDDS_HASH_CONTAINER_BOOST 1
51 : #include <mdds/mixed_type_matrix.hpp>
52 :
53 : #include <iostream>
54 :
55 : using namespace ::com::sun::star;
56 : using ::rtl::OUString;
57 : using ::rtl::OUStringBuffer;
58 : using ::std::cout;
59 : using ::std::cerr;
60 : using ::std::endl;
61 : using ::std::vector;
62 :
63 :
64 47 : class SheetPrinter
65 : {
66 : typedef ::mdds::mixed_type_matrix<OUString, bool> MatrixType;
67 : public:
68 47 : SheetPrinter(size_t rows, size_t cols) :
69 47 : maMatrix(rows, cols, ::mdds::matrix_density_sparse_empty) {}
70 :
71 1164 : void set(size_t row, size_t col, const OUString& aStr)
72 : {
73 1164 : maMatrix.set_string(row, col, new OUString(aStr));
74 1164 : }
75 :
76 : #if CALC_DEBUG_OUTPUT
77 : void print(const char* header) const
78 : {
79 : if (header)
80 : cout << header << endl;
81 :
82 : MatrixType::size_pair_type ns = maMatrix.size();
83 : vector<sal_Int32> aColWidths(ns.second, 0);
84 :
85 : // Calculate column widths first.
86 : for (size_t row = 0; row < ns.first; ++row)
87 : {
88 : for (size_t col = 0; col < ns.second; ++col)
89 : {
90 : const OUString* p = maMatrix.get_string(row, col);
91 : if (aColWidths[col] < p->getLength())
92 : aColWidths[col] = p->getLength();
93 : }
94 : }
95 :
96 : // Make the row separator string.
97 : OUStringBuffer aBuf;
98 : aBuf.appendAscii("+");
99 : for (size_t col = 0; col < ns.second; ++col)
100 : {
101 : aBuf.appendAscii("-");
102 : for (sal_Int32 i = 0; i < aColWidths[col]; ++i)
103 : aBuf.append(sal_Unicode('-'));
104 : aBuf.appendAscii("-+");
105 : }
106 :
107 : OUString aSep = aBuf.makeStringAndClear();
108 :
109 : // Now print to stdout.
110 : cout << aSep << endl;
111 : for (size_t row = 0; row < ns.first; ++row)
112 : {
113 : cout << "| ";
114 : for (size_t col = 0; col < ns.second; ++col)
115 : {
116 : const OUString* p = maMatrix.get_string(row, col);
117 : size_t nPadding = aColWidths[col] - p->getLength();
118 : aBuf.append(*p);
119 : for (size_t i = 0; i < nPadding; ++i)
120 : aBuf.append(sal_Unicode(' '));
121 : cout << aBuf.makeStringAndClear() << " | ";
122 : }
123 : cout << endl;
124 : cout << aSep << endl;
125 : }
126 : }
127 : #else
128 47 : void print(const char*) const {}
129 : #endif
130 :
131 : /**
132 : * Print nested string array which can be copy-n-pasted into the test code
133 : * for content verification.
134 : */
135 : void printArray() const
136 : {
137 : #if CALC_DEBUG_OUTPUT
138 : MatrixType::size_pair_type ns = maMatrix.size();
139 : for (size_t row = 0; row < ns.first; ++row)
140 : {
141 : cout << " { ";
142 : for (size_t col = 0; col < ns.second; ++col)
143 : {
144 : const OUString* p = maMatrix.get_string(row, col);
145 : if (p->getLength())
146 : cout << "\"" << *p << "\"";
147 : else
148 : cout << "0";
149 : if (col < ns.second - 1)
150 : cout << ", ";
151 : }
152 : cout << " }";
153 : if (row < ns.first - 1)
154 : cout << ",";
155 : cout << endl;
156 : }
157 : #endif
158 : }
159 :
160 : void clear() { maMatrix.clear(); }
161 : void resize(size_t rows, size_t cols) { maMatrix.resize(rows, cols); }
162 :
163 : private:
164 : MatrixType maMatrix;
165 : };
166 :
167 : #endif
168 :
169 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|