Alexandria  2.22.0
Please provide a description of the project.
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 
32 namespace Euclid {
33 namespace Table {
34 
35 AsciiWriter::AsciiWriter(std::ostream& stream) : AsciiWriter(InstOrRefHolder<std::ostream>::create(stream)) {}
36 
37 AsciiWriter::AsciiWriter(const std::string& filename) : AsciiWriter(create<std::ofstream>(filename)) {}
38 
40  : m_stream_holder(std::move(stream_holder)) {}
41 
43  if (m_writing_started) {
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 
55  if (m_writing_started) {
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 
63 void AsciiWriter::addComment(const std::string& message) {
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 
78 void AsciiWriter::init(const Table& table) {
79  m_initialized = true;
80  // If we have already written anything we leave an empty line
81  if (m_writing_started) {
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();
90  if (m_show_column_info) {
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 
115 void 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
Definition: AsciiWriter.h:180
void append(const Table &table) override
void init(const Table &table) override
Definition: AsciiWriter.cpp:78
AsciiWriter & showColumnInfo(bool show)
Sets if the column information will be written to the stream.
Definition: AsciiWriter.cpp:54
void addComment(const std::string &message) override
Adds a comment to the stream.
Definition: AsciiWriter.cpp:63
AsciiWriter(std::ostream &stream)
Constructs an AsciiWriter which writes to the given stream.
Definition: AsciiWriter.cpp:35
AsciiWriter & setCommentIndicator(const std::string &indicator)
Set the comment indicator.
Definition: AsciiWriter.cpp:42
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)