-
Notifications
You must be signed in to change notification settings - Fork 6
/
CsvWriter.cpp
69 lines (57 loc) · 1.22 KB
/
CsvWriter.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
#include "CsvWriter.hpp"
#include <iomanip>
#include <cstring>
using namespace std;
namespace csv {
CsvWriter::CsvWriter(const string &path)
: out(path)
, firstWordInLine(true)
{
if (!out.good()) {
cout << "\nOh no, I can not create file: '" << path << "'." << std::endl;
cout << "abooooooorting !!!!!!" << std::endl;
exit(-1);
}
}
void CsvWriter::prePrint()
{
if (firstWordInLine) {
firstWordInLine = false;
} else {
out << ',';
}
}
CsvWriter &CsvWriter::printString(const char *str, int len)
{
prePrint();
out.write(str, std::min((size_t) len, strnlen(str, len)));
return *this;
}
CsvWriter &operator<<(CsvWriter &csv, int64_t num)
{
csv.prePrint();
csv.out << num;
return csv;
}
CsvWriter &operator<<(CsvWriter &csv, float num)
{
csv.prePrint();
csv.out << fixed << num;
return csv;
}
CsvWriter &operator<<(CsvWriter &csv, const std::string &str)
{
return csv.printString(str.c_str(), str.size());
}
CsvWriter &operator<<(CsvWriter &csv, EndlStruct)
{
csv.out << "\n";
csv.firstWordInLine = true;
return csv;
}
CsvWriter &operator<<(CsvWriter &csv, Precision precision)
{
csv.out << std::setprecision(precision.p);
return csv;
}
}