Alexandria  2.22.0
Please provide a description of the project.
AsciiWriterHelper.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 "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 
37 namespace Euclid {
38 namespace Table {
39 
40 using NdArray::NdArray;
42 
44  for (auto p = KeywordTypeMap.begin(); p != KeywordTypeMap.end(); ++p) {
45  if (p->second == type) {
46  return p->first;
47  }
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 (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  for (auto& s : sizes) {
65  s += 1;
66  }
67  return sizes;
68 }
69 
71  regex::regex whitespace_quotes{".*[\\s\"].*"};
72  if (!regex_match(str, whitespace_quotes))
73  return str;
75  q << boost::io::quoted(str);
76  return q.str();
77 }
78 
79 } // namespace Table
80 } // end of namespace Euclid
NdArray(const std::vector< size_t > &shape_)
Represents a table.
Definition: Table.h:49
T max(T... args)
T name(T... args)
constexpr double s
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)