-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv.cpp
80 lines (67 loc) · 1.33 KB
/
csv.cpp
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
#include "csv.h"
#include <utility>
#include <string>
template<typename d>
struct Specific_Item : public Item
{
d data;
Specific_Item( d ndata ): data(ndata) { }
void write( std::stringstream & out ) { out << data; }
};
typedef Specific_Item<int> int_Item;
typedef Specific_Item<double> double_Item;
typedef Specific_Item<std::string> string_Item;
void row::write( std::ostream & out )
{
std::stringstream ss;
for(auto && item : Items)
{
item->write(ss);
ss << ',';
}
ss <<'\n';
out << ss.str();
}
void row::append( Item * item )
{
std::unique_ptr<Item> item_ptr(item);
Items.push_back( std::move(item_ptr) );
}
void CSV::create_row()
{
rows.push_back( row() );
}
bool CSV::append(unsigned int sel, int data)
{
if( sel < rows.size() )
{
rows[sel].append(new int_Item(data));
return true;
}
return false;
}
bool CSV::append(unsigned int sel, double data)
{
if( sel < rows.size() )
{
rows[sel].append(new double_Item(data));
return true;
}
return false;
}
bool CSV::append(unsigned int sel, std::string data)
{
if( sel < rows.size() )
{
rows[sel].append(new string_Item(data));
return true;
}
return false;
}
void CSV::write( std::ostream & out )
{
for( auto & r : rows)
{
r.write(out);
}
}