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
28namespace 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
std::vector< std::vector< float > > readAllFloatList()
Definition CsvReader.cpp:89
std::vector< std::string > getHeader()
CsvReader(const std::string &filename, bool hasHeader)
Definition CsvReader.cpp:30
bool read(std::vector< std::string > &row)
Definition CsvReader.cpp:43
std::vector< std::map< std::string, float > > readAllFloatMap()
std::map< std::string, std::vector< float > > readAllColumnsFloatMap()
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
float toFloat(const std::string &input)
Converts a string to float and uses always dot as seperator.