Kaydet (Commit) f15e440d authored tarafından Kohei Yoshida's avatar Kohei Yoshida

Move SheetPrinter to svl and rename it to GridPrinter.

I need to use this outside of sc.

Change-Id: I153863d6c5c31e5ab5f25da2dba81bd4d4b6d3fe
üst b7de76d8
/* -*- 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: */
......@@ -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: */
......@@ -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)
......
......@@ -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)
......
......@@ -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 \
......
/* -*- 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: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment