Euphoria
table_string.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 
5 
6 #include <iostream>
7 #include <functional>
8 
9 #include "core/table.h"
10 #include "core/textfileparser.h"
11 #include "core/multisort.h"
12 
13 
14 namespace eu::core
15 {
17 
18  template <typename T>
19  struct TableGenerator : public SortBuilder<T, TableGenerator<T>>
20  {
21  using ToStringFunction = std::function<std::string(const T&)>;
22 
23  const std::vector<T>& data;
24  std::vector<ToStringFunction> column_to_string;
25  std::vector<std::string> column_titles;
26 
27  explicit TableGenerator(const std::vector<T>& d)
28  : data(d)
29  {
30  }
31 
33  add_column(const std::string& title, ToStringFunction to_string)
34  {
35  column_titles.emplace_back(title);
36  column_to_string.emplace_back(to_string);
37  return *this;
38  }
39 
40  [[nodiscard]] StringTable
41  to_table() const
42  {
43  StringTable ret;
45 
46  const auto column_count = column_titles.size();
47 
48  const auto indices = get_sorted_indices(data, *this);
49  for(const auto index: indices)
50  {
51  const auto& d = data[index];
52  std::vector<std::string> row_strings;
53  row_strings.reserve(column_count);
54  for(size_t column_index = 0; column_index < column_count; ++column_index)
55  {
56  row_strings.emplace_back(column_to_string[column_index](d));
57  }
58  ret.add_row(row_strings);
59  }
60 
61  return ret;
62  }
63  };
64 
65  enum class CsvTrim
66  {
67  trim,
68  dont_trim
69  };
70 
72  {
73  char delim = ',';
74  char str = '\"';
76  };
77 
79  (
80  const std::string& data,
81  const CsvParserOptions& options = CsvParserOptions()
82  );
83 
84 
85  /*
86  First name Last name
87  ------------ -----------
88  Jerry Seinfeld
89  Elaine Benes
90  Cosmo Kramer
91  George Costanza
92  */
93  void print_table_simple(std::ostream& out, const StringTable& table);
94 
95  /*
96  +------------+-----------+
97  | First name | Last name |
98  +------------+-----------+
99  | Jerry | Seinfeld |
100  | Elaine | Benes |
101  | Cosmo | Kramer |
102  | George | Costanza |
103  +------------+-----------+
104  */
105  void print_table_grid(std::ostream& out, const StringTable& table);
106 
107 }
void print_table_grid(std::ostream &out, const StringTable &maintable)
std::string to_string(const EnumValue &v)
Definition: enum.cc:175
void print_table_simple(std::ostream &out, const StringTable &maintable)
StringTable parse_csv(const std::string &csv_data, const CsvParserOptions &options)
Definition: table_string.cc:15
std::vector< size_t > get_sorted_indices(const std::vector< T > &data, const SortBuilder< T, TSelf > &builder)
Definition: multisort.h:114
std::function< std::string(const T &)> ToStringFunction
Definition: table_string.h:21
TableGenerator(const std::vector< T > &d)
Definition: table_string.h:27
StringTable to_table() const
Definition: table_string.h:41
std::vector< ToStringFunction > column_to_string
Definition: table_string.h:24
std::vector< std::string > column_titles
Definition: table_string.h:25
const std::vector< T > & data
Definition: table_string.h:23
TableGenerator< T > & add_column(const std::string &title, ToStringFunction to_string)
Definition: table_string.h:33
void add_row(T d=T())
Definition: table.h:33