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