Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
AsciiWriterHelper.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012-2022 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 "AsciiWriterHelper.h"
28#include <algorithm>
29#include <boost/lexical_cast.hpp>
30
31#if BOOST_VERSION < 107300
32#include <boost/io/detail/quoted_manip.hpp>
33#else
34#include <boost/io/quoted.hpp>
35#endif
36
37namespace Euclid {
38namespace Table {
39
40using NdArray::NdArray;
42
44 auto i = std::find_if(KeywordTypeMap.begin(), KeywordTypeMap.end(),
45 [type](const std::pair<std::string, std::type_index>& p) { return p.second == type; });
46 if (i != KeywordTypeMap.end()) {
47 return i->first;
48 }
49 throw Elements::Exception() << "Conversion to string for type " << type.name() << " is not supported";
50}
51
53 std::vector<size_t> sizes{};
54 // We initialize the values to the required size for the column name
55 auto column_info = table.getColumnInfo();
56 for (size_t i = 0; i < column_info->size(); ++i) {
57 sizes.push_back(quoted(column_info->getDescription(i).name).size());
58 }
59 for (const auto& row : table) {
60 for (size_t i = 0; i < sizes.size(); ++i) {
61 sizes[i] = std::max(sizes[i], boost::apply_visitor(ToStringVisitor{}, row[i]).size());
62 }
63 }
64 std::for_each(sizes.begin(), sizes.end(), [](size_t& s) { ++s; });
65 return sizes;
66}
67
69 regex::regex whitespace_quotes{".*[\\s\"].*"};
70 if (!regex_match(str, whitespace_quotes))
71 return str;
73 q << boost::io::quoted(str);
74 return q.str();
75}
76
77} // namespace Table
78} // end of namespace Euclid
Represents a table.
Definition Table.h:49
T find_if(T... args)
T for_each(T... args)
T max(T... args)
T name(T... args)
std::vector< size_t > calculateColumnLengths(const Table &table)
Calculates the sizes in characters each column of the table needs.
const std::vector< std::pair< std::string, std::type_index > > KeywordTypeMap
std::string quoted(const std::string &str)
std::string typeToKeyword(std::type_index type)
Converts a type to its string representation.
T size(T... args)
T str(T... args)