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"
26 
27 namespace armarx
28 {
29  CsvReader::CsvReader(const std::string& filename, bool hasHeader)
30  : 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 CsvReader::read(std::vector<std::string>& row)
43  {
44  std::string line;
45  if (!readLine(line))
46  {
47  return false;
48  }
49  row = Split(line, separator);
50  return true;
51  }
52 
53  bool CsvReader::read(std::vector<float>& row)
54  {
55  row.clear();
56  std::string line;
57  if (!readLine(line))
58  {
59  return false;
60  }
61  std::vector<std::string> rowStr = Split(line, separator);
62  for (const std::string& s : rowStr)
63  {
64  row.push_back(toFloat(s));
65  }
66  return true;
67  }
68 
69  bool CsvReader::read(std::map<std::string, float>& row)
70  {
71  row.clear();
72  std::string line;
73  if (!readLine(line))
74  {
75  return false;
76  }
77  std::vector<std::string> rowStr = Split(line, separator);
78  for (size_t i = 0; i < rowStr.size() && i < header.size(); i++)
79  {
80  row[header.at(i)] = toFloat(rowStr.at(i));
81  }
82  return true;
83  }
84 
85  std::vector<std::vector<float> > CsvReader::readAllFloatList()
86  {
87  std::vector<std::vector<float> > data;
88  std::vector<float> row;
89  while (read(row))
90  {
91  data.push_back(row);
92  }
93  return data;
94  }
95 
96  std::vector<std::map<std::string, float> > CsvReader::readAllFloatMap()
97  {
98  std::vector<std::map<std::string, float> > data;
99  std::map<std::string, float> row;
100  while (read(row))
101  {
102  data.push_back(row);
103  }
104  return data;
105  }
106 
107  std::map<std::string, std::vector<float> > CsvReader::readAllColumnsFloatMap()
108  {
109  std::map<std::string, std::vector<float> > data;
110  std::vector<float> row;
111  while (read(row))
112  {
113  for (size_t i = 0; i < row.size() && i < header.size(); i++)
114  {
115  data[header.at(i)].push_back(row.at(i));
116  }
117  }
118  return data;
119  }
120 
121  std::vector<std::string> CsvReader::getHeader()
122  {
123  return header;
124  }
125 
126 
128  {
129  return file.eof();
130  }
131 
132  bool CsvReader::readLine(std::string& line)
133  {
134  return static_cast<bool>(std::getline(file, line));
135  }
136 }
armarx::CsvReader::getHeader
std::vector< std::string > getHeader()
Definition: CsvReader.cpp:121
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
armarx::toFloat
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.
Definition: StringHelpers.cpp:97
armarx::CsvReader::isEof
bool isEof()
Definition: CsvReader.cpp:127
StringHelpers.h
armarx::CsvReader::readAllFloatList
std::vector< std::vector< float > > readAllFloatList()
Definition: CsvReader.cpp:85
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::CsvReader::readAllColumnsFloatMap
std::map< std::string, std::vector< float > > readAllColumnsFloatMap()
Definition: CsvReader.cpp:107
filename
std::string filename
Definition: VisualizationRobot.cpp:84
armarx::CsvReader::readAllFloatMap
std::vector< std::map< std::string, float > > readAllFloatMap()
Definition: CsvReader.cpp:96
armarx::CsvReader::read
bool read(std::vector< std::string > &row)
Definition: CsvReader.cpp:42
CsvReader.h
armarx::CsvReader::CsvReader
CsvReader(const std::string &filename, bool hasHeader)
Definition: CsvReader.cpp:29
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:28