CsvWriter.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 "CsvWriter.h"
25 
26 namespace armarx
27 {
28  CsvWriter::CsvWriter(const std::string& filename, const std::vector<std::string>& header, bool append)
29  : autoflush(true), separator(",")
30  {
31  file.open(filename.c_str(), append ? std::ios_base::app : std::ios_base::out | std::ios_base::trunc);
32  file.imbue(std::locale::classic());
33  if (header.size() > 0)
34  {
35  write(header);
36  }
37  }
38 
39  void CsvWriter::write(const std::vector<std::string>& row)
40  {
41  bool first = true;
42  for (const std::string& s : row)
43  {
44  if (!first)
45  {
46  file << separator;
47  }
48  first = false;
49  file << s;
50  }
51  file << "\n";
52  if (autoflush)
53  {
54  file.flush();
55  }
56  }
57 
58  void CsvWriter::write(const std::vector<float>& row)
59  {
60  bool first = true;
61  for (float f : row)
62  {
63  if (!first)
64  {
65  file << separator;
66  }
67  first = false;
68  file << f;
69  }
70  file << "\n";
71  if (autoflush)
72  {
73  file.flush();
74  }
75  }
76 
78  {
79  file.flush();
80  file.close();
81  }
82 
84  {
85  if (file.is_open())
86  {
87  file.close();
88  }
89  }
90 }
CsvWriter.h
armarx::CsvWriter::close
void close()
Definition: CsvWriter.cpp:77
filename
std::string filename
Definition: VisualizationRobot.cpp:84
armarx::CsvWriter::write
void write(const std::vector< std::string > &row)
Definition: CsvWriter.cpp:39
armarx::CsvWriter::~CsvWriter
~CsvWriter()
Definition: CsvWriter.cpp:83
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
armarx::CsvWriter::CsvWriter
CsvWriter(const std::string &filename, const std::vector< std::string > &header, bool append)
Definition: CsvWriter.cpp:28