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