Alexandria  2.16
Please provide a description of the project.
AsciiWriterHelper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 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 <algorithm>
26 #include <boost/lexical_cast.hpp>
27 #include <boost/regex.hpp>
28 #include <boost/io/detail/quoted_manip.hpp>
30 #include "AsciiWriterHelper.h"
31 
32 namespace Euclid {
33 namespace Table {
34 
35 using NdArray::NdArray;
36 using boost::regex;
37 using boost::regex_match;
38 
40  if (type == typeid(bool)) {
41  return "bool";
42  } if (type == typeid(int32_t)) {
43  return "int";
44  } if (type == typeid(int64_t)) {
45  return "long";
46  } if (type == typeid(float)) {
47  return "float";
48  } if (type == typeid(double)) {
49  return "double";
50  } if (type == typeid(std::string)) {
51  return "string";
52  } if (type == typeid(std::vector<bool>)) {
53  return "[bool]";
54  } if (type == typeid(std::vector<int32_t>)) {
55  return "[int]";
56  } if (type == typeid(std::vector<int64_t>)) {
57  return "[long]";
58  } if (type == typeid(std::vector<float>)) {
59  return "[float]";
60  } if (type == typeid(std::vector<double>)) {
61  return "[double]";
62  } if (type == typeid(NdArray<bool>)) {
63  return "[bool+]";
64  } if (type == typeid(NdArray<int32_t>)) {
65  return "[int+]";
66  } if (type == typeid(NdArray<int64_t>)) {
67  return "[long+]";
68  } if (type == typeid(NdArray<float>)) {
69  return "[float+]";
70  } if (type == typeid(NdArray<double>)) {
71  return "[double+]";
72  }
73  throw Elements::Exception() << "Conversion to string for type " << type.name()
74  << " is not supported";
75 }
76 
78  std::vector<size_t> sizes {};
79  // We initialize the values to the required size for the column name
80  auto column_info = table.getColumnInfo();
81  for (size_t i=0; i<column_info->size(); ++i) {
82  sizes.push_back(quoted(column_info->getDescription(i).name).size());
83  }
84  for (auto row : table) {
85  for (size_t i=0; i<sizes.size(); ++i) {
86  sizes[i] = std::max(sizes[i], boost::apply_visitor(ToStringVisitor{}, row[i]).size());
87  }
88  }
89  for (auto& s : sizes) {
90  s += 1;
91  }
92  return sizes;
93 }
94 
96  regex whitespace_quotes{".*[\\s\"].*"};
97  if (!regex_match(str, whitespace_quotes))
98  return str;
100  q << boost::io::quoted(str);
101  return q.str();
102 }
103 
104 }
105 } // end of namespace Euclid
constexpr double s
STL class.
NdArray(const std::vector< size_t > &shape)
Definition: NdArray.h:62
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.