Alexandria  2.19
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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"
27 #include <algorithm>
28 #include <boost/lexical_cast.hpp>
29 #include <boost/regex.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 boost::regex;
41 using boost::regex_match;
42 using NdArray::NdArray;
43 
45  if (type == typeid(bool)) {
46  return "bool";
47  }
48  if (type == typeid(int32_t)) {
49  return "int";
50  }
51  if (type == typeid(int64_t)) {
52  return "long";
53  }
54  if (type == typeid(float)) {
55  return "float";
56  }
57  if (type == typeid(double)) {
58  return "double";
59  }
60  if (type == typeid(std::string)) {
61  return "string";
62  }
63  if (type == typeid(std::vector<bool>)) {
64  return "[bool]";
65  }
66  if (type == typeid(std::vector<int32_t>)) {
67  return "[int]";
68  }
69  if (type == typeid(std::vector<int64_t>)) {
70  return "[long]";
71  }
72  if (type == typeid(std::vector<float>)) {
73  return "[float]";
74  }
75  if (type == typeid(std::vector<double>)) {
76  return "[double]";
77  }
78  if (type == typeid(NdArray<int32_t>)) {
79  return "[int+]";
80  }
81  if (type == typeid(NdArray<int64_t>)) {
82  return "[long+]";
83  }
84  if (type == typeid(NdArray<float>)) {
85  return "[float+]";
86  }
87  if (type == typeid(NdArray<double>)) {
88  return "[double+]";
89  }
90  throw Elements::Exception() << "Conversion to string for type " << type.name() << " is not supported";
91 }
92 
94  std::vector<size_t> sizes{};
95  // We initialize the values to the required size for the column name
96  auto column_info = table.getColumnInfo();
97  for (size_t i = 0; i < column_info->size(); ++i) {
98  sizes.push_back(quoted(column_info->getDescription(i).name).size());
99  }
100  for (auto row : table) {
101  for (size_t i = 0; i < sizes.size(); ++i) {
102  sizes[i] = std::max(sizes[i], boost::apply_visitor(ToStringVisitor{}, row[i]).size());
103  }
104  }
105  for (auto& s : sizes) {
106  s += 1;
107  }
108  return sizes;
109 }
110 
112  regex whitespace_quotes{".*[\\s\"].*"};
113  if (!regex_match(str, whitespace_quotes))
114  return str;
116  q << boost::io::quoted(str);
117  return q.str();
118 }
119 
120 } // namespace Table
121 } // end of namespace Euclid
constexpr double s
STL class.
T str(T...args)
T max(T...args)
std::string typeToKeyword(std::type_index type)
Converts a type to its string representation.
Represents a table.
Definition: Table.h:49
std::string quoted(const std::string &str)
STL class.
T name(T...args)
std::vector< size_t > calculateColumnLengths(const Table &table)
Calculates the sizes in characters each column of the table needs.