-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector_ostream.h
120 lines (96 loc) · 2.63 KB
/
vector_ostream.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
#ifndef VECTOR_OSTREAM_H
#define VECTOR_OSTREAM_H
#include <string>
#include <streambuf>
#include <cstring>
#include <vector>
#include <iostream>
namespace amps
{
/*
* thanks to http://www.voidcn.com/article/p-vjnlygmc-gy.html
*/
class vector_ostreambuf : public std::streambuf
{
constexpr static size_t MAX_RING_SIZE = 256;
std::vector<std::string> data_;
char tempbuff_[MAX_RING_SIZE];
private:
int overflow(int c) final
{
if (c == traits_type::eof()) {
return c;
}
*pptr() = static_cast<char>(c);
pbump(1);
if (sync() == 0) {
return c;
}
return traits_type::eof();
}
int sync() final
{
char *start = pbase();
ptrdiff_t len = 0;
std::string str = std::string(start, pptr() - pbase());
for (char c : str) {
if (c != '\n') {
len++;
continue;
}
data_.push_back(std::string(start, len));
start += len + 1;
len = 0;
}
if (len > 0) {
data_.push_back(std::string(start, len));
}
pbump(-(pptr() - pbase()));
return 0;
}
public:
vector_ostreambuf()
{
memset(tempbuff_, 0, sizeof(char) * MAX_RING_SIZE);
setp(tempbuff_, tempbuff_ + sizeof(char) * MAX_RING_SIZE);
}
~vector_ostreambuf()
{
sync();
}
vector_ostreambuf(const vector_ostreambuf&) = delete;
vector_ostreambuf(vector_ostreambuf&&) = delete;
vector_ostreambuf &operator=(const vector_ostreambuf&) = delete;
vector_ostreambuf &operator=(vector_ostreambuf&&) = delete;
const std::vector<std::string> get_errors() const
{
return data_;
}
std::string get_str(size_t index) const
{
if (index >= data_.size()) {
return std::string("");
}
return data_[index];
}
std::string get_first_str() const
{
if (data_.size() == 0) {
return std::string("");
}
return data_.front();
}
std::string get_last_str() const
{
if (data_.size() == 0) {
return std::string("");
}
return data_.back();
}
void clear()
{
data_.clear();
}
};
}
#endif // VECTOR_OSTREAM_H