-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvcc.h
182 lines (148 loc) · 2.87 KB
/
csvcc.h
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// CSVcc 0.1.0
// https://github.com/kuraga/csvcc
// Copyright (C) 2015, by Kurakin Alexander (kuraga333@mail.ru)
//
// The MIT License (MIT)
// http://opensource.org/licenses/MIT
// Minimalistic CSV Streams 1.4
// https://code.google.com/p/minicsv/
// Copyright (C) 2014, by Wong Shao Voon (shaovoon@yahoo.com)
//
// The MIT License (MIT)
// http://opensource.org/licenses/MIT
#ifndef _CSV_H
#define _CSV_H
#include <string>
#include <iostream>
namespace csv
{
class istream
{
public:
istream(std::istream &istm, char delimeter = ',')
: _istream(istm), _str(""), _pos(0), _delimeter(delimeter)
{
}
bool eof() const
{
return (_istream.eof()&&_str=="");
}
bool eol() const
{
return (_pos>=_str.size());
}
char get_delimeter() const
{
return _delimeter;
}
std::istream& get_istream()
{
return _istream;
}
void skip_line()
{
if(!_istream.eof())
{
std::getline(_istream, _str);
_pos = 0;
}
}
std::string get_delimited_str()
{
std::string str = "";
char ch = '\0';
do
{
if(_pos>=_str.size())
{
if(!_istream.eof())
{
std::getline(_istream, _str);
_pos = 0;
}
else
{
_str = "";
break;
}
if(!str.empty())
return str;
}
ch = _str[_pos];
++(_pos);
if(ch==_delimeter||ch=='\r'||ch=='\n')
break;
str += ch;
}
while(true);
return str;
}
private:
std::istream& _istream;
std::string _str;
size_t _pos;
char _delimeter;
};
class ostream
{
public:
ostream(std::ostream& ostr, char delimeter = ',', bool after_newline = true)
: _ostream(ostr), _after_newline(after_newline), _delimeter(delimeter)
{
}
char get_delimeter() const
{
return _delimeter;
}
bool get_after_newline() const
{
return _after_newline;
}
std::ostream& get_ostream()
{
return _ostream;
}
void set_after_newline(bool after_newline)
{
_after_newline = after_newline;
}
private:
std::ostream& _ostream;
bool _after_newline;
char _delimeter;
};
}
template<typename T>
inline csv::istream& operator >> (csv::istream& _istream, T& val)
{
std::string str = _istream.get_delimited_str();
return _istream;
}
template<>
inline csv::istream& operator >> (csv::istream& _istream, std::string& val)
{
val = _istream.get_delimited_str();
return _istream;
}
template<typename T>
inline csv::ostream& operator << (csv::ostream& _ostream, const T& val)
{
if(!_ostream.get_after_newline())
_ostream.get_ostream() << _ostream.get_delimeter();
_ostream.get_ostream() << val;
_ostream.set_after_newline(false);
return _ostream;
}
template<>
inline csv::ostream& operator << (csv::ostream& _ostream, const char& val)
{
if(val=='\n')
{
_ostream.get_ostream() << std::endl;
_ostream.set_after_newline(true);
}
else
_ostream.get_ostream() << val;
return _ostream;
}
#endif