CsvReader.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5  * Karlsruhe Institute of Technology (KIT), all rights reserved.
6  *
7  * ArmarX is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * ArmarX is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include "CsvReader.h"
25 
27 
28 namespace armarx
29 {
30  CsvReader::CsvReader(const std::string& filename, bool hasHeader) : separator(",")
31  {
32  file.open(filename, std::ios_base::in);
33 
34  if (hasHeader)
35  {
36  std::string line;
37  readLine(line);
38  header = Split(line, separator);
39  }
40  }
41 
42  bool
43  CsvReader::read(std::vector<std::string>& row)
44  {
45  std::string line;
46  if (!readLine(line))
47  {
48  return false;
49  }
50  row = Split(line, separator);
51  return true;
52  }
53 
54  bool
55  CsvReader::read(std::vector<float>& row)
56  {
57  row.clear();
58  std::string line;
59  if (!readLine(line))
60  {
61  return false;
62  }
63  std::vector<std::string> rowStr = Split(line, separator);
64  for (const std::string& s : rowStr)
65  {
66  row.push_back(toFloat(s));
67  }
68  return true;
69  }
70 
71  bool
72  CsvReader::read(std::map<std::string, float>& row)
73  {
74  row.clear();
75  std::string line;
76  if (!readLine(line))
77  {
78  return false;
79  }
80  std::vector<std::string> rowStr = Split(line, separator);
81  for (size_t i = 0; i < rowStr.size() && i < header.size(); i++)
82  {
83  row[header.at(i)] = toFloat(rowStr.at(i));
84  }
85  return true;
86  }
87 
88  std::vector<std::vector<float>>
90  {
91  std::vector<std::vector<float>> data;
92  std::vector<float> row;
93  while (read(row))
94  {
95  data.push_back(row);
96  }
97  return data;
98  }
99 
100  std::vector<std::map<std::string, float>>
102  {
103  std::vector<std::map<std::string, float>> data;
104  std::map<std::string, float> row;
105  while (read(row))
106  {
107  data.push_back(row);
108  }
109  return data;
110  }
111 
112  std::map<std::string, std::vector<float>>
114  {
115  std::map<std::string, std::vector<float>> data;
116  std::vector<float> row;
117  while (read(row))
118  {
119  for (size_t i = 0; i < row.size() && i < header.size(); i++)
120  {
121  data[header.at(i)].push_back(row.at(i));
122  }
123  }
124  return data;
125  }
126 
127  std::vector<std::string>
129  {
130  return header;
131  }
132 
133  bool
135  {
136  return file.eof();
137  }
138 
139  bool
140  CsvReader::readLine(std::string& line)
141  {
142  return static_cast<bool>(std::getline(file, line));
143  }
144 } // namespace armarx
armarx::CsvReader::getHeader
std::vector< std::string > getHeader()
Definition: CsvReader.cpp:128
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:36
armarx::toFloat
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
Definition: StringHelpers.cpp:100
armarx::CsvReader::isEof
bool isEof()
Definition: CsvReader.cpp:134
StringHelpers.h
armarx::CsvReader::readAllFloatList
std::vector< std::vector< float > > readAllFloatList()
Definition: CsvReader.cpp:89
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::CsvReader::readAllColumnsFloatMap
std::map< std::string, std::vector< float > > readAllColumnsFloatMap()
Definition: CsvReader.cpp:113
filename
std::string filename
Definition: VisualizationRobot.cpp:86
armarx::CsvReader::readAllFloatMap
std::vector< std::map< std::string, float > > readAllFloatMap()
Definition: CsvReader.cpp:101
armarx::CsvReader::read
bool read(std::vector< std::string > &row)
Definition: CsvReader.cpp:43
CsvReader.h
armarx::CsvReader::CsvReader
CsvReader(const std::string &filename, bool hasHeader)
Definition: CsvReader.cpp:30
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27