-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindenter.hpp
220 lines (197 loc) · 6.72 KB
/
indenter.hpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#pragma once
#ifndef __INDENTER_HPP__
#define __INDENTER_HPP__
#include <iostream>
#include <sstream>
#include <vector>
namespace indent {
class indentbuf: public std::streambuf {
public:
static const int idx;
indentbuf(std::ostream& orig, size_t indent)
: orig(orig),
orig_buf(orig.rdbuf()),
comment_on(false),
suppress(false),
line_indent(indent),
wrap(70),
lookback(30),
string_wrap_indent(2)
{
orig.rdbuf(this);
update_prefix();
}
virtual ~indentbuf() {
pubsync();
orig.rdbuf(orig_buf);
}
// This one needs to be int_type so that it can be + or - to the current indent.
void adjust_indent(int_type adj) {
pubsync();
line_indent += adj;
update_prefix();
}
void set_indent(size_t val) {
pubsync();
line_indent = val;
update_prefix();
}
void push_indent(size_t val) {
pubsync();
line_indent_stack.push_back(line_indent);
line_indent = val;
update_prefix();
}
void pop_indent() {
pubsync();
if (line_indent_stack.size() == 0) return;
line_indent = line_indent_stack.back();
line_indent_stack.pop_back();
update_prefix();
}
void set_wrap(size_t wrap_val, size_t lookback_val, size_t string_wrap_indent_val=2) {
pubsync();
wrap = wrap_val;
lookback = lookback_val;
string_wrap_indent = string_wrap_indent_val;
}
void set_suppress(bool suppress_val) {
pubsync();
suppress = suppress_val;
}
// Comment will only last until the next natural newline.
void comment() {
pubsync();
comment_on = true;
}
protected:
int_type overflow(int_type ch) {
// If we get an eof just return the right stuff.
if (traits_type::eq_int_type(ch, traits_type::eof())) {
return traits_type::not_eof(ch);
}
if (suppress) {
return orig_buf->sputc(ch);
}
buffer += ch;
if (ch == '\n' || ch == '\r') {
buffer = prefix + buffer;
// Ok.. we just got a new line, so output the buffer in wrapped chunks.
while (buffer.size() > wrap) {
// find the last space before the wrap point.
size_t wrap_pos = buffer.find_last_of("\n ", wrap);
size_t extra = 1;
// If we didn't find a space, just wrap at the wrap point.
if (wrap_pos == std::string::npos || (wrap - wrap_pos > lookback)) { wrap_pos = wrap; extra = 0; }
if (comment_on) orig_buf->sputc(';');
orig_buf->sputn(buffer.c_str(), wrap_pos);
orig_buf->sputc('\n');
buffer = buffer.substr(wrap_pos + extra);
// If the remaining buffer is just a newline, clear it
if (buffer == "\n") buffer.clear();
if (buffer.size() > 0) {
buffer = prefix + std::string(string_wrap_indent, ' ') + buffer;
}
}
if (buffer.size() > 0) {
if (comment_on) orig_buf->sputc(';');
orig_buf->sputn(buffer.c_str(), buffer.size());
}
buffer.clear();
// all output, turn off comment now.
comment_on = false;
}
return ch;
}
int_type sync(void) {
if (buffer.size() > 0) {
if (comment_on) orig_buf->sputc(';');
buffer = prefix + buffer;
orig_buf->sputn(buffer.c_str(), buffer.size());
buffer.clear();
}
return orig_buf->pubsync();
}
private:
void update_prefix() {
if (line_indent < 0) line_indent = 0;
// Just make sure we never get a huge indent.
if (line_indent > 30) line_indent = 30;
if (line_indent) prefix = std::string(line_indent, ' ');
else prefix.clear();
}
std::ostream& orig;
std::streambuf *orig_buf;
bool comment_on;
bool suppress;
int_type line_indent;
size_t wrap;
size_t lookback;
size_t string_wrap_indent;
std::string buffer;
std::string prefix;
std::vector<int_type> line_indent_stack;
};
std::ostream &wrap(std::ostream &os);
std::ostream &comment(std::ostream &os);
std::ostream &incr(std::ostream &os);
std::ostream &decr(std::ostream &os);
std::ostream &clear(std::ostream &os);
struct wrap_data { size_t wrap; size_t lookback; size_t string_wrap_indent; };
inline wrap_data wrap(size_t wrap=70, size_t lookback=30, size_t string_wrap_indent=2) {
return { wrap, lookback, string_wrap_indent };
}
template<typename _CharT, typename _Traits>
inline std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& os, wrap_data data) {
if(os.pword(indentbuf::idx) == nullptr) {
indentbuf *newbuf = new indentbuf(os, 0);
newbuf->set_wrap(data.wrap, data.lookback, data.string_wrap_indent);
os.pword(indentbuf::idx) = newbuf;
} else {
indentbuf *buf = static_cast<indentbuf *>(os.pword(indentbuf::idx));
buf->set_wrap(data.wrap, data.lookback, data.string_wrap_indent);
}
return os;
}
struct indent_data { size_t indent; };
inline indent_data set_indent(size_t indent=2) { return { indent }; }
template<typename _CharT, typename _Traits>
inline std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& os, indent_data data) {
if(os.pword(indentbuf::idx) == nullptr) {
indentbuf *newbuf = new indentbuf(os, data.indent);
os.pword(indentbuf::idx) = newbuf;
} else {
indentbuf *buf = static_cast<indentbuf *>(os.pword(indentbuf::idx));
buf->set_indent(data.indent);
}
return os;
}
struct suppress_data { bool suppress; };
inline suppress_data suppress_format(bool suppress) { return { suppress }; }
template<typename _CharT, typename _Traits>
inline std::basic_ostream<_CharT, _Traits>&operator<<(std::basic_ostream<_CharT, _Traits>& os, suppress_data data) {
if(os.pword(indentbuf::idx) == nullptr) {
indentbuf *newbuf = new indentbuf(os, 0);
os.pword(indentbuf::idx) = newbuf;
}
indentbuf *buf = static_cast<indentbuf *>(os.pword(indentbuf::idx));
buf->set_suppress(data.suppress);
return os;
}
struct push_indent_data { size_t indent; bool push; };
inline push_indent_data push_indent(size_t indent) { return { indent, true }; }
inline push_indent_data pop_indent() { return { 0, false }; }
template<typename _CharT, typename _Traits>
inline std::basic_ostream<_CharT, _Traits>& operator<<(
std::basic_ostream<_CharT, _Traits>& os, push_indent_data data
) {
if(os.pword(indentbuf::idx) == nullptr) {
indentbuf *newbuf = new indentbuf(os, 0);
os.pword(indentbuf::idx) = newbuf;
}
indentbuf *buf = static_cast<indentbuf *>(os.pword(indentbuf::idx));
(data.push ? buf->push_indent(data.indent) : buf->pop_indent());
return os;
}
}
#endif