diff options
author | Kohei Yoshida <kohei.yoshida@collabora.com> | 2014-06-26 13:33:09 -0400 |
---|---|---|
committer | Kohei Yoshida <kohei.yoshida@collabora.com> | 2014-06-26 13:35:33 -0400 |
commit | f15e440d148ed021091ef9b20f3aed2488f0fde3 (patch) | |
tree | 99a1f7c54ffd0c63588d2599075d2be7eebc79da | |
parent | b7de76d825b02005ab8cf02b3977e4be441200c5 (diff) |
Move SheetPrinter to svl and rename it to GridPrinter.
I need to use this outside of sc.
Change-Id: I153863d6c5c31e5ab5f25da2dba81bd4d4b6d3fe
-rw-r--r-- | include/svl/gridprinter.hxx | 44 | ||||
-rw-r--r-- | sc/qa/unit/helper/debughelper.hxx | 113 | ||||
-rw-r--r-- | sc/qa/unit/helper/qahelper.hxx | 2 | ||||
-rw-r--r-- | sc/qa/unit/ucalc.cxx | 2 | ||||
-rw-r--r-- | svl/Library_svl.mk | 1 | ||||
-rw-r--r-- | svl/source/misc/gridprinter.cxx | 141 |
6 files changed, 191 insertions, 112 deletions
diff --git a/include/svl/gridprinter.hxx b/include/svl/gridprinter.hxx new file mode 100644 index 000000000000..d63adfcb2c0b --- /dev/null +++ b/include/svl/gridprinter.hxx @@ -0,0 +1,44 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_SVL_GRIDPRINTER_HXX +#define INCLUDED_SVL_GRIDPRINTER_HXX + +#include <rtl/ustring.hxx> +#include <svl/svldllapi.h> + +namespace svl { + +/** + * Print 2-dimensional data in a nice and pleasant fashion. Useful when + * debugging grid layout data. + */ +class SVL_DLLPUBLIC GridPrinter +{ + struct Impl; + Impl* mpImpl; + +public: + GridPrinter( size_t nRows, size_t nCols, bool bPrint = true ); + ~GridPrinter(); + + void set( size_t nRow, size_t nCol, const OUString& rStr ); + + void print( const char* pHeader ) const; + + void clear(); + + void resize( size_t nRows, size_t nCols ); +}; + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/qa/unit/helper/debughelper.hxx b/sc/qa/unit/helper/debughelper.hxx index dc6e7bd3d190..509f4aff72fc 100644 --- a/sc/qa/unit/helper/debughelper.hxx +++ b/sc/qa/unit/helper/debughelper.hxx @@ -23,10 +23,10 @@ #undef NOMINMAX #endif -#define MDDS_HASH_CONTAINER_BOOST 1 -#include <mdds/mixed_type_matrix.hpp> - #include <iostream> +#include <vector> + +#include <svl/gridprinter.hxx> #define CALC_DEBUG_OUTPUT 0 #define CALC_TEST_PERF 0 @@ -37,113 +37,6 @@ using ::std::cerr; using ::std::endl; using ::std::vector; -/** - * Print nicely formatted sheet content to stdout. Indispensable when - * debugging the unit test code involving testing of sheet contents. - */ -class SheetPrinter -{ - typedef ::mdds::mixed_type_matrix<OUString, bool> MatrixType; -public: - SheetPrinter(size_t rows, size_t cols) : - maMatrix(rows, cols, ::mdds::matrix_density_sparse_empty) {} - - void set(size_t row, size_t col, const OUString& aStr) - { - maMatrix.set_string(row, col, new OUString(aStr)); - } - -#if CALC_DEBUG_OUTPUT - void print(const char* header) const - { - if (header) - cout << header << endl; - - MatrixType::size_pair_type ns = maMatrix.size(); - vector<sal_Int32> aColWidths(ns.second, 0); - - // Calculate column widths first. - for (size_t row = 0; row < ns.first; ++row) - { - for (size_t col = 0; col < ns.second; ++col) - { - const OUString* p = maMatrix.get_string(row, col); - if (aColWidths[col] < p->getLength()) - aColWidths[col] = p->getLength(); - } - } - - // Make the row separator string. - OUStringBuffer aBuf; - aBuf.appendAscii("+"); - for (size_t col = 0; col < ns.second; ++col) - { - aBuf.appendAscii("-"); - for (sal_Int32 i = 0; i < aColWidths[col]; ++i) - aBuf.append(sal_Unicode('-')); - aBuf.appendAscii("-+"); - } - - OUString aSep = aBuf.makeStringAndClear(); - - // Now print to stdout. - cout << aSep << endl; - for (size_t row = 0; row < ns.first; ++row) - { - cout << "| "; - for (size_t col = 0; col < ns.second; ++col) - { - const OUString* p = maMatrix.get_string(row, col); - size_t nPadding = aColWidths[col] - p->getLength(); - aBuf.append(*p); - for (size_t i = 0; i < nPadding; ++i) - aBuf.append(sal_Unicode(' ')); - cout << aBuf.makeStringAndClear() << " | "; - } - cout << endl; - cout << aSep << endl; - } - } -#else - void print(const char*) const {} -#endif - - /** - * Print nested string array which can be copy-n-pasted into the test code - * for content verification. - */ - void printArray() const - { -#if CALC_DEBUG_OUTPUT - MatrixType::size_pair_type ns = maMatrix.size(); - for (size_t row = 0; row < ns.first; ++row) - { - cout << " { "; - for (size_t col = 0; col < ns.second; ++col) - { - const OUString* p = maMatrix.get_string(row, col); - if (p->getLength()) - cout << "\"" << *p << "\""; - else - cout << "0"; - if (col < ns.second - 1) - cout << ", "; - } - cout << " }"; - if (row < ns.first - 1) - cout << ","; - cout << endl; - } -#endif - } - - void clear() { maMatrix.clear(); } - void resize(size_t rows, size_t cols) { maMatrix.resize(rows, cols); } - -private: - MatrixType maMatrix; -}; - #endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/qa/unit/helper/qahelper.hxx b/sc/qa/unit/helper/qahelper.hxx index a4273bc8a4c4..8c96e360a731 100644 --- a/sc/qa/unit/helper/qahelper.hxx +++ b/sc/qa/unit/helper/qahelper.hxx @@ -136,7 +136,7 @@ bool checkOutput(ScDocument* pDoc, const ScRange& aOutRange, const char* aOutput bool bResult = true; const ScAddress& s = aOutRange.aStart; const ScAddress& e = aOutRange.aEnd; - SheetPrinter printer(e.Row() - s.Row() + 1, e.Col() - s.Col() + 1); + svl::GridPrinter printer(e.Row() - s.Row() + 1, e.Col() - s.Col() + 1, CALC_DEBUG_OUTPUT != 0); SCROW nOutRowSize = e.Row() - s.Row() + 1; SCCOL nOutColSize = e.Col() - s.Col() + 1; for (SCROW nRow = 0; nRow < nOutRowSize; ++nRow) diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx index 8d2dd47b91be..d412abdd4aa4 100644 --- a/sc/qa/unit/ucalc.cxx +++ b/sc/qa/unit/ucalc.cxx @@ -6410,7 +6410,7 @@ void Test::printRange(ScDocument* pDoc, const ScRange& rRange, const char* pCapt { SCROW nRow1 = rRange.aStart.Row(), nRow2 = rRange.aEnd.Row(); SCCOL nCol1 = rRange.aStart.Col(), nCol2 = rRange.aEnd.Col(); - SheetPrinter printer(nRow2 - nRow1 + 1, nCol2 - nCol1 + 1); + svl::GridPrinter printer(nRow2 - nRow1 + 1, nCol2 - nCol1 + 1, CALC_DEBUG_OUTPUT != 0); for (SCROW nRow = nRow1; nRow <= nRow2; ++nRow) { for (SCCOL nCol = nCol1; nCol <= nCol2; ++nCol) diff --git a/svl/Library_svl.mk b/svl/Library_svl.mk index 2b337ff88f1f..d00505eb110a 100644 --- a/svl/Library_svl.mk +++ b/svl/Library_svl.mk @@ -107,6 +107,7 @@ $(eval $(call gb_Library_add_exception_objects,svl,\ svl/source/misc/filenotation \ svl/source/misc/fstathelper \ svl/source/misc/getstringresource \ + svl/source/misc/gridprinter \ svl/source/misc/inethist \ svl/source/misc/inettype \ svl/source/misc/lngmisc \ diff --git a/svl/source/misc/gridprinter.cxx b/svl/source/misc/gridprinter.cxx new file mode 100644 index 000000000000..c36de35fcf44 --- /dev/null +++ b/svl/source/misc/gridprinter.cxx @@ -0,0 +1,141 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <svl/gridprinter.hxx> +#include <rtl/ustrbuf.hxx> + +#include <mdds/multi_type_vector_types.hpp> +#include <mdds/multi_type_vector_trait.hpp> +#include <mdds/multi_type_vector_custom_func1.hpp> +#include <mdds/multi_type_matrix.hpp> + +#include <iostream> + +using namespace std; + +namespace svl { + +// String ID +const mdds::mtv::element_t element_type_string = mdds::mtv::element_type_user_start; +// String block +typedef mdds::mtv::default_element_block<element_type_string, OUString> string_block; + +struct custom_string_trait +{ + typedef OUString string_type; + typedef string_block string_element_block; + + static const mdds::mtv::element_t string_type_identifier = element_type_string; + + typedef mdds::mtv::custom_block_func1<string_block> element_block_func; +}; + +} + +namespace rtl { + +// Callbacks for the string block. This needs to be in the same namespace as +// OUString for argument dependent lookup. +MDDS_MTV_DEFINE_ELEMENT_CALLBACKS(OUString, svl::element_type_string, OUString(), svl::string_block) + +} + +namespace svl { + +typedef mdds::multi_type_matrix<custom_string_trait> MatrixImplType; + +struct GridPrinter::Impl +{ + MatrixImplType maMatrix; + bool mbPrint; + + Impl( size_t nRows, size_t nCols, bool bPrint ) : + maMatrix(nRows, nCols), mbPrint(bPrint) {} +}; + +GridPrinter::GridPrinter( size_t nRows, size_t nCols, bool bPrint ) : + mpImpl(new Impl(nRows, nCols, bPrint)) {} + +GridPrinter::~GridPrinter() +{ + delete mpImpl; +} + +void GridPrinter::set( size_t nRow, size_t nCol, const OUString& rStr ) +{ + mpImpl->maMatrix.set(nRow, nCol, rStr); +} + +void GridPrinter::print( const char* pHeader ) const +{ + if (!mpImpl->mbPrint) + return; + + if (pHeader) + cout << pHeader << endl; + + MatrixImplType::size_pair_type ns = mpImpl->maMatrix.size(); + vector<sal_Int32> aColWidths(ns.column, 0); + + // Calculate column widths first. + for (size_t row = 0; row < ns.row; ++row) + { + for (size_t col = 0; col < ns.column; ++col) + { + OUString aStr = mpImpl->maMatrix.get_string(row, col); + if (aColWidths[col] < aStr.getLength()) + aColWidths[col] = aStr.getLength(); + } + } + + // Make the row separator string. + OUStringBuffer aBuf; + aBuf.appendAscii("+"); + for (size_t col = 0; col < ns.column; ++col) + { + aBuf.appendAscii("-"); + for (sal_Int32 i = 0; i < aColWidths[col]; ++i) + aBuf.append(sal_Unicode('-')); + aBuf.appendAscii("-+"); + } + + OUString aSep = aBuf.makeStringAndClear(); + + // Now print to stdout. + cout << aSep << endl; + for (size_t row = 0; row < ns.row; ++row) + { + cout << "| "; + for (size_t col = 0; col < ns.column; ++col) + { + OUString aStr = mpImpl->maMatrix.get_string(row, col); + size_t nPadding = aColWidths[col] - aStr.getLength(); + aBuf.append(aStr); + for (size_t i = 0; i < nPadding; ++i) + aBuf.append(sal_Unicode(' ')); + cout << aBuf.makeStringAndClear() << " | "; + } + cout << endl; + cout << aSep << endl; + } +} + +void GridPrinter::clear() +{ + mpImpl->maMatrix.clear(); +} + +void GridPrinter::resize( size_t nRows, size_t nCols ) +{ + mpImpl->maMatrix.resize(nRows, nCols); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |