-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsv_writer.cpp
More file actions
162 lines (119 loc) · 3.42 KB
/
Copy pathCsv_writer.cpp
File metadata and controls
162 lines (119 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Copyright (C) 2024 Pierre BLAVY
This program (cpp-csv) is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "Csv_writer.hpp"
#include <fstream>
namespace csv{
Csv_writer::Csv_writer(char sep_, char endl_):sep(sep_),endl(endl_){};
Csv_writer::~Csv_writer(){ if(own_out){ try{delete out;}catch(...){} } }
void Csv_writer::set_write(std::ostream &out_, const std::string &name_){
auto finally=[&,this](){
line_count=0;
own_out = false;
out = &out_;
name=name_;
check();
};
if(own_out){
try{
delete out;
}catch(...){
finally();
throw;
}
}
finally();
}
void Csv_writer::set_write(const std::filesystem::path &p){
auto finally=[&,this](){
line_count=0;
name=p.generic_string();
own_out = true;
out = new std::ofstream(p);
if(out==nullptr){throw std::runtime_error("Error in Csv_writer : cannot create new ofstream, name="+name);}
check();
};
if(own_out){
try{
delete out;
out=nullptr; //(1)
}catch(...){
out=nullptr;//(1)
finally();
throw;
}
//(1) important, as new std::ofstream(p); may throw, we don't want to delete the old out value twice
}
finally();
}
size_t Csv_writer::get_column(const std::string &col_name)const{
auto f = colname_to_index.find(col_name);
if(f==colname_to_index.end()){
throw std::runtime_error("Error in Csv_writer : the column doesn't exists, colname="+col_name+", name="+name);
}
return f->second;
}
void Csv_writer::write_header(){
auto b = std::cbegin(header_v);
auto e = std::cend (header_v);
if(b != e){*out << *(*b); ++b;}
while(b!=e){
*out<< sep<< *(*b);
++b;
}
*out << endl;
++line_count;
col_count=0;
}
void Csv_writer::write_endl(){
//write line, set all line_v value to "";
auto b = std::begin(line_v);
auto e = std::end (line_v);
if(b != e){*out << (*b); *b=""; ++b;}
while(b!=e){
*out<< sep<< (*b);
*b="";
++b;
}
*out << endl;
++line_count;
col_count=0;
}
void Csv_writer::close(){
if(own_out){
try{
delete out;
out=nullptr;
own_out=false;
}catch(...){
out=nullptr;
own_out=false;
throw;
}
}
name="<closed "+name+">";
std::fill(line_v.begin(), line_v.end(), "");
line_count=0;
col_count=0;
}
void Csv_writer::reset(){
close();
header_v.clear();
line_v.clear();
colname_to_index.clear();
}
void Csv_writer::check(){
if(!*out){throw std::runtime_error("Error in Csv_writer : cannot write, name="+name);}
};
}//end csv