1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
//
// RowFormatter.cpp
//
// Library: Data
// Package: DataCore
// Module: RowFormatter
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Data/SimpleRowFormatter.h"
#include "Poco/Exception.h"
#include <iomanip>
namespace Poco {
namespace Data {
SimpleRowFormatter::SimpleRowFormatter(std::streamsize columnWidth, std::streamsize spacing):
_colWidth(columnWidth), _spacing(spacing), _rowCount(0)
{
}
SimpleRowFormatter::SimpleRowFormatter(const SimpleRowFormatter& other):
RowFormatter(other.prefix(), other.postfix()),
_colWidth(other._colWidth),
_spacing(other._spacing),
_rowCount(0)
{
}
SimpleRowFormatter::~SimpleRowFormatter()
{
}
SimpleRowFormatter& SimpleRowFormatter::operator = (const SimpleRowFormatter& row)
{
SimpleRowFormatter tmp(row);
swap(tmp);
return *this;
}
void SimpleRowFormatter::swap(SimpleRowFormatter& other)
{
using std::swap;
setPrefix(other.prefix());
setPostfix(other.postfix());
swap(_colWidth, other._colWidth);
swap(_spacing, other._spacing);
}
std::string& SimpleRowFormatter::formatNames(const NameVecPtr pNames, std::string& formattedNames)
{
_rowCount = 0;
std::ostringstream str;
std::string line(std::string::size_type(pNames->size()*_colWidth + (pNames->size() - 1)*_spacing), '-');
std::string space(_spacing, ' ');
NameVec::const_iterator it = pNames->begin();
NameVec::const_iterator end = pNames->end();
for (; it != end; ++it)
{
if (it != pNames->begin()) str << space;
str << std::left << std::setw(_colWidth) << *it;
}
str << std::endl << line << std::endl;
return formattedNames = str.str();
}
std::string& SimpleRowFormatter::formatValues(const ValueVec& vals, std::string& formattedValues)
{
std::ostringstream str;
std::string space(_spacing, ' ');
ValueVec::const_iterator it = vals.begin();
ValueVec::const_iterator end = vals.end();
for (; it != end; ++it)
{
if (it != vals.begin()) str << space;
if (it->isNumeric())
{
str << std::right
<< std::fixed
<< std::setprecision(2);
}
else str << std::left;
if (!it->isEmpty())
str << std::setw(_colWidth) << it->convert<std::string>();
else
str << std::setw(_colWidth) << "null";
}
str << std::endl;
++_rowCount;
return formattedValues = str.str();
}
} } // namespace Poco::Data
|