Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
AsciiWriter.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012-2021 Euclid Science Ground Segment
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 3.0 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
25#include "Table/AsciiWriter.h"
26#include "AsciiWriterHelper.h"
28#include <boost/lexical_cast.hpp>
29#include <fstream>
30#include <sstream>
31
32namespace Euclid {
33namespace Table {
34
36
37AsciiWriter::AsciiWriter(const std::string& filename) : AsciiWriter(create<std::ofstream>(filename)) {}
38
40 : m_stream_holder(std::move(stream_holder)) {}
41
44 throw Elements::Exception() << "Changing comment indicator after writing "
45 << "has started is not allowed";
46 }
47 if (indicator.empty()) {
48 throw Elements::Exception() << "Empty string as comment indicator";
49 }
50 m_comment = indicator;
51 return *this;
52}
53
56 throw Elements::Exception() << "Changing column info visibility after writing "
57 << "has started is not allowed";
58 }
59 m_show_column_info = show;
60 return *this;
61}
62
64 if (m_initialized) {
65 throw Elements::Exception() << "Adding comments after writing data in ASCII "
66 << "format is not allowed";
67 }
68 m_writing_started = true;
69
70 std::stringstream message_stream{message};
71 while (!message_stream.eof()) {
72 std::string line;
73 std::getline(message_stream, line);
74 m_stream_holder->ref() << m_comment << ' ' << line << '\n';
75 }
76}
77
78void AsciiWriter::init(const Table& table) {
79 m_initialized = true;
80 // If we have already written anything we leave an empty line
82 m_stream_holder->ref() << '\n';
83 }
84 m_writing_started = true;
85
86 auto& out = m_stream_holder->ref();
87
88 // Write the column descriptions
89 auto& info = *table.getColumnInfo();
91 for (size_t i = 0; i < info.size(); ++i) {
92 auto& desc = info.getDescription(i);
93
94 out << m_comment << " Column: " << quoted(desc.name) << ' ' << typeToKeyword(desc.type);
95 if (!desc.unit.empty()) {
96 out << " (" << desc.unit << ")";
97 }
98 if (!desc.description.empty()) {
99 out << " - " << desc.description;
100 }
101 out << '\n';
102 }
103 out << '\n';
104 }
105
106 // Write the column names
107 auto column_lengths = calculateColumnLengths(table);
108 out << m_comment.c_str();
109 for (size_t i = 0; i < info.size(); ++i) {
110 out << std::setw(column_lengths[i]) << quoted(info.getDescription(i).name);
111 }
112 out << "\n\n";
113}
114
115void AsciiWriter::append(const Table& table) {
116 auto& out = m_stream_holder->ref();
117 auto column_lengths = calculateColumnLengths(table);
118 // The data lines are not prefixed with the comment string, so we need to fix
119 // the length of the first column to get the alignment correctly
120 column_lengths[0] = column_lengths[0] + m_comment.size();
121 for (auto row : table) {
122 for (size_t i = 0; i < row.size(); ++i) {
123 out << std::setw(column_lengths[i]) << boost::apply_visitor(ToStringVisitor{}, row[i]);
124 }
125 out << "\n";
126 }
127}
128
129} // namespace Table
130} // namespace Euclid
T c_str(T... args)
TableWriter implementation for writing ASCII tables to streams.
Definition AsciiWriter.h:80
std::unique_ptr< InstOrRefHolder< std::ostream > > m_stream_holder
void append(const Table &table) override
void init(const Table &table) override
AsciiWriter & showColumnInfo(bool show)
Sets if the column information will be written to the stream.
void addComment(const std::string &message) override
Adds a comment to the stream.
AsciiWriter(std::ostream &stream)
Constructs an AsciiWriter which writes to the given stream.
AsciiWriter & setCommentIndicator(const std::string &indicator)
Set the comment indicator.
Represents a table.
Definition Table.h:49
T empty(T... args)
T getline(T... args)
std::vector< size_t > calculateColumnLengths(const Table &table)
Calculates the sizes in characters each column of the table needs.
std::string quoted(const std::string &str)
std::string typeToKeyword(std::type_index type)
Converts a type to its string representation.
STL namespace.
T setw(T... args)
T size(T... args)