ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
csv_reader.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2020 German Aerospace Center (DLR).
3  * Eclipse ADORe, Automated Driving Open Research https://eclipse.org/adore
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License 2.0 which is available at
7  * http://www.eclipse.org/legal/epl-2.0.
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * Thomas Lobig - initial API and implementation
13  ********************************************************************************/
14 
15 #pragma once
16 
17 // #include <istream>
18 #include <fstream>
19 #include <locale>
20 #include <vector>
21 
22 namespace adore
23 {
24  namespace mad
25  {
26  namespace CsvReader
27  {
32  struct csv_reader_facet: std::ctype<char> {
33 
34  csv_reader_facet(): std::ctype<char>(get_table()) {}
35 
36  static std::ctype_base::mask const* get_table() {
37  static std::vector<std::ctype_base::mask>
38  rc(table_size, std::ctype_base::mask());
39 
40  rc['\n'] = std::ctype_base::space;
41  rc['\r'] = std::ctype_base::space;
42  rc[','] = std::ctype_base::space;
43  rc[' '] = std::ctype_base::space;
44  rc[';'] = std::ctype_base::space;
45  rc['\t'] = std::ctype_base::space;
46  return &rc[0];
47  }
48  };
49 
56  template<typename T>
57  static std::vector<T> get_data(std::string filepath)
58  {
59  std::vector<T> result;
60  try
61  {
62  std::ifstream inputfile;
63  inputfile.open(filepath);
64  if(inputfile.is_open())
65  {
66  std::string firstline;
67  std::getline(inputfile,firstline);
68  inputfile.imbue(std::locale(std::locale(),new csv_reader_facet()));
69  int count = 0;
70  T value;
71  while(inputfile >> value)
72  {
73  result.push_back(value);
74  }
75  }
76  else
77  {
78  std::cerr << "ERROR opening csv file " << filepath << '\n';
79  }
80 
81  }
82  catch(const std::exception& e)
83  {
84  std::cerr << "ERROR processing csv file " << filepath << '\n' << e.what() << '\n';
85  }
86  return result;
87  }
88 
89  };
90  }
91 }
static std::vector< T > get_data(std::string filepath)
A simple csv reader function which just gets all data T from a file as a vector of T,...
Definition: csv_reader.h:57
Definition: areaofeffectconverter.h:20
a helper construct to facilitate reading csv files
Definition: csv_reader.h:32
csv_reader_facet()
Definition: csv_reader.h:34
static std::ctype_base::mask const * get_table()
Definition: csv_reader.h:36